11 #include <CL/cl_ext_xilinx.h>
21 std::vector<cl::Platform> platforms;
25 for (
const cl::Platform &
platform : platforms) {
29 std::string platform_name;
31 if (platform_name !=
"Xilinx") {
37 std::vector<cl::Device> devices;
40 ATH_MSG_DEBUG(
"Found XRT/OpenCL platform '" << platform_name <<
"' with "
48 for (
const cl::Device &device : devices) {
54 std::string list_device_name;
55 ATH_CHECK(
list[0].getInfo(CL_DEVICE_NAME, &list_device_name) ==
57 if (device_name == list_device_name) {
59 list.push_back(device);
64 std::vector<cl::Device> new_list = {device};
76 return StatusCode::FAILURE;
83 return StatusCode::SUCCESS;
96 return StatusCode::FAILURE;
103 "More XCLBIN file(s) specified than "
104 "devices type available ("
109 return StatusCode::FAILURE;
116 ATH_MSG_ERROR(
"XCLBIN file does not exist: " << xclbin_path);
117 return StatusCode::FAILURE;
125 xrt::xclbin xrt_xclbin(xclbin_path);
126 xclbin_info.
path = xclbin_path;
127 xclbin_info.
xsa_name = xrt_xclbin.get_xsa_name();
129 xclbin_info.
uuid = xrt_xclbin.get_uuid().to_string();
130 for (
const xrt::xclbin::kernel &kernel : xrt_xclbin.get_kernels()) {
135 if (!kernel.get_cus().empty()) {
140 ATH_MSG_ERROR(
"Could not create xrt::xclbin from " << xclbin_path);
141 return StatusCode::FAILURE;
149 std::set<std::string> uuids;
150 std::set<std::string> fpga_device_names;
152 uuids.insert(
info.uuid);
153 fpga_device_names.insert(
info.fpga_device_name);
158 return StatusCode::SUCCESS;
167 cl_int
err = CL_SUCCESS;
168 std::string device_name;
169 err = device.getInfo(CL_DEVICE_NAME, &device_name);
170 if (
err != CL_SUCCESS) {
172 return std::string(
"error");
182 cl_int
err = CL_SUCCESS;
183 std::string device_bdf;
184 err = device.getInfo(CL_DEVICE_PCIE_BDF, &device_bdf);
185 if (
err != CL_SUCCESS) {
187 return std::string(
"error");
198 static std::string getPrefixUpToNthOccurrence(
const std::string &
str,
205 while (
count <
n &&
pos != std::string::npos) {
211 if (
pos == std::string::npos) {
216 return str.substr(0,
pos + 1);
229 const cl::Device &device)
const {
231 const std::string device_prefix =
233 const std::string xsa_prefix =
234 getPrefixUpToNthOccurrence(xclbin_info.
xsa_name,
'_', 2);
235 if (device_prefix == xsa_prefix) {
272 ATH_MSG_DEBUG(
"Case 1: One or multiple identical device(s), one xclbin");
275 ath_cl_context.
devices.push_back(device);
287 "Specified XCLBINs target multiple device types, but only one "
288 "device type is present");
289 return StatusCode::FAILURE;
298 "Case 2: Multiple identical devices, multiple identical xclbins");
313 "Case 3: Multiple identical devices, multiple different XCLBIN "
314 "files, but targeting the same device type");
329 ATH_MSG_DEBUG(
"Case 4: Multiple different devices, multiple xclbins");
331 for (
const std::vector<cl::Device> &device_type : si.
device_types) {
332 for (
const cl::Device &device : device_type) {
334 ath_cl_context.
devices.push_back(device);
339 for (
iter = unaffected_xclbin_infos.begin();
340 iter != unaffected_xclbin_infos.end();) {
343 iter = unaffected_xclbin_infos.erase(
iter);
364 for (
const XclbinInfo &xclbin_info : unaffected_xclbin_infos) {
368 "No compatible device found for XCLBIN: " << xclbin_info.path);
372 return StatusCode::SUCCESS;
384 cl_int
err = CL_SUCCESS;
389 ath_cl_context.context = std::make_shared<cl::Context>(
390 ath_cl_context.devices,
nullptr,
nullptr,
nullptr, &
err);
391 if (
err != CL_SUCCESS) {
393 return StatusCode::FAILURE;
399 file.open(ath_cl_context.xclbin_info.path.c_str(), std::ios::binary);
401 ATH_MSG_ERROR(
"Could not open " << ath_cl_context.xclbin_info.path
403 return StatusCode::FAILURE;
405 std::vector<char> xclbin_buffer((std::istreambuf_iterator<char>(
file)),
406 std::istreambuf_iterator<char>());
411 cl::Program::Binaries binary;
412 for (std::size_t
i = 0;
i < ath_cl_context.devices.size(); ++
i) {
413 binary.push_back({xclbin_buffer.data(), xclbin_buffer.size()});
418 ath_cl_context.program = std::make_shared<cl::Program>(
419 *ath_cl_context.context, ath_cl_context.devices, binary,
nullptr, &
err);
420 if (
err != CL_SUCCESS) {
422 return StatusCode::FAILURE;
426 std::string bdfs =
"";
427 for (
const cl::Device &device : ath_cl_context.devices) {
431 ATH_MSG_INFO(
"Loaded " << ath_cl_context.xclbin_info.path <<
" on "
432 << ath_cl_context.devices.size() <<
" "
434 <<
" device(s): " << bdfs);
437 return StatusCode::SUCCESS;
468 return StatusCode::SUCCESS;
477 return StatusCode::SUCCESS;
480 const std::vector<std::shared_ptr<xrt::device>>
483 std::vector<std::shared_ptr<xrt::device>> devices;
488 if (
std::find(ath_cl_context.xclbin_info.kernel_names.begin(),
489 ath_cl_context.xclbin_info.kernel_names.end(),
490 name) != ath_cl_context.xclbin_info.kernel_names.end()) {
491 for (
const cl::Device &device : ath_cl_context.devices) {
492 devices.push_back(std::make_shared<xrt::device>(
493 xrt::opencl::get_xrt_device(device())));
501 const std::vector<IDeviceMgmtSvc::OpenCLHandle>
503 const std::string &
name)
const {
505 std::vector<IDeviceMgmtSvc::OpenCLHandle> handles;
510 if (
std::find(ath_cl_context.xclbin_info.kernel_names.begin(),
511 ath_cl_context.xclbin_info.kernel_names.end(),
512 name) != ath_cl_context.xclbin_info.kernel_names.end()) {
514 ath_cl_context.program};
515 handles.push_back(handle);