1#ifndef STAN_MATH_OPENCL_KERNEL_GENERATOR_ELT_FUNCTION_CL_HPP
2#define STAN_MATH_OPENCL_KERNEL_GENERATOR_ELT_FUNCTION_CL_HPP
51template <
typename Derived,
typename Scal,
typename... T>
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 {
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";
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_; \
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", \
118 if (a.cols() != base::dynamic && b.cols() != base::dynamic) { \
119 check_size_match(#fun, "Columns of ", "a", a.cols(), "columns of ", \
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)}; \
130 inline std::pair<int, int> extreme_diagonals() const { \
131 return {-rows() + 1, cols() - 1}; \
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, \
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))); \
144 template <typename T1, typename T2> \
145 const std::vector<const char*> fun##_<T1, T2>::includes{__VA_ARGS__};
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_; \
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)}; \
169 inline std::pair<int, int> extreme_diagonals() const { \
170 return {-rows() + 1, cols() - 1}; \
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))); \
179 template <typename T> \
180 const std::vector<const char*> fun##_<T>::includes{__VA_ARGS__};
187#define ADD_UNARY_FUNCTION(fun) ADD_UNARY_FUNCTION_WITH_INCLUDES(fun)
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_; \
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)}; \
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))); \
219 template <typename T> \
220 const std::vector<const char*> fun##_<T>::includes{};
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_; \
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)}; \
245 inline std::pair<int, int> extreme_diagonals() const { \
246 return __VA_ARGS__; \
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))); \
255 template <typename T> \
256 const std::vector<const char*> fun##_<T>::includes{};
296 opencl_kernels::digamma_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)
311 opencl_kernels::logit_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)
323 opencl_kernels::phi_device_function,
324 opencl_kernels::inv_phi_device_function)
327 opencl_kernels::log1m_inv_logit_device_function)
329 opencl_kernels::trigamma_device_function)
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"
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())
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)
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_;
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",
377 if (a.cols() != base::dynamic && b.cols() != base::dynamic) {
378 check_size_match("lbeta", "Columns of ", "a", a.cols(), "columns of ",
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)};
389 inline std::pair<int, int> extreme_diagonals() const {
390 return {-rows() + 1, cols() - 1};
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,
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)));
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)
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
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)
fvar< T > sin(const fvar< T > &x)
fvar< T > acosh(const fvar< T > &x)
fvar< T > logit(const fvar< T > &x)
fvar< T > expm1(const fvar< T > &x)
fvar< T > atanh(const fvar< T > &x)
Return inverse hyperbolic tangent of specified value.
fvar< T > inv_square(const fvar< T > &x)
fvar< T > exp2(const fvar< T > &x)
constexpr double log2()
Return natural logarithm of two.
fvar< T > log1m_exp(const fvar< T > &x)
Return the natural logarithm of one minus the exponentiation of the specified argument.
fvar< T > asinh(const fvar< T > &x)
fvar< T > cosh(const fvar< T > &x)
fvar< T > log(const fvar< T > &x)
fvar< T > erf(const fvar< T > &x)
auto inv_logit(T &&x)
Returns the inverse logit function applied to the argument.
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.
fvar< T > cbrt(const fvar< T > &x)
Return cube root of specified argument.
fvar< T > sinh(const fvar< T > &x)
fvar< T > log1p_exp(const fvar< T > &x)
fvar< T > sqrt(const fvar< T > &x)
fvar< T > atan(const fvar< T > &x)
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...
fvar< T > tan(const fvar< T > &x)
fvar< T > Phi(const fvar< T > &x)
fvar< T > erfc(const fvar< T > &x)
fvar< T > log1p(const fvar< T > &x)
fvar< T > inv_Phi(const fvar< T > &p)
fvar< T > floor(const fvar< T > &x)
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
static constexpr double log10()
Returns the natural logarithm of ten.
fvar< T > tanh(const fvar< T > &x)
fvar< T > cos(const fvar< T > &x)
fvar< T > round(const fvar< T > &x)
Return the closest integer to the specified argument, with halfway cases rounded away from zero.
fvar< T > tgamma(const fvar< T > &x)
Return the result of applying the gamma function to the specified argument.
fvar< T > asin(const fvar< T > &x)
fvar< T > ceil(const fvar< T > &x)
fvar< T > log1m(const fvar< T > &x)
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.
fvar< T > square(const fvar< T > &x)
fvar< T > trunc(const fvar< T > &x)
Return the nearest integral value that is not larger in magnitude than the specified argument.
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Parts of an OpenCL kernel, generated by an expression.