'Intel oneapi detect if I'm on FPGA

Is there any way to detect via CMake whether an FPGA accelerator is available or not ?

I'd like to do something like

if (FPGA_AVAILABLE or FPGA_EMULATOR_ON)
   # set stuff here
   add_subdirectory(fpga_src)
endif()

Is there any way to do this ? I had a look at Intel OneAPI examples but they don't this as far as I understand and they kind of assume they run on the right platform.



Solution 1:[1]

As @aland mentioned when compiling a program via CMake and most other compilers there is no assumption that the platform you are on is necessarily the target platform.

A typical workflow would have that pre-defined script run in CMake that does the detection and then sends a preprocessor flag using the -D flag to the DPC++ compiler. Since you said you looked at the oneAPI samples I'm guessing you saw this, which is our simple example of using the -D flag which assumes that the customer manually passes in the flag

https://github.com/oneapi-src/oneAPI-samples/tree/master/DirectProgramming/DPC%2B%2BFPGA/Tutorials/GettingStarted/fpga_compile

Here's the output of the sycl-ls function that was previously mentioned:

$ sycl-ls
[opencl:acc:0] Intel® FPGA Emulation Platform for OpenCL™, Intel® FPGA Emulation Device 1.2 [2021.12.9.0.24_005321]

Putting that into CMake directly would involve using the CMake execute_process function, assigning it to a variable and then building your CMake logic around it. It would look something like this:

execute_process(COMMAND sycl-ls OUTPUT_VARIABLE FOO)

reference for that command here: https://cmake.org/cmake/help/latest/command/execute_process.html

and if you really want to get fancy you can leverage something like this type of structure to trim the text of the output:

execute_process( COMMAND bash "-c" "echo -n hello | sed 's/hello/world/;'" OUTPUT_VARIABLE FOO )

credit for that here and so you can dive in more: CMake's execute_process and arbitrary shell scripts

You'd probably also want to guard in case the sycl-ls command doesn't exist as well just to be safe. Good luck and happy coding!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TonyM-Intel