user4576114
user4576114

Reputation:

matrix as a parameter in CUDA

I have two problems in CUDA programming.

  1. I want to pass matrix as a function parameter in a CUDA program. I tried following. GCC compiler compiles the following code but NVIDIA CUDA C compiler does not compiles this code and prompts error. (I have installed CUDA 7.5)

    void printMatrix( size_t rows, size_t cols, int a[][cols] )
    

    and

    void printMatrix(int row, int col, int matrix[row][col])
    

Both are not working. It gives "a parameter is not allowed" error.

  1. Inside the main method I want to declare a matrix

    int a[n][n];
    

    where n runs from 1 to 5 (in a for loop). It gives "expression must have a constant value" error.

Where am I making the error.

I have tried to compile the code from this question with gcc and nvcc compiler, gcc compiles and nvcc does not.

compiler results

Upvotes: 1

Views: 401

Answers (1)

talonmies
talonmies

Reputation: 72349

Because you are doing this on Windows, nvcc (note nvccis not a compiler) uses the Visual Studio compiler to compile host host. Visual Studio does not support C99 language features, so you cannot use them in any host code you will compile on Windows in conjunction with CUDA. You will have to rewrite your code without using C99 language features in your host code.

If you were doing this on linux, you would be using gcc to compile the host code via nvcc, and C99 language features would be available if you provide the correct command line options and pass the file with a .c extension, as you have ably demonstrated in your question. C99 features and CUDA cannot be mixed in a .cu file because CUDA requires a C++ compiler to compile host code contain CUDA language extensions.

Upvotes: 2

Related Questions