11/*
22 *
3- * Copyright (C) 2020-2021 Intel Corporation
3+ * Copyright (C) 2020-2025 Intel Corporation
44 *
55 * SPDX-License-Identifier: MIT
66 *
@@ -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,96 +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 {
135+ std::cout << " Driver # " << driver << " \n " ;
134136 pDriver = drivers[driver];
135137 pDevice = findDevice ( pDriver, type );
136- if ( pDevice )
137- {
138- break ;
138+ if (pDevice) {
139+ devices_per_driver.push_back (pDevice);
140+ } else {
141+ devices_per_driver.push_back (nullptr );
139142 }
140143 }
141144 }
142145
143- if ( !pDevice )
146+ if ( devices_per_driver. empty () || drivers. empty () )
144147 {
145148 std::cout << " Did NOT find matching " << to_string (type) <<" device!" << " \n " ;
146149 return -1 ;
147150 }
148151
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+ }
149169
150- // Create the context
151- ze_context_handle_t context ;
152- ze_context_desc_t context_desc = {} ;
153- context_desc. stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC ;
154- status = zeContextCreate (pDriver, &context_desc , &context );
155- if (status != ZE_RESULT_SUCCESS) {
156- std::cout << " zeContextCreate Failed with return code: " << to_string (status) << std::endl;
157- exit ( 1 ) ;
158- }
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+ }
159179
160- // Create an immediate command list for direct submission
161- ze_command_queue_desc_t altdesc = {};
162- altdesc.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
163- ze_command_list_handle_t command_list = {};
164- status = zeCommandListCreateImmediate (context, pDevice, &altdesc, &command_list);
165- if (status != ZE_RESULT_SUCCESS) {
166- std::cout << " zeCommandListCreateImmediate Failed with return code: " << to_string (status) << std::endl;
167- exit (1 );
168- }
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+ }
169197
170- // Create an event to be signaled by the device
171- ze_event_pool_desc_t ep_desc = {};
172- ep_desc.stype = ZE_STRUCTURE_TYPE_EVENT_POOL_DESC;
173- ep_desc.count = 1 ;
174- ep_desc.flags = ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
175- ze_event_desc_t ev_desc = {};
176- ev_desc.stype = ZE_STRUCTURE_TYPE_EVENT_DESC;
177- ev_desc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
178- ev_desc.wait = ZE_EVENT_SCOPE_FLAG_HOST;
179- ze_event_handle_t event;
180- ze_event_pool_handle_t event_pool;
181-
182- status = zeEventPoolCreate (context, &ep_desc, 1 , &pDevice, &event_pool);
183- if (status != ZE_RESULT_SUCCESS) {
184- std::cout << " zeEventPoolCreate Failed with return code: " << to_string (status) << std::endl;
185- exit (1 );
186- }
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+ }
187203
188- status = zeEventCreate (event_pool, &ev_desc, &event);
189- if (status != ZE_RESULT_SUCCESS) {
190- std::cout << " zeEventCreate Failed with return code: " << to_string (status) << std::endl;
191- exit (1 );
192- }
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;
193208
194- // signal the event from the device and wait for completion
195- zeCommandListAppendSignalEvent (command_list, event);
196- zeEventHostSynchronize (event, UINT64_MAX );
197- std::cout << " Congratulations, the device completed execution!\n " ;
198-
199- if (!zelCheckIsLoaderInTearDown ())
200- zeContextDestroy (context);
201- if (!zelCheckIsLoaderInTearDown ())
202- zeCommandListDestroy (command_list);
203- if (!zelCheckIsLoaderInTearDown ())
204- zeEventDestroy (event);
205- if (!zelCheckIsLoaderInTearDown ())
206- zeEventPoolDestroy (event_pool);
207-
208- if (tracing_enabled) {
209- status = zelTracerDestroy (tracer);
210- if (status != ZE_RESULT_SUCCESS) {
211- std::cout << " zelTracerDestroy Failed with return code: " << to_string (status) << std::endl;
212- 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+ }
213224 }
214- }
215225
216- if (tracing_runtime_enabled) {
217- std::cout << " Disable Tracing Layer after init" << std::endl;
218- status = zelDisableTracingLayer ();
219- if (status != ZE_RESULT_SUCCESS) {
220- std::cout << " zelDisableTracingLayer Failed with return code: " << to_string (status) << std::endl;
221- 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+ }
222233 }
223234 }
224235
0 commit comments