Skip to content

Commit

Permalink
Correct rmm tests for validity of device pointers (#1714)
Browse files Browse the repository at this point in the history
The `is_host_memory` function has been updated to understand that `cudaMemoryTypeUnregistered` is returned when provided pointers allocated by `malloc` and other host side allocation functions.

The `is_device_memory` function has been restricted to report only when device pointer that is usable by the calling cuda context. For that reason the tests now also set the active cuda device for all calling threads.

Authors:
  - Robert Maynard (https://github.com/robertmaynard)

Approvers:
  - Rong Ou (https://github.com/rongou)
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #1714
  • Loading branch information
robertmaynard authored Nov 1, 2024
1 parent 8d49fff commit 9b76d36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
19 changes: 17 additions & 2 deletions tests/mr/device/mr_ref_multithreaded_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,13 @@ TEST_P(mr_ref_test_mt, SetCurrentDeviceResourceRef_mt)
{
// single thread changes default resource, then multiple threads use it
auto old = rmm::mr::set_current_device_resource_ref(this->ref);
test_get_current_device_resource_ref();

spawn([mr = this->ref]() {
int device;
RMM_CUDA_TRY(cudaGetDevice(&device));

spawn([device, mr = this->ref]() {
RMM_CUDA_TRY(cudaSetDevice(device));
EXPECT_EQ(mr, rmm::mr::get_current_device_resource_ref());
test_get_current_device_resource_ref(); // test allocating with the new default resource
});
Expand Down Expand Up @@ -156,7 +161,17 @@ TEST_P(mr_ref_test_mt, SetCurrentDeviceResourceRefPerThread_mt)
}
}

TEST_P(mr_ref_test_mt, Allocate) { spawn(test_various_allocations, this->ref); }
TEST_P(mr_ref_test_mt, Allocate)
{
int device;
RMM_CUDA_TRY(cudaGetDevice(&device));

auto mr = this->ref;
spawn([device, mr]() {
RMM_CUDA_TRY(cudaSetDevice(device));
test_various_allocations(mr);
});
}

TEST_P(mr_ref_test_mt, AllocateDefaultStream)
{
Expand Down
7 changes: 2 additions & 5 deletions tests/mr/device/test_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ inline bool is_device_accessible_memory(void* ptr)
{
cudaPointerAttributes attributes{};
if (cudaSuccess != cudaPointerGetAttributes(&attributes, ptr)) { return false; }
return (attributes.type == cudaMemoryTypeDevice) or (attributes.type == cudaMemoryTypeManaged) or
((attributes.type == cudaMemoryTypeHost) and (attributes.devicePointer != nullptr)) or
((attributes.type == cudaMemoryTypeUnregistered) and
(rmm::mr::detail::is_system_memory_supported(rmm::get_current_cuda_device())));
return attributes.devicePointer != nullptr;
}

inline bool is_host_memory(void* ptr)
{
cudaPointerAttributes attributes{};
if (cudaSuccess != cudaPointerGetAttributes(&attributes, ptr)) { return false; }
return attributes.type == cudaMemoryTypeHost;
return attributes.hostPointer != nullptr || attributes.type == cudaMemoryTypeUnregistered;
}

inline bool is_properly_aligned(void* ptr)
Expand Down

0 comments on commit 9b76d36

Please sign in to comment.