|
1 | 1 | #ifdef STAN_OPENCL
|
2 | 2 | #include <stan/math/opencl/prim.hpp>
|
3 | 3 | #include <stan/math/prim.hpp>
|
4 |
| - |
5 | 4 | #include <test/unit/util.hpp>
|
6 | 5 | #include <gtest/gtest.h>
|
7 |
| -#include <stan/math/opencl/pinned_matrix.hpp> |
8 |
| - |
9 |
| -TEST(MathMetaPrim, ref_type_for_opencl_for_opencl_non_eigen) { |
10 |
| - using stan::math::ref_type_for_opencl_t; |
11 |
| - std::vector<int> a{1, 2, 3}; |
12 |
| - ref_type_for_opencl_t<std::vector<int>> a_ref1 = a; |
13 |
| - ref_type_for_opencl_t<std::vector<int>&> a_ref2 = a; |
14 |
| - ref_type_for_opencl_t<std::vector<int>&&> a_ref3 = std::vector<int>{1, 2, 3}; |
15 |
| - |
16 |
| - double b = 3; |
17 |
| - ref_type_for_opencl_t<double> b_ref1 = b; |
18 |
| - ref_type_for_opencl_t<double&> b_ref2 = b; |
19 |
| - ref_type_for_opencl_t<double&&> b_ref3 = 3; |
20 |
| - |
21 |
| - const std::vector<double> c{0.5, 4, 0.7}; |
22 |
| - ref_type_for_opencl_t<const std::vector<double>> c_ref1 = c; |
23 |
| - ref_type_for_opencl_t<const std::vector<double>&> c_ref2 = c; |
24 |
| - |
25 |
| - EXPECT_STD_VECTOR_FLOAT_EQ(a_ref1, a); |
26 |
| - EXPECT_STD_VECTOR_FLOAT_EQ(a_ref2, a); |
27 |
| - EXPECT_STD_VECTOR_FLOAT_EQ(a_ref3, a); |
28 |
| - EXPECT_EQ(b_ref1, b); |
29 |
| - EXPECT_EQ(b_ref2, b); |
30 |
| - EXPECT_EQ(b_ref3, b); |
31 |
| - EXPECT_STD_VECTOR_FLOAT_EQ(c_ref1, c); |
32 |
| - EXPECT_STD_VECTOR_FLOAT_EQ(c_ref2, c); |
33 |
| - EXPECT_TRUE(std::is_lvalue_reference<ref_type_for_opencl_t<double>>::value); |
34 |
| - EXPECT_TRUE(std::is_lvalue_reference<ref_type_for_opencl_t<double&>>::value); |
35 |
| - EXPECT_FALSE(std::is_reference<ref_type_for_opencl_t<double&&>>::value); |
36 |
| - EXPECT_TRUE(std::is_lvalue_reference< |
37 |
| - ref_type_for_opencl_t<const std::vector<double>>>::value); |
38 |
| - EXPECT_TRUE(std::is_lvalue_reference< |
39 |
| - ref_type_for_opencl_t<const std::vector<double>&>>::value); |
40 |
| - EXPECT_FALSE(std::is_reference< |
41 |
| - ref_type_for_opencl_t<const std::vector<double>&&>>::value); |
42 |
| -} |
43 |
| - |
44 |
| -TEST(MathMetaPrim, ref_type_for_opencl_eigen_contiguous) { |
45 |
| - using stan::math::ref_type_for_opencl_t; |
46 |
| - Eigen::MatrixXd a(3, 3); |
47 |
| - a << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
48 |
| - Eigen::MatrixXd a2 = a; |
49 |
| - ref_type_for_opencl_t<Eigen::MatrixXd> a_ref1 = a; |
50 |
| - ref_type_for_opencl_t<Eigen::MatrixXd&> a_ref2 = a; |
51 |
| - ref_type_for_opencl_t<Eigen::MatrixXd&&> a_ref3 = std::move(a2); |
52 |
| - |
53 |
| - auto b = a.leftCols(2); |
54 |
| - ref_type_for_opencl_t<decltype(b)> b_ref1 = b; |
55 |
| - ref_type_for_opencl_t<decltype(b)&> b_ref2 = b; |
56 |
| - ref_type_for_opencl_t<decltype(b)&&> b_ref3 = a.leftCols(2); |
57 |
| - |
58 |
| - using ContiguousMap = Eigen::Map<Eigen::MatrixXd, 0, Eigen::Stride<0, 0>>; |
59 |
| - ContiguousMap c(a.data(), 3, 3); |
60 |
| - ContiguousMap c2(a.data(), 3, 3); |
61 |
| - ref_type_for_opencl_t<ContiguousMap> c_ref1 = c; |
62 |
| - ref_type_for_opencl_t<ContiguousMap&> c_ref2 = c; |
63 |
| - ref_type_for_opencl_t<ContiguousMap&&> c_ref3 = std::move(c2); |
64 | 6 |
|
65 |
| - EXPECT_MATRIX_EQ(a_ref1, a); |
66 |
| - EXPECT_MATRIX_EQ(a_ref2, a); |
67 |
| - EXPECT_MATRIX_EQ(a_ref3, a); |
68 |
| - |
69 |
| - EXPECT_MATRIX_EQ(b_ref1, b); |
70 |
| - EXPECT_MATRIX_EQ(b_ref2, b); |
71 |
| - EXPECT_MATRIX_EQ(b_ref3, b); |
72 |
| - |
73 |
| - EXPECT_MATRIX_EQ(c_ref1, c); |
74 |
| - EXPECT_MATRIX_EQ(c_ref2, c); |
75 |
| - EXPECT_MATRIX_EQ(c_ref3, c); |
76 |
| - EXPECT_TRUE( |
77 |
| - (std::is_same<decltype(a), ref_type_for_opencl_t<decltype(a)&&>>::value)); |
78 |
| - EXPECT_TRUE( |
79 |
| - (std::is_same<decltype(b), ref_type_for_opencl_t<decltype(b)&&>>::value)); |
80 |
| - EXPECT_TRUE( |
81 |
| - (std::is_same<decltype(c), ref_type_for_opencl_t<decltype(c)&&>>::value)); |
82 |
| - EXPECT_TRUE( |
83 |
| - std::is_lvalue_reference<ref_type_for_opencl_t<Eigen::MatrixXd>>::value); |
84 |
| - EXPECT_TRUE( |
85 |
| - std::is_lvalue_reference<ref_type_for_opencl_t<Eigen::MatrixXd&>>::value); |
86 |
| - EXPECT_FALSE( |
87 |
| - std::is_reference<ref_type_for_opencl_t<Eigen::MatrixXd&&>>::value); |
| 7 | +TEST(MathMetaPrim, ref_type_matrix_cl) { |
| 8 | + using stan::ref_type_t; |
| 9 | + using stan::math::matrix_cl; |
| 10 | + Eigen::MatrixXd a_eig(3, 3); |
| 11 | + a_eig << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
| 12 | + matrix_cl<double> a(a_eig); |
| 13 | + matrix_cl<double> a2 = a; |
| 14 | + ref_type_t<matrix_cl<double>> a_ref1 = a; |
| 15 | + ref_type_t<matrix_cl<double>&> a_ref2 = a; |
| 16 | + ref_type_t<matrix_cl<double>&&> a_ref3 = std::move(a2); |
| 17 | + |
| 18 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref1), from_matrix_cl(a)); |
| 19 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref2), from_matrix_cl(a)); |
| 20 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref3), from_matrix_cl(a)); |
| 21 | + |
| 22 | + EXPECT_TRUE((std::is_same<decltype(a), ref_type_t<decltype(a)&&>>::value)); |
| 23 | + EXPECT_TRUE(std::is_lvalue_reference<ref_type_t<matrix_cl<double>>>::value); |
| 24 | + EXPECT_TRUE(std::is_lvalue_reference<ref_type_t<matrix_cl<double>&>>::value); |
| 25 | + EXPECT_FALSE(std::is_reference<ref_type_t<matrix_cl<double>&&>>::value); |
88 | 26 | }
|
89 | 27 |
|
90 |
| -TEST(MathMetaPrim, ref_type_for_opencl_eigen_non_contiguous) { |
91 |
| - using stan::math::ref_type_for_opencl_t; |
92 |
| - Eigen::MatrixXd m(3, 3); |
93 |
| - m << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
94 |
| - using RowMajorMatrixXd |
95 |
| - = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; |
96 |
| - RowMajorMatrixXd a = m; |
97 |
| - RowMajorMatrixXd a2 = m; |
98 |
| - ref_type_for_opencl_t<RowMajorMatrixXd> a_ref1 = a; |
99 |
| - ref_type_for_opencl_t<RowMajorMatrixXd&> a_ref2 = a; |
100 |
| - ref_type_for_opencl_t<RowMajorMatrixXd&&> a_ref3 = std::move(a2); |
101 |
| - |
102 |
| - auto b = m.block(1, 0, 2, 2); |
103 |
| - ref_type_for_opencl_t<decltype(b)> b_ref1 = b; |
104 |
| - ref_type_for_opencl_t<decltype(b)&> b_ref2 = b; |
105 |
| - ref_type_for_opencl_t<decltype(b)&&> b_ref3 = a.block(1, 0, 2, 2); |
106 |
| - |
107 |
| - Eigen::Ref<Eigen::MatrixXd> c = m; |
108 |
| - Eigen::Ref<Eigen::MatrixXd> c2 = m; |
109 |
| - ref_type_for_opencl_t<Eigen::Ref<Eigen::MatrixXd>> c_ref1 = c; |
110 |
| - ref_type_for_opencl_t<Eigen::Ref<Eigen::MatrixXd>&> c_ref2 = c; |
111 |
| - ref_type_for_opencl_t<Eigen::Ref<Eigen::MatrixXd>&&> c_ref3 = std::move(c2); |
112 |
| - |
113 |
| - EXPECT_MATRIX_EQ(a_ref1, a); |
114 |
| - EXPECT_MATRIX_EQ(a_ref2, a); |
115 |
| - EXPECT_MATRIX_EQ(a_ref3, a); |
116 |
| - |
117 |
| - EXPECT_MATRIX_EQ(b_ref1, b); |
118 |
| - EXPECT_MATRIX_EQ(b_ref2, b); |
119 |
| - EXPECT_MATRIX_EQ(b_ref3, b); |
120 |
| - |
121 |
| - EXPECT_MATRIX_EQ(c_ref1, c); |
122 |
| - EXPECT_MATRIX_EQ(c_ref2, c); |
123 |
| - EXPECT_MATRIX_EQ(c_ref3, c); |
124 |
| - EXPECT_TRUE((std::is_same<stan::math::pinned_matrix<Eigen::MatrixXd>, |
125 |
| - ref_type_for_opencl_t<decltype(a)&&>>::value)); |
126 |
| - EXPECT_TRUE((std::is_same<stan::math::pinned_matrix<Eigen::MatrixXd>, |
127 |
| - ref_type_for_opencl_t<decltype(b)&&>>::value)); |
128 |
| - EXPECT_TRUE((std::is_same<stan::math::pinned_matrix<Eigen::MatrixXd>, |
129 |
| - ref_type_for_opencl_t<decltype(c)&&>>::value)); |
| 28 | +TEST(MathMetaPrim, ref_type_kg_light_expression) { |
| 29 | + using stan::ref_type_t; |
| 30 | + using stan::math::matrix_cl; |
| 31 | + Eigen::MatrixXd m_eig(3, 3); |
| 32 | + m_eig << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
| 33 | + matrix_cl<double> m(m_eig); |
| 34 | + auto a = stan::math::block_zero_based(m, 0, 1, 2, 2); |
| 35 | + ref_type_t<decltype(a)> a_ref1 = a; |
| 36 | + ref_type_t<decltype(a)&> a_ref2 = a; |
| 37 | + ref_type_t<decltype(a)&&> a_ref3 |
| 38 | + = stan::math::block_zero_based(m, 0, 1, 2, 2); |
| 39 | + |
| 40 | + matrix_cl<double> a_eval = a; |
| 41 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref1), from_matrix_cl(a_eval)); |
| 42 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref2), from_matrix_cl(a_eval)); |
| 43 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref3), from_matrix_cl(a_eval)); |
| 44 | + |
| 45 | + EXPECT_FALSE((stan::is_matrix_cl<ref_type_t<decltype(a)>>::value)); |
| 46 | + EXPECT_FALSE((stan::is_matrix_cl<ref_type_t<decltype(a)&>>::value)); |
| 47 | + EXPECT_FALSE((stan::is_matrix_cl<ref_type_t<decltype(a)&&>>::value)); |
130 | 48 | }
|
131 | 49 |
|
132 |
| -TEST(MathMetaPrim, ref_type_for_opencl_eigen_expression) { |
133 |
| - using stan::plain_type_t; |
134 |
| - using stan::math::ref_type_for_opencl_t; |
135 |
| - Eigen::MatrixXd m(3, 3); |
136 |
| - m << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
| 50 | +TEST(MathMetaPrim, ref_type_kg_heavy_expression) { |
| 51 | + using stan::ref_type_t; |
| 52 | + using stan::math::matrix_cl; |
| 53 | + Eigen::MatrixXd m_eig(3, 3); |
| 54 | + m_eig << 1, 2, 3, 4, 5, 6, 7, 8, 9; |
| 55 | + matrix_cl<double> m(m_eig); |
137 | 56 | auto a = m * 3;
|
138 |
| - ref_type_for_opencl_t<decltype(a)> a_ref1 = a; |
139 |
| - ref_type_for_opencl_t<decltype(a)&> a_ref2 = a; |
140 |
| - ref_type_for_opencl_t<decltype(a)&&> a_ref3 = m * 3; |
141 |
| - |
142 |
| - Eigen::MatrixXd a_eval = a; |
143 |
| - EXPECT_MATRIX_EQ(a_ref1, a_eval); |
144 |
| - EXPECT_MATRIX_EQ(a_ref2, a_eval); |
145 |
| - EXPECT_MATRIX_EQ(a_ref3, a_eval); |
146 |
| - |
147 |
| - EXPECT_TRUE( |
148 |
| - (std::is_same<stan::math::pinned_matrix<plain_type_t<decltype(a)>>, |
149 |
| - ref_type_for_opencl_t<decltype(a)>>::value)); |
150 |
| - EXPECT_TRUE( |
151 |
| - (std::is_same<stan::math::pinned_matrix<plain_type_t<decltype(a)>>, |
152 |
| - ref_type_for_opencl_t<decltype(a)&>>::value)); |
153 |
| - EXPECT_TRUE( |
154 |
| - (std::is_same<stan::math::pinned_matrix<plain_type_t<decltype(a)>>, |
155 |
| - ref_type_for_opencl_t<decltype(a)&&>>::value)); |
| 57 | + ref_type_t<decltype(a)> a_ref1 = a; |
| 58 | + ref_type_t<decltype(a)&> a_ref2 = a; |
| 59 | + ref_type_t<decltype(a)&&> a_ref3 = m * 3; |
| 60 | + |
| 61 | + matrix_cl<double> a_eval = a; |
| 62 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref1), from_matrix_cl(a_eval)); |
| 63 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref2), from_matrix_cl(a_eval)); |
| 64 | + EXPECT_MATRIX_EQ(from_matrix_cl(a_ref3), from_matrix_cl(a_eval)); |
| 65 | + |
| 66 | + EXPECT_TRUE((std::is_same<stan::math::matrix_cl<double>, |
| 67 | + ref_type_t<decltype(a)>>::value)); |
| 68 | + EXPECT_TRUE((std::is_same<stan::math::matrix_cl<double>, |
| 69 | + ref_type_t<decltype(a)&>>::value)); |
| 70 | + EXPECT_TRUE((std::is_same<stan::math::matrix_cl<double>, |
| 71 | + ref_type_t<decltype(a)&&>>::value)); |
156 | 72 | }
|
157 | 73 |
|
158 | 74 | #endif
|
0 commit comments