Automatic Differentiation
 
Loading...
Searching...
No Matches
cholesky_decompose.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_KERNELS_CHOLESKY_DECOMPOSE_HPP
2#define STAN_MATH_OPENCL_KERNELS_CHOLESKY_DECOMPOSE_HPP
3#ifdef STAN_OPENCL
4
7#include <string>
8
9namespace stan {
10namespace math {
11namespace opencl_kernels {
12// \cond
13static constexpr const char* cholesky_decompose_kernel_code = STRINGIFY(
14 // \endcond
34 __kernel void cholesky_decompose(__global double* A, int rows,
35 __local double* row_j) {
36 const int local_index = get_local_id(0);
37 // The following code is the sequential version of the inplace
38 // cholesky decomposition. Only the innermost loops are parallelized. The
39 // rows are processed sequentially. This loop process all the rows:
40 for (int j = 0; j < rows; j++) {
41 if (local_index == j) {
42 double sum = 0.0;
43 for (int k = 0; k < j; k++) {
44 const double a = A(j, k);
45 row_j[k] = a;
46 sum += a * a;
47 }
48 A(j, j) = sqrt(A(j, j) - sum);
49 row_j[j] = A(j, j);
50 }
51 // Each work item accesses only its own row of A and reads row j from
52 // local memory, so the barriers need only a local memory fence.
53 barrier(CLK_LOCAL_MEM_FENCE);
54 if (local_index < j) {
55 A(local_index, j) = 0.0;
56 } else if (local_index > j) {
57 double sum = 0.0;
58 for (int k = 0; k < j; k++)
59 sum += A(local_index, k) * row_j[k];
60 A(local_index, j) = (A(local_index, j) - sum) / row_j[j];
61 }
62 barrier(CLK_LOCAL_MEM_FENCE);
63 }
64 }
65 // \cond
66);
67// \endcond
68
74 "cholesky_decompose", {indexing_helpers, cholesky_decompose_kernel_code});
75
76} // namespace opencl_kernels
77} // namespace math
78} // namespace stan
79#endif
80#endif
const kernel_cl< in_out_buffer, int, cl::LocalSpaceArg > cholesky_decompose("cholesky_decompose", {indexing_helpers, cholesky_decompose_kernel_code})
See the docs for cholesky_decompose() .
int64_t rows(const T_x &x)
Returns the number of rows in the specified kernel generator expression.
Definition rows.hpp:22
static const std::string indexing_helpers
Defines helper macros for common matrix indexing operations.
Definition helpers.hpp:14
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:18
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
#define STRINGIFY(...)
Definition stringify.hpp:9
Creates functor for kernels.