60 namespace implementation
68 template <enum Backend,
class Matrix>
72 typedef typename Matrix::Scalar
T;
81 static T
compute(Matrix m,
bool no_diag);
98 template <enum Backend,
class Matrix>
102 typedef typename Matrix::Scalar
T;
111 static T
compute(Matrix m,
bool no_diag);
128 template <enum Backend,
class Matrix>
132 typedef typename Matrix::Scalar
T;
177 template <enum Backend,
class Matrix>
181 typedef typename Matrix::Scalar
T;
225 template <
class Matrix>
226 struct sum<Backend::EIGEN3,Matrix>
229 typedef typename Matrix::Scalar
T;
249 sum-=m.diagonal().sum();
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);
273 sum-=b_eigen.diagonal().sum();
283 template <
class Matrix>
287 typedef typename Matrix::Scalar
T;
303 REQUIRE(m.rows()==m.cols(),
"Matrix is not square!\n");
307 const MatrixXt& m_upper=m.template triangularView<Eigen::StrictlyUpper>();
315 sum+=m.diagonal().sum();
331 const MatrixXt& m=map.template
block(b.m_row_begin, b.m_col_begin,
332 b.m_row_size, b.m_col_size);
334 REQUIRE(m.rows()==m.cols(),
"Matrix is not square!\n");
338 const MatrixXt& m_upper=m.template triangularView<Eigen::StrictlyUpper>();
346 sum+=m.diagonal().sum();
356 template <
class Matrix>
360 typedef typename Matrix::Scalar
T;
412 r = m.colwise().sum();
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)
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);
440 r = m.colwise().sum();
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)
456 template <
class Matrix>
460 typedef typename Matrix::Scalar
T;
512 r = m.rowwise().sum();
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)
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);
540 r = m.rowwise().sum();
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)
558 template <
class Matrix>
559 struct sum<Backend::VIENNACL,Matrix>
562 typedef typename Matrix::Scalar
T;
566 static viennacl::ocl::kernel& generate_kernel(
bool no_diag)
568 std::string kernel_name =
"sum_" + ocl::get_type_string<T>();
569 if (no_diag) kernel_name.append(
"_no_diag");
571 if (ocl::kernel_exists(kernel_name))
572 return ocl::get_kernel(kernel_name);
574 std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
575 if (no_diag) source.append(
"#define NO_DIAG\n");
579 __kernel void KERNEL_NAME(
580 __global DATATYPE* mat, int nrows, int ncols, int offset,
581 __global DATATYPE* result)
583 __local DATATYPE buffer[WORK_GROUP_SIZE_1D];
584 int size = nrows*ncols;
586 int local_id = get_local_id(0);
588 DATATYPE thread_sum = 0;
589 for (int i=local_id; i<size; i+=WORK_GROUP_SIZE_1D)
592 if (!(i/nrows == i%nrows))
594 thread_sum += mat[i+offset];
597 buffer[local_id] = thread_sum;
599 for (int j = WORK_GROUP_SIZE_1D/2; j > 0; j = j>>1)
601 barrier(CLK_LOCAL_MEM_FENCE);
603 buffer[local_id] += buffer[local_id + j];
606 barrier(CLK_LOCAL_MEM_FENCE);
608 if (get_global_id(0)==0)
614 viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
616 kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
617 kernel.global_work_size(0, OCL_WORK_GROUP_SIZE_1D);
629 static T
compute(CGPUMatrix<T> mat,
bool no_diag)
631 viennacl::ocl::kernel& kernel = generate_kernel<T>(no_diag);
633 CGPUVector<T> result(1);
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()));
649 static T
compute(Block<CGPUMatrix<T> > b,
bool no_diag)
651 SG_SERROR(
"The operation sum() on a matrix block is currently not supported\n");
660 template <
class Matrix>
661 struct sum_symmetric<Backend::VIENNACL,Matrix>
664 typedef typename Matrix::Scalar
T;
673 static T
compute(CGPUMatrix<T> mat,
bool no_diag)
675 return sum<Backend::VIENNACL, CGPUMatrix<T> >
::compute(mat, no_diag);
685 static T
compute(Block<CGPUMatrix<T> > b,
bool no_diag)
687 SG_SERROR(
"The operation sum_symmetric() on a matrix block is currently not supported\n");
696 template <
class Matrix>
697 struct colwise_sum<Backend::VIENNACL,Matrix>
700 typedef typename Matrix::Scalar
T;
703 typedef CGPUVector<T> ReturnType;
707 static viennacl::ocl::kernel& generate_kernel(
bool no_diag)
709 std::string kernel_name =
"colwise_sum_" + ocl::get_type_string<T>();
710 if (no_diag) kernel_name.append(
"_no_diag");
712 if (ocl::kernel_exists(kernel_name))
713 return ocl::get_kernel(kernel_name);
715 std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
716 if (no_diag) source.append(
"#define NO_DIAG\n");
720 __kernel void KERNEL_NAME(
721 __global DATATYPE* mat, int nrows, int ncols, int offset,
722 __global DATATYPE* result, int result_offset)
724 int j = get_global_id(0);
730 for (int i=0; i<nrows; i++)
735 sum += mat[offset+i+j*nrows];
738 result[j+result_offset] = sum;
743 viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
745 kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
757 static CGPUVector<T>
compute(CGPUMatrix<T> m,
bool no_diag)
759 CGPUVector<T> result(m.num_cols);
772 static CGPUVector<T>
compute(Block<CGPUMatrix<T> > b,
bool no_diag)
774 SG_SERROR(
"The operation colwise_sum() on a matrix block is currently not supported\n");
775 return CGPUVector<T>();
785 static void compute(CGPUMatrix<T> mat, CGPUVector<T> result,
bool no_diag)
787 viennacl::ocl::kernel& kernel = generate_kernel<T>(no_diag);
788 kernel.global_work_size(0, ocl::align_to_multiple_1d(mat.num_cols));
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)));
803 static void compute(Block<CGPUMatrix<T> > b, CGPUVector<T> result,
bool no_diag)
805 SG_SERROR(
"The operation colwise_sum() on a matrix block is currently not supported\n");
813 template <
class Matrix>
814 struct rowwise_sum<Backend::VIENNACL,Matrix>
817 typedef typename Matrix::Scalar
T;
820 typedef CGPUVector<T> ReturnType;
824 static viennacl::ocl::kernel& generate_kernel(
bool no_diag)
826 std::string kernel_name =
"rowwise_sum_" + ocl::get_type_string<T>();
827 if (no_diag) kernel_name.append(
"_no_diag");
829 if (ocl::kernel_exists(kernel_name))
830 return ocl::get_kernel(kernel_name);
832 std::string source = ocl::generate_kernel_preamble<T>(kernel_name);
833 if (no_diag) source.append(
"#define NO_DIAG\n");
837 __kernel void KERNEL_NAME(
838 __global DATATYPE* mat, int nrows, int ncols, int offset,
839 __global DATATYPE* result, int result_offset)
841 int i = get_global_id(0);
847 for (int j=0; j<ncols; j++)
852 sum += mat[offset+i+j*nrows];
855 result[i+result_offset] = sum;
860 viennacl::ocl::kernel& kernel = ocl::compile_kernel(kernel_name, source);
862 kernel.local_work_size(0, OCL_WORK_GROUP_SIZE_1D);
874 static CGPUVector<T>
compute(CGPUMatrix<T> m,
bool no_diag)
876 CGPUVector<T> result(m.num_rows);
889 static CGPUVector<T>
compute(Block<CGPUMatrix<T> > b,
bool no_diag)
891 SG_SERROR(
"The operation rowwise_sum() on a matrix block is currently not supported\n");
892 return CGPUVector<T>();
902 static void compute(CGPUMatrix<T> mat, CGPUVector<T> result,
bool no_diag)
904 viennacl::ocl::kernel& kernel = generate_kernel<T>(no_diag);
905 kernel.global_work_size(0, ocl::align_to_multiple_1d(mat.num_rows));
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)));
920 static void compute(Block<CGPUMatrix<T> > b, CGPUVector<T> result,
bool no_diag)
922 SG_SERROR(
"The operation rowwise_sum() on a matrix block is currently not supported\n");
926 #endif // HAVE_VIENNACL
933 #endif // SUM_IMPL_H_
Generic class colwise_sum which provides a static compute method. This class is specialized for diffe...
static SGVector< T > compute(Matrix m, bool no_diag)
static SGVector< T > compute(SGMatrix< T > m, bool no_diag)
static void compute(SGMatrix< T > mat, SGVector< T > result, bool no_diag)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Generic class rowwise_sum which provides a static compute method. This class is specialized for diffe...
static T compute(Matrix m, bool no_diag)
static void compute(Block< SGMatrix< T > > b, SGVector< T > result, bool no_diag)
static T compute(SGMatrix< T > mat, bool no_diag)
static T compute(SGMatrix< T > mat, bool no_diag)
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.
static void compute(SGMatrix< T > mat, SGVector< T > result, bool no_diag)
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorXt
Generic class sum which provides a static compute method. This class is specialized for different typ...
static T compute(Matrix m, bool no_diag)
static SGVector< T > compute(Block< SGMatrix< T > > b, bool no_diag)
static SGVector< T > compute(Matrix m, bool no_diag)
all of classes and functions are contained in the shogun namespace
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
static T compute(Block< SGMatrix< T > > b, bool no_diag)
static SGVector< T > compute(SGMatrix< T > m, bool no_diag)
Eigen::Matrix< T, Eigen::Dynamic, 1 > VectorXt
static T compute(Block< SGMatrix< T > > b, bool no_diag)
static void compute(Block< SGMatrix< T > > b, SGVector< T > result, bool no_diag)
static SGVector< T > compute(Block< SGMatrix< T > > b, bool no_diag)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Block< Matrix > block(Matrix matrix, index_t row_begin, index_t col_begin, index_t row_size, index_t col_size)
Generic class sum symmetric which provides a static compute method. This class is specialized for dif...