天天看點

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

原文出處:點選打開連結

#############################################################

不要在.cpp檔案聲明核函數,否則會報錯,具體可以參考下面兩個連結:

http://blog.csdn.net/lingerlanlan/article/details/25063331?utm_source=tuicool

https://stackoverflow.com/questions/16550031/cuda-device-and-global-error-expected-constructor-destructor-or-type

############################################################

在做項目內建的時候需要用到cpp和cuda檔案聯調,自己摸索了兩種方式實作cpp和cu檔案混合編譯。

本文環境:

  • windows7 64位
  • VS2010
  • CUDA5.5
  • 英偉達顯示卡Tesla C1060

前言

裝好CUDA 5.5 sdk後,預設會自動添加好系統環境變量。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

是以不需要額外配置,不過為了保險起見,可以選擇性地添加以下環境變量:

CUDA_BIN_PATH  %CUDA_PATH%\bin
CUDA_LIB_PATH  %CUDA_PATH%\lib\Win32
CUDA_SDK_BIN  %CUDA_SDK_PATH%\bin\Win32
CUDA_SDK_LIB  %CUDA_SDK_PATH%\common\lib\Win32
CUDA_SDK_PATH  C:\cuda\cudasdk\common      

這時可以打開CUDA自帶的sample運作一下,運作能通過才可以繼續下面的内容————cpp和cuda聯調。

方法一:先建立cuda工程,再添加cpp檔案

1.打開vs2010,建立一個cuda項目,名稱CudaCpp。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

2.cuda預設建立的工程是如下,實作了兩個一維向量的并行相加。kernel函數和執行函數還有main函數全都寫在了一個cu檔案裡。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

3.接下來在工程裡添加一個空的cpp檔案。将原來cu檔案裡main函數裡的内容剪切到cpp檔案main函數裡。

為了讓cpp能夠調用cu檔案裡面的函數,在addWithCuda函數前加上extern "C" 關鍵字  (注意C大寫,為什麼addKernel不用加呢?因為cpp裡面直接調用的是addWithCuda)

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

4.在cpp裡也要加上addWithCuda函數的完整前向聲明。下圖就是工程的完整結構

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

5.可以在cpp裡的main函數return之間加入getchar()防止運作後一閃就退出,加上system("pause")或者直接ctrl+F5也行。

運作結果:

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

下面貼出CudaCpp項目代碼。

kernel.cu

[plain]  view plain  copy  print ?

  1. #include "cuda_runtime.h"  
  2. #include "device_launch_parameters.h"  
  3. #include <stdio.h>  
  4. __global__ void addKernel(int *c, const int *a, const int *b)  
  5. {  
  6.     int i = threadIdx.x;  
  7.     c[i] = a[i] + b[i];  
  8. }  
  9. // Helper function for using CUDA to add vectors in parallel.  
  10. extern "C"  
  11. cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)  
  12. {  
  13.     int *dev_a = 0;  
  14.     int *dev_b = 0;  
  15.     int *dev_c = 0;  
  16.     cudaError_t cudaStatus;  
  17.     // Choose which GPU to run on, change this on a multi-GPU system.  
  18.     cudaStatus = cudaSetDevice(0);  
  19.     if (cudaStatus != cudaSuccess) {  
  20.         fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");  
  21.         goto Error;  
  22.     }  
  23.     // Allocate GPU buffers for three vectors (two input, one output)    .  
  24.     cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));  
  25.     if (cudaStatus != cudaSuccess) {  
  26.         fprintf(stderr, "cudaMalloc failed!");  
  27.         goto Error;  
  28.     }  
  29.     cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));  
  30.     if (cudaStatus != cudaSuccess) {  
  31.         fprintf(stderr, "cudaMalloc failed!");  
  32.         goto Error;  
  33.     }  
  34.     cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));  
  35.     if (cudaStatus != cudaSuccess) {  
  36.         fprintf(stderr, "cudaMalloc failed!");  
  37.         goto Error;  
  38.     }  
  39.     // Copy input vectors from host memory to GPU buffers.  
  40.     cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);  
  41.     if (cudaStatus != cudaSuccess) {  
  42.         fprintf(stderr, "cudaMemcpy failed!");  
  43.         goto Error;  
  44.     }  
  45.     cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);  
  46.     if (cudaStatus != cudaSuccess) {  
  47.         fprintf(stderr, "cudaMemcpy failed!");  
  48.         goto Error;  
  49.     }  
  50.     // Launch a kernel on the GPU with one thread for each element.  
  51.     addKernel<<<1, size>>>(dev_c, dev_a, dev_b);  
  52.     // Check for any errors launching the kernel  
  53.     cudaStatus = cudaGetLastError();  
  54.     if (cudaStatus != cudaSuccess) {  
  55.         fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));  
  56.         goto Error;  
  57.     }  
  58.     // cudaDeviceSynchronize waits for the kernel to finish, and returns  
  59.     // any errors encountered during the launch.  
  60.     cudaStatus = cudaDeviceSynchronize();  
  61.     if (cudaStatus != cudaSuccess) {  
  62.         fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);  
  63.         goto Error;  
  64.     }  
  65.     // Copy output vector from GPU buffer to host memory.  
  66.     cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);  
  67.     if (cudaStatus != cudaSuccess) {  
  68.         fprintf(stderr, "cudaMemcpy failed!");  
  69.         goto Error;  
  70.     }  
  71. Error:  
  72.     cudaFree(dev_c);  
  73.     cudaFree(dev_a);  
  74.     cudaFree(dev_b);  
  75.     return cudaStatus;  
  76. }  

