forked from NVIDIA-Korea/CUDA
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjacobi.c
69 lines (52 loc) · 1.47 KB
/
jacobi.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
/*
command for pgprof
pgcc -Mprof=lines jacobi.c
pgcollect ./a.out
pgprof ./a.out ./pgprof.out
command for gcc profile for line
gcc -std=c99 -lm -fprofile-arcs -ftest-coverage jacobi.c
./a.out
gcov jacobi.c
cat jacobi.c.gcov
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#define NN 1024
#define NM 1024
double A[NN][NM];
double Anew[NN][NM];
int main(int argc, char** argv){
const int n = NN;
const int m = NM;
const int iter_max = 1000;
const double tol = 1.0e-6;
double error = 1.0;
memset(A, 0, n * m * sizeof(double));
memset(Anew, 0, n * m * sizeof(double));
for (int j = 0; j < n; j++) {
A[j][0] = 1.0;
Anew[j][0] = 1.0;
}
printf("Jacobi relaxation Calculation: %d x %d mesh\n", n, m);
int iter = 0;
while ( error > tol && iter < iter_max )
{
error = 0.0;
for( int j = 1; j < n-1; j++) {
for( int i = 1; i < m-1; i++ ) {
Anew[j][i] = 0.25 * ( A[j][i+1] + A[j][i-1]
+ A[j-1][i] + A[j+1][i]);
error = fmax( error, fabs(Anew[j][i] - A[j][i]));
}
}
for( int j = 1; j < n-1; j++) {
for( int i = 1; i < m-1; i++ ) {
A[j][i] = Anew[j][i];
}
}
if(iter % 100 == 0) printf("%5d, %0.6f\n", iter, error);
iter++;
}
return 0;
}