|
| 1 | +//===------------------- enqueue_kernel.cpp ---------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// SYCL error handling of enqueue kernel operations |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "error_handling.hpp" |
| 14 | + |
| 15 | +#include <CL/sycl/detail/pi.hpp> |
| 16 | + |
| 17 | +namespace cl { |
| 18 | +namespace sycl { |
| 19 | +namespace detail { |
| 20 | + |
| 21 | +namespace enqueue_kernel_launch { |
| 22 | + |
| 23 | +bool handleInvalidWorkGroupSize(pi_device Device, pi_kernel Kernel, |
| 24 | + const NDRDescT &NDRDesc) { |
| 25 | + const bool HasLocalSize = (NDRDesc.LocalSize[0] != 0); |
| 26 | + |
| 27 | + size_t VerSize = 0; |
| 28 | + PI_CALL(piDeviceGetInfo)(Device, PI_DEVICE_VERSION, 0, nullptr, &VerSize); |
| 29 | + assert(VerSize >= 10 && |
| 30 | + "Unexpected device version string"); // strlen("OpenCL X.Y") |
| 31 | + string_class VerStr(VerSize, '\0'); |
| 32 | + PI_CALL(piDeviceGetInfo)(Device, PI_DEVICE_VERSION, VerSize, &VerStr.front(), |
| 33 | + nullptr); |
| 34 | + const char *Ver = &VerStr[7]; // strlen("OpenCL ") |
| 35 | + |
| 36 | + size_t CompileWGSize[3] = {0}; |
| 37 | + PI_CALL(piKernelGetGroupInfo)(Kernel, Device, |
| 38 | + CL_KERNEL_COMPILE_WORK_GROUP_SIZE, |
| 39 | + sizeof(size_t) * 3, CompileWGSize, nullptr); |
| 40 | + |
| 41 | + if (CompileWGSize[0] != 0) { |
| 42 | + // OpenCL 1.x && 2.0: |
| 43 | + // CL_INVALID_WORK_GROUP_SIZE if local_work_size is NULL and the |
| 44 | + // reqd_work_group_size attribute is used to declare the work-group size |
| 45 | + // for kernel in the program source. |
| 46 | + if (!HasLocalSize && (Ver[0] == '1' || (Ver[0] == '2' && Ver[2] == '0'))) |
| 47 | + throw sycl::nd_range_error( |
| 48 | + "OpenCL 1.x and 2.0 requires to pass local size argument even if " |
| 49 | + "required work-group size was specified in the program source", |
| 50 | + PI_INVALID_WORK_GROUP_SIZE); |
| 51 | + |
| 52 | + // Any OpenCL version: |
| 53 | + // CL_INVALID_WORK_GROUP_SIZE if local_work_size is specified and does not |
| 54 | + // match the required work-group size for kernel in the program source. |
| 55 | + if (NDRDesc.LocalSize[0] != CompileWGSize[0] || |
| 56 | + NDRDesc.LocalSize[1] != CompileWGSize[1] || |
| 57 | + NDRDesc.LocalSize[2] != CompileWGSize[2]) |
| 58 | + throw sycl::nd_range_error( |
| 59 | + "Specified local size doesn't match the required work-group size " |
| 60 | + "specified in the program source", |
| 61 | + PI_INVALID_WORK_GROUP_SIZE); |
| 62 | + } |
| 63 | + |
| 64 | + if (Ver[0] == '1') { |
| 65 | + // OpenCL 1.x: |
| 66 | + // CL_INVALID_WORK_GROUP_SIZE if local_work_size is specified and the |
| 67 | + // total number of work-items in the work-group computed as |
| 68 | + // local_work_size[0] * ... * local_work_size[work_dim – 1] is greater |
| 69 | + // than the value specified by CL_DEVICE_MAX_WORK_GROUP_SIZE in |
| 70 | + // table 4.3 |
| 71 | + size_t MaxWGSize = 0; |
| 72 | + PI_CALL(piDeviceGetInfo)(Device, PI_DEVICE_MAX_WORK_GROUP_SIZE, |
| 73 | + sizeof(size_t), &MaxWGSize, nullptr); |
| 74 | + const size_t TotalNumberOfWIs = |
| 75 | + NDRDesc.LocalSize[0] * NDRDesc.LocalSize[1] * NDRDesc.LocalSize[2]; |
| 76 | + if (TotalNumberOfWIs > MaxWGSize) |
| 77 | + throw sycl::nd_range_error( |
| 78 | + "Total number of work-items in a work-group cannot exceed " |
| 79 | + "info::device::max_work_group_size which is equal to " + |
| 80 | + std::to_string(MaxWGSize), |
| 81 | + PI_INVALID_WORK_GROUP_SIZE); |
| 82 | + } else { |
| 83 | + // OpenCL 2.x: |
| 84 | + // CL_INVALID_WORK_GROUP_SIZE if local_work_size is specified and the |
| 85 | + // total number of work-items in the work-group computed as |
| 86 | + // local_work_size[0] * ... * local_work_size[work_dim – 1] is greater |
| 87 | + // than the value specified by CL_KERNEL_WORK_GROUP_SIZE in table 5.21. |
| 88 | + size_t KernelWGSize = 0; |
| 89 | + PI_CALL(piKernelGetGroupInfo)(Kernel, Device, CL_KERNEL_WORK_GROUP_SIZE, |
| 90 | + sizeof(size_t), &KernelWGSize, nullptr); |
| 91 | + const size_t TotalNumberOfWIs = |
| 92 | + NDRDesc.LocalSize[0] * NDRDesc.LocalSize[1] * NDRDesc.LocalSize[2]; |
| 93 | + if (TotalNumberOfWIs > KernelWGSize) |
| 94 | + throw sycl::nd_range_error( |
| 95 | + "Total number of work-items in a work-group cannot exceed " |
| 96 | + "info::kernel_work_group::work_group_size which is equal to " + |
| 97 | + std::to_string(KernelWGSize) + " for this kernel", |
| 98 | + PI_INVALID_WORK_GROUP_SIZE); |
| 99 | + } |
| 100 | + |
| 101 | + if (HasLocalSize) { |
| 102 | + const bool NonUniformWGs = |
| 103 | + (NDRDesc.LocalSize[0] != 0 && |
| 104 | + NDRDesc.GlobalSize[0] % NDRDesc.LocalSize[0] != 0) || |
| 105 | + (NDRDesc.LocalSize[1] != 0 && |
| 106 | + NDRDesc.GlobalSize[1] % NDRDesc.LocalSize[1] != 0) || |
| 107 | + (NDRDesc.LocalSize[2] != 0 && |
| 108 | + NDRDesc.GlobalSize[2] % NDRDesc.LocalSize[2] != 0); |
| 109 | + |
| 110 | + if (Ver[0] == '1') { |
| 111 | + // OpenCL 1.x: |
| 112 | + // CL_INVALID_WORK_GROUP_SIZE if local_work_size is specified and |
| 113 | + // number of workitems specified by global_work_size is not evenly |
| 114 | + // divisible by size of work-group given by local_work_size |
| 115 | + |
| 116 | + if (NonUniformWGs) |
| 117 | + throw sycl::nd_range_error( |
| 118 | + "Non-uniform work-groups are not supported by the target device", |
| 119 | + PI_INVALID_WORK_GROUP_SIZE); |
| 120 | + } else { |
| 121 | + // OpenCL 2.x: |
| 122 | + // CL_INVALID_WORK_GROUP_SIZE if the program was compiled with |
| 123 | + // –cl-uniform-work-group-size and the number of work-items specified |
| 124 | + // by global_work_size is not evenly divisible by size of work-group |
| 125 | + // given by local_work_size |
| 126 | + |
| 127 | + pi_program Program = nullptr; |
| 128 | + PI_CALL(piKernelGetInfo)(Kernel, CL_KERNEL_PROGRAM, sizeof(pi_program), |
| 129 | + &Program, nullptr); |
| 130 | + size_t OptsSize = 0; |
| 131 | + PI_CALL(piProgramGetBuildInfo)(Program, Device, CL_PROGRAM_BUILD_OPTIONS, |
| 132 | + 0, nullptr, &OptsSize); |
| 133 | + string_class Opts(OptsSize, '\0'); |
| 134 | + PI_CALL(piProgramGetBuildInfo)(Program, Device, CL_PROGRAM_BUILD_OPTIONS, |
| 135 | + OptsSize, &Opts.front(), nullptr); |
| 136 | + if (NonUniformWGs) { |
| 137 | + const bool HasStd20 = Opts.find("-cl-std=CL2.0") != string_class::npos; |
| 138 | + if (!HasStd20) |
| 139 | + throw sycl::nd_range_error( |
| 140 | + "Non-uniform work-groups are not allowed by default. Underlying " |
| 141 | + "OpenCL 2.x implementation supports this feature and to enable " |
| 142 | + "it, build device program with -cl-std=CL2.0", |
| 143 | + PI_INVALID_WORK_GROUP_SIZE); |
| 144 | + else |
| 145 | + throw sycl::nd_range_error( |
| 146 | + "Non-uniform work-groups are not allowed by default. Underlying " |
| 147 | + "OpenCL 2.x implementation supports this feature, but it is " |
| 148 | + "disabled by -cl-uniform-work-group-size build flag", |
| 149 | + PI_INVALID_WORK_GROUP_SIZE); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // TODO: required number of sub-groups, OpenCL 2.1: |
| 155 | + // CL_INVALID_WORK_GROUP_SIZE if local_work_size is specified and is not |
| 156 | + // consistent with the required number of sub-groups for kernel in the |
| 157 | + // program source. |
| 158 | + |
| 159 | + // Fallback |
| 160 | + constexpr pi_result Error = PI_INVALID_WORK_GROUP_SIZE; |
| 161 | + throw runtime_error( |
| 162 | + "OpenCL API failed. OpenCL API returns: " + codeToString(Error), Error); |
| 163 | +} |
| 164 | + |
| 165 | +bool handleError(pi_result Error, pi_device Device, pi_kernel Kernel, |
| 166 | + const NDRDescT &NDRDesc) { |
| 167 | + assert(Error != PI_SUCCESS && |
| 168 | + "Success is expected to be handled on caller side"); |
| 169 | + switch (Error) { |
| 170 | + case PI_INVALID_WORK_GROUP_SIZE: |
| 171 | + return handleInvalidWorkGroupSize(Device, Kernel, NDRDesc); |
| 172 | + // TODO: Handle other error codes |
| 173 | + default: |
| 174 | + throw runtime_error( |
| 175 | + "OpenCL API failed. OpenCL API returns: " + codeToString(Error), Error); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace enqueue_kernel_launch |
| 180 | + |
| 181 | +} // namespace detail |
| 182 | +} // namespace sycl |
| 183 | +} // namespace cl |
0 commit comments