在C++ class 中使用CUDA(包含texutre 2d的使用)
晚上熬夜寫了一個簡單的C++ class 封裝CUDA的demo,裡面涉及到了texture的使用,希望對他的CUDA學習有幫助:
下面是三部分的程式碼
1. 呼叫class的sample.cu 檔案這裡沒多少好解釋的,就是直接生產一個cuda的class例項,然後呼叫各個方法
- /********************************************************************
- * sample.cu
- * This is a example of the CUDA program.
- * author: zhao.kaiyong(at)gmail.com
- * http://www.comp.hkbu.edu.hk/~kyzhao/
- *********************************************************************/
- #include
- #include
- #include
- #include
- #include "cuda_class.h"
- /************************************************************************/
- /* HelloCUDA */
- /************************************************************************/
- int main(int argc, char* argv[])
- {
- cuda_class cudaA;
- if(!cudaA.InitCUDA()) {
- return 0;
- }
- float host_data[22] = {0};
- float host_result[11] ={0};
- for (int i = 0; i
- {
- host_data[i] = i;
- }
- cudaA.InitTexture(host_data, 11, 2);
- cudaA.Malloc垃圾廣告(11);
- unsigned int timer = 0;
- CUT_SAFE_CALL( cutCreateTimer( &timer));
- CUT_SAFE_CALL( cutStartTimer( timer));
- cudaA.DoWork();
- CUDA_SAFE_CALL( cudaThreadSynchronize() );
- CUT_SAFE_CALL( cutStopTimer( timer));
- printf("Processing time: %f (ms)\n", cutGetTimerValue( timer));
- CUT_SAFE_CALL( cutDeleteTimer( timer));
- cudaA.TranslateResult(host_result);
- cudaA.ReleaseMem();
- for (int i = 0; i
- {
- printf("%f \n", host_result[i]);
- }
- CUT_EXIT(argc, argv);
- return 0;
- }
2. 兩個class 檔案
- /********************************************************************
- * cuda_class.h
- * This is a example of the CUDA program.
- * author: zhao.kaiyong(at)gmail.com
- * http://www.comp.hkbu.edu.hk/~kyzhao/
- *********************************************************************/
- #ifndef __CUDA_CLASS_H__
- #define __CUDA_CLASS_H__
- #include
- class cuda_class
- {
- public:
- cuda_class(void);
- ~cuda_class(void);
- int InitCUDA();
- int Malloc垃圾廣告(int len);
- int InitTexture(float* init_data,int w, int h);
- int DoWork();
- int TranslateResult(float* out_data);
- int ReleaseMem();
- int ReleaseTex();
- private:
- float *device_result;
- cudaArray* device_tex;
- int m_ret_len;
- };
- #endif // __CUDA_CLASS_H__
- /********************************************************************
- * cuda_class.cu
- * This is a example of the CUDA program.
- * author: zhao.kaiyong(at)gmail.com
- * http://www.comp.hkbu.edu.hk/~kyzhao/
- *********************************************************************/
- #include "cuda_class.h"
- #include
- #include
- #include
- #include
- texture<float, 2, cudaReadModeElementType> tex;
- /************************************************************************/
- /* Example */
- /************************************************************************/
- __global__ static void HelloCUDA(float* result, int num)
- {
- int i = 0;
- for(i = 0; i
- result[i] = tex2D(tex,(float) i,0) + tex2D(tex,(float)i,1);
- }
- }
- cuda_class::cuda_class(void)
- {
- }
- cuda_class::~cuda_class(void)
- {
- }
- int cuda_class::InitCUDA()
- {
- /************************************************************************/
- /* Init CUDA */
- /************************************************************************/
- #if __DEVICE_EMULATION__
- return true;
- #else
- int count = 0;
- int i = 0;
- cudaGetDeviceCount(&count);
- if(count == 0) {
- fprintf(stderr, "There is no device.\n");
- return false;
- }
- for(i = 0; i
- cudaDeviceProp prop;
- if(cudaGetDeviceProperties(∝, i) == cudaSuccess) {
- if(prop.major >= 1) {
- break;
- }
- }
- }
- if(i == count) {
- fprintf(stderr, "There is no device supporting CUDA.\n");
- return false;
- }
- cudaSetDevice(i);
- printf("CUDA initialized.\n");
- return true;
- #endif
- }
- int cuda_class::Malloc垃圾廣告(int len)
- {
- m_ret_len = len;
- CUDA_SAFE_CALL( cudaMalloc((void**) &device_result, sizeof(float) * m_ret_len));
- return 1;
- }
- int cuda_class::DoWork()
- {
- HelloCUDA<<<1, 1, 0>>>(device_result, m_ret_len);
- CUT_CHECK_ERROR("Kernel execution failed\n");
- return 1;
- }
- int cuda_class::TranslateResult(float * out_data)
- {
- CUDA_SAFE_CALL( cudaMemcpy(out_data, device_result, sizeof(float) * m_ret_len, cudaMemcpyDeviceToHost));
- return 1;
- }
- int cuda_class::ReleaseMem()
- {
- CUDA_SAFE_CALL( cudaFree(device_result));
- CUDA_SAFE_CALL( cudaFreeArray(device_tex));
- CUDA_SAFE_CALL( cudaUnbindTexture(tex));
- return 1;
- }
- int cuda_class::InitTexture(float* init_data, int w, int h)
- {
- cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
- CUDA_SAFE_CALL( cudaMallocArray(&device_tex, &channelDesc, w, h));
- CUDA_SAFE_CALL( cudaMemcpyToArray(device_tex, 0, 0, init_data, sizeof(float)* w*h , cudaMemcpyHostToDevice));
- CUDA_SAFE_CALL( cudaBindTextureToArray(tex, device_tex, channelDesc));
- return 1;
- }
這裡需要做個解釋,現在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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [譯]在CUDA C/C++中使用共享儲存器C++
- cuda在ubuntu的安裝使用分享Ubuntu
- Nvidia的CUDA庫現在恢復使用了
- [譯]在CUDA C/C++中如何衡量程式碼效能C++
- Class 的基本使用
- cuda的c++程式C++
- Ubuntu下使用conda在虛擬環境中安裝CUDA、CUDNN及TensorflowUbuntuDNN
- vue 的 class 與 style 使用Vue
- python3 class的使用Python
- C++中的抽象基類(Abstract Base Class)C++抽象
- C++ Templates (2.2 使用Stack類别範本 Use of Class Template Stack )C++
- C++ Templates (2.3 類别範本的區域性使用 Partial Usage of Class Templates)C++
- sqlalchemy在python中的使用(基本使用)一SQLPython
- AspectJ 在 Spring 中的使用Spring
- JSON在Python中的使用JSONPython
- Cordova在Android中的使用Android
- reload在python中的使用Python
- iota 在 Go 中的使用Go
- 關於C++中在模板引數中使用Lambda表示式的問題C++
- Redis在.net中的使用(2).net專案中的Redis使用Redis
- C++中map的使用詳解說明C++
- validator庫在gin中的使用
- Mobx在Flutter中的使用教程Flutter
- Protocol Buffers 在 iOS 中的使用ProtocoliOS
- DNS在架構中的使用DNS架構
- input在python中的使用注意Python
- Mybatis在Spring中的使用(三)MyBatisSpring
- CUTLASS: Fast Linear Algebra in CUDA C++ASTC++
- class dump使用方式和原理
- C++ Empty Class OptimizationC++
- OpenGL Matrix Class (C++)C++
- 使用 Alfred 在 markdown 中優雅的使用圖片Alfred
- C++ instance的使用C++
- Springboot在包含有參構造方法的類中使用@Value註解取值Spring Boot構造方法
- 在Node.js中使用C++模組Node.jsC++
- JSTL的標籤及使用,包含例項JS
- C++/C++11中std numeric limits的使用C++MIT
- 使用C++解析IFC中的構件名稱C++
- Ubuntu上使用QT creator執行cuda程式UbuntuQT