|
5 | 5 | #include <gtest/gtest.h> |
6 | 6 | #include <algorithm> |
7 | 7 |
|
| 8 | +TEST(OpenCLPrim, add_exceptions) { |
| 9 | + stan::math::vector_d vd1(2), vd2(3); |
| 10 | + stan::math::matrix_cl<double> vd11(vd1); |
| 11 | + stan::math::matrix_cl<double> vd22(vd2); |
| 12 | + EXPECT_THROW(stan::math::add(vd11, vd22), std::invalid_argument); |
| 13 | + |
| 14 | + 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); |
| 17 | + EXPECT_THROW(stan::math::add(rvd11, rvd22), std::invalid_argument); |
| 18 | + |
| 19 | + 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); |
| 22 | + EXPECT_THROW(stan::math::add(md11, md22), std::invalid_argument); |
| 23 | +} |
| 24 | + |
| 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 | + |
8 | 178 | TEST(OpenCLPrim, add_aliasing) { |
9 | 179 | stan::math::matrix_d d1(3, 3); |
10 | 180 | d1 << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
11 | 181 | using stan::math::matrix_cl; |
12 | | - using stan::math::var; |
13 | 182 | using stan::math::var_value; |
| 183 | + using stan::math::var; |
14 | 184 | using varmat_cl = var_value<matrix_cl<double>>; |
15 | 185 | varmat_cl d11 = stan::math::to_matrix_cl(d1); |
16 | 186 | // Add the same matrix as the left and right hand side |
|
0 commit comments