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(ProbDistributionsBernoulliCdf,error_checking)8 TEST(ProbDistributionsBernoulliCdf, error_checking) {
9 int N = 3;
10
11 std::vector<int> n{1, -2, 11};
12 std::vector<int> n_size{1, 0, 1, 0};
13 Eigen::VectorXd theta(N);
14 theta << 0.0, 0.8, 1.0;
15 Eigen::VectorXd theta_size(N - 1);
16 theta_size << 0.3, 0.8;
17 Eigen::VectorXd theta_value1(N);
18 theta_value1 << 0.3, -0.8, 0.5;
19 Eigen::VectorXd theta_value2(N);
20 theta_value2 << 0.3, 10.8, 0.5;
21
22 stan::math::matrix_cl<int> n_cl(n);
23 stan::math::matrix_cl<int> n_size_cl(n_size);
24 stan::math::matrix_cl<double> theta_cl(theta);
25 stan::math::matrix_cl<double> theta_size_cl(theta_size);
26 stan::math::matrix_cl<double> theta_value1_cl(theta_value1);
27 stan::math::matrix_cl<double> theta_value2_cl(theta_value2);
28
29 EXPECT_NO_THROW(stan::math::bernoulli_cdf(n_cl, theta_cl));
30
31 EXPECT_THROW(stan::math::bernoulli_cdf(n_size_cl, theta_cl),
32 std::invalid_argument);
33 EXPECT_THROW(stan::math::bernoulli_cdf(n_cl, theta_size_cl),
34 std::invalid_argument);
35
36 EXPECT_THROW(stan::math::bernoulli_cdf(n_cl, theta_value1_cl),
37 std::domain_error);
38 EXPECT_THROW(stan::math::bernoulli_cdf(n_cl, theta_value2_cl),
39 std::domain_error);
40 }
41
__anon2823e4f50102(const auto& n, const auto& theta) 42 auto bernoulli_cdf_functor = [](const auto& n, const auto& theta) {
43 return stan::math::bernoulli_cdf(n, theta);
44 };
45
TEST(ProbDistributionsBernoulliCdf,opencl_matches_cpu_small)46 TEST(ProbDistributionsBernoulliCdf, opencl_matches_cpu_small) {
47 int N = 3;
48 int M = 2;
49
50 std::vector<int> n{0, 1, 3};
51 Eigen::VectorXd theta(N);
52 theta << 0.3, 0.8, 1.0;
53
54 stan::math::test::compare_cpu_opencl_prim_rev(bernoulli_cdf_functor, n,
55 theta);
56 stan::math::test::compare_cpu_opencl_prim_rev(bernoulli_cdf_functor, n,
57 theta.transpose().eval());
58 }
59
TEST(ProbDistributionsBernoulliCdf,opencl_matches_cpu_small_n_negative)60 TEST(ProbDistributionsBernoulliCdf, opencl_matches_cpu_small_n_negative) {
61 int N = 3;
62 int M = 2;
63
64 std::vector<int> n{0, 1, -5};
65 Eigen::VectorXd theta(N);
66 theta << 0.3, 0.8, 1.0;
67
68 stan::math::test::compare_cpu_opencl_prim_rev(bernoulli_cdf_functor, n,
69 theta);
70 stan::math::test::compare_cpu_opencl_prim_rev(bernoulli_cdf_functor, n,
71 theta.transpose().eval());
72 }
73
TEST(ProbDistributionsBernoulliCdf,opencl_broadcast_n)74 TEST(ProbDistributionsBernoulliCdf, opencl_broadcast_n) {
75 int N = 3;
76
77 int n_scal = 1;
78 Eigen::VectorXd theta(N);
79 theta << 0.3, 0.8, 1.0;
80
81 stan::math::test::test_opencl_broadcasting_prim_rev<0>(bernoulli_cdf_functor,
82 n_scal, theta);
83 }
84
TEST(ProbDistributionsBernoulliCdf,opencl_broadcast_theta)85 TEST(ProbDistributionsBernoulliCdf, opencl_broadcast_theta) {
86 int N = 3;
87
88 std::vector<int> n{0, 1, 0};
89 double theta_scal = 0.4;
90
91 stan::math::test::test_opencl_broadcasting_prim_rev<1>(bernoulli_cdf_functor,
92 n, theta_scal);
93 }
94
TEST(ProbDistributionsBernoulliCdf,opencl_matches_cpu_big)95 TEST(ProbDistributionsBernoulliCdf, opencl_matches_cpu_big) {
96 int N = 153;
97
98 std::vector<int> n(N);
99 for (int i = 0; i < N; i++) {
100 n[i] = Eigen::Array<int, Eigen::Dynamic, 1>::Random(1, 1).abs()(0) % 2;
101 }
102 Eigen::Matrix<double, Eigen::Dynamic, 1> theta
103 = Eigen::Array<double, Eigen::Dynamic, 1>::Random(N, 1).abs();
104
105 stan::math::test::compare_cpu_opencl_prim_rev(bernoulli_cdf_functor, n,
106 theta);
107 stan::math::test::compare_cpu_opencl_prim_rev(bernoulli_cdf_functor, n,
108 theta.transpose().eval());
109 }
110
111 #endif
112