We have two GPU memories, one is allocated with cuMalloc
as normal device memory, the other is allocated with cuMallocManaged
as unified memory. Is it possible to copy between them? and if we use driver API, what direction should I use?
float* normalMem, unifiedMem;
cuMalloc(&normalMem, 100);
cuMallocManaged(&unifiedMem, 100);
cuMemcpyD2D(unifiedMem, normalMem, 100); // ? D2D? or D2H? or else?
Yes you can. Look at the following code for instance.
float
.I think that the code is pretty self-explanatory
__global__
void test(float* d_ptr){
for(int i=0;i<100;i++)
printf("%f \n",d_ptr[i]);
printf("\n");
}
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
size_t size = sizeof(float)*100;
float* h_p =(float*) malloc(size);
float* d_p, dm_p ;
cudaMalloc(&d_p,size);
cudaMallocManaged(&dm_p,size);
for(int i=0;i<100;i++)
h_p[i]=2*(float)i;
cudaMemcpy(d_p,h_p,size,cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
cudaMemcpy(dm_p,d_p,size,cudaMemcpyDeviceToDevice);
cudaDeviceSynchronize();
test<<<1,1>>>(dm_p);
cudaDeviceSynchronize();
cudaFree(dm_p);
cudaFree(d_p);
free(h_p);
return 0;
}
Remember to read the Unified Memory access rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With