1

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

Community
  • 1
  • 1
  • You have already asked this question once today. I think that is enough. – talonmies Feb 22 '16 at 16:19
  • @talonmies my problem is, gcc compiler compiles where CUDA compiler does not. Can you understand my problem –  Feb 22 '16 at 16:29
  • Someone already provided you with a complete answer to your question [here](http://stackoverflow.com/a/35549908/681865). nvcc uses the host C++ compiler by default, so the second version is the most suitable. If you try and compile the first version in that answer, nvcc/g++ will tell you *exactly* what option you must use to compile C99 code without error. This doesn't warrant a new question. – talonmies Feb 22 '16 at 19:11
  • @talonmies i have edited the question and added a picture of the results I get –  Feb 22 '16 at 19:28
  • @talonmies I have tried "nvcc -Xcompiler -std=c99 printmatrix.cu", "nvcc -arch=sm_20 printmatrix.cu" both are not compiling –  Feb 23 '16 at 07:36
  • I have told you exactly why that is - you are using the Visual Studio compiler on Windows and it has no C99 support. You *cannot* use C99 syntax in code you will build on Windows via nvcc – talonmies Feb 23 '16 at 07:41
  • @talonmies I got access to a ubuntu machine with cuda 7.5. I tried to compile with "nvcc printmatrix.cu", "nvcc -Xcompiler -std=c99 printmatrix.cu" and "nvcc -arch=sm_20 printmatrix.cu". But it prompts compilation error ((23): error: a value of type "void *" cannot be used to initialize an entity of type "int **") and ((27): error: a value of type "void *" cannot be assigned to an entity of type "int *") –  Feb 23 '16 at 10:41
  • `.cu` files are compiled with a C++ compiler. If you have a C99 containing file, you must pass it to nvcc with a `.c` file extension – talonmies Feb 24 '16 at 06:43
  • @talonmies As shown above, I tried to compile nvcc with .cu file extension. ie, printmatrix.cu –  Feb 24 '16 at 08:16
  • I understand that. I am telling you that you must use a `.c` extension and pass -Xcompiler="-std=C99" to nvcc if you want to compile C99 code. You cannot mixed C99 features and CUDA in a `.cu`file because those are compiled with a C++ compiler – talonmies Feb 24 '16 at 08:43
  • @talonmies i tried and I get this error "command line option ‘-std=c99’ is valid for C/ObjC but not for C++" –  Feb 24 '16 at 10:29
  • That is only a warning, not an error. – talonmies Feb 24 '16 at 10:34
  • @talonmies This is subpart of a cuda program that I am writing, is it possible to compile this code in `.cu` file –  Feb 24 '16 at 10:38
  • As I have now said about 10 times in comments and in my answer, NO – talonmies Feb 24 '16 at 12:24

1 Answers1

2

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.

Community
  • 1
  • 1
talonmies
  • 70,661
  • 34
  • 192
  • 269