20 std::vector<cl::Platform> platforms;
24 for (
const cl::Platform &
platform : platforms) {
28 std::string platform_name;
30 if (platform_name !=
"Xilinx") {
36 std::vector<cl::Device> devices;
39 ATH_MSG_DEBUG(
"Found XRT/OpenCL platform '" << platform_name <<
"' with "
47 for (
const cl::Device &device : devices) {
53 std::string list_device_name;
54 ATH_CHECK(
list[0].getInfo(CL_DEVICE_NAME, &list_device_name) ==
56 if (device_name == list_device_name) {
58 list.push_back(device);
63 std::vector<cl::Device> new_list = {device};
75 return StatusCode::FAILURE;
82 return StatusCode::SUCCESS;
95 return StatusCode::FAILURE;
102 "More XCLBIN file(s) specified than "
103 "devices type available ("
108 return StatusCode::FAILURE;
115 ATH_MSG_ERROR(
"XCLBIN file does not exist: " << xclbin_path);
116 return StatusCode::FAILURE;
124 xrt::xclbin xrt_xclbin(xclbin_path);
125 xclbin_info.
path = xclbin_path;
126 xclbin_info.
xsa_name = xrt_xclbin.get_xsa_name();
128 xclbin_info.
uuid = xrt_xclbin.get_uuid().to_string();
129 for (
const xrt::xclbin::kernel &kernel : xrt_xclbin.get_kernels()) {
134 if (!kernel.get_cus().empty()) {
139 ATH_MSG_ERROR(
"Could not create xrt::xclbin from " << xclbin_path);
140 return StatusCode::FAILURE;
148 std::set<std::string> uuids;
149 std::set<std::string> fpga_device_names;
151 uuids.insert(
info.uuid);
152 fpga_device_names.insert(
info.fpga_device_name);
157 return StatusCode::SUCCESS;
166 cl_int
err = CL_SUCCESS;
167 std::string device_name;
168 err = device.getInfo(CL_DEVICE_NAME, &device_name);
169 if (
err != CL_SUCCESS) {
171 return std::string(
"error");
181 cl_int
err = CL_SUCCESS;
182 std::string device_bdf;
183 err = device.getInfo(CL_DEVICE_PCIE_BDF, &device_bdf);
184 if (
err != CL_SUCCESS) {
186 return std::string(
"error");
197 static std::string getPrefixUpToNthOccurrence(
const std::string &
str,
204 while (
count <
n &&
pos != std::string::npos) {
210 if (
pos == std::string::npos) {
215 return str.substr(0,
pos + 1);
228 const cl::Device &device)
const {
230 const std::string device_prefix =
232 const std::string xsa_prefix =
233 getPrefixUpToNthOccurrence(xclbin_info.
xsa_name,
'_', 2);
234 if (device_prefix == xsa_prefix) {
271 ATH_MSG_DEBUG(
"Case 1: One or multiple identical device(s), one xclbin");
274 ath_cl_context.
devices.push_back(device);
286 "Specified XCLBINs target multiple device types, but only one "
287 "device type is present");
288 return StatusCode::FAILURE;
297 "Case 2: Multiple identical devices, multiple identical xclbins");
312 "Case 3: Multiple identical devices, multiple different XCLBIN "
313 "files, but targeting the same device type");
328 ATH_MSG_DEBUG(
"Case 4: Multiple different devices, multiple xclbins");
330 for (
const std::vector<cl::Device> &device_type : si.
device_types) {
331 for (
const cl::Device &device : device_type) {
333 ath_cl_context.
devices.push_back(device);
338 for (iter = unaffected_xclbin_infos.begin();
339 iter != unaffected_xclbin_infos.end();) {
342 iter = unaffected_xclbin_infos.erase(iter);
363 for (
const XclbinInfo &xclbin_info : unaffected_xclbin_infos) {
367 "No compatible device found for XCLBIN: " << xclbin_info.path);
371 return StatusCode::SUCCESS;
383 cl_int
err = CL_SUCCESS;
388 ath_cl_context.context = std::make_shared<cl::Context>(
389 ath_cl_context.devices,
nullptr,
nullptr,
nullptr, &
err);
390 if (
err != CL_SUCCESS) {
392 return StatusCode::FAILURE;
398 file.open(ath_cl_context.xclbin_info.path.c_str(), std::ios::binary);
400 ATH_MSG_ERROR(
"Could not open " << ath_cl_context.xclbin_info.path
402 return StatusCode::FAILURE;
404 std::vector<char> xclbin_buffer((std::istreambuf_iterator<char>(
file)),
405 std::istreambuf_iterator<char>());
410 cl::Program::Binaries binary;
411 for (std::size_t
i = 0;
i < ath_cl_context.devices.size(); ++
i) {
412 binary.push_back({xclbin_buffer.data(), xclbin_buffer.size()});
417 ath_cl_context.program = std::make_shared<cl::Program>(
418 *ath_cl_context.context, ath_cl_context.devices, binary,
nullptr, &
err);
419 if (
err != CL_SUCCESS) {
421 return StatusCode::FAILURE;
425 std::string bdfs =
"";
426 for (
const cl::Device &device : ath_cl_context.devices) {
430 ATH_MSG_INFO(
"Loaded " << ath_cl_context.xclbin_info.path <<
" on "
431 << ath_cl_context.devices.size() <<
" "
433 <<
" device(s): " << bdfs);
436 return StatusCode::SUCCESS;
467 return StatusCode::SUCCESS;
476 return StatusCode::SUCCESS;
479 const std::vector<std::shared_ptr<xrt::device>>
482 std::vector<std::shared_ptr<xrt::device>> devices;
487 if (
std::find(ath_cl_context.xclbin_info.kernel_names.begin(),
488 ath_cl_context.xclbin_info.kernel_names.end(),
489 name) != ath_cl_context.xclbin_info.kernel_names.end()) {
490 for (
const cl::Device &device : ath_cl_context.devices) {
491 devices.push_back(std::make_shared<xrt::device>(
492 xrt::opencl::get_xrt_device(device())));
500 const std::vector<IDeviceMgmtSvc::OpenCLHandle>
502 const std::string &
name)
const {
504 std::vector<IDeviceMgmtSvc::OpenCLHandle> handles;
509 if (
std::find(ath_cl_context.xclbin_info.kernel_names.begin(),
510 ath_cl_context.xclbin_info.kernel_names.end(),
511 name) != ath_cl_context.xclbin_info.kernel_names.end()) {
513 ath_cl_context.program};
514 handles.push_back(handle);