Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an exception if using SetVals on non-managed pointer #176

Merged
merged 1 commit into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ target_link_libraries(matx INTERFACE CUDA::cudart
CUDA::cublas
CUDA::cublasLt
CUDA::cufft
CUDA::cusolver)
CUDA::cusolver
CUDA::cuda_driver)

# Build config files if the user isn't adding this as a subdirectory. At this point our transitive target
# should have all build properties needed based on the options passed in
Expand Down
4 changes: 2 additions & 2 deletions include/matx_make.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ auto make_tensor() {
* @returns New tensor
*
**/
template <typename T>
template <typename T, typename O = owning>
auto make_tensor(T *ptr) {
std::array<T, 0> shape;
return make_tensor<T, 0>(ptr, std::move(shape));
return make_tensor<T, 0, O>(ptr, std::move(shape));
}


Expand Down
44 changes: 37 additions & 7 deletions include/matx_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1154,34 +1154,48 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
return tensor_t<T, N, Storage, decltype(new_desc)>{storage_, std::move(new_desc), this->ldata_};
}

__MATX_INLINE__ __MATX_HOST__ bool IsManagedPointer() {
bool managed;
MATX_ASSERT(cuPointerGetAttribute(&managed, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr)Data()) == CUDA_SUCCESS, matxNotSupported);
return managed;
}

/**
* Rank-0 initializer list setting
*
* Note that for performance reasons only CUDA managed pointers are supported with SetVals
* at the moment.
*
* @param val
* 0 initializer list value
*
* @returns reference to view
*
*/
__MATX_INLINE__ __MATX_HOST__ void SetVals(T const &val) noexcept
__MATX_INLINE__ __MATX_HOST__ void SetVals(T const &val)
{
static_assert(RANK == 0, "Single value in SetVals must be applied only to rank-0 tensor");
MATX_ASSERT_STR(IsManagedPointer(), matxNotSupported, "SetVals only supports CUDA managed pointers");
this->operator()() = val;
}

