对比设备线性二维数组和 CUDA 二维数组在纹理引用中的效率

▶ 源代码。分别绑定相同大小的设备线性二维数组和 CUDA 二维数组为纹理引用,做简单的平移操作,重复若干次计算带宽和访问速度。

 #include <stdio.h>
#ifdef _WIN32
# define WINDOWS_LEAN_AND_MEAN
# define NOMINMAX
# include <windows.h>
#endif
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <helper_functions.h>
#include <helper_cuda.h> #define NUM_REPS 100 // test 重复次数
#define TILE_DIM 16 // 线程块尺寸 texture<float, , cudaReadModeElementType> texRefPL;
texture<float, , cudaReadModeElementType> texRefArray; __global__ void shiftPitchLinear(float *odata, int pitch, int width, int height, int shiftX, int shiftY)
{
int xid = blockIdx.x * blockDim.x + threadIdx.x;
int yid = blockIdx.y * blockDim.y + threadIdx.y; odata[yid * pitch + xid] = tex2D(texRefPL, (xid + shiftX) / (float)width, (yid + shiftY) / (float)height);
} __global__ void shiftArray(float *odata, int pitch, int width, int height, int shiftX, int shiftY)
{
int xid = blockIdx.x * blockDim.x + threadIdx.x;
int yid = blockIdx.y * blockDim.y + threadIdx.y; odata[yid * pitch + xid] = tex2D(texRefArray, (xid + shiftX) / (float)width, (yid + shiftY) / (float)height);
} bool test()
{
bool result = true;
int i, j, ishift, jshift;
// 数组大小以及 x,y 方向上的偏移量
const int nx = ;
const int ny = ;
const int x_shift = ;
const int y_shift = ;
if ((nx % TILE_DIM) || (ny % TILE_DIM))
{
printf("nx and ny must be multiples of TILE_DIM\n");
return EXIT_FAILURE;
}
dim3 dimGrid(nx / TILE_DIM, ny / TILE_DIM), dimBlock(TILE_DIM, TILE_DIM); cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop); //int devID = findCudaDevice(argc, (const char **)argv);// 使用device 0,不再使用命令行参数进行判断 // 申请内存
float *h_idata = (float *)malloc(sizeof(float) * nx * ny);
float *h_odata = (float *)malloc(sizeof(float) * nx * ny);
float *h_ref = (float *)malloc(sizeof(float) * nx * ny);
for (int i = ; i < nx * ny; ++i)
h_idata[i] = (float)i;
float *d_idataPL;
size_t d_pitchBytes;
cudaMallocPitch((void **)&d_idataPL, &d_pitchBytes, nx * sizeof(float), ny);
cudaArray *d_idataArray;
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
cudaMallocArray(&d_idataArray, &channelDesc, nx, ny);
float *d_odata;
cudaMallocPitch((void **)&d_odata, &d_pitchBytes, nx * sizeof(float), ny); // 拷贝内存(两组)
size_t h_pitchBytes = nx * sizeof(float);
cudaMemcpy2D(d_idataPL, d_pitchBytes, h_idata, h_pitchBytes, nx * sizeof(float), ny, cudaMemcpyHostToDevice);
cudaMemcpyToArray(d_idataArray, , , h_idata, nx * ny * sizeof(float), cudaMemcpyHostToDevice); // 绑定纹理(两组)
texRefPL.normalized = ;
texRefPL.filterMode = cudaFilterModePoint;
texRefPL.addressMode[] = cudaAddressModeWrap;
texRefPL.addressMode[] = cudaAddressModeWrap;
cudaBindTexture2D(, &texRefPL, d_idataPL, &channelDesc, nx, ny, d_pitchBytes); texRefArray.normalized = ;
texRefArray.filterMode = cudaFilterModePoint;
texRefArray.addressMode[] = cudaAddressModeWrap;
texRefArray.addressMode[] = cudaAddressModeWrap;
cudaBindTextureToArray(texRefArray, d_idataArray, channelDesc); // 理论计算结果
for (i = ; i < ny; i++)
{
for (j = ; j < nx; ++j)
h_ref[i * nx + j] = h_idata[(i + y_shift) % ny * nx + (j + x_shift) % nx];
} // 使用线性数组的纹理计算
cudaMemset2D(d_odata, d_pitchBytes, , nx * sizeof(float), ny);
cudaEventRecord(start, );
for (int i = ; i < NUM_REPS; ++i)
shiftPitchLinear << <dimGrid, dimBlock >> > (d_odata, (int)(d_pitchBytes / sizeof(float)), nx, ny, x_shift, y_shift);
cudaEventRecord(stop, );
cudaEventSynchronize(stop);
float timePL;
cudaEventElapsedTime(&timePL, start, stop); // 检查结果
cudaMemcpy2D(h_odata, h_pitchBytes, d_odata, d_pitchBytes, nx * sizeof(float), ny, cudaMemcpyDeviceToHost);
if (!compareData(h_ref, h_odata, nx*ny, 0.0f, 0.15f))
{
printf("\n\t ShiftPitchLinear failed\n");
result = false;
} // 使用 CUDA数组的纹理计算
cudaMemset2D(d_odata, d_pitchBytes, , nx * sizeof(float), ny);
cudaEventRecord(start, );
for (int i = ; i < NUM_REPS; ++i)
shiftArray << <dimGrid, dimBlock >> > (d_odata, (int)(d_pitchBytes / sizeof(float)), nx, ny, x_shift, y_shift);
cudaEventRecord(stop, );
cudaEventSynchronize(stop);
float timeArray;
cudaEventElapsedTime(&timeArray, start, stop); // 检查结果
cudaMemcpy2D(h_odata, h_pitchBytes, d_odata, d_pitchBytes, nx * sizeof(float), ny, cudaMemcpyDeviceToHost);
if (!compareData(h_ref, h_odata, nx*ny, 0.0f, 0.15f))
{
printf("\n\tShiftArray failed\n");
result = false;
} // 计算带宽和读取速度
float bandwidthPL = .f * nx * ny * sizeof(float) / (timePL / .f / NUM_REPS * .e+9f);
float bandwidthArray = .f * nx * ny * sizeof(float) / (timeArray / .f / NUM_REPS * .e+9f);
printf("\n\tBandwidth for pitch linear: %.2f GB/s; for array: %.2f GB/s\n", bandwidthPL, bandwidthArray);
float fetchRatePL = nx * ny / .e+6f / (timePL / 1000.0f / NUM_REPS);
float fetchRateArray = nx * ny / .e+6f / (timeArray / 1000.0f / NUM_REPS);
printf("\n\tTexture fetch rate for pitch linear: %.2f Mpix/s; for array: %.2f Mpix/s\n", fetchRatePL, fetchRateArray); // 回收工作
free(h_idata);
free(h_odata);
free(h_ref);
cudaUnbindTexture(texRefPL);
cudaUnbindTexture(texRefArray);
cudaFree(d_idataPL);
cudaFreeArray(d_idataArray);
cudaFree(d_odata);
cudaEventDestroy(start);
cudaEventDestroy(stop); return result;
} int main(int argc, char **argv)
{
printf("\n\tStart\n");
printf("\n\tFinished, %s\n", test() ? "Passed" : "Failed"); getchar();
return ;
}

