Skip to content

Commit 42291fb

Browse files
committed
add test for matrix_cl.setZero()
1 parent 1d29dcc commit 42291fb

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

stan/math/opencl/matrix_cl.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,17 +515,18 @@ class matrix_cl : public matrix_cl_base {
515515
std::vector<cl::Event> read_write_events(read_write_size, cl::Event{});
516516
auto&& read_events_vec = this->read_events();
517517
auto&& write_events_vec = this->write_events();
518-
for (std::size_t i = 0; i < write_events_size; ++i) {
518+
for (std::size_t i = 0; i < read_events_size; ++i) {
519519
read_write_events[i] = read_events_vec[i];
520520
}
521-
for (std::size_t i = write_events_size, j = 0; i < read_write_size;
521+
for (std::size_t i = read_events_size, j = 0; j < write_events_size;
522522
++i, ++j) {
523523
read_write_events[i] = write_events_vec[j];
524524
}
525525
try {
526526
opencl_context.queue().enqueueFillBuffer(buffer_cl_, static_cast<T>(0), 0,
527527
sizeof(T) * this->size(),
528528
&read_write_events, &zero_event);
529+
zero_event.wait();
529530
} catch (const cl::Error& e) {
530531
check_opencl_error("setZero", e);
531532
}

test/unit/math/opencl/matrix_cl_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,18 @@ TEST(MathMatrixCL, assignment) {
7777
EXPECT_EQ(nullptr, mat1_cl.buffer()());
7878
}
7979

80+
TEST(MathMatrixCL, setZeroFun) {
81+
using stan::math::matrix_cl;
82+
Eigen::Matrix<double, 2, 2> mat_1;
83+
mat_1 << 1, 2, 3, 4;
84+
matrix_cl<double> mat1_cl(mat_1);
85+
mat1_cl.setZero();
86+
Eigen::Matrix<double, 2, 2> mat_1_fromcl
87+
= stan::math::from_matrix_cl(mat1_cl);
88+
EXPECT_EQ(mat_1_fromcl(0), 0);
89+
EXPECT_EQ(mat_1_fromcl(1), 0);
90+
EXPECT_EQ(mat_1_fromcl(2), 0);
91+
EXPECT_EQ(mat_1_fromcl(3), 0);
92+
}
93+
8094
#endif

test/unit/math/rev/core/var_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,19 @@ TEST_F(AgradRev, assign_nan_matvar) {
960960
EXPECT_MATRIX_EQ(z_ans_adj, z.adj());
961961
}
962962

963+
/**
964+
* For var<Matrix> and Matrix<var>, we need to make sure
965+
* the tape, when going through reverse mode, leads to the same outcomes.
966+
* In the case where we declare a var<Matrix> without initializing it, aka
967+
* `var_value<Eigen::MatrixXd>`, we need to think about what the equivalent
968+
* behavior is for `Eigen::Matrix<var, -1, -1>`.
969+
* When default constructing `Eigen::Matrix<var, -1, -1>` we would have an array of `var` types
970+
* with `nullptr` as the vari. The first assignment to that array would then
971+
* just copy the vari pointer from the other array. This is the behavior we
972+
* want to mimic for `var_value<Eigen::MatrixXd>`. So in this test show that
973+
* for uninitialized `var_value<Eigen::MatrixXd>`, we can assign it and the
974+
* adjoints are the same as x.
975+
*/
963976
TEST_F(AgradRev, assign_nullptr_var) {
964977
using stan::math::var_value;
965978
using var_vector = var_value<Eigen::Matrix<double, -1, 1>>;

0 commit comments

Comments
 (0)