0_Simple__simplePitchLinearTexture
对比设备线性二维数组和 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的更多相关文章
随机推荐
- PHP CURL HTTPS内存泄露问题
还原场景:通过一直运行脚本,向微信用户发送模板消息,发现运行了一段时间,内存就爆了,然后立马看了一下代码,发现跟其他的消息逻辑一模一样,唯一不一样的就是请求了微信的接口:然后继续开始找问题,发现当时使 ...
- java设计模式--创建型模式(一)
2016-04-24 10:10:34 创建型模式:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式 注意:工厂模式可以分为三类: 1)简单工厂模式(Simple Factory) 2)工厂 ...
- Spark的启动进程详解
Master和Worker是执行任务之前存在的进程 (类似于公司) Driver和Excutor是任务执行之后存在的进程(类似于公司接到项目后才成立的项目小组) 启动步骤: 启动Master资源管理进 ...
- 03.将MPP部署到开发板上
转载侵删 在一般的嵌入式开发中,只要将uboot,kernel,rootfs下载到开发板上,就可以进行程序开发了.但是海思又进一步的把一些常用视频编解码算法等封装到MPP平台中,进一步简化了工程师的开 ...
- maven 知识点1
在POM 4中,dependency 中还引入了 scope,它主要管理依赖的部署.目前 scope 可以使用5个值: compile,缺省值,适用于所有阶段,会随着项目一起发布. provided, ...
- Microsoft Dynamics CRM 4.0导入组织(Import Organization)时间过长的原因总结
952934 How to move the Microsoft Dynamics CRM 4.0 deployment http://support.microsoft.com/default ...
- GitHub + circleCI 自动构建/自动部署 应用
GitHub + circleCI 自动构建/自动部署, 这里略过了单元测试,以部署 laravel 应用为例子 比起 gitlab + ansible + genkins 操作起来节省了很多硬件资源 ...
- 10个CSS+HOVER 的创意按钮
CSS hover 样式很简单,但是想创造出有意思.实用.有创意性的特效是很考验设计师的创意能力,所以设计达人每隔一段时间都会分享一些与鼠标点击.悬停的相关特效,以便大家获得更好的创造灵感. 今天我们 ...
- OpenFileDialog 打开快捷方式时,返回的是快捷方式引用的路径,而不是快捷方式(.lnk)自身的路径
OpenFileDialog 打开 .lnk 文件. OpenFileDialog 有个DereferenceLinks 属性:获取或设置一个值,该值指示文件对话框是返回快捷方式引用的文件的位置,还是 ...
- java 静态导入