1 #ifdef STAN_OPENCL
2 #include <stan/math/prim/fun/Eigen.hpp>
3 #include <stan/math/opencl/kernel_generator.hpp>
4 #include <stan/math/opencl/matrix_cl.hpp>
5 #include <stan/math/opencl/copy.hpp>
6 #include <test/unit/math/opencl/kernel_generator/reference_kernel.hpp>
7 #include <stan/math.hpp>
8 #include <test/unit/util.hpp>
9 #include <gtest/gtest.h>
10 #include <string>
11
TEST(KernelGenerator,as_column_vector_or_scalar_errors)12 TEST(KernelGenerator, as_column_vector_or_scalar_errors) {
13 using stan::math::as_column_vector_or_scalar;
14 stan::math::matrix_cl<double> m(7, 1);
15 stan::math::matrix_cl<double> n(1, 7);
16 stan::math::matrix_cl<double> j(7, 7);
17
18 auto block_m = stan::math::block_zero_based(m, 0, 0, 7, 1);
19 auto block_n = stan::math::block_zero_based(n, 0, 0, 1, 7);
20
21 EXPECT_THROW(as_column_vector_or_scalar(j), std::invalid_argument);
22 EXPECT_THROW(block_n = as_column_vector_or_scalar(n), std::invalid_argument);
23 EXPECT_THROW(block_n = as_column_vector_or_scalar(m), std::invalid_argument);
24 EXPECT_NO_THROW(block_m = as_column_vector_or_scalar(m));
25 EXPECT_NO_THROW(as_column_vector_or_scalar(block_m) = m);
26 EXPECT_NO_THROW(as_column_vector_or_scalar(block_m)
27 = as_column_vector_or_scalar(m));
28 EXPECT_NO_THROW(block_m = as_column_vector_or_scalar(n));
29 EXPECT_NO_THROW(as_column_vector_or_scalar(block_n) = m);
30 EXPECT_NO_THROW(as_column_vector_or_scalar(block_n)
31 = as_column_vector_or_scalar(n));
32 auto a = as_column_vector_or_scalar(m);
33 EXPECT_NO_THROW(a = a);
34 EXPECT_NO_THROW(a = a + 1);
35 }
36
TEST(KernelGenerator,as_column_vector_or_scalar_vector_test)37 TEST(KernelGenerator, as_column_vector_or_scalar_vector_test) {
38 using Eigen::MatrixXd;
39 using Eigen::VectorXd;
40 using stan::math::matrix_cl;
41 VectorXd m(6, 1);
42 m << 1.1, 1.2, 1.3, 1.4, 1.5, 1.6;
43
44 matrix_cl<double> m_cl(m);
45 auto tmp = stan::math::as_column_vector_or_scalar(m_cl);
46 matrix_cl<double> res_cl = tmp;
47
48 MatrixXd res = stan::math::from_matrix_cl(res_cl);
49 MatrixXd correct = stan::math::as_column_vector_or_scalar(m);
50 EXPECT_MATRIX_NEAR(correct, res, 1e-9);
51 }
52
TEST(KernelGenerator,as_column_vector_or_scalar_row_vector_test)53 TEST(KernelGenerator, as_column_vector_or_scalar_row_vector_test) {
54 using Eigen::MatrixXd;
55 using Eigen::RowVectorXd;
56 using stan::math::matrix_cl;
57 RowVectorXd m(6);
58 m << 1.1, 1.2, 1.3, 1.4, 1.5, 1.6;
59
60 matrix_cl<double> m_cl(m);
61 auto tmp = stan::math::as_column_vector_or_scalar(m_cl);
62 matrix_cl<double> res_cl = tmp;
63
64 MatrixXd res = stan::math::from_matrix_cl(res_cl);
65 MatrixXd correct = stan::math::as_column_vector_or_scalar(m);
66 EXPECT_MATRIX_NEAR(correct, res, 1e-9);
67 }
68
TEST(KernelGenerator,double_as_column_vector_or_scalar_test)69 TEST(KernelGenerator, double_as_column_vector_or_scalar_test) {
70 using Eigen::MatrixXd;
71 using Eigen::VectorXd;
72 using stan::math::matrix_cl;
73 VectorXd m(6);
74 m << 1.1, 1.2, 1.3, 1.4, 1.5, 1.6;
75
76 matrix_cl<double> m_cl(m);
77 auto tmp = stan::math::as_column_vector_or_scalar(
78 stan::math::as_column_vector_or_scalar(m_cl));
79 matrix_cl<double> res_cl = tmp;
80
81 MatrixXd res = stan::math::from_matrix_cl(res_cl);
82 MatrixXd correct = m;
83 EXPECT_MATRIX_NEAR(correct, res, 1e-9);
84 }
85
TEST(KernelGenerator,double_as_column_vector_or_scalar_accepts_lvalue_test)86 TEST(KernelGenerator, double_as_column_vector_or_scalar_accepts_lvalue_test) {
87 using Eigen::MatrixXd;
88 using Eigen::RowVectorXd;
89 using stan::math::matrix_cl;
90 RowVectorXd m(6);
91 m << 1.1, 1.2, 1.3, 1.4, 1.5, 1.6;
92
93 matrix_cl<double> m_cl(m);
94 auto tmp2 = stan::math::as_column_vector_or_scalar(m_cl);
95 auto tmp = stan::math::as_column_vector_or_scalar(tmp2);
96 matrix_cl<double> res_cl = tmp;
97
98 MatrixXd res = stan::math::from_matrix_cl(res_cl);
99 MatrixXd correct = stan::math::as_column_vector_or_scalar(m);
100 EXPECT_MATRIX_NEAR(correct, res, 1e-9);
101 }
102
TEST(KernelGenerator,as_column_vector_or_scalar_vector_block_test)103 TEST(KernelGenerator, as_column_vector_or_scalar_vector_block_test) {
104 using Eigen::MatrixXd;
105 using stan::math::matrix_cl;
106 MatrixXd m(5, 5);
107 m << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
108 21, 22, 23, 24, 25;
109
110 matrix_cl<double> m_cl(m);
111 auto tmp2 = stan::math::block_zero_based(m_cl, 1, 2, 3, 1);
112 auto tmp = stan::math::as_column_vector_or_scalar(tmp2);
113 matrix_cl<double> res_cl = tmp;
114
115 MatrixXd res = stan::math::from_matrix_cl(res_cl);
116 MatrixXd correct
117 = stan::math::as_column_vector_or_scalar(m.col(2).segment(1, 3));
118 EXPECT_MATRIX_NEAR(correct, res, 1e-9);
119 }
120
TEST(KernelGenerator,as_column_vector_or_scalar_row_vector_block_test)121 TEST(KernelGenerator, as_column_vector_or_scalar_row_vector_block_test) {
122 using Eigen::MatrixXd;
123 using stan::math::matrix_cl;
124 MatrixXd m(5, 5);
125 m << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
126 21, 22, 23, 24, 25;
127
128 matrix_cl<double> m_cl(m);
129 auto tmp2 = stan::math::block_zero_based(m_cl, 2, 1, 1, 3);
130 auto tmp = stan::math::as_column_vector_or_scalar(tmp2);
131 matrix_cl<double> res_cl = tmp;
132
133 MatrixXd res = stan::math::from_matrix_cl(res_cl);
134 MatrixXd correct
135 = stan::math::as_column_vector_or_scalar(m.row(2).segment(1, 3));
136 EXPECT_MATRIX_NEAR(correct, res, 1e-9);
137 }
138
TEST(KernelGenerator,lhs_as_column_vector_or_scalar_vector_plus_eq_test)139 TEST(KernelGenerator, lhs_as_column_vector_or_scalar_vector_plus_eq_test) {
140 using Eigen::MatrixXd;
141 using Eigen::VectorXd;
142 using stan::math::as_column_vector_or_scalar;
143 using stan::math::matrix_cl;
144 VectorXd m1(6);
145 m1 << 1, 2, 3, 4, 5, 6;
146 VectorXd correct = m1.array() + 1;
147
148 matrix_cl<double> m1_cl(m1);
149
150 as_column_vector_or_scalar(m1_cl) += 1;
151
152 MatrixXd res = stan::math::from_matrix_cl(m1_cl);
153
154 EXPECT_MATRIX_NEAR(res, correct, 1e-9);
155 }
156
TEST(KernelGenerator,lhs_as_column_vector_or_scalar_row_vector_plus_eq_test)157 TEST(KernelGenerator, lhs_as_column_vector_or_scalar_row_vector_plus_eq_test) {
158 using Eigen::MatrixXd;
159 using Eigen::RowVectorXd;
160 using stan::math::as_column_vector_or_scalar;
161 using stan::math::matrix_cl;
162 RowVectorXd m1(6);
163 m1 << 1, 2, 3, 4, 5, 6;
164 RowVectorXd correct = m1.array() + 1;
165
166 matrix_cl<double> m1_cl(m1);
167
168 as_column_vector_or_scalar(m1_cl) += 1;
169
170 MatrixXd res = stan::math::from_matrix_cl(m1_cl);
171
172 EXPECT_MATRIX_NEAR(res, correct, 1e-9);
173 }
174
175 #endif
176