Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sycl/plugins/cuda/pi_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,7 @@ pi_result cuda_piQueueFinish(pi_queue command_queue) {
nullptr); // need PI_ERROR_INVALID_EXTERNAL_HANDLE error code
ScopedContext active(command_queue->get_context());

command_queue->for_each_stream([&result](CUstream s) {
command_queue->sync_streams([&result](CUstream s) {
result = PI_CHECK_ERROR(cuStreamSynchronize(s));
});

Expand Down
43 changes: 42 additions & 1 deletion sycl/plugins/cuda/pi_cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ struct _pi_queue {
std::atomic_uint32_t transfer_stream_idx_;
unsigned int num_compute_streams_;
unsigned int num_transfer_streams_;
unsigned int last_sync_compute_streams_;
unsigned int last_sync_transfer_streams_;
unsigned int flags_;
std::mutex compute_stream_mutex_;
std::mutex transfer_stream_mutex_;
Expand All @@ -403,7 +405,9 @@ struct _pi_queue {
transfer_streams_{std::move(transfer_streams)}, context_{context},
device_{device}, properties_{properties}, refCount_{1}, eventCount_{0},
compute_stream_idx_{0}, transfer_stream_idx_{0},
num_compute_streams_{0}, num_transfer_streams_{0}, flags_(flags) {
num_compute_streams_{0}, num_transfer_streams_{0},
last_sync_compute_streams_{0}, last_sync_transfer_streams_{0},
flags_(flags) {
cuda_piContextRetain(context_);
cuda_piDeviceRetain(device_);
}
Expand Down Expand Up @@ -440,6 +444,43 @@ struct _pi_queue {
}
}

template <typename T> void sync_streams(T &&f) {
{
std::lock_guard<std::mutex> compute_guard(compute_stream_mutex_);
unsigned int size = static_cast<unsigned int>(compute_streams_.size());
unsigned int start = last_sync_compute_streams_;
unsigned int end = num_compute_streams_ < size
? num_compute_streams_
: compute_stream_idx_.load() % size;
Comment thread
steffenlarsen marked this conversation as resolved.
Outdated
if (size == 1 && end == 0) {
f(compute_streams_[0]);
} else {
for (unsigned int i = start; i != end; (++i < size) ? i : (i = 0)) {
f(compute_streams_[i]);
}
last_sync_compute_streams_ = end;
}
}
{
std::lock_guard<std::mutex> transfer_guard(transfer_stream_mutex_);
unsigned int size = static_cast<unsigned int>(transfer_streams_.size());
if (size > 0) {
unsigned int start = last_sync_transfer_streams_;
unsigned int end = num_transfer_streams_ < size
? num_transfer_streams_
: transfer_stream_idx_.load() % size;
if (size == 1 && end == 0) {
f(transfer_streams_[0]);
} else {
for (unsigned int i = start; i != end; (++i < size) ? i : (i = 0)) {
f(transfer_streams_[i]);
}
last_sync_transfer_streams_ = end;
}
}
}
}

_pi_context *get_context() const { return context_; };

pi_uint32 increment_reference_count() noexcept { return ++refCount_; }
Expand Down