1 #ifndef STAN_MATH_OPENCL_MATRIX_CL_HPP 2 #define STAN_MATH_OPENCL_MATRIX_CL_HPP 36 cl::Buffer oclBuffer_;
39 mutable std::vector<cl::Event> write_events_;
40 mutable std::vector<cl::Event> read_events_;
44 template <TriangularViewCL triangular_view = TriangularViewCL::Entire>
46 template <TriangularMapCL triangular_map = TriangularMapCL::LowerToUpper>
48 template <TriangularViewCL triangular_view = TriangularViewCL::Entire>
50 size_t this_j,
size_t nrows,
size_t ncols);
51 int rows()
const {
return rows_; }
53 int cols()
const {
return cols_; }
55 int size()
const {
return rows_ * cols_; }
61 write_events_.clear();
78 write_events_.clear();
111 this->read_events_.push_back(new_event);
119 this->write_events_.push_back(new_event);
127 this->read_events_.push_back(new_event);
128 this->write_events_.push_back(new_event);
136 cl::Event copy_event;
137 queue.enqueueBarrierWithWaitList(&this->
write_events(), ©_event);
139 write_events_.clear();
148 cl::Event copy_event;
149 queue.enqueueBarrierWithWaitList(&this->
read_events(), ©_event);
151 read_events_.clear();
161 cl::Event copy_event;
163 queue.enqueueBarrierWithWaitList(&mat_events, ©_event);
165 read_events_.clear();
166 write_events_.clear();
170 const cl::Buffer&
buffer()
const {
return oclBuffer_; }
183 oclBuffer_ = cl::Buffer(ctx, CL_MEM_READ_WRITE,
sizeof(
double) *
size());
184 cl::Event cstr_event;
185 queue.enqueueCopyBuffer(A.
buffer(), this->
buffer(), 0, 0,
189 }
catch (
const cl::Error&
e) {
212 = cl::Buffer(ctx, CL_MEM_READ_WRITE,
sizeof(
double) * rows_ * cols_);
213 }
catch (
const cl::Error&
e) {
228 template <
int R,
int C>
229 explicit matrix_cl(
const Eigen::Matrix<double, R, C>& A)
230 : rows_(A.
rows()), cols_(A.
cols()) {
240 = cl::Buffer(ctx, CL_MEM_READ_WRITE,
sizeof(
double) * A.size());
249 cl::Event transfer_event;
250 queue.enqueueWriteBuffer(oclBuffer_, CL_FALSE, 0,
251 sizeof(
double) * A.size(), A.data(), NULL,
254 }
catch (
const cl::Error&
e) {
261 a.
rows(),
"destination.rows()",
rows());
263 a.
cols(),
"destination.cols()",
cols());
const std::vector< T > & vec_concat(const std::vector< T > &v1)
Ends the recursion to extract the event stack.
void clear_read_events() const
Clear the read events from the event stacks.
const std::vector< cl::Event > & read_events() const
Get the events from the event stacks.
const cl::Buffer & buffer() const
void clear_write_events() const
Clear the write events from the event stacks.
void wait_for_read_events() const
Waits for the read events and clears the read event stack.
void wait_for_read_write_events() const
Waits for read and write events to finish and clears the read, write, and read/write event stacks...
matrix_cl(const matrix_cl &A)
The API to access the methods and values in opencl_context_base.
void add_write_event(cl::Event new_event) const
Add an event to the write event stack.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
void triangular_transpose()
Copies a lower/upper triangular of a matrix to it's upper/lower.
void zeros()
Stores zeros in the matrix on the OpenCL device.
void add_read_event(cl::Event new_event) const
Add an event to the read event stack.
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
void add_read_write_event(cl::Event new_event) const
Add an event to the read/write event stack.
Represents a matrix on the OpenCL device.
checking OpenCL error numbers
cl::Context & context()
Returns the reference to the OpenCL context.
matrix_cl(const Eigen::Matrix< double, R, C > &A)
Constructor for the matrix_cl that creates a copy of the Eigen matrix on the OpenCL device...
Initialization for OpenCL:
void wait_for_write_events() const
Waits for the write events and clears the read event stack.
const std::vector< cl::Event > read_write_events() const
Get the events from the event stacks.
double e()
Return the base of the natural logarithm.
void clear_read_write_events() const
Clear the write events from the event stacks.
matrix_cl(const int &rows, const int &cols)
Constructor for the matrix_cl that only allocates the buffer on the OpenCL device.
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.
cl::CommandQueue & queue()
Returns the reference to the active OpenCL command queue for the device.
matrix_cl & operator=(const matrix_cl &a)