Automatic Differentiation
 
Loading...
Searching...
No Matches
sum.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_SUM_HPP
2#define STAN_MATH_OPENCL_PRIM_SUM_HPP
3#ifdef STAN_OPENCL
4
9
10namespace stan {
11namespace math {
12
19template <typename T,
20 require_all_kernel_expressions_and_none_scalar_t<T>* = nullptr>
21inline value_type_t<T> sum(const T& m) {
22 if constexpr (is_matrix_cl<T>::value) {
23 if (m.size() < 1000) {
24 // for small matrices running another kernel is not worth it
25 return sum(from_matrix_cl(m));
26 }
27 }
28 matrix_cl<value_type_t<T>> res;
29 if (m.rows() <= 8) {
30 // without transpose we would use just a few threads in a work group
31 res = sum_2d(transpose(m));
32 } else {
33 res = sum_2d(m);
34 }
35 return sum(from_matrix_cl(res));
36}
37
38} // namespace math
39} // namespace stan
40
41#endif
42#endif
auto sum_2d(T &&a)
Two dimensional sum - reduction of a kernel generator expression.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
Definition copy.hpp:61
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...