-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnifiedMemoryAccess.cu
More file actions
38 lines (31 loc) · 927 Bytes
/
UnifiedMemoryAccess.cu
File metadata and controls
38 lines (31 loc) · 927 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
33
34
35
36
37
38
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.h>
#include <sys/time.h>
__global__ void vecMultiply(int *arr, int size){
int tid = blockIdx.x * blockDim.x + threadIdx.x;
if(tid<size){
for(int i = 0;i<100000;i++){
*(arr + tid) += 10;
}
}
}
int main(int argc, char *argv[]){
// Initialize
int elementSize = 64;
int threadsPerBlock = 32;
int blockSize = (elementSize+threadsPerBlock-1)/threadsPerBlock;
int *host_input_arr;
cudaMallocManaged((void**)&host_input_arr, sizeof(int) * elementSize);
for(int i = 0;i<elementSize;i++){
host_input_arr[i] = i;
}
vecMultiply<<<blockSize, threadsPerBlock>>>(host_input_arr, elementSize);
cudaDeviceSynchronize();
for(int i = 0;i<elementSize;i++){
printf("%d ", host_input_arr[i]);
}
printf("\n");
cudaFree(host_input_arr);
return 0;
}