30.04.2014 Views

NVIDIA CUDA 计算统一设备架构

NVIDIA CUDA 计算统一设备架构

NVIDIA CUDA 计算统一设备架构

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

中<br />

的<br />

个<br />

、cuMemAllocPitch() 分<br />

线 性 存 储 器 。 放<br />

节<br />

、<strong>CUDA</strong><br />

数<br />

int offset = 0;<br />

int i;<br />

cuParamSeti(cuFunction, offset, i);<br />

offset += sizeof(i);<br />

float f;<br />

cuParamSetf(cuFunction, offset, f);<br />

offset += sizeof(f);<br />

char data[32];<br />

cuParamSetv(cuFunction, offset, (void*)data, sizeof(data));<br />

offset += sizeof(data);<br />

cuParamSetSize(cuFunction, offset);<br />

cuFuncSetSharedSize(cuFunction, numElements * sizeof(float));<br />

cuLaunchGrid(cuFunction, gridWidth, gridHeight);<br />

4.5.3.6 存 储 器 管 理<br />

或<br />

分<br />

释<br />

CUdeviceptr devPtr;<br />

cuMemAlloc(&devPtr, 256 * sizeof(float));<br />

:<br />

// host code<br />

CUdeviceptr devPtr;<br />

int pitch;<br />

cuMemAllocPitch(&devPtr, &pitch,<br />

因<br />

浮 点 元 素 的 数 组<br />

width * sizeof(float), height, 4);<br />

cuModuleGetFunction(&cuFunction, cuModule, “myKernel”);<br />

cuFuncSetBlockShape(cuFunction, 512, 1, 1);<br />

cuParamSeti(cuFunction, 0, devPtr);<br />

cuParamSetSize(cuFunction, sizeof(devPtr));<br />

cuLaunchGrid(cuFunction, 100, 1);<br />

// device code<br />

__global__ void myKernel(float* devPtr)<br />

{<br />

for (int r = 0; r < height; ++r) {<br />

float* row = (float*)((char*)devPtr + r * pitch);<br />

for (int c = 0; c < width; ++c) {<br />

float element = row[c];<br />

}<br />

}<br />

}<br />

<strong>CUDA</strong> 数<br />

创<br />

销<br />

cuMemAlloc() 用 使 可<br />

cuMemAllocPitch()<br />

cuMemFree() 性 存 储 器 , 并 使 用 线 配<br />

256 的 代 码 示 例 在 线 性 存 储 器 中 分 配 了 一 个 包 含 面 下<br />

建 议 在 分 配 二 维 数 组 时 使<br />

用 cudaMallocPitch(),<br />

5.1.2.1 能 确 保 合 理 填 充 已 分 配 的 存 储 器 , 满 足 第 它 为<br />

绍 的 对 齐 要 求 , 从 而 确 保 访 问 行 地 址 或 执 行 二 维 数 组 与 设 备 存 储 器 的 其 他 区 域 之 间 的 复 制 ( 使 用<br />

cudaMemcpy2D()) 时 获 得 最 优 性 能 。 所 返 回 的 间 距 ( 或 步 幅 ) 必 须 用 于 访 问 数 组 元 素 。 以 下 代 码 示 例 将 分 介<br />

widthxheight 个 一 配<br />

维 浮 点 值 数 组 , 并 显 示 如 何 在 设 备 代 码 中 循 环 遍 历 数 组 元 素 : 二<br />

。 毁<br />

的<br />

数<br />

位<br />

浮 点 组 件 : 的<br />

<strong>CUDA</strong>_ARRAY_DESCRIPTOR desc;<br />

desc.Format = CU_AD_FORMAT_FLOAT;<br />

desc.NumChannels = 1;<br />

desc.Width = width;<br />

desc.Height = height;<br />

CUarray cuArray;<br />

cuArrayCreate(&cuArray, &desc);<br />

分<br />

组<br />

32 <strong>CUDA</strong> 编<br />

cuArrayCreate() 使 用 是 组<br />

cuArrayDestroy() , 使 用 的 建<br />

配 的 线 性 存 储 器<br />

widthxheight 代 码 示 例 分 配 了 一 个 下 以<br />

<strong>CUDA</strong><br />

32 包 含 一 个 , 组<br />

<strong>CUDA</strong>_MEMCPY2D copyParam;<br />

memset(&copyParam, 0, sizeof(copyParam));<br />

存 储 器 : 性 线 的 配<br />

copyParam.dstMemoryType = CU_MEMORYTYPE_ARRAY;<br />

copyParam.dstArray = cuArray;<br />

copyParam.srcMemoryType = CU_MEMORYTYPE_DEVICE;<br />

cuMemAlloc() 手 册 列 举 了 用 于 在 考 参<br />

2.0 南 , 版 本 指 程<br />

<strong>CUDA</strong> 之 间 复 制 存 储 器 的 所 有 函 数 。 下 面 的 代 码 示 例 将 二 维 数 组 复 制 到 之 前 代 码 示 例 中 分 配 的 组 数

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!