Thomas Wagenaar
Thomas Wagenaar

Reputation: 6759

How to force CPU usage of CuPy?

This is a bit of a weird question, as CuPy is meant for GPU. However, depending on the input of my program, I actually want to use the CPU as it is faster. I have already tried

if DISABLE_GPU:
  import os
  os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

although it blocks access to the GPU, this causes CuPy to throw an error:

cupy_backends.cuda.api.runtime.CUDARuntimeError: cudaErrorNoDevice: no CUDA-capable device is detected

is there any way to force CuPy to use the CPU instead of the GPU? Or should I use some trick where I say that cp=np for instance...

Upvotes: 1

Views: 2061

Answers (2)

bio
bio

Reputation: 511

You could try writing agnostic code.

For example:

import cupy as cp
import numpy as np
from cupyx.profiler import benchmark

def softplus(x):
    xp = cp.get_array_module(x)
    return xp.sum(x)

x = np.random.random(int(1e5))
x_gpu = cp.asarray(x)

cpu_bench = benchmark(softplus, (x,), n_repeat=10)
gpu_bench = benchmark(softplus, (x_gpu,), n_repeat=10)

print(cpu_bench)
print(gpu_bench)

This snippet will use numpy when you pass x array, cupy when you pass x_gpu.

Upvotes: 0

linkhyrule5
linkhyrule5

Reputation: 918

CuPy's API is such that any time you use cp, you're implicitly working with device memory. So your best bet is to write your code so that it conditionally uses np instead of cp if you want it to run on the CPU.

Upvotes: 1

Related Questions