dparkar
dparkar

Reputation: 2134

cudaMalloc segmentation fault high values

First the error : Segmentation Fault (at the highlighted cudaMalloc line if I make a > approximately 61432)

unsigned int xarray[a];
unsigned int *dev_a;
int result[33*a];
int *dev_result;

**cudaMalloc((void **)&dev_a,a * sizeof(unsigned int));**

cudaMemcpy(dev_a,xarray,a*sizeof(int),cudaMemcpyHostToDevice);

kernel<<<a,66>>>(dev_a,dev_result);

cudaMemcpy(result,dev_result,33*a*sizeof(int),cudaMemcpyDeviceToHost);

The reason why I mentioned 'approximately' above is because sometimes it works when a = 61432 and sometimes it doesn't. I am not able to understand why.

Also, this is only for a launch of 1D grid of blocks. My intent is to launch a 3D grid [a*a*a].

Upvotes: 1

Views: 1438

Answers (1)

Jared Hoberock
Jared Hoberock

Reputation: 11396

You code is segfaulting because the array result is too large. In practice, you can't really expect to statically allocate an array of size 33 * 61432 on the stack.

Instead, use a std::vector to dynamically allocate the array and pass a pointer to the pointer's data to cudaMemcpy:

#include <vector>
...
std::vector<int> result(33 * 1);
...
cudaMemcpy(&result[0], dev_result, 33 * a * sizeof(int), cudaMemcpyDeviceToHost);

Upvotes: 1

Related Questions