在 Vitis 核开发套件中,有时器件包含单一内核或不同内核的多个内核实例。虽然 OpenCL API
clCreateSubDevices
允许主机代码将器件分割为多个子器件,但 Vitis 核开发套件仅支持均等分割的子器件(使用 CL_DEVICE_PARTITION_EQUALLY
),且每个子器件包含一个内核实例。
以下示例显示:
- 按均等大小分区创建的子器件,每个子器件执行一个内核实例。
- 基于子器件列表迭代,使用独立的关联环境和命令队列在每个子器件上执行内核。
- 简单起见,其中并未显示与内核执行(和对应的相关缓冲器)代码相关的 API,但在
run_cu
函数中将对此类 API 进行描述。
cl_uint num_devices = 0;
cl_device_partition_property props[3] = {CL_DEVICE_PARTITION_EQUALLY,1,0};
// Get the number of sub-devices
clCreateSubDevices(device,props,0,nullptr,&num_devices);
// Container to hold the sub-devices
std::vector<cl_device_id> devices(num_devices);
// Second call of clCreateSubDevices
// We get sub-device handles in devices.data()
clCreateSubDevices(device,props,num_devices,devices.data(),nullptr);
// Iterating over sub-devices
std::for_each(devices.begin(),devices.end(),[kernel](cl_device_id sdev) {
// Context for sub-device
auto context = clCreateContext(0,1,&sdev,nullptr,nullptr,&err);
// Command-queue for sub-device
auto queue = clCreateCommandQueue(context,sdev,
CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE,&err);
// Execute the kernel on the sub-device using local context and
queue run_cu(context,queue,kernel); // Function not shown
});
重要: 如以上示例所示,您必须为每个子器件创建单独的关联环境。虽然 OpenCL 支持可包含多个器件和子器件的关联环境,但 XRT 要求每个器件和子器件都具有独立的关联环境。