From 1a802f15aa1008d1b7562296804d1a68e9db7830 Mon Sep 17 00:00:00 2001 From: tadej Date: Fri, 25 Oct 2019 10:39:19 +0200 Subject: [PATCH 1/9] optimized matrix_cl copying --- stan/math/opencl/matrix_cl.hpp | 89 +++++++++++++++++++++++++--- stan/math/prim/mat/meta/is_eigen.hpp | 35 ++++++++++- 2 files changed, 116 insertions(+), 8 deletions(-) diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index 73c35bda8da..3d0bf20c9d5 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -294,12 +294,45 @@ class matrix_cl> { * @throw std::system_error if the memory on the device could not * be allocated */ - template ..., require_same_vt...> + template ..., require_same_vt..., + require_not_eigen_contiguous_map_t...> explicit matrix_cl(Mat&& A, matrix_cl_view partial_view = matrix_cl_view::Entire) : rows_(A.rows()), cols_(A.cols()), view_(partial_view) { - initialize_buffer::value - || !is_eigen_matrix::value>(A.eval().data()); + using Mat_type = std::decay_t; + if (is_eigen_matrix_or_array::value + && std::is_lvalue_reference::value) { + initialize_buffer(A.eval().data()); + } else { + auto* A_heap = new Mat_type(std::move(A.eval())); + try { + cl::Event e = initialize_buffer(A_heap->data()); + e.setCallback(CL_COMPLETE, &delete_it, A_heap); + } catch (...) { + delete A_heap; + throw; + } + } + } + + /** + * Constructor for the matrix_cl that + * creates a copy of the Eigen Map on the OpenCL device. + * Regardless of `partial_view`, whole matrix is stored. + * + * @tparam T type of data in the \c Eigen \c Matrix + * @param A the \c Eigen \c Map + * @param partial_view which part of the matrix is used + * + * @throw std::system_error if the memory on the device could not + * be allocated + */ + template ..., + require_same_vt...> + explicit matrix_cl(Map&& A, + matrix_cl_view partial_view = matrix_cl_view::Entire) + : rows_(A.rows()), cols_(A.cols()), view_(partial_view) { + initialize_buffer_optionally_from_heap(std::forward(A)); } /** @@ -352,7 +385,7 @@ class matrix_cl> { explicit matrix_cl(Vec&& A, const int& R, const int& C, matrix_cl_view partial_view = matrix_cl_view::Entire) : rows_(R), cols_(C), view_(partial_view) { - initialize_buffer::value>(A.data()); + initialize_buffer_optionally_from_heap(std::forward(A)); } /** @@ -406,17 +439,18 @@ class matrix_cl> { * buffer size. * @tparam in_order whether copying must be done in order * @param A pointer to buffer + * @return event for the copy */ template - void initialize_buffer(const T* A) { + cl::Event initialize_buffer(const T* A) { + cl::Event transfer_event; if (size() == 0) { - return; + return transfer_event; } cl::Context& ctx = opencl_context.context(); cl::CommandQueue& queue = opencl_context.queue(); try { buffer_cl_ = cl::Buffer(ctx, CL_MEM_READ_WRITE, sizeof(T) * size()); - cl::Event transfer_event; queue.enqueueWriteBuffer(buffer_cl_, opencl_context.in_order() || in_order, 0, sizeof(T) * size(), A, nullptr, &transfer_event); @@ -424,6 +458,35 @@ class matrix_cl> { } catch (const cl::Error& e) { check_opencl_error("initialize_buffer", e); } + return transfer_event; + } + + /** + * Initializes the OpencL buffer of this matrix by copying the data from given + * object. Assumes that size of \c this is already set and matches the + * buffer size. If the object is rvalue (temporary) it is first copied to heap + * and callback is set to delete it after copying to OpenCL device is + * complete. + * @tparam in_order whether copying must be done in order + * @tparam U type of object + * @param obj object + * @return event for the copy + */ + template + void initialize_buffer_optionally_from_heap(U&& obj) { + using U_val = std::decay_t; + if (std::is_lvalue_reference::value) { + initialize_buffer(obj.data()); + } else { + auto* obj_heap = new U_val(std::move(obj)); + try { + cl::Event e = initialize_buffer(obj_heap->data()); + e.setCallback(CL_COMPLETE, &delete_it, obj_heap); + } catch (...) { + delete obj_heap; + throw; + } + } } /** @@ -447,6 +510,18 @@ class matrix_cl> { check_opencl_error("copy (OpenCL)->(OpenCL)", e); } } + + /** + * Deletes the container. Used as callback OpenCL event. + * @tparam U type of container + * @param e cl_event handle + * @param status status of event + * @param container container to delete + */ + template + static void delete_it(cl_event e, cl_int status, void* container) { + delete static_cast(container); + } }; template diff --git a/stan/math/prim/mat/meta/is_eigen.hpp b/stan/math/prim/mat/meta/is_eigen.hpp index fff50e83d94..a6ddad329b8 100644 --- a/stan/math/prim/mat/meta/is_eigen.hpp +++ b/stan/math/prim/mat/meta/is_eigen.hpp @@ -43,11 +43,44 @@ template struct is_eigen_matrix_impl> : std::true_type {}; template struct is_eigen_matrix_impl> : std::true_type {}; - } // namespace internal template struct is_eigen_matrix : internal::is_eigen_matrix_impl> {}; +namespace internal { +template +struct is_eigen_array_impl : std::false_type {}; +template +struct is_eigen_array_impl> : std::true_type {}; +} // namespace internal + +template +struct is_eigen_array : internal::is_eigen_array_impl> {}; + +template +using is_eigen_matrix_or_array + = math::disjunction, is_eigen_array>; + +namespace internal { +template +struct is_eigen_contiguous_map_impl : std::false_type {}; +template +struct is_eigen_contiguous_map_impl>> + : std::true_type {}; + +} // namespace internal + +template +struct is_eigen_contiguous_map + : internal::is_eigen_contiguous_map_impl> {}; + +template +using require_eigen_contiguous_map_t = require_t>; + +template +using require_not_eigen_contiguous_map_t + = require_not_t>; + } // namespace stan #endif From 2fef5bd8de86e7dd0b32bc9440043f9c70877e38 Mon Sep 17 00:00:00 2001 From: tadej Date: Fri, 25 Oct 2019 12:39:26 +0200 Subject: [PATCH 2/9] added missing zero check --- stan/math/opencl/matrix_cl.hpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index 3d0bf20c9d5..0f99a064d08 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -300,6 +300,9 @@ class matrix_cl> { matrix_cl_view partial_view = matrix_cl_view::Entire) : rows_(A.rows()), cols_(A.cols()), view_(partial_view) { using Mat_type = std::decay_t; + if (size() == 0) { + return; + } if (is_eigen_matrix_or_array::value && std::is_lvalue_reference::value) { initialize_buffer(A.eval().data()); @@ -475,6 +478,9 @@ class matrix_cl> { template void initialize_buffer_optionally_from_heap(U&& obj) { using U_val = std::decay_t; + if (size() == 0) { + return; + } if (std::is_lvalue_reference::value) { initialize_buffer(obj.data()); } else { From e5894b15cc938fef136ba815bcb364588d4a51bf Mon Sep 17 00:00:00 2001 From: tadej Date: Fri, 25 Oct 2019 12:41:53 +0200 Subject: [PATCH 3/9] minor fix --- stan/math/opencl/matrix_cl.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index 0f99a064d08..3b66cd2269f 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -467,15 +467,14 @@ class matrix_cl> { /** * Initializes the OpencL buffer of this matrix by copying the data from given * object. Assumes that size of \c this is already set and matches the - * buffer size. If the object is rvalue (temporary) it is first copied to heap + * buffer size. If the object is rvalue (temporary) it is first moved to heap * and callback is set to delete it after copying to OpenCL device is * complete. - * @tparam in_order whether copying must be done in order * @tparam U type of object * @param obj object * @return event for the copy */ - template + template void initialize_buffer_optionally_from_heap(U&& obj) { using U_val = std::decay_t; if (size() == 0) { @@ -518,7 +517,7 @@ class matrix_cl> { } /** - * Deletes the container. Used as callback OpenCL event. + * Deletes the container. Used as a callback for OpenCL event. * @tparam U type of container * @param e cl_event handle * @param status status of event From f1226fe7c6e7018b500f876ba07650ef47691eeb Mon Sep 17 00:00:00 2001 From: tadej Date: Fri, 25 Oct 2019 13:34:50 +0200 Subject: [PATCH 4/9] fixed headers --- stan/math/opencl/matrix_cl.hpp | 4 ++-- stan/math/prim/mat/meta/is_eigen.hpp | 7 ------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index 3b66cd2269f..33df67ce6d6 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -295,7 +295,7 @@ class matrix_cl> { * be allocated */ template ..., require_same_vt..., - require_not_eigen_contiguous_map_t...> + require_not_t>...> explicit matrix_cl(Mat&& A, matrix_cl_view partial_view = matrix_cl_view::Entire) : rows_(A.rows()), cols_(A.cols()), view_(partial_view) { @@ -330,7 +330,7 @@ class matrix_cl> { * @throw std::system_error if the memory on the device could not * be allocated */ - template ..., + template >..., require_same_vt...> explicit matrix_cl(Map&& A, matrix_cl_view partial_view = matrix_cl_view::Entire) diff --git a/stan/math/prim/mat/meta/is_eigen.hpp b/stan/math/prim/mat/meta/is_eigen.hpp index a6ddad329b8..40ef7539c27 100644 --- a/stan/math/prim/mat/meta/is_eigen.hpp +++ b/stan/math/prim/mat/meta/is_eigen.hpp @@ -75,12 +75,5 @@ template struct is_eigen_contiguous_map : internal::is_eigen_contiguous_map_impl> {}; -template -using require_eigen_contiguous_map_t = require_t>; - -template -using require_not_eigen_contiguous_map_t - = require_not_t>; - } // namespace stan #endif From cbe1033758be6aa03042278cd8f1ec390da6011b Mon Sep 17 00:00:00 2001 From: tadej Date: Fri, 25 Oct 2019 13:46:17 +0200 Subject: [PATCH 5/9] fixed headers2 --- stan/math/prim/mat/meta/is_eigen.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/stan/math/prim/mat/meta/is_eigen.hpp b/stan/math/prim/mat/meta/is_eigen.hpp index 40ef7539c27..b9c4fa7df13 100644 --- a/stan/math/prim/mat/meta/is_eigen.hpp +++ b/stan/math/prim/mat/meta/is_eigen.hpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace stan { From 38438d5f5f8a7be1778d2667f1ef9d6c47204821 Mon Sep 17 00:00:00 2001 From: tadej Date: Mon, 11 Nov 2019 09:09:32 +0100 Subject: [PATCH 6/9] added more documentation --- stan/math/opencl/matrix_cl.hpp | 54 ++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index 33df67ce6d6..e53af4f59a2 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -220,9 +220,11 @@ class matrix_cl> { /** * Constructor for the matrix_cl that creates a copy of a std::vector of Eigen * matrices on the OpenCL device. Each matrix is flattened into one column - * of the resulting matrix_cl. + * of the resulting matrix_cl. If a lvalue is passed to this constructor the + * caller must make sure that the vector does not go out of scope before + * copying is complete. * - * @param A the vector of Eigen matrices + * @param A the vector of Eigen matrices * * @throw std::invalid_argument if the * matrices do not have matching dimensions @@ -283,12 +285,14 @@ class matrix_cl> { } /** - * Constructor for the matrix_cl that - * creates a copy of the Eigen matrix on the OpenCL device. - * Regardless of `partial_view`, whole matrix is stored. + * Constructor for the matrix_cl that creates a copy of the Eigen matrix or + * Eigen expression on the OpenCL device. Regardless of `partial_view`, whole + * matrix is stored. If a lvalue matrix is passed to this constructor the + * caller must make sure that the matrix does not go out of scope before + * copying is complete. * - * @tparam T type of data in the \c Eigen \c Matrix - * @param A the \c Eigen \c Matrix + * @tparam Mat type of \c Eigen \c Matrix or expression + * @param A the \c Eigen \c Matrix or expression * @param partial_view which part of the matrix is used * * @throw std::system_error if the memory on the device could not @@ -310,6 +314,10 @@ class matrix_cl> { auto* A_heap = new Mat_type(std::move(A.eval())); try { cl::Event e = initialize_buffer(A_heap->data()); + // We set a callback that will delete the memory once copying is + // complete. This event object does not hold the information about + // callback. OpenCL implementation does. So nothing is lost as event + // goes out of scope. e.setCallback(CL_COMPLETE, &delete_it, A_heap); } catch (...) { delete A_heap; @@ -319,9 +327,10 @@ class matrix_cl> { } /** - * Constructor for the matrix_cl that - * creates a copy of the Eigen Map on the OpenCL device. - * Regardless of `partial_view`, whole matrix is stored. + * Constructor for the matrix_cl that creates a copy of the Eigen Map on the + * OpenCL device. Regardless of `partial_view`, whole matrix is stored. The + * caller must make sure that the memory referenced by map is not deleted + * before copying is complete. * * @tparam T type of data in the \c Eigen \c Matrix * @param A the \c Eigen \c Map @@ -339,9 +348,10 @@ class matrix_cl> { } /** - * Constructor for the matrix_cl that - * creates a copy of a scalar on the OpenCL device. - * Regardless of `partial_view`, whole matrix is stored. + * Constructor for the matrix_cl that creates a copy of a scalar on the OpenCL + * device. Regardless of `partial_view`, whole matrix is stored. If a lvalue + * is passed to this constructor the caller must make sure that it does not go + * out of scope before copying is complete. * * @param A the scalar * @param partial_view which part of the matrix is used @@ -358,7 +368,9 @@ class matrix_cl> { } /** - * Construct a matrix_cl of size Nx1 from \c std::vector + * Construct a matrix_cl of size Nx1 from \c std::vector. If a lvalue is + * passed to this constructor the caller must make sure that it does not go + * out of scope before copying is complete. * * @param A Standard vector * @param partial_view which part of the matrix is used @@ -373,7 +385,9 @@ class matrix_cl> { : matrix_cl(std::forward(A), A.size(), 1) {} /** - * Construct from \c std::vector with given rows and columns + * Construct from \c std::vector with given rows and columns. If a lvalue + * is passed to this constructor the caller must make sure that it does not + * go out of scope before copying is complete. * * @param A Standard vector * @param R Number of rows the matrix should have. @@ -392,7 +406,8 @@ class matrix_cl> { } /** - * Construct from \c array of doubles with given rows and columns + * Construct from \c array of doubles with given rows and columns. The caller + * must make sure that data is not deleted before copying is complete. * * @param A array of doubles * @param R Number of rows the matrix should have. @@ -439,7 +454,8 @@ class matrix_cl> { /** * Initializes the OpencL buffer of this matrix by copying the data from given * buffer. Assumes that size of \c this is already set and matches the - * buffer size. + * buffer size. If \c in_order is false the caller must make sure that data + * is not deleted before copying is complete. * @tparam in_order whether copying must be done in order * @param A pointer to buffer * @return event for the copy @@ -469,7 +485,9 @@ class matrix_cl> { * object. Assumes that size of \c this is already set and matches the * buffer size. If the object is rvalue (temporary) it is first moved to heap * and callback is set to delete it after copying to OpenCL device is - * complete. + * complete. If a lvalue is passed to this constructor the caller must make + * sure that input object does not go out of scope before copying is complete. + * * @tparam U type of object * @param obj object * @return event for the copy From f64bc6814015d89e3bde0a7f83e0a5ac288737e3 Mon Sep 17 00:00:00 2001 From: tadej Date: Mon, 11 Nov 2019 09:23:12 +0100 Subject: [PATCH 7/9] fixed constructor from Map --- stan/math/opencl/matrix_cl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index e53af4f59a2..ff4e7b186bd 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -344,7 +344,7 @@ class matrix_cl> { explicit matrix_cl(Map&& A, matrix_cl_view partial_view = matrix_cl_view::Entire) : rows_(A.rows()), cols_(A.cols()), view_(partial_view) { - initialize_buffer_optionally_from_heap(std::forward(A)); + initialize_buffer(A.data()); } /** From 1c60b8047af7e607c1e37b6c294b89c8e8e8100b Mon Sep 17 00:00:00 2001 From: tadej Date: Thu, 28 Nov 2019 09:19:18 +0100 Subject: [PATCH 8/9] clarified documentation --- stan/math/opencl/copy.hpp | 52 +++++++++++++++++++++++++--------- stan/math/opencl/matrix_cl.hpp | 48 ++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/stan/math/opencl/copy.hpp b/stan/math/opencl/copy.hpp index b836b2b9da4..a3423d1085d 100644 --- a/stan/math/opencl/copy.hpp +++ b/stan/math/opencl/copy.hpp @@ -26,9 +26,15 @@ namespace stan { namespace math { /** - * Copies the source Eigen matrix to - * the destination matrix that is stored - * on the OpenCL device. + * Copies the source Eigen matrix to the destination matrix that is stored on + * the OpenCL device. If a lvalue matrix is passed to this function the caller + * must make sure that the matrix does not go out of scope before copying is + * complete. + * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * returned matrix or any matrix that is calculated from that one. * * @param src source Eigen matrix * @return matrix_cl with a copy of the data in the source matrix @@ -40,9 +46,14 @@ inline matrix_cl to_matrix_cl(Mat&& src) { } /** - * Copies the source std::vector to - * the destination matrix that is stored - * on the OpenCL device. + * Copies the source `std::vector` to the destination matrix that is stored on + * the OpenCL device. If a lvalue is passed to this constructor the caller must + * make sure that it does not go out of scope before copying is complete. + * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * returned matrix or any matrix that is calculated from that one. * * @param src source std::vector * @return matrix_cl with a copy of the data in the source matrix @@ -54,9 +65,8 @@ inline matrix_cl to_matrix_cl(Vec&& src) { } /** - * Copies the source matrix that is stored - * on the OpenCL device to the destination Eigen - * matrix. + * Copies the source matrix that is stored on the OpenCL device to the + * destination Eigen matrix. * * @param src source matrix on the OpenCL device * @return Eigen matrix with a copy of the data in the source matrix @@ -92,7 +102,7 @@ inline Eigen::Matrix from_matrix_cl(const matrix_cl& src) { } /** - * Packs the flat triagnular matrix on the OpenCL device and + * Packs the flat triangular matrix on the OpenCL device and * copies it to the std::vector. * * @param src the flat triangular source matrix on the OpenCL device @@ -128,9 +138,15 @@ inline std::vector packed_copy(const matrix_cl& src) { } /** - * Copies the packed triangular matrix from - * the source std::vector to an OpenCL buffer and - * unpacks it to a flat matrix on the OpenCL device. + * Copies the packed triangular matrix from the source std::vector to an OpenCL + * buffer and unpacks it to a flat matrix on the OpenCL device. If a lvalue is + * passed to this constructor the caller must make sure that it does not go out + * of scope before copying is complete. + * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * returned matrix or any matrix that is calculated from that one. * * @tparam matrix_view the triangularity of the source matrix * @param src the packed source std::vector @@ -211,7 +227,15 @@ inline T from_matrix_cl_error_code(const matrix_cl& src) { } /** - * Copy an arithmetic type to the device. + * Copy an arithmetic type to the device. If a lvalue is passed to this + * constructor the caller must make sure that it does not go out of scope before + * copying is complete. + * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * returned matrix or any matrix that is calculated from that one. + * * @tparam T An arithmetic type to pass the value from the OpenCL matrix to. * @param src Arithmetic to receive the matrix_cl value. * @return A 1x1 matrix on the device. diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index ff4e7b186bd..41b77e3f97c 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -224,6 +224,11 @@ class matrix_cl> { * caller must make sure that the vector does not go out of scope before * copying is complete. * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @param A the vector of Eigen matrices * * @throw std::invalid_argument if the @@ -291,6 +296,11 @@ class matrix_cl> { * caller must make sure that the matrix does not go out of scope before * copying is complete. * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @tparam Mat type of \c Eigen \c Matrix or expression * @param A the \c Eigen \c Matrix or expression * @param partial_view which part of the matrix is used @@ -332,6 +342,11 @@ class matrix_cl> { * caller must make sure that the memory referenced by map is not deleted * before copying is complete. * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @tparam T type of data in the \c Eigen \c Matrix * @param A the \c Eigen \c Map * @param partial_view which part of the matrix is used @@ -353,6 +368,11 @@ class matrix_cl> { * is passed to this constructor the caller must make sure that it does not go * out of scope before copying is complete. * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @param A the scalar * @param partial_view which part of the matrix is used * @@ -372,6 +392,11 @@ class matrix_cl> { * passed to this constructor the caller must make sure that it does not go * out of scope before copying is complete. * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @param A Standard vector * @param partial_view which part of the matrix is used * @@ -389,6 +414,11 @@ class matrix_cl> { * is passed to this constructor the caller must make sure that it does not * go out of scope before copying is complete. * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @param A Standard vector * @param R Number of rows the matrix should have. * @param C Number of columns the matrix should have. @@ -409,6 +439,11 @@ class matrix_cl> { * Construct from \c array of doubles with given rows and columns. The caller * must make sure that data is not deleted before copying is complete. * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @param A array of doubles * @param R Number of rows the matrix should have. * @param C Number of columns the matrix should have. @@ -456,6 +491,12 @@ class matrix_cl> { * buffer. Assumes that size of \c this is already set and matches the * buffer size. If \c in_order is false the caller must make sure that data * is not deleted before copying is complete. + * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @tparam in_order whether copying must be done in order * @param A pointer to buffer * @return event for the copy @@ -485,9 +526,14 @@ class matrix_cl> { * object. Assumes that size of \c this is already set and matches the * buffer size. If the object is rvalue (temporary) it is first moved to heap * and callback is set to delete it after copying to OpenCL device is - * complete. If a lvalue is passed to this constructor the caller must make + * complete. If a lvalue is passed to this function the caller must make * sure that input object does not go out of scope before copying is complete. * + * That means `.wait()` must be called on the event associated on copying or + * any other event that requires completion of this event. This can be done by + * calling `.wait_for_write_events()` or `.wait_for_read_write_events()` on + * this matrix or any matrix that is calculated from this one. + * * @tparam U type of object * @param obj object * @return event for the copy From caab623988e0f28f06a8c5255ed2b4f3bc6085ac Mon Sep 17 00:00:00 2001 From: tadej Date: Tue, 3 Dec 2019 09:48:19 +0100 Subject: [PATCH 9/9] bugfix --- stan/math/opencl/matrix_cl.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index 6d7a549a0c6..d1cff51c7b5 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -481,6 +481,9 @@ class matrix_cl> { this->view_ = a.view(); this->rows_ = a.rows(); this->cols_ = a.cols(); + if (a.size() == 0) { + return *this; + } this->wait_for_read_write_events(); initialize_buffer(a); return *this;