/**
* Rank-1 non-complex or rank-0 initializer list setting
*
* Note that for performance reasons only CUDA managed pointers are supported with SetVals
* at the moment.
*
* @param vals
* 1D initializer list of values
*
* @returns reference to view
*
*/
__MATX_INLINE__ __MATX_HOST__ void SetVals(const std::initializer_list<T> &vals) noexcept
__MATX_INLINE__ __MATX_HOST__ void SetVals(const std::initializer_list<T> &vals)
{
static_assert(((!is_cuda_complex_v<T> && RANK == 1) || (is_cuda_complex_v<T> && RANK == 0)),
"Single initializer list on SetVals only for non-complex rank 1 tensor or complex rank 0 tensors");
MATX_ASSERT_STR(IsManagedPointer(), matxNotSupported, "SetVals only supports CUDA managed pointers");
for (size_t i = 0; i < vals.size(); i++) {
if constexpr (is_cuda_complex_v<T>) {
typename T::value_type real = (vals.begin() + i)->real();
Expand All @@ -1197,6 +1211,9 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
/**
* Rank-2 non-complex or rank-1 initializer list setting
*
* Note that for performance reasons only CUDA managed pointers are supported with SetVals
* at the moment.
*
* @param vals
* 1D/2D initializer list of values
*
Expand All @@ -1205,10 +1222,11 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
*/
__MATX_INLINE__ __MATX_HOST__ void
SetVals(const std::initializer_list<const std::initializer_list<T>>
&vals) noexcept
&vals)
{
static_assert(((!is_cuda_complex_v<T> && RANK == 2) || (is_cuda_complex_v<T> && RANK == 1)),
"Double initializer list on SetVals only for non-complex rank 2 tensor or complex rank 1 tensors");
MATX_ASSERT_STR(IsManagedPointer(), matxNotSupported, "SetVals only supports CUDA managed pointers");
for (size_t i = 0; i < vals.size(); i++) {
for (size_t j = 0; j < (vals.begin() + i)->size(); j++) {
if constexpr (is_cuda_complex_v<T>) {
Expand All @@ -1229,6 +1247,9 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
/**
* Rank-3 non-complex or rank-2 complex initializer list setting
*
* Note that for performance reasons only CUDA managed pointers are supported with SetVals
* at the moment.
*
* @param vals
* 3D/2D initializer list of values
*
Expand All @@ -1238,10 +1259,11 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
__MATX_INLINE__ __MATX_HOST__ void
SetVals(const std::initializer_list<
const std::initializer_list<const std::initializer_list<T>>>
vals) noexcept
vals)
{
static_assert(((!is_cuda_complex_v<T> && RANK == 3) || (is_cuda_complex_v<T> && RANK == 2)),
"Triple initializer list on SetVals only for non-complex rank 3 tensor or complex rank 2 tensors");
"Triple initializer list on SetVals only for non-complex rank 3 tensor or complex rank 2 tensors");
MATX_ASSERT_STR(IsManagedPointer(), matxNotSupported, "SetVals only supports CUDA managed pointers");
for (size_t i = 0; i < vals.size(); i++) {
for (size_t j = 0; j < (vals.begin() + i)->size(); j++) {
for (size_t k = 0; k < ((vals.begin() + i)->begin() + j)->size(); k++) {
Expand All @@ -1265,6 +1287,9 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
/**
* Rank-4 non-complex or rank-3 complex initializer list setting
*
* Note that for performance reasons only CUDA managed pointers are supported with SetVals
* at the moment.
*
* @param vals
* 3D/4D initializer list of values
*
Expand All @@ -1274,10 +1299,11 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
__MATX_INLINE__ __MATX_HOST__ void
SetVals(const std::initializer_list<const std::initializer_list<
const std::initializer_list<const std::initializer_list<T>>>>
&vals) noexcept
&vals)
{
static_assert(((!is_cuda_complex_v<T> && RANK == 4) || (is_cuda_complex_v<T> && RANK == 3)),
"Quad initializer list on SetVals only for non-complex rank 4 tensor or complex rank 3 tensors");
MATX_ASSERT_STR(IsManagedPointer(), matxNotSupported, "SetVals only supports CUDA managed pointers");
for (size_t i = 0; i < vals.size(); i++) {
for (size_t j = 0; j < (vals.begin() + i)->size(); j++) {
for (size_t k = 0; k < ((vals.begin() + i)->begin() + j)->size(); k++) {
Expand Down Expand Up @@ -1310,6 +1336,9 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
/**
* Rank-4 complex initializer list setting
*
* Note that for performance reasons only CUDA managed pointers are supported with SetVals
* at the moment.
*
* @param vals
* 4D initializer list of values
*
Expand All @@ -1320,10 +1349,11 @@ class tensor_t : public detail::tensor_impl_t<T,RANK,Desc> {
SetVals(const std::initializer_list<
const std::initializer_list<const std::initializer_list<
const std::initializer_list<const std::initializer_list<T>>>>>
&vals) noexcept
&vals)
{
static_assert((is_cuda_complex_v<T> && RANK == 4),
"Quintuple initializer list on SetVals only for complex rank 3 tensors");
MATX_ASSERT_STR(IsManagedPointer(), matxNotSupported, "SetVals only supports CUDA managed pointers");
for (size_t i = 0; i < vals.size(); i++) {
for (size_t j = 0; j < (vals.begin() + i)->size(); j++) {
for (size_t k = 0; k < ((vals.begin() + i)->begin() + j)->size(); k++) {
Expand Down
1 change: 1 addition & 0 deletions test/00_tensor/BasicTensorTests.cu
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ TYPED_TEST(BasicTensorTestsIntegral, InitAssign)
ASSERT_EQ(t2v_small(i, j), i * 4 + j + 1);
}
}

MATX_EXIT_HANDLER();
}