-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPageable.cu
43 lines (36 loc) · 1.22 KB
/
Pageable.cu
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
39
40
41
42
43
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.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 = (int*)malloc(sizeof(int) * elementSize);
int *host_output_arr = (int*)malloc(sizeof(int) * elementSize);
int *device_arr;
for(int i = 0;i<elementSize;i++){
host_input_arr[i] = i;
}
cudaMalloc((void**)&device_arr, sizeof(int) * elementSize);
cudaMemcpy(device_arr, host_input_arr, sizeof(int) * elementSize, cudaMemcpyHostToDevice);
vecMultiply<<<blockSize, threadsPerBlock>>>(device_arr, elementSize);
cudaMemcpy(host_output_arr, device_arr, sizeof(int) * elementSize, cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
for(int i = 0;i<elementSize;i++){
printf("%d ", host_output_arr[i]);
}
printf("\n");
cudaFree(device_arr);
free(host_input_arr);
free(host_output_arr);
return 0;
}