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的更多相关文章
随机推荐
- 不要使用 Dispatcher.Invoke,因为它可能在你的延迟初始化 Lazy 中导致死锁
WPF 中为了 UI 的跨线程访问,提供了 Dispatcher 线程模型.其 Invoke 方法,无论在哪个线程调用,都可以让传入的方法回到 UI 线程. 然而,如果你在 Lazy 上下文中使用了 ...
- lets encrypt 申请nginx 泛域名
1. 安装certbot工具 wget https://dl.eff.org/certbot-auto chmod a+x ./certbot-auto 2. 申请通配符域名 ./certbot-au ...
- php过滤html标签截取部分内容
<?php $str = '<span>fdsfsdf</span><a href="#">href</a>'; echo h ...
- juc包下的集合类
import java.util.Iterator;import java.util.concurrent.CopyOnWriteArrayList; /** * 集合在多线程中同步的方式: * 1. ...
- azkaban:java任务调度系统
github:https://github.com/azkaban/azkaban 官方网站:https://azkaban.github.io/ 好的介绍文章:http://srcct.com/20 ...
- java 方向术语
缩写 英文 中文意思 POJO Plain Ordinary Java Object 简单的Java对象 slf4j Simple Logging Facade for Java 简单日志门面,跟 C ...
- mysql 存储过程知识点
一.创建存储过程 create procedure sp_name() begin ......... end 二.调用存储过程 1.基本语法:call sp_name(): 注意:存储过程名称后面必 ...
- 异步Socket服务器与客户端
本文灵感来自Andre Azevedo 在CodeProject上面的一片文章,An Asynchronous Socket Server and Client,讲的是异步的Socket通信. S ...
- 关于 ake sure class name exists, is public, and has an empty constructor that is public
解决方法:自定义的fragment最好有一个Public的参数为空的构造函数,若需要传入一个参数,可以使用下面的方法 public FileViewFragment(){ } public stati ...
- 多线程执行顺序诡异现象谈,你不知道的pthread_create
引文:学而时习之,不亦说乎.总是忙于具体项目,业务功能的实现:关于编程本身的技能都要有些生疏了,于是就选择了几个专题做了一次温习,重点放在了多线程和多进程上,跑了一个实例,居然有新的发现: (1)多个 ...