fml  0.1-0
Fused Matrix Library
linalg_chol.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_GPU_LINALG_LINALG_CHOL_H
6 #define FML_GPU_LINALG_LINALG_CHOL_H
7 #pragma once
8 
9 
10 #include <stdexcept>
11 
12 #include "../../_internals/linalgutils.hh"
13 
14 #include "../arch/arch.hh"
15 
16 #include "../internals/gpu_utils.hh"
17 #include "../internals/gpuscalar.hh"
18 
19 #include "../gpumat.hh"
20 
21 #include "linalg_err.hh"
22 
23 
24 namespace fml
25 {
26 namespace linalg
27 {
47  template <typename REAL>
48  void chol(gpumat<REAL> &x)
49  {
50  const len_t n = x.nrows();
51  if (n != x.ncols())
52  throw std::runtime_error("'x' must be a square matrix");
53 
54  auto c = x.get_card();
55  const auto fill = GPUBLAS_FILL_L;
56 
57  int lwork;
58  gpulapack_status_t check = gpulapack::potrf_buflen(c->lapack_handle(), fill, n,
59  x.data_ptr(), n, &lwork);
60  gpulapack::err::check_ret(check, "potrf_bufferSize");
61 
62  gpuvec<REAL> work(c, lwork);
63 
64  int info = 0;
65  gpuscalar<int> info_device(c, info);
66  check = gpulapack::potrf(c->lapack_handle(), fill, n, x.data_ptr(), n,
67  work.data_ptr(), lwork, info_device.data_ptr());
68 
69  info_device.get_val(&info);
70  gpulapack::err::check_ret(check, "potrf");
71  if (info < 0)
72  fml::linalgutils::check_info(info, "potrf");
73  else if (info > 0)
74  throw std::runtime_error("chol: leading minor of order " + std::to_string(info) + " is not positive definite");
75 
76  fml::gpu_utils::tri2zero('U', false, n, n, x.data_ptr(), n);
77  }
78 }
79 }
80 
81 
82 #endif
fml::gpuvec
Vector class for data held on a single GPU.
Definition: gpuvec.hh:32
fml::unimat::nrows
len_t nrows() const
Number of rows.
Definition: unimat.hh:36
fml::unimat::ncols
len_t ncols() const
Number of columns.
Definition: unimat.hh:38
fml::unimat::data_ptr
REAL * data_ptr()
Pointer to the internal array.
Definition: unimat.hh:40
fml
Core namespace.
Definition: dimops.hh:10
fml::linalg::chol
void chol(cpumat< REAL > &x)
Compute the Choleski factorization.
Definition: linalg_chol.hh:47
fml::gpumat
Matrix class for data held on a single GPU.
Definition: gpumat.hh:35
fml::gpuscalar
Definition: gpuscalar.hh:16