tim_chemeng
tim_chemeng

Reputation: 119

Cuda mixed C project linking

I have a large project in C and i'm trying to integrate some Cuda kernels in it. I'm compiling my c-files with "gcc -c main.c" and my .cu files with "nvcc -c cuda_GMRES.cu" and then I try to link the 2 object files with nvcc: "nvcc -o main.o cuda_GMRES.o" and receive the following error:

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function _start': (.text+0x20): undefined reference tomain' collect2: ld returned 1 exit status

It's the first time I'm trying to combine cuda with C files and I might have done something wrong.Can someone help me please. I'm on a GPU Cluster with Rocks OS.

My main.c file:

#include <stdio.h>
#include <math.h> 
#include "cuda_wrapper.h"   //header containing wrapper function
//cuda_GMRES that calls the kernel   cuda_dot

int main (int argc,char* argv[])
{
//content  
//bla bla bla
//cuda Function call  
  cuda_GMRES(50); 
   return 0;
}

My cuda_wrapper.h file:

#ifndef Cuda_GMRES_cuda_wrapper_h
#define Cuda_GMRES_cuda_wrapper_h
//wrapper function declaration

void cuda_GMRES(double a);
#endif

My cuda_GMRES.cu file that contains the kernel calling function:

#include <stdio.h>
#include "cuda_wrapper.h"
#include "cuda_dot.cu"   

//kernel declaration
__global__ void cuda_dot();

//kernel calling function
extern "C"
void cuda_GMRES(double a)
{
double b;

double *dev_a;
double *res;

cudaMemcpy(dev_a, &a, sizeof(double), cudaMemcpyHostToDevice );
cuda_dot<<< 1, 1 >>>(*dev_a, res );
cudaMemcpy(&b, res, sizeof(double), cudaMemcpyDeviceToHost );
}    

My cuda_dot.cu file that contains the kernel:

__global__ void cuda_dot(double a, double *help)
{
   *help=2*a;
}

Upvotes: 2

Views: 3568

Answers (1)

talonmies
talonmies

Reputation: 72382

Your linking command appears to contain a fatal error. Supposing you first compile two objects like this:

gcc -c main.c
nvcc -c cuda_GMRES.cu 

you should have two object files main.o and cuda_GMRES.o. You then do this:

nvcc -o main.o cuda_GMRES.o

This command says "link a program file called main.o using cuda_GMRES.o", ie. overwrite main.o. It is for this reason that the linker is complaining about a missing main subroutine, you are not supplying one (and you are destroying the object file which contains one at the same time).

You want something like this:

nvcc -o executable main.o cuda_GMRES.o

where executable is the name of the final linked program, or

nvcc main.o cuda_GMRES.o

which will emit a default linked program called a.out

Upvotes: 3

Related Questions