【发布时间】:2011-06-14 18:41:44
【问题描述】:
我正在尝试弄清楚如何将 CUDA 内核用作库的一部分,这样我就可以将该库添加到我现有的 C++ 源文件中,并能够使用 cuda 内核。
那么你是怎么做的呢?我尝试创建一个包装器,如下所示:
.h 文件:
#ifndef __reductions2d_H_
#define __reductions2d_H_
#include <stdio.h>
#include <cuda.h>
#include <cuda_runtime.h>
extern "C" void getMean_wrapper();
#endif
.cu
__global__ void getMean(float *devDataPtr, size_t pitch, int rows, int cols)
{
for (int r = 0; r < height; ++r)
{
float* row = (float*)((char*)devPtr + r * pitch);
for (int c = 0; c < width; ++c)
{
printf("Row[%i][%i]: %4.3f \n",r,c row[c]);
}
}
}
void getMean_wrapper()
{
// Host code
int width = 3, height = 3;
int N = width*height;
float* devData; size_t pitch;
cudaMallocPitch(&devData, &pitch, width * sizeof(float),height);
int blockSize = 4;
int nBlocks = N/blockSize + (N%blockSize == 0?0:1);
getMean<<<nBlocks, blockSize>>>(devData, pitch, width,height);
}
main.cpp
#include "reductions2d.h"
int main(void){
getMean_wrapper();
return 0;
}
但是,当我用 nvcc *.cpp 编译它时,它告诉我它找不到 getMean_wrapper(),当我尝试只用 g++ -c main.cpp 编译时,它告诉我它找不到 cuda.h 和cuda_runtime.h
用我的 G++ 命令行指定 cuda 库的位置、构建这些对象、构建 .cu 对象然后链接它们的最佳方法是什么?似乎必须有一个 3 步过程来添加一些 cuda 功能很麻烦
谢谢
编辑:
似乎当我尝试单独进行时,然后链接到
g++ -o runme *.o -lcuda
我明白了
$ g++ -o runme *.o -lcuda
reductions2d.o: In function `__sti____cudaRegisterAll_47_tmpxft_00007643_00000000_4_reductions2d_cpp1_ii_4ef 611a7()':
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text+0x15e): undefined reference to `__cudaRegisterFatBinary'
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text+0x1b9): undefined reference to `__cudaRegisterFunction'
reductions2d.o: In function `__cudaUnregisterBinaryUtil()':
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text+0x1d8): undefined reference to `__cudaUnregisterFatBinary'
reductions2d.o: In function `__device_stub__Z7getMeanPfmii(float*, unsigned long, int, int)':
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text+0x20d): undefined reference to `cudaSetupArgument'
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text+0x22f): undefined reference to `cudaSetupArgument'
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text+0x251): undefined reference to `cudaSetupArgument'
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text+0x273): undefined reference to `cudaSetupArgument'
reductions2d.o: In function `getMean_wrapper':
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text+0x164c): undefined reference to `cudaConfigureCall'
reductions2d.o: In function `cudaError cudaLaunch<char>(char*)':
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text._Z10cudaLaunchIcE9cudaErrorPT_[cudaError cudaLaunch<char>(char*)]+0x11): undefined reference to `cudaLaunch'
reductions2d.o: In function `cudaError cudaMallocPitch<float>(float**, unsigned long*, unsigned long, unsigned long)':
tmpxft_00007643_00000000-1_reductions2d.cudafe1.cpp:(.text._Z15cudaMallocPitchIfE9cudaErrorPPT_Pmmm[cudaError cudaMallocPitch<float>(float**, unsigned long*, unsigned long, unsigned long)]+0x29): undefined reference to `cudaMallocPitch'
我读到我需要包含 cuda 运行时库,所以我做了
ldconfig -p | grep cudart 并将 /usr/local/cuda/lib64 包含在我的 LD_LIBRARY_PATH 中,但它仍然找不到 cudart
【问题讨论】:
标签: c++ g++ cuda linker-errors