I have met very strange errors when i move my sample code from RHEL5.3 to the mac os.
Basically, I am trying to initialize the devices and open one of them via CUDA driver API. the code works well and returns correct information in RHEL5.3, but on the Mac machine, the cuInit returns weird device ID and then can not open device. Finally I get the Bus Error notification.
#include <stdio.h>
#include <stdlib.h>
#if defined(__GNUC__)
#include <stdint.h>
#endif
#include <cuda.h>
#include <cutil.h>
#include <cuda_runtime_api.h>
#include "c_DevObjectGlobals.h"
extern "C"
{
CUcontext cuContext;
void cgpu_init()
{
CUresult cuStatus=cuInit(0);
if (cuStatus==CUDA_SUCCESS)
printf("cuStatus success\n");
else if (cuStatus==CUDA_ERROR_INVALID_VALUE)
printf("invalid value\n");
else if (cuStatus==CUDA_ERROR_INVALID_DEVICE)
printf("invalid device\n");
else
printf("fails\n");
int count; cuDeviceGetCount(&count);
printf("Devices on system: %d\n",count);
}
void cgpu_open(int deviceID, int *gPtr)
{
CUdevice cuDevice;
cuDeviceGet(&cuDevice,deviceID);
printf("cuda get device %d\n", deviceID);
char name[100];
cuDeviceGetName(name,100,cuDevice);
*gPtr = (uintptr_t)cuDevice;
printf("Using device %s with deviceID = %i, and handle = %i\n",name, deviceID,*gPtr);
CUresult status = cuCtxCreate(&cuContext, 0, cuDevice);
}
}
main(){
cgpu_init();
printf("---------------------------\n");
int *gPtr0;
//int *gPtr1;
cgpu_open(0, gPtr0);
//cgpu_open(1, gPtr0);
printf("++++++++++++++++++++++++++++++\n");
}
my compiling and linking command is :
g++ -c -I./ -I/usr/local/cuda/include devQ.cpp
g++ -o myDev -L/usr/local/cuda/lib -lcuda devQ.o
The retuning message on the Mac is
$ ./myDev
fails
Devices on system: -1881144128
---------------------------
Bus error
btw, my RHEL5.3 is 64 bits. And the Mac OS is i386 (32 bits).
The way you modified dose not work for me. (But the code can go through. Better than before) the output is
$ ./myDev
fails
Devices on system: -1881144128
---------------------------
Using device �\������ with deviceID = 0, and handle = -1802694391
++++++++++++++++++++++++++++++
I guess the core thing is that I dont get the cuInit works correctly. From your output, you get the cuInit works well (return success) . But I can not. It is very strange :blink:
I previous suspect the Mac OS does not support the cuda driver API well since (in the SDK sample project) deviceQuery.cpp for Mac version is quite different deviceQuery.cpp for the RHEL version. so why is that?
It is my bad. The failure is because that i am using the emulator but not the GPU device. I guess that the machine I used updated the OS last week. So the CUDA driver seems to be over-written. I reinstall the driver and rerun the compiled code and get everything works now.