cuda纹理内存使用例子 vs2013 cuda7.5

来源:互联网 发布:50岁知天命是什么意思 编辑:程序博客网 时间:2024/05/21 11:13
#include"cuda_runtime.h"
#include"device_launch_parameters.h"
#include<iostream>
#define __CUDACC__
#define __cplusplus
#include"cuda_texture_types.h"
#include"texture_fetch_functions.h"
using namespace std;


texture<float, 2, cudaReadModeElementType> texRef;


__global__ void resizePic(float * output, int width, int height)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;


float u = x / (float)width;
float v = y / (float)height;


output[y*width + x] = tex2D(texRef, u, v);
}


int main()
{
float * h_data = NULL;//输入数据
unsigned int height = 512, width = 512;
int size = height*width*sizeof(float);
h_data = (float *)malloc(sizeof(float)*size);
for (int i = 0; i < size; i++)
h_data[i] = 24;

unsigned int newheight = 1024, newwidth = 1024;
int newsize = newheight*newwidth*sizeof(float);
float * d_data = NULL;
cudaError_t err = cudaMalloc((void **)&d_data, newsize);//用做输出


//为cuda数组分配内存,并将图像拷贝到内存
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
cudaArray * cuArray;
err = cudaMallocArray(&cuArray, &channelDesc, width, height);
err = cudaMemcpyToArray(cuArray, 0, 0, h_data, size, cudaMemcpyHostToDevice);

//设置纹理参数
texRef.addressMode[0] = cudaAddressModeWrap;
texRef.addressMode[1] = cudaAddressModeWrap;
texRef.filterMode = cudaFilterModeLinear;
texRef.normalized = true;


//纹理和数组绑定
err = cudaBindTextureToArray(&texRef, cuArray, &channelDesc);


//开始计算
dim3 dimBlock(8, 8, 1);
dim3 dimGrid(newwidth / dimBlock.x, newheight / dimBlock.y, 1);

resizePic << <dimGrid, dimBlock >> >(d_data, newwidth, newheight);
cudaThreadSynchronize();


//拷贝结束,并存储
float * h_odata;
h_odata = (float *)malloc(newsize);
err = cudaMemcpy(h_odata, d_data, newsize, cudaMemcpyDeviceToHost);


cudaUnbindTexture(&texRef);
cudaFree(d_data);
cudaFreeArray(cuArray);
free(h_data);
free(h_odata);
system("pause");
return 0;

}

//输入都是24,如果输出也都是24说明正常。     懒得读图了。。

0 0
原创粉丝点击