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

38 篇文章 0 订阅
34 篇文章 0 订阅
一直想写一下global内存访问的文章,但是最近的事情太多,哎昨天。。。,今天的一篇paper也被拒了~~哎--
不过收到一个QQ群朋友( from this AVerMedia TECHNOLOGIES(Suzhou), Inc. )求助邮,看得出来项目比较棘手,应该是对CUDA的使用有些不清楚;
晚上熬夜写了一个简单的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 <stdio.h>
  8. #include <stdlib.h>
  9. #include <cuda_runtime.h>
  10. #include <cutil.h>
  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 < 22; i++)
  24.     {
  25.         host_data[i] = i;
  26.     }
  27.     cudaA.InitTexture(host_data, 11, 2);
  28.     cudaA.MallocMemA(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 < 11; 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 <cutil.h>
  10. class cuda_class
  11. {
  12. public:
  13.     cuda_class(void);
  14.     ~cuda_class(void);
  15.     int InitCUDA();
  16.     int MallocMemA(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 <stdio.h>
  9. #include <stdlib.h>
  10. #include <cuda_runtime.h>
  11. #include <cutil.h>
  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 < num; 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 < count; 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::MallocMemA(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比较迷茫的朋友有一些帮忙.


rel="File-List" href="file:///C:%5CDOCUME%7E1%5Ckelvin%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5Ckelvin%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5Ckelvin%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值