1 #ifdef STAN_OPENCL
2 #include <stan/math/opencl/rev.hpp>
3 #include <stan/math.hpp>
4 #include <gtest/gtest.h>
5 #include <test/unit/math/opencl/util.hpp>
6 #include <vector>
7
TEST(ProbDistributionsUniform,error_checking)8 TEST(ProbDistributionsUniform, error_checking) {
9 int N = 3;
10
11 Eigen::VectorXd y(N);
12 y << 0.3, 0.8, 1.0;
13 Eigen::VectorXd y_size(N - 1);
14 y_size << 0.3, 0.8;
15 Eigen::VectorXd y_value(N);
16 y_value << 0.3, NAN, 0.5;
17
18 Eigen::VectorXd alpha(N);
19 alpha << 0.2, -0.8, -2.0;
20 Eigen::VectorXd alpha_size(N - 1);
21 alpha_size << 0.3, 0.8;
22 Eigen::VectorXd alpha_value(N);
23 alpha_value << 0.2, INFINITY, -1.5;
24
25 Eigen::VectorXd beta(N);
26 beta << 0.3, 0.8, -1.0;
27 Eigen::VectorXd beta_size(N - 1);
28 beta_size << 0.3, 0.8;
29 Eigen::VectorXd beta_value1(N);
30 beta_value1 << 0.3, INFINITY, 0.5;
31 Eigen::VectorXd beta_value2(N);
32 beta_value2 << 0.3, -0.8, 0.5;
33
34 stan::math::matrix_cl<double> y_cl(y);
35 stan::math::matrix_cl<double> y_size_cl(y_size);
36 stan::math::matrix_cl<double> y_value_cl(y_value);
37 stan::math::matrix_cl<double> alpha_cl(alpha);
38 stan::math::matrix_cl<double> alpha_size_cl(alpha_size);
39 stan::math::matrix_cl<double> alpha_value_cl(alpha_value);
40 stan::math::matrix_cl<double> beta_cl(beta);
41 stan::math::matrix_cl<double> beta_size_cl(beta_size);
42 stan::math::matrix_cl<double> beta_value1_cl(beta_value1);
43 stan::math::matrix_cl<double> beta_value2_cl(beta_value2);
44
45 EXPECT_NO_THROW(stan::math::uniform_lpdf(y_cl, alpha_cl, beta_cl));
46
47 EXPECT_THROW(stan::math::uniform_lpdf(y_size_cl, alpha_cl, beta_cl),
48 std::invalid_argument);
49 EXPECT_THROW(stan::math::uniform_lpdf(y_cl, alpha_size_cl, beta_cl),
50 std::invalid_argument);
51 EXPECT_THROW(stan::math::uniform_lpdf(y_cl, alpha_cl, beta_size_cl),
52 std::invalid_argument);
53
54 EXPECT_THROW(stan::math::uniform_lpdf(y_value_cl, alpha_cl, beta_cl),
55 std::domain_error);
56 EXPECT_THROW(stan::math::uniform_lpdf(y_cl, alpha_value_cl, beta_cl),
57 std::domain_error);
58 EXPECT_THROW(stan::math::uniform_lpdf(y_cl, alpha_cl, beta_value1_cl),
59 std::domain_error);
60 EXPECT_THROW(stan::math::uniform_lpdf(y_cl, alpha_cl, beta_value2_cl),
61 std::domain_error);
62 }
63
64 auto uniform_lpdf_functor
__anond04750020102(const auto& y, const auto& alpha, const auto& beta) 65 = [](const auto& y, const auto& alpha, const auto& beta) {
66 return stan::math::uniform_lpdf(y, alpha, beta);
67 };
68 auto uniform_lpdf_functor_propto
__anond04750020202(const auto& y, const auto& alpha, const auto& beta) 69 = [](const auto& y, const auto& alpha, const auto& beta) {
70 return stan::math::uniform_lpdf<true>(y, alpha, beta);
71 };
72
TEST(ProbDistributionsUniform,opencl_matches_cpu_small)73 TEST(ProbDistributionsUniform, opencl_matches_cpu_small) {
74 int N = 3;
75
76 Eigen::VectorXd y(N);
77 y << 0.3, 0.8, 1.0;
78 Eigen::VectorXd alpha(N);
79 alpha << 0.2, 0.4, -1.0;
80 Eigen::VectorXd beta(N);
81 beta << 0.4, 1.5, 5.0;
82
83 stan::math::test::compare_cpu_opencl_prim_rev(uniform_lpdf_functor, y, alpha,
84 beta);
85 stan::math::test::compare_cpu_opencl_prim_rev(uniform_lpdf_functor_propto, y,
86 alpha, beta);
87 stan::math::test::compare_cpu_opencl_prim_rev(
88 uniform_lpdf_functor, y.transpose().eval(), alpha.transpose().eval(),
89 beta.transpose().eval());
90 stan::math::test::compare_cpu_opencl_prim_rev(
91 uniform_lpdf_functor_propto, y.transpose().eval(),
92 alpha.transpose().eval(), beta.transpose().eval());
93 }
94
TEST(ProbDistributionsUniform,opencl_matches_cpu_small_y_out_of_bounds)95 TEST(ProbDistributionsUniform, opencl_matches_cpu_small_y_out_of_bounds) {
96 int N = 3;
97 int M = 2;
98
99 Eigen::VectorXd y(N);
100 y << 0.3, 0.8, 14.0;
101 Eigen::VectorXd alpha(N);
102 alpha << 0.2, 0.4, -1.0;
103 Eigen::VectorXd beta(N);
104 beta << 0.4, 1.5, 5.0;
105
106 stan::math::test::compare_cpu_opencl_prim_rev(uniform_lpdf_functor, y, alpha,
107 beta);
108 stan::math::test::compare_cpu_opencl_prim_rev(uniform_lpdf_functor_propto, y,
109 alpha, beta);
110 }
111
TEST(ProbDistributionsUniform,opencl_broadcast_y)112 TEST(ProbDistributionsUniform, opencl_broadcast_y) {
113 int N = 3;
114
115 double y_scal = 0.7;
116 Eigen::VectorXd alpha(N);
117 alpha << 0.5, -1.2, -1.0;
118 Eigen::VectorXd beta(N);
119 beta << 1.3, 0.8, 1.0;
120
121 stan::math::test::test_opencl_broadcasting_prim_rev<0>(uniform_lpdf_functor,
122 y_scal, alpha, beta);
123 stan::math::test::test_opencl_broadcasting_prim_rev<0>(
124 uniform_lpdf_functor_propto, y_scal, alpha, beta);
125 stan::math::test::test_opencl_broadcasting_prim_rev<0>(
126 uniform_lpdf_functor, y_scal, alpha.transpose().eval(), beta);
127 stan::math::test::test_opencl_broadcasting_prim_rev<0>(
128 uniform_lpdf_functor_propto, y_scal, alpha, beta.transpose().eval());
129 }
130
TEST(ProbDistributionsUniform,opencl_broadcast_alpha)131 TEST(ProbDistributionsUniform, opencl_broadcast_alpha) {
132 int N = 3;
133
134 Eigen::VectorXd y(N);
135 y << 0.3, 0.8, 1.0;
136 double alpha_scal = -12.3;
137 Eigen::VectorXd beta(N);
138 beta << 1.3, 2.8, 3.0;
139
140 stan::math::test::test_opencl_broadcasting_prim_rev<1>(uniform_lpdf_functor,
141 y, alpha_scal, beta);
142 stan::math::test::test_opencl_broadcasting_prim_rev<1>(
143 uniform_lpdf_functor_propto, y, alpha_scal, beta);
144 stan::math::test::test_opencl_broadcasting_prim_rev<1>(
145 uniform_lpdf_functor, y.transpose().eval(), alpha_scal, beta);
146 stan::math::test::test_opencl_broadcasting_prim_rev<1>(
147 uniform_lpdf_functor_propto, y, alpha_scal, beta.transpose().eval());
148 }
149
TEST(ProbDistributionsUniform,opencl_broadcast_beta)150 TEST(ProbDistributionsUniform, opencl_broadcast_beta) {
151 int N = 3;
152
153 Eigen::VectorXd y(N);
154 y << 0.3, 0.8, 1.0;
155 Eigen::VectorXd alpha(N);
156 alpha << -0.3, 0.5, -1.0;
157 double beta_scal = 12.3;
158
159 stan::math::test::test_opencl_broadcasting_prim_rev<2>(uniform_lpdf_functor,
160 y, alpha, beta_scal);
161 stan::math::test::test_opencl_broadcasting_prim_rev<2>(
162 uniform_lpdf_functor_propto, y, alpha, beta_scal);
163 stan::math::test::test_opencl_broadcasting_prim_rev<2>(
164 uniform_lpdf_functor, y.transpose().eval(), alpha, beta_scal);
165 stan::math::test::test_opencl_broadcasting_prim_rev<2>(
166 uniform_lpdf_functor_propto, y, alpha.transpose().eval(), beta_scal);
167 }
168
TEST(ProbDistributionsUniform,opencl_matches_cpu_big)169 TEST(ProbDistributionsUniform, opencl_matches_cpu_big) {
170 int N = 153;
171
172 Eigen::Matrix<double, Eigen::Dynamic, 1> alpha
173 = Eigen::Array<double, Eigen::Dynamic, 1>::Random(N, 1);
174 Eigen::Matrix<double, Eigen::Dynamic, 1> beta
175 = Eigen::Array<double, Eigen::Dynamic, 1>::Random(N, 1).abs()
176 + alpha.array();
177 Eigen::Matrix<double, Eigen::Dynamic, 1> y
178 = Eigen::Array<double, Eigen::Dynamic, 1>::Random(N, 1).abs()
179 * (beta.array() - alpha.array())
180 + beta.array();
181
182 stan::math::test::compare_cpu_opencl_prim_rev(uniform_lpdf_functor, y, alpha,
183 beta);
184 stan::math::test::compare_cpu_opencl_prim_rev(uniform_lpdf_functor_propto, y,
185 alpha, beta);
186 stan::math::test::compare_cpu_opencl_prim_rev(
187 uniform_lpdf_functor, y.transpose().eval(), alpha.transpose().eval(),
188 beta.transpose().eval());
189 stan::math::test::compare_cpu_opencl_prim_rev(
190 uniform_lpdf_functor_propto, y.transpose().eval(),
191 alpha.transpose().eval(), beta.transpose().eval());
192 }
193
194 #endif
195