1+ /*
2+ * Copyright (C) 2020 Intel Corporation
3+ *
4+ * SPDX-License-Identifier: MIT
5+ *
6+ */
7+
8+ #include < level_zero/ze_api.h>
9+
10+ #include < cstring>
11+ #include < fstream>
12+ #include < iostream>
13+ #include < limits>
14+ #include < memory>
15+ #include < string>
16+ #include < vector>
17+
18+ extern bool verbose;
19+
20+ template <bool TerminateOnFailure, typename ResulT>
21+ inline void validate (ResulT result, const char *message) {
22+ if (result == ZE_RESULT_SUCCESS) {
23+ if (verbose) {
24+ std::cerr << " SUCCESS : " << message << std::endl;
25+ }
26+ return ;
27+ }
28+
29+ if (verbose) {
30+ std::cerr << (TerminateOnFailure ? " ERROR : " : " WARNING : " ) << message << " : " << result
31+ << std::endl;
32+ }
33+
34+ if (TerminateOnFailure) {
35+ std::terminate ();
36+ }
37+ }
38+
39+ #define SUCCESS_OR_TERMINATE (CALL ) validate<true >(CALL, #CALL)
40+ #define SUCCESS_OR_TERMINATE_BOOL (FLAG ) validate<true >(!(FLAG), #FLAG)
41+ #define SUCCESS_OR_WARNING (CALL ) validate<false >(CALL, #CALL)
42+ #define SUCCESS_OR_WARNING_BOOL (FLAG ) validate<false >(!(FLAG), #FLAG)
43+
44+ inline bool isParamEnabled (int argc, char *argv[], const char *shortName, const char *longName) {
45+ char **arg = &argv[1 ];
46+ char **argE = &argv[argc];
47+
48+ for (; arg != argE; ++arg) {
49+ if ((0 == strcmp (*arg, shortName)) || (0 == strcmp (*arg, longName))) {
50+ return true ;
51+ }
52+ }
53+
54+ return false ;
55+ }
56+
57+ inline bool isVerbose (int argc, char *argv[]) {
58+ bool enabled = isParamEnabled (argc, argv, " -v" , " --verbose" );
59+ if (enabled == false ) {
60+ return false ;
61+ }
62+
63+ std::cerr << " Verbose mode detected" ;
64+
65+ return true ;
66+ }
67+
68+ uint32_t getCommandQueueOrdinal (ze_device_handle_t &device) {
69+ uint32_t numQueueGroups = 0 ;
70+ SUCCESS_OR_TERMINATE (zeDeviceGetCommandQueueGroupProperties (device, &numQueueGroups, nullptr ));
71+ if (numQueueGroups == 0 ) {
72+ std::cout << " No queue groups found!\n " ;
73+ std::terminate ();
74+ }
75+ std::vector<ze_command_queue_group_properties_t > queueProperties (numQueueGroups);
76+ SUCCESS_OR_TERMINATE (zeDeviceGetCommandQueueGroupProperties (device, &numQueueGroups,
77+ queueProperties.data ()));
78+ uint32_t computeQueueGroupOrdinal = numQueueGroups;
79+ for (uint32_t i = 0 ; i < numQueueGroups; i++) {
80+ if (queueProperties[i].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COMPUTE) {
81+ computeQueueGroupOrdinal = i;
82+ break ;
83+ }
84+ }
85+ return computeQueueGroupOrdinal;
86+ }
87+
88+ ze_result_t createCommandQueue (ze_context_handle_t &context, ze_device_handle_t &device, ze_command_queue_handle_t &cmdQueue) {
89+ ze_command_queue_desc_t descriptor = {};
90+ descriptor.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
91+
92+ descriptor.pNext = nullptr ;
93+ descriptor.flags = 0 ;
94+ descriptor.mode = ZE_COMMAND_QUEUE_MODE_DEFAULT;
95+ descriptor.priority = ZE_COMMAND_QUEUE_PRIORITY_NORMAL;
96+
97+ descriptor.ordinal = getCommandQueueOrdinal (device);
98+ descriptor.index = 0 ;
99+ return zeCommandQueueCreate (context, device, &descriptor, &cmdQueue);
100+ }
101+
102+ ze_result_t createCommandList (ze_context_handle_t &context, ze_device_handle_t &device, ze_command_list_handle_t &cmdList) {
103+ ze_command_list_desc_t descriptor = {};
104+ descriptor.stype = ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC;
105+
106+ descriptor.pNext = nullptr ;
107+ descriptor.flags = 0 ;
108+ descriptor.commandQueueGroupOrdinal = getCommandQueueOrdinal (device);
109+
110+ return zeCommandListCreate (context, device, &descriptor, &cmdList);
111+ }
112+
113+ ze_device_handle_t zelloInitContextAndGetDevices (ze_context_handle_t &context) {
114+ SUCCESS_OR_TERMINATE (zeInit (ZE_INIT_FLAG_GPU_ONLY));
115+
116+ uint32_t driverCount = 0 ;
117+ SUCCESS_OR_TERMINATE (zeDriverGet (&driverCount, nullptr ));
118+ if (driverCount == 0 ) {
119+ std::cout << " No driver handle found!\n " ;
120+ std::terminate ();
121+ }
122+ ze_driver_handle_t driverHandle;
123+ SUCCESS_OR_TERMINATE (zeDriverGet (&driverCount, &driverHandle));
124+ ze_context_desc_t context_desc = {};
125+ context_desc.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC;
126+ SUCCESS_OR_TERMINATE (zeContextCreate (driverHandle, &context_desc, &context));
127+
128+ uint32_t deviceCount = 0 ;
129+ SUCCESS_OR_TERMINATE (zeDeviceGet (driverHandle, &deviceCount, nullptr ));
130+ if (deviceCount == 0 ) {
131+ std::cout << " No device found!\n " ;
132+ std::terminate ();
133+ }
134+ std::vector<ze_device_handle_t > devices (deviceCount, nullptr );
135+ SUCCESS_OR_TERMINATE (zeDeviceGet (driverHandle, &deviceCount, devices.data ()));
136+ return devices[0 ];
137+ }
0 commit comments