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.
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
Store and set the correct CUDA device in device_buffer #1370
Store and set the correct CUDA device in device_buffer #1370
Changes from 4 commits
c7bf767
4633fdf
c2656d2
a4d590e
3778c1b
c3a5edf
14ff4e3
8cadcd6
082283d
fa2c892
2381a79
6a403e0
c9cd9fb
7168fb0
34bbfde
2f56c48
d1ef1be
a70f19d
06e080c
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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: Should the setting of the current device live inside
allocate/deallocate
rather than it being the responsibility of the caller to ensure the device is correct? Or, is this deliberate because we might want more than just the allocate call to occur with the same device active and this approach avoids excessive device switching?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.
Yeah, it's deliberate. I wanted to put it in
allocate_async
/deallocate_async
, but those calls are often made in places where the correct device is also needed for other operations, and we don't want tocuda_set_device_raii
multiple times. There are also places such asresize / shrink_to_fit
where a newdevice_buffer
is created and we want that to happen with the original device active, but inside it we callallocate_async
and that would cause redundant current device checking.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 think what I've arrived at is that in order to minimize device switching, we want to do it at the highest level in
device_buffer
possible, which means the public API functions (when necessary). For the same reason, we assume the user has set the device before constructing thedevice_buffer
, and we just store the ID at that stage.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: Does the appearance of this pattern suggest that the
device_buffer
constructor should have an (optional)device
argument that one can provide, rather than relying on the implicit current cuda device (which is then managed by this raii object here)?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.
If we did that, then if we are eliminating the
cuda_set_device_raii
here, then the constructor would have to first callcudaSetDevice(device)
, and I assume it would do so usingcuda_set_device_raii
, which means on exiting the ctor the previous device would be reset (if different).So then we would need to call
cuda_set_device_raii
again after calling the constructor with the optionaldevice
argument because of the subsequentcudaMemcpyAsync
. That could mean two calls tocudaGetDevice
and four calls tocudaSetDevice
, worst case. The way it is now, there is at most 1cudaGetDevice
and at most 2cudaSetDevice
.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.
Hmm, my understanding from the docs was that runtime calls (excepting [some] of those to do with events, where the call has to happen with the live device matching the event's stream) don't care about the current device and hence allocation/deallocation (which, with a pool mr record events) are the only places we need to handle it.