main.cpp

[cpp]  view plain  copy  print ?

  1. #include <stdio.h>  
  2. #include "cuda_runtime.h"  
  3. #include "device_launch_parameters.h"  
  4. extern "C"  
  5.     cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);  
  6. int main()  
  7. {  
  8.     const int arraySize = 5;  
  9.     const int a[arraySize] = { 1, 2, 3, 4, 5 };  
  10.     const int b[arraySize] = { 10, 20, 30, 40, 50 };  
  11.     int c[arraySize] = { 0 };  
  12.     // Add vectors in parallel.  
  13.     cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);  
  14.     if (cudaStatus != cudaSuccess) {  
  15.         fprintf(stderr, "addWithCuda failed!");  
  16.         return 1;  
  17.     }  
  18.     printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",  
  19.         c[0], c[1], c[2], c[3], c[4]);  
  20.     printf("cuda工程中調用cpp成功!\n");  
  21.     // cudaDeviceReset must be called before exiting in order for profiling and  
  22.     // tracing tools such as Nsight and Visual Profiler to show complete traces.  
  23.     cudaStatus = cudaDeviceReset();  
  24.     if (cudaStatus != cudaSuccess) {  
  25.         fprintf(stderr, "cudaDeviceReset failed!");  
  26.         return 1;  
  27.     }  
  28.     getchar(); //here we want the console to hold for a while  
  29.     return 0;  
  30. }  

方法二:先建立cpp工程,再添加cu檔案

方法一由于是cuda工程是自動建立的,是以比較簡單,不需要多少額外的配置。而在cpp工程裡面添加cu就要複雜一些。為了簡單起見,這裡采用console程式講解,至于MFC或者Direct3D程式同理。

1.建立一個空的win32控制台工程,名稱CppCuda。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

2.然後右鍵工程-->添加一個cu檔案

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

3.将方法一中cu和cpp檔案的代碼分别拷貝到這個工程裡來(做了少許修改,extern "C"關鍵字和某些頭檔案不要忘了加),工程結構如圖:

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

這個時候編譯是通不過的,需要作一些配置。

4.關鍵的一步,右鍵工程-->生成自定義 ,将對話框中CUDA5.5前面的勾打上。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

這時點選 工程-->屬性,會發現多了CUDA連結器這一項。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

5.關鍵的一步,右鍵kernel.cu檔案-->屬性,在 正常-->項類型 裡面選擇CUDA C/C++(由于cu檔案是由nvcc編譯的,這裡要修改編譯連結屬性)

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

6.工程-->屬性-->連結器-->附加依賴項,加入cudart.lib

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

7.工具-->選項-->文本編輯器-->檔案擴充名 添加cu \cuh兩個檔案擴充名

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

8.至此配置成功。運作一下:

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

9.為了更加确信cuda中的函數确實被調用,在main.cpp裡面調用cuda函數的地方加入了一個斷點。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

單步執行一下。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

可以看到程式跳到了cu檔案裡去執行了,說明cpp調用cuda函數成功。

VS裡面關于.cpp檔案與.cu檔案混合編譯問題---不要在.cpp檔案聲明核函數(__device__()和global__()) 前言 方法一:先建立cuda工程,再添加cpp檔案 方法二:先建立cpp工程,再添加cu檔案

貼上代碼(其實跟方式一基本一樣,沒怎麼改),工程CppCuda

kernel.cu

