二维数组 cudaMallocPitch() 和三维数组 cudaMalloc3D() 的使用
▶ 使用函数 cudaMallocPitch() 和配套的函数 cudaMemcpy2D() 来使用二维数组。C 中二维数组内存分配是转化为一维数组,连贯紧凑,每次访问数组中的元素都必须从数组首元素开始遍历;而 cuda 中这样分配的二维数组内存保证了数组每一行首元素的地址值都按照 256 或 512 的倍数对齐,提高访问效率,但使得每行末尾元素与下一行首元素地址可能不连贯,使用指针寻址时要注意考虑尾部。
// cuda_rumtime_api.h
extern __host__ cudaError_t CUDARTAPI cudaMallocPitch(void **devPtr, size_t *pitch, size_t widthByte, size_t height); extern __host__ cudaError_t CUDARTAPI cudaMemcpy2D(void *dst, size_t dpitch, const void *src, size_t spitch, size_t width, size_t height, enum cudaMemcpyKind kind);
● cudaMAllocPitch() 传入存储器指针 **devPtr,偏移值的指针 *pitch,数组行字节数 widthByte,数组行数 height。函数返回后指针指向分配的内存(每行地址对齐到 AlignByte 字节,为 256B 或 512B),偏移值指针指向的值为该行实际字节数(= sizeof(datatype) * width + alignByte - 1) / alignByte)。
● cudaMemcpy2D() 传入目标存储器的指针 *dst,目标存储器行字节数 dpitch,源存储器指针 *src,源存储器行字节数 spitch,数组行字节数 width,数组行数 height,拷贝方向 kind。这里要求存储器行字节数不小于数组行字节数,多出来的部分就是每行尾部空白部分。
● 整个测试代码。
#include <stdio.h>
#include <malloc.h>
#include <cuda_runtime_api.h>
#include "device_launch_parameters.h" __global__ void myKernel(float* devPtr, int height, int width, int pitch)
{
int row, col;
float *rowHead; for (row = ; row < height; row++)
{
rowHead = (float*)((char*)devPtr + row * pitch); for (col = ; col < width; col++)
{
printf("\t%f", rowHead[col]);// 逐个打印并自增 1
rowHead[col]++;
}
printf("\n");
}
} int main()
{
size_t width = ;
size_t height = ;
float *h_data, *d_data;
size_t pitch; h_data = (float *)malloc(sizeof(float)*width*height);
for (int i = ; i < width*height; i++)
h_data[i] = (float)i; printf("\n\tAlloc memory.");
cudaMallocPitch((void **)&d_data, &pitch, sizeof(float)*width, height);
printf("\n\tPitch = %d B\n", pitch); printf("\n\tCopy to Device.\n");
cudaMemcpy2D(d_data, pitch, h_data, sizeof(float)*width, sizeof(float)*width, height, cudaMemcpyHostToDevice); myKernel << <, >> > (d_data, height, width, pitch);
cudaDeviceSynchronize(); printf("\n\tCopy back to Host.\n");
cudaMemcpy2D(h_data, sizeof(float)*width, d_data, pitch, sizeof(float)*width, height, cudaMemcpyDeviceToHost); for (int i = ; i < width*height; i++)
{
printf("\t%f", h_data[i]);
if ((i + ) % width == )
printf("\n");
} free(h_data);
cudaFree(d_data); getchar();
return ;
}
● 输出结果:
Alloc memory.
Pitch = B Copy to Device.
0.000000 1.000000 2.000000 3.000000 4.000000 5.000000
6.000000 7.000000 8.000000 9.000000 10.000000 11.000000
12.000000 13.000000 14.000000 15.000000 16.000000 17.000000
18.000000 19.000000 20.000000 21.000000 22.000000 23.000000
24.000000 25.000000 26.000000 27.000000 28.000000 29.000000 Copy back to Host.
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000
7.000000 8.000000 9.000000 10.000000 11.000000 12.000000
13.000000 14.000000 15.000000 16.000000 17.000000 18.000000
19.000000 20.000000 21.000000 22.000000 23.000000 24.000000
25.000000 26.000000 27.000000 28.000000 29.000000 30.000000
▶ 使用函数 cudaMalloc3D() 和配套的函数 cudaMemcpy3D() 来使用三维数组。因为涉及的参数较多,需要定义一些用来传参的结构,形式上和二维数组的使用有较大差距,不好看。
● 涉及的相关代码
// driver_types.h
struct cudaArray; // cuda 数组
typedef struct cudaArray * cudaArray_t;// cuda 指针 struct __device_builtin__ cudaPitchedPtr
{
void *ptr; // 实际数组指针(用完后要用 cudaFree() 释放掉)
size_t pitch; // 数组行字节数
size_t xsize; // 数组列数
size_t ysize; // 数组行数
}; struct __device_builtin__ cudaExtent
{
size_t width; // 数组行字节数
size_t height; // 数组行数
size_t depth; // 数组层数
}; struct __device_builtin__ cudaPos
{
size_t x;
size_t y;
size_t z;
}; struct __device_builtin__ cudaMemcpy3DParms
{
cudaArray_t srcArray; // 原数组指针
struct cudaPos srcPos; // 原数组偏移
struct cudaPitchedPtr srcPtr; // ?Pitched source memory address cudaArray_t dstArray; // 目标数组指针
struct cudaPos dstPos; // 目标数组偏移
struct cudaPitchedPtr dstPtr; // ?Pitched destination memory address struct cudaExtent extent; // 数组实际尺寸(去掉对齐用的空白部分)
enum cudaMemcpyKind kind; // 拷贝类型
}; // driver_functions.h
static __inline__ __host__ struct cudaPitchedPtr make_cudaPitchedPtr(void *d, size_t p, size_t xsz, size_t ysz)
{ // 简单生成 cudaPitchedPtr 结构的方法
struct cudaPitchedPtr s; s.ptr = d;
s.pitch = p;
s.xsize = xsz;
s.ysize = ysz; return s;
} static __inline__ __host__ struct cudaPos make_cudaPos(size_t x, size_t y, size_t z)
{ // 简单的生成 cudaPos 结构的方法
struct cudaPos p; p.x = x;
p.y = y;
p.z = z; return p;
} static __inline__ __host__ struct cudaExtent make_cudaExtent(size_t w, size_t h, size_t d)
{ // 简单的生成 cudaExtent 结构的方法
struct cudaExtent e; e.width = w;
e.height = h;
e.depth = d; return e;
} // cuda_runtime_api.h
extern __host__ cudaError_t CUDARTAPI cudaMalloc3D(struct cudaPitchedPtr* pitchedDevPtr, struct cudaExtent extent); extern __host__ cudaError_t CUDARTAPI cudaMemcpy3D(const struct cudaMemcpy3DParms *p);
● 完整的测试程序
#include <stdio.h>
#include <malloc.h>
#include <cuda_runtime_api.h>
#include "device_launch_parameters.h"
#include <driver_functions.h> __global__ void myKernel(cudaPitchedPtr devPitchedPtr, cudaExtent extent)
{
float * devPtr = (float *)devPitchedPtr.ptr;
float *sliceHead, *rowHead;
// 可以定义为 char * 作面、行迁移的时候直接加减字节数,取行内元素的时候再换回 float * for (int z = ; z < extent.depth; z++)
{
sliceHead = (float *)((char *)devPtr + z * devPitchedPtr.pitch * extent.height);
for (int y = ; y < extent.height; y++)
{
rowHead = (float*)((char *)sliceHead + y * devPitchedPtr.pitch);
for (int x = ; x < extent.width / sizeof(float); x++)// extent 存储的是行有效字节数,要除以元素大小
{
printf("\t%f",rowHead[x]);// 逐个打印并自增 1
rowHead[x]++;
}
printf("\n");
}
printf("\n");
}
} int main()
{
size_t width = ;
size_t height = ;
size_t depth = ;
float *h_data; cudaPitchedPtr d_data;
cudaExtent extent;
cudaMemcpy3DParms cpyParm; h_data = (float *)malloc(sizeof(float) * width * height * depth);
for (int i = ; i < width * height * depth; i++)
h_data[i] = (float)i; printf("\n\tAlloc memory.");
extent = make_cudaExtent(sizeof(float) * width, height, depth);
cudaMalloc3D(&d_data, extent); printf("\n\tCopy to Device.\n");
cpyParm = {};
cpyParm.srcPtr = make_cudaPitchedPtr((void*)h_data, sizeof(float) * width, width, height);
cpyParm.dstPtr = d_data;
cpyParm.extent = extent;
cpyParm.kind = cudaMemcpyHostToDevice;
cudaMemcpy3D(&cpyParm); myKernel << <, >> > (d_data, extent);
cudaDeviceSynchronize(); printf("\n\tCopy back to Host.\n");
cpyParm = { };
cpyParm.srcPtr = d_data;
cpyParm.dstPtr = make_cudaPitchedPtr((void*)h_data, sizeof(float) * width, width, height);
cpyParm.extent = extent;
cpyParm.kind = cudaMemcpyDeviceToHost;
cudaMemcpy3D(&cpyParm); for (int i = ; i < width*height*depth; i++)
{
printf("\t%f", h_data[i]);
if ((i + ) % width == )
printf("\n");
if ((i + ) % (width*height) == )
printf("\n");
} free(h_data);
cudaFree(d_data.ptr);
getchar();
return ;
}
● 输出结果:
Alloc memory.
Copy to Device.
0.000000 1.000000
2.000000 3.000000
4.000000 5.000000 6.000000 7.000000
8.000000 9.000000
10.000000 11.000000 12.000000 13.000000
14.000000 15.000000
16.000000 17.000000 18.000000 19.000000
20.000000 21.000000
22.000000 23.000000 Copy back to Host.
1.000000 2.000000
3.000000 4.000000
5.000000 6.000000 7.000000 8.000000
9.000000 10.000000
11.000000 12.000000 13.000000 14.000000
15.000000 16.000000
17.000000 18.000000 19.000000 20.000000
21.000000 22.000000
23.000000 24.000000
二维数组 cudaMallocPitch() 和三维数组 cudaMalloc3D() 的使用的更多相关文章
- c# 基础之数组(包含三维数组)
public enum ChessType { White = , None=, Black=, } class Program { static void Main(string[] args) { ...
- 【opencv】 solvepnp 和 solvepnpRansac 求解 【空间三维坐标系 到 图像二维坐标系】的 三维旋转R 和 三维平移 T 【opencv2使用solvepnp求解rt不准的问题】
参考: pnp问题 与 solvepnp函数:https://www.jianshu.com/p/b97406d8833c 对图片进行二维仿射变换cv2.warpAffine() or 对图片进行二维 ...
- PHP 把MYSQL重复ID 二维数组重组为三维数组
应用场景 MYSQL在使用关联查询时,比如 产品表 与 产品图片表关联,一个产品多张产品图片,关联查询结果如下: $arr=[['id'=>1,'img'=>'img1'],['id'=& ...
- C语言之二维数组
二维数组 还是一个数组,只不过数组中得每一个元素又是一个数组 1). 声明语法 类型 数组名[行][列]; 例: int nums[2][3];//2行3列的二维数组,保存的数据类型是int类型 c ...
- C语言数组篇(五)多级指针和二维数组指针的区别
多级指针 以二级指针为例 二级指针的由来是 指针数组 的指针形式. int *p[10] 读取的顺序是 p[] --> 10个空间的数组 * p[] --> 这10个空间的数组里面存放 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- JavaScript -- 定义二维数组
方法一:直接定义并且初始化,这种遇到数量少的情况可以用var _TheArray = [["0-1","0-2"],["1-1"," ...
- js二维数组定义和初始化的三种方法总结
js二维数组定义和初始化的三种方法总结 方法一:直接定义并且初始化,这种遇到数量少的情况可以用var _TheArray = [["0-1","0-2"],[& ...
- [Swift]多维数组的表示和存储:N维数组映射到一维数组(一一对应)!
数组:有序的元素序列. 若将有限个类型相同的变量的集合命名,那么这个名称为数组名.组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量.用于区分数组的各个元素的数字编号称为下标.数组 ...
随机推荐
- test20190324 树
题意 树(tree.cpp/c/pas) [题目背景] 这道题标算在评测机上的时间约为自己电脑的2/3 [问题描述] [输入格式] 共 n+2 行.第 1 行 1 个数,n. 后面 2-n 行,每行两 ...
- FastAdmin 教程草稿大纲
FastAdmin 教程草稿大纲 计划 FastAdmin 教程大纲 FastAdmin 环境搭建 phpStudy 2018 安装 一键 CRUD 教程 环境变量配置 环境安装 命令行安装 列出所需 ...
- 【转】每天一个linux命令(21):find命令之xargs
原文网址:http://www.cnblogs.com/peida/archive/2012/11/15/2770888.html 在使用 find命令的-exec选项处理匹配到的文件时, find命 ...
- jquery操作select大全详解
每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...
- UOJ 188 【UR #13】Sanrd——min_25筛
题目:http://uoj.ac/problem/188 令 \( s(n,j)=\sum\limits_{i=1}^{n}[min_i>=p_j]f(j) \) ,其中 \( min_i \) ...
- POJ1006——中国剩余定理
题目:http://poj.org/problem?id=1006 中国剩余定理:x= m/mj + bj + aj 讲解:http://www.cnblogs.com/MashiroSky/p/59 ...
- IPv6调用java后端接口报错:java.net.SocketException: Protocol family unavailable
目前需求是java后端的接口需要支持IPv6.先确认linux机器已经绑定了IPv6: CMREAD-SV43 apache-tomcat/bin> ifconfig eth0 Link enc ...
- BaseAction编写:免去一些重复的代码,比如继承ActionSupport和实现ModelDriven接口
1.BaseAction package com.learning.crm.base; import java.lang.reflect.ParameterizedType; import java. ...
- [转]NSIS:常量大全
原文链接 http://www.flighty.cn/html/bushu/20140915_251.html ;轻狂志www.flighty.cn ;运行后会在桌面生成NSIS常量大全.txt文件 ...
- 1016 Phone Bills (25 分)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rul ...