source: CIVL/examples/messagePassing/diffusion1d_good.cvl@ 4e47f0d

1.23 2.0 main test-branch
Last change on this file since 4e47f0d was 3db34e3, checked in by Stephen Siegel <siegel@…>, 12 years ago

Tiny improvement to diffusion1d_good.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@779 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 8.1 KB
Line 
1/* Composite model of sequential and parallel 1d-diffusion solvers.
2 * Compares the results of the sequential and parallel computation to
3 * determine functional equivalence of the two versions.
4 *
5 * The initial values are taken as inputs. The two global boundary
6 * points are fixed.
7 *
8 * Command line example:
9 *
10 * civl verify -inputNPROCSB=3 -inputNSTEPSB=3 \
11 -inputNXB=6 diffusion1d_good.cvl
12 */
13#include <civlc.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <assert.h>
17#include "mpi.cvl"
18
19// Definitions from programs:
20#define OWNER(index) ((nprocs*(index+1)-1)/nx)
21
22// inputs:
23$input int NPROCS; // number of processes for parallel version
24$input int NPROCSB; // upper bound on nprocs
25$input double K; // k = alpha^2 * dt/(dx^2)
26$input int NSTEPS; // number of time steps
27$input int WSTEP; // write every this many time steps
28$input int NSTEPSB; // upperbound on nsteps
29$input int NX; // global number of discrete spatial points
30$input int NXB; // upper bound on nx
31$input double u_init[NX]; // initial values for temperature (u)
32
33// assumptions:
34$assume 2 <= NX && NX <= NXB;
35$assume K>0 && K<.5;
36$assume NSTEPS >= 1;
37$assume WSTEP >= 1 && WSTEP <= NSTEPS;
38// $assume NX >= NPROCS;
39$assume 1 <= NPROCS && NPROCS <= NPROCSB;
40$assume 1 <= NSTEPS && NSTEPS <= NSTEPSB;
41
42// global variables:
43int output_seq[NX]; // final (color) result of sequential computation
44int output_par[NX]; // final (color) result of parallel computation
45CMPI_Gcomm MPI_GCOMM_WORLD; // the global communicator object
46
47/* Abstract function representing conversion from temperature to color */
48//$abstract int colorOf(double x);
49int colorOf(double x) { return (int)x; }
50
51/* Sequential algorithm: performs simulation storing result of final
52 * time step in output_seq */
53void run_seq() {
54 $scope seq_scope = $here;
55 int nx, nsteps, wstep;
56 double k, *u;
57
58 void init() {
59 int i;
60 int pos = 0; // file position
61
62 // in place of reading these from file:
63 nx = NX;
64 nsteps = NSTEPS;
65 wstep = WSTEP;
66 k = K;
67 printf("Diffusion1d (seq) with nx=%d, k=%f, nsteps=%d, wstep=%d\n",
68 nx, k, nsteps, wstep);
69 assert(nx>=2);
70 assert(k>0 && k<.5);
71 assert(nsteps >= 1);
72 assert(wstep >= 1 && wstep <=nsteps);
73 u = (double*)$malloc(seq_scope, nx*sizeof(double));
74 assert(u);
75 for (i = 0; i < nx; i++) u[i] = u_init[pos++];
76 }
77
78 void write_frame(int time) {
79 for (int i=0; i<nx; i++)
80 output_seq[i] = colorOf(u[i]);
81 }
82
83 void update() {
84 int i;
85 double u_new[nx];
86
87 for (i = 1; i < nx-1; i++)
88 u_new[i] = u[i] + k*(u[i+1] + u[i-1] -2*u[i]);
89 for (i = 1; i < nx-1; i++)
90 u[i] = u_new[i];
91 }
92
93 void _main() {
94 int iter;
95
96 init();
97 write_frame(0);
98 for (iter = 1; iter <= nsteps; iter++) {
99 update();
100 if (iter%wstep==0) write_frame(iter);
101 }
102 free(u);
103 }
104
105 _main();
106}
107
108/* MPI Process in parallel algorithm */
109void MPI_Process (int _rank) {
110 $scope proc_scope = $here; /* this scope */
111 MPI_Comm MPI_COMM_WORLD;
112
113 int nx = -1; /* number of discrete points including endpoints */
114 double k = -1; /* D*dt/(dx*dx) */
115 int nsteps = -1; /* number of time steps */
116 int wstep = -1; /* write frame every this many time steps */
117 double *u; /* temperature function */
118 double *u_new; /* temp. used to update u */
119
120 int nprocs; /* number of processes */
121 int rank; /* the rank of this process */
122 int left; /* rank of left neighbor */
123 int right; /* rank of right neighbor on torus */
124 int nxl; /* horizontal extent of one process */
125 int first; /* global index for local index 0 */
126 int start; /* first local index to update */
127 int stop; /* last local index to update */
128 double *buf; /* temp. buffer used on proc 0 only */
129
130 int firstForProc(int rank) {
131 return (rank*nx)/nprocs;
132 }
133
134 int countForProc(int rank) {
135 int a;
136 int b;
137
138 a = firstForProc(rank+1);
139 b = firstForProc(rank);
140 return a-b;
141 }
142
143 /* init: initializes global variables. */
144 void init() {
145 int i, j;
146 int pos = 0; // position in input file
147
148 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
149 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
150 if (rank == 0) {
151 // in place of reading these from file:
152 nx = NX;
153 k = K;
154 nsteps = NSTEPS;
155 wstep = WSTEP;
156 printf("Diffusion1d (par) with nx=%d, k=%f, nsteps=%d, wstep=%d nprocs=%d\n",
157 nx, k, nsteps, wstep, nprocs);
158 }
159 MPI_Bcast(&nx, 1, MPI_INT, 0, MPI_COMM_WORLD);
160 MPI_Bcast(&k, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
161 MPI_Bcast(&nsteps, 1, MPI_INT, 0, MPI_COMM_WORLD);
162 MPI_Bcast(&wstep, 1, MPI_INT, 0, MPI_COMM_WORLD);
163 // assert(nx>=nprocs);
164 assert(k>0 && k<.5);
165 assert(nx>=2);
166 assert(nsteps>=1);
167 first = firstForProc(rank);
168 nxl = countForProc(rank);
169 if (first == 0 || nxl == 0)
170 left = MPI_PROC_NULL;
171 else
172 left = OWNER(first-1);
173 if (first+nxl >= nx || nxl == 0)
174 right = MPI_PROC_NULL;
175 else
176 right = OWNER(first+nxl);
177 u = (double*)$malloc(proc_scope, (nxl+2)*sizeof(double));
178 assert(u != NULL);
179 u_new = (double*)$malloc(proc_scope, (nxl+2)*sizeof(double));
180 assert(u_new != NULL);
181 if (rank == 0) {
182 buf = (double*)$malloc(proc_scope, (1+nx/nprocs)*sizeof(double));
183 for (i=1; i <= nxl; i++)
184 u[i] = u_init[pos++]; // instead of reading from file
185 for (i=1; i < nprocs; i++) {
186 int count_i = countForProc(i);
187
188 for (j=0; j<count_i; j++)
189 buf[j] = u_init[pos++];
190 MPI_Send(buf, count_i, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
191 }
192 }
193 else {
194 buf = NULL;
195 MPI_Recv(u+1, nxl, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
196 }
197 if (rank == OWNER(0)) {
198 start = 2;
199 }
200 else {
201 start = 1;
202 }
203 if (rank == OWNER(nx-1)) {
204 stop = nxl - 1;
205 }
206 else {
207 stop = nxl;
208 }
209 }
210
211 void write_frame(int time) {
212 if (rank != 0) {
213 MPI_Send(u+1, nxl, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
214 } else {
215 int source, i, j, count, global_index;
216 MPI_Status status;
217
218 global_index = 0;
219 for (source = 0; source < nprocs; source++) {
220 if (source != 0) {
221 MPI_Recv(buf, 1+nx/nprocs, MPI_DOUBLE, source, 0,
222 MPI_COMM_WORLD, &status);
223 MPI_Get_count(&status, MPI_DOUBLE, &count);
224 } else {
225 for (i = 1; i <= nxl; i++) buf[i-1] = u[i];
226 count = nxl;
227 }
228 for (i = 0; i < count; i++) {
229 output_par[global_index] = colorOf(buf[i]);
230 global_index++;
231 }
232 }
233 }
234 }
235
236 /* exchange_ghost_cells: updates ghost cells using MPI communication */
237 void exchange_ghost_cells() {
238 MPI_Sendrecv(&u[1], 1, MPI_DOUBLE, left, 0,
239 &u[nxl+1], 1, MPI_DOUBLE, right, 0,
240 MPI_COMM_WORLD, MPI_STATUS_IGNORE);
241 MPI_Sendrecv(&u[nxl], 1, MPI_DOUBLE, right, 0,
242 &u[0], 1, MPI_DOUBLE, left, 0,
243 MPI_COMM_WORLD, MPI_STATUS_IGNORE);
244 }
245
246 /* update: updates u. Uses ghost cells. Purely local operation. */
247 void update() {
248 int i;
249
250 for (i = start; i <= stop; i++)
251 u_new[i] = u[i] + k*(u[i+1] + u[i-1] - 2*u[i]);
252 for (i = start; i <= stop; i++)
253 u[i] = u_new[i];
254 }
255
256 void _main() {
257 int iter;
258
259 MPI_Init();
260 MPI_COMM_WORLD = MPI_Comm_create(proc_scope, MPI_GCOMM_WORLD, _rank);
261 init();
262 write_frame(0);
263 for (iter = 1; iter <= nsteps; iter++) {
264 exchange_ghost_cells();
265 update();
266 if (iter%wstep==0) write_frame(iter);
267 }
268 MPI_Finalize();
269 MPI_Comm_destroy(MPI_COMM_WORLD);
270 free(u);
271 free(u_new);
272 if (rank == 0)
273 free(buf);
274 }
275
276 _main();
277}
278
279void run_par() {
280 $proc procs[NPROCS]; // the MPI processes
281
282 MPI_GCOMM_WORLD = CMPI_Gcomm_create($root, NPROCS);
283 for (int i=0; i<NPROCS; i++) procs[i] = $spawn MPI_Process(i);
284 for (int i=0; i<NPROCS; i++) $wait(procs[i]);
285 CMPI_Gcomm_destroy(MPI_GCOMM_WORLD);
286}
287
288void main() {
289 for (int i=0; i<NX; i++) ;
290 for (int i=0; i<NSTEPS; i++) ;
291 for (int i=0; i<WSTEP; i++) ;
292 for (int i=0; i<NPROCS; i++) ;
293 run_seq();
294 run_par();
295 for (int i=0; i<NX; i++) {
296 printf("output[%d] = %d\n", i, output_seq[i]);
297 $assert(output_seq[i]==output_par[i]);
298 }
299}
Note: See TracBrowser for help on using the repository browser.