在C++ class 中使用CUDA(包含texutre 2d的使用)

洛欣發表於2009-11-17

晚上熬夜寫了一個簡單的C++ class 封裝CUDA的demo,裡面涉及到了texture的使用,希望對他的CUDA學習有幫助:
下面是三部分的程式碼
1. 呼叫class的sample.cu 檔案這裡沒多少好解釋的,就是直接生產一個cuda的class例項,然後呼叫各個方法

  1. /********************************************************************
  2. *  sample.cu
  3. *  This is a example of the CUDA program.
  4. *  author: zhao.kaiyong(at)gmail.com
  5. *  http://www.comp.hkbu.edu.hk/~kyzhao/
  6. *********************************************************************/
  7. #include 
  8. #include 
  9. #include 
  10. #include 
  11. #include "cuda_class.h"
  12. /************************************************************************/
  13. /* HelloCUDA                                                            */
  14. /************************************************************************/
  15. int main(int argc, char* argv[])
  16. {
  17.     cuda_class cudaA;
  18.     if(!cudaA.InitCUDA()) {
  19.         return 0;
  20.     }
  21.     float   host_data[22] = {0};
  22.     float   host_result[11] ={0};
  23.     for (int i = 0; i 
  24.     {
  25.         host_data[i] = i;
  26.     }
  27.     cudaA.InitTexture(host_data, 11, 2);
  28.     cudaA.Malloc垃圾廣告(11);
  29.     unsigned int timer = 0;
  30.     CUT_SAFE_CALL( cutCreateTimer( &timer));
  31.     CUT_SAFE_CALL( cutStartTimer( timer));
  32.     cudaA.DoWork();
  33.     CUDA_SAFE_CALL( cudaThreadSynchronize() );
  34.     CUT_SAFE_CALL( cutStopTimer( timer));
  35.     printf("Processing time: %f (ms)\n", cutGetTimerValue( timer));
  36.     CUT_SAFE_CALL( cutDeleteTimer( timer));
  37.     cudaA.TranslateResult(host_result);
  38.     cudaA.ReleaseMem();
  39.     for (int i = 0; i 
  40.     {
  41.         printf("%f \n", host_result[i]);
  42.     }
  43.     CUT_EXIT(argc, argv);
  44.     return 0;
  45. }


2. 兩個class 檔案

  1. /********************************************************************
  2. *  cuda_class.h
  3. *  This is a example of the CUDA program.
  4. *  author: zhao.kaiyong(at)gmail.com
  5. *  http://www.comp.hkbu.edu.hk/~kyzhao/
  6. *********************************************************************/
  7. #ifndef __CUDA_CLASS_H__
  8. #define __CUDA_CLASS_H__
  9. #include 
  10. class cuda_class
  11. {
  12. public:
  13.     cuda_class(void);
  14.     ~cuda_class(void);
  15.     int InitCUDA();
  16.     int Malloc垃圾廣告(int len);
  17.     int InitTexture(float* init_data,int w, int h);
  18.     int DoWork();
  19.     int TranslateResult(float* out_data);
  20.     int ReleaseMem();
  21.     int ReleaseTex();
  22. private:
  23.     float   *device_result;
  24.     cudaArray* device_tex;
  25.     int m_ret_len;
  26. };
  27. #endif // __CUDA_CLASS_H__
  1. /********************************************************************
  2. *  cuda_class.cu
  3. *  This is a example of the CUDA program.
  4. *  author: zhao.kaiyong(at)gmail.com
  5. *  http://www.comp.hkbu.edu.hk/~kyzhao/
  6. *********************************************************************/
  7. #include "cuda_class.h"
  8. #include 
  9. #include 
  10. #include 
  11. #include 
  12. texture<float, 2, cudaReadModeElementType> tex;
  13. /************************************************************************/
  14. /* Example                                                              */
  15. /************************************************************************/
  16. __global__ static void HelloCUDA(float* result, int num)
  17. {
  18.     int i = 0;
  19.     for(i = 0; i 
  20.         result[i] = tex2D(tex,(float) i,0) + tex2D(tex,(float)i,1);
  21.     }
  22. }
  23. cuda_class::cuda_class(void)
  24. {
  25. }
  26. cuda_class::~cuda_class(void)
  27. {
  28. }
  29. int cuda_class::InitCUDA()
  30. {
  31.     /************************************************************************/
  32.     /* Init CUDA                                                            */
  33.     /************************************************************************/
  34. #if __DEVICE_EMULATION__
  35.     return true;
  36. #else
  37.     int count = 0;
  38.     int i = 0;
  39.     cudaGetDeviceCount(&count);
  40.     if(count == 0) {
  41.         fprintf(stderr, "There is no device.\n");
  42.         return false;
  43.     }
  44.     for(i = 0; i 
  45.         cudaDeviceProp prop;
  46.         if(cudaGetDeviceProperties(∝, i) == cudaSuccess) {
  47.             if(prop.major >= 1) {
  48.                 break;
  49.             }
  50.         }
  51.     }
  52.     if(i == count) {
  53.         fprintf(stderr, "There is no device supporting CUDA.\n");
  54.         return false;
  55.     }
  56.     cudaSetDevice(i);
  57.     printf("CUDA initialized.\n");
  58.     return true;
  59. #endif
  60. }
  61. int cuda_class::Malloc垃圾廣告(int len)
  62. {
  63.     m_ret_len = len;
  64.     CUDA_SAFE_CALL( cudaMalloc((void**) &device_result, sizeof(float) * m_ret_len));
  65.     return 1;
  66. }
  67. int cuda_class::DoWork()
  68. {
  69.     HelloCUDA<<<1, 1, 0>>>(device_result, m_ret_len);
  70.     CUT_CHECK_ERROR("Kernel execution failed\n");
  71.     return 1;
  72. }
  73. int cuda_class::TranslateResult(float * out_data)
  74. {
  75.     CUDA_SAFE_CALL( cudaMemcpy(out_data, device_result, sizeof(float) * m_ret_len, cudaMemcpyDeviceToHost));
  76.     return 1;
  77. }
  78. int cuda_class::ReleaseMem()
  79. {
  80.     CUDA_SAFE_CALL( cudaFree(device_result));
  81.     CUDA_SAFE_CALL( cudaFreeArray(device_tex));
  82.     CUDA_SAFE_CALL( cudaUnbindTexture(tex));
  83.     return 1;
  84. }
  85. int cuda_class::InitTexture(float* init_data, int w, int h)
  86. {
  87.     cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
  88.     CUDA_SAFE_CALL( cudaMallocArray(&device_tex, &channelDesc, w, h));
  89.     CUDA_SAFE_CALL( cudaMemcpyToArray(device_tex, 0, 0, init_data, sizeof(float)* w*h , cudaMemcpyHostToDevice));
  90.     CUDA_SAFE_CALL( cudaBindTextureToArray(tex, device_tex, channelDesc));
  91.     return 1;
  92. }

這裡需要做個解釋,現在CUDA的cu檔案是可以直接按照c++方式編譯的,這個得在nvcc的編譯選項裡面可以設定為--host-compilation C++,在預設的情況下,以前2.0版本以前都是按照C格式編譯檔案,現在都是會按照C++方式編譯檔案,所以這裡可以直接用cu檔案來寫Class。

在編譯的時候,需要加上/MTd(debug), /MT(Release)版本,這裡是告訴xcompiler選項,支援C++的runtime庫,不然會有一些lib 訪問衝突。

下面是下載整個工程的下載連線:不過release版本編譯選項沒設定,這就留給大家自己熟悉環境吧~~不要問我怎麼設定……
http://download.csdn.net/user/OpenHero
cuda_cpp_class_texture_demo.rar

應該對C++和texture比較迷茫的朋友有一些幫忙.
原文

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22785983/viewspace-619849/,如需轉載,請註明出處,否則將追究法律責任。

相關文章