Automatic Differentiation
 
Loading...
Searching...
No Matches
multiply.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_MULTIPLY_HPP
2#define STAN_MATH_OPENCL_REV_MULTIPLY_HPP
3#ifdef STAN_OPENCL
4
14
15namespace stan {
16namespace math {
17
27template <
28 typename T_a, typename T_b,
29 require_all_nonscalar_prim_or_rev_kernel_expression_t<T_a, T_b>* = nullptr,
30 require_any_var_t<T_a, T_b>* = nullptr>
31inline auto multiply(T_a&& A, T_b&& B) {
32 check_size_match("multiply ((OpenCL))", "A.cols()", A.cols(), "B.rows()",
33 B.rows());
34 arena_t<T_a> a_arena = std::forward<T_a>(A);
35 arena_t<T_b> b_arena = std::forward<T_b>(B);
36
37 return make_callback_var(
38 value_of(a_arena) * value_of(b_arena),
39 [a_arena, b_arena](vari_value<matrix_cl<double>>& res) mutable {
41 adjoint_of(a_arena) += res.adj() * transpose(value_of(b_arena));
42 }
44 adjoint_of(b_arena) += transpose(value_of(a_arena)) * res.adj();
45 }
46 });
47}
48
58template <
59 typename T_a, typename T_b,
62inline auto operator*(const T_a& A, const T_b& B) {
63 return multiply(A, B);
64}
65
76template <typename T1, typename T2, require_stan_scalar_t<T1>* = nullptr,
77 require_all_nonscalar_prim_or_rev_kernel_expression_t<T2>* = nullptr,
78 require_any_var_t<T1, T2>* = nullptr>
79inline auto multiply(const T1& A, T2&& B) {
80 arena_t<T2> b_arena = std::forward<T2>(B);
81
82 return make_callback_var(
83 value_of(A) * value_of(b_arena),
84 [A, b_arena](vari_value<matrix_cl<double>>& res) mutable {
85 adjoint_results(A, b_arena)
86 += expressions(elt_multiply(res.adj(), value_of(b_arena)),
87 value_of(A) * res.adj());
88 });
89}
90
101template <typename T1, typename T2, require_stan_scalar_t<T2>* = nullptr,
102 require_all_nonscalar_prim_or_rev_kernel_expression_t<T1>* = nullptr,
103 require_any_var_t<T1, T2>* = nullptr>
104inline auto multiply(const T1& A, const T2& B) {
105 return multiply(B, A);
106}
107
108} // namespace math
109} // namespace stan
110
111#endif
112#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
require_all_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_nonscalar_prim_or_rev_kernel_expression_t
Require all of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
auto transpose(Arg &&a)
Transposes a kernel generator expression.
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
require_any_t< is_var< std::decay_t< Types > >... > require_any_var_t
Require any of the types satisfy is_var.
Definition is_var.hpp:39
fvar< T > operator*(const fvar< T > &x, const fvar< T > &y)
Return the product of the two arguments.
adjoint_results_cl< T_results... > adjoint_results(T_results &&... results)
Deduces types for constructing adjoint_results_cl object.
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...
auto & adjoint_of(const T &x)
Returns a reference to a variable's adjoint.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:18
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 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 ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...