1#ifndef STAN_MATH_OPENCL_KERNEL_GENERATOR_LOAD_HPP
2#define STAN_MATH_OPENCL_KERNEL_GENERATOR_LOAD_HPP
31template <
typename T, assign_op_cl AssignOp = assign_op_cl::equals>
34 typename std::remove_reference_t<T>::type> {
40 using Scalar =
typename std::remove_reference_t<T>::type;
44 "load_: argument a must be a matrix_cl<T>!");
46 std::is_arithmetic<Scalar>::value,
47 "load_: T in \"matrix_cl<T> a\" argument must be an arithmetic type!");
80 std::unordered_map<const void*, const char*>& generated,
81 std::unordered_map<const void*, const char*>& generated_all,
83 const std::string& col_index_name,
bool view_handled)
const {
85 if (generated.count(&
a_) == 0) {
86 if (generated_all.count(&
a_) == 0) {
88 generated_all[&
a_] = generated[&
a_] = this->
var_name_.c_str();
89 res.body =
generate_body(row_index_name, col_index_name, view_handled,
91 res.args =
"__global " + type_str<Scalar>() +
"* " +
var_name_
95 const char* arg_var_name = generated_all[&
a_];
98 res.body =
generate_body(row_index_name, col_index_name, view_handled,
116 const std::string& col_index_name,
117 const bool view_handled,
118 const char* arg_var_name)
const {
119 std::string type = type_str<Scalar>();
121 return type +
" " +
var_name_ +
" = " + arg_var_name +
"_global["
122 + row_index_name +
" + " + arg_var_name +
"_rows * "
123 + col_index_name +
"];\n";
126 " if (!((!contains_nonzero(" + arg_var_name
127 +
"_view, LOWER) && " + col_index_name +
" < " + row_index_name
128 +
") || (!contains_nonzero(" + arg_var_name
129 +
"_view, UPPER) && " + col_index_name +
" > " + row_index_name
130 +
"))) {" +
var_name_ +
" = " + arg_var_name +
"_global["
131 + row_index_name +
" + " + arg_var_name +
"_rows * "
132 + col_index_name +
"];}\n";
149 std::unordered_map<const void*, const char*>& generated,
150 std::unordered_map<const void*, const char*>& generated_all,
152 const std::string& col_index_name)
const {
153 if (generated_all.count(&
a_) == 0) {
160 if (generated_all.count(&
a_) == 0) {
176 const std::string& col_index_name)
const {
178 std::string type = type_str<Scalar>();
179 res.
args =
"__global " + type +
"* " +
var_name_ +
"_global, int "
182 +
"_rows * " + col_index_name +
"]";
197 std::unordered_map<const void*, const char*>& generated,
198 std::unordered_map<const void*, const char*>& generated_all,
199 cl::Kernel& kernel,
int& arg_num)
const {
200 if (generated_all.count(&
a_) == 0) {
201 generated_all[&
a_] =
"";
202 kernel.setArg(arg_num++,
a_.buffer());
203 kernel.setArg(arg_num++,
a_.rows());
204 kernel.setArg(arg_num++,
a_.view());
226 std::vector<cl::Event>& events)
const {
227 events.insert(events.end(),
a_.read_events().begin(),
228 a_.read_events().end());
229 events.insert(events.end(),
a_.write_events().begin(),
230 a_.write_events().end());
231 a_.clear_read_write_events();
239 events.insert(events.end(),
a_.write_events().begin(),
240 a_.write_events().end());
248 inline int rows()
const {
return a_.rows(); }
255 inline int cols()
const {
return a_.cols(); }
276 inline void set_view(
int bottom_diagonal,
int top_diagonal,
277 int bottom_zero_diagonal,
int top_zero_diagonal)
const {
278 if (bottom_zero_diagonal <= top_diagonal && bottom_diagonal < 0) {
280 }
else if (bottom_zero_diagonal <= 1 -
a_.rows()) {
283 if (top_zero_diagonal >= bottom_diagonal && top_diagonal > 0) {
285 }
else if (top_zero_diagonal >=
a_.cols() - 1) {
300 return {bottom, top};
329 std::vector<int>& uids, std::unordered_map<const void*, int>& id_map,
330 int& next_id)
const {
331 if (id_map.count(&
a_) == 0) {
332 id_map[&
a_] = next_id;
333 uids.push_back(next_id);
336 uids.push_back(id_map[&
a_]);
std::string generate_body(const std::string &row_index_name, const std::string &col_index_name, const bool view_handled, const char *arg_var_name) const
Generates kernel code for main body of this expression.
void get_clear_read_write_events(std::vector< cl::Event > &events) const
Adds all read and write events on the matrix used by this expression to a list and clears them from t...
int cols() const
Number of columns of a matrix that would be the result of evaluating this expression.
void set_args(std::unordered_map< const void *, const char * > &generated, std::unordered_map< const void *, const char * > &generated_all, cl::Kernel &kernel, int &arg_num) const
Sets kernel arguments for this expression.
void get_unique_matrix_accesses(std::vector< int > &uids, std::unordered_map< const void *, int > &id_map, int &next_id) const
Collects data that is needed beside types to uniqly identify a kernel generator expression.
kernel_parts get_kernel_parts_lhs(std::unordered_map< const void *, const char * > &generated, std::unordered_map< const void *, const char * > &generated_all, name_generator &name_gen, const std::string &row_index_name, const std::string &col_index_name) const
Generates kernel code for this expression if it appears on the left hand side of an assignment.
const T & eval() const
Evaluates the expression.
void get_write_events(std::vector< cl::Event > &events) const
Adds all write events on the matrix used by this expression to a list.
void add_write_event(cl::Event &e) const
Adds write event to the matrix used in this expression.
load_< const T &, AssignOp > deep_copy() const &
load_< T &, AssignOp > deep_copy() &
Creates a deep copy of this expression.
kernel_parts generate_lhs(const std::string &row_index_name, const std::string &col_index_name) const
Generates kernel code for this expression if it appears on the left hand side of an assignment.
void check_assign_dimensions(int rows, int cols) const
If needed resizes underlying matrix to desired number of rows and cols.
void add_read_event(cl::Event &e) const
Adds read event to the matrix used in this expression.
std::pair< int, int > extreme_diagonals() const
Determine indices of extreme sub- and superdiagonals written.
void set_view(int bottom_diagonal, int top_diagonal, int bottom_zero_diagonal, int top_zero_diagonal) const
Sets the view of the matrix depending on which of its parts are written to.
matrix_cl_view view() const
View of a matrix that would be the result of evaluating this expression.
static constexpr assign_op_cl assignment_op
int rows() const
Number of rows of a matrix that would be the result of evaluating this expression.
typename std::remove_reference_t< T >::type Scalar
load_< T, AssignOp > deep_copy() &&
kernel_parts get_kernel_parts(std::unordered_map< const void *, const char * > &generated, std::unordered_map< const void *, const char * > &generated_all, name_generator &name_gen, const std::string &row_index_name, const std::string &col_index_name, bool view_handled) const
Generates kernel code for this expression.
Represents an access to a matrix_cl in kernel generator expressions.
Represents an arithmetic matrix on the OpenCL device.
std::string generate()
Generates a unique variable name.
Unique name generator for variables used in generated kernels.
Base for all kernel generator operations that can be used on left hand side of an expression.
Base for all kernel generator operations.
const matrix_cl_view both(const matrix_cl_view left_view, const matrix_cl_view right_view)
Determines which parts are nonzero in both input views.
const matrix_cl_view either(const matrix_cl_view left_view, const matrix_cl_view right_view)
Determines which parts are nonzero in any of the input views.
bool contains_nonzero(const matrix_cl_view view, const matrix_cl_view part)
Check whether a view contains certain nonzero part.
static constexpr double e()
Return the base of the natural logarithm.
assign_op_cl
Ops that decide the type of assignment for LHS operations.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::false_type when instantiated with zero or more template parameters, all of which extend ...
Parts of an OpenCL kernel, generated by an expression.