Skip to content

Commit 8ccadbb

Browse files
Correct clGetDeviceIDs: return all requested devices
Related-To: NEO-4000 Change-Id: I2723b5364bf742aa490d88967c19329830f45322 Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
1 parent 5ac1d12 commit 8ccadbb

File tree

3 files changed

+91
-17
lines changed

3 files changed

+91
-17
lines changed

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!groovy
22
dependenciesRevision='8430df09c44f1b2739eee6ce13122f8e20d05776-1351'
33
strategy='EQUAL'
4-
allowedCD=258
4+
allowedCD=257
55
allowedF=7

runtime/api/api.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ cl_int CL_API_CALL clGetDeviceIDs(cl_platform_id platform,
190190
break;
191191
}
192192

193-
Device *device = pPlatform->getDevice(0);
194-
DEBUG_BREAK_IF(device == nullptr);
193+
if (DebugManager.flags.LimitAmountOfReturnedDevices.get()) {
194+
numDev = std::min(static_cast<cl_uint>(DebugManager.flags.LimitAmountOfReturnedDevices.get()), numDev);
195+
}
195196

196197
if (deviceType == CL_DEVICE_TYPE_ALL) {
197198
/* According to Spec, set it to all except TYPE_CUSTOM. */
@@ -203,16 +204,20 @@ cl_int CL_API_CALL clGetDeviceIDs(cl_platform_id platform,
203204
}
204205

205206
cl_uint retNum = 0;
207+
for (auto rootDeviceIndex = 0u; rootDeviceIndex < numDev; rootDeviceIndex++) {
206208

207-
if (deviceType & device->getDeviceInfo().deviceType) {
208-
if (devices) {
209-
devices[retNum] = device;
210-
}
211-
retNum++;
212-
}
209+
Device *device = pPlatform->getDevice(rootDeviceIndex);
210+
DEBUG_BREAK_IF(device == nullptr);
213211

214-
if (DebugManager.flags.LimitAmountOfReturnedDevices.get()) {
215-
retNum = std::min(static_cast<uint32_t>(DebugManager.flags.LimitAmountOfReturnedDevices.get()), retNum);
212+
if (deviceType & device->getDeviceInfo().deviceType) {
213+
if (devices) {
214+
if (retNum >= numEntries) {
215+
break;
216+
}
217+
devices[retNum] = device;
218+
}
219+
retNum++;
220+
}
216221
}
217222

218223
if (numDevices) {

unit_tests/api/cl_get_device_ids_tests.inl

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313

1414
using namespace NEO;
1515

16-
typedef api_tests clGetDeviceIDsTests;
16+
namespace NEO {
17+
extern bool overrideDeviceWithDefaultHardwareInfo;
18+
extern bool overrideCommandStreamReceiverCreation;
19+
20+
} // namespace NEO
21+
22+
using clGetDeviceIDsTests = api_tests;
1723

1824
namespace ULT {
1925

@@ -96,9 +102,72 @@ TEST_F(clGetDeviceIDsTests, GivenDeviceTypeCpuWhenGettingDeviceIdsThenDeviceNotF
96102
EXPECT_EQ(numDevices, (cl_uint)0);
97103
}
98104

99-
} // namespace ULT
100-
namespace NEO {
101-
extern bool overrideDeviceWithDefaultHardwareInfo;
102-
extern bool overrideCommandStreamReceiverCreation;
105+
TEST(clGetDeviceIDsTest, givenMultipleRootDevicesWhenGetDeviceIdsThenAllRootDevicesAreReturned) {
106+
constexpr auto numRootDevices = 3u;
107+
VariableBackup<bool> backup(&overrideDeviceWithDefaultHardwareInfo, false);
108+
DebugManagerStateRestore restorer;
109+
DebugManager.flags.CreateMultipleRootDevices.set(numRootDevices);
110+
cl_uint numDevices = 0;
111+
cl_uint numEntries = numRootDevices;
112+
cl_device_id devices[numRootDevices];
113+
auto retVal = clGetDeviceIDs(nullptr, CL_DEVICE_TYPE_ALL, numEntries, devices, &numDevices);
114+
EXPECT_EQ(retVal, CL_SUCCESS);
115+
EXPECT_EQ(numEntries, numDevices);
116+
for (auto i = 0u; i < numRootDevices; i++) {
117+
EXPECT_EQ(devices[i], platform()->getDevice(i));
118+
}
119+
}
120+
TEST(clGetDeviceIDsTest, givenMultipleRootDevicesWhenGetDeviceIdsButNumEntriesIsLowerThanNumDevicesThenSubsetOfRootDevicesIsReturned) {
121+
constexpr auto numRootDevices = 3u;
122+
VariableBackup<bool> backup(&overrideDeviceWithDefaultHardwareInfo, false);
123+
DebugManagerStateRestore restorer;
124+
DebugManager.flags.CreateMultipleRootDevices.set(numRootDevices);
125+
cl_uint maxNumDevices;
126+
auto retVal = clGetDeviceIDs(nullptr, CL_DEVICE_TYPE_ALL, 0, nullptr, &maxNumDevices);
127+
EXPECT_EQ(retVal, CL_SUCCESS);
128+
EXPECT_EQ(numRootDevices, maxNumDevices);
103129

104-
} // namespace NEO
130+
cl_uint numDevices = 0;
131+
cl_uint numEntries = numRootDevices - 1;
132+
cl_device_id devices[numRootDevices];
133+
134+
const auto dummyDevice = reinterpret_cast<cl_device_id>(0x1357);
135+
for (auto i = 0u; i < numRootDevices; i++) {
136+
devices[i] = dummyDevice;
137+
}
138+
139+
retVal = clGetDeviceIDs(nullptr, CL_DEVICE_TYPE_ALL, numEntries, devices, &numDevices);
140+
EXPECT_EQ(retVal, CL_SUCCESS);
141+
EXPECT_LT(numDevices, maxNumDevices);
142+
EXPECT_EQ(numEntries, numDevices);
143+
for (auto i = 0u; i < numEntries; i++) {
144+
EXPECT_EQ(devices[i], platform()->getDevice(i));
145+
}
146+
EXPECT_EQ(devices[numEntries], dummyDevice);
147+
}
148+
149+
TEST(clGetDeviceIDsTest, givenMultipleRootDevicesAndLimitedNumberOfReturnedDevicesWhenGetDeviceIdsThenLimitedNumberOfRootDevicesIsReturned) {
150+
constexpr auto numRootDevices = 3u;
151+
VariableBackup<bool> backup(&overrideDeviceWithDefaultHardwareInfo, false);
152+
DebugManagerStateRestore restorer;
153+
DebugManager.flags.CreateMultipleRootDevices.set(numRootDevices);
154+
DebugManager.flags.LimitAmountOfReturnedDevices.set(numRootDevices - 1);
155+
156+
cl_uint numDevices = 0;
157+
cl_uint numEntries = numRootDevices;
158+
cl_device_id devices[numRootDevices];
159+
160+
const auto dummyDevice = reinterpret_cast<cl_device_id>(0x1357);
161+
for (auto i = 0u; i < numRootDevices; i++) {
162+
devices[i] = dummyDevice;
163+
}
164+
165+
auto retVal = clGetDeviceIDs(nullptr, CL_DEVICE_TYPE_ALL, numEntries, devices, &numDevices);
166+
EXPECT_EQ(retVal, CL_SUCCESS);
167+
EXPECT_EQ(numEntries - 1, numDevices);
168+
for (auto i = 0u; i < numDevices; i++) {
169+
EXPECT_EQ(devices[i], platform()->getDevice(i));
170+
}
171+
EXPECT_EQ(devices[numDevices], dummyDevice);
172+
}
173+
} // namespace ULT

0 commit comments

Comments
 (0)