@@ -82,6 +82,8 @@ int main( int argc, char *argv[] )
8282 ze_device_handle_t pDevice = nullptr ;
8383 uint32_t driverCount = 0 ;
8484 zel_tracer_handle_t tracer = nullptr ;
85+ std::vector<ze_driver_handle_t > drivers;
86+ std::vector<ze_device_handle_t > devices_per_driver;
8587 if ( init_ze (legacy_init, driverCount, driverTypeDesc) )
8688 {
8789
@@ -106,7 +108,6 @@ int main( int argc, char *argv[] )
106108 }
107109 }
108110
109- std::vector<ze_driver_handle_t > drivers;
110111 if (legacy_init) {
111112 status = zeDriverGet (&driverCount, nullptr );
112113 if (status != ZE_RESULT_SUCCESS ) {
@@ -129,93 +130,106 @@ int main( int argc, char *argv[] )
129130 }
130131 }
131132
132- for ( uint32_t driver = 0 ; driver < driverCount ; ++driver )
133+ for ( size_t driver = 0 ; driver < drivers. size () ; ++driver )
133134 {
134135 std::cout << " Driver # " << driver << " \n " ;
135136 pDriver = drivers[driver];
136137 pDevice = findDevice ( pDriver, type );
138+ if (pDevice) {
139+ devices_per_driver.push_back (pDevice);
140+ } else {
141+ devices_per_driver.push_back (nullptr );
142+ }
137143 }
138144 }
139145
140- if ( !pDevice )
146+ if ( devices_per_driver. empty () || drivers. empty () )
141147 {
142148 std::cout << " Did NOT find matching " << to_string (type) <<" device!" << " \n " ;
143149 return -1 ;
144150 }
145151
152+ for (size_t driver_idx = 0 ; driver_idx < drivers.size (); ++driver_idx) {
153+ if (driver_idx >= devices_per_driver.size () || devices_per_driver[driver_idx] == nullptr ) {
154+ std::cout << " No valid device found for Driver #" << driver_idx << std::endl;
155+ continue ;
156+ }
157+ pDriver = drivers[driver_idx];
158+ pDevice = devices_per_driver[driver_idx];
159+ std::cout << " Executing on Driver #" << driver_idx << " , Device #" << 0 << std::endl;
160+ // Create the context
161+ ze_context_handle_t context;
162+ ze_context_desc_t context_desc = {};
163+ context_desc.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC ;
164+ status = zeContextCreate (pDriver, &context_desc, &context);
165+ if (status != ZE_RESULT_SUCCESS ) {
166+ std::cout << " zeContextCreate Failed with return code: " << to_string (status) << std::endl;
167+ continue ;
168+ }
146169
147- // Create the context
148- ze_context_handle_t context ;
149- ze_context_desc_t context_desc = {} ;
150- context_desc. stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC ;
151- status = zeContextCreate (pDriver, &context_desc , &context );
152- if (status != ZE_RESULT_SUCCESS ) {
153- std::cout << " zeContextCreate Failed with return code: " << to_string (status) << std::endl;
154- exit ( 1 ) ;
155- }
170+ // Create an immediate command list for direct submission
171+ ze_command_queue_desc_t altdesc = {} ;
172+ altdesc. stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC ;
173+ ze_command_list_handle_t command_list = {} ;
174+ status = zeCommandListCreateImmediate (context, pDevice, &altdesc , &command_list );
175+ if (status != ZE_RESULT_SUCCESS ) {
176+ std::cout << " zeCommandListCreateImmediate Failed with return code: " << to_string (status) << std::endl;
177+ continue ;
178+ }
156179
157- // Create an immediate command list for direct submission
158- ze_command_queue_desc_t altdesc = {};
159- altdesc.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC ;
160- ze_command_list_handle_t command_list = {};
161- status = zeCommandListCreateImmediate (context, pDevice, &altdesc, &command_list);
162- if (status != ZE_RESULT_SUCCESS ) {
163- std::cout << " zeCommandListCreateImmediate Failed with return code: " << to_string (status) << std::endl;
164- exit (1 );
165- }
180+ // Create an event to be signaled by the device
181+ ze_event_pool_desc_t ep_desc = {};
182+ ep_desc.stype = ZE_STRUCTURE_TYPE_EVENT_POOL_DESC ;
183+ ep_desc.count = 1 ;
184+ ep_desc.flags = ZE_EVENT_POOL_FLAG_HOST_VISIBLE ;
185+ ze_event_desc_t ev_desc = {};
186+ ev_desc.stype = ZE_STRUCTURE_TYPE_EVENT_DESC ;
187+ ev_desc.signal = ZE_EVENT_SCOPE_FLAG_HOST ;
188+ ev_desc.wait = ZE_EVENT_SCOPE_FLAG_HOST ;
189+ ze_event_handle_t event;
190+ ze_event_pool_handle_t event_pool;
191+
192+ status = zeEventPoolCreate (context, &ep_desc, 1 , &pDevice, &event_pool);
193+ if (status != ZE_RESULT_SUCCESS ) {
194+ std::cout << " zeEventPoolCreate Failed with return code: " << to_string (status) << std::endl;
195+ continue ;
196+ }
166197
167- // Create an event to be signaled by the device
168- ze_event_pool_desc_t ep_desc = {};
169- ep_desc.stype = ZE_STRUCTURE_TYPE_EVENT_POOL_DESC ;
170- ep_desc.count = 1 ;
171- ep_desc.flags = ZE_EVENT_POOL_FLAG_HOST_VISIBLE ;
172- ze_event_desc_t ev_desc = {};
173- ev_desc.stype = ZE_STRUCTURE_TYPE_EVENT_DESC ;
174- ev_desc.signal = ZE_EVENT_SCOPE_FLAG_HOST ;
175- ev_desc.wait = ZE_EVENT_SCOPE_FLAG_HOST ;
176- ze_event_handle_t event;
177- ze_event_pool_handle_t event_pool;
178-
179- status = zeEventPoolCreate (context, &ep_desc, 1 , &pDevice, &event_pool);
180- if (status != ZE_RESULT_SUCCESS ) {
181- std::cout << " zeEventPoolCreate Failed with return code: " << to_string (status) << std::endl;
182- exit (1 );
183- }
198+ status = zeEventCreate (event_pool, &ev_desc, &event);
199+ if (status != ZE_RESULT_SUCCESS ) {
200+ std::cout << " zeEventCreate Failed with return code: " << to_string (status) << std::endl;
201+ continue ;
202+ }
184203
185- status = zeEventCreate (event_pool, &ev_desc, &event);
186- if (status != ZE_RESULT_SUCCESS ) {
187- std::cout << " zeEventCreate Failed with return code: " << to_string (status) << std::endl;
188- exit (1 );
189- }
204+ // signal the event from the device and wait for completion
205+ zeCommandListAppendSignalEvent (command_list, event);
206+ zeEventHostSynchronize (event, UINT64_MAX );
207+ std::cout << " Congratulations, Executing on Driver #" << driver_idx << " , Device #" << 0 << " completed execution!" << std::endl;
190208
191- // signal the event from the device and wait for completion
192- zeCommandListAppendSignalEvent (command_list, event);
193- zeEventHostSynchronize (event, UINT64_MAX );
194- std::cout << " Congratulations, the device completed execution!\n " ;
195-
196- if (!zelCheckIsLoaderInTearDown ())
197- zeContextDestroy (context);
198- if (!zelCheckIsLoaderInTearDown ())
199- zeCommandListDestroy (command_list);
200- if (!zelCheckIsLoaderInTearDown ())
201- zeEventDestroy (event);
202- if (!zelCheckIsLoaderInTearDown ())
203- zeEventPoolDestroy (event_pool);
204-
205- if (tracing_enabled) {
206- status = zelTracerDestroy (tracer);
207- if (status != ZE_RESULT_SUCCESS ) {
208- std::cout << " zelTracerDestroy Failed with return code: " << to_string (status) << std::endl;
209- exit (1 );
209+ if (!zelCheckIsLoaderInTearDown ())
210+ zeContextDestroy (context);
211+ if (!zelCheckIsLoaderInTearDown ())
212+ zeCommandListDestroy (command_list);
213+ if (!zelCheckIsLoaderInTearDown ())
214+ zeEventDestroy (event);
215+ if (!zelCheckIsLoaderInTearDown ())
216+ zeEventPoolDestroy (event_pool);
217+
218+ if (tracing_enabled) {
219+ status = zelTracerDestroy (tracer);
220+ if (status != ZE_RESULT_SUCCESS ) {
221+ std::cout << " zelTracerDestroy Failed with return code: " << to_string (status) << std::endl;
222+ exit (1 );
223+ }
210224 }
211- }
212225
213- if (tracing_runtime_enabled) {
214- std::cout << " Disable Tracing Layer after init" << std::endl;
215- status = zelDisableTracingLayer ();
216- if (status != ZE_RESULT_SUCCESS ) {
217- std::cout << " zelDisableTracingLayer Failed with return code: " << to_string (status) << std::endl;
218- exit (1 );
226+ if (tracing_runtime_enabled) {
227+ std::cout << " Disable Tracing Layer after init" << std::endl;
228+ status = zelDisableTracingLayer ();
229+ if (status != ZE_RESULT_SUCCESS ) {
230+ std::cout << " zelDisableTracingLayer Failed with return code: " << to_string (status) << std::endl;
231+ exit (1 );
232+ }
219233 }
220234 }
221235
0 commit comments