Turn on CUDASIM only if no cuda

In my test suite, I would like to turn on the cuda simulator if there is no cuda available on the machine. However, if I load the cuda package to check cuda.is_available(), it is already too late to set NUMBA_ENABLE_CUDASIM in the environment. Is there another way to turn on the simulator?

Hi @shaunc

off the top of my head, I only have a somewhat hacky idea.
You could spawn a new python interpreter (e.g. through subprocess.run) and do an import of numba.cuda there to avoid side effects in your main session.

The following little snippted should exit with code 0 (OK) if cuda is available and with exit code 1(NOT OK) otherwise.

python -c "import sys; import numba.cuda; sys.exit(not numba.cuda.is_available())"
1 Like

That is exactly what I’m doing – in conftest.py I have:

subprocess.check_output(
        [
            "python",
            "-c",
            "from numba import cuda; print(cuda.is_available())",
        ]
    ).strip() == "True"

:slight_smile: … but indeed a hack… presumably under the hood there must be a better way?

Haha, great minds think alike :stuck_out_tongue:

there must be a better way

The Raymond Hettinger is strong within this one :slight_smile:

At the end of the day you must check something. Be it the hardware specs, or look for installed cuda libraries, so if this works and is simple, why not.

I’d also think that making use of numba to determine if CUDA works, has the advantage of letting numba make that decision based on additional logic and information (potentially). So if CUDA is available in principle, but numba did not figure that out, it’s not gonna try and fail.

Hi @shaunc,

I’m not sure there’s a way around this at present. Testing via:

from numba.cuda.device_init import is_available
print(is_available)

would let you know if the hardware is working, but the issue with that is that it will call __init__ in numba.cuda which in turn reads the numba.config to determine whether to import from the simulator or not.

Ping @gmarkall any ideas?

This seems to be the numba interal function that is searching for the CUDA libraries:
If you want to avoid calling a subprocess maybe you can immitate the behaviour here to simulate whether numba will be able to use CUDA or whether you need the simulator (there might be one or two other things to check besides the existence of the libraries to make sure you are in line with numba’s behaviour (Look for CudaSupportError))

Would it be useful for numba to provide a way of determining CUDA abilities without triggering the import side effects currently in the way of that?

1 Like

Would it be useful for numba to provide a way of determining CUDA abilities without triggering the import side effects currently in the way of that?

I, for one, would welcome this! It would be even nicer to be able to turn on or enable the CUDA simulator from inside Python

1 Like