Skip to content

Commit dd45af4

Browse files
authored
Merge pull request #1225 from bstatcomp/feature/issue-1217-change-name-of-copy-function
Feature/issue 1217 change name of copy function
2 parents d9cfb98 + 60c3ec3 commit dd45af4

23 files changed

+217
-223
lines changed

stan/math/opencl/copy.hpp

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef STAN_MATH_PRIM_MAT_FUN_OPENCL_COPY_HPP
2-
#define STAN_MATH_PRIM_MAT_FUN_OPENCL_COPY_HPP
1+
#ifndef STAN_MATH_OPENCL_COPY_HPP
2+
#define STAN_MATH_OPENCL_COPY_HPP
33
#ifdef STAN_OPENCL
44

55
#include <stan/math/opencl/opencl_context.hpp>
@@ -28,21 +28,16 @@ namespace math {
2828
* the destination matrix that is stored
2929
* on the OpenCL device.
3030
*
31-
* @tparam T type of data in the Eigen matrix
32-
* @param dst destination matrix on the OpenCL device
31+
* @tparam R Compile time rows of the Eigen matrix
32+
* @tparam C Compile time columns of the Eigen matrix
3333
* @param src source Eigen matrix
34-
*
35-
* @throw <code>std::invalid_argument</code> if the
36-
* matrices do not have matching dimensions
34+
* @return matrix_cl with a copy of the data in the source matrix
3735
*/
3836
template <int R, int C>
39-
void copy(matrix_cl& dst, const Eigen::Matrix<double, R, C>& src) {
40-
check_size_match("copy (Eigen -> (OpenCL))", "src.rows()", src.rows(),
41-
"dst.rows()", dst.rows());
42-
check_size_match("copy (Eigen -> (OpenCL))", "src.cols()", src.cols(),
43-
"dst.cols()", dst.cols());
37+
inline matrix_cl to_matrix_cl(const Eigen::Matrix<double, R, C>& src) {
38+
matrix_cl dst(src.rows(), src.cols());
4439
if (src.size() == 0) {
45-
return;
40+
return dst;
4641
}
4742
try {
4843
/**
@@ -61,28 +56,23 @@ void copy(matrix_cl& dst, const Eigen::Matrix<double, R, C>& src) {
6156
} catch (const cl::Error& e) {
6257
check_opencl_error("copy Eigen->(OpenCL)", e);
6358
}
59+
return dst;
6460
}
6561

6662
/**
6763
* Copies the source matrix that is stored
6864
* on the OpenCL device to the destination Eigen
6965
* matrix.
7066
*
71-
* @tparam T type of data in the Eigen matrix
72-
* @param dst destination Eigen matrix
7367
* @param src source matrix on the OpenCL device
74-
*
75-
* @throw <code>std::invalid_argument</code> if the
76-
* matrices do not have matching dimensions
68+
* @return Eigen matrix with a copy of the data in the source matrix
7769
*/
78-
template <int R, int C>
79-
void copy(Eigen::Matrix<double, R, C>& dst, const matrix_cl& src) {
80-
check_size_match("copy ((OpenCL) -> Eigen)", "src.rows()", src.rows(),
81-
"dst.rows()", dst.rows());
82-
check_size_match("copy ((OpenCL) -> Eigen)", "src.cols()", src.cols(),
83-
"dst.cols()", dst.cols());
70+
inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> from_matrix_cl(
71+
const matrix_cl& src) {
72+
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> dst(src.rows(),
73+
src.cols());
8474
if (src.size() == 0) {
85-
return;
75+
return dst;
8676
}
8777
try {
8878
/**
@@ -103,6 +93,7 @@ void copy(Eigen::Matrix<double, R, C>& dst, const matrix_cl& src) {
10393
} catch (const cl::Error& e) {
10494
check_opencl_error("copy (OpenCL)->Eigen", e);
10595
}
96+
return dst;
10697
}
10798

10899
/**
@@ -184,19 +175,15 @@ inline matrix_cl packed_copy(const std::vector<double>& src, int rows) {
184175
* destination matrix. Both matrices
185176
* are stored on the OpenCL device.
186177
*
187-
* @param dst destination matrix
188178
* @param src source matrix
189-
*
179+
* @return matrix_cl with copies of values in the source matrix
190180
* @throw <code>std::invalid_argument</code> if the
191181
* matrices do not have matching dimensions
192182
*/
193-
inline void copy(matrix_cl& dst, const matrix_cl& src) {
194-
check_size_match("copy ((OpenCL) -> (OpenCL))", "src.rows()", src.rows(),
195-
"dst.rows()", dst.rows());
196-
check_size_match("copy ((OpenCL) -> (OpenCL))", "src.cols()", src.cols(),
197-
"dst.cols()", dst.cols());
183+
inline matrix_cl copy_cl(const matrix_cl& src) {
184+
matrix_cl dst(src.rows(), src.cols());
198185
if (src.size() == 0) {
199-
return;
186+
return dst;
200187
}
201188
try {
202189
/**
@@ -216,16 +203,18 @@ inline void copy(matrix_cl& dst, const matrix_cl& src) {
216203
} catch (const cl::Error& e) {
217204
check_opencl_error("copy (OpenCL)->(OpenCL)", e);
218205
}
206+
return dst;
219207
}
220208

221209
/**
222210
* Copy A 1 by 1 source matrix from the Device to the host.
223-
* @tparam An arithmetic type to pass the value from the OpenCL matrix to.
224-
* @param dst Arithmetic to receive the matrix_cl value.
211+
* @tparam T An arithmetic type to pass the value from the OpenCL matrix to.
225212
* @param src A 1x1 matrix on the device.
213+
* @return dst Arithmetic to receive the matrix_cl value.
226214
*/
227215
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
228-
inline void copy(T& dst, const matrix_cl& src) {
216+
inline T from_matrix_cl(const matrix_cl& src) {
217+
T dst;
229218
check_size_match("copy ((OpenCL) -> (OpenCL))", "src.rows()", src.rows(),
230219
"dst.rows()", 1);
231220
check_size_match("copy ((OpenCL) -> (OpenCL))", "src.cols()", src.cols(),
@@ -240,16 +229,18 @@ inline void copy(T& dst, const matrix_cl& src) {
240229
} catch (const cl::Error& e) {
241230
check_opencl_error("copy (OpenCL)->(OpenCL)", e);
242231
}
232+
return dst;
243233
}
244234

245235
/**
246236
* Copy an arithmetic type to the device.
247-
* @tparam An arithmetic type to pass the value from the OpenCL matrix to.
237+
* @tparam T An arithmetic type to pass the value from the OpenCL matrix to.
248238
* @param src Arithmetic to receive the matrix_cl value.
249-
* @param dst A 1x1 matrix on the device.
239+
* @return A 1x1 matrix on the device.
250240
*/
251241
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
252-
inline void copy(matrix_cl& dst, const T& src) {
242+
inline matrix_cl to_matrix_cl(const T& src) {
243+
matrix_cl dst(1, 1);
253244
check_size_match("copy ((OpenCL) -> (OpenCL))", "src.rows()", dst.rows(),
254245
"dst.rows()", 1);
255246
check_size_match("copy ((OpenCL) -> (OpenCL))", "src.cols()", dst.cols(),
@@ -263,6 +254,7 @@ inline void copy(matrix_cl& dst, const T& src) {
263254
} catch (const cl::Error& e) {
264255
check_opencl_error("copy (OpenCL)->(OpenCL)", e);
265256
}
257+
return dst;
266258
}
267259

268260
} // namespace math

stan/math/opencl/err/check_diagonal_zeros.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ inline void check_diagonal_zeros(const char* function, const char* name,
2929
try {
3030
int zero_on_diagonal_flag = 0;
3131
matrix_cl zeros_flag(1, 1);
32-
copy(zeros_flag, zero_on_diagonal_flag); // NOLINT
32+
zeros_flag = to_matrix_cl(zero_on_diagonal_flag);
3333
opencl_kernels::check_diagonal_zeros(cl::NDRange(y.rows(), y.cols()), y,
3434
zeros_flag, y.rows(), y.cols());
35-
copy(zero_on_diagonal_flag, zeros_flag); // NOLINT
35+
zero_on_diagonal_flag = from_matrix_cl<int>(zeros_flag);
3636
// if zeros were found on the diagonal
3737
if (zero_on_diagonal_flag) {
3838
domain_error(function, name, "has zeros on the diagonal.", "");

stan/math/opencl/err/check_nan.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ inline void check_nan(const char* function, const char* name,
2828
try {
2929
int nan_flag = 0;
3030
matrix_cl nan_chk(1, 1);
31-
copy(nan_chk, nan_flag); // NOLINT
31+
nan_chk = to_matrix_cl(nan_flag);
3232
opencl_kernels::check_nan(cl::NDRange(y.rows(), y.cols()), y, nan_chk,
3333
y.rows(), y.cols());
34-
copy(nan_flag, nan_chk); // NOLINT
34+
nan_flag = from_matrix_cl<int>(nan_chk);
3535
if (nan_flag) {
3636
domain_error(function, name, "has NaN values", "");
3737
}

stan/math/opencl/err/check_symmetric.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ inline void check_symmetric(const char* function, const char* name,
2929
try {
3030
int symmetric_flag = 1;
3131
matrix_cl symm_flag(1, 1);
32-
copy(symm_flag, symmetric_flag); // NOLINT
32+
symm_flag = to_matrix_cl(symmetric_flag);
3333
opencl_kernels::check_symmetric(cl::NDRange(y.rows(), y.cols()), y,
3434
symm_flag, y.rows(), y.cols(),
3535
math::CONSTRAINT_TOLERANCE);
36-
copy(symmetric_flag, symm_flag); // NOLINT
36+
symmetric_flag = from_matrix_cl<int>(symm_flag);
3737
if (!symmetric_flag) {
3838
domain_error(function, name, "is not symmetric", "");
3939
}

stan/math/prim/mat/fun/cholesky_decompose.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> cholesky_decompose(
6969
cholesky_decompose(m_cl);
7070
check_nan("cholesky_decompose (OpenCL)", "Matrix m", m_cl);
7171
check_diagonal_zeros("cholesky_decompose (OpenCL)", "Matrix m", m_cl);
72-
copy(m_chol, m_cl); // NOLINT
72+
m_chol = from_matrix_cl(m_cl);
7373
return m_chol;
7474
} else {
7575
check_symmetric("cholesky_decompose", "m", m);

test/unit/math/opencl/add_test.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <gtest/gtest.h>
66
#include <algorithm>
77

8-
TEST(MathMatrixGPU, add_v_exception_pass) {
8+
TEST(MathMatrixCL, add_v_exception_pass) {
99
stan::math::vector_d d1, d2;
1010

1111
d1.resize(3);
@@ -16,7 +16,7 @@ TEST(MathMatrixGPU, add_v_exception_pass) {
1616
EXPECT_NO_THROW(d33 = d11 + d22);
1717
}
1818

19-
TEST(MathMatrixGPU, add_v_exception_pass_zero) {
19+
TEST(MathMatrixCL, add_v_exception_pass_zero) {
2020
stan::math::vector_d d1, d2;
2121
d1.resize(0);
2222
d2.resize(0);
@@ -26,7 +26,7 @@ TEST(MathMatrixGPU, add_v_exception_pass_zero) {
2626
EXPECT_NO_THROW(d33 = d11 + d22);
2727
}
2828

29-
TEST(MathMatrixGPU, add_v_exception_pass_invalid_arg) {
29+
TEST(MathMatrixCL, add_v_exception_pass_invalid_arg) {
3030
stan::math::row_vector_d d1, d2;
3131

3232
d1.resize(2);
@@ -37,7 +37,7 @@ TEST(MathMatrixGPU, add_v_exception_pass_invalid_arg) {
3737
EXPECT_THROW(d33 = d11 + d22, std::invalid_argument);
3838
}
3939

40-
TEST(MathMatrixGPU, add_rv_exception_pass) {
40+
TEST(MathMatrixCL, add_rv_exception_pass) {
4141
stan::math::row_vector_d d1, d2;
4242

4343
d1.resize(3);
@@ -48,7 +48,7 @@ TEST(MathMatrixGPU, add_rv_exception_pass) {
4848
EXPECT_NO_THROW(d33 = d11 + d22);
4949
}
5050

51-
TEST(MathMatrixGPU, add_rv_exception_pass_zero) {
51+
TEST(MathMatrixCL, add_rv_exception_pass_zero) {
5252
stan::math::row_vector_d d1, d2;
5353

5454
d1.resize(0);
@@ -59,7 +59,7 @@ TEST(MathMatrixGPU, add_rv_exception_pass_zero) {
5959
EXPECT_NO_THROW(d33 = d11 + d22);
6060
}
6161

62-
TEST(MathMatrixGPU, add_rv_exception_fail_invalid_arg) {
62+
TEST(MathMatrixCL, add_rv_exception_fail_invalid_arg) {
6363
stan::math::row_vector_d d1, d2;
6464

6565
d1.resize(2);
@@ -70,7 +70,7 @@ TEST(MathMatrixGPU, add_rv_exception_fail_invalid_arg) {
7070
EXPECT_THROW(d33 = d11 + d22, std::invalid_argument);
7171
}
7272

73-
TEST(MathMatrixGPU, add_m_exception_pass_simple) {
73+
TEST(MathMatrixCL, add_m_exception_pass_simple) {
7474
stan::math::matrix_d d1, d2;
7575

7676
d1.resize(2, 3);
@@ -81,7 +81,7 @@ TEST(MathMatrixGPU, add_m_exception_pass_simple) {
8181
EXPECT_NO_THROW(d33 = d11 + d22);
8282
}
8383

84-
TEST(MathMatrixGPU, add_m_exception_pass_zero) {
84+
TEST(MathMatrixCL, add_m_exception_pass_zero) {
8585
stan::math::matrix_d d1, d2;
8686
d1.resize(0, 0);
8787
d2.resize(0, 0);
@@ -91,7 +91,7 @@ TEST(MathMatrixGPU, add_m_exception_pass_zero) {
9191
EXPECT_NO_THROW(d33 = d11 + d22);
9292
}
9393

94-
TEST(MathMatrixGPU, add_m_exception_fail_invalid_arg) {
94+
TEST(MathMatrixCL, add_m_exception_fail_invalid_arg) {
9595
stan::math::matrix_d d1, d2;
9696
d1.resize(2, 3);
9797
d2.resize(3, 3);
@@ -101,7 +101,7 @@ TEST(MathMatrixGPU, add_m_exception_fail_invalid_arg) {
101101
EXPECT_THROW(d33 = d11 + d22, std::invalid_argument);
102102
}
103103

104-
TEST(MathMatrixGPU, add_non_matching_sizes_exception) {
104+
TEST(MathMatrixCL, add_non_matching_sizes_exception) {
105105
stan::math::vector_d v1(2);
106106
v1 << 1, 2;
107107
stan::math::vector_d v2(3);
@@ -134,7 +134,7 @@ TEST(MathMatrixGPU, add_non_matching_sizes_exception) {
134134
EXPECT_THROW(m33 = m11 + m22, std::invalid_argument);
135135
}
136136

137-
TEST(MathMatrixGPU, add_value_check) {
137+
TEST(MathMatrixCL, add_value_check) {
138138
stan::math::vector_d v1(3);
139139
v1 << 1, 2, 3;
140140
stan::math::vector_d v2(3);
@@ -169,17 +169,17 @@ TEST(MathMatrixGPU, add_value_check) {
169169
EXPECT_NO_THROW(rv33 = rv11 + rv22);
170170
EXPECT_NO_THROW(m33 = m11 + m22);
171171

172-
stan::math::copy(v3, v33);
172+
v3 = stan::math::from_matrix_cl(v33);
173173
EXPECT_EQ(11, v3(0));
174174
EXPECT_EQ(102, v3(1));
175175
EXPECT_EQ(1003, v3(2));
176176

177-
stan::math::copy(rv3, rv33);
177+
rv3 = stan::math::from_matrix_cl(rv33);
178178
EXPECT_EQ(11, rv3(0));
179179
EXPECT_EQ(102, rv3(1));
180180
EXPECT_EQ(1003, rv3(2));
181181

182-
stan::math::copy(m3, m33);
182+
m3 = stan::math::from_matrix_cl(m33);
183183
EXPECT_EQ(11, m3(0, 0));
184184
EXPECT_EQ(102, m3(0, 1));
185185
EXPECT_EQ(1003, m3(0, 2));
@@ -191,7 +191,7 @@ TEST(MathMatrixGPU, add_value_check) {
191191
EXPECT_EQ(17, m3(2, 2));
192192
}
193193

194-
TEST(MathMatrixGPU, add_batch) {
194+
TEST(MathMatrixCL, add_batch) {
195195
// used to represent 5 matrices of size 10x10
196196
const int batch_size = 11;
197197
const int size = 13;
@@ -207,7 +207,7 @@ TEST(MathMatrixGPU, add_batch) {
207207
stan::math::matrix_cl a_cl_res(size, size);
208208
stan::math::opencl_kernels::add_batch(cl::NDRange(size, size), a_cl_res, a_cl,
209209
size, size, batch_size);
210-
copy(a_res, a_cl_res);
210+
a_res = stan::math::from_matrix_cl(a_cl_res);
211211
for (int k = 0; k < batch_size; k++) {
212212
for (int i = 0; i < size; i++)
213213
for (int j = 0; j < size; j++) {

test/unit/math/opencl/async_test.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ TEST(async_opencl, thrash_opencl) {
1414
stan::math::matrix_d m1 = stan::math::matrix_d::Random(size, size);
1515
stan::math::matrix_d m1_result = m1 + m1 * m1 - m1;
1616
stan::math::matrix_cl m1_cl(size, size);
17-
stan::math::copy(m1_cl, m1); // NOLINT
17+
m1_cl = stan::math::to_matrix_cl(m1);
1818
stan::math::matrix_cl m1_result_cl = m1_cl + m1_cl * m1_cl - m1_cl;
1919
stan::math::matrix_d m1_result_test(size, size);
20-
stan::math::copy(m1_result_test, m1_result_cl); // NOLINT
20+
m1_result_test = stan::math::from_matrix_cl(m1_result_cl);
2121
EXPECT_MATRIX_NEAR(m1_result, m1_result_test, 1e-12)
2222
}
2323

@@ -28,11 +28,11 @@ TEST(async_opencl, assign_miss) {
2828
stan::math::matrix_d m1_result = m1 + m1 * m1 - m1;
2929
m1_result = m1_result + m1_result * m1_result - m1_result;
3030
stan::math::matrix_cl m1_cl(size, size);
31-
stan::math::copy(m1_cl, m1); // NOLINT
31+
m1_cl = stan::math::to_matrix_cl(m1);
3232
stan::math::matrix_cl m1_result_cl = m1_cl + m1_cl * m1_cl - m1_cl;
3333
m1_result_cl = m1_result_cl + m1_result_cl * m1_result_cl - m1_result_cl;
3434
stan::math::matrix_d m1_result_test(size, size);
35-
stan::math::copy(m1_result_test, m1_result_cl); // NOLINT
35+
m1_result_test = stan::math::from_matrix_cl(m1_result_cl);
3636
EXPECT_MATRIX_NEAR(m1_result, m1_result_test, 1e-12)
3737
}
3838

@@ -42,15 +42,15 @@ TEST(async_opencl, read_miss) {
4242
stan::math::matrix_d m1 = stan::math::matrix_d::Random(size, size);
4343
stan::math::matrix_d m1_result = m1 + m1 * m1 - m1;
4444
stan::math::matrix_cl m1_cl(size, size);
45-
stan::math::copy(m1_cl, m1); // NOLINT
45+
m1_cl = stan::math::to_matrix_cl(m1);
4646
stan::math::matrix_cl m1_result_cl = m1_cl + m1_cl * m1_cl - m1_cl;
4747
m1_cl = m1_cl * 2;
4848
stan::math::matrix_d m1_result_test(size, size);
49-
stan::math::copy(m1_result_test, m1_result_cl); // NOLINT
49+
m1_result_test = stan::math::from_matrix_cl(m1_result_cl);
5050
EXPECT_MATRIX_NEAR(m1_result, m1_result_test, 1e-12)
5151
m1 = m1 * 2;
5252
stan::math::matrix_d m1_multiply_test(size, size);
53-
stan::math::copy(m1_multiply_test, m1_cl); // NOLINT
53+
m1_multiply_test = stan::math::from_matrix_cl(m1_cl);
5454
EXPECT_MATRIX_NEAR(m1, m1_multiply_test, 1e-12)
5555
}
5656

test/unit/math/opencl/check_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ TEST(ErrorHandlingScalarGPU, check_nan_positions) {
5353
EXPECT_THROW(check_nan(function, "xx_mat3", xx_mat3), std::domain_error);
5454
}
5555

56-
TEST(ErrorHandlingScalarGPU, check_rv_v_symmetric_gpu) {
56+
TEST(ErrorHandlingScalarGPU, check_rv_v_symmetric_cl) {
5757
const char* function = "check_symmetric";
5858

5959
stan::math::row_vector_d rv;

0 commit comments

Comments
 (0)