1313#include " PluginManager.h"
1414#include " Shared/Debug.h"
1515#include " Shared/Profile.h"
16+ #include " device.h"
1617
1718#include " llvm/Support/Error.h"
1819#include " llvm/Support/ErrorHandling.h"
@@ -60,15 +61,61 @@ void PluginManager::deinit() {
6061 DP (" RTLs unloaded!\n " );
6162}
6263
63- void PluginManager::initAllPlugins () {
64- for (auto &R : plugins ()) {
65- if (auto Err = R.init ()) {
66- [[maybe_unused]] std::string InfoMsg = toString (std::move (Err));
67- DP (" Failed to init plugin: %s\n " , InfoMsg.c_str ());
64+ bool PluginManager::initializePlugin (GenericPluginTy &Plugin) {
65+ if (Plugin.is_initialized ())
66+ return true ;
67+
68+ if (auto Err = Plugin.init ()) {
69+ [[maybe_unused]] std::string InfoMsg = toString (std::move (Err));
70+ DP (" Failed to init plugin: %s\n " , InfoMsg.c_str ());
71+ return false ;
72+ }
73+
74+ DP (" Registered plugin %s with %d visible device(s)\n " , Plugin.getName (),
75+ Plugin.number_of_devices ());
76+ return true ;
77+ }
78+
79+ bool PluginManager::initializeDevice (GenericPluginTy &Plugin,
80+ int32_t DeviceId) {
81+ if (Plugin.is_device_initialized (DeviceId))
82+ return true ;
83+
84+ // Initialize the device information for the RTL we are about to use.
85+ auto ExclusiveDevicesAccessor = getExclusiveDevicesAccessor ();
86+
87+ int32_t UserId = ExclusiveDevicesAccessor->size ();
88+
89+ // Set the device identifier offset in the plugin.
90+ #ifdef OMPT_SUPPORT
91+ Plugin.set_device_identifier (UserId, DeviceId);
92+ #endif
93+
94+ auto Device = std::make_unique<DeviceTy>(&Plugin, UserId, DeviceId);
95+ if (auto Err = Device->init ()) {
96+ [[maybe_unused]] std::string InfoMsg = toString (std::move (Err));
97+ DP (" Failed to init device %d: %s\n " , DeviceId, InfoMsg.c_str ());
98+ return false ;
99+ }
100+
101+ ExclusiveDevicesAccessor->push_back (std::move (Device));
102+
103+ // We need to map between the plugin's device identifier and the one
104+ // that OpenMP will use.
105+ PM->DeviceIds [std::make_pair (&Plugin, DeviceId)] = UserId;
106+
107+ return true ;
108+ }
109+
110+ void PluginManager::initializeAllDevices () {
111+ for (auto &Plugin : plugins ()) {
112+ if (!initializePlugin (Plugin))
68113 continue ;
114+
115+ for (int32_t DeviceId = 0 ; DeviceId < Plugin.number_of_devices ();
116+ ++DeviceId) {
117+ initializeDevice (Plugin, DeviceId);
69118 }
70- DP (" Registered plugin %s with %d visible device(s)\n " , R.getName (),
71- R.number_of_devices ());
72119 }
73120}
74121
@@ -94,19 +141,12 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
94141
95142 // Scan the RTLs that have associated images until we find one that supports
96143 // the current image.
97- for (auto &R : PM-> plugins ()) {
144+ for (auto &R : plugins ()) {
98145 if (!R.is_plugin_compatible (Img))
99146 continue ;
100147
101- if (!R.is_initialized ()) {
102- if (auto Err = R.init ()) {
103- [[maybe_unused]] std::string InfoMsg = toString (std::move (Err));
104- DP (" Failed to init plugin: %s\n " , InfoMsg.c_str ());
105- continue ;
106- }
107- DP (" Registered plugin %s with %d visible device(s)\n " , R.getName (),
108- R.number_of_devices ());
109- }
148+ if (!initializePlugin (R))
149+ continue ;
110150
111151 if (!R.number_of_devices ()) {
112152 DP (" Skipping plugin %s with no visible devices\n " , R.getName ());
@@ -120,30 +160,8 @@ void PluginManager::registerLib(__tgt_bin_desc *Desc) {
120160 DP (" Image " DPxMOD " is compatible with RTL %s device %d!\n " ,
121161 DPxPTR (Img->ImageStart ), R.getName (), DeviceId);
122162
123- if (!R.is_device_initialized (DeviceId)) {
124- // Initialize the device information for the RTL we are about to use.
125- auto ExclusiveDevicesAccessor = getExclusiveDevicesAccessor ();
126-
127- int32_t UserId = ExclusiveDevicesAccessor->size ();
128-
129- // Set the device identifier offset in the plugin.
130- #ifdef OMPT_SUPPORT
131- R.set_device_identifier (UserId, DeviceId);
132- #endif
133-
134- auto Device = std::make_unique<DeviceTy>(&R, UserId, DeviceId);
135- if (auto Err = Device->init ()) {
136- [[maybe_unused]] std::string InfoMsg = toString (std::move (Err));
137- DP (" Failed to init device %d: %s\n " , DeviceId, InfoMsg.c_str ());
138- continue ;
139- }
140-
141- ExclusiveDevicesAccessor->push_back (std::move (Device));
142-
143- // We need to map between the plugin's device identifier and the one
144- // that OpenMP will use.
145- PM->DeviceIds [std::make_pair (&R, DeviceId)] = UserId;
146- }
163+ if (!initializeDevice (R, DeviceId))
164+ continue ;
147165
148166 // Initialize (if necessary) translation table for this library.
149167 PM->TrlTblMtx .lock ();
@@ -219,7 +237,7 @@ void PluginManager::unregisterLib(__tgt_bin_desc *Desc) {
219237
220238 // Scan the RTLs that have associated images until we find one that supports
221239 // the current image. We only need to scan RTLs that are already being used.
222- for (auto &R : PM-> plugins ()) {
240+ for (auto &R : plugins ()) {
223241 if (R.is_initialized ())
224242 continue ;
225243
0 commit comments