0

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.

Elle
  • 305
  • 2
  • 10
  • 2
    CMake is run when your program gets compiled, which is not necessarily taking place on the same hardware your program will be run on. So it's a bit unusual to detect the presence of accelerators at CMake time. It might make more sense to ask the user whether they want to compile with FPGA support or not. If you really want to run the hardware detection in CMake, the easiest way, IMHO, is parsing `sycl-ls` results. You can also use CMake scripting to compile and execute your own hardware detection program, and use its output to set CMake flags. – aland May 06 '22 at 14:56

1 Answers1

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!

TonyM
  • 366
  • 1
  • 4