Cuda 混合 C 项目链接
我有一个用 C 语言编写的大型项目,我正在尝试在其中集成一些 Cuda 内核。我正在使用“gcc -c main.c”编译我的c文件,并使用“nvcc -c cuda_GMRES.cu”编译我的.cu文件,然后我尝试使用nvcc链接这2个目标文件:“nvcc -o main.c”。 o cuda_GMRES.o”并收到以下错误:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:在函数中 <代码>_start': (.text+0x20):未定义对 main' 的引用 collect2: ld returned 1 exit status
这是我第一次尝试将 cuda 与 C 文件结合起来,我可能做错了什么。有人可以帮助我吗?我使用的是 Rocks 操作系统的 GPU 集群。
我的 main.c 文件:
#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;
}
我的 cuda_wrapper.h 文件:
#ifndef Cuda_GMRES_cuda_wrapper_h
#define Cuda_GMRES_cuda_wrapper_h
//wrapper function declaration
void cuda_GMRES(double a);
#endif
我的 cuda_GMRES.cu 文件,包含内核调用函数:
#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 );
}
我的 cuda_dot.cu 文件,包含内核:
__global__ void cuda_dot(double a, double *help)
{
*help=2*a;
}
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':
main'
(.text+0x20): undefined reference to
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的链接命令似乎包含致命错误。假设您首先像这样编译两个对象:
您应该有两个对象文件
main.o
和cuda_GMRES.o
。然后你可以这样做:这个命令说“使用 cuda_GMRES.o 链接一个名为 main.o 的程序文件”,即。覆盖 main.o。正是由于这个原因,链接器抱怨缺少主子例程,而您没有提供主子例程(并且您正在销毁同时包含一个主子例程的目标文件)。
您想要这样的东西:
其中
executable
是最终链接程序的名称,或者它将发出一个名为
a.out
的默认链接程序Your linking command appears to contain a fatal error. Supposing you first compile two objects like this:
you should have two object files
main.o
andcuda_GMRES.o
. You then do this: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:
where
executable
is the name of the final linked program, orwhich will emit a default linked program called
a.out