One of the brand new features introduced by NVIDIA in CUDA 6.0 is Unified Memory. Helping the programmer to reduce the amount of code written, and simplify the code. The Unified Memory helps managing memory, and maximizing data access speed transparently between the CPU and GPU. This is a simple C example, on how to use unified memory, following a question I have seen on different forums.
The wrong code I have recently seen was the following :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
__global__ void printValue( int *value) { printf("value %d",value[0]); } void hostFunction(int *value){ cudaMallocManaged(&value, 2 * sizeof(int)); printValue<<< 1, 1 >>>(value); cudaDeviceSynchronize(); cudaFree(value); } int main() { int *value=(int*)malloc(2*sizeof(int)); value[0]=1; value[1]=2; hostFunction(value); return 0; } |
This will not work, and the returned value will be
1 |
$: 0 |
To be clear, you do not need to allocate memory twice !
The correct code is the following :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
__global__ void printValue( int *value) { printf("value %d",value[0]); } void hostFunction(){ int *value; cudaMallocManaged(&value, 2 * sizeof(int)); value[0]=1; value[1]=2; printValue<<< 1, 1 >>>(value); cudaDeviceSynchronize(); cudaFree(value); } int main() { hostFunction(); return 0; } |
If you want to keep the function and allocate the memory in the main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
__global__ void printValue( int *value) { printf("value %d",value[0]); } void hostFunction(int *value){ value[0]=1; value[1]=2; printValue<<< 1, 1 >>>(value); cudaDeviceSynchronize(); cudaFree(value); } int main() { int *value; cudaMallocManaged(&value, 2 * sizeof(int)); hostFunction(value); return 0; } |
and there you go.
{ 1 } Comments
Fatal error: Uncaught Error: Call to undefined function ereg() in /home/users4/n/noktec/www/noktec/wp-content/themes/barthelme/functions.php:178 Stack trace: #0 /home/users4/n/noktec/www/noktec/wp-content/themes/barthelme/comments.php(34): barthelme_commenter_link() #1 /home/users4/n/noktec/www/noktec/wp-includes/comment-template.php(1554): require('/home/users4/n/...') #2 /home/users4/n/noktec/www/noktec/wp-content/themes/barthelme/single.php(44): comments_template() #3 /home/users4/n/noktec/www/noktec/wp-includes/template-loader.php(106): include('/home/users4/n/...') #4 /home/users4/n/noktec/www/noktec/wp-blog-header.php(19): require_once('/home/users4/n/...') #5 /home/users4/n/noktec/www/noktec/index.php(17): require('/home/users4/n/...') #6 {main} thrown in /home/users4/n/noktec/www/noktec/wp-content/themes/barthelme/functions.php on line 178