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

#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说明正常。     懒得读图了。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值