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