|
| 1 | +#ifndef STAN_MATH_OPENCL_KERNELS_gp_MATERN52_COV_HPP |
| 2 | +#define STAN_MATH_OPENCL_KERNELS_gp_MATERN52_COV_HPP |
| 3 | +#ifdef STAN_OPENCL |
| 4 | + |
| 5 | +#include <stan/math/opencl/kernel_cl.hpp> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +namespace stan { |
| 9 | +namespace math { |
| 10 | +namespace opencl_kernels { |
| 11 | +// \cond |
| 12 | +static const std::string gp_matern52_cov_kernel_code = STRINGIFY( |
| 13 | + // \endcond |
| 14 | + /** \ingroup opencl_kernels |
| 15 | + * GPU part of calculation of Matern 5/2 kernel. |
| 16 | + * |
| 17 | + * @param[in] x input vector or matrix |
| 18 | + * @param[out] res squared distances between elements of x |
| 19 | + * @param sigma_sq squared standard deviation |
| 20 | + * @param root_5_inv_l sqrt(5.0) / length_scale |
| 21 | + * @param inv_l_sq_5_3 5.0 / 3.0 / square(length_scale) |
| 22 | + * @param size number of elements in x |
| 23 | + * @param element_size the number of doubles that make one element of x |
| 24 | + */ |
| 25 | + __kernel void gp_matern52_cov( |
| 26 | + const __global double* x, __global double* res, const double sigma_sq, |
| 27 | + const double root_5_inv_l, const double inv_l_sq_5_3, const int size, |
| 28 | + const int element_size) { |
| 29 | + const int i = get_global_id(0); |
| 30 | + const int j = get_global_id(1); |
| 31 | + if (i < size && j < size) { |
| 32 | + if (i > j) { |
| 33 | + double sum = 0; |
| 34 | + for (int k = 0; k < element_size; k++) { |
| 35 | + double d = x[i * element_size + k] - x[j * element_size + k]; |
| 36 | + sum += d * d; |
| 37 | + } |
| 38 | + double dist = sqrt(sum); |
| 39 | + double a = sigma_sq * (1.0 + root_5_inv_l * dist + inv_l_sq_5_3 * sum) |
| 40 | + * exp(-root_5_inv_l * dist); |
| 41 | + res[j * size + i] = a; |
| 42 | + res[i * size + j] = a; |
| 43 | + } else if (i == j) { |
| 44 | + res[j * size + i] = sigma_sq; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + // \cond |
| 49 | +); |
| 50 | +// \endcond |
| 51 | + |
| 52 | +/** \ingroup opencl_kernels |
| 53 | + * See the docs for \link kernels/gp_matern52_cov.hpp gp_matern52_cov() \endlink |
| 54 | + */ |
| 55 | +const kernel_cl<in_buffer, out_buffer, double, double, double, int, int> |
| 56 | + gp_matern52_cov("gp_matern52_cov", {gp_matern52_cov_kernel_code}); |
| 57 | + |
| 58 | +// \cond |
| 59 | +static const std::string gp_matern52_cov_cross_kernel_code = STRINGIFY( |
| 60 | + // \endcond |
| 61 | + /** \ingroup opencl_kernels |
| 62 | + * GPU part of calculation of Matern 5/2 kernel. |
| 63 | + * |
| 64 | + * This function is for the cross covariance |
| 65 | + * matrix needed to compute the posterior predictive density. |
| 66 | + * |
| 67 | + * @param[in] x1 first input vector or matrix |
| 68 | + * @param[in] x2 second input vector or matrix |
| 69 | + * @param[out] res squared distances between elements of x |
| 70 | + * @param sigma_sq squared standard deviation |
| 71 | + * @param root_5_inv_l sqrt(5.0) / length_scale |
| 72 | + * @param inv_l_sq_5_3 5.0 / 3.0 / square(length_scale) |
| 73 | + * @param size1 number of elements in x1 |
| 74 | + * @param size2 number of elements in x2 |
| 75 | + * @param element_size the number of doubles that make one element of x and |
| 76 | + * y |
| 77 | + */ |
| 78 | + __kernel void gp_matern52_cov_cross( |
| 79 | + const __global double* x1, const __global double* x2, |
| 80 | + __global double* res, const double sigma_sq, const double root_5_inv_l, |
| 81 | + const double inv_l_sq_5_3, const int size1, const int size2, |
| 82 | + const int element_size) { |
| 83 | + const int i = get_global_id(0); |
| 84 | + const int j = get_global_id(1); |
| 85 | + if (i < size1 && j < size2) { |
| 86 | + double sum = 0; |
| 87 | + for (int k = 0; k < element_size; k++) { |
| 88 | + double d = x1[i * element_size + k] - x2[j * element_size + k]; |
| 89 | + sum += d * d; |
| 90 | + } |
| 91 | + double dist = sqrt(sum); |
| 92 | + res[j * size1 + i] = sigma_sq |
| 93 | + * (1.0 + root_5_inv_l * dist + inv_l_sq_5_3 * sum) |
| 94 | + * exp(-root_5_inv_l * dist); |
| 95 | + } |
| 96 | + } |
| 97 | + // \cond |
| 98 | +); |
| 99 | +// \endcond |
| 100 | + |
| 101 | +/** \ingroup opencl_kernels |
| 102 | + * See the docs for \link kernels/gp_matern52_cov.hpp gp_matern52_cov_cross() |
| 103 | + * \endlink |
| 104 | + */ |
| 105 | +const kernel_cl<in_buffer, in_buffer, out_buffer, double, double, double, int, |
| 106 | + int, int> |
| 107 | + gp_matern52_cov_cross("gp_matern52_cov_cross", |
| 108 | + {gp_matern52_cov_cross_kernel_code}); |
| 109 | + |
| 110 | +} // namespace opencl_kernels |
| 111 | +} // namespace math |
| 112 | +} // namespace stan |
| 113 | +#endif |
| 114 | +#endif |
0 commit comments