Automatic Differentiation
 
Loading...
Searching...
No Matches
gp_exponential_cov.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_GP_EXPONENTIAL_COV_HPP
2#define STAN_MATH_OPENCL_PRIM_GP_EXPONENTIAL_COV_HPP
3#ifdef STAN_OPENCL
4
10#include <CL/opencl.hpp>
11
12namespace stan {
13namespace math {
26template <typename T1, typename T2, typename T3,
27 require_all_kernel_expressions_and_none_scalar_t<T1>* = nullptr,
28 require_all_arithmetic_t<T2, T3>* = nullptr>
30 const T1& x, const T2 sigma, const T3 length_scale) {
31 const auto& x_eval = x.eval();
32 matrix_cl<return_type_t<T1, T2, T3>> res(x.cols(), x.cols());
33 int block_size = 16;
34 int n_blocks = (x.cols() + block_size - 1) / block_size;
35 int blocked_size = block_size * n_blocks;
36 try {
37 opencl_kernels::gp_exponential_cov(cl::NDRange(blocked_size, blocked_size),
38 cl::NDRange(block_size, block_size),
39 x_eval, res, sigma * sigma,
40 -1.0 / length_scale, x.cols(), x.rows());
41 } catch (const cl::Error& e) {
42 check_opencl_error("gp_exponential_cov", e);
43 }
44 return res;
45}
46
64template <typename T1, typename T2, typename T3, typename T4,
68 const T1& x, const T2& y, const T3 sigma, const T4 length_scale) {
69 check_size_match("gp_exponential_cov_cross", "x", x.rows(), "y", y.rows());
70 matrix_cl<return_type_t<T1, T2, T3, T4>> res(x.cols(), y.cols());
71 const auto& x_eval = x.eval();
72 const auto& y_eval = y.eval();
73 int block_size = 16;
74 int x_blocks = (x.cols() + block_size - 1) / block_size;
75 int x_blocked_size = block_size * x_blocks;
76 int y_blocks = (y.cols() + block_size - 1) / block_size;
77 int y_blocked_size = block_size * y_blocks;
78 try {
80 cl::NDRange(x_blocked_size, y_blocked_size),
81 cl::NDRange(block_size, block_size), x_eval, y_eval, res, sigma * sigma,
82 -1.0 / length_scale, x.cols(), y.cols(), x.rows());
83 } catch (const cl::Error& e) {
84 check_opencl_error("gp_exponential_cov_cross", e);
85 }
86 return res;
87}
88
101template <typename T1, typename T2, typename T3,
104inline matrix_cl<return_type_t<T1, T2, T3>> gp_exponential_cov(
105 const T1& x, const T2 sigma, const T3 length_scale) {
106 const auto& x_eval = elt_divide(x, rowwise_broadcast(length_scale)).eval();
107 matrix_cl<return_type_t<T1, T2, T3>> res(x.cols(), x.cols());
108 int block_size = 16;
109 int n_blocks = (x.cols() + block_size - 1) / block_size;
110 int blocked_size = block_size * n_blocks;
111 try {
112 opencl_kernels::gp_exponential_cov(cl::NDRange(blocked_size, blocked_size),
113 cl::NDRange(block_size, block_size),
114 x_eval, res, sigma * sigma, -1.0,
115 x.cols(), x.rows());
116 } catch (const cl::Error& e) {
117 check_opencl_error("gp_exponential_cov", e);
118 }
119 return res;
120}
121
139template <
140 typename T1, typename T2, typename T3, typename T4,
141 require_all_kernel_expressions_and_none_scalar_t<T1, T2, T4>* = nullptr,
142 require_all_arithmetic_t<T3>* = nullptr>
143inline matrix_cl<return_type_t<T1, T2, T3, T4>> gp_exponential_cov(
144 const T1& x, const T2& y, const T3 sigma, const T4 length_scale) {
145 check_size_match("gp_exponential_cov_cross", "x", x.rows(), "y", y.rows());
146 matrix_cl<return_type_t<T1, T2, T3, T4>> res(x.cols(), y.cols());
147 const auto& x_eval = elt_divide(x, rowwise_broadcast(length_scale)).eval();
148 const auto& y_eval = elt_divide(y, rowwise_broadcast(length_scale)).eval();
149 int block_size = 16;
150 int x_blocks = (x.cols() + block_size - 1) / block_size;
151 int x_blocked_size = block_size * x_blocks;
152 int y_blocks = (y.cols() + block_size - 1) / block_size;
153 int y_blocked_size = block_size * y_blocks;
154 try {
156 cl::NDRange(x_blocked_size, y_blocked_size),
157 cl::NDRange(block_size, block_size), x_eval, y_eval, res, sigma * sigma,
158 -1.0, x.cols(), y.cols(), x.rows());
159 } catch (const cl::Error& e) {
160 check_opencl_error("gp_exponential_cov_cross", e);
161 }
162 return res;
163}
164
165} // namespace math
166} // namespace stan
167
168#endif
169#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
require_all_t< std::is_arithmetic< std::decay_t< Types > >... > require_all_arithmetic_t
Require all of the types satisfy std::is_arithmetic.
void check_opencl_error(const char *function, const cl::Error &e)
Throws the domain error with specifying the OpenCL error that occurred.
elt_divide_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_divide(T_a &&a, T_b &&b)
auto rowwise_broadcast(T &&a)
Broadcast an expression in rowwise dimmension.
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...
const kernel_cl< in_buffer, in_buffer, out_buffer, double, double, int, int, int > gp_exponential_cov_cross("gp_exponential_cov_cross", {gp_exponential_cov_cross_kernel_code})
See the docs for gp_exponential_cov_cross() .
const kernel_cl< in_buffer, out_buffer, double, double, int, int > gp_exponential_cov("gp_exponential_cov", {gp_exponential_cov_kernel_code})
See the docs for gp_exponential_cov() .
matrix_cl< return_type_t< T1, T2, T3 > > gp_exponential_cov(const T1 &x, const T2 sigma, const T3 length_scale)
Matern exponential kernel on the GPU.
static constexpr double e()
Return the base of the natural logarithm.
Definition constants.hpp:20
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...