Automatic Differentiation
 
Loading...
Searching...
No Matches
elt_function_cl.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_KERNEL_GENERATOR_ELT_FUNCTION_CL_HPP
2#define STAN_MATH_OPENCL_KERNEL_GENERATOR_ELT_FUNCTION_CL_HPP
3#ifdef STAN_OPENCL
4
32#include <array>
33#include <string>
34#include <type_traits>
35#include <set>
36#include <utility>
37
38namespace stan {
39namespace math {
40
51template <typename Derived, typename Scal, typename... T>
52class elt_function_cl : public operation_cl<Derived, Scal, T...> {
53 public:
54 using Scalar = Scal;
55 using base = operation_cl<Derived, Scalar, T...>;
56 using base::var_name_;
57
63 elt_function_cl(const std::string& fun, T&&... args) // NOLINT
64 : base(std::forward<T>(args)...), fun_(fun) {}
65
75 const std::string& row_index_name, const std::string& col_index_name,
76 const bool view_handled,
77 std::conditional_t<false, T, const std::string&>... var_names_arg) const {
78 kernel_parts res{};
79
80 for (const char* incl : base::derived().includes) {
81 res.includes += incl;
82 }
83 std::array<std::string, sizeof...(T)> var_names_arg_arr
84 = {(var_names_arg + ", ")...};
85 std::string var_names_list = std::accumulate(
86 var_names_arg_arr.begin(), var_names_arg_arr.end(), std::string());
87 res.body = type_str<Scalar>() + " " + var_name_ + " = " + fun_ + "((double)"
88 + var_names_list.substr(0, var_names_list.size() - 2) + ");\n";
89 return res;
90 }
91
92 protected:
93 std::string fun_;
94};
95
102#define ADD_BINARY_FUNCTION_WITH_INCLUDES(fun, ...) \
103 template <typename T1, typename T2> \
104 class fun##_ : public elt_function_cl<fun##_<T1, T2>, double, T1, T2> { \
105 using base = elt_function_cl<fun##_<T1, T2>, double, T1, T2>; \
106 using base::arguments_; \
107 \
108 public: \
109 using base::rows; \
110 using base::cols; \
111 static const std::vector<const char*> includes; \
112 explicit fun##_(T1&& a, T2&& b) \
113 : base(#fun, std::forward<T1>(a), std::forward<T2>(b)) { \
114 if (a.rows() != base::dynamic && b.rows() != base::dynamic) { \
115 check_size_match(#fun, "Rows of ", "a", a.rows(), "rows of ", "b", \
116 b.rows()); \
117 } \
118 if (a.cols() != base::dynamic && b.cols() != base::dynamic) { \
119 check_size_match(#fun, "Columns of ", "a", a.cols(), "columns of ", \
120 "b", b.cols()); \
121 } \
122 } \
123 inline auto deep_copy() const { \
124 auto&& arg1_copy = this->template get_arg<0>().deep_copy(); \
125 auto&& arg2_copy = this->template get_arg<1>().deep_copy(); \
126 return fun##_<std::remove_reference_t<decltype(arg1_copy)>, \
127 std::remove_reference_t<decltype(arg2_copy)>>{ \
128 std::move(arg1_copy), std::move(arg2_copy)}; \
129 } \
130 inline std::pair<int, int> extreme_diagonals() const { \
131 return {-rows() + 1, cols() - 1}; \
132 } \
133 }; \
134 \
135 template <typename T1, typename T2, \
136 require_all_kernel_expressions_t<T1, T2>* = nullptr, \
137 require_any_not_stan_scalar_t<T1, T2>* = nullptr> \
138 inline fun##_<as_operation_cl_t<T1>, as_operation_cl_t<T2>> fun(T1&& a, \
139 T2&& b) { \
140 return fun##_<as_operation_cl_t<T1>, as_operation_cl_t<T2>>( \
141 as_operation_cl(std::forward<T1>(a)), \
142 as_operation_cl(std::forward<T2>(b))); \
143 } \
144 template <typename T1, typename T2> \
145 const std::vector<const char*> fun##_<T1, T2>::includes{__VA_ARGS__};
146
153#define ADD_UNARY_FUNCTION_WITH_INCLUDES(fun, ...) \
154 template <typename T> \
155 class fun##_ : public elt_function_cl<fun##_<T>, double, T> { \
156 using base = elt_function_cl<fun##_<T>, double, T>; \
157 using base::arguments_; \
158 \
159 public: \
160 using base::rows; \
161 using base::cols; \
162 static const std::vector<const char*> includes; \
163 explicit fun##_(T&& a) : base(#fun, std::forward<T>(a)) {} \
164 inline auto deep_copy() const { \
165 auto&& arg_copy = this->template get_arg<0>().deep_copy(); \
166 return fun##_<std::remove_reference_t<decltype(arg_copy)>>{ \
167 std::move(arg_copy)}; \
168 } \
169 inline std::pair<int, int> extreme_diagonals() const { \
170 return {-rows() + 1, cols() - 1}; \
171 } \
172 }; \
173 \
174 template <typename T, typename Cond \
175 = require_all_kernel_expressions_and_none_scalar_t<T>> \
176 inline fun##_<as_operation_cl_t<T>> fun(T&& a) { \
177 return fun##_<as_operation_cl_t<T>>(as_operation_cl(std::forward<T>(a))); \
178 } \
179 template <typename T> \
180 const std::vector<const char*> fun##_<T>::includes{__VA_ARGS__};
181
187#define ADD_UNARY_FUNCTION(fun) ADD_UNARY_FUNCTION_WITH_INCLUDES(fun)
188
195#define ADD_UNARY_FUNCTION_PASS_ZERO(fun) \
196 template <typename T> \
197 class fun##_ : public elt_function_cl<fun##_<T>, double, T> { \
198 using base = elt_function_cl<fun##_<T>, double, T>; \
199 using base::arguments_; \
200 \
201 public: \
202 using base::rows; \
203 using base::cols; \
204 static constexpr auto view_transitivness = std::make_tuple(true); \
205 static const std::vector<const char*> includes; \
206 explicit fun##_(T&& a) : base(#fun, std::forward<T>(a)) {} \
207 inline auto deep_copy() const { \
208 auto&& arg_copy = this->template get_arg<0>().deep_copy(); \
209 return fun##_<std::remove_reference_t<decltype(arg_copy)>>{ \
210 std::move(arg_copy)}; \
211 } \
212 }; \
213 \
214 template <typename T, typename Cond \
215 = require_all_kernel_expressions_and_none_scalar_t<T>> \
216 inline fun##_<as_operation_cl_t<T>> fun(T&& a) { \
217 return fun##_<as_operation_cl_t<T>>(as_operation_cl(std::forward<T>(a))); \
218 } \
219 template <typename T> \
220 const std::vector<const char*> fun##_<T>::includes{};
221
228#define ADD_CLASSIFICATION_FUNCTION(fun, ...) \
229 template <typename T> \
230 class fun##_ : public elt_function_cl<fun##_<T>, bool, T> { \
231 using base = elt_function_cl<fun##_<T>, bool, T>; \
232 using base::arguments_; \
233 \
234 public: \
235 using base::rows; \
236 using base::cols; \
237 static constexpr auto view_transitivness = std::make_tuple(true); \
238 static const std::vector<const char*> includes; \
239 explicit fun##_(T&& a) : base(#fun, std::forward<T>(a)) {} \
240 inline auto deep_copy() const { \
241 auto&& arg_copy = this->template get_arg<0>().deep_copy(); \
242 return fun##_<std::remove_reference_t<decltype(arg_copy)>>{ \
243 std::move(arg_copy)}; \
244 } \
245 inline std::pair<int, int> extreme_diagonals() const { \
246 return __VA_ARGS__; \
247 } \
248 }; \
249 \
250 template <typename T, typename Cond \
251 = require_all_kernel_expressions_and_none_scalar_t<T>> \
252 inline fun##_<as_operation_cl_t<T>> fun(T&& a) { \
253 return fun##_<as_operation_cl_t<T>>(as_operation_cl(std::forward<T>(a))); \
254 } \
255 template <typename T> \
256 const std::vector<const char*> fun##_<T>::includes{};
257
261
265
270
283
288
294
296 opencl_kernels::digamma_device_function)
297ADD_UNARY_FUNCTION_WITH_INCLUDES(erfcx, opencl_kernels::erfcx_device_function)
298ADD_UNARY_FUNCTION_WITH_INCLUDES(log1m, opencl_kernels::log1m_device_function)
300 opencl_kernels::log1p_exp_device_function,
301 opencl_kernels::log_inv_logit_device_function)
303 opencl_kernels::log1m_exp_device_function)
305 opencl_kernels::log1p_exp_device_function)
307 opencl_kernels::inv_square_device_function)
309 opencl_kernels::inv_logit_device_function)
310ADD_UNARY_FUNCTION_WITH_INCLUDES(logit, opencl_kernels::log1m_device_function,
311 opencl_kernels::logit_device_function)
312ADD_UNARY_FUNCTION_WITH_INCLUDES(Phi, opencl_kernels::phi_device_function)
314 opencl_kernels::inv_logit_device_function,
315 opencl_kernels::phi_approx_device_function)
318 opencl_kernels::std_normal_lcdf_device_function)
321 opencl_kernels::std_normal_lcdf_device_function)
322ADD_UNARY_FUNCTION_WITH_INCLUDES(inv_Phi, opencl_kernels::log1m_device_function,
323 opencl_kernels::phi_device_function,
324 opencl_kernels::inv_phi_device_function)
326 log1m_inv_logit, opencl_kernels::log1p_exp_device_function,
327 opencl_kernels::log1m_inv_logit_device_function)
329 opencl_kernels::trigamma_device_function)
331 square,
332 "\n#ifndef STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_SQUARE\n"
333 "#define STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_SQUARE\n"
334 "double square(double x){return x*x;}\n"
335 "#endif\n")
336
337ADD_CLASSIFICATION_FUNCTION(isfinite, {-rows() + 1, cols() - 1})
338ADD_CLASSIFICATION_FUNCTION(isinf,
339 this->template get_arg<0>().extreme_diagonals())
340ADD_CLASSIFICATION_FUNCTION(isnan,
341 this->template get_arg<0>().extreme_diagonals())
342
343ADD_BINARY_FUNCTION_WITH_INCLUDES(fdim)
344ADD_BINARY_FUNCTION_WITH_INCLUDES(fmax)
345ADD_BINARY_FUNCTION_WITH_INCLUDES(fmin)
346ADD_BINARY_FUNCTION_WITH_INCLUDES(fmod)
347ADD_BINARY_FUNCTION_WITH_INCLUDES(hypot)
348ADD_BINARY_FUNCTION_WITH_INCLUDES(ldexp)
349ADD_BINARY_FUNCTION_WITH_INCLUDES(pow)
350ADD_BINARY_FUNCTION_WITH_INCLUDES(copysign)
351
352ADD_BINARY_FUNCTION_WITH_INCLUDES(
353 erfcx_derivative, stan::math::opencl_kernels::erfcx_device_function)
354ADD_BINARY_FUNCTION_WITH_INCLUDES(
355 beta, stan::math::opencl_kernels::beta_device_function)
356ADD_BINARY_FUNCTION_WITH_INCLUDES(
357 binomial_coefficient_log,
358 stan::math::opencl_kernels::lgamma_stirling_device_function,
359 stan::math::opencl_kernels::lgamma_stirling_diff_device_function,
360 stan::math::opencl_kernels::lbeta_device_function,
361 stan::math::opencl_kernels::binomial_coefficient_log_device_function)
362template <typename T1, typename T2>
363class lbeta_ : public elt_function_cl<lbeta_<T1, T2>, double, T1, T2> {
364 using base = elt_function_cl<lbeta_<T1, T2>, double, T1, T2>;
365 using base::arguments_;
366
367 public:
368 using base::cols;
369 using base::rows;
370 static const std::vector<const char*> includes;
371 explicit lbeta_(T1&& a, T2&& b)
372 : base("stan_lbeta", std::forward<T1>(a), std::forward<T2>(b)) {
373 if (a.rows() != base::dynamic && b.rows() != base::dynamic) {
374 check_size_match("lbeta", "Rows of ", "a", a.rows(), "rows of ", "b",
375 b.rows());
376 }
377 if (a.cols() != base::dynamic && b.cols() != base::dynamic) {
378 check_size_match("lbeta", "Columns of ", "a", a.cols(), "columns of ",
379 "b", b.cols());
380 }
381 }
382 inline auto deep_copy() const {
383 auto&& arg1_copy = this->template get_arg<0>().deep_copy();
384 auto&& arg2_copy = this->template get_arg<1>().deep_copy();
385 return lbeta_<std::remove_reference_t<decltype(arg1_copy)>,
386 std::remove_reference_t<decltype(arg2_copy)>>{
387 std::move(arg1_copy), std::move(arg2_copy)};
388 }
389 inline std::pair<int, int> extreme_diagonals() const {
390 return {-rows() + 1, cols() - 1};
391 }
392};
393
394template <typename T1, typename T2,
395 require_all_kernel_expressions_t<T1, T2>* = nullptr,
396 require_any_not_stan_scalar_t<T1, T2>* = nullptr>
397inline lbeta_<as_operation_cl_t<T1>, as_operation_cl_t<T2>> lbeta(T1&& a,
398 T2&& b) {
399 return lbeta_<as_operation_cl_t<T1>, as_operation_cl_t<T2>>(
400 as_operation_cl(std::forward<T1>(a)),
401 as_operation_cl(std::forward<T2>(b)));
402}
403
404template <typename T1, typename T2>
405const std::vector<const char*> lbeta_<T1, T2>::includes{
406 stan::math::opencl_kernels::lgamma_stirling_device_function,
407 stan::math::opencl_kernels::lgamma_stirling_diff_device_function,
408 stan::math::opencl_kernels::lbeta_device_function};
409ADD_BINARY_FUNCTION_WITH_INCLUDES(
410 log_inv_logit_diff, opencl_kernels::log1p_exp_device_function,
411 opencl_kernels::log1m_exp_device_function,
412 opencl_kernels::log_inv_logit_diff_device_function)
413ADD_BINARY_FUNCTION_WITH_INCLUDES(log_diff_exp,
414 opencl_kernels::log1m_exp_device_function,
415 opencl_kernels::log_diff_exp_device_function)
416ADD_BINARY_FUNCTION_WITH_INCLUDES(
417 lmultiply, stan::math::opencl_kernels::lmultiply_device_function)
418
419#undef ADD_BINARY_FUNCTION_WITH_INCLUDES
420#undef ADD_UNARY_FUNCTION_WITH_INCLUDES
421#undef ADD_UNARY_FUNCTION
422#undef ADD_UNARY_FUNCTION_PASS_ZERO
423#undef ADD_CLASSIFICATION_FUNCTION
424
426} // namespace math
427} // namespace stan
428#endif
429#endif
elt_function_cl(const std::string &fun, T &&... args)
Constructor.
kernel_parts generate(const std::string &row_index_name, const std::string &col_index_name, const bool view_handled, std::conditional_t< false, T, const std::string & >... var_names_arg) const
Generates kernel code for this expression.
Represents an element-wise function in kernel generator expressions.
Derived & derived()
Casts the instance into its derived type.
Base for all kernel generator operations.
rsqrt_< as_operation_cl_t< T > > rsqrt(T &&a)
#define ADD_UNARY_FUNCTION(fun)
Generates a class and function for a general unary function that is defined by OpenCL.
#define ADD_UNARY_FUNCTION_PASS_ZERO(fun)
Generates a class and function for an unary function, defined by OpenCL with special property that it...
#define ADD_UNARY_FUNCTION_WITH_INCLUDES(fun,...)
Generates a class and function for a general unary function that is defined by OpenCL or in the inclu...
std_normal_lcdf_dscaled_impl_< as_operation_cl_t< T > > std_normal_lcdf_dscaled_impl(T &&a)
std_normal_lcdf_scaled_impl_< as_operation_cl_t< T > > std_normal_lcdf_scaled_impl(T &&a)
fvar< T > acos(const fvar< T > &x)
Definition acos.hpp:16
fvar< T > sin(const fvar< T > &x)
Definition sin.hpp:16
fvar< T > acosh(const fvar< T > &x)
Definition acosh.hpp:16
fvar< T > logit(const fvar< T > &x)
Definition logit.hpp:14
fvar< T > expm1(const fvar< T > &x)
Definition expm1.hpp:14
fvar< T > atanh(const fvar< T > &x)
Return inverse hyperbolic tangent of specified value.
Definition atanh.hpp:26
fvar< T > inv_square(const fvar< T > &x)
fvar< T > exp2(const fvar< T > &x)
Definition exp2.hpp:14
constexpr double log2()
Return natural logarithm of two.
Definition log2.hpp:17
fvar< T > log1m_exp(const fvar< T > &x)
Return the natural logarithm of one minus the exponentiation of the specified argument.
Definition log1m_exp.hpp:22
fvar< T > asinh(const fvar< T > &x)
Definition asinh.hpp:16
fvar< T > cosh(const fvar< T > &x)
Definition cosh.hpp:16
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
fvar< T > erf(const fvar< T > &x)
Definition erf.hpp:16
auto inv_logit(T &&x)
Returns the inverse logit function applied to the argument.
Definition inv_logit.hpp:20
fvar< T > Phi_approx(const fvar< T > &x)
Return an approximation of the unit normal cumulative distribution function (CDF).
fvar< T > log_inv_logit(const fvar< T > &x)
fvar< T > erfcx(const fvar< T > &x)
Return the scaled complementary error function of the argument.
Definition erfcx.hpp:26
fvar< T > cbrt(const fvar< T > &x)
Return cube root of specified argument.
Definition cbrt.hpp:20
fvar< T > sinh(const fvar< T > &x)
Definition sinh.hpp:15
fvar< T > log1p_exp(const fvar< T > &x)
Definition log1p_exp.hpp:14
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:18
fvar< T > atan(const fvar< T > &x)
Definition atan.hpp:16
fvar< T > trigamma(const fvar< T > &u)
Return the value of the trigamma function at the specified argument (i.e., the second derivative of t...
Definition trigamma.hpp:25
fvar< T > tan(const fvar< T > &x)
Definition tan.hpp:16
fvar< T > Phi(const fvar< T > &x)
Definition Phi.hpp:16
fvar< T > erfc(const fvar< T > &x)
Definition erfc.hpp:16
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
fvar< T > inv_Phi(const fvar< T > &p)
Definition inv_Phi.hpp:16
fvar< T > floor(const fvar< T > &x)
Definition floor.hpp:13
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
static constexpr double log10()
Returns the natural logarithm of ten.
fvar< T > tanh(const fvar< T > &x)
Definition tanh.hpp:15
fvar< T > cos(const fvar< T > &x)
Definition cos.hpp:16
fvar< T > round(const fvar< T > &x)
Return the closest integer to the specified argument, with halfway cases rounded away from zero.
Definition round.hpp:24
fvar< T > tgamma(const fvar< T > &x)
Return the result of applying the gamma function to the specified argument.
Definition tgamma.hpp:21
fvar< T > asin(const fvar< T > &x)
Definition asin.hpp:16
fvar< T > ceil(const fvar< T > &x)
Definition ceil.hpp:13
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
fvar< T > log1m_inv_logit(const fvar< T > &x)
Return the natural logarithm of one minus the inverse logit of the specified argument.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
fvar< T > trunc(const fvar< T > &x)
Return the nearest integral value that is not larger in magnitude than the specified argument.
Definition trunc.hpp:20
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.
Parts of an OpenCL kernel, generated by an expression.