Stan Math Library  2.20.0
reverse mode automatic differentiation
sub_block.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_OPENCL_SUB_BLOCK_HPP
2 #define STAN_MATH_OPENCL_SUB_BLOCK_HPP
3 #ifdef STAN_OPENCL
4 
10 #include <CL/cl.hpp>
11 #include <vector>
12 
13 namespace stan {
14 namespace math {
15 
27 template <TriangularViewCL triangular_view>
28 inline void matrix_cl::sub_block(const matrix_cl& A, size_t A_i, size_t A_j,
29  size_t this_i, size_t this_j, size_t nrows,
30  size_t ncols) try {
31  if (nrows == 0 || ncols == 0) {
32  return;
33  }
34  if ((A_i + nrows) > A.rows() || (A_j + ncols) > A.cols()
35  || (this_i + nrows) > this->rows() || (this_j + ncols) > this->cols()) {
36  domain_error("sub_block", "submatrix in *this", " is out of bounds", "");
37  }
38  cl::CommandQueue cmdQueue = opencl_context.queue();
39  if (triangular_view == TriangularViewCL::Entire) {
40  cl::size_t<3> src_offset
41  = opencl::to_size_t<3>({A_i * sizeof(double), A_j, 0});
42  cl::size_t<3> dst_offset
43  = opencl::to_size_t<3>({this_i * sizeof(double), this_j, 0});
44  cl::size_t<3> size
45  = opencl::to_size_t<3>({nrows * sizeof(double), ncols, 1});
46  std::vector<cl::Event> kernel_events
48  cl::Event copy_event;
49  cmdQueue.enqueueCopyBufferRect(A.buffer(), this->buffer(), src_offset,
50  dst_offset, size, A.rows() * sizeof(double),
51  A.rows() * A.cols() * sizeof(double),
52  sizeof(double) * this->rows(),
53  this->rows() * this->cols() * sizeof(double),
54  &kernel_events, &copy_event);
55  A.add_read_event(copy_event);
56  this->add_write_event(copy_event);
57  } else {
58  opencl_kernels::sub_block(cl::NDRange(nrows, ncols), A, *this, A_i, A_j,
59  this_i, this_j, nrows, ncols, A.rows(), A.cols(),
60  this->rows(), this->cols(), triangular_view);
61  }
62 } catch (const cl::Error& e) {
63  check_opencl_error("copy_submatrix", e);
64 }
65 
66 } // namespace math
67 } // namespace stan
68 
69 #endif
70 #endif
const std::vector< T > & vec_concat(const std::vector< T > &v1)
Ends the recursion to extract the event stack.
Definition: vec_concat.hpp:17
const cl::Buffer & buffer() const
Definition: matrix_cl.hpp:170
const kernel_cl< in_buffer, out_buffer, int, int, int, int, int, int, int, int, int, int, TriangularViewCL > sub_block("sub_block", {indexing_helpers, sub_block_kernel_code})
See the docs for sub_block() .
The API to access the methods and values in opencl_context_base.
The matrix_cl class - allocates memory space on the OpenCL device, functions for transfering matrices...
void add_write_event(cl::Event new_event) const
Add an event to the write event stack.
Definition: matrix_cl.hpp:118
void add_read_event(cl::Event new_event) const
Add an event to the read event stack.
Definition: matrix_cl.hpp:110
void sub_block(const matrix_cl &A, size_t A_i, size_t A_j, size_t this_i, size_t this_j, size_t nrows, size_t ncols)
Write the contents of A into this starting at the top left of this
Definition: sub_block.hpp:28
Represents a matrix on the OpenCL device.
Definition: matrix_cl.hpp:29
Initialization for OpenCL:
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
const std::vector< cl::Event > read_write_events() const
Get the events from the event stacks.
Definition: matrix_cl.hpp:102
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:87
void check_opencl_error(const char *function, const cl::Error &e)
Throws the domain error with specifying the OpenCL error that occured.
const std::vector< cl::Event > & write_events() const
Get the events from the event stacks.
Definition: matrix_cl.hpp:86
cl::CommandQueue & queue()
Returns the reference to the active OpenCL command queue for the device.

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