0

I'm trying to pass a 2D array to a C function in a CUDA code, and nvcc won't compile it, but gcc works fine. Here's the code:

#include<stdio.h>

void f(int row, int col, int[row][col]) {
    printf("LOL\n");
}

int main(void) {
    int mat[2][3];
    f(2, 3, mat);
}

If I put this code in test.c and run gcc test.c, it works as expected.

If I put this code in test.cu and run nvcc test.cu, it throws the following error:

test.cu(3): error: a parameter is not allowed

Why is it not working with nvcc ? It it's not supported, what's the best way to give a 2D array as parameter in CUDA ? A pointer to the first element of the first row ?

Thanks in advance for help on this.

MeanStreet
  • 1,217
  • 1
  • 15
  • 33
  • 4
    When you compile with nvcc, a C++ compiler is used for that code. – talonmies Jan 22 '19 at 16:38
  • 1
    This [answer](https://stackoverflow.com/questions/45643682/cuda-using-2d-and-3d-arrays/45644824#45644824) covers a variety of methods to handle 2D arrays in CUDA. If you know the width of your 2D array at compile time, then the methods described there with known width are good choices. They allow you to express 2D subscripting while still having efficient single-pointer access. Otherwise, flattening is probably the best approach for performance ("A pointer to the first element of the first row", assuming the underlying allocation is contiguous across rows.) – Robert Crovella Jan 22 '19 at 16:40

1 Answers1

1

For C99 compatible Compiler this method works, otherwise you should use pointer with cast, like that:

#include<stdio.h>

void f(int row, int col, int *a) {
    printf("LOL\n");
}

int main(void) {
    int mat[2][3];
    f(2, 3, (int*)mat);
}
GM1
  • 380
  • 3
  • 14