AlexS
AlexS

Reputation: 520

why is cudaMalloc returning an illegal memory access

I'm writing a mexFunction for MATLAB and I've got the CUDA MEX functionality running with the MATLAB examples no problems.

Below is a simple "load the data to the device" script. It returns 3 messages, the first is before the cudaMalloc and the other two are just after the cudMalloc functions.

no error
cudaMalloc failed! an illegal memory access was encountered
an illegal memory access was encountered

System: Win7 64Bit, MATLAB 2015a, VS2012 Professional, CUDA 6.5.

Any ideas? it looks correct to me.

Code:

void process(double *x, double *y, size_t n)
{
float *d_x, *d_y; // Pointers to data on Device.
cudaError_t cudaStatus;

cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
    mexPrintf("cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");        
}   

// Check If no Errors with GPU so far
mexPrintf(cudaGetErrorString(cudaGetLastError()));  mexPrintf("\n");

// Allocate Memory on Device    
cudaStatus = cudaMalloc( (void**)&d_x, n * sizeof(float) );
if (cudaStatus != cudaSuccess) {
    mexPrintf("cudaMalloc failed!  ");        
}
mexPrintf(cudaGetErrorString(cudaGetLastError()));  mexPrintf("\n");

cudaStatus = cudaMalloc( (void**)&d_y, n * sizeof(float) );
mexPrintf(cudaGetErrorString(cudaGetLastError()));  mexPrintf("\n");

// free the memory allocated on the GPU
cudaFree( d_x );
cudaFree( d_y ); }

Upvotes: 0

Views: 1535

Answers (1)

AlexS
AlexS

Reputation: 520

I solved the issue by restarting the computer, the above code in the question works fine

I can only presume that a previous bad memory allocation in an earlier iteration of the code was causing the GPU to fail.

If anyone has any insight into spotting this, or re-initialising the device without restarting i'd be happy to hear it!

Thanks

Upvotes: 2

Related Questions