C與CUDA混合編程配置

原文: http://blog.csdn.NET/u012234115/article/details/34860273


在做項目集成的時候需要用到cpp和cuda文件聯調,自己摸索了兩種方式實現cpp和cu文件混合編譯。

本文環境:

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

前言

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



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

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。




2.cuda默認建立的工程是如下,實現了兩個一維向量的並行相加。kernel函數和執行函數還有main函數全都寫在了一個cu文件裏。




3.接下來在工程裏添加一個空的cpp文件。將原來cu文件裏main函數裏的內容剪切到cpp文件main函數裏。

爲了讓cpp能夠調用cu文件裏面的函數,在addWithCuda函數前加上extern “C” 關鍵字  (注意C大寫,爲什麼addKernel不用加呢?因爲cpp裏面直接調用的是addWithCuda)




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




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

運行結果:




下面貼出CudaCpp項目代碼。

kernel.cu

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

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

方法二:先建立cpp工程,再添加cu文件

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


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



2.然後右鍵工程–>添加一個cu文件




3.將方法一中cu和cpp文件的代碼分別拷貝到這個工程裏來(做了少許修改,extern “C”關鍵字和某些頭文件不要忘了加),工程結構如圖:




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


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




這時點擊 工程–>屬性,會發現多了CUDA鏈接器這一項。




5.關鍵的一步,右鍵kernel.cu文件–>屬性,在 常規–>項類型 裏面選擇CUDA C/C++(由於cu文件是由nvcc編譯的,這裏要修改編譯鏈接屬性)




6.工程–>屬性–>鏈接器–>附加依賴項,加入cudart.lib




7.工具–>選項–>文本編輯器–>文件擴展名 添加cu \cuh兩個文件擴展名




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




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




單步執行一下。




可以看到程序跳到了cu文件裏去執行了,說明cpp調用cuda函數成功。




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

kernel.cu

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

  1. #include <iostream>  
  2. #include “cuda_runtime.h”  
  3. #include “device_launch_parameters.h”  
  4. using namespace std;  
  5.   
  6. extern “C”  
  7.     cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);  
  8. int main(int argc,char **argv)  
  9. {  
  10.     const int arraySize = 5;  
  11.     const int a[arraySize] = { 1, 2, 3, 4, 5 };  
  12.     const int b[arraySize] = { 10, 20, 30, 40, 50 };  
  13.     int c[arraySize] = { 0 };  
  14.   
  15.     // Add vectors in parallel.  
  16.     cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);  
  17.     if (cudaStatus != cudaSuccess) {  
  18.         fprintf(stderr, ”addWithCuda failed!”);  
  19.         return 1;  
  20.     }  
  21.   
  22.     cout<<”{1,2,3,4,5} + {10,20,30,40,50} = {“<<c[0]<<‘,’<<c[1]<<‘,’<<c[2]<<‘,’<<c[3]<<‘}’<<endl;  
  23.     printf(”cpp工程中調用cu成功!\n”);  
  24.   
  25.     // cudaDeviceReset must be called before exiting in order for profiling and  
  26.     // tracing tools such as Nsight and Visual Profiler to show complete traces.  
  27.     cudaStatus = cudaDeviceReset();  
  28.     if (cudaStatus != cudaSuccess) {  
  29.         fprintf(stderr, ”cudaDeviceReset failed!”);  
  30.         return 1;  
  31.     }  
  32.     system(”pause”); //here we want the console to hold for a while  
  33.     return 0;  
  34. }  
注意有時候編譯出問題,把  “device_launch_parameters.h” 這個頭文件去掉就好了(去掉之後就不能調裏面的函數或變量了),至於爲什麼,還不是很清楚。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章