Stan Math Library  2.20.0
reverse mode automatic differentiation
identity.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_OPENCL_KERNELS_IDENTITY_HPP
2 #define STAN_MATH_OPENCL_KERNELS_IDENTITY_HPP
3 #ifdef STAN_OPENCL
4 
7 
8 namespace stan {
9 namespace math {
10 namespace opencl_kernels {
11 // \cond
12 static const char* identity_kernel_code = STRINGIFY(
13  // \endcond
25  __kernel void identity(__global double* A, unsigned int rows,
26  unsigned int cols) {
27  int i = get_global_id(0);
28  int j = get_global_id(1);
29  if (i < rows && j < cols) {
30  if (i == j) {
31  A(i, j) = 1.0;
32  } else {
33  A(i, j) = 0.0;
34  }
35  }
36  }
37  // \cond
38 );
39 // \endcond
40 // \cond
41 static const char* batch_identity_kernel_code = STRINGIFY(
42  // \endcond
43 
68  __kernel void batch_identity(__global double* A, unsigned int batch_rows,
69  unsigned int size) {
70  // The ID of the matrix in the batch the thread is assigned to
71  int batch_id = get_global_id(0);
72  // The row and column of the matrix in the batch
73  int batch_row = get_global_id(1);
74  int batch_col = get_global_id(2);
75  int index = batch_id * batch_rows * batch_rows + batch_col * batch_rows
76  + batch_row;
77  // Check for potential overflows of A.
78  if (index < size) {
79  if (batch_row == batch_col) {
80  A[index] = 1.0;
81  } else {
82  A[index] = 0.0;
83  }
84  }
85  }
86  // \cond
87 );
88 // \endcond
89 
95  identity_kernel_code});
96 
101  "batch_identity", {indexing_helpers, batch_identity_kernel_code});
102 
103 } // namespace opencl_kernels
104 } // namespace math
105 } // namespace stan
106 #endif
107 #endif
int rows(const Eigen::Matrix< T, R, C > &m)
Return the number of rows in the specified matrix, vector, or row vector.
Definition: rows.hpp:20
#define STRINGIFY(src)
Definition: kernel_cl.hpp:22
static const char * indexing_helpers
Definition: helpers.hpp:14
const kernel_cl< out_buffer, int, int > batch_identity("batch_identity", {indexing_helpers, batch_identity_kernel_code})
See the docs for batch_identity() .
int cols(const Eigen::Matrix< T, R, C > &m)
Return the number of columns in the specified matrix, vector, or row vector.
Definition: cols.hpp:20
const kernel_cl< out_buffer, int, int > identity("identity", {indexing_helpers, identity_kernel_code})
See the docs for identity() .
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
Creates functor for kernels.
Definition: kernel_cl.hpp:201

     [ Stan Home Page ] © 2011–2018, Stan Development Team.