A Texture sample: texture+cpp

http://blog.csdn.net/OpenHero/archive/2008…06/3457704.aspx

  1. class sample.cu
    /************************************************************


/
#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include <cutil.h>
#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 < 22; i++)
{
host_data[i] = i;
}
cudaA.InitTexture(host_data, 11, 2);
cudaA.MallocMemA(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 < 11; i++)
{
printf("%f \n", host_result[i]);
}
CUT_EXIT(argc, argv);
return 0;
}

  1. 两个class 文件
    /************************************************************


/
#ifndef CUDA_CLASS_H
#define CUDA_CLASS_H
#include <cutil.h>
class cuda_class
{
public:
cuda_class(void);
~cuda_class(void);
int InitCUDA();
int MallocMemA(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
/
*****************************************************



/
#include “cuda_class.h”
#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include <cutil.h>
texture<float, 2, cudaReadModeElementType> tex;
/
***************************************************
/
/
Example /
/
************************************************
/
global static void HelloCUDA(float
result, int num)
{
int i = 0;
for(i = 0; i < num; 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 < count; 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::MallocMemA(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();
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;
}
…
http://download.csdn.net/user/OpenHero
cuda_cpp_class_texture_demo.rar