Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Add managed_pointer that is compatible with STL. #1068

Merged
merged 1 commit into from
Mar 30, 2020

Conversation

alliepiper
Copy link
Collaborator

The existing cuda::pointer uses a fancy reference that overloads
operator&, and some STL implementations misbehave when that operator
does not return the actual memory address of the object.

Since universal_memory_resource allocates memory that works on both host
and device, we need to be able to use these types with stl containers,
such as std::vector.

This patch adds a managed_pointer implementation that behaves like
cuda::pointer, but returns a regular c++ reference, allowing
the thrust universal allocator to work with STL containers.

@alliepiper
Copy link
Collaborator Author

Internal shelve: 28110928.

testing/cuda/universal_pointer.cu Outdated Show resolved Hide resolved
thrust/system/cuda/memory_resource.h Outdated Show resolved Hide resolved
Copy link
Collaborator

@brycelelbach brycelelbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

managed_pointer is definitely the wrong name for this; it suggests that it is a smart pointer and will clean up the object itself.

Please call it universal_pointer.

@jaredhoberock
Copy link
Contributor

Please call it universal_pointer.

I think the right name is managed_ptr. It's allocated by cudaMallocManaged. "Universal" doesn't mean anything to CUDA programmers. The _ptr suffix should also be used for consistency.

Others have implemented this functionality in the past, and it's been consistently referred to as "managed" pointer.

@griwes
Copy link
Collaborator

griwes commented Mar 14, 2020

..._pointer is the right naming for this, as an analogy to the pointer class template in the same namespace (device_ptr is a different abstraction layer).

@brycelelbach
Copy link
Collaborator

brycelelbach commented Mar 14, 2020 via email

@alliepiper
Copy link
Collaborator Author

It's in a cuda specific detail namespace as an implementation detail of the cuda managed_memory_resource.

@jaredhoberock
Copy link
Contributor

OK, if it's in a detail namespace, it doesn't really matter what it's called :-)

@brycelelbach
Copy link
Collaborator

We should have a top-level way of spelling the pointer type returned by thrust::universal_allocator; it should be called thrust::universal_pointer or thrust::universal_ptr (@griwes will have an opinion about which of those two names is more correct, I suspect the later).

@brycelelbach
Copy link
Collaborator

Should we have a managed_ptr in each backend?

@alliepiper
Copy link
Collaborator Author

alliepiper commented Mar 17, 2020

Ya'll seem to have much stronger opinions on this than I do, so let's see if we can find a color for this bikeshed that we'll all be happy with 😄If I'm following correctly:

  • Jared: It doesn't really matter since it's a detail, but prefer managed_... to universal_...
  • Michal: Just make sure it's ..._pointer and not ...ptr to prevent confusion.
  • Bryce: managed_... makes it sound like it does resource management and might be confusing.
  • Allie: 🤷‍♀

How about we go with detail::managed_memory_pointer<T>, I think that would be agreeable with everyone?

@alliepiper
Copy link
Collaborator Author

alliepiper commented Mar 17, 2020

We should have a top-level way of spelling the pointer type returned by thrust::universal_allocator; it should be called thrust::universal_pointer or thrust::universal_ptr (@griwes will have an opinion about which of those two names is more correct, I suspect the later).

As it stands, thrust::universal_allocator<T> doesn't exist. It currently has to be spelled out explicitly, as thrust::mr::stateless_resource_allocator<T, thrust::universal_memory_resource>. Perhaps we should add universal_allocator<T> typedefs to simplify things for users?

And if we had that, I think thrust::universal_allocator<T>::pointer would be sufficient for spelling the pointer type. I don't think we really need to add a specific pointer typedef for this to thrust::.

@jaredhoberock
Copy link
Contributor

If the purpose of this allocator is to be useful in any Thrust backend, I'd prefer it simply be named thrust::allocator (if it must be named at all).

That naming makes it obvious what it does when CUDA is not involved.

@alliepiper
Copy link
Collaborator Author

Only concern I have there is that we're already using the term "universal" outside of cuda, eg. thrust::universal_memory_resource in thrust/memory/detail/device_system_resource.h. In that context, universal_allocator is more consistent IMO.

@brycelelbach
Copy link
Collaborator

Yes, we need a top level alias for the allocator and for the pointer type.

I'd suggest a quick meeting to discuss this.

@alliepiper alliepiper force-pushed the feature/universal_pointer branch from 09cd854 to eeca28d Compare March 24, 2020 18:01
The existing `cuda::pointer` uses a fancy reference that overloads
`operator&`, and some STL implementations misbehave when that operator
does not return the actual memory address of the object.

Since universal_memory_resource allocates memory that works on both host
and device, we need to be able to use these types with stl containers,
such as std::vector, std::unique_ptr, etc.

This patch adds a managed_pointer implementation that behaves like
`cuda::pointer`, but returns a regular c++ reference, allowing
the thrust universal allocator to work with STL containers.
@alliepiper alliepiper force-pushed the feature/universal_pointer branch from eeca28d to 3c9b9bd Compare March 25, 2020 19:43
@alliepiper alliepiper requested a review from brycelelbach March 27, 2020 16:21
@alliepiper
Copy link
Collaborator Author

@brycelelbach this is blocked on your change request.

New Shelve: 28165473

testing/cuda/universal_pointer.cu Outdated Show resolved Hide resolved
thrust/system/cuda/memory_resource.h Outdated Show resolved Hide resolved
@alliepiper alliepiper merged commit 3cfcc0c into NVIDIA:master Mar 30, 2020
@alliepiper alliepiper deleted the feature/universal_pointer branch March 30, 2020 19:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants