Copying memory allocated by cudaMallocPitch

Can cudaMemcpy be used for memory allocated by cudaMallocPitch? If not, can you tell which function should be used. cudaMallocPitch returns linear memory, so I suggest that cudaMemcpy should be used.

+4
source share
1 answer

Of course, you could use cudaMemcpy to copy the scanned memory of the device, but it would be more common to use cudaMemcpy2D . An example of a copied copy from a node to a device will look something like this:

 #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; } 

(note: untested, cavaet emptor and all this .....)

+8
source

Source: https://habr.com/ru/post/1388851/


All Articles