0

I am writing this code for CUDA. I am facing Segfault at CudaMalloc for int* at d_runs

    int* d_runs;   
    BYTE* d_min;
    BYTE* d_max;
    BYTE* d_rawDataByte[size];

    /Allocate space for device copies
    cudaMalloc((void**)&d_runs, size*sizeof(int));
    cudaMemcpy(d_runs, &_runs[0], size*sizeof(int), cudaMemcpyHostToDevice);

    cudaMalloc((void **)&d_min, size*sizeof(BYTE));
    cudaMemcpy(d_min, _min, size*sizeof(BYTE), cudaMemcpyHostToDevice);

    cudaMalloc((void **)&d_max, size*sizeof(BYTE));
    cudaMemcpy(d_max, _max, size*sizeof(BYTE), cudaMemcpyHostToDevice);
    cudaMalloc((void**)&d_rawDataByte, size*sizeof(BYTE *));

size=1000.

Edit: Ok. I have changed BYTE* d_rawDataByte[size] to BYTE** d_rawDataByte

cudaError_t rc = cudaMalloc((void **)&d_min, size*sizeof(BYTE));
    if (rc != cudaSuccess)
        std::cout<<"Could not allocate memory: " <<rc;
    cudaMemcpy(d_min, _min, size*sizeof(BYTE), cudaMemcpyHostToDevice);

Still the problem persists. Infact, none of the cudaMalloc is behaving properly. I am getting Segfault in every one of them.

Avi Garg
  • 11
  • 3
  • The CUDA error might be able to tell you more, if you just let it. See [What is the canonical way to check for errors using the CUDA runtime API?](https://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api). Or are you segfaulting even before reaching the first `if (rc != cudaSuccess)`? – paleonix Sep 16 '22 at 11:43
  • 1
    A segfault at the `cudaMalloc` could hypothetically also mean that you wrote out of bounds in previous host code and corrupted something which causes the later segfault. This will be impossible to argue about without a [minmal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – paleonix Sep 16 '22 at 11:48

1 Answers1

1

You should check the result of cudaMalloc to see if it was successful.

This is clearly wrong:

cudaMalloc((void**)&d_rawDataByte, size*sizeof(BYTE *));

d_rawDataByte was declared as an array, so you can't reassign its address to point somewhere else. It would appear that you intended to allocate an array of pointers? In that case it should have been BYTE** d_rawDataByte;

Lundin
  • 195,001
  • 40
  • 254
  • 396