链接到包含 cuda 内核引用的库

发布于 2024-10-14 15:41:46 字数 3592 浏览 3 评论 0原文

我正在尝试弄清楚如何使用 CUDA 内核作为库的一部分,以便我可以将该库添加到现有的 C++ 源文件中,并且能够使用 cuda 内核。

那么你要如何去做呢?我尝试创建一个包装器,如下所示:

.h file:

#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

I am trying to figure out how to use a CUDA kernel as part of a library so that I can just add the library to my existing C++ source files, and be able to use the cuda kernel.

So how do you go about doing this? I tried to create a wrapper, like so:

.h file:

#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;

}

However, when I compile this with nvcc *.cpp, it tells me it cant find getMean_wrapper(), and when I try to just compile with g++ -c main.cpp, it tells me it cant find cuda.h and cuda_runtime.h

Is the best approach to specify the location of the cuda libraries with my G++ command line, build those objects, build the .cu objects, then link them? Seems like a hassle to have to have a 3 step process to add in some cuda functionality

Thanks

edit:

it seems like when I try to do it individually,t hen link wtih

g++ -o runme *.o -lcuda

I get

$ 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'

I read that i need to include the cuda runtime libraries, so I did

ldconfig -p | grep cudart and included /usr/local/cuda/lib64 in my LD_LIBRARY_PATH and it still cant find cudart

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-10-21 15:41:46

也将 .h 包含到 .cu 中。 nvcc 是 c++ 编译器和 mangles 的名称。

编译为:

nvcc -c file.cu // compile cuda kernel
nvcc file.o main.cpp // compile and link

我会将您的代码更改为:

.hpp

#ifndef __reductions2d_H_
#define __reductions2d_H_

void getMean_wrapper(); // c++ linkga

#endif

.cu:

#include "...hpp"
#include <cuda.h>
#include <cuda_runtime.h>

__global__ void getMean(float *devDataPtr, size_t pitch, int rows, int cols)
   {

然后您编译为

nvcc -c file.cu // compile cuda kernel
g++ -lcudart file.o main.cpp // no cuda stuff needed save for lib

include .h into .cu as well. nvcc is c++ compiler and mangles names.

compile as:

nvcc -c file.cu // compile cuda kernel
nvcc file.o main.cpp // compile and link

I would change you code as:

.hpp

#ifndef __reductions2d_H_
#define __reductions2d_H_

void getMean_wrapper(); // c++ linkga

#endif

.cu:

#include "...hpp"
#include <cuda.h>
#include <cuda_runtime.h>

__global__ void getMean(float *devDataPtr, size_t pitch, int rows, int cols)
   {

which then you compile as

nvcc -c file.cu // compile cuda kernel
g++ -lcudart file.o main.cpp // no cuda stuff needed save for lib
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文