CUDA Texture纹理存储器 示例程序
CUDA Texture纹理存储器 示例程序
纹理存储器非常适合实现图像处理和查找表,对大量数据的随机访问或非随机访问也有良好的加速效果。第一次接触纹理存储器,写了以下一个小程序。
相关阅读:
Ubuntu 11.10 上安装CUDA开发环境
Ubuntu 11.04 安装 NVIDIA CUDA 4.0 RC2 见
- /*
- * Copyright 徐洪志(西北农林科技大学.信息工程学院). All rights reserved.
- * Data: 2012-4-20
- */
- //
- // 此程序是演示了1D和2D纹理存储器的使用
- #include <stdio.h>
- #include <cutil_inline.h>
- #include <iostream>
- using namespace std;
- texture<float> texRef1D; // 1D texture
- texture<float, 2> texRef2D; // 2D texture
- // 1D 纹理操作函数
- __global__ void Texture1D(float *dst, int w, int h)
- {
- int x = threadIdx.x + blockIdx.x * blockDim.x;
- int y = threadIdx.y + blockIdx.y * blockDim.y;
- int offset = x + y * blockDim.x * gridDim.x;
- if(x<w && y<h)
- dst[offset] = tex1Dfetch(texRef1D, offset);
- }
- // 2D 纹理操作函数
- __global__ void Texture2D(float *dst, int w, int h)
- {
- int x = threadIdx.x + blockIdx.x * blockDim.x;
- int y = threadIdx.y + blockIdx.y * blockDim.y;
- int offset = x + y * blockDim.x * gridDim.x;
- dst[offset] = tex2D(texRef2D, x, y);
- }
- int main(int argc, char **argv)
- {
- CUT_DEVICE_INIT(argc, argv); // 启动 CUDA
- /// 1D 纹理内存
- cout << "1D texture" << endl;
- float *host1D = (float*)calloc(10, sizeof(float)); // 内存原数据
- float *hostRet1D = (float*)calloc(10, sizeof(float));// 内存保存返回数据
- float *dev1D, *devRet1D; // 显存数据
- int i;
- cout << " host1D:" << endl;
- for(i = 0; i < 10; ++i) // 初始化内存原数据
- {
- host1D[i] = i * 2;
- cout << " " << host1D[i] << " ";
- }
- cutilSafeCall( cudaMalloc((void**)&dev1D, sizeof(float)*10)); // 申请显存空间
- cutilSafeCall( cudaMalloc((void**)&devRet1D, sizeof(float)*10));
- cutilSafeCall( cudaMemcpy(dev1D, host1D, sizeof(float)*10, cudaMemcpyHostToDevice)); // 将内存数据拷贝入显存
- cutilSafeCall( cudaBindTexture(NULL, texRef1D, dev1D, sizeof(float)*10)); // 将显存数据和纹理绑定
- Texture1D<<<10, 1>>>(devRet1D, 10, 1); // 运行1D纹理操作函数
- cutilSafeCall( cudaMemcpy(hostRet1D, devRet1D, sizeof(float)*10, cudaMemcpyDeviceToHost)); // 将显存数据拷贝入内存
- // 打印内存数据
- cout << endl << " hostRet1D:" << endl;
- for(i = 0; i < 10; ++i)
- cout << " " << hostRet1D[i] << " ";
- cutilSafeCall( cudaUnbindTexture(texRef1D)); // 解绑定
- cutilSafeCall( cudaFree(dev1D)); // 释放显存空间
- cutilSafeCall( cudaFree(devRet1D));
- free(host1D); // 释放内存空间
- free(hostRet1D);
- /// 2D 纹理内存
- cout << endl << "2D texture" << endl;
- int width = 5, height = 3;
- float *host2D = (float*)calloc(width*height, sizeof(float)); // 内存原数据
- float *hostRet2D = (float*)calloc(width*height, sizeof(float)); // 内存返回数据
- cudaArray *cuArray; // CUDA数组
- float *devRet2D; // 显存数据
- int row, col;
- cout << " host2D:" << endl;
- for(row = 0; row < height; ++row) // 初始化内存原数据
- {
- for(col = 0; col < width; ++col)
- {
- host2D[row*width + col] = row + col;
- cout << " " << host2D[row*width + col] << " ";
- }
- cout << endl;
- }
- cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
- cutilSafeCall( cudaMallocArray(&cuArray, &channelDesc, width, height)); // 申请显存空间
- cutilSafeCall( cudaMalloc((void**) &devRet2D, sizeof(float)*width*height));
- cutilSafeCall( cudaBindTextureToArray(texRef2D, cuArray)); // 将显存数据和纹理绑定
- cutilSafeCall( cudaMemcpyToArray(cuArray, 0, 0, host2D, sizeof(float)*width*height, cudaMemcpyHostToDevice)); // 将内存数据拷贝入CUDA数组
- dim3 threads(width, height);
- Texture2D<<<1, threads>>>(devRet2D, width, height); // 运行2D纹理操作函数
- cutilSafeCall( cudaMemcpy(hostRet2D, devRet2D, sizeof(float)*width*height, cudaMemcpyDeviceToHost)); // 将显存数据拷贝入内存
- // 打印内存数据
- cout << " hostRet2D:" << endl;
- for(row = 0; row < height; ++row)
- {
- for(col = 0; col < width; ++col)
- cout << " " << hostRet2D[row*width + col] << " ";
- cout << endl;
- }
- cutilSafeCall( cudaUnbindTexture(texRef2D)); // 解绑定
- cutilSafeCall( cudaFreeArray(cuArray)); // 释放显存空间
- cutilSafeCall( cudaFree(devRet2D));
- free(host2D); // 释放内存空间
- free(hostRet2D);
- CUT_EXIT(argc, argv); // 退出CUDA
- }
评论暂时关闭