Skip to content

Commit

Permalink
SWDEV-501921 - Introduce an activeQueues set
Browse files Browse the repository at this point in the history
The new set tracks only the queues that have a command
submitted to them. This allows for fast iteration
in waitActiveStreams.

Change-Id: I2c832eefa01280d9a87a5f57874d36d2e9441de7
(cherry picked from commit bcc545e)
  • Loading branch information
iassiour authored and rakesroy committed Dec 6, 2024
1 parent d14f55b commit 1b9c177
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
7 changes: 3 additions & 4 deletions hipamd/src/hip_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ void Device::WaitActiveStreams(hip::Stream* blocking_stream, bool wait_null_stre
waitForStream(null_stream_);
}
} else {
amd::ScopedLock lock(streamSetLock);

for (const auto& active_stream : streamSet) {
// If it's the current device
auto activeQueues = blocking_stream->device().getActiveQueues();
for (const auto& command : activeQueues) {
hip::Stream* active_stream = static_cast<hip::Stream*>(command);
if (// Make sure it's a default stream
((active_stream->Flags() & hipStreamNonBlocking) == 0) &&
// and it's not the current stream
Expand Down
20 changes: 20 additions & 0 deletions rocclr/device/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,24 @@ class Device : public RuntimeObject {
return false;
}

//! Returns the queues that have at least one submitted command
std::vector<amd::CommandQueue*> getActiveQueues() {
amd::ScopedLock lock(activeQueuesLock_);
return std::vector<amd::CommandQueue*>(activeQueues.begin(), activeQueues.end());
}

//! Adds the queue to the set of active command queues
void addToActiveQueues(amd::CommandQueue* commandQueue) {
amd::ScopedLock lock(activeQueuesLock_);
activeQueues.insert(commandQueue);
}

//! Removes the queue from the set of active command queues
void removeFromActiveQueues(amd::CommandQueue* commandQueue) {
amd::ScopedLock lock(activeQueuesLock_);
activeQueues.erase(commandQueue);
}

// Notifies device about context destroy
virtual void ContextDestroy() {}

Expand Down Expand Up @@ -2131,6 +2149,8 @@ class Device : public RuntimeObject {
uint64_t stack_size_{1024}; //!< Device stack size
device::Memory* initial_heap_buffer_; //!< Initial heap buffer
uint64_t initial_heap_size_{HIP_INITIAL_DM_SIZE}; //!< Initial device heap size
amd::Monitor activeQueuesLock_ {"Guards access to the activeQueues set"};
std::unordered_set<amd::CommandQueue*> activeQueues; //!< The set of active queues
private:
const Isa *isa_; //!< Device isa
bool IsTypeMatching(cl_device_type type, bool offlineDevices);
Expand Down
5 changes: 5 additions & 0 deletions rocclr/platform/commandqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ bool HostQueue::terminate() {
// Note that if lastCommand isn't a marker, it may not be lastEnqueueCommand_ now
// after lastCommand->awaitCompletion() is called.
if (lastEnqueueCommand_ != nullptr) {
device_.removeFromActiveQueues(this);
lastEnqueueCommand_ ->release(); // lastEnqueueCommand_ should be a marker
lastEnqueueCommand_ = nullptr;
}
Expand Down Expand Up @@ -162,6 +163,7 @@ void HostQueue::finish(bool cpu_wait) {
// Runtime can clear the last command only if no other submissions occured
// during finish()
if (command == lastEnqueueCommand_) {
device_.removeFromActiveQueues(this);
lastEnqueueCommand_->release();
lastEnqueueCommand_ = nullptr;
}
Expand Down Expand Up @@ -278,6 +280,9 @@ void HostQueue::append(Command& command) {

if (prevLastEnqueueCommand != nullptr) {
prevLastEnqueueCommand->release();
} else {
// The queue becomes active. Add it to the set of activeQueues.
device_.addToActiveQueues(this);
}
}

Expand Down
3 changes: 3 additions & 0 deletions rocclr/platform/commandqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ class HostQueue : public CommandQueue {
// Release the last command in the batch
if (lastEnqueueCommand_ != nullptr) {
lastEnqueueCommand_->release();
} else {
// The queue becomes active. Add it to the set of activeQueues.
device_.addToActiveQueues(this);
}

// Extra retain for the last command
Expand Down

0 comments on commit 1b9c177

Please sign in to comment.