cudaMalloc3D输出倾斜指针中xsize和ysize字段的含义

cudaMalloc3D输出倾斜指针中xsize和ysize字段的含义,cuda,Cuda,警察说 返回的cudaPitchedPtr包含附加字段xsize和 ysize,分配的逻辑宽度和高度 等效于由提供的宽度和高度范围参数 程序员在分配过程中 但是,如果我运行下面的示例 #include<stdio.h> #include<cuda.h> #include<cuda_runtime.h> #include<device_launch_parameters.h> #include<conio.h> #define Nrow

警察说

返回的
cudaPitchedPtr
包含附加字段
xsize
ysize
,分配的逻辑宽度和高度 等效于由提供的宽度和高度范围参数 程序员在分配过程中

但是,如果我运行下面的示例

#include<stdio.h>
#include<cuda.h>
#include<cuda_runtime.h>
#include<device_launch_parameters.h>
#include<conio.h>

#define Nrows 64
#define Ncols 64
#define Nslices 16

/********************/
/* CUDA ERROR CHECK */
/********************/
// --- Credit to http://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api
void gpuAssert(cudaError_t code, char *file, int line, bool abort = true)
{
    if (code != cudaSuccess)
    {
        fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
        if (abort) { exit(code); }
    }
}

void gpuErrchk(cudaError_t ans) { gpuAssert((ans), __FILE__, __LINE__); }

/********/
/* MAIN */
/********/
int main() {

    // --- 3D pitched allocation and host->device memcopy
    cudaExtent extent = make_cudaExtent(Ncols * sizeof(float), Nrows, Nslices);
    cudaPitchedPtr devPitchedPtr;
    gpuErrchk(cudaMalloc3D(&devPitchedPtr, extent));

    printf("xsize = %i; xsize in bytes = %i; ysize = %i\n", devPitchedPtr.xsize, devPitchedPtr.pitch, devPitchedPtr.ysize);

    return 0;
}
因此,
ysize
实际上等于
Nrows
,但
xsize
不同于
Ncols
xsizein bytes/sizeof(float)

您能帮助我理解
cudamaloc3d
cudaPitchedPtr
xsize
ysize
字段的含义吗

事先非常感谢您的帮助


我的系统:
windows10
CUDA 8.0
GT 920M
cc 3.5
xsize
=
Ncols*sizeof(float)

xsize
是分配的逻辑宽度(以字节为单位),与倾斜宽度相反

逻辑宽度=256字节

倾斜宽度=512字节


它相当于(相同)您在分配过程中提供的宽度参数(即,您传递给
make_cudaExtent
的第一个参数)

这个问题的一个非常相关且有效的示例(@jackolanten您自己在另一篇文章中的答案)显示了如何使用
cudamaloc3d

我已经学会了一条经验法则,它以某种方式回答了这个问题,我想与大家分享它:“在CUDA库的上下文中,除非我们使用
CUDA数组
width
表示
nCols*sizeof(datatype)
以字节为单位,而
pitch
表示
width+0
width+一些填充
(取决于阵列和GPU硬件的大小)以字节为单位。“


PS使用CUDA数组时,我们根据一行中的元素数(而不是字节数)(
nCols
)定义
width
).这是因为CUDA阵列负责内部内存布局,我们不需要提供字节数方面的
width

xsize是您请求的节距宽度(以字节为单位)。pitch是以字节为单位的实际节距宽度。ysize是您请求的行数,而不是句子文档中的“至少分配线性内存的宽度*高度*深度字节”和“函数可能会填充分配…”。@talonmies非常感谢您的及时评论。感谢Robert的及时回答。现在我很清楚,
xsize
是以
字节
为单位“测量”的列数。
xsize = 256; xsize in bytes = 512; ysize = 64