-
Notifications
You must be signed in to change notification settings - Fork 124
Fix race condition in CUDA stream creation #1984
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
omarahmed1111
merged 1 commit into
oneapi-src:main
from
rafbiels:rafbiels/cuda-stream-race-cond
Aug 19, 2024
Merged
Fix race condition in CUDA stream creation #1984
omarahmed1111
merged 1 commit into
oneapi-src:main
from
rafbiels:rafbiels/cuda-stream-race-cond
Aug 19, 2024
Conversation
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
Contributor
Author
|
Reproducer for the crashes: #include <future>
#include <sycl/sycl.hpp>
int main() {
sycl::queue q{};
constexpr static unsigned int numThreads{128};
std::vector<std::future<void>> futures;
std::array<unsigned int, numThreads> hostData{};
std::array<unsigned int*, numThreads> devicePointers{};
futures.reserve(numThreads);
for (unsigned int i{0}; i<numThreads; ++i) {
devicePointers[i] = sycl::malloc_device<unsigned int>(1, q);
}
for (unsigned int i{0}; i<numThreads; ++i) {
futures.push_back(std::async([&q, &devicePointers, &hostData, i](){
q.copy(hostData.data()+i, devicePointers[i], 1);
}));
}
for (unsigned int i{0}; i<numThreads; ++i) {
futures[i].wait();
}
futures.clear();
q.wait_and_throw();
for (unsigned int i{0}; i<numThreads; ++i) {
sycl::free(devicePointers[i], q);
}
}This crashes for me in ~50% of runs before this PR with: |
hdelan
approved these changes
Aug 15, 2024
Contributor
hdelan
left a comment
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 catch! Tricky one
Do not increment NumComputeStreams / NumTransferStreams
before cuStreamCreateWithPriority returns. Too early increment
caused other threads to read the incremented count before a CUDA
stream was created and try to use an invalid stream handle,
causing crashes.
The construction:
```
if (condition) {
lock_this_scope
if (condition) {
create_object
update_condition
}
}
use_object
```
is only thread-safe if update_condition happens after create_object
is completed.
24b6ff7 to
15bca3b
Compare
Contributor
|
Should we add this to v0.10.0? |
steffenlarsen
pushed a commit
to intel/llvm
that referenced
this pull request
Aug 20, 2024
Fix race condition in CUDA stream creation in the UR CUDA adapter See oneapi-src/unified-runtime#1984
kbenzie
pushed a commit
that referenced
this pull request
Aug 20, 2024
Fix race condition in CUDA stream creation
53 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
cuda
CUDA adapter specific issues
ready to merge
Added to PR's which are ready to merge
v0.10.x
Include in the v0.10.x release
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.
Do not increment
NumComputeStreams/NumTransferStreamsbeforecuStreamCreateWithPriorityreturns. Too early increment caused other threads to read the incremented count before a CUDA stream was created and try to use an invalid stream handle, causing crashes.The construction:
is only thread-safe if
update_conditionhappens aftercreate_objectis completed. This PR ensures the ordering.intel/llvm PR: intel/llvm#15100