SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Sum.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Soumyajit De
4  * Written (w) 2014 Khaled Nasr
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
32 #ifndef SUM_IMPL_H_
33 #define SUM_IMPL_H_
34 
35 #include <shogun/lib/config.h>
36 #include <shogun/lib/SGMatrix.h>
37 #include <shogun/lib/SGVector.h>
38 #include <shogun/io/SGIO.h>
40 
42 
43 #ifdef HAVE_VIENNACL
45 #include <shogun/lib/GPUMatrix.h>
46 #include <shogun/lib/GPUVector.h>
47 #endif
48 
49 #include <string>
50 
51 namespace shogun
52 {
53 
54 namespace linalg
55 {
56 
60 namespace implementation
61 {
62 
68 template <enum Backend,class Matrix>
69 struct sum
70 {
72  typedef typename Matrix::Scalar T;
73 
81  static T compute(Matrix m, bool no_diag);
82 
90  static T compute(Block<Matrix> b, bool no_diag);
91 };
92 
98 template <enum Backend,class Matrix>
100 {
102  typedef typename Matrix::Scalar T;
103 
111  static T compute(Matrix m, bool no_diag);
112 
120  static T compute(Block<Matrix> b, bool no_diag);
121 };
122 
128 template <enum Backend,class Matrix>
130 {
132  typedef typename Matrix::Scalar T;
133 
141  static SGVector<T> compute(Matrix m, bool no_diag);
142 
150  static SGVector<T> compute(Block<Matrix> b, bool no_diag);
151 
159  static void compute(SGMatrix<T> m, SGVector<T> result, bool no_diag);
160 
169  static void compute(Block<SGMatrix<T> > b, SGVector<T> result, bool no_diag);
170 };
171 
177 template <enum Backend,class Matrix>
179 {
181  typedef typename Matrix::Scalar T;
182 
190  static SGVector<T> compute(Matrix m, bool no_diag);
191 
199  static SGVector<T> compute(Block<Matrix> b, bool no_diag);
200 
208  static void compute(SGMatrix<T> m, SGVector<T> result, bool no_diag);
209 
218  static void compute(Block<SGMatrix<T> > b, SGVector<T> result, bool no_diag);
219 };
220 
225 template <class Matrix>
226 struct sum<Backend::EIGEN3,Matrix>
227 {
229  typedef typename Matrix::Scalar T;
230 
233 
241  static T compute(SGMatrix<T> mat, bool no_diag)
242  {
243  Eigen::Map<MatrixXt> m = mat;
244 
245  T sum=m.sum();
246 
247  // remove the main diagonal elements if required
248  if (no_diag)
249  sum-=m.diagonal().sum();
250 
251  return sum;
252  }
253 
261  static T compute(Block<SGMatrix<T> > b, bool no_diag)
262  {
263  Eigen::Map<MatrixXt> map = b.m_matrix;
264 
265  Eigen::Block< Eigen::Map<MatrixXt> > b_eigen = map.block(
266  b.m_row_begin, b.m_col_begin,
267  b.m_row_size, b.m_col_size);
268 
269  T sum=b_eigen.sum();
270 
271  // remove the main diagonal elements if required
272  if (no_diag)
273  sum-=b_eigen.diagonal().sum();
274 
275  return sum;
276  }
277 };
278 
283 template <class Matrix>
284 struct sum_symmetric<Backend::EIGEN3,Matrix>
285 {
287  typedef typename Matrix::Scalar T;
288 
291 
299  static T compute(SGMatrix<T> mat, bool no_diag)
300  {
301  Eigen::Map<MatrixXt> m = mat;
302 
303  REQUIRE(m.rows()==m.cols(), "Matrix is not square!\n");
304 
305  // since the matrix is symmetric with main diagonal inside, we can save half
306  // the computation with using only the upper triangular part.
307  const MatrixXt& m_upper=m.template triangularView<Eigen::StrictlyUpper>();
308  T sum=m_upper.sum();
309 
310  // the actual sum would be twice of what we computed
311  sum*=2;
312 
313  // add the diagonal elements if required
314  if (!no_diag)
315  sum+=m.diagonal().sum();
316 
317  return sum;
318  }
319 
327  static T compute(Block<SGMatrix<T> > b, bool no_diag)
328  {
329  Eigen::Map<MatrixXt> map = b.m_matrix;
330 
331  const MatrixXt& m=map.template block(b.m_row_begin, b.m_col_begin,
332  b.m_row_size, b.m_col_size);
333 
334  REQUIRE(m.rows()==m.cols(), "Matrix is not square!\n");
335 
336  // since the matrix is symmetric with main diagonal inside, we can save half
337  // the computation with using only the upper triangular part.
338  const MatrixXt& m_upper=m.template triangularView<Eigen::StrictlyUpper>();
339  T sum=m_upper.sum();
340 
341  // the actual sum would be twice of what we computed
342  sum*=2;
343 
344  // add the diagonal elements if required
345  if (!no_diag)
346  sum+=m.diagonal().sum();
347 
348  return sum;
349  }
350 };
351 
356 template <class Matrix>
357 struct colwise_sum<Backend::EIGEN3,Matrix>
358 {
360  typedef typename Matrix::Scalar T;
361 
364 
367 
370 
378  static SGVector<T> compute(SGMatrix<T> m, bool no_diag)
379  {
380  SGVector<T> result(m.num_cols);
381  compute(m, result, no_diag);
382  return result;
383  }
384 
393  static SGVector<T> compute(Block<SGMatrix<T> > b, bool no_diag)
394  {
395  SGVector<T> result(b.m_col_size);
396  compute(b, result, no_diag);
397  return result;
398  }
399 
407  static void compute(SGMatrix<T> mat, SGVector<T> result, bool no_diag)
408  {
409  Eigen::Map<MatrixXt> m = mat;
410  Eigen::Map<VectorXt> r = result;
411 
412  r = m.colwise().sum();
413 
414  // remove the main diagonal elements if required
415  if (no_diag)
416  {
417  index_t len_major_diag=m.rows() < m.cols() ? m.rows() : m.cols();
418  for (index_t i=0; i<len_major_diag; ++i)
419  r[i]-=m(i,i);
420  }
421  }
422 
431  static void compute(Block<SGMatrix<T> > b, SGVector<T> result, bool no_diag)
432  {
433  Eigen::Map<MatrixXt> map = b.m_matrix;
434  Eigen::Map<VectorXt> r = result;
435 
436  Eigen::Block< Eigen::Map<MatrixXt> > m = map.block(
437  b.m_row_begin, b.m_col_begin,
438  b.m_row_size, b.m_col_size);
439 
440  r = m.colwise().sum();
441 
442  // remove the main diagonal elements if required
443  if (no_diag)
444  {
445  index_t len_major_diag=m.rows() < m.cols() ? m.rows() : m.cols();
446  for (index_t i=0; i<len_major_diag; ++i)
447  r[i]-=m(i,i);
448  }
449  }
450 };
451 
456 template <class Matrix>
457 struct rowwise_sum<Backend::EIGEN3,Matrix>
458 {
460  typedef typename Matrix::Scalar T;
461 
464 
467 
470 
478  static SGVector<T> compute(SGMatrix<T> m, bool no_diag)
479  {
480  SGVector<T> result(m.num_rows);
481  compute(m, result, no_diag);
482  return result;
483  }
484 
493  static SGVector<T> compute(Block<SGMatrix<T> > b, bool no_diag)
494  {
495  SGVector<T> result(b.m_row_size);
496  compute(b, result, no_diag);
497  return result;
498  }
499 
507  static void compute(SGMatrix<T> mat, SGVector<T> result, bool no_diag)
508  {
509  Eigen::Map<MatrixXt> m = mat;
510  Eigen::Map<VectorXt> r = result;
511 
512  r = m.rowwise().sum();
513 
514  // remove the main diagonal elements if required
515  if (no_diag)
516  {
517  index_t len_major_diag=m.rows() < m.cols() ? m.rows() : m.cols();
518  for (index_t i=0; i<len_major_diag; ++i)
519  r[i]-=m(i,i);
520  }
521  }
522 
531  static void compute(Block<SGMatrix<T> > b, SGVector<T> result, bool no_diag)
532  {
533  Eigen::Map<MatrixXt> map = b.m_matrix;
534  Eigen::Map<VectorXt> r = result;
535 
536  Eigen::Block< Eigen::Map<MatrixXt> > m = map.block(
537  b.m_row_begin, b.m_col_begin,
538  b.m_row_size, b.m_col_size);
539 
540  r = m.rowwise().sum();
541 
542  // remove the main diagonal elements if required
543  if (no_diag)
544  {
545  index_t len_major_diag=m.rows() < m.cols() ? m.rows() : m.cols();
546  for (index_t i=0; i<len_major_diag; ++i)
547  r[i]-=m(i,i);
548  }
549  }
550 };
551 
552 
553 #ifdef HAVE_VIENNACL
554 
558 template <class Matrix>
559 struct sum<Backend::VIENNACL,Matrix>
560 {
562  typedef typename Matrix::Scalar T;
563 
565  template <class T>
566  static viennacl::ocl::kernel& generate_kernel(bool no_diag)
567  {
568  std::string kernel_name = "sum_" + ocl::get_type_string<T>();
569  if (no_diag) kernel_name.append("_no_diag");
570 
571  if (ocl::kernel_exists(kernel_name))
572  return ocl::get_kernel(kernel_name);
573 
574  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
575  if (no_diag) source.append("#define NO_DIAG\n");
576 
577  source.append(
578  R"(
579  __kernel void KERNEL_NAME(
580  __global DATATYPE* mat, int nrows, int ncols, int offset,
581  __global DATATYPE* result)
582  {
583  __local DATATYPE buffer[WORK_GROUP_SIZE_1D];
584  int size = nrows*ncols;
585 
586  int local_id = get_local_id(0);
587 
588  DATATYPE thread_sum = 0;
589  for (int i=local_id; i<size; i+=WORK_GROUP_SIZE_1D)
590  {
591  #ifdef NO_DIAG
592  if (!(i/nrows == i%nrows))
593  #endif
594  thread_sum += mat[i+offset];
595  }
596 
597  buffer[local_id] = thread_sum;
598 
599  for (int j = WORK_GROUP_SIZE_1D/2; j > 0; j = j>>1)
600  {
601  barrier(CLK_LOCAL_MEM_FENCE);
602  if (local_id < j)
603  buffer[local_id] += buffer[local_id + j];
604  }
605 
606  barrier(CLK_LOCAL_MEM_FENCE);
607 
608  if (get_global_id(0)==0)
609  *result = buffer[0];
610  }
611  )"
612  );
613 
614  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
615 
616  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
617  kernel.global_work_size(0, OCL_WORK_GROUP_SIZE_1D);
618 
619  return kernel;
620  }
621 
629  static T compute(CGPUMatrix<T> mat, bool no_diag)
630  {
631  viennacl::ocl::kernel& kernel = generate_kernel<T>(no_diag);
632 
633  CGPUVector<T> result(1);
634 
635  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
636  cl_int(mat.num_rows), cl_int(mat.num_cols), cl_int(mat.offset),
637  result.vcl_vector()));
638 
639  return result[0];
640  }
641 
649  static T compute(Block<CGPUMatrix<T> > b, bool no_diag)
650  {
651  SG_SERROR("The operation sum() on a matrix block is currently not supported\n");
652  return 0;
653  }
654 };
655 
660 template <class Matrix>
661 struct sum_symmetric<Backend::VIENNACL,Matrix>
662 {
664  typedef typename Matrix::Scalar T;
665 
673  static T compute(CGPUMatrix<T> mat, bool no_diag)
674  {
675  return sum<Backend::VIENNACL, CGPUMatrix<T> >::compute(mat, no_diag);
676  }
677 
685  static T compute(Block<CGPUMatrix<T> > b, bool no_diag)
686  {
687  SG_SERROR("The operation sum_symmetric() on a matrix block is currently not supported\n");
688  return 0;
689  }
690 };
691 
696 template <class Matrix>
697 struct colwise_sum<Backend::VIENNACL,Matrix>
698 {
700  typedef typename Matrix::Scalar T;
701 
703  typedef CGPUVector<T> ReturnType;
704 
706  template <class T>
707  static viennacl::ocl::kernel& generate_kernel(bool no_diag)
708  {
709  std::string kernel_name = "colwise_sum_" + ocl::get_type_string<T>();
710  if (no_diag) kernel_name.append("_no_diag");
711 
712  if (ocl::kernel_exists(kernel_name))
713  return ocl::get_kernel(kernel_name);
714 
715  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
716  if (no_diag) source.append("#define NO_DIAG\n");
717 
718  source.append(
719  R"(
720  __kernel void KERNEL_NAME(
721  __global DATATYPE* mat, int nrows, int ncols, int offset,
722  __global DATATYPE* result, int result_offset)
723  {
724  int j = get_global_id(0);
725 
726  if (j>=ncols)
727  return;
728 
729  DATATYPE sum = 0;
730  for (int i=0; i<nrows; i++)
731  {
732  #ifdef NO_DIAG
733  if (i!=j)
734  #endif
735  sum += mat[offset+i+j*nrows];
736  }
737 
738  result[j+result_offset] = sum;
739  }
740  )"
741  );
742 
743  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
744 
745  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
746 
747  return kernel;
748  }
749 
757  static CGPUVector<T> compute(CGPUMatrix<T> m, bool no_diag)
758  {
759  CGPUVector<T> result(m.num_cols);
760  compute(m, result, no_diag);
761  return result;
762  }
763 
772  static CGPUVector<T> compute(Block<CGPUMatrix<T> > b, bool no_diag)
773  {
774  SG_SERROR("The operation colwise_sum() on a matrix block is currently not supported\n");
775  return CGPUVector<T>();
776  }
777 
785  static void compute(CGPUMatrix<T> mat, CGPUVector<T> result, bool no_diag)
786  {
787  viennacl::ocl::kernel& kernel = generate_kernel<T>(no_diag);
788  kernel.global_work_size(0, ocl::align_to_multiple_1d(mat.num_cols));
789 
790  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
791  cl_int(mat.num_rows), cl_int(mat.num_cols), cl_int(mat.offset),
792  result.vcl_vector(), cl_int(result.offset)));
793  }
794 
803  static void compute(Block<CGPUMatrix<T> > b, CGPUVector<T> result, bool no_diag)
804  {
805  SG_SERROR("The operation colwise_sum() on a matrix block is currently not supported\n");
806  }
807 };
808 
813 template <class Matrix>
814 struct rowwise_sum<Backend::VIENNACL,Matrix>
815 {
817  typedef typename Matrix::Scalar T;
818 
820  typedef CGPUVector<T> ReturnType;
821 
823  template <class T>
824  static viennacl::ocl::kernel& generate_kernel(bool no_diag)
825  {
826  std::string kernel_name = "rowwise_sum_" + ocl::get_type_string<T>();
827  if (no_diag) kernel_name.append("_no_diag");
828 
829  if (ocl::kernel_exists(kernel_name))
830  return ocl::get_kernel(kernel_name);
831 
832  std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
833  if (no_diag) source.append("#define NO_DIAG\n");
834 
835  source.append(
836  R"(
837  __kernel void KERNEL_NAME(
838  __global DATATYPE* mat, int nrows, int ncols, int offset,
839  __global DATATYPE* result, int result_offset)
840  {
841  int i = get_global_id(0);
842 
843  if (i>=nrows)
844  return;
845 
846  DATATYPE sum = 0;
847  for (int j=0; j<ncols; j++)
848  {
849  #ifdef NO_DIAG
850  if (i!=j)
851  #endif
852  sum += mat[offset+i+j*nrows];
853  }
854 
855  result[i+result_offset] = sum;
856  }
857  )"
858  );
859 
860  viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
861 
862  kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
863 
864  return kernel;
865  }
866 
874  static CGPUVector<T> compute(CGPUMatrix<T> m, bool no_diag)
875  {
876  CGPUVector<T> result(m.num_rows);
877  compute(m, result, no_diag);
878  return result;
879  }
880 
889  static CGPUVector<T> compute(Block<CGPUMatrix<T> > b, bool no_diag)
890  {
891  SG_SERROR("The operation rowwise_sum() on a matrix block is currently not supported\n");
892  return CGPUVector<T>();
893  }
894 
902  static void compute(CGPUMatrix<T> mat, CGPUVector<T> result, bool no_diag)
903  {
904  viennacl::ocl::kernel& kernel = generate_kernel<T>(no_diag);
905  kernel.global_work_size(0, ocl::align_to_multiple_1d(mat.num_rows));
906 
907  viennacl::ocl::enqueue(kernel(mat.vcl_matrix(),
908  cl_int(mat.num_rows), cl_int(mat.num_cols), cl_int(mat.offset),
909  result.vcl_vector(), cl_int(result.offset)));
910  }
911 
920  static void compute(Block<CGPUMatrix<T> > b, CGPUVector<T> result, bool no_diag)
921  {
922  SG_SERROR("The operation rowwise_sum() on a matrix block is currently not supported\n");
923  }
924 };
925 
926 #endif // HAVE_VIENNACL
927 
928 }
929 
930 }
931 
932 }
933 #endif // SUM_IMPL_H_
Generic class colwise_sum which provides a static compute method. This class is specialized for diffe...
Definition: Sum.h:129
static SGVector< T > compute(Matrix m, bool no_diag)
static SGVector< T > compute(SGMatrix< T > m, bool no_diag)
Definition: Sum.h:478
static void compute(SGMatrix< T > mat, SGVector< T > result, bool no_diag)
Definition: Sum.h:407
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Sum.h:232
Generic class rowwise_sum which provides a static compute method. This class is specialized for diffe...
Definition: Sum.h:178
static T compute(Matrix m, bool no_diag)
static void compute(Block< SGMatrix< T > > b, SGVector< T > result, bool no_diag)
Definition: Sum.h:531
int32_t index_t
Definition: common.h:62
static T compute(SGMatrix< T > mat, bool no_diag)
Definition: Sum.h:241
static T compute(SGMatrix< T > mat, bool no_diag)
Definition: Sum.h:299
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:376
Generic class Block which wraps a matrix class and contains block specific information, providing a uniform way to deal with matrix blocks for all supported backend matrices.
Definition: Block.h:49
static void compute(SGMatrix< T > mat, SGVector< T > result, bool no_diag)
Definition: Sum.h:507
index_t num_rows
Definition: SGMatrix.h:374
Generic class sum which provides a static compute method. This class is specialized for different typ...
Definition: Sum.h:69
shogun matrix
static T compute(Matrix m, bool no_diag)
static SGVector< T > compute(Block< SGMatrix< T > > b, bool no_diag)
Definition: Sum.h:393
shogun vector
static SGVector< T > compute(Matrix m, bool no_diag)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Sum.h:366
#define SG_SERROR(...)
Definition: SGIO.h:179
static T compute(Block< SGMatrix< T > > b, bool no_diag)
Definition: Sum.h:261
static SGVector< T > compute(SGMatrix< T > m, bool no_diag)
Definition: Sum.h:378
static T compute(Block< SGMatrix< T > > b, bool no_diag)
Definition: Sum.h:327
static void compute(Block< SGMatrix< T > > b, SGVector< T > result, bool no_diag)
Definition: Sum.h:431
static SGVector< T > compute(Block< SGMatrix< T > > b, bool no_diag)
Definition: Sum.h:493
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Sum.h:466
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: Sum.h:290
Block< Matrix > block(Matrix matrix, index_t row_begin, index_t col_begin, index_t row_size, index_t col_size)
Definition: Block.h:102
Generic class sum symmetric which provides a static compute method. This class is specialized for dif...
Definition: Sum.h:99

SHOGUN Machine Learning Toolbox - Documentation