How to switch between function definitions for using CUDA.jl

Very similar to the last approach you suggested, is to use Val:

function f(useGPU::Val{false}, args...)
    # CPU-specific implementation
    ...
end

function f(useGPU::Val{true}, args...)
    # GPU-specific implementation
    ...
end

With

const useGPU = Val(true)  # or Val(false)
f(useGPU, ...)

you would then call the (compiled) CPU/GPU version, depending on the flag value.

3 Likes