Automatic Differentiation
 
Loading...
Searching...
No Matches
cast.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_KERNEL_GENERATOR_CAST_HPP
2#define STAN_MATH_OPENCL_KERNEL_GENERATOR_CAST_HPP
3#ifdef STAN_OPENCL
4
12#include <array>
13#include <string>
14#include <type_traits>
15#include <set>
16#include <utility>
17
18namespace stan {
19namespace math {
20
31template <typename Scal, typename T>
32class cast_ : public operation_cl<cast_<Scal, T>, Scal, T> {
33 public:
34 using Scalar = Scal;
36 using base::var_name_;
37
42 explicit cast_(T&& arg) : base(std::forward<T>(arg)) {}
43
53 inline kernel_parts generate(const std::string& row_index_name,
54 const std::string& col_index_name,
55 const bool view_handled,
56 const std::string& var_name_arg) const {
57 kernel_parts res{};
58
59 res.body = type_str<Scalar>() + " " + var_name_ + " = ("
60 + type_str<Scalar>() + ")" + var_name_arg + ";\n";
61 return res;
62 }
63
64 inline auto deep_copy() const {
65 auto&& arg_copy = this->template get_arg<0>().deep_copy();
66 return cast_<Scalar, std::remove_reference_t<decltype(arg_copy)>>{
67 std::move(arg_copy)};
68 }
69};
70
78template <typename Scalar, typename T,
80inline auto cast(T&& a) {
81 auto&& a_operation = as_operation_cl(std::forward<T>(a)).deep_copy();
82 return cast_<Scalar, std::remove_reference_t<decltype(a_operation)>>(
83 std::move(a_operation));
84}
85
93template <typename Scalar, typename T, require_stan_scalar_t<T>* = nullptr>
94inline Scalar cast(T a) {
95 return a;
96}
97
99} // namespace math
100} // namespace stan
101#endif
102#endif
kernel_parts generate(const std::string &row_index_name, const std::string &col_index_name, const bool view_handled, const std::string &var_name_arg) const
Generates kernel code for this expression.
Definition cast.hpp:53
cast_(T &&arg)
Constructor.
Definition cast.hpp:42
std::string var_name_
auto deep_copy() const
Definition cast.hpp:64
Represents a typecast os scalar in kernel generator expressions.
Definition cast.hpp:32
Base for all kernel generator operations.
auto cast(T &&a)
Typecast a kernel generator expression scalar.
Definition cast.hpp:80
T_operation && as_operation_cl(T_operation &&a)
Converts any valid kernel generator expression into an operation.
require_all_t< is_kernel_expression_and_not_scalar< Types >... > require_all_kernel_expressions_and_none_scalar_t
Enables a template if all given types are non-scalar types that are a valid kernel generator expressi...
fvar< T > arg(const std::complex< fvar< T > > &z)
Return the phase angle of the complex argument.
Definition arg.hpp:19
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
STL namespace.
Parts of an OpenCL kernel, generated by an expression.