复制cudaMallocPitch分配的内存

use*_*148 5 memory cuda

cudaMemcpy可以用于分配cudaMallocPitch的内存吗?如果没有,你能说出应该使用哪个功能.cudaMallocPitch返回线性内存,所以我想应该使用cudaMemcpy.

tal*_*ies 10

你当然可以cudaMemcpy用来复制音调设备内存,但它更常用cudaMemcpy2D.从主机到设备的倾斜副本的示例如下所示:

#include "cuda.h"
#include <assert.h>

typedef float real;

int main(void)
{

    cudaFree(0); // Establish context

    // Host array dimensions
    const size_t dx = 300, dy = 300; 

    // For the CUDA API width and pitch are specified in bytes
    size_t width = dx * sizeof(real), height = dy;

    // Host array allocation
    real * host = new real[dx * dy];
    size_t pitch1 = dx * sizeof(real);

    // Device array allocation
    // pitch is determined by the API call
    real * device;
    size_t pitch2;
    assert( cudaMallocPitch((real **)&device, &pitch2, width, height) == cudaSuccess );

    // Sample memory copy - note source and destination pitches can be different
    assert( cudaMemcpy2D(device, pitch2, host, pitch1, width, height, cudaMemcpyHostToDevice) == cudaSuccess );

    // Destroy context
    assert( cudaDeviceReset() == cudaSuccess );

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

(注意:未经测试,cavaet emptor和所有......)