Skip to content

Commit c8c91b2

Browse files
[SYCL] Add a leaf limit to the execution graph
This patch adds a leaf limit (per memory object) for the command execution graph in order to avoid leaf bloat in applications that have an overwhelming number of command groups that can be executed in parallel. Limiting the number of leaves is necessary for reducing performance overhead of regular cleanup of finished command nodes. Whenever the limit is exceeded, the oldest leaf is added as a dependency of the new one instead. Signed-off-by: Sergey Semenov <[email protected]>
1 parent 835a05d commit c8c91b2

File tree

4 files changed

+139
-16
lines changed

4 files changed

+139
-16
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//==---------------- circular_buffer.hpp - Circular buffer -----------------==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include <CL/sycl/detail/defines.hpp>
12+
13+
#include <deque>
14+
#include <utility>
15+
16+
__SYCL_INLINE namespace cl {
17+
namespace sycl {
18+
namespace detail {
19+
20+
// A partial implementation of a circular buffer: once its capacity is full,
21+
// new data overwrites the old.
22+
template <typename T> class CircularBuffer {
23+
public:
24+
explicit CircularBuffer(size_t Capacity) : MCapacity{Capacity} {};
25+
26+
using value_type = T;
27+
using pointer = T *;
28+
using const_pointer = const T *;
29+
using reference = T &;
30+
using const_reference = const T &;
31+
32+
using iterator = typename std::deque<T>::iterator;
33+
using const_iterator = typename std::deque<T>::const_iterator;
34+
35+
iterator begin() { return MValues.begin(); }
36+
37+
const_iterator begin() const { return MValues.begin(); }
38+
39+
iterator end() { return MValues.end(); }
40+
41+
const_iterator end() const { return MValues.end(); }
42+
43+
reference front() { return MValues.front(); }
44+
45+
const_reference front() const { return MValues.front(); }
46+
47+
reference back() { return MValues.back(); }
48+
49+
const_reference back() const { return MValues.back(); }
50+
51+
reference operator[](size_t Idx) { return MValues[Idx]; }
52+
53+
const_reference operator[](size_t Idx) const { return MValues[Idx]; }
54+
55+
size_t size() const { return MValues.size(); }
56+
57+
size_t capacity() const { return MCapacity; }
58+
59+
bool empty() const { return MValues.empty(); };
60+
61+
bool full() const { return MValues.size() == MCapacity; };
62+
63+
void push_back(T Val) {
64+
if (MValues.size() == MCapacity)
65+
MValues.pop_front();
66+
MValues.push_back(std::move(Val));
67+
}
68+
69+
void push_front(T Val) {
70+
if (MValues.size() == MCapacity)
71+
MValues.pop_back();
72+
MValues.push_front(std::move(Val));
73+
}
74+
75+
void pop_back() { MValues.pop_back(); }
76+
77+
void pop_front() { MValues.pop_front(); }
78+
79+
void erase(const_iterator Pos) { MValues.erase(Pos); }
80+
81+
void erase(const_iterator First, const_iterator Last) {
82+
MValues.erase(First, Last);
83+
}
84+
85+
void clear() { MValues.clear(); }
86+
87+
private:
88+
std::deque<T> MValues;
89+
const size_t MCapacity;
90+
};
91+
92+
} // namespace detail
93+
} // namespace sycl
94+
} // namespace cl

sycl/include/CL/sycl/detail/scheduler/scheduler.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#pragma once
1010

1111
#include <CL/sycl/detail/cg.hpp>
12+
#include <CL/sycl/detail/circular_buffer.hpp>
1213
#include <CL/sycl/detail/scheduler/commands.hpp>
1314
#include <CL/sycl/detail/sycl_mem_obj_i.hpp>
1415

@@ -32,21 +33,25 @@ using ContextImplPtr = std::shared_ptr<detail::context_impl>;
3233
// The MemObjRecord is created for each memory object used in command
3334
// groups. There should be only one MemObjRecord for SYCL memory object.
3435
struct MemObjRecord {
36+
MemObjRecord(ContextImplPtr CurContext, size_t LeafLimit)
37+
: MReadLeaves{LeafLimit}, MWriteLeaves{LeafLimit}, MCurContext{
38+
CurContext} {}
39+
3540
// Contains all allocation commands for the memory object.
3641
std::vector<AllocaCommandBase *> MAllocaCommands;
3742

3843
// Contains latest read only commands working with memory object.
39-
std::vector<Command *> MReadLeaves;
44+
CircularBuffer<Command *> MReadLeaves;
4045

4146
// Contains latest write commands working with memory object.
42-
std::vector<Command *> MWriteLeaves;
47+
CircularBuffer<Command *> MWriteLeaves;
4348

4449
// The context which has the latest state of the memory object.
4550
ContextImplPtr MCurContext;
4651

4752
// The flag indicates that the content of the memory object was/will be
4853
// modified. Used while deciding if copy back needed.
49-
bool MMemModified;
54+
bool MMemModified = false;
5055
};
5156

