Skip to content

Commit aa33d56

Browse files
authored
Fix CL_INVALID_WORK_ITEM_SIZE for "reduce" test (#143)
* Fix CL_INVALID_WORK_ITEM_SIZE for "reduce" test The work-group size (WGS) may exceed the maximum allowed per-dimension limit reported by CL_DEVICE_MAX_WORK_ITEM_SIZES[0], leading to clEnqueueNDRangeKernel failing with CL_INVALID_WORK_GROUP_SIZE. This patch queries the device's max work-item sizes and clamps WGS to the valid maximum for the x-dimension. This ensures correct behavior on devices with smaller limits and improves portability. Signed-off-by: Xin Jin <xin.jin@arm.com> * Clamp WGS to CL_DEVICE_MAX_WORK_ITEM_SIZES[0] in reduce.cpp The work-group size (WGS) may exceed the maximum allowed per-dimension limit reported by CL_DEVICE_MAX_WORK_ITEM_SIZES[0], leading to clEnqueueNDRangeKernel failing with CL_INVALID_WORK_GROUP_SIZE. The reduce sample launches kernels with a 1-D NDRange over a flat array of integers, so only the x-dimension limit is relevant. This patch queries the device's max work-item sizes and clamps WGS to the valid maximum for the x-dimension. This ensures correct behavior on devices with smaller limits and improves portability. Signed-off-by: Xin Jin <xin.jin@arm.com> --------- Signed-off-by: Xin Jin <xin.jin@arm.com>
1 parent 1010c00 commit aa33d56

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

samples/core/reduce/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,15 @@ int main(int argc, char *argv[])
386386
OCLERROR_RET(clGetDeviceInfo(device, CL_DEVICE_LOCAL_MEM_SIZE,
387387
sizeof(cl_ulong), &loc_mem, NULL),
388388
error, red);
389+
390+
// Ensure WGS is valid for this device
391+
size_t max_wi_dims[3];
392+
OCLERROR_RET(clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES,
393+
sizeof(max_wi_dims), max_wi_dims, NULL),
394+
error, red);
395+
396+
wgs = wgs > max_wi_dims[0] ? max_wi_dims[0] : wgs;
397+
389398
while (loc_mem < wgs * 2 * sizeof(cl_int)) wgs -= psm;
390399

391400
if (wgs == 0)

samples/core/reduce/main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,14 @@ int main(int argc, char* argv[])
175175
cl::KernelFunctor<cl::Buffer, cl::Buffer, cl::LocalSpaceArg,
176176
cl_ulong, cl_int>(program, "reduce");
177177

178-
// Query maximum supported WGS of kernel on device based on private mem
179-
// (register) constraints
180-
auto wgs =
178+
// Query kernel- and device-specific limits
179+
size_t kernel_wgs =
181180
reduce.getKernel().getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE>(
182181
device);
182+
size_t device_wi_x = device.getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>()[0];
183+
184+
// Clamp work-group size to both limits
185+
size_t wgs = std::min(kernel_wgs, device_wi_x);
183186

184187
// Further constrain (reduce) WGS based on shared mem size on device
185188
while (device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>()
@@ -191,7 +194,7 @@ int main(int argc, char* argv[])
191194

192195
if (wgs == 0)
193196
throw std::runtime_error{
194-
"Not enough local memory to serve a single sub-group."
197+
"Not enough local memory to serve a single work-group."
195198
};
196199

197200
cl_ulong factor = wgs * 2;

0 commit comments

Comments
 (0)