33 #ifndef LINALG_NAMESPACE_H_ 34 #define LINALG_NAMESPACE_H_ 51 template <
typename T,
template <
typename>
class Container>
60 SG_SERROR(
"Vector or matrix is on GPU but no GPU backend registered. \ 61 This can happen if the GPU backend was de-activated \ 62 after memory has been transferred to GPU.\n");
78 template <
typename T,
template <
typename>
class Container>
81 if (a.on_gpu() && b.on_gpu())
87 SG_SERROR(
"Vector or matrix is on GPU but no GPU backend registered. \ 88 This can happen if the GPU backend was de-activated \ 89 after memory has been transferred to GPU.\n");
93 else if (a.on_gpu() || b.on_gpu())
95 SG_SERROR(
"Cannot operate with first vector/matrix on_gpu flag(%d) \ 96 and second vector/matrix on_gpu flag (%d).\n",
97 a.on_gpu(), b.on_gpu());
111 template <
typename T>
131 without GPU backend registered.\n");
146 template <
typename T>
166 without GPU backend registered.\n");
179 template <
typename T,
template<
typename>
class Container>
192 template <
typename T>
201 typedef typename std::aligned_storage<sizeof(T), alignof(T)>::type aligned_t;
203 data =
reinterpret_cast<T*
>(SG_MALLOC(aligned_t, a.
size()));
204 gpu_backend->from_gpu(a, data);
208 SG_SERROR(
"Data memory on GPU but no GPU backend registered. \ 209 This can happen if the GPU backend was de-activated \ 210 after memory has been transferred to GPU.\n");
229 template <
typename T>
238 typedef typename std::aligned_storage<sizeof(T), alignof(T)>::type aligned_t;
241 gpu_backend->from_gpu(a, data);
245 SG_SERROR(
"Data memory on GPU but no GPU backend registered. \ 246 This can happen if the GPU backend was de-activated \ 247 after memory has been transferred to GPU.\n");
265 template <
typename T,
template<
typename>
class Container>
283 template <
typename T>
287 "Length of vector a (%d) doesn't match vector b (%d).\n", a.
vlen, b.
vlen);
289 "Length of vector result (%d) doesn't match vector a (%d).\n",
293 "Cannot operate with vector result on_gpu (%d) and vector a on_gpu (%d).\n",
296 "Cannot operate with vector result on_gpu (%d) and vector b on_gpu (%d).\n",
314 template <
typename T>
318 "Number of rows of matrix a (%d) must match matrix b (%d).\n",
321 "Number of columns of matrix a (%d) must match matrix b (%d).\n",
325 "Cannot operate with matrix result on_gpu (%d) and matrix a on_gpu (%d).\n",
328 "Cannot operate with matrix result on_gpu (%d) and matrix b on_gpu (%d).\n",
344 template <
typename T,
template<
typename>
class Container>
345 Container<T>
add(Container<T>& a, Container<T>& b, T alpha=1, T beta=1)
347 auto result = a.clone();
348 add(a, b, result, alpha, beta);
361 template <
typename T>
377 template <
typename T>
379 const bool lower=
true)
392 template <
typename T>
396 "Length of vector a (%d) doesn't match vector b (%d).\n", a.
vlen, b.
vlen);
412 template <
typename T>
415 REQUIRE(a.m_row_size == b.m_row_size && a.m_col_size == b.m_col_size,
416 "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
417 a.m_row_size, a.m_col_size, b.m_row_size, b.m_col_size);
419 "Dimension mismatch! A(%d x %d) vs result(%d x %d)\n",
422 REQUIRE(!result.
on_gpu(),
"Cannot operate with matrix result on_gpu (%d) \ 423 as matrix blocks are on CPU.\n", result.
on_gpu());
425 sg_linalg->get_cpu_backend()->element_prod(a, b, result);
437 template <
typename T>
440 REQUIRE(a.m_row_size == b.m_row_size && a.m_col_size == b.m_col_size,
441 "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
442 a.m_row_size, a.m_col_size, b.m_row_size, b.m_col_size);
462 template <
typename T>
466 "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
469 "Dimension mismatch! A(%d x %d) vs result(%d x %d)\n",
473 "Cannot operate with matrix result on_gpu (%d) and \ 476 "Cannot operate with matrix result on_gpu (%d) and \ 490 template <
typename T>
494 "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
515 template <
typename T>
523 column number of Matrix A (%d).\n", result.
vlen, A.
num_cols);
530 row number of Matrix A (%d).\n", result.
vlen, A.
num_rows);
534 "Cannot operate with vector result on_gpu (%d) and vector a on_gpu (%d).\n",
537 "Cannot operate with vector result on_gpu (%d) and vector b on_gpu (%d).\n",
551 template <
typename T>
586 template <
typename T>
588 bool transpose_A=
false,
bool transpose_B=
false)
591 "Cannot operate with matrix result on_gpu (%d) and \ 594 "Cannot operate with matrix result on_gpu (%d) and \ 600 number of rows for result (%d) should be equal!\n", A.
num_cols, result.
num_rows);
606 number of columns for result (%d) should be equal!\n",
614 number of columns for result (%d) should be equal!\n",
621 number of rows for result (%d) should be equal!\n", A.
num_rows, result.
num_rows);
627 number of columns for result (%d) should be equal!\n",
635 number of columns for result (%d) should be equal!\n",
640 infer_backend(A, B)->matrix_prod(A, B, result, transpose_A, transpose_B);
654 template <
typename T>
656 bool transpose_A=
false,
bool transpose_B=
false)
660 if (transpose_A & transpose_B)
666 else if (transpose_A)
672 else if (transpose_B)
688 matrix_prod(A, B, result, transpose_A, transpose_B);
699 template<
typename T,
template<
typename>
class Container>
700 T
max(
const Container<T>& a)
711 template<
typename T,
template<
typename>
class Container>
712 typename std::enable_if<!std::is_same<T, complex128_t>::value,
float64_t>::type
715 REQUIRE(a.size() > 0,
"Vector/Matrix cannot be empty!\n");
725 template<
template<
typename>
class Container>
728 REQUIRE(a.size() > 0,
"Vector/Matrix cannot be empty!\n");
738 template <
typename T,
template<
typename>
class Container>
754 template <
typename T>
757 REQUIRE(result.
vlen == a.
vlen,
"Length of vector result (%d) doesn't match vector a (%d).\n", result.
vlen, a.
vlen);
771 template <
typename T>
776 REQUIRE((A.
num_cols == result.
num_cols),
"Number of columns of matrix A (%d) must match matrix result (%d).\n",
789 template<
typename T,
template<
typename>
class Container>
790 Container<T>
scale(Container<T>& a, T alpha=1)
792 auto result = a.clone();
793 scale(a, result, alpha);
803 template <
typename T,
template<
typename>
class Container>
816 template <
typename T,
template <
typename>
class Container>
817 T
sum(
const Container<T>& a,
bool no_diag=
false)
830 template <
typename T>
833 return sg_linalg->get_cpu_backend()->sum(a, no_diag);
843 template <
typename T>
858 template <
typename T>
861 REQUIRE(a.m_row_size == a.m_col_size,
"Matrix is not square!\n");
862 return sg_linalg->get_cpu_backend()->sum_symmetric(a, no_diag);
872 template <
typename T>
886 template <
typename T>
889 return sg_linalg->get_cpu_backend()->colwise_sum(a, no_diag);
899 template <
typename T>
913 template <
typename T>
916 return sg_linalg->get_cpu_backend()->rowwise_sum(a, no_diag);
923 #endif //LINALG_NAMESPACE_H_ T sum_symmetric(const SGMatrix< T > &a, bool no_diag=false)
std::complex< float64_t > complex128_t
void scale(SGVector< T > &a, SGVector< T > &result, T alpha=1)
SGMatrix< T > cholesky_factor(const SGMatrix< T > &A, const bool lower=true)
SGVector< T > rowwise_sum(const SGMatrix< T > &mat, bool no_diag=false)
void add(SGVector< T > &a, SGVector< T > &b, SGVector< T > &result, T alpha=1, T beta=1)
T dot(const SGVector< T > &a, const SGVector< T > &b)
void set_const(Container< T > &a, T value)
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.
LinalgBackendBase * infer_backend(const Container< T > &a)
std::enable_if<!std::is_same< T, complex128_t >::value, float64_t >::type mean(const Container< T > &a)
std::unique_ptr< SGLinalg > sg_linalg
SGVector< T > colwise_sum(const SGMatrix< T > &mat, bool no_diag=false)
SGMatrix< T > clone() const
SGVector< T > cholesky_solver(const SGMatrix< T > &L, const SGVector< T > &b, const bool lower=true)
void range_fill(Container< T > &a, const T start=0)
void matrix_prod(SGMatrix< T > &A, SGVector< T > &b, SGVector< T > &result, bool transpose=false)
all of classes and functions are contained in the shogun namespace
T sum(const Container< T > &a, bool no_diag=false)
Base interface of generic linalg methods and generic memory transfer methods.
void to_gpu(SGVector< T > &a, SGVector< T > &b)
void from_gpu(SGVector< T > &a, SGVector< T > &b)
T max(const Container< T > &a)
void element_prod(Block< SGMatrix< T >> &a, Block< SGMatrix< T >> &b, SGMatrix< T > &result)