0

I'm trying to create a kernel that flattens a 3d pointer array to a 1d pointer array. To do so, I need to first allocate memory for the 3d pointer array in global memory. This is my approach to doing this.

#include <stdio.h>

#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

int main() {
    int image[3][3][3] = { //3 rows, columns, and 3 rgb values for each pixel
        {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
        {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}},
        {{19, 20, 21}, {22, 23, 24}, {25, 26, 27}}
    };

    const int IMAGE_ROW_COUNT = 3;
    const int IMAGE_COLUMN_COUNT = 3;
    const int ARRAY_BYTES = IMAGE_ROW_COUNT * IMAGE_COLUMN_COUNT * 3 * sizeof(float);

    float*** d_in2;
    float* d_out2;

    gpuErrchk(cudaMalloc((void**)&d_in2, IMAGE_ROW_COUNT * sizeof(float**)));
    gpuErrchk(cudaMalloc((void**)&d_out2, ARRAY_BYTES));

    for (int i = 0; i < IMAGE_ROW_COUNT; ++i) {
        gpuErrchk(cudaMalloc((void**)&d_in2[i], IMAGE_COLUMN_COUNT * sizeof(float*)));//issue
        for (int j = 0; j < IMAGE_COLUMN_COUNT; ++j) {
            gpuErrchk(cudaMalloc((void**)&d_in2[i][j], 3 * sizeof(float)));
        }
    }
}

I have labeled where the segmentation fault occurs in the above code. Strangely there should also be some error message printed from source, but only segmentation fault is shown in the terminal.

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • 1
    It is illegal to access the memory pointed to by d_in2 and d_out2 in host code after you allocate them on the device – talonmies Sep 02 '23 at 04:01

0 Answers0