-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_GPU.cu
More file actions
66 lines (55 loc) · 1.46 KB
/
Copy pathparallel_GPU.cu
File metadata and controls
66 lines (55 loc) · 1.46 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
//#include <mpi.h>
#include <time.h>
#include "config.c"
// function to add the elements of two arrays
// CUDA kernel function to add the elements of two arrays on the GPU
__global__
void add(int n, float *x, float *y)
{
int t = threadIdx.x;
int T = blockDim.x;
for (int i = t; i < n; i += T)
{
y[i] = x[i] + y[i];
}
}
int main()
{
cudaMallocManaged(&x,N*sizeof(*x));
cudaMallocManaged(&y,N*sizeof(*y));
// initialize x and y arrays on the host
for (int i = 0; i < N; i++) {
x[i] = 1.0;
y[i] = 2.0;
}
//=========================================================
// Run kernel on 1M elements on the CPU
//-------------------------------------
start = timeInMilliseconds();
//----------
for (int i = 0; i<rep; i++)
{
add<<<1, 512>>>(N,x,y);
cudaDeviceSynchronize();
}
//-----------
end = timeInMilliseconds();
//-------------------------------------
//=========================================================
cpu_time_used = ((double) (end-start));
// cpu_time_used = 0;
// Check for errors (all values should be 3.0f)
float maxError = 0.0;
for (int i = 0; i < N; i++)
maxError = fmax(maxError, fabs(y[i]-(2.0+rep)));
printf("1\t%f\t%f\n",cpu_time_used,maxError);
// Free memory
cudaFree(x);
cudaFree(y);
return 0;
}