|
| 1 | +#ifndef STAN_MATH_OPENCL_KERNEL_GENERATOR_CAST_HPP |
| 2 | +#define STAN_MATH_OPENCL_KERNEL_GENERATOR_CAST_HPP |
| 3 | +#ifdef STAN_OPENCL |
| 4 | + |
| 5 | +#include <stan/math/prim/meta.hpp> |
| 6 | +#include <stan/math/opencl/matrix_cl_view.hpp> |
| 7 | +#include <stan/math/opencl/kernel_generator/common_return_scalar.hpp> |
| 8 | +#include <stan/math/opencl/kernel_generator/type_str.hpp> |
| 9 | +#include <stan/math/opencl/kernel_generator/name_generator.hpp> |
| 10 | +#include <stan/math/opencl/kernel_generator/operation_cl.hpp> |
| 11 | +#include <stan/math/opencl/kernel_generator/as_operation_cl.hpp> |
| 12 | +#include <array> |
| 13 | +#include <string> |
| 14 | +#include <type_traits> |
| 15 | +#include <set> |
| 16 | +#include <utility> |
| 17 | + |
| 18 | +namespace stan { |
| 19 | +namespace math { |
| 20 | + |
| 21 | +/** \addtogroup opencl_kernel_generator |
| 22 | + * @{ |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * Represents a typecast os scalar in kernel generator expressions. |
| 27 | + * @tparam Derived derived type |
| 28 | + * @tparam T type of argument |
| 29 | + * @tparam Scal type of the scalar of result |
| 30 | + */ |
| 31 | +template <typename Scal, typename T> |
| 32 | +class cast_ : public operation_cl<cast_<Scal, T>, Scal, T> { |
| 33 | + public: |
| 34 | + using Scalar = Scal; |
| 35 | + using base = operation_cl<cast_<Scal, T>, Scalar, T>; |
| 36 | + using base::var_name_; |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor |
| 40 | + * @param args argument expression(s) |
| 41 | + */ |
| 42 | + explicit cast_(T&& arg) : base(std::forward<T>(arg)) {} |
| 43 | + |
| 44 | + /** |
| 45 | + * Generates kernel code for this expression. |
| 46 | + * @param row_index_name row index variable name |
| 47 | + * @param col_index_name column index variable name |
| 48 | + * @param view_handled whether whether caller already handled matrix view |
| 49 | + * @param var_names_arg variable names of the nested expressions |
| 50 | + * @return part of kernel with code for this expression |
| 51 | + */ |
| 52 | + inline kernel_parts generate(const std::string& row_index_name, |
| 53 | + const std::string& col_index_name, |
| 54 | + const bool view_handled, |
| 55 | + const std::string& var_name_arg) const { |
| 56 | + kernel_parts res{}; |
| 57 | + |
| 58 | + res.body = type_str<Scalar>() + " " + var_name_ + " = (" |
| 59 | + + type_str<Scalar>() + ")" + var_name_arg + ";\n"; |
| 60 | + return res; |
| 61 | + } |
| 62 | + |
| 63 | + inline auto deep_copy() const { |
| 64 | + auto&& arg_copy = this->template get_arg<0>().deep_copy(); |
| 65 | + return cast_<Scalar, std::remove_reference_t<decltype(arg_copy)>>{ |
| 66 | + std::move(arg_copy)}; |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * Typecast a kernel generator expression scalar. |
| 72 | + * |
| 73 | + * @tparam T type of argument |
| 74 | + * @param a input argument |
| 75 | + * @return Typecast of given expression |
| 76 | + */ |
| 77 | +template <typename Scalar, typename T, |
| 78 | + require_all_kernel_expressions_and_none_scalar_t<T>* = nullptr> |
| 79 | +inline auto cast(T&& a) { |
| 80 | + auto&& a_operation = as_operation_cl(std::forward<T>(a)).deep_copy(); |
| 81 | + return cast_<Scalar, std::remove_reference_t<decltype(a_operation)>>( |
| 82 | + std::move(a_operation)); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Typecast a scalar. |
| 87 | + * |
| 88 | + * @tparam T type of argument |
| 89 | + * @param a input argument |
| 90 | + * @return Typecast of given expression |
| 91 | + */ |
| 92 | +template <typename Scalar, typename T, require_stan_scalar_t<T>* = nullptr> |
| 93 | +inline Scalar cast(T a) { |
| 94 | + return a; |
| 95 | +} |
| 96 | + |
| 97 | +/** @}*/ |
| 98 | +} // namespace math |
| 99 | +} // namespace stan |
| 100 | +#endif |
| 101 | +#endif |
0 commit comments