-
Notifications
You must be signed in to change notification settings - Fork 196
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
Add cuda::mr::cuda_memory_resource
#1513
Conversation
@harrism you might be interested in this |
aae814f
to
21ce157
Compare
21ce157
to
98485fb
Compare
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
a76e390
to
842943e
Compare
libcudacxx/include/cuda/__memory_resource/cuda_pinned_memory_resource.h
Outdated
Show resolved
Hide resolved
cuda::mr::cuda_memory_resource
cuda::mr::
memory resources
885201b
to
919cd42
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition. I didn't review the refactoring, only the new cuda_memory_resource
. I think the refactoring is just splitting into multiple files, and that should be clarified in the PR description. It would have been nice to separate these into separate PRs.
Most of my suggestions are doc clarifications and fixes.
*/ | ||
void* allocate(const size_t __bytes, const size_t __alignment) const | ||
{ | ||
_LIBCUDACXX_ASSERT(__alignment <= 256 && (256 % __alignment == 0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: I think allocation should throw, not assert. Also note that this assertion/error means that the alignment parameter is NOT ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Just because cudaMalloc aligns to 256 (or possibly 512, even though documented at 256), doesn't mean an MR can't support larger allocations. I suppose users who really want that can write a wrapping resource that aligns, but is there another reason we don't support it here? Seems a bit strange that cuda::mr has this parameter and our most basic implementation semi-ignores it and adds another method without it that is not part of the concept.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added an exception / trap when we are facing an invalid alignment. I also merged the two allocate functions through a default argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems a bit strange that cuda::mr has this parameter and our most basic implementation semi-ignores it and adds another method without it that is not part of the concept.
The existence of the alignment parameter does not mean all possible alignment values are valid. Each resource implementation will have its own limitations and this includes alignment.
cuda_memory_resource
is intended to be the simplest possible wrapper on top of cudaMalloc
, and so it will inherit all the limitations of cudaMalloc
, including alignment. So a user requesting an alignment up to 256 is fine, but beyond that, we throw.
We could add an additional resource that allows for larger alignments, (or an adapter!), but that would no longer be a trivial wrapper on top of cudaMalloc
.
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/test/libcudacxx/cuda/memory_resource/cuda_memory_resource/allocate.pass.cpp
Outdated
Show resolved
Hide resolved
8cfde90
to
64088dd
Compare
bbd9e91
to
16f9082
Compare
// We need to ensure that the provided alignment matches the minimal provided alignment | ||
_LIBCUDACXX_ASSERT( | ||
__default_cuda_malloc_alignment <= __alignment && (__alignment % __default_cuda_malloc_alignment == 0), | ||
"Invalid alignment passed to cuda_memory_resource::deallocate."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: I think this check is unnecessary. It's illegal to ever call deallocate(p, N, M)
for p
that was not returned by p = allocate(N, M)
. Therefore, we would have already checked the alignment during the allocate
call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no guarantee that a user matches allocate and deallocate correctly.
This is just a tiny bit safer than without and it does not hurt
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
* | ||
* @param __lhs The cuda_memory_resource | ||
* @param __rhs The resource to compare to | ||
* @return Comparison of both resources converted to a resource_ref<> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: The semantics are pretty simple, can we just spell it out something like the following (or whatever the incantation would be)?
* @return Comparison of both resources converted to a resource_ref<> | |
* @return true if both arguments are convertible to resource_ref<>(cuda_memory_resource&), false otherwise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with: Result of equality comparison of both resources converted to a resource_ref<>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, but then the reader has to go dig up docs on the semantics of comparison of refs. Doesn't that just compare the referenced object types? Is there a section in the docs on equality comparison that you could link to?
std::pmr::memory_resource and RMM (current) memory resources have very different comparison semantics, so I think this should be clearly documented.
libcudacxx/include/cuda/__memory_resource/cuda_memory_resource.h
Outdated
Show resolved
Hide resolved
Typo in issue title (hyphen should be underscore). |
cuda::mr::cuda_memory-resource
cuda::mr::cuda_memory_resource
7674e55
to
b65158b
Compare
eee001b
to
318af5c
Compare
318af5c
to
14ddff1
Compare
This introduces a new memory resource that allocates device memory through
cudaMalloc
/cudaFree
Fixes #1512