1+ /*
2+ * Copyright (C) 2021 Intel Corporation
3+ *
4+ * SPDX-License-Identifier: MIT
5+ *
6+ */
7+
8+ #include " shared/source/helpers/array_count.h"
9+ #include " shared/source/helpers/file_io.h"
10+ #include " shared/test/common/helpers/test_files.h"
11+
12+ #include " test.h"
13+
14+ #include " level_zero/core/test/aub_tests/fixtures/aub_fixture.h"
15+ #include " level_zero/core/test/unit_tests/mocks/mock_driver_handle.h"
16+
17+ namespace L0 {
18+ namespace ult {
19+
20+ struct AUBAppendKernelIndirectL0 : Test<AUBFixtureL0> {
21+
22+ static ze_module_handle_t createModuleFromFile (const std::string &fileName, ze_context_handle_t context, ze_device_handle_t device) {
23+ ze_module_handle_t moduleHandle;
24+ std::string testFile;
25+ retrieveBinaryKernelFilenameNoRevision (testFile, fileName + " _" , " .bin" );
26+
27+ size_t size = 0 ;
28+ auto src = loadDataFromFile (
29+ testFile.c_str (),
30+ size);
31+
32+ EXPECT_NE (0u , size);
33+ EXPECT_NE (nullptr , src);
34+
35+ if (!src || size == 0 ) {
36+ return nullptr ;
37+ }
38+
39+ ze_module_desc_t moduleDesc = {ZE_STRUCTURE_TYPE_MODULE_DESC};
40+ moduleDesc.format = ZE_MODULE_FORMAT_NATIVE;
41+ moduleDesc.pInputModule = reinterpret_cast <const uint8_t *>(src.get ());
42+ moduleDesc.inputSize = size;
43+ moduleDesc.pBuildFlags = " " ;
44+
45+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeModuleCreate (context, device, &moduleDesc, &moduleHandle, nullptr ));
46+ return moduleHandle;
47+ }
48+ };
49+ TEST_F (AUBAppendKernelIndirectL0, whenAppendKernelIndirectThenGlobalWorkSizeIsProperlyProgrammed) {
50+ const uint32_t groupSize[] = {1 , 2 , 3 };
51+ const uint32_t groupCount[] = {4 , 3 , 1 };
52+ const uint32_t expectedGlobalWorkSize[] = {groupSize[0 ] * groupCount[0 ],
53+ groupSize[1 ] * groupCount[1 ],
54+ groupSize[2 ] * groupCount[2 ]};
55+ uint8_t size = 3 * sizeof (uint32_t );
56+
57+ NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties (InternalMemoryType::HOST_UNIFIED_MEMORY,
58+ context->rootDeviceIndices ,
59+ context->deviceBitfields );
60+
61+ auto pDispatchTraits = driverHandle->svmAllocsManager ->createHostUnifiedMemoryAllocation (sizeof (ze_group_count_t ), unifiedMemoryProperties);
62+
63+ auto outBuffer = driverHandle->svmAllocsManager ->createHostUnifiedMemoryAllocation (size, unifiedMemoryProperties);
64+
65+ memset (outBuffer, 0 , size);
66+
67+ ze_group_count_t &dispatchTraits = *reinterpret_cast <ze_group_count_t *>(pDispatchTraits);
68+ dispatchTraits.groupCountX = groupCount[0 ];
69+ dispatchTraits.groupCountY = groupCount[1 ];
70+ dispatchTraits.groupCountZ = groupCount[2 ];
71+
72+ ze_module_handle_t moduleHandle = createModuleFromFile (" test_kernel" , context, device);
73+ ASSERT_NE (nullptr , moduleHandle);
74+ ze_kernel_handle_t kernel;
75+
76+ ze_kernel_desc_t kernelDesc = {ZE_STRUCTURE_TYPE_KERNEL_DESC};
77+ kernelDesc.pKernelName = " test_get_global_sizes" ;
78+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelCreate (moduleHandle, &kernelDesc, &kernel));
79+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelSetArgumentValue (kernel, 0 , sizeof (void *), &outBuffer));
80+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelSetGroupSize (kernel, groupSize[0 ], groupSize[1 ], groupSize[2 ]));
81+ ze_command_list_handle_t cmdListHandle = commandList->toHandle ();
82+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeCommandListAppendLaunchKernelIndirect (cmdListHandle, kernel, &dispatchTraits, nullptr , 0 , nullptr ));
83+ commandList->close ();
84+
85+ pCmdq->executeCommandLists (1 , &cmdListHandle, nullptr , false );
86+ pCmdq->synchronize (std::numeric_limits<uint32_t >::max ());
87+
88+ EXPECT_TRUE (csr->expectMemory (outBuffer, expectedGlobalWorkSize, size, AubMemDump::CmdServicesMemTraceMemoryCompare::CompareOperationValues::CompareEqual));
89+
90+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelDestroy (kernel));
91+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeModuleDestroy (moduleHandle));
92+ driverHandle->svmAllocsManager ->freeSVMAlloc (outBuffer);
93+ driverHandle->svmAllocsManager ->freeSVMAlloc (pDispatchTraits);
94+ }
95+
96+ TEST_F (AUBAppendKernelIndirectL0, whenAppendKernelIndirectThenGroupCountIsProperlyProgrammed) {
97+ const uint32_t groupSize[] = {1 , 2 , 3 };
98+ const uint32_t groupCount[] = {4 , 3 , 1 };
99+ uint8_t size = 3 * sizeof (uint32_t );
100+
101+ NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties (InternalMemoryType::HOST_UNIFIED_MEMORY,
102+ context->rootDeviceIndices ,
103+ context->deviceBitfields );
104+
105+ auto pDispatchTraits = driverHandle->svmAllocsManager ->createHostUnifiedMemoryAllocation (sizeof (ze_group_count_t ), unifiedMemoryProperties);
106+
107+ auto outBuffer = driverHandle->svmAllocsManager ->createHostUnifiedMemoryAllocation (size, unifiedMemoryProperties);
108+
109+ memset (outBuffer, 0 , size);
110+
111+ ze_group_count_t &dispatchTraits = *reinterpret_cast <ze_group_count_t *>(pDispatchTraits);
112+ dispatchTraits.groupCountX = groupCount[0 ];
113+ dispatchTraits.groupCountY = groupCount[1 ];
114+ dispatchTraits.groupCountZ = groupCount[2 ];
115+
116+ ze_module_handle_t moduleHandle = createModuleFromFile (" test_kernel" , context, device);
117+ ASSERT_NE (nullptr , moduleHandle);
118+ ze_kernel_handle_t kernel;
119+
120+ ze_kernel_desc_t kernelDesc = {ZE_STRUCTURE_TYPE_KERNEL_DESC};
121+ kernelDesc.pKernelName = " test_get_group_count" ;
122+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelCreate (moduleHandle, &kernelDesc, &kernel));
123+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelSetArgumentValue (kernel, 0 , sizeof (void *), &outBuffer));
124+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelSetGroupSize (kernel, groupSize[0 ], groupSize[1 ], groupSize[2 ]));
125+ ze_command_list_handle_t cmdListHandle = commandList->toHandle ();
126+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeCommandListAppendLaunchKernelIndirect (cmdListHandle, kernel, &dispatchTraits, nullptr , 0 , nullptr ));
127+ commandList->close ();
128+
129+ pCmdq->executeCommandLists (1 , &cmdListHandle, nullptr , false );
130+ pCmdq->synchronize (std::numeric_limits<uint32_t >::max ());
131+
132+ EXPECT_TRUE (csr->expectMemory (outBuffer, groupCount, size, AubMemDump::CmdServicesMemTraceMemoryCompare::CompareOperationValues::CompareEqual));
133+
134+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelDestroy (kernel));
135+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeModuleDestroy (moduleHandle));
136+ driverHandle->svmAllocsManager ->freeSVMAlloc (outBuffer);
137+ driverHandle->svmAllocsManager ->freeSVMAlloc (pDispatchTraits);
138+ }
139+
140+ TEST_F (AUBAppendKernelIndirectL0, whenAppendKernelIndirectThenWorkDimIsProperlyProgrammed) {
141+ NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties (InternalMemoryType::HOST_UNIFIED_MEMORY,
142+ context->rootDeviceIndices ,
143+ context->deviceBitfields );
144+
145+ std::tuple<std::array<uint32_t , 3 > /* groupSize*/ , std::array<uint32_t , 3 > /* groupCount*/ , uint32_t /* expected workdim*/ > testData[]{
146+ {{1 , 1 , 1 }, {1 , 1 , 1 }, 1 },
147+ {{1 , 1 , 1 }, {2 , 1 , 1 }, 1 },
148+ {{1 , 1 , 1 }, {1 , 2 , 1 }, 2 },
149+ {{1 , 1 , 1 }, {1 , 1 , 2 }, 3 },
150+ {{2 , 1 , 1 }, {1 , 1 , 1 }, 1 },
151+ {{2 , 1 , 1 }, {2 , 1 , 1 }, 1 },
152+ {{2 , 1 , 1 }, {1 , 2 , 1 }, 2 },
153+ {{2 , 1 , 1 }, {1 , 1 , 2 }, 3 },
154+ {{1 , 2 , 1 }, {1 , 1 , 1 }, 2 },
155+ {{1 , 2 , 1 }, {2 , 1 , 1 }, 2 },
156+ {{1 , 2 , 1 }, {1 , 2 , 1 }, 2 },
157+ {{1 , 2 , 1 }, {1 , 1 , 2 }, 3 },
158+ {{1 , 1 , 2 }, {1 , 1 , 1 }, 3 },
159+ {{1 , 1 , 2 }, {2 , 1 , 1 }, 3 },
160+ {{1 , 1 , 2 }, {1 , 2 , 1 }, 3 },
161+ {{1 , 1 , 2 }, {1 , 1 , 2 }, 3 }};
162+
163+ ze_command_list_handle_t cmdListHandle = commandList->toHandle ();
164+ ze_module_handle_t moduleHandle = createModuleFromFile (" test_kernel" , context, device);
165+ ASSERT_NE (nullptr , moduleHandle);
166+ ze_kernel_handle_t kernel;
167+
168+ ze_kernel_desc_t kernelDesc = {ZE_STRUCTURE_TYPE_KERNEL_DESC};
169+ kernelDesc.pKernelName = " test_get_work_dim" ;
170+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelCreate (moduleHandle, &kernelDesc, &kernel));
171+ for (auto i = 0u ; i < arrayCount<>(testData); i++) {
172+ std::array<uint32_t , 3 > groupSize;
173+ std::array<uint32_t , 3 > groupCount;
174+ uint32_t expectedWorkDim;
175+
176+ std::tie (groupSize, groupCount, expectedWorkDim) = testData[i];
177+
178+ uint8_t size = sizeof (uint32_t );
179+
180+ auto pDispatchTraits = driverHandle->svmAllocsManager ->createHostUnifiedMemoryAllocation (sizeof (ze_group_count_t ), unifiedMemoryProperties);
181+
182+ auto outBuffer = driverHandle->svmAllocsManager ->createHostUnifiedMemoryAllocation (size, unifiedMemoryProperties);
183+
184+ memset (outBuffer, 0 , size);
185+
186+ ze_group_count_t &dispatchTraits = *reinterpret_cast <ze_group_count_t *>(pDispatchTraits);
187+ dispatchTraits.groupCountX = groupCount[0 ];
188+ dispatchTraits.groupCountY = groupCount[1 ];
189+ dispatchTraits.groupCountZ = groupCount[2 ];
190+
191+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelSetArgumentValue (kernel, 0 , sizeof (void *), &outBuffer));
192+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelSetGroupSize (kernel, groupSize[0 ], groupSize[1 ], groupSize[2 ]));
193+
194+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeCommandListAppendLaunchKernelIndirect (cmdListHandle, kernel, &dispatchTraits, nullptr , 0 , nullptr ));
195+ commandList->close ();
196+
197+ pCmdq->executeCommandLists (1 , &cmdListHandle, nullptr , false );
198+ pCmdq->synchronize (std::numeric_limits<uint32_t >::max ());
199+
200+ EXPECT_TRUE (csr->expectMemory (outBuffer, &expectedWorkDim, size, AubMemDump::CmdServicesMemTraceMemoryCompare::CompareOperationValues::CompareEqual));
201+
202+ driverHandle->svmAllocsManager ->freeSVMAlloc (outBuffer);
203+ driverHandle->svmAllocsManager ->freeSVMAlloc (pDispatchTraits);
204+ commandList->reset ();
205+ }
206+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeKernelDestroy (kernel));
207+ EXPECT_EQ (ZE_RESULT_SUCCESS, zeModuleDestroy (moduleHandle));
208+ }
209+
210+ } // namespace ult
211+ } // namespace L0
0 commit comments