Can we copy “normal” GPU memory into “unified” memory?

We have two GPU memories, one of which is allocated cuMallocas regular device memory, the other is allocated cuMallocManagedas a single memory. Can I copy between them? and if we use the driver API, in which direction should I use?

float* normalMem, unifiedMem;
cuMalloc(&normalMem, 100);
cuMallocManaged(&unifiedMem, 100);
cuMemcpyD2D(unifiedMem, normalMem, 100); // ? D2D? or D2H? or else?
+4
source share
1 answer

Yes you can . Look at the following code for example.

  • He declared the normal pointer a managed pointer and a host pointer of all 100 of them float.
  • It then initializes the values ​​in the host pointer, and then copies the values ​​with cudaMemCpy to a regular pointer.
  • , , .

,

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

.

+1

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


All Articles