1#ifndef STAN_MATH_OPENCL_REV_COPY_HPP
2#define STAN_MATH_OPENCL_REV_COPY_HPP
15#include <CL/opencl.hpp>
36 a.adj() += from_matrix_cl<plain_type_t<T>>(res_vari.adj());
48template <
typename T, require_stan_scalar_t<T>* =
nullptr>
52 Eigen::Map<
const Eigen::Matrix<
var_value<T>, Eigen::Dynamic, 1>>(
66template <
typename T, require_eigen_vt<is_var, T>* =
nullptr>
72 to_matrix_cl(src_stacked.val()), [src_stacked](
auto& res_vari)
mutable {
73 src_stacked.adj() += from_matrix_cl<
74 Eigen::Matrix<double, T::RowsAtCompileTime, T::ColsAtCompileTime>>(
88template <
typename T, require_eigen_vt<is_var, T>* =
nullptr>
90 const std::vector<T>& src) {
95 [src_stacked](
auto& res_vari)
mutable {
97 for (
int i = 0; i < src_stacked.size(); i++) {
99 += Eigen::Map<
plain_type_t<
decltype(src_stacked[i].adj())>>(
100 adj.data() + adj.rows() * i, src_stacked[i].rows(),
101 src_stacked[i].cols());
115template <
typename T_dst,
typename T,
121 T_dst::ColsAtCompileTime>>(a.val()),
122 [a](
auto& res_vari)
mutable { a.adj() += to_matrix_cl(res_vari.adj()); });
134template <
typename T_dst,
typename T,
140 T_dst::ColsAtCompileTime>>(a.val());
142 [a, res]()
mutable { a.adj() +=
to_matrix_cl(res.adj()); });
155template <
typename T_dst,
typename T,
156 require_std_vector_vt<is_var, T_dst>* =
nullptr,
157 require_all_stan_scalar_t<value_type_t<T_dst>>* =
nullptr,
158 require_all_kernel_expressions_t<T>* =
nullptr>
160 check_size_match(
"from_matrix_cl<std::vector<var>>",
"src.cols()", a.cols(),
162 std::vector<double> val = from_matrix_cl<std::vector<double>>(a.val());
167 return {res.begin(), res.end()};
180template <
typename T_dst,
typename T, require_std_vector_t<T_dst>* =
nullptr,
181 require_rev_vector_t<value_type_t<T_dst>>* =
nullptr,
182 require_all_kernel_expressions_t<T>* =
nullptr>
186 res.reserve(a.cols());
187 for (
int i = 0; i < a.cols(); i++) {
188 res.emplace_back(val.col(i));
191 Eigen::MatrixXd adj(a.rows(), a.cols());
192 for (
int i = 0; i < a.cols(); i++) {
193 adj.col(i) = res[i].adj();
197 return {res.begin(), res.end()};
208template <
typename T, require_all_kernel_expressions_t<T>* =
nullptr>
210 return from_matrix_cl<var_value<Eigen::MatrixXd>>(src);
require_t< container_type_check_base< is_eigen, value_type_t, TypeCheck, Check... > > require_eigen_vt
Require type satisfies is_eigen.
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
require_all_t< is_kernel_expression< Types >... > require_all_kernel_expressions_t
Enables a template if all given types are are a valid kernel generator expressions.
matrix_cl< scalar_type_t< T > > to_matrix_cl(T &&src)
Copies the source Eigen matrix, std::vector or scalar to the destination matrix that is stored on the...
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
require_t< container_type_check_base< is_var, value_type_t, TypeCheck, Check... > > require_var_vt
Require type satisfies is_var.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
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.
typename plain_type< T >::type plain_type_t
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...