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

Do not initialize the pinned mdarray at construction time #2478

Merged

Conversation

achirkin
Copy link
Contributor

@achirkin achirkin commented Nov 1, 2024

thrust::host_vector initializes its elements at creation and requires the element type be default-constructible.
This translates to raft::pinned_mdarray and makes the mdarray unusable for non-default-constructible objects, like cuda::atomic<> (and many user-defined types). This is against all other mdarray types in raft, which are based on rmm::device_uvector and are not initialized at construction time.
The PR changes the underlying container to a plain pointer + cudaMallocHost/cudaFreeHost.

Breaking change: if anyone relies on the pinned_mdarray to initialize itself, the code will break (but mdarrays should not initialize at construction in raft anyway). The affected classes have different private members now, so the ABI changes as well.

@achirkin achirkin added 3 - Ready for Review improvement Improvement / enhancement to an existing function breaking Breaking change labels Nov 1, 2024
@achirkin achirkin requested a review from a team as a code owner November 1, 2024 10:12
@github-actions github-actions bot added the cpp label Nov 1, 2024
@achirkin
Copy link
Contributor Author

achirkin commented Nov 1, 2024

CC @cjnolet @benfred do you have any objections to this? I may be missing some context on why the previous implementation was based on thrust.

@achirkin
Copy link
Contributor Author

achirkin commented Nov 1, 2024

The problem is encountered in rapidsai/cuvs#261, which uses a custom container as a workaround at the moment.

@achirkin achirkin self-assigned this Nov 1, 2024
Copy link
Contributor

@wphicks wphicks left a comment

Choose a reason for hiding this comment

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

I'm pretty sure I introduced pinned mdarrays with the mdbuffer PR. I was squeamish about this at the time but was trying to stick to "official" container types from Thrust/RMM. If we still don't have a good pinned uvector type, though, I'm definitely in support of this change.

Would we want to do the allocation through RMM's pinned memory resource, or are we okay with a raw cudaMallocHost here?

@achirkin
Copy link
Contributor Author

achirkin commented Nov 5, 2024

Thanks, that's a good point! I've looked at the RMM resources and there were two pinned memory resources, so I was not sure which not to use. But perhaps more importantly, they have the stream semantics in their API, which is a bit misleading in the context of pinned arrays, which are created on the host and do not offer any stream ordering.

@achirkin
Copy link
Contributor Author

achirkin commented Nov 5, 2024

It's also worth noting, that we don't need the .resize() functionality for the mdarrays, which makes the implementation extremely simple compared to RMM.

Copy link
Contributor

@wphicks wphicks left a comment

Choose a reason for hiding this comment

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

Good question on which pinned memory resource to use. As near as I can tell, the only difference is that pinned_host_memory_resource uses cudaHostAlloc while pinned_memory_resource uses cudaMallocHost. For modern platforms, I believe we should prefer the cudaHostAlloc version. In terms of stream ordering, I'm not really sure why we pass a stream there. Maybe just for API compatibility with other memory resources?

Regardless, I'd definitely support merging as is. How we perform the allocation is an implementation detail we can revisit later unless we have easy answers right now.

@achirkin
Copy link
Contributor Author

achirkin commented Nov 6, 2024

/merge

@rapids-bot rapids-bot bot merged commit 45f24ab into rapidsai:branch-24.12 Nov 6, 2024
70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 - Ready for Review breaking Breaking change cpp improvement Improvement / enhancement to an existing function
Projects
Development

Successfully merging this pull request may close these issues.

2 participants