-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poisson-mpi.c
214 lines (175 loc) · 4.39 KB
/
poisson-mpi.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
C-program to solve the two-dimensional Poisson equation on
a unit square using one-dimensional eigenvalue decompositions
and fast sine transforms.
einar m. ronquist
ntnu, october 2000
revised, october 2001
Additions made for running with MPI.
Teodor A. Elstad
Trondheim, april 2013
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <mpi.h>
typedef double Real;
/* function prototypes */
Real *createRealArray (int n);
Real **createReal2DArray (int m, int n);
void fst_(Real *v, int *n, Real *w, int *nn);
void fstinv_(Real *v, int *n, Real *w, int *nn);
void transpose (Real **A, int m, int n, int size, int bb, int bre);
void fillA (Real **A, Real *V, int *re, int *rd, int m, int n, int size);
int main(int argc, char **argv )
{
Real *diag, **A, *z;
Real pi, h, umax, globalumax, emax, globalemax, error, time;
int i, j, n, m, nn, b, re, l, bb, bre, rank, size;
MPI_Init (&argc, &argv);
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
MPI_Comm_size (MPI_COMM_WORLD, &size);
/* the total number of grid points in each spatial direction is (n+1) */
/* the total number of degrees-of-freedom in each spatial direction is (n-1) */
/* this version requires n to be a power of 2 */
if( argc < 2 ) {
printf("need a problem size\n");
return 0;
}
n = atoi(argv[1]);
m = n-1;
nn = 4*n;
h = 1./(Real)n;
pi = 4.*atan(1.);
b = floor(m/size);
re = m - (size-1)*b;
l = b;
bb = b*b;
bre = b*re;
if(rank+1 == size) {
l = re;
bb = bre;
bre = re*re;
}
diag = createRealArray (m);
A = createReal2DArray (l,m);
z = createRealArray (nn);
time = MPI_Wtime();
for (i=0; i < m; i++) {
diag[i] = 2.*(1.-cos((i+1)*pi/(Real)n));
}
for (j=0; j < l; j++) {
for (i=0; i < m; i++) {
// h^2 * f(x,y)
A[j][i] = h*h*5*pi*pi*sin(pi*i*h)*sin(2*pi*(j + rank*b)*h);
}
}
for (j=0; j < l; j++) {
fst_(A[j], &n, z, &nn);
}
transpose(A, l, m, size, bb, bre);
for (i=0; i < l; i++) {
fstinv_(A[i], &n, z, &nn);
}
for (j=0; j < l; j++) {
for (i=0; i < m; i++) {
A[j][i] = A[j][i]/(diag[i]+diag[j + rank*b]);
}
}
for (i=0; i < l; i++) {
fst_(A[i], &n, z, &nn);
}
transpose(A, l, m, size, bb, bre);
for (j=0; j < l; j++) {
fstinv_(A[j], &n, z, &nn);
}
umax = 0.0;
emax = 0.0;
for (j=0; j < l; j++) {
for (i=0; i < m; i++) {
// error = abs( numerical u(x,y) - exact u(x,y) )
error = fabs(A[j][i] - sin(pi*i*h)*sin(2*pi*(j + rank*b)*h));
if (A[j][i] > umax) umax = A[j][i];
if (error > emax) emax = error;
}
}
MPI_Reduce (&umax, &globalumax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
MPI_Reduce (&emax, &globalemax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
if (rank == 0)
{
printf("elapsed: %f\n", MPI_Wtime()-time);
printf ("umax = %e \n",globalumax);
printf ("emax = %e \n",globalemax);
}
MPI_Finalize();
return 0;
}
Real *createRealArray (int n)
{
Real *a;
int i;
a = (Real *)malloc(n*sizeof(Real));
for (i=0; i < n; i++) {
a[i] = 0.0;
}
return (a);
}
Real **createReal2DArray (int n1, int n2)
{
int i, n;
Real **a;
a = (Real **)malloc(n1 *sizeof(Real *));
a[0] = (Real *)malloc(n1*n2*sizeof(Real));
for (i=1; i < n1; i++) {
a[i] = a[i-1] + n2;
}
n = n1*n2;
memset(a[0],0,n*sizeof(Real));
return (a);
}
void transpose (Real **A, int m, int n, int size, int bb, int bre)
{
int se[size], sd[size], re[size], rd[size];
Real *V = createRealArray (n*m);
Real *Vt = createRealArray (n*m);
for (int i = 0; i < size; ++i) {
se[i] = bb;
sd[i] = bb*i;
re[i] = bb;
rd[i] = bb*i;
}
se[size-1] = bre;
re[size-1] = bre;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
V[j + i*m] = A[j][i];
}
}
MPI_Alltoallv(V, se, sd, MPI_DOUBLE, Vt, re, rd, MPI_DOUBLE, MPI_COMM_WORLD);
fillA(A, Vt, re, rd, m, n, size);
}
void fillA (Real **A, Real *V, int *re, int *rd, int m, int n, int size)
{
int rem[size];
for (int i = 0; i < size; ++i) {
rem[i] = re[i]/m;
}
for (int i = 0; i < m; i++) {
int r, k, k1;
r = 0;
k = rd[r] + rem[r]*i;
k1 = k + rem[r] - 1;
for (int j = 0; j < n; j++) {
A[i][j] = V[k];
if(k == k1) {
r++;
k = rd[r] + rem[r]*i;
k1 = k + rem[r] - 1;
} else {
k++;
}
}
}
}