@@ -39,16 +39,13 @@ class KernelProgramCache {
39
39
bool isFilledIn () const { return !Msg.empty (); }
40
40
};
41
41
42
- // / Denotes the state of a build.
43
- enum BuildState { BS_InProgress, BS_Done, BS_Failed };
44
-
45
42
// / Denotes pointer to some entity with its general state and build error.
46
43
// / The pointer is not null if and only if the entity is usable.
47
44
// / State of the entity is provided by the user of cache instance.
48
45
// / Currently there is only a single user - ProgramManager class.
49
46
template <typename T> struct BuildResult {
50
47
std::atomic<T *> Ptr;
51
- std::atomic<BuildState > State;
48
+ std::atomic<int > State;
52
49
BuildError Error;
53
50
54
51
// / Condition variable to signal that build result is ready.
@@ -65,23 +62,15 @@ class KernelProgramCache {
65
62
// / A mutex to be employed along with MBuildCV.
66
63
std::mutex MBuildResultMutex;
67
64
68
- BuildResult (T *P, BuildState S) : Ptr{P}, State{S}, Error{" " , 0 } {}
65
+ BuildResult (T *P, int S) : Ptr{P}, State{S}, Error{" " , 0 } {}
69
66
};
70
67
71
68
using PiProgramT = std::remove_pointer<RT::PiProgram>::type;
72
69
using PiProgramPtrT = std::atomic<PiProgramT *>;
73
70
using ProgramWithBuildStateT = BuildResult<PiProgramT>;
74
71
using ProgramCacheKeyT = std::pair<std::pair<SerializedObj, std::uintptr_t >,
75
72
std::pair<RT::PiDevice, std::string>>;
76
- using CommonProgramKeyT = std::pair<std::uintptr_t , RT::PiDevice>;
77
-
78
- struct ProgramCache {
79
- std::map<ProgramCacheKeyT, ProgramWithBuildStateT> Cache;
80
- std::multimap<CommonProgramKeyT, ProgramCacheKeyT> KeyMap;
81
-
82
- size_t size () const noexcept { return Cache.size (); }
83
- };
84
-
73
+ using ProgramCacheT = std::map<ProgramCacheKeyT, ProgramWithBuildStateT>;
85
74
using ContextPtr = context_impl *;
86
75
87
76
using PiKernelT = std::remove_pointer<RT::PiKernel>::type;
@@ -102,66 +91,21 @@ class KernelProgramCache {
102
91
103
92
void setContextPtr (const ContextPtr &AContext) { MParentContext = AContext; }
104
93
105
- Locked<ProgramCache > acquireCachedPrograms () {
94
+ Locked<ProgramCacheT > acquireCachedPrograms () {
106
95
return {MCachedPrograms, MProgramCacheMutex};
107
96
}
108
97
109
98
Locked<KernelCacheT> acquireKernelsPerProgramCache () {
110
99
return {MKernelsPerProgramCache, MKernelsPerProgramCacheMutex};
111
100
}
112
101
113
- std::pair<ProgramWithBuildStateT *, bool >
114
- getOrInsertProgram (const ProgramCacheKeyT &CacheKey) {
115
- auto LockedCache = acquireCachedPrograms ();
116
- auto &ProgCache = LockedCache.get ();
117
- auto Inserted = ProgCache.Cache .emplace (
118
- std::piecewise_construct, std::forward_as_tuple (CacheKey),
119
- std::forward_as_tuple (nullptr , BS_InProgress));
120
- if (Inserted.second ) {
121
- // Save reference between the common key and the full key.
122
- CommonProgramKeyT CommonKey =
123
- std::make_pair (CacheKey.first .second , CacheKey.second .first );
124
- ProgCache.KeyMap .emplace (std::piecewise_construct,
125
- std::forward_as_tuple (CommonKey),
126
- std::forward_as_tuple (CacheKey));
127
- }
128
- return std::make_pair (&Inserted.first ->second , Inserted.second );
129
- }
130
-
131
- std::pair<KernelWithBuildStateT *, bool >
132
- getOrInsertKernel (RT::PiProgram Program, const std::string &KernelName) {
133
- auto LockedCache = acquireKernelsPerProgramCache ();
134
- auto &Cache = LockedCache.get ()[Program];
135
- auto Inserted = Cache.emplace (
136
- std::piecewise_construct, std::forward_as_tuple (KernelName),
137
- std::forward_as_tuple (nullptr , BS_InProgress));
138
- return std::make_pair (&Inserted.first ->second , Inserted.second );
139
- }
140
-
141
102
template <typename T, class Predicate >
142
103
void waitUntilBuilt (BuildResult<T> &BR, Predicate Pred) const {
143
104
std::unique_lock<std::mutex> Lock (BR.MBuildResultMutex );
144
105
145
106
BR.MBuildCV .wait (Lock, Pred);
146
107
}
147
108
148
- template <typename ExceptionT, typename RetT>
149
- RetT *waitUntilBuilt (BuildResult<RetT> *BuildResult) {
150
- // Any thread which will find nullptr in cache will wait until the pointer
151
- // is not null anymore.
152
- waitUntilBuilt (*BuildResult, [BuildResult]() {
153
- int State = BuildResult->State .load ();
154
- return State == BuildState::BS_Done || State == BuildState::BS_Failed;
155
- });
156
-
157
- if (BuildResult->Error .isFilledIn ()) {
158
- const BuildError &Error = BuildResult->Error ;
159
- throw ExceptionT (Error.Msg , Error.Code );
160
- }
161
-
162
- return BuildResult->Ptr .load ();
163
- }
164
-
165
109
template <typename T> void notifyAllBuild (BuildResult<T> &BR) const {
166
110
BR.MBuildCV .notify_all ();
167
111
}
@@ -188,7 +132,7 @@ class KernelProgramCache {
188
132
// /
189
133
// / This member function should only be used in unit tests.
190
134
void reset () {
191
- MCachedPrograms = ProgramCache {};
135
+ MCachedPrograms = ProgramCacheT {};
192
136
MKernelsPerProgramCache = KernelCacheT{};
193
137
MKernelFastCache = KernelFastCacheT{};
194
138
}
@@ -197,7 +141,7 @@ class KernelProgramCache {
197
141
std::mutex MProgramCacheMutex;
198
142
std::mutex MKernelsPerProgramCacheMutex;
199
143
200
- ProgramCache MCachedPrograms;
144
+ ProgramCacheT MCachedPrograms;
201
145
KernelCacheT MKernelsPerProgramCache;
202
146
ContextPtr MParentContext;
203
147
0 commit comments