Automatic Differentiation
 
Loading...
Searching...
No Matches
columns_dot_product.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_COLUMNS_DOT_PRODUCT_HPP
2#define STAN_MATH_OPENCL_REV_COLUMNS_DOT_PRODUCT_HPP
3#ifdef STAN_OPENCL
4
11
12namespace stan {
13namespace math {
14
27template <
28 typename T1, typename T2, require_any_var_t<T1, T2>* = nullptr,
29 require_all_nonscalar_prim_or_rev_kernel_expression_t<T1, T2>* = nullptr>
31 check_matching_sizes("columns_dot_product(OpenCL)", "v1", v1, "v2", v2);
32
33 if (size_zero(v1, v2)) {
34 return var_value<matrix_cl<double>>(constant(0.0, 1, v1.cols()));
35 }
36
37 arena_t<T1> v1_arena;
38 arena_t<T2> v2_arena;
39 if ((std::is_rvalue_reference<T1&&>::value && is_matrix_cl<T2>::value)
41 v1_arena = std::forward<T1>(v1);
42 }
43 if ((std::is_rvalue_reference<T2&&>::value && is_matrix_cl<T2>::value)
45 v2_arena = std::forward<T2>(v2);
46 }
47
48 matrix_cl<double> res_val;
49 results(v1_arena, v2_arena, res_val) = expressions(
50 calc_if<((std::is_lvalue_reference<T1>::value || !is_matrix_cl<T1>::value)
51 && !is_var<T1>::value)>(value_of(v1)),
52 calc_if<((std::is_lvalue_reference<T2>::value || !is_matrix_cl<T2>::value)
53 && !is_var<T2>::value)>(value_of(v2)),
55 while (res_val.rows() > 1) {
56 res_val = colwise_sum(res_val).eval();
57 }
58
59 return make_callback_var(
60 res_val,
61 [v1_arena, v2_arena](const vari_value<matrix_cl<double>>& res) mutable {
62 auto v1_deriv
63 = elt_multiply(colwise_broadcast(res.adj()), value_of(v2_arena));
64 auto v2_deriv
65 = elt_multiply(colwise_broadcast(res.adj()), value_of(v1_arena));
66 adjoint_results(v1_arena, v2_arena) += expressions(v1_deriv, v2_deriv);
67 });
68}
69
70} // namespace math
71} // namespace stan
72
73#endif
74#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
auto constant(const T a, int rows, int cols)
Matrix of repeated values in kernel generator expressions.
Definition constant.hpp:130
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
Definition calc_if.hpp:121
auto colwise_broadcast(T &&a)
Broadcast an expression in colwise dimmension.
auto colwise_sum(T &&a)
Column wise sum - reduction of a kernel generator expression.
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
adjoint_results_cl< T_results... > adjoint_results(T_results &&... results)
Deduces types for constructing adjoint_results_cl object.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
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...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
void check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Check if two structures at the same size.
auto columns_dot_product(const T_a &a, const T_b &b)
Returns the dot product of columns of the specified matrices.
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 ...
Checks if the decayed type of T is a matrix_cl.
Defines a static member named value which is defined to be false as the primitive scalar types cannot...
Definition is_var.hpp:14