我如何使用cudaMemcpy2D()DeviceToHost

use*_*001 2 cuda visual-c++

我是cuda和C ++的新手,似乎无法弄清楚这一点。

我想要做的是将2d数组A复制到设备,然后将其复制回相同的数组B。

我希望B数组具有与A相同的值,但是有些地方我做错了。

CUDA-4.2,针对Win32、64位计算机,NVIDIA Quadro K5000进行编译

这是代码。

void main(){

cout<<"Host main" << endl;

// Host code
const int width = 3;
const int height = 3;
float* devPtr;
float a[width][height]; 

//load and display input array
cout << "a array: "<< endl;
for (int i = 0 ; i < width; i ++)
{
    for (int j = 0 ; j < height; j ++)
    {
        a[i][j] = i + j;
        cout << a[i][j] << " ";

    }
    cout << endl;
}
cout<< endl;


//Allocating Device memory for 2D array using pitch
size_t host_orig_pitch = width * sizeof(float); //host original array pitch in bytes
size_t pitch;// pitch for the device array 

cudaMallocPitch(&devPtr, &pitch, width * sizeof(float), height);

cout << "host_orig_pitch: " << host_orig_pitch << endl;
cout << "sizeof(float): " << sizeof(float)<< endl;
cout << "width: " << width << endl;
cout << "height: " << height << endl;
cout << "pitch:  " << pitch << endl;
cout << endl;

cudaMemcpy2D(devPtr, pitch, a, host_orig_pitch, width, height, cudaMemcpyHostToDevice);

float b[width][height];
//load b and display array
cout << "b array: "<< endl;
for (int i = 0 ; i < width; i ++)
{
    for (int j = 0 ; j < height; j ++)
    {
        b[i][j] = 0;
        cout << b[i][j] << " ";
    }
    cout << endl;
}
cout<< endl;


//MyKernel<<<100, 512>>>(devPtr, pitch, width, height);
//cudaThreadSynchronize();


//cudaMemcpy2d(dst, dPitch,src ,sPitch, width, height, typeOfCopy )
cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width, height, cudaMemcpyDeviceToHost);


// should be filled in with the values of array a.
cout << "returned array" << endl;
for(int i = 0 ; i < width ; i++){
    for (int j = 0 ; j < height ; j++){
        cout<< b[i][j] << " " ;
    }
    cout<<endl;
}

cout<<endl;
system("pause");
Run Code Online (Sandbox Code Playgroud)

}

这是输出。

主机主A阵列0 1 2 1 2 3 2 3 4

host_orig_pitch:12 sizeof(float):4宽度:3高度:3间距:512

b数组:0 0 0 0 0 0 0 0 0

返回的数组0 0 0 1.17549e-038 0 0 0 0 0

按任意键继续 。。。

如果需要更多信息,请告诉我,我将其发布。

任何帮助将不胜感激。

tal*_*ies 5

正如评论中指出的那样,原始发帖人为cudaMemcpy2D通话提供了不正确的参数。传输的width参数始终以字节为单位,因此在上面的代码中:

cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width, height, cudaMemcpyDeviceToHost);
Run Code Online (Sandbox Code Playgroud)

应该

cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width * sizeof(float), height, cudaMemcpyDeviceToHost);
Run Code Online (Sandbox Code Playgroud)

请注意,此答案已添加为社区Wiki,以使该问题脱离未答复列表