Search code examples
ccudanvcc

cannot compile the CUDA code when splitted into several files


I'm writing this simple cuda code and I'm unable to compile it. The code contains part of code written in C. This is the structure of the program:

read_data.c file contains a function called read_data add.cu file contains a function called add (this is the part that should run in the GPGPU) optimize.h file contains the necessary headers. master.c file contains the main function.

The optimize.h file looks like follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>

__global__ void add(int, float*, float*);
void read_data(char*, float**, float**, int**);

master.c file looks like follows:

#include "optimize.h"

int main(){
    char* path = "./data/0";
    float* base_load;
    float* comfortable_temperatures;
    int* comfort_index;

    read_data(path, &base_load, &comfortable_temperatures, &comfort_index);
    int N = 1<<20;
    float *x, *y;
    int i;

    cudaMallocManaged(&x, N*sizeof(float));
    cudaMallocManaged(&y, N*sizeof(float));
  
    for (i = 0; i < N; i++) {
       x[i] = 1.0f;
       y[i] = 2.0f;
    }
    add<<<1, 256>>>(N, x, y);

    cudaDeviceSynchronize();

    // still need to read the result back.

    cudaFree(x);
    cudaFree(y);
   }

I compiled this using the following line:

nvcc -o master master.c read_data.c add.cu

and I'm getting this error:

In file included from master.c:1:0:
optimize.h:9:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
 __global__ void add(int, float*, float*);
            ^
master.c: In function ‘main’:
master.c:51:26: error: ‘add’ undeclared (first use in this function)
                          add<<<1, 256>>>(N, x, y);
                          ^
master.c:51:26: note: each undeclared identifier is reported only once for each function it appears in
master.c:51:31: error: expected expression before ‘<’ token
                          add<<<1, 256>>>(N, x, y);

I think the whatever the error is, it should be a very small one. But I cannot find it.


Solution

  • nvcc by default treats filenames ending in .c or .cpp as having no CUDA-specific syntax, and sends those to the host compiler. The host compiler cannot handle CUDA-specific syntax, which is why you are getting the errors.

    The usual recommendations are to place your CUDA code in files ending with .cu. You can alternatively pass -x cu as a compile switch to do this.

    Note that nvcc uses c++ style linkage, so you will need to arrange for correct c-style linkage if you are trying to link code in a .cu file with code in a .c file. If you have no C-specific usage, again, a simple solution may be to rename your .c file to .cpp or .cu.

    There are many questions here on the cuda tag explaining how to do C++/C linkage, otherwise.