-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomp_solver.c~
87 lines (65 loc) · 1.33 KB
/
omp_solver.c~
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#define Tolerance 0.00001
#define TRUE 1
#define FALSE 0
#define N 5000
double ** A;
int initialize (double **A, int n)
{
int i,j;
for (j=0;j<n+1;j++){
A[0][j]=1.0;
}
for (i=1;i<n+1;i++){
A[i][0]=1.0;
for (j=1;j<n+1;j++) A[i][j]=0.0;
}
}
void solve(double **A, int n)
{
int convergence=FALSE;
double diff, tmp;
int i,j, iters=0;
int for_iters;
for (for_iters=1;for_iters<21;for_iters++)
{
diff = 0.0;
for (i=1;i<n;i++)
{
for (j=1;j<n;j++)
{
tmp = A[i][j];
A[i][j] = 0.2*(A[i][j] + A[i][j-1] + A[i-1][j] + A[i][j+1] + A[i+1][j]);
diff += fabs(A[i][j] - tmp);
}
}
iters++;
if (diff/((double)N*(double)N) < Tolerance)
convergence=TRUE;
} /*for*/
}
long usecs (void)
{
struct timeval t;
gettimeofday(&t,NULL);
return t.tv_sec*1000000+t.tv_usec;
}
int main(int argc, char * argv[])
{
int i;
long t_start,t_end;
double time;
A = malloc((N+2) * sizeof(double *));
for (i=0; i<N+2; i++) {
A[i] = malloc((N+2) * sizeof(double));
}
initialize(A, N);
t_start=usecs();
#pragma omp parallel
solve(A, N);
t_end=usecs();
time = ((double)(t_end-t_start))/1000000;
printf("Computation time = %f\n", time);
}