-
Notifications
You must be signed in to change notification settings - Fork 472
UCT/CUDA/CUDA_COPY: Enabled memory attributes query after switching CUDA GPU. #10388
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/** | ||
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2025. ALL RIGHTS RESERVED. | ||
* | ||
* See file LICENSE for terms. | ||
*/ | ||
|
||
#include <uct/test_md.h> | ||
|
||
extern "C" { | ||
#include <ucs/sys/ptr_arith.h> | ||
} | ||
|
||
#include <cuda.h> | ||
#include <cuda_runtime.h> | ||
|
||
class test_switch_cuda_device : public test_md { | ||
public: | ||
template<class T> void detect_mem_type(ucs_memory_type_t mem_type) const; | ||
}; | ||
|
||
template<class T> | ||
void test_switch_cuda_device::detect_mem_type(ucs_memory_type_t mem_type) const | ||
{ | ||
int num_devices; | ||
ASSERT_EQ(cudaGetDeviceCount(&num_devices), cudaSuccess); | ||
|
||
if (num_devices < 2) { | ||
UCS_TEST_SKIP_R("less than two cuda devices available"); | ||
} | ||
|
||
int current_device; | ||
ASSERT_EQ(cudaGetDevice(¤t_device), cudaSuccess); | ||
ASSERT_EQ(cudaSetDevice((current_device + 1) % num_devices), cudaSuccess); | ||
|
||
const size_t size = 16; | ||
T buffer(size, mem_type); | ||
|
||
EXPECT_EQ(cudaSetDevice(current_device), cudaSuccess); | ||
|
||
ucs_memory_type_t detected_mem_type; | ||
ASSERT_EQ(uct_md_detect_memory_type(m_md, buffer.ptr(), size, | ||
&detected_mem_type), | ||
UCS_OK); | ||
EXPECT_EQ(detected_mem_type, mem_type); | ||
} | ||
|
||
#if HAVE_CUDA_FABRIC | ||
class cuda_fabric_mem_buffer { | ||
public: | ||
cuda_fabric_mem_buffer(size_t size, ucs_memory_type_t mem_type); | ||
virtual ~cuda_fabric_mem_buffer(); | ||
void *ptr() const; | ||
|
||
private: | ||
size_t m_size; | ||
CUmemGenericAllocationHandle m_alloc_handle; | ||
CUdeviceptr m_ptr; | ||
}; | ||
|
||
cuda_fabric_mem_buffer::cuda_fabric_mem_buffer(size_t size, | ||
ucs_memory_type_t mem_type) : | ||
m_size(size) | ||
{ | ||
size_t granularity = 0; | ||
CUmemAllocationProp prop = {}; | ||
CUmemAccessDesc access_desc = {}; | ||
CUdevice device; | ||
if (cuCtxGetDevice(&device) != CUDA_SUCCESS) { | ||
UCS_TEST_ABORT("failed to get the device handle for the current " | ||
"context"); | ||
} | ||
|
||
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED; | ||
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE; | ||
prop.location.id = device; | ||
prop.requestedHandleTypes = CU_MEM_HANDLE_TYPE_FABRIC; | ||
if (cuMemGetAllocationGranularity(&granularity, &prop, | ||
CU_MEM_ALLOC_GRANULARITY_MINIMUM) != | ||
CUDA_SUCCESS) { | ||
goto err; | ||
} | ||
|
||
m_size = ucs_align_up(m_size, granularity); | ||
if (cuMemCreate(&m_alloc_handle, m_size, &prop, 0) != CUDA_SUCCESS) { | ||
goto err; | ||
} | ||
|
||
if (cuMemAddressReserve(&m_ptr, m_size, 0, 0, 0) != CUDA_SUCCESS) { | ||
goto err_mem_release; | ||
} | ||
|
||
if (cuMemMap(m_ptr, m_size, 0, m_alloc_handle, 0) != CUDA_SUCCESS) { | ||
goto err_address_free; | ||
} | ||
|
||
access_desc.location.type = CU_MEM_LOCATION_TYPE_DEVICE; | ||
access_desc.location.id = device; | ||
access_desc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE; | ||
if (cuMemSetAccess(m_ptr, m_size, &access_desc, 1) != CUDA_SUCCESS) { | ||
goto err_mem_unmap; | ||
} | ||
|
||
return; | ||
|
||
err_mem_unmap: | ||
cuMemUnmap(m_ptr, m_size); | ||
err_address_free: | ||
cuMemAddressFree(m_ptr, m_size); | ||
err_mem_release: | ||
cuMemRelease(m_alloc_handle); | ||
err: | ||
UCS_TEST_SKIP_R("failed to allocate CUDA fabric memory"); | ||
} | ||
|
||
cuda_fabric_mem_buffer::~cuda_fabric_mem_buffer() | ||
{ | ||
cuMemUnmap(m_ptr, m_size); | ||
cuMemAddressFree(m_ptr, m_size); | ||
cuMemRelease(m_alloc_handle); | ||
} | ||
|
||
void *cuda_fabric_mem_buffer::ptr() const | ||
{ | ||
return (void*)m_ptr; | ||
} | ||
#endif | ||
|
||
UCS_TEST_P(test_switch_cuda_device, detect_mem_type_cuda) | ||
{ | ||
detect_mem_type<mem_buffer>(UCS_MEMORY_TYPE_CUDA); | ||
} | ||
|
||
UCS_TEST_P(test_switch_cuda_device, detect_mem_type_cuda_managed) | ||
{ | ||
detect_mem_type<mem_buffer>(UCS_MEMORY_TYPE_CUDA_MANAGED); | ||
} | ||
|
||
#if HAVE_CUDA_FABRIC | ||
UCS_TEST_P(test_switch_cuda_device, detect_mem_type_cuda_fabric) | ||
{ | ||
detect_mem_type<cuda_fabric_mem_buffer>(UCS_MEMORY_TYPE_CUDA); | ||
} | ||
#endif | ||
|
||
_UCT_MD_INSTANTIATE_TEST_CASE(test_switch_cuda_device, cuda_cpy); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.