The Unified Memory technique can manage the following kinds of variables:
|
|
Since CUDA programs use arrays (1-dim and 2-dim) frequently, I will show you an example of sharing a global array variable next
__managed__ int x[10]; // Defines global shared array variable __global__ void GPU_func( ) { printf("++ GPU sees x: "); for (int i = 0; i < 10; i++ ) printf("%d ", x[i]); printf("\n"); for (int i = 0; i < 10; i++ ) x[i]+=i; // Update x[i] } int main() { for (int i = 0; i < 10; i++ ) x[i] = 1000+i; GPU_func<<< 1, 1 >>>( ); // Launch kernel on grid cudaDeviceSynchronize(); printf("** CPU sees x: "); for (int i = 0; i < 10; i++ ) printf("%d ", x[i]); printf("\n"); } |
DEMO: /home/cs355001/demo/CUDA/2-unified-mem/shared-global-array.cu
|
Before I show you how to create managed dynamically allocated variables, I will review some relevant C material first.