| [f83804d] | 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:
|
|---|
| [58c0342] | 9 | *
|
|---|
| 10 | * civl verify -inputNPROCSB=3 -inputNSTEPSB=3 \
|
|---|
| 11 | -inputNXB=6 diffusion1d_good.cvl
|
|---|
| [f83804d] | 12 | */
|
|---|
| [e6b02c8] | 13 | #include <comm.cvh>
|
|---|
| [58c0342] | 14 | #include <stdio.h>
|
|---|
| 15 | #include <stdlib.h>
|
|---|
| 16 | #include <assert.h>
|
|---|
| 17 | #include "mpi.cvl"
|
|---|
| [f83804d] | 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;
|
|---|
| [58c0342] | 36 | $assume WSTEP >= 1 && WSTEP <= NSTEPS;
|
|---|
| 37 | // $assume NX >= NPROCS;
|
|---|
| 38 | $assume 1 <= NPROCS && NPROCS <= NPROCSB;
|
|---|
| 39 | $assume 1 <= NSTEPS && NSTEPS <= NSTEPSB;
|
|---|
| [f83804d] | 40 |
|
|---|
| 41 | // global variables:
|
|---|
| 42 | int output_seq[NX]; // final (color) result of sequential computation
|
|---|
| 43 | int output_par[NX]; // final (color) result of parallel computation
|
|---|
| [58c0342] | 44 | CMPI_Gcomm MPI_GCOMM_WORLD; // the global communicator object
|
|---|
| [f83804d] | 45 |
|
|---|
| 46 | /* Abstract function representing conversion from temperature to color */
|
|---|
| 47 | $abstract int colorOf(double x);
|
|---|
| 48 |
|
|---|
| 49 | /* Sequential algorithm: performs simulation storing result of final
|
|---|
| 50 | * time step in output_seq */
|
|---|
| [58c0342] | 51 | void run_seq() {
|
|---|
| [f83804d] | 52 | $scope seq_scope = $here;
|
|---|
| 53 | int nx, nsteps, wstep;
|
|---|
| 54 | double k, *u;
|
|---|
| 55 |
|
|---|
| [04e9b23] | 56 | void init_seq() {
|
|---|
| [f83804d] | 57 | int i;
|
|---|
| 58 | int pos = 0; // file position
|
|---|
| 59 |
|
|---|
| 60 | // in place of reading these from file:
|
|---|
| 61 | nx = NX;
|
|---|
| 62 | nsteps = NSTEPS;
|
|---|
| 63 | wstep = WSTEP;
|
|---|
| 64 | k = K;
|
|---|
| [58c0342] | 65 | printf("Diffusion1d (seq) with nx=%d, k=%f, nsteps=%d, wstep=%d\n",
|
|---|
| [f83804d] | 66 | nx, k, nsteps, wstep);
|
|---|
| 67 | assert(nx>=2);
|
|---|
| 68 | assert(k>0 && k<.5);
|
|---|
| 69 | assert(nsteps >= 1);
|
|---|
| 70 | assert(wstep >= 1 && wstep <=nsteps);
|
|---|
| 71 | u = (double*)$malloc(seq_scope, nx*sizeof(double));
|
|---|
| 72 | assert(u);
|
|---|
| [02c65b2] | 73 | for (i = 0; i < nx; i++)
|
|---|
| 74 | u[i] = u_init[pos++];
|
|---|
| [f83804d] | 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void write_frame(int time) {
|
|---|
| [02c65b2] | 78 | for (int i=0; i<nx; i++){
|
|---|
| [f83804d] | 79 | output_seq[i] = colorOf(u[i]);
|
|---|
| [02c65b2] | 80 | }
|
|---|
| [f83804d] | 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 |
|
|---|
| [58c0342] | 93 | void _main() {
|
|---|
| 94 | int iter;
|
|---|
| 95 |
|
|---|
| [04e9b23] | 96 | init_seq();
|
|---|
| [58c0342] | 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);
|
|---|
| [f83804d] | 103 | }
|
|---|
| [58c0342] | 104 |
|
|---|
| 105 | _main();
|
|---|
| [f83804d] | 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /* MPI Process in parallel algorithm */
|
|---|
| [58c0342] | 109 | void MPI_Process (int _rank) {
|
|---|
| [f83804d] | 110 | $scope proc_scope = $here; /* this scope */
|
|---|
| [58c0342] | 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 */
|
|---|
| [f83804d] | 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
|
|---|
| [58c0342] | 147 |
|
|---|
| 148 | MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
|
|---|
| 149 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| [f83804d] | 150 | if (rank == 0) {
|
|---|
| 151 | // in place of reading these from file:
|
|---|
| 152 | nx = NX;
|
|---|
| 153 | k = K;
|
|---|
| 154 | nsteps = NSTEPS;
|
|---|
| 155 | wstep = WSTEP;
|
|---|
| [58c0342] | 156 | printf("Diffusion1d (par) with nx=%d, k=%f, nsteps=%d, wstep=%d nprocs=%d\n",
|
|---|
| [f83804d] | 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);
|
|---|
| [58c0342] | 163 | // assert(nx>=nprocs);
|
|---|
| [f83804d] | 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++) {
|
|---|
| [58c0342] | 186 | int count_i = countForProc(i);
|
|---|
| 187 |
|
|---|
| 188 | for (j=0; j<count_i; j++)
|
|---|
| [f83804d] | 189 | buf[j] = u_init[pos++];
|
|---|
| [58c0342] | 190 | MPI_Send(buf, count_i, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
|
|---|
| [f83804d] | 191 | }
|
|---|
| [4dc3672] | 192 | // $free(buf);
|
|---|
| [f83804d] | 193 | }
|
|---|
| 194 | else {
|
|---|
| 195 | buf = NULL;
|
|---|
| 196 | MPI_Recv(u+1, nxl, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 197 | }
|
|---|
| 198 | if (rank == OWNER(0)) {
|
|---|
| 199 | start = 2;
|
|---|
| 200 | }
|
|---|
| 201 | else {
|
|---|
| 202 | start = 1;
|
|---|
| 203 | }
|
|---|
| 204 | if (rank == OWNER(nx-1)) {
|
|---|
| 205 | stop = nxl - 1;
|
|---|
| 206 | }
|
|---|
| 207 | else {
|
|---|
| 208 | stop = nxl;
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | void write_frame(int time) {
|
|---|
| 213 | if (rank != 0) {
|
|---|
| 214 | MPI_Send(u+1, nxl, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
|
|---|
| 215 | } else {
|
|---|
| 216 | int source, i, j, count, global_index;
|
|---|
| 217 | MPI_Status status;
|
|---|
| 218 |
|
|---|
| 219 | global_index = 0;
|
|---|
| 220 | for (source = 0; source < nprocs; source++) {
|
|---|
| 221 | if (source != 0) {
|
|---|
| 222 | MPI_Recv(buf, 1+nx/nprocs, MPI_DOUBLE, source, 0,
|
|---|
| 223 | MPI_COMM_WORLD, &status);
|
|---|
| 224 | MPI_Get_count(&status, MPI_DOUBLE, &count);
|
|---|
| 225 | } else {
|
|---|
| 226 | for (i = 1; i <= nxl; i++) buf[i-1] = u[i];
|
|---|
| 227 | count = nxl;
|
|---|
| 228 | }
|
|---|
| 229 | for (i = 0; i < count; i++) {
|
|---|
| 230 | output_par[global_index] = colorOf(buf[i]);
|
|---|
| 231 | global_index++;
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /* exchange_ghost_cells: updates ghost cells using MPI communication */
|
|---|
| 238 | void exchange_ghost_cells() {
|
|---|
| 239 | MPI_Sendrecv(&u[1], 1, MPI_DOUBLE, left, 0,
|
|---|
| 240 | &u[nxl+1], 1, MPI_DOUBLE, right, 0,
|
|---|
| 241 | MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 242 | MPI_Sendrecv(&u[nxl], 1, MPI_DOUBLE, right, 0,
|
|---|
| 243 | &u[0], 1, MPI_DOUBLE, left, 0,
|
|---|
| 244 | MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /* update: updates u. Uses ghost cells. Purely local operation. */
|
|---|
| 248 | void update() {
|
|---|
| 249 | int i;
|
|---|
| 250 |
|
|---|
| 251 | for (i = start; i <= stop; i++)
|
|---|
| [58c0342] | 252 | u_new[i] = u[i] + k*(u[i+1] + u[i-1] - 2*u[i]);
|
|---|
| [f83804d] | 253 | for (i = start; i <= stop; i++)
|
|---|
| 254 | u[i] = u_new[i];
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| [58c0342] | 257 | void _main() {
|
|---|
| 258 | int iter;
|
|---|
| 259 |
|
|---|
| 260 | MPI_Init();
|
|---|
| 261 | MPI_COMM_WORLD = MPI_Comm_create(proc_scope, MPI_GCOMM_WORLD, _rank);
|
|---|
| 262 | init();
|
|---|
| 263 | write_frame(0);
|
|---|
| 264 | for (iter = 1; iter <= nsteps; iter++) {
|
|---|
| 265 | exchange_ghost_cells();
|
|---|
| 266 | update();
|
|---|
| 267 | if (iter%wstep==0) write_frame(iter);
|
|---|
| 268 | }
|
|---|
| 269 | MPI_Finalize();
|
|---|
| 270 | MPI_Comm_destroy(MPI_COMM_WORLD);
|
|---|
| 271 | free(u);
|
|---|
| 272 | free(u_new);
|
|---|
| 273 | if (rank == 0)
|
|---|
| 274 | free(buf);
|
|---|
| [f83804d] | 275 | }
|
|---|
| [58c0342] | 276 |
|
|---|
| 277 | _main();
|
|---|
| [f83804d] | 278 | }
|
|---|
| 279 |
|
|---|
| [58c0342] | 280 | void run_par() {
|
|---|
| 281 | $proc procs[NPROCS]; // the MPI processes
|
|---|
| 282 |
|
|---|
| 283 | MPI_GCOMM_WORLD = CMPI_Gcomm_create($root, NPROCS);
|
|---|
| [f83804d] | 284 | for (int i=0; i<NPROCS; i++) procs[i] = $spawn MPI_Process(i);
|
|---|
| 285 | for (int i=0; i<NPROCS; i++) $wait(procs[i]);
|
|---|
| [58c0342] | 286 | CMPI_Gcomm_destroy(MPI_GCOMM_WORLD);
|
|---|
| [f83804d] | 287 | }
|
|---|
| 288 |
|
|---|
| 289 | void main() {
|
|---|
| [58c0342] | 290 | for (int i=0; i<NX; i++) ;
|
|---|
| 291 | for (int i=0; i<NSTEPS; i++) ;
|
|---|
| 292 | for (int i=0; i<WSTEP; i++) ;
|
|---|
| 293 | for (int i=0; i<NPROCS; i++) ;
|
|---|
| [478ec61] | 294 |
|
|---|
| [58c0342] | 295 | run_seq();
|
|---|
| 296 | run_par();
|
|---|
| 297 | for (int i=0; i<NX; i++) {
|
|---|
| 298 | printf("output[%d] = %d\n", i, output_seq[i]);
|
|---|
| [f83804d] | 299 | $assert(output_seq[i]==output_par[i]);
|
|---|
| [58c0342] | 300 | }
|
|---|
| [f83804d] | 301 | }
|
|---|