| 1 | /* FEVS: A Functional Equivalence Verification Suite for High-Performance
|
|---|
| 2 | * Scientific Computing
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (C) 2010, Stephen F. Siegel, Timothy K. Zirkel,
|
|---|
| 5 | * University of Delaware
|
|---|
| 6 | *
|
|---|
| 7 | * This program is free software; you can redistribute it and/or
|
|---|
| 8 | * modify it under the terms of the GNU General Public License as
|
|---|
| 9 | * published by the Free Software Foundation; either version 3 of the
|
|---|
| 10 | * License, or (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * This program is distributed in the hope that it will be useful, but
|
|---|
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 15 | * General Public License for more details.
|
|---|
| 16 | *
|
|---|
| 17 | * You should have received a copy of the GNU General Public License
|
|---|
| 18 | * along with this program; if not, write to the Free Software
|
|---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|---|
| 20 | * 02110-1301 USA.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | /*
|
|---|
| 25 | * diffusion1d.c: parallel 1d-diffusion simulation.
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | #include <stdlib.h>
|
|---|
| 29 | #include <stdio.h>
|
|---|
| 30 | #include <string.h>
|
|---|
| 31 | #include <assert.h>
|
|---|
| 32 | #include <math.h>
|
|---|
| 33 | #include <mpi.h>
|
|---|
| 34 | #include "gd.h"
|
|---|
| 35 | #define MAXCOLORS 256
|
|---|
| 36 | #define OWNER(index) ((nprocs*(index+1)-1)/nx)
|
|---|
| 37 | #define PWIDTH 1
|
|---|
| 38 | #define PHEIGHT 1
|
|---|
| 39 | #define M 10
|
|---|
| 40 |
|
|---|
| 41 | /* Parameters: These are defined at the beginning of the input file:
|
|---|
| 42 | *
|
|---|
| 43 | * nx = number of points in x direction, including endpoints
|
|---|
| 44 | * k = D*dt/(dx*dx)
|
|---|
| 45 | * nsteps = number of time steps
|
|---|
| 46 | * wstep = write frame every this many steps
|
|---|
| 47 | *
|
|---|
| 48 | * Compiling with the flag -DDEBUG will also cause the data to be written
|
|---|
| 49 | * to a sequence of plain text files.
|
|---|
| 50 | */
|
|---|
| 51 |
|
|---|
| 52 | /* Global variables */
|
|---|
| 53 |
|
|---|
| 54 | int nx = -1; /* number of discrete points including endpoints */
|
|---|
| 55 | double k = -1; /* D*dt/(dx*dx) */
|
|---|
| 56 | int nsteps = -1; /* number of time steps */
|
|---|
| 57 | int wstep = -1; /* write frame every this many time steps */
|
|---|
| 58 | double *u; /* temperature function */
|
|---|
| 59 | double *u_new; /* temp. used to update u */
|
|---|
| 60 | FILE *file; /* file containing animated GIF */
|
|---|
| 61 | gdImagePtr im, previm; /* pointers to GIF images */
|
|---|
| 62 | int *colors; /* colors we will use */
|
|---|
| 63 |
|
|---|
| 64 | int nprocs; /* number of processes */
|
|---|
| 65 | int rank; /* the rank of this process */
|
|---|
| 66 | int left; /* rank of left neighbor */
|
|---|
| 67 | int right; /* rank of right neighbor on torus */
|
|---|
| 68 | int nxl; /* horizontal extent of one process */
|
|---|
| 69 | int first; /* global index for local index 0 */
|
|---|
| 70 | int start; /* first local index to update */
|
|---|
| 71 | int stop; /* last local index to update */
|
|---|
| 72 | double *buf; /* temp. buffer used on proc 0 only */
|
|---|
| 73 | MPI_Request requests[4]; /* for ghost cell exchange */
|
|---|
| 74 |
|
|---|
| 75 | int firstForProc(int rank) {
|
|---|
| 76 | return (rank*nx)/nprocs;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | int countForProc(int rank) {
|
|---|
| 80 | int a;
|
|---|
| 81 | int b;
|
|---|
| 82 |
|
|---|
| 83 | a = firstForProc(rank+1);
|
|---|
| 84 | b = firstForProc(rank);
|
|---|
| 85 | return a-b;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void quit() {
|
|---|
| 89 | printf("Input file must have format:\n\n");
|
|---|
| 90 | printf("nx = <INTEGER>\n");
|
|---|
| 91 | printf("k = <DOUBLE>\n");
|
|---|
| 92 | printf("nsteps = <INTEGER>\n");
|
|---|
| 93 | printf("wstep = <INTEGER>\n");
|
|---|
| 94 | printf("<DOUBLE> <DOUBLE> ...\n\n");
|
|---|
| 95 | printf("where there are nx doubles at the end.\n");
|
|---|
| 96 | fflush(stdout);
|
|---|
| 97 | exit(1);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | void readint(FILE *file, char *keyword, int *ptr) {
|
|---|
| 101 | char buf[101];
|
|---|
| 102 | int value;
|
|---|
| 103 | int returnval;
|
|---|
| 104 |
|
|---|
| 105 | returnval = fscanf(file, "%100s", buf);
|
|---|
| 106 | if (returnval != 1) quit();
|
|---|
| 107 | if (strcmp(keyword, buf) != 0) quit();
|
|---|
| 108 | returnval = fscanf(file, "%10s", buf);
|
|---|
| 109 | if (returnval != 1) quit();
|
|---|
| 110 | if (strcmp("=", buf) != 0) quit();
|
|---|
| 111 | returnval = fscanf(file, "%d", ptr);
|
|---|
| 112 | if (returnval != 1) quit();
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | void readdouble(FILE *file, char *keyword, double *ptr) {
|
|---|
| 116 | char buf[101];
|
|---|
| 117 | int value;
|
|---|
| 118 | int returnval;
|
|---|
| 119 |
|
|---|
| 120 | returnval = fscanf(file, "%100s", buf);
|
|---|
| 121 | if (returnval != 1) quit();
|
|---|
| 122 | if (strcmp(keyword, buf) != 0) quit();
|
|---|
| 123 | returnval = fscanf(file, "%10s", buf);
|
|---|
| 124 | if (returnval != 1) quit();
|
|---|
| 125 | if (strcmp("=", buf) != 0) quit();
|
|---|
| 126 | returnval = fscanf(file, "%lf", ptr);
|
|---|
| 127 | if (returnval != 1) quit();
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /* init: initializes global variables. */
|
|---|
| 131 | void init(char* infilename) {
|
|---|
| 132 | char keyword[101];
|
|---|
| 133 | FILE *infile = fopen(infilename, "r");
|
|---|
| 134 | int i,j;
|
|---|
| 135 |
|
|---|
| 136 | MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
|
|---|
| 137 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| 138 | if (rank == 0) {
|
|---|
| 139 | assert(infile);
|
|---|
| 140 | readint(infile, "nx", &nx);
|
|---|
| 141 | readdouble(infile, "k", &k);
|
|---|
| 142 | readint(infile, "nsteps", &nsteps);
|
|---|
| 143 | readint(infile, "wstep", &wstep);
|
|---|
| 144 | printf("Diffusion1d with nx=%d, k=%f, nsteps=%d, wstep=%d nprocs=%d\n",
|
|---|
| 145 | nx, k, nsteps, wstep, nprocs);
|
|---|
| 146 | fflush(stdout);
|
|---|
| 147 | }
|
|---|
| 148 | MPI_Bcast(&nx, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 149 | MPI_Bcast(&k, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
|---|
| 150 | MPI_Bcast(&nsteps, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 151 | MPI_Bcast(&wstep, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 152 |
|
|---|
| 153 | assert(nx>=nprocs); /* is this necessary ? */
|
|---|
| 154 | assert(k>0 && k<.5);
|
|---|
| 155 | assert(nx>=2);
|
|---|
| 156 | assert(nsteps>=1);
|
|---|
| 157 | // nxl: number actual points (incl. end-points)
|
|---|
| 158 | // nxl+2: size of array (incl. ghost cells)
|
|---|
| 159 | first = firstForProc(rank);
|
|---|
| 160 | nxl = countForProc(rank);
|
|---|
| 161 | if (first == 0 || nxl == 0) left = MPI_PROC_NULL; else left = OWNER(first-1);
|
|---|
| 162 | if (first+nxl >= nx || nxl == 0) right = MPI_PROC_NULL; else right = OWNER(first+nxl);
|
|---|
| 163 | u = (double*)malloc((nxl+2)*sizeof(double));
|
|---|
| 164 | assert(u);
|
|---|
| 165 | u_new = (double*)malloc((nxl+2)*sizeof(double));
|
|---|
| 166 | assert(u_new);
|
|---|
| 167 | if (rank == 0) {
|
|---|
| 168 | buf = (double*)malloc((1+nx/nprocs)*sizeof(double));
|
|---|
| 169 | for (i=1; i <= nxl; i++){
|
|---|
| 170 | if (fscanf(infile, "%lf", u+i) != 1) quit();
|
|---|
| 171 | }
|
|---|
| 172 | for (i=1; i < nprocs; i++){
|
|---|
| 173 | for (j=0; j<countForProc(i); j++){
|
|---|
| 174 | if (fscanf(infile, "%lf", buf+j) != 1) quit();
|
|---|
| 175 | }
|
|---|
| 176 | MPI_Send(buf, countForProc(i), MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 | else {
|
|---|
| 180 | MPI_Recv(u+1, nxl, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 181 | }
|
|---|
| 182 | if (rank == 0) {
|
|---|
| 183 | buf = (double*)malloc((1+nx/nprocs)*sizeof(double));
|
|---|
| 184 | assert(buf);
|
|---|
| 185 | file = fopen("./nbout/out.gif", "wb");
|
|---|
| 186 | assert(file);
|
|---|
| 187 | colors = (int*)malloc(MAXCOLORS*sizeof(int));
|
|---|
| 188 | assert(colors);
|
|---|
| 189 | } else {
|
|---|
| 190 | buf = NULL;
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | void write_plain(int time) {
|
|---|
| 195 | if (rank != 0) {
|
|---|
| 196 | MPI_Send(u+1, nxl, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
|
|---|
| 197 | } else {
|
|---|
| 198 | int source, i, count;
|
|---|
| 199 | char filename[50];
|
|---|
| 200 | FILE *plain = NULL;
|
|---|
| 201 | MPI_Status status;
|
|---|
| 202 |
|
|---|
| 203 | sprintf(filename, "./nbout/out_%d", time);
|
|---|
| 204 | plain = fopen(filename, "w");
|
|---|
| 205 | assert(plain);
|
|---|
| 206 | for (source = 0; source < nprocs; source++) {
|
|---|
| 207 | if (source != 0) {
|
|---|
| 208 | MPI_Recv(buf, 1+nx/nprocs, MPI_DOUBLE, source, 0, MPI_COMM_WORLD,
|
|---|
| 209 | &status);
|
|---|
| 210 | MPI_Get_count(&status, MPI_DOUBLE, &count);
|
|---|
| 211 | } else {
|
|---|
| 212 | for (i = 1; i <= nxl; i++) buf[i-1] = u[i];
|
|---|
| 213 | count = nxl;
|
|---|
| 214 | }
|
|---|
| 215 | for (i = 0; i < count; i++) fprintf(plain, "%8.2f", buf[i]);
|
|---|
| 216 | }
|
|---|
| 217 | fprintf(plain, "\n");
|
|---|
| 218 | fclose(plain);
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | void write_frame(int time) {
|
|---|
| 223 | if (rank != 0) {
|
|---|
| 224 | MPI_Send(u+1, nxl, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
|
|---|
| 225 | } else {
|
|---|
| 226 | int source, i, j, count, global_index;
|
|---|
| 227 | MPI_Status status;
|
|---|
| 228 |
|
|---|
| 229 | im = gdImageCreate(nx*PWIDTH,PHEIGHT);
|
|---|
| 230 | if (time == 0) {
|
|---|
| 231 | for (j=0; j<MAXCOLORS; j++)
|
|---|
| 232 | colors[j] = gdImageColorAllocate (im, j, 0, MAXCOLORS-j-1);
|
|---|
| 233 | gdImageGifAnimBegin(im, file, 1, -1);
|
|---|
| 234 | } else {
|
|---|
| 235 | gdImagePaletteCopy(im, previm);
|
|---|
| 236 | }
|
|---|
| 237 | global_index = 0;
|
|---|
| 238 | for (source = 0; source < nprocs; source++) {
|
|---|
| 239 | if (source != 0) {
|
|---|
| 240 | MPI_Recv(buf, 1+nx/nprocs, MPI_DOUBLE, source, 0, MPI_COMM_WORLD,
|
|---|
| 241 | &status);
|
|---|
| 242 | MPI_Get_count(&status, MPI_DOUBLE, &count);
|
|---|
| 243 | } else {
|
|---|
| 244 | for (i = 1; i <= nxl; i++) buf[i-1] = u[i];
|
|---|
| 245 | count = nxl;
|
|---|
| 246 | }
|
|---|
| 247 | for (i = 0; i < count; i++) {
|
|---|
| 248 | int color = (int)(buf[i]*MAXCOLORS/M);
|
|---|
| 249 |
|
|---|
| 250 | if (color < 0) {
|
|---|
| 251 | printf("rank = %d, i = %d, buf[i] = %lf, color = %d, M = %d\n", rank, i, buf[i], color, M);
|
|---|
| 252 | }
|
|---|
| 253 | assert(color >= 0);
|
|---|
| 254 | if (color >= MAXCOLORS) color = MAXCOLORS-1;
|
|---|
| 255 | gdImageFilledRectangle
|
|---|
| 256 | (im, global_index*PWIDTH, 0, (global_index+1)*PWIDTH-1,
|
|---|
| 257 | PHEIGHT-1, colors[color]);
|
|---|
| 258 | global_index++;
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 | if (time == 0) {
|
|---|
| 262 | gdImageGifAnimAdd(im, file, 0, 0, 0, 0, gdDisposalNone, NULL);
|
|---|
| 263 | } else {
|
|---|
| 264 | gdImageGifAnimAdd(im, file, 0, 0, 0, 5, gdDisposalNone, previm);
|
|---|
| 265 | gdImageDestroy(previm);
|
|---|
| 266 | }
|
|---|
| 267 | previm=im;
|
|---|
| 268 | im=NULL;
|
|---|
| 269 | }
|
|---|
| 270 | #ifdef DEBUG
|
|---|
| 271 | write_plain(time);
|
|---|
| 272 | #endif
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /* exchange_ghost_cells: updates ghost cells using MPI communication */
|
|---|
| 276 | void initiate_exchange() {
|
|---|
| 277 | MPI_Isend(&u[1], 1, MPI_DOUBLE, left, 0, MPI_COMM_WORLD, &requests[0]);
|
|---|
| 278 | MPI_Irecv(&u[nxl+1], 1, MPI_DOUBLE, right, 0, MPI_COMM_WORLD, &requests[1]);
|
|---|
| 279 | MPI_Isend(&u[nxl], 1, MPI_DOUBLE, right, 0, MPI_COMM_WORLD, &requests[2]);
|
|---|
| 280 | MPI_Irecv(&u[0], 1, MPI_DOUBLE, left, 0, MPI_COMM_WORLD, &requests[3]);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | void finalize_exchange() {
|
|---|
| 284 | MPI_Waitall(4, requests, MPI_STATUSES_IGNORE);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | void update_interior() {
|
|---|
| 288 | int i;
|
|---|
| 289 |
|
|---|
| 290 | for (i = 2; i <= nxl-1; i++)
|
|---|
| 291 | u_new[i] = u[i] + k*(u[i+1] + u[i-1] -2*u[i]);
|
|---|
| 292 | for (i = 3; i <= nxl-2; i++) u[i] = u_new[i];
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | void update_boundary() {
|
|---|
| 296 | int i;
|
|---|
| 297 |
|
|---|
| 298 | if (rank != OWNER(0))
|
|---|
| 299 | u_new[1] = u[1]+k*(u[2] + u[0] - 2*u[1]);
|
|---|
| 300 | if (rank != OWNER(nx-1))
|
|---|
| 301 | u_new[nxl] = u[nxl]+k*(u[nxl+1] + u[nxl-1] - 2*u[nxl]);
|
|---|
| 302 | if (rank != OWNER(0))
|
|---|
| 303 | u[1] = u_new[1];
|
|---|
| 304 | /* Can't update u[nxl-1] if nxl-1 is 1, or u[2] if nxl = 2 */
|
|---|
| 305 | if (nxl - 1 > 1){
|
|---|
| 306 | u[nxl-1] = u_new[nxl-1];
|
|---|
| 307 | u[2] = u_new[2];
|
|---|
| 308 | }
|
|---|
| 309 | if (rank != OWNER(nx-1))
|
|---|
| 310 | u[nxl] = u_new[nxl];
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | /* main: executes simulation, creates one output file for each time
|
|---|
| 314 | * step */
|
|---|
| 315 | int main(int argc,char *argv[]) {
|
|---|
| 316 | int iter;
|
|---|
| 317 |
|
|---|
| 318 | MPI_Init(&argc, &argv);
|
|---|
| 319 | assert(argc==2);
|
|---|
| 320 | init(argv[1]);
|
|---|
| 321 | write_frame(0);
|
|---|
| 322 | for (iter = 1; iter <= nsteps; iter++) {
|
|---|
| 323 | initiate_exchange(); /* post sends and recvs */
|
|---|
| 324 | update_interior();
|
|---|
| 325 | finalize_exchange(); /* wait on sends/recvs */
|
|---|
| 326 | update_boundary(); /* positions 1,2,nxl-1,nxl */
|
|---|
| 327 | if (iter%wstep==0) write_frame(iter);
|
|---|
| 328 | }
|
|---|
| 329 | MPI_Finalize();
|
|---|
| 330 | free(u);
|
|---|
| 331 | free(u_new);
|
|---|
| 332 | if (rank == 0) {
|
|---|
| 333 | gdImageDestroy(previm);
|
|---|
| 334 | gdImageGifAnimEnd(file);
|
|---|
| 335 | fclose(file);
|
|---|
| 336 | free(buf);
|
|---|
| 337 | free(colors);
|
|---|
| 338 | }
|
|---|
| 339 | return 0;
|
|---|
| 340 | }
|
|---|