ссылки в библиотеке, которая содержит ссылку на ядро ​​CUDA - PullRequest
2 голосов
/ 24 января 2011

Я пытаюсь понять, как использовать ядро ​​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

Наилучший подход - указать местоположение библиотек cuda с помощью моей командной строки G ++, построить эти объекты, создать объекты .cu, а затем связать их?Похоже, хлопотно иметь трехэтапный процесс для добавления некоторых функций cuda

Спасибо

edit:

похоже, что когда я пытаюсь сделать это индивидуально,После ссылки

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

1 Ответ

5 голосов
/ 24 января 2011

включает .h в .cu. nvcc - это компилятор c ++, который искажает имена.

скомпилировать как:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...