▶ 输出结果

    Start

    Bandwidth for pitch linear: 12.58 GB/s; for array: 14.64 GB/s

    Texture fetch rate for pitch linear: 1573.09 Mpix/s; for array: 1829.39 Mpix/s

    Finished, Passed

▶ 涨姿势

● 用到的函数都在以前的,有关线性二维数组和纹理内存使用方法的博客汇总讨论过了。

● 由运行结果可知,使用二维纹理引用时,CUDA 二维数组的效率比线性二维数组更高。

0_Simple__simplePitchLinearTexture的更多相关文章

随机推荐

  1. ElasticSearch(八):springboot集成ElasticSearch集群并使用

    1. 集群的搭建 见:ElasticSearch(七) 2. springboot配置集群 2.1 创建springboot项目,使用idea创建,不过多介绍(创建项目时候建议不要勾选elastics ...

  2. Arpa’s obvious problem and Mehrdad’s terrible solution 思维

    There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious ...

  3. ACM大牛的BLOG(转)

    Twilightgod CUSThttp://blog.csdn.net/twilightgodAekdycoin FZU http://hi.baidu.com/aekdycoinForeverli ...

  4. [转]G++与GCC的区别

    转自http://www.52pojie.cn/thread-58109-1-1.html 误区一:gcc只能编译c代码,g++只能编译c++代码两者都可以,但是请注意:1.后缀为.c的,gcc把它当 ...

  5. vue 文件中的注释

    在每个代码块内,注释的时候,需要使用各自语言的注释语法去注释(HTML.CSS.JavaScript.Jade 等).在文件最顶部注释的时候用HTML的注释语法:<!- 在这里写注释的内容 -- ...

  6. 【Reporting Services 报表开发】— 如何设置报表分页列标题每一页都显示

    一.打开已经开发好的报表ReportTest,选择列组下的高级模式—>选择行组的静态(会关联列标题订单编号),修改下面的属性: 1.Hidden:False 2.FixedData:True 3 ...

  7. AI(四): 微信与luis结合(下)

    LUIS(Language Understanding Intelligent Services)是微软新近推出了的的语义理解服务,可以方便用户进行API调用,创建自己场景的语义理解服务,网址为 ht ...

  8. webSocket支持的浏览器

  9. 【pushlet学习】具体实战

    业务需求: 1. 前端界面需要实时显示空调.照明等设备的状态, 如:空调电压.空调电流.光照强度等,这些量每一个称作一个测点: 2. 不同的用户登录系统后,用户只能看到自己设备的运行状态,而看不到其他 ...

  10. Qt5布局管理(一)——QSplitter分割窗口类

    转载:LeeHDsniper 概述 本文首先通过三个实例分别介绍Qt5的分割窗口QSplitter类.停靠窗口QDockWidget类.堆栈窗体QStackedWidget类,然后介绍布局管理器的使用 ...