need HELP on the cuInit error and Bus error

Hi, all

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).

thanks!

Your code will not compile on my Linux box.

Anyway, change the line:
gPtr = (uintptr_t)cuDevice;
to
gPtr = &cuDevice;

With this change, it will work on both Mac:
./a.out
cuStatus success
Devices on system: 1

cuda get device 0
Using device GeForce 8600M GT with deviceID = 0, and handle = 0
++++++++++++++++++++++++++++++

and Linux:
./a.out
cuStatus success
Devices on system: 1

cuda get device 0
Using device Tesla T10 Processor with deviceID = 0, and handle = 0
++++++++++++++++++++++++++++++

Sir, many thanks for your quick reply!

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?

thanks again!

I also removed the cutil call ( cutil should not be used outside the SDK examples) and few other things.

#include <stdio.h>

#include <stdlib.h>

#if defined(__GNUC__)

#include <stdint.h>

#endif

#include "cuda.h"

#include "cuda_runtime_api.h"

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);

	char name[100];

	cuDeviceGetName(name,100,cuDevice);

	printf("cuda get device %d\n", deviceID);

	//gPtr = (uintptr_t)cuDevice;

	gPtr = &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");

}

Compile with your favorite compiler ( gcc or nvcc):

nvcc mac_bug.cu -L /usr/local/cuda/lib -lcuda

I remove the cutil.h. in fact, i dont use cutil in this file. I get it compiled and run again. But nothing changes.

what is the version of your mac os? is it related to the problem?

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.