|
5 | 5 | #include <gtest/gtest.h> |
6 | 6 | #include <algorithm> |
7 | 7 |
|
8 | | -TEST(OpenCLPrim, add_exceptions) { |
| 8 | +auto add_functor |
| 9 | + = [](const auto& a, const auto& b) { return stan::math::add(a, b).eval(); }; |
| 10 | + |
| 11 | +TEST(OpenCLPrim, add_v_small_zero) { |
| 12 | + stan::math::vector_d d1(3), d2(3); |
| 13 | + d1 << 1, 2, 3; |
| 14 | + d2 << 3, 2, 1; |
| 15 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d1, d2); |
| 16 | + |
| 17 | + stan::math::vector_d d0(0); |
| 18 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d0, d0); |
| 19 | + |
| 20 | + double d3 = 3.0; |
| 21 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d1, d3); |
| 22 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d3, d1); |
| 23 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d3, d0); |
| 24 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d0, d3); |
| 25 | +} |
| 26 | + |
| 27 | +TEST(OpenCLPrim, add_rv_small_zero) { |
| 28 | + stan::math::row_vector_d d1(3), d2(3); |
| 29 | + d1 << 1, 2, 3; |
| 30 | + d2 << 3, 2, 1; |
| 31 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d1, d2); |
| 32 | + |
| 33 | + stan::math::vector_d d0(0); |
| 34 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d0, d0); |
| 35 | + |
| 36 | + double d3 = 3.0; |
| 37 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d1, d3); |
| 38 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d3, d1); |
| 39 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d3, d0); |
| 40 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d0, d3); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(OpenCLPrim, add_m_small_zero) { |
| 44 | + stan::math::matrix_d d1(3, 3), d2(3, 3); |
| 45 | + d1 << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
| 46 | + d2 << 10, 100, 1000, 0, -10, -12, 2, 4, 8; |
| 47 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d1, d2); |
| 48 | + |
| 49 | + stan::math::matrix_d d0(0, 0); |
| 50 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d0, d0); |
| 51 | + |
| 52 | + double d3 = 3.0; |
| 53 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d1, d3); |
| 54 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d3, d1); |
| 55 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d3, d0); |
| 56 | + stan::math::test::compare_cpu_opencl_prim_rev(add_functor, d0, d3); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(OpenCLPrim, add_rev_exceptions) { |
| 60 | + using stan::math::matrix_cl; |
9 | 61 | stan::math::vector_d vd1(2), vd2(3); |
10 | | - stan::math::matrix_cl<double> vd11(vd1); |
11 | | - stan::math::matrix_cl<double> vd22(vd2); |
| 62 | + stan::math::var_value<matrix_cl<double>> vd11 = stan::math::to_matrix_cl(vd1); |
| 63 | + stan::math::var_value<matrix_cl<double>> vd22 = stan::math::to_matrix_cl(vd2); |
12 | 64 | EXPECT_THROW(stan::math::add(vd11, vd22), std::invalid_argument); |
13 | 65 |
|
14 | 66 | stan::math::row_vector_d rvd1(2), rvd2(3); |
15 | | - stan::math::matrix_cl<double> rvd11(rvd1); |
16 | | - stan::math::matrix_cl<double> rvd22(rvd2); |
| 67 | + stan::math::var_value<matrix_cl<double>> rvd11 |
| 68 | + = stan::math::to_matrix_cl(rvd1); |
| 69 | + stan::math::var_value<matrix_cl<double>> rvd22 |
| 70 | + = stan::math::to_matrix_cl(rvd2); |
17 | 71 | EXPECT_THROW(stan::math::add(rvd11, rvd22), std::invalid_argument); |
18 | 72 |
|
19 | 73 | stan::math::matrix_d md1(2, 2), md2(3, 3); |
20 | | - stan::math::matrix_cl<double> md11(md1); |
21 | | - stan::math::matrix_cl<double> md22(md2); |
| 74 | + stan::math::var_value<matrix_cl<double>> md11 = stan::math::to_matrix_cl(md1); |
| 75 | + stan::math::var_value<matrix_cl<double>> md22 = stan::math::to_matrix_cl(md2); |
22 | 76 | EXPECT_THROW(stan::math::add(md11, md22), std::invalid_argument); |
23 | 77 | } |
24 | 78 |
|
25 | | -TEST(OpenCLPrim, add_tri_value_check) { |
26 | | - Eigen::MatrixXd a(3, 3); |
27 | | - a << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
28 | | - Eigen::MatrixXd b = Eigen::MatrixXd::Ones(3, 3) * -3; |
29 | | - |
30 | | - stan::math::matrix_cl<double> a_cl(a); |
31 | | - stan::math::matrix_cl<double> b_cl(b); |
32 | | - stan::math::matrix_cl<double> c_cl(3, 3); |
33 | | - stan::math::matrix_cl<double> c_cl_fun(3, 3); |
34 | | - Eigen::MatrixXd c(3, 3); |
35 | | - |
36 | | - a_cl.view(stan::math::matrix_cl_view::Lower); |
37 | | - b_cl.view(stan::math::matrix_cl_view::Lower); |
38 | | - c_cl = a_cl + b_cl; |
39 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Lower); |
40 | | - c = stan::math::from_matrix_cl(c_cl); |
41 | | - EXPECT_MATRIX_NEAR((Eigen::MatrixXd(a.triangularView<Eigen::Lower>()) |
42 | | - + Eigen::MatrixXd(b.triangularView<Eigen::Lower>())), |
43 | | - c, 1E-8); |
44 | | - |
45 | | - c_cl_fun = add(a_cl, b_cl); |
46 | | - EXPECT_EQ(c_cl_fun.view(), stan::math::matrix_cl_view::Lower); |
47 | | - c = stan::math::from_matrix_cl(c_cl_fun); |
48 | | - EXPECT_MATRIX_NEAR((Eigen::MatrixXd(a.triangularView<Eigen::Lower>()) |
49 | | - + Eigen::MatrixXd(b.triangularView<Eigen::Lower>())), |
50 | | - c, 1E-8); |
51 | | - |
52 | | - a_cl.view(stan::math::matrix_cl_view::Lower); |
53 | | - b_cl.view(stan::math::matrix_cl_view::Upper); |
54 | | - c_cl = a_cl + b_cl; |
55 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
56 | | - c = stan::math::from_matrix_cl(c_cl); |
57 | | - EXPECT_MATRIX_NEAR((Eigen::MatrixXd(a.triangularView<Eigen::Lower>()) |
58 | | - + Eigen::MatrixXd(b.triangularView<Eigen::Upper>())), |
59 | | - c, 1E-8); |
60 | | - |
61 | | - c_cl_fun = add(a_cl, b_cl); |
62 | | - EXPECT_EQ(c_cl_fun.view(), stan::math::matrix_cl_view::Entire); |
63 | | - c = stan::math::from_matrix_cl(c_cl_fun); |
64 | | - EXPECT_MATRIX_NEAR((Eigen::MatrixXd(a.triangularView<Eigen::Lower>()) |
65 | | - + Eigen::MatrixXd(b.triangularView<Eigen::Upper>())), |
66 | | - c, 1E-8); |
67 | | - |
68 | | - a_cl.view(stan::math::matrix_cl_view::Upper); |
69 | | - b_cl.view(stan::math::matrix_cl_view::Lower); |
70 | | - c_cl = a_cl + b_cl; |
71 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
72 | | - c = stan::math::from_matrix_cl(c_cl); |
73 | | - EXPECT_MATRIX_NEAR((Eigen::MatrixXd(a.triangularView<Eigen::Upper>()) |
74 | | - + Eigen::MatrixXd(b.triangularView<Eigen::Lower>())), |
75 | | - c, 1E-8); |
76 | | - |
77 | | - c_cl_fun = add(a_cl, b_cl); |
78 | | - EXPECT_EQ(c_cl_fun.view(), stan::math::matrix_cl_view::Entire); |
79 | | - c = stan::math::from_matrix_cl(c_cl_fun); |
80 | | - EXPECT_MATRIX_NEAR((Eigen::MatrixXd(a.triangularView<Eigen::Upper>()) |
81 | | - + Eigen::MatrixXd(b.triangularView<Eigen::Lower>())), |
82 | | - c, 1E-8); |
83 | | - |
84 | | - a_cl.view(stan::math::matrix_cl_view::Entire); |
85 | | - b_cl.view(stan::math::matrix_cl_view::Lower); |
86 | | - c_cl = a_cl + b_cl; |
87 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
88 | | - c = stan::math::from_matrix_cl(c_cl); |
89 | | - EXPECT_MATRIX_NEAR((a + Eigen::MatrixXd(b.triangularView<Eigen::Lower>())), c, |
90 | | - 1E-8); |
91 | | - |
92 | | - c_cl_fun = add(a_cl, b_cl); |
93 | | - EXPECT_EQ(c_cl_fun.view(), stan::math::matrix_cl_view::Entire); |
94 | | - c = stan::math::from_matrix_cl(c_cl_fun); |
95 | | - EXPECT_MATRIX_NEAR((a + Eigen::MatrixXd(b.triangularView<Eigen::Lower>())), c, |
96 | | - 1E-8); |
97 | | -} |
98 | | - |
99 | | -TEST(OpenCLPrim, add_tri_scalar_value_check) { |
100 | | - Eigen::MatrixXd a(3, 3); |
101 | | - a << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
102 | | - stan::math::matrix_cl<double> a_cl(a); |
103 | | - stan::math::matrix_cl<double> c_cl(3, 3); |
104 | | - Eigen::MatrixXd c(3, 3); |
105 | | - |
106 | | - using stan::math::add; |
107 | | - a_cl.view(stan::math::matrix_cl_view::Lower); |
108 | | - c_cl = add(a_cl, 1.5); |
109 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
110 | | - c = stan::math::from_matrix_cl(c_cl); |
111 | | - EXPECT_MATRIX_NEAR( |
112 | | - add(Eigen::MatrixXd(a.triangularView<Eigen::Lower>()), 1.5), c, 1E-8); |
113 | | - |
114 | | - a_cl.view(stan::math::matrix_cl_view::Lower); |
115 | | - c_cl = add(1.5, a_cl); |
116 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
117 | | - c = stan::math::from_matrix_cl(c_cl); |
118 | | - EXPECT_MATRIX_NEAR( |
119 | | - add(1.5, Eigen::MatrixXd(a.triangularView<Eigen::Lower>())), c, 1E-8); |
120 | | - |
121 | | - a_cl.view(stan::math::matrix_cl_view::Upper); |
122 | | - c_cl = add(a_cl, 1.5); |
123 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
124 | | - c = stan::math::from_matrix_cl(c_cl); |
125 | | - EXPECT_MATRIX_NEAR( |
126 | | - add(Eigen::MatrixXd(a.triangularView<Eigen::Upper>()), 1.5), c, 1E-8); |
127 | | - |
128 | | - a_cl.view(stan::math::matrix_cl_view::Upper); |
129 | | - c_cl = add(1.5, a_cl); |
130 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
131 | | - c = stan::math::from_matrix_cl(c_cl); |
132 | | - EXPECT_MATRIX_NEAR( |
133 | | - add(1.5, Eigen::MatrixXd(a.triangularView<Eigen::Upper>())), c, 1E-8); |
134 | | - |
135 | | - a_cl.view(stan::math::matrix_cl_view::Entire); |
136 | | - c_cl = add(a_cl, 1.5); |
137 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
138 | | - c = stan::math::from_matrix_cl(c_cl); |
139 | | - EXPECT_MATRIX_NEAR(add(a, 1.5), c, 1E-8); |
140 | | - |
141 | | - a_cl.view(stan::math::matrix_cl_view::Entire); |
142 | | - c_cl = add(1.5, a_cl); |
143 | | - EXPECT_EQ(c_cl.view(), stan::math::matrix_cl_view::Entire); |
144 | | - c = stan::math::from_matrix_cl(c_cl); |
145 | | - EXPECT_MATRIX_NEAR(add(1.5, a), c, 1E-8); |
146 | | -} |
147 | | - |
148 | | -TEST(OpenCLPrim, add_batch) { |
149 | | - // used to represent 5 matrices of size 10x10 |
150 | | - const int batch_size = 11; |
151 | | - const int size = 13; |
152 | | - stan::math::matrix_d a(size, size * batch_size); |
153 | | - stan::math::matrix_d a_res(size, size); |
154 | | - for (int k = 0; k < batch_size; k++) { |
155 | | - for (int i = 0; i < size; i++) |
156 | | - for (int j = 0; j < size; j++) { |
157 | | - a(i, k * size + j) = k; |
158 | | - } |
159 | | - } |
160 | | - stan::math::matrix_cl<double> a_cl(a); |
161 | | - stan::math::matrix_cl<double> a_cl_res(size, size); |
162 | | - stan::math::opencl_kernels::add_batch(cl::NDRange(size, size), a_cl_res, a_cl, |
163 | | - size, size, batch_size); |
164 | | - a_res = stan::math::from_matrix_cl(a_cl_res); |
165 | | - for (int k = 0; k < batch_size; k++) { |
166 | | - for (int i = 0; i < size; i++) |
167 | | - for (int j = 0; j < size; j++) { |
168 | | - a(i, j) += a(i, k * size + j); |
169 | | - } |
170 | | - } |
171 | | - for (int i = 0; i < size; i++) { |
172 | | - for (int j = 0; j < size; j++) { |
173 | | - EXPECT_EQ(a(i, j), a_res(i, j)); |
174 | | - } |
175 | | - } |
176 | | -} |
177 | 79 |
|
178 | 80 | TEST(OpenCLPrim, add_aliasing) { |
179 | 81 | stan::math::matrix_d d1(3, 3); |
|
0 commit comments