2011-01-24 4 views
2

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 객체를 작성한 다음 링크하는 것이 가장 좋습니다. 번거 로움이 일부 CUDA 기능에

감사

을 추가하는 3 단계 과정을해야 할 것 같다

편집 :

그것은 내가

wtih, 개별적으로 t 암탉 링크를 할 때처럼 보인다
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와 LD_LIBRARY_PATH에/usr/local/cuda/lib64가 포함되어 있고 여전히 cudart를 찾을 수 없습니다.

답변

5

.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 
+0

편집 된 답변을 주셔서 감사합니다. 나는 링크를하기 위해 g ++을 사용하여 얻은 링커 에러로 질문을 편집했다. – Derek

+0

@Derek 업데이트보기 – Anycorn

관련 문제