2

I'm writing code on a GPU enabled machine, but my code needs to be portable to computers without a GPU. So I wrote 2 functions, one that uses only the CPU and one that uses CPU+GPU.

I'm for a conditional compliance code, for example:

if (COMPUTER_HAS_GPU)
    //Run CPU+GPU code  
else  
    //Run CPU only code

Is there anything like this?

3
  • 2
    Your example suggests you don't want to do this at runtime, but at compile time. The easiest would be to add a define and let the user specify what they want. Commented Jan 27, 2012 at 15:14
  • You're right, my example did suggest runtime, but now I changed it to compile time. Commented Jan 27, 2012 at 15:21
  • 1
    @user10007692 I assume you meant your example suggested compile time, but you changed it to runtime. Commented Jan 27, 2012 at 15:23

2 Answers 2

6

You can use cudaGetDeviceCount()

For Example:

int devices = 0; 

cudaError_t err = cudaGetDeviceCount(&devices); 

if (devices > 0 && err == cudaSuccess) 
{ 
    // Run CPU+GPU code
} 
else
{ 
    // Run CPU only code
} 
Sign up to request clarification or add additional context in comments.

Comments

2

You can try to use the cudaGetDeviceCount(*int); function, that gives you how many cuda devices do you have and you can check the error code.

int i;

cudaError_t e = cudaGetDeviceCount(&i);

if (e == cudaErrorNoDevice) {
 // No CUDA device :-(
} else {
 // CUDA device .o/
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.