5257
class Scheduler {
@@ -165,6 +170,9 @@ class Scheduler {
165170
std::set<Command *> findDepsForReq(MemObjRecord *Record, Requirement *Req,
166171
const ContextImplPtr &Context);
167172

173+
// Finds a command dependency corresponding to the record
174+
DepDesc findDepForRecord(Command *Cmd, MemObjRecord *Record);
175+
168176
// Searches for suitable alloca in memory record.
169177
AllocaCommandBase *findAllocaForReq(MemObjRecord *Record, Requirement *Req,
170178
const ContextImplPtr &Context);

sycl/source/detail/scheduler/graph_builder.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ Scheduler::GraphBuilder::getOrInsertMemObjRecord(const QueueImplPtr &Queue,
120120
if (nullptr != Record)
121121
return Record;
122122

123-
MemObject->MRecord.reset(new MemObjRecord{/*MAllocaCommands*/ {},
124-
/*MReadLeaves*/ {},
125-
/*MWriteLeaves*/ {},
126-
Queue->getContextImplPtr(),
127-
/*MMemModified*/ false});
123+
const size_t LeafLimit = 8;
124+
MemObject->MRecord.reset(
125+
new MemObjRecord{Queue->getContextImplPtr(), LeafLimit});
128126

129127
MMemObjs.push_back(MemObject);
130128
return MemObject->MRecord.get();
@@ -153,10 +151,21 @@ void Scheduler::GraphBuilder::UpdateLeaves(const std::set<Command *> &Cmds,
153151
void Scheduler::GraphBuilder::AddNodeToLeaves(MemObjRecord *Record,
154152
Command *Cmd,
155153
access::mode AccessMode) {
156-
if (AccessMode == access::mode::read)
157-
Record->MReadLeaves.push_back(Cmd);
158-
else
159-
Record->MWriteLeaves.push_back(Cmd);
154+
CircularBuffer<Command *> &Leaves{AccessMode == access::mode::read
155+
? Record->MReadLeaves
156+
: Record->MWriteLeaves};
157+
if (Leaves.full()) {
158+
Command *OldLeaf = Leaves.front();
159+
if (OldLeaf == Cmd)
160+
return;
161+
// Add the old leaf as a dependency for the new one by duplicating one of
162+
// the requirements for the current record
163+
DepDesc Dep = findDepForRecord(Cmd, Record);
164+
Dep.MDepCommand = OldLeaf;
165+
Cmd->addDep(Dep);
166+
OldLeaf->addUser(Cmd);
167+
}
168+
Leaves.push_back(Cmd);
160169
}
161170

162171
UpdateHostRequirementCommand *Scheduler::GraphBuilder::insertUpdateHostReqCmd(
@@ -389,9 +398,8 @@ Scheduler::GraphBuilder::findDepsForReq(MemObjRecord *Record, Requirement *Req,
389398
std::set<Command *> Visited;
390399
const bool ReadOnlyReq = Req->MAccessMode == access::mode::read;
391400

392-
std::vector<Command *> ToAnalyze;
393-
394-
ToAnalyze = Record->MWriteLeaves;
401+
std::vector<Command *> ToAnalyze{Record->MWriteLeaves.begin(),
402+
Record->MWriteLeaves.end()};
395403

396404
if (!ReadOnlyReq)
397405
ToAnalyze.insert(ToAnalyze.begin(), Record->MReadLeaves.begin(),
@@ -436,6 +444,19 @@ Scheduler::GraphBuilder::findDepsForReq(MemObjRecord *Record, Requirement *Req,
436444
return RetDeps;
437445
}
438446

447+
// A helper function for finding a command dependency on a specific memory
448+
// object
449+
DepDesc Scheduler::GraphBuilder::findDepForRecord(Command *Cmd,
450+
MemObjRecord *Record) {
451+
for (const DepDesc &DD : Cmd->MDeps) {
452+
if (getMemObjRecord(DD.MDepRequirement->MSYCLMemObj) == Record) {
453+
return DD;
454+
}
455+
}
456+
assert(false && "No dependency found for a leaf of the record");
457+
return {nullptr, nullptr, nullptr};
458+
}
459+
439460
// The function searches for the alloca command matching context and
440461
// requirement.
441462
AllocaCommandBase *Scheduler::GraphBuilder::findAllocaForReq(

sycl/source/detail/scheduler/scheduler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ EventImplPtr Scheduler::addHostAccessor(Requirement *Req) {
146146
void Scheduler::releaseHostAccessor(Requirement *Req) {
147147
Req->MBlockedCmd->MCanEnqueue = true;
148148
MemObjRecord* Record = Req->MSYCLMemObj->MRecord.get();
149-
auto EnqueueLeaves = [](std::vector<Command *> &Leaves) {
149+
auto EnqueueLeaves = [](CircularBuffer<Command *> &Leaves) {
150150
for (Command *Cmd : Leaves) {
151151
EnqueueResultT Res;
152152
bool Enqueued = GraphProcessor::enqueueCommand(Cmd, Res);

0 commit comments

Comments
 (0)