c++ - 从 cuda 3D 内存复制到线性内存 : copied data is not where I expected

标签 c++ cuda

这是我的问题:

我的设备上有一个 float3 的 3D 数组:

int size[3] = {416,464,512};
cudaExtent extent = make_cudaExtent(size[0]*sizeof(float3),size[1],size[2]);
cudaPitchedPtr renderedVolume;
int ret = cudaMalloc3D(&renderedVolume, extent);
size_t pitch = renderedVolume.pitch; //pitch = 5,120
size_t slicePitch = pitch * size[1]; //slicePitch = 2,375,680

然后我使用它并使其充满出色的数据。

之后我希望将它复制到我主机上的一维线性内存中:

float *host_memory = (float*)malloc(size[0]*size[1]*size[2]*sizeof(float3));
cudaMemcpy3DParms p = {0};
p.srcPtr = renderedVolume;
p.dstPtr = make_cudaPitchedPtr(host_memory,size[0]*sizeof(float3),size[0],size[1]); 
p.extent = make_cudaExtent(size[0]*sizeof(float3),size[1],size[2]);
p.srcPos = make_cudaPos(0,0,0);
p.dstPos = make_cudaPos(0,0,0);
p.kind=cudaMemcpyDeviceToHost;
cudaMemcpy3D(&p);

我正在将 host_memory 中的结果与我最初写入 tu renderedVolume (my_data) 的数据以及我在 3Dmemory 中读取的数据进行比较>,逐片:

float* test1 = (float*)malloc(size[0]*size[1]*sizeof(float3));
cudaMemcpy(test1, myData, size[0]*size[1]*sizeof(float3) , cudaMemcpyDeviceToHost);
float* test2 = (float*)malloc(size[0]*size[1]*sizeof(float3));
cudaMemcpy(test2,(char*)renderedVolume.ptr + slicePitch * i,size[0]*size[1]*sizeof(float3), cudaMemcpyDeviceToHost);

问题:

  • 第一个切片 (i=0) 没问题,我在 host_memory、test1test2 中有相同的数据。
  • 在第二个切片中,我在 test1test2 中有相同的数据。但是,我应该在 host_memory+579072 中找到此数据(=每个切片的 float 数,以及目标倾斜指针的 heigth*pitch)我在 host_memory+577504 中找到了它。它偏离了 1568 字节,这与我所知道的没有任何关系,这就是为什么如果你们中的任何人知道我的代码中可能存在什么问题,我将不胜感激?

最佳答案

这是为从未回答列表中删除此问题而提供的迟到答案。

下面,我提供了一个完整的代码来展示如何分配 3D内存 cudaMalloc3D , 移动主机分配1D内存到3D设备内存 cudaMemcpy3D ,对 3D 执行一些操作设备数据由 test_kernel_3D __global__函数并移动 3D结果数据返回1D主机内存,又是cudaMemcpy3D .

__global__函数 test_kernel_3D3D 的每个元素进行平方设备内存。特别是,2D 的每个线程网格负责执行 for沿着“深度”维度循环。

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

#define BLOCKSIZE_x 16
#define BLOCKSIZE_y 16

#define N 128
#define M 64
#define W 16

/*****************/
/* CUDA MEMCHECK */
/*****************/
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline 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) { getch(); exit(code); }
    }
}

/*******************/
/* iDivUp FUNCTION */
/*******************/
int iDivUp(int a, int b){ return ((a % b) != 0) ? (a / b + 1) : (a / b); }

/******************/
/* TEST KERNEL 3D */
/******************/
__global__ void test_kernel_3D(cudaPitchedPtr devPitchedPtr)
{
    int tidx =  blockIdx.x*blockDim.x+threadIdx.x;
    int tidy =  blockIdx.y*blockDim.y+threadIdx.y;

    char* devPtr = (char*) devPitchedPtr.ptr;
    size_t pitch = devPitchedPtr.pitch;
    size_t slicePitch = pitch * N;

    for (int w = 0; w < W; w++) {
        char* slice = devPtr + w * slicePitch;
        float* row = (float*)(slice + tidy * pitch);
        row[tidx] = row[tidx] * row[tidx];
    }
}

/********/
/* MAIN */
/********/
int main()
{
    float a[N][M][W];

    for (int i=0; i<N; i++)
        for (int j=0; j<M; j++) 
            for (int w=0; w<W; w++) {
                a[i][j][w] = 3.f;
                //printf("row %i column %i depth %i value %f \n",i,j,w,a[i][j][w]);
            }

    // --- 3D pitched allocation and host->device memcopy
    cudaExtent extent = make_cudaExtent(M * sizeof(float), N, W);

    cudaPitchedPtr devPitchedPtr;
    gpuErrchk(cudaMalloc3D(&devPitchedPtr, extent));

    cudaMemcpy3DParms p = { 0 };
    p.srcPtr.ptr = a;
    p.srcPtr.pitch = M * sizeof(float);
    p.srcPtr.xsize = M;
    p.srcPtr.ysize = N;
    p.dstPtr.ptr = devPitchedPtr.ptr;
    p.dstPtr.pitch = devPitchedPtr.pitch;
    p.dstPtr.xsize = M;
    p.dstPtr.ysize = N;
    p.extent.width = M * sizeof(float);
    p.extent.height = N;
    p.extent.depth = W;
    p.kind = cudaMemcpyHostToDevice;
    gpuErrchk(cudaMemcpy3D(&p));

    dim3 GridSize(iDivUp(M,BLOCKSIZE_x),iDivUp(N,BLOCKSIZE_y));
    dim3 BlockSize(BLOCKSIZE_y,BLOCKSIZE_x);
    test_kernel_3D<<<GridSize,BlockSize>>>(devPitchedPtr);
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());

    p.srcPtr.ptr = devPitchedPtr.ptr;
    p.srcPtr.pitch = devPitchedPtr.pitch;
    p.dstPtr.ptr = a;
    p.dstPtr.pitch = M * sizeof(float); 
    p.kind = cudaMemcpyDeviceToHost;
    gpuErrchk(cudaMemcpy3D(&p));

    for (int i=0; i<N; i++) 
        for (int j=0; j<M; j++) 
            for (int w=0; w<W; w++)
                printf("row %i column %i depth %i value %f\n",i,j,w,a[i][j][w]);

    getch();
    return 0;
}

关于c++ - 从 cuda 3D 内存复制到线性内存 : copied data is not where I expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16107480/

相关文章:

CUDA:我可以知道我是否有全局内存合并吗?

c - 从 Fortran 调用 C 函数时指针被屏蔽

c++ - 在每个应用程序的事件循环迭代中执行槽

c++ - 如何创建一个添加数字的模板函数

image - Parallelizeable jpeg like compression using only DCT, run length encoding stages, 什么样的压缩/性能可能?

c++ - 如何在 nsight eclipse 版本中查看变量值

c++ - 如何在不同的线程上调用 glReadPixels?

c++ - c++中设置任意类型动态数组长度的泛型方法

c++ - 我收到 "undefined reference"错误,我不明白为什么 (C++ OO)

cuda - cudaAddressModeBorder 是否适用于非标准化坐标?