-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmem_copy_gpu.cu
More file actions
34 lines (21 loc) · 765 Bytes
/
Copy pathmem_copy_gpu.cu
File metadata and controls
34 lines (21 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <stdlib.h>
// Kernel to give a value
__global__ void value( int *a ){
*a = 1;
}
// Main program
int main(void){
int *a; // Host memory
int *a_dev; // Device memory
int size = sizeof(int); // size of integer
a = (int *) malloc(size); // Allocate host memory
cudaMalloc( (void**) &a_dev, size); // Allocate device memory
value <<<1,1>>> (a_dev); // Launch kernel on device
// Copy device result back to host
cudaMemcpy( a, a_dev, size, cudaMemcpyDeviceToHost );
printf("%d\n",*a); // Print result
cudaFree(a_dev); // Free device memory
free(a); // Free host memory
return 0;
}