[plain]  view plain  copy  print ?

  1. #include "cuda_runtime.h"  
  2. #include "device_launch_parameters.h"  
  3. #include <stdio.h>  
  4. //cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);  
  5. __global__ void addKernel(int *c, const int *a, const int *b)  
  6. {  
  7.     int i = threadIdx.x;  
  8.     c[i] = a[i] + b[i];  
  9. }  
  10. // Helper function for using CUDA to add vectors in parallel.  
  11. extern "C"  
  12. cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)  
  13. {  
  14.     int *dev_a = 0;  
  15.     int *dev_b = 0;  
  16.     int *dev_c = 0;  
  17.     cudaError_t cudaStatus;  
  18.     // Choose which GPU to run on, change this on a multi-GPU system.  
  19.     cudaStatus = cudaSetDevice(0);  
  20.     if (cudaStatus != cudaSuccess) {  
  21.         fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");  
  22.         goto Error;  
  23.     }  
  24.     // Allocate GPU buffers for three vectors (two input, one output)    .  
  25.     cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));  
  26.     if (cudaStatus != cudaSuccess) {  
  27.         fprintf(stderr, "cudaMalloc failed!");  
  28.         goto Error;  
  29.     }  
  30.     cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));  
  31.     if (cudaStatus != cudaSuccess) {  
  32.         fprintf(stderr, "cudaMalloc failed!");  
  33.         goto Error;  
  34.     }  
  35.     cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));  
  36.     if (cudaStatus != cudaSuccess) {  
  37.         fprintf(stderr, "cudaMalloc failed!");  
  38.         goto Error;  
  39.     }  
  40.     // Copy input vectors from host memory to GPU buffers.  
  41.     cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);  
  42.     if (cudaStatus != cudaSuccess) {  
  43.         fprintf(stderr, "cudaMemcpy failed!");  
  44.         goto Error;  
  45.     }  
  46.     cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);  
  47.     if (cudaStatus != cudaSuccess) {  
  48.         fprintf(stderr, "cudaMemcpy failed!");  
  49.         goto Error;  
  50.     }  
  51.     // Launch a kernel on the GPU with one thread for each element.  
  52.     addKernel<<<1, size>>>(dev_c, dev_a, dev_b);  
  53.     // Check for any errors launching the kernel  
  54.     cudaStatus = cudaGetLastError();  
  55.     if (cudaStatus != cudaSuccess) {  
  56.         fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));  
  57.         goto Error;  
  58.     }  
  59.     // cudaDeviceSynchronize waits for the kernel to finish, and returns  
  60.     // any errors encountered during the launch.  
  61.     cudaStatus = cudaDeviceSynchronize();  
  62.     if (cudaStatus != cudaSuccess) {  
  63.         fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);  
  64.         goto Error;  
  65.     }  
  66.     // Copy output vector from GPU buffer to host memory.  
  67.     cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);  
  68.     if (cudaStatus != cudaSuccess) {  
  69.         fprintf(stderr, "cudaMemcpy failed!");  
  70.         goto Error;  
  71.     }  
  72. Error:  
  73.     cudaFree(dev_c);  
  74.     cudaFree(dev_a);  
  75.     cudaFree(dev_b);  
  76.     return cudaStatus;  
  77. }  

main.cpp

[cpp]  view plain  copy  print ?

  1. #include <iostream>  
  2. #include "cuda_runtime.h"  
  3. #include "device_launch_parameters.h"  
  4. using namespace std;  
  5. extern "C"  
  6.     cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);  
  7. int main(int argc,char **argv)  
  8. {  
  9.     const int arraySize = 5;  
  10.     const int a[arraySize] = { 1, 2, 3, 4, 5 };  
  11.     const int b[arraySize] = { 10, 20, 30, 40, 50 };  
  12.     int c[arraySize] = { 0 };  
  13.     // Add vectors in parallel.  
  14.     cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);  
  15.     if (cudaStatus != cudaSuccess) {  
  16.         fprintf(stderr, "addWithCuda failed!");  
  17.         return 1;  
  18.     }  
  19.     cout<<"{1,2,3,4,5} + {10,20,30,40,50} = {"<<c[0]<<','<<c[1]<<','<<c[2]<<','<<c[3]<<'}'<<endl;  
  20.     printf("cpp工程中調用cu成功!\n");  
  21.     // cudaDeviceReset must be called before exiting in order for profiling and  
  22.     // tracing tools such as Nsight and Visual Profiler to show complete traces.  
  23.     cudaStatus = cudaDeviceReset();  
  24.     if (cudaStatus != cudaSuccess) {  
  25.         fprintf(stderr, "cudaDeviceReset failed!");  
  26.         return 1;  
  27.     }  
  28.     system("pause"); //here we want the console to hold for a while  
  29.     return 0;  
  30. }  

注意有時候編譯出問題,把   "device_launch_parameters.h"  這個頭檔案去掉就好了(去掉之後就不能調裡面的函數或變量了),至于為什麼,還不是很清楚。

以後将cu檔案加入到任何MFC,qt,D3D或者OpenGL等C++工程中步驟都是類似的。