Skip to content

Commit 1b9c177

Browse files
iassiourrakesroy
authored andcommitted
SWDEV-501921 - Introduce an activeQueues set
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)
1 parent d14f55b commit 1b9c177

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

hipamd/src/hip_device.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,9 @@ void Device::WaitActiveStreams(hip::Stream* blocking_stream, bool wait_null_stre
185185
waitForStream(null_stream_);
186186
}
187187
} else {
188-
amd::ScopedLock lock(streamSetLock);
189-
190-
for (const auto& active_stream : streamSet) {
191-
// If it's the current device
188+
auto activeQueues = blocking_stream->device().getActiveQueues();
189+
for (const auto& command : activeQueues) {
190+
hip::Stream* active_stream = static_cast<hip::Stream*>(command);
192191
if (// Make sure it's a default stream
193192
((active_stream->Flags() & hipStreamNonBlocking) == 0) &&
194193
// and it's not the current stream

rocclr/device/device.hpp

+20
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,24 @@ class Device : public RuntimeObject {
20822082
return false;
20832083
}
20842084

2085+
//! Returns the queues that have at least one submitted command
2086+
std::vector<amd::CommandQueue*> getActiveQueues() {
2087+
amd::ScopedLock lock(activeQueuesLock_);
2088+
return std::vector<amd::CommandQueue*>(activeQueues.begin(), activeQueues.end());
2089+
}
2090+
2091+
//! Adds the queue to the set of active command queues
2092+
void addToActiveQueues(amd::CommandQueue* commandQueue) {
2093+
amd::ScopedLock lock(activeQueuesLock_);
2094+
activeQueues.insert(commandQueue);
2095+
}
2096+
2097+
//! Removes the queue from the set of active command queues
2098+
void removeFromActiveQueues(amd::CommandQueue* commandQueue) {
2099+
amd::ScopedLock lock(activeQueuesLock_);
2100+
activeQueues.erase(commandQueue);
2101+
}
2102+
20852103
// Notifies device about context destroy
20862104
virtual void ContextDestroy() {}
20872105

@@ -2131,6 +2149,8 @@ class Device : public RuntimeObject {
21312149
uint64_t stack_size_{1024}; //!< Device stack size
21322150
device::Memory* initial_heap_buffer_; //!< Initial heap buffer
21332151
uint64_t initial_heap_size_{HIP_INITIAL_DM_SIZE}; //!< Initial device heap size
2152+
amd::Monitor activeQueuesLock_ {"Guards access to the activeQueues set"};
2153+
std::unordered_set<amd::CommandQueue*> activeQueues; //!< The set of active queues
21342154
private:
21352155
const Isa *isa_; //!< Device isa
21362156
bool IsTypeMatching(cl_device_type type, bool offlineDevices);

rocclr/platform/commandqueue.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ bool HostQueue::terminate() {
6969
// Note that if lastCommand isn't a marker, it may not be lastEnqueueCommand_ now
7070
// after lastCommand->awaitCompletion() is called.
7171
if (lastEnqueueCommand_ != nullptr) {
72+
device_.removeFromActiveQueues(this);
7273
lastEnqueueCommand_ ->release(); // lastEnqueueCommand_ should be a marker
7374
lastEnqueueCommand_ = nullptr;
7475
}
@@ -162,6 +163,7 @@ void HostQueue::finish(bool cpu_wait) {
162163
// Runtime can clear the last command only if no other submissions occured
163164
// during finish()
164165
if (command == lastEnqueueCommand_) {
166+
device_.removeFromActiveQueues(this);
165167
lastEnqueueCommand_->release();
166168
lastEnqueueCommand_ = nullptr;
167169
}
@@ -278,6 +280,9 @@ void HostQueue::append(Command& command) {
278280

279281
if (prevLastEnqueueCommand != nullptr) {
280282
prevLastEnqueueCommand->release();
283+
} else {
284+
// The queue becomes active. Add it to the set of activeQueues.
285+
device_.addToActiveQueues(this);
281286
}
282287
}
283288

rocclr/platform/commandqueue.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ class HostQueue : public CommandQueue {
267267
// Release the last command in the batch
268268
if (lastEnqueueCommand_ != nullptr) {
269269
lastEnqueueCommand_->release();
270+
} else {
271+
// The queue becomes active. Add it to the set of activeQueues.
272+
device_.addToActiveQueues(this);
270273
}
271274

272275
// Extra retain for the last command

0 commit comments

Comments
 (0)