@@ -78,7 +78,7 @@ Error PluginAdaptorTy::init() {
78
78
}
79
79
80
80
// No devices are supported by this RTL?
81
- NumberOfPluginDevices = number_of_devices ();
81
+ int32_t NumberOfPluginDevices = number_of_devices ();
82
82
if (!NumberOfPluginDevices) {
83
83
return createStringError (inconvertibleErrorCode (),
84
84
" No devices supported in this RTL\n " );
@@ -110,32 +110,33 @@ void PluginManager::init() {
110
110
DP (" RTLs loaded!\n " );
111
111
}
112
112
113
- void PluginAdaptorTy::initDevices (PluginManager &PM) {
114
- if (isUsed ())
113
+ void PluginManager::initDevices (PluginAdaptorTy &RTL) {
114
+ // If this RTL has already been initialized.
115
+ if (PM->DeviceOffsets .contains (&RTL))
115
116
return ;
116
117
TIMESCOPE ();
117
118
118
119
// If this RTL is not already in use, initialize it.
119
- assert (getNumberOfPluginDevices () > 0 &&
120
+ assert (RTL. number_of_devices () > 0 &&
120
121
" Tried to initialize useless plugin adaptor" );
121
122
122
123
// Initialize the device information for the RTL we are about to use.
123
- auto ExclusiveDevicesAccessor = PM. getExclusiveDevicesAccessor ();
124
+ auto ExclusiveDevicesAccessor = getExclusiveDevicesAccessor ();
124
125
125
126
// Initialize the index of this RTL and save it in the used RTLs.
126
- DeviceOffset = ExclusiveDevicesAccessor->size ();
127
+ int32_t DeviceOffset = ExclusiveDevicesAccessor->size ();
127
128
128
- // If possible, set the device identifier offset in the plugin.
129
- if (set_device_offset)
130
- set_device_offset (DeviceOffset);
129
+ // Set the device identifier offset in the plugin.
130
+ RTL.set_device_offset (DeviceOffset);
131
131
132
- int32_t NumPD = getNumberOfPluginDevices ();
132
+ int32_t NumberOfUserDevices = 0 ;
133
+ int32_t NumPD = RTL.number_of_devices ();
133
134
ExclusiveDevicesAccessor->reserve (DeviceOffset + NumPD);
134
135
// Auto zero-copy is a per-device property. We need to ensure
135
136
// that all devices are suggesting to use it.
136
137
bool UseAutoZeroCopy = !(NumPD == 0 );
137
138
for (int32_t PDevI = 0 , UserDevId = DeviceOffset; PDevI < NumPD; PDevI++) {
138
- auto Device = std::make_unique<DeviceTy>(this , UserDevId, PDevI);
139
+ auto Device = std::make_unique<DeviceTy>(&RTL , UserDevId, PDevI);
139
140
if (auto Err = Device->init ()) {
140
141
DP (" Skip plugin known device %d: %s\n " , PDevI,
141
142
toString (std::move (Err)).c_str ());
@@ -153,20 +154,23 @@ void PluginAdaptorTy::initDevices(PluginManager &PM) {
153
154
// If all devices suggest to use it, change requirment flags to trigger
154
155
// zero-copy behavior when mapping memory.
155
156
if (UseAutoZeroCopy)
156
- PM. addRequirements (OMPX_REQ_AUTO_ZERO_COPY);
157
+ addRequirements (OMPX_REQ_AUTO_ZERO_COPY);
157
158
159
+ DeviceOffsets[&RTL] = DeviceOffset;
160
+ DeviceUsed[&RTL] = NumberOfUserDevices;
158
161
DP (" Plugin adaptor " DPxMOD " has index %d, exposes %d out of %d devices!\n " ,
159
- DPxPTR (LibraryHandler.get ()), DeviceOffset, NumberOfUserDevices,
160
- NumberOfPluginDevices );
162
+ DPxPTR (RTL. LibraryHandler .get ()), DeviceOffset, NumberOfUserDevices,
163
+ RTL. number_of_devices () );
161
164
}
162
165
163
166
void PluginManager::initAllPlugins () {
164
167
for (auto &R : PluginAdaptors)
165
- R-> initDevices (*this );
168
+ initDevices (*R );
166
169
}
167
170
168
171
static void registerImageIntoTranslationTable (TranslationTable &TT,
169
- PluginAdaptorTy &RTL,
172
+ int32_t DeviceOffset,
173
+ int32_t NumberOfUserDevices,
170
174
__tgt_device_image *Image) {
171
175
172
176
// same size, as when we increase one, we also increase the other.
@@ -175,8 +179,7 @@ static void registerImageIntoTranslationTable(TranslationTable &TT,
175
179
176
180
// Resize the Targets Table and Images to accommodate the new targets if
177
181
// required
178
- unsigned TargetsTableMinimumSize =
179
- RTL.DeviceOffset + RTL.getNumberOfUserDevices ();
182
+ unsigned TargetsTableMinimumSize = DeviceOffset + NumberOfUserDevices;
180
183
181
184
if (TT.TargetsTable .size () < TargetsTableMinimumSize) {
182
185
TT.DeviceTables .resize (TargetsTableMinimumSize, {});
@@ -186,11 +189,11 @@ static void registerImageIntoTranslationTable(TranslationTable &TT,
186
189
}
187
190
188
191
// Register the image in all devices for this target type.
189
- for (int32_t I = 0 ; I < RTL. getNumberOfUserDevices () ; ++I) {
192
+ for (int32_t I = 0 ; I < NumberOfUserDevices ; ++I) {
190
193
// If we are changing the image we are also invalidating the target table.
191
- if (TT.TargetsImages [RTL. DeviceOffset + I] != Image) {
192
- TT.TargetsImages [RTL. DeviceOffset + I] = Image;
193
- TT.TargetsTable [RTL. DeviceOffset + I] =
194
+ if (TT.TargetsImages [DeviceOffset + I] != Image) {
195
+ TT.TargetsImages [DeviceOffset + I] = Image;
196
+ TT.TargetsTable [DeviceOffset + I] =
194
197
0 ; // lazy initialization of target table.
195
198
}
196
199
}
@@ -228,7 +231,7 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
228
231
DP (" Image " DPxMOD " is compatible with RTL %s!\n " ,
229
232
DPxPTR (Img->ImageStart ), R.Name .c_str ());
230
233
231
- R. initDevices (* this );
234
+ PM-> initDevices (R );
232
235
233
236
// Initialize (if necessary) translation table for this library.
234
237
PM->TrlTblMtx .lock ();
@@ -246,8 +249,10 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
246
249
247
250
DP (" Registering image " DPxMOD " with RTL %s!\n " , DPxPTR (Img->ImageStart ),
248
251
R.Name .c_str ());
249
- registerImageIntoTranslationTable (TransTable, R, Img);
250
- R.UsedImages .insert (Img);
252
+
253
+ registerImageIntoTranslationTable (TransTable, PM->DeviceOffsets [&R],
254
+ PM->DeviceUsed [&R], Img);
255
+ PM->UsedImages .insert (Img);
251
256
252
257
PM->TrlTblMtx .unlock ();
253
258
FoundRTL = &R;
@@ -283,11 +288,11 @@ void PluginManager::unregisterLib(__tgt_bin_desc *Desc) {
283
288
// Scan the RTLs that have associated images until we find one that supports
284
289
// the current image. We only need to scan RTLs that are already being used.
285
290
for (auto &R : PM->pluginAdaptors ()) {
286
- if (!R. isUsed ( ))
291
+ if (!DeviceOffsets. contains (&R ))
287
292
continue ;
288
293
289
294
// Ensure that we do not use any unused images associated with this RTL.
290
- if (!R. UsedImages .contains (Img))
295
+ if (!UsedImages.contains (Img))
291
296
continue ;
292
297
293
298
FoundRTL = &R;
0 commit comments