14
14
15
15
#include < boost/process.hpp>
16
16
#include < boost/filesystem.hpp>
17
+ #include < thread>
17
18
18
19
namespace bp = boost::process;
19
20
namespace fs = boost::filesystem;
@@ -24,11 +25,9 @@ namespace lzt = level_zero_tests;
24
25
25
26
namespace {
26
27
27
- struct workload_thread_parameters {
28
- ze_device_handle_t device;
29
- std::mutex device_mutex;
30
- std::condition_variable condition;
31
- bool get_process_state_flag;
28
+ struct device_handles_t {
29
+ ze_device_handle_t core_handle;
30
+ zes_device_handle_t sysman_handle;
32
31
};
33
32
34
33
uint32_t get_prop_length (char prop[ZES_STRING_PROPERTY_SIZE]) {
@@ -538,7 +537,33 @@ ze_device_handle_t get_core_device_by_uuid(uint8_t *uuid) {
538
537
}
539
538
#endif // USE_ZESINIT
540
539
541
- void compute_workload (workload_thread_parameters *t_params) {
540
+ bool is_compute_engine_used (int pid, zes_device_handle_t device) {
541
+ uint32_t count = 0 ;
542
+ auto processes = lzt::get_processes_state (device, count);
543
+
544
+ for (const auto &process : processes) {
545
+ if (process.processId == pid &&
546
+ process.engines == ZES_ENGINE_TYPE_FLAG_COMPUTE) {
547
+ return true ;
548
+ }
549
+ }
550
+ return false ;
551
+ }
552
+
553
+ bool validate_engine_type (ze_event_handle_t event,
554
+ zes_device_handle_t sysman_device) {
555
+ bool is_compute_engine = false ;
556
+ int process_id = getpid ();
557
+
558
+ do {
559
+ is_compute_engine = is_compute_engine_used (process_id, sysman_device);
560
+ } while (zeEventQueryStatus (event) != ZE_RESULT_SUCCESS &&
561
+ !is_compute_engine);
562
+
563
+ return is_compute_engine;
564
+ }
565
+
566
+ bool compute_workload_and_validate (device_handles_t device) {
542
567
543
568
int m, k, n;
544
569
m = k = n = 512 ;
@@ -547,15 +572,17 @@ void compute_workload(workload_thread_parameters *t_params) {
547
572
std::vector<float > b (k * n, 1 );
548
573
std::vector<float > c (m * n, 0 );
549
574
550
- ze_device_handle_t device = t_params->device ;
551
- void *a_buffer = lzt::allocate_device_memory (
552
- m * k * sizeof (float ), 1 , 0 , device, lzt::get_default_context ());
553
- void *b_buffer = lzt::allocate_device_memory (
554
- k * n * sizeof (float ), 1 , 0 , device, lzt::get_default_context ());
555
- void *c_buffer = lzt::allocate_device_memory (
556
- m * n * sizeof (float ), 1 , 0 , device, lzt::get_default_context ());
575
+ void *a_buffer = lzt::allocate_device_memory (m * k * sizeof (float ), 1 , 0 ,
576
+ device.core_handle ,
577
+ lzt::get_default_context ());
578
+ void *b_buffer = lzt::allocate_device_memory (k * n * sizeof (float ), 1 , 0 ,
579
+ device.core_handle ,
580
+ lzt::get_default_context ());
581
+ void *c_buffer = lzt::allocate_device_memory (m * n * sizeof (float ), 1 , 0 ,
582
+ device.core_handle ,
583
+ lzt::get_default_context ());
557
584
ze_module_handle_t module =
558
- lzt::create_module (device, " sysman_matrix_multiplication.spv" ,
585
+ lzt::create_module (device. core_handle , " sysman_matrix_multiplication.spv" ,
559
586
ZE_MODULE_FORMAT_IL_SPIRV, nullptr , nullptr );
560
587
ze_kernel_handle_t function =
561
588
lzt::create_function (module , " sysman_matrix_multiplication" );
@@ -566,7 +593,8 @@ void compute_workload(workload_thread_parameters *t_params) {
566
593
lzt::set_argument_value (function, 3 , sizeof (k), &k);
567
594
lzt::set_argument_value (function, 4 , sizeof (n), &n);
568
595
lzt::set_argument_value (function, 5 , sizeof (c_buffer), &c_buffer);
569
- ze_command_list_handle_t cmd_list = lzt::create_command_list (device);
596
+ ze_command_list_handle_t cmd_list =
597
+ lzt::create_command_list (device.core_handle );
570
598
lzt::append_memory_copy (cmd_list, a_buffer, a.data (), lzt::size_in_bytes (a),
571
599
nullptr );
572
600
lzt::append_barrier (cmd_list, nullptr , 0 , nullptr );
@@ -581,26 +609,57 @@ void compute_workload(workload_thread_parameters *t_params) {
581
609
tg.groupCountY = group_count_y;
582
610
tg.groupCountZ = 1 ;
583
611
584
- zeCommandListAppendLaunchKernel (cmd_list, function, &tg, nullptr , 0 , nullptr );
612
+ // events creation
613
+ ze_event_pool_handle_t event_pool = lzt::create_event_pool (
614
+ lzt::get_default_context (), 2 , ZE_EVENT_POOL_FLAG_HOST_VISIBLE);
615
+
616
+ ze_event_handle_t start_event, end_event;
617
+ ze_event_desc_t event_desc = {ZE_STRUCTURE_TYPE_EVENT_DESC, nullptr , 0 ,
618
+ ZE_EVENT_SCOPE_FLAG_DEVICE,
619
+ ZE_EVENT_SCOPE_FLAG_HOST};
620
+
621
+ start_event = lzt::create_event (event_pool, event_desc);
622
+ event_desc.index = 1 ;
623
+ end_event = lzt::create_event (event_pool, event_desc);
624
+
625
+ lzt::append_signal_event (cmd_list, start_event);
626
+
627
+ for (int i = 0 ; i < 20 ; ++i) {
628
+ lzt::append_launch_function (cmd_list, function, &tg, nullptr , 0 , nullptr );
629
+ }
630
+
631
+ lzt::append_signal_event (cmd_list, end_event);
632
+
585
633
lzt::append_barrier (cmd_list, nullptr , 0 , nullptr );
586
634
lzt::close_command_list (cmd_list);
587
- ze_command_queue_handle_t cmd_q = lzt::create_command_queue (device);
635
+ ze_command_queue_handle_t cmd_q =
636
+ lzt::create_command_queue (device.core_handle );
588
637
589
- uint32_t number_iterations = 100 ;
590
- while (number_iterations--) {
591
- lzt::execute_command_lists (cmd_q, 1 , &cmd_list, nullptr );
592
- lzt::synchronize (cmd_q, UINT64_MAX);
593
- t_params->get_process_state_flag = true ;
594
- t_params->condition .notify_one ();
595
- }
638
+ lzt::execute_command_lists (cmd_q, 1 , &cmd_list, nullptr );
639
+
640
+ lzt::event_host_synchronize (start_event,
641
+ std::numeric_limits<uint64_t >::max ());
642
+
643
+ // validating engine type between two events
644
+ auto is_compute_engine =
645
+ validate_engine_type (end_event, device.sysman_handle );
646
+
647
+ lzt::event_host_synchronize (end_event, std::numeric_limits<uint64_t >::max ());
648
+
649
+ lzt::synchronize (cmd_q, UINT64_MAX);
596
650
651
+ lzt::destroy_event (start_event);
652
+ lzt::destroy_event (end_event);
653
+ lzt::destroy_event_pool (event_pool);
597
654
lzt::destroy_command_queue (cmd_q);
598
655
lzt::destroy_command_list (cmd_list);
599
656
lzt::destroy_function (function);
600
657
lzt::free_memory (a_buffer);
601
658
lzt::free_memory (b_buffer);
602
659
lzt::free_memory (c_buffer);
603
660
lzt::destroy_module (module );
661
+
662
+ return is_compute_engine;
604
663
}
605
664
606
665
TEST_F (
@@ -659,37 +718,21 @@ TEST_F(
659
718
660
719
for (auto device : devices) {
661
720
662
- workload_thread_parameters t_params;
663
- t_params.get_process_state_flag = false ;
664
-
721
+ device_handles_t device_handle{};
665
722
#ifdef USE_ZESINIT
666
723
auto sysman_device_properties = lzt::get_sysman_device_properties (device);
667
724
ze_device_handle_t core_device =
668
725
get_core_device_by_uuid (sysman_device_properties.core .uuid .id );
669
726
EXPECT_NE (core_device, nullptr );
670
- t_params. device = core_device;
671
- std::thread thread (compute_workload, &t_params) ;
727
+ device_handle. core_handle = core_device;
728
+ device_handle. sysman_handle = device ;
672
729
#else // USE_ZESINIT
673
- t_params. device = device;
674
- std::thread thread (compute_workload, &t_params) ;
730
+ device_handle. core_handle = device;
731
+ device_handle. sysman_handle = device ;
675
732
#endif // USE_ZESINIT
676
733
677
- uint32_t count = 0 ;
678
- std::unique_lock<std::mutex> locker (t_params.device_mutex );
679
- t_params.condition .wait (locker,
680
- [&] { return t_params.get_process_state_flag ; });
681
-
682
- uint32_t engine_type = 0 ;
683
- while (engine_type != ZES_ENGINE_TYPE_FLAG_COMPUTE) {
684
- auto processes = lzt::get_processes_state (device, count);
685
- for (auto process : processes) {
686
- engine_type = process.engines ;
687
- }
688
- processes.clear ();
689
- }
690
-
691
- thread.join ();
692
- EXPECT_EQ (engine_type, ZES_ENGINE_TYPE_FLAG_COMPUTE);
734
+ bool is_success = compute_workload_and_validate (device_handle);
735
+ EXPECT_TRUE (is_success);
693
736
}
694
737
}
695
738
0 commit comments