I am trying to get cudaGetDevice to work on the device. Here is my program:
#include <stdio.h>
global void hello (void)
{
int adevice;
cudaError_t code = cudaGetDevice (&adevice);
if (code == cudaSuccess)
printf (“device: cudaGetDevice succeeded: code %08x\n”, code);
else
printf (“device: cudaGetDevice failed: code %08x\n”, code);
printf (“Hello from device %d\n”, adevice);
// const char *astring = cudaGetErrorString (code);
// printf (“device: cudaGetDevice returned with code "%s"\n”, astring);
}
int main (void)
{
hello<<<1,1>>> ();
cudaError_t code = cudaPeekAtLastError ();
printf (“host: kernel returned with code "%s"\n”,
cudaGetErrorString (code));
code = cudaDeviceSynchronize();
printf (“host: cudaDeviceSynchronize returned with code "%s"\n”,
cudaGetErrorString (code));
printf (“Hello from host\n”);
return 0;
}
Here is the output:
host: kernel returned with code “no error”
device: cudaGetDevice failed: code 0000001e
Hello from device 0
host: cudaDeviceSynchronize returned with code “no error”
Hello from host
Note that cudaGetDevice did not return cudaSuccess (00000000)
but rather returned a code of 1e, which is not a valid cudaError_t.
(according to the cuda.h file). Despite this, the kernel returns
properly, and the hello routine thinks that it is on device number 0
(irrespective of whether this is the case of not).
If I uncomment the last two lines of void hello (void), the output is
host: kernel returned with code “unknown error”
host: cudaDeviceSynchronize returned with code “unknown error”
Hello from host
In other words, cudaGetErrorString (code) is failing, and terminating
the kernel, because code has an invalid value (i.e. 0000001e ).
Why is cudeGetDevice failing and returning an invalid error code?