Fortran OpenACC fallback to OpenMP if there is no GPU


I have annotated a Fortran program with OpenACC directives and things run nicely on the GPU. I have also created a second version of the program where I have annotated the same spots in the program with OpenMP directives and things run nicely in parallel on multi-core CPUs.

Is it possible to create a single version of the source code, annotated with both types of directives, so that the code runs in parallel on the CPU when no GPU is detected?

Thank you in advance for your help!

Why not use Fortran preprocessor (FPP) to generate a single set of codes with both directives, and two final sets of executable one for CPU and the other for CPU (using FPP conditional compilation), then have a higher-level program or script (either in Fortran, Python, MATLAB, Bash, …) to call either of the two based on the available architecture.

Try using just OpenACC for both the GPU offload and when targeting multicore CPUs by using the flag “-ta=tesla,multicore”. By default if a supported GPU is available, then the code will be offloaded. Otherwise, the multicore CPU is used.

You can also toggle this via the environment variable “ACC_DEVICE_TYPE=acc_device_host” if you want to run on the CPU even if a GPU is present.

Thank you! This worked, although for nvfortran the option I used was -acc=gpu,multicore