main() { int rowsize, /* number of bytes in each row */ iterno, /* current iteration number */ i, j; /* loop indices */ double resid, /* residual */ residtmp, /* temp */ residglob, /* global residual */ Atmp, /* temp */ Amax; /* value corresponding residglob */ FILE *fd; /* file descriptor */ char host[128]; /* host name */ long t0, t1; /* start and end elapsed time */ /* * initialize * */ gethostname(host, sizeof(host)); t0 = time ((long*)0); rdim = DIM; rowsize = sizeof(double) * (DIM + 2); inita(); /* guess initial values for A and set B.C. */ /* * This is the main iteration loop for the Gauss-Seidal method. Refinement * is done in successive iterations till either the global residual is * less than desired or MAXITER is reached. * */ Amax = 0.0; for (iterno= 1; iterno <= MAXITER; iterno++) { resid = 0.0; /* * Refine the values in A, these two loops consume most of * the computational time. */ for (i = 1; i <= rdim; i++) for (j = 1; j <= DIM; j++) { Atmp = A[i][j]; A[i][j] = 0.25 * (A[i+1][j] + A[i-1][j] + A[i][j+1] + A[i][j-1]); residtmp = fabs(A[i][j] - Atmp); if (residtmp > resid) { resid = residtmp; Amax = Atmp; } } if (Amax == 0.0) /* set global residual */ residglob = 1.0 + EPS; else residglob = fabs( resid/Amax ); if (DEBUG && iterno % 100 == 0) printf ("\nsequential(%d) iterations done\n",iterno); if ( residglob < EPS) /* termination of iterations */ { break; } } t1 = time((long *)0) - t0; /* * Output the solution * */ fd = fopen("seq_time.dat", "a"); if (t1 > 0) { fprintf(fd, "\n (%s) Elapsed time = %d seconds. n: (%d)\n", host, t1, DIM); fprintf(fd, " MFLOPS: (%f) Iterations: (%d) Precision (%f)\n", (float)(DIM)*(DIM) * iterno * 5.0/(t1)/1000000.0, iterno, residglob); } else fprintf(fd, " (%s) Zero second recorded. n: (%d).\n", host, DIM); if (iterno >= MAXITER) { printf ("iterno: (%d), Precision: (%f), time: (%d sec), n:(%d)\n", iterno, residglob, t1, DIM); printf("\n \n WARNING: Maximum iterations reached \n"); printf("%s\n%s\n%s\n\n", " Try increasing MAXITER if the current Precision,", " i.e. maximum relative change at any gridblock, is", " to be further improved "); } if (LOG) /* to get output profile */ { fprintf(fd,"\n"); for (i=0; i<DIM+2; i++) { for (j=0; j<DIM+2; j++) { fprintf(fd, "%8.1f, ",A[i][j]); } fprintf(fd,"\n"); } fprintf (fd, "\n%s\n\n", "==============================="); } printf ("iterno: (%d), residglob: (%f), t1: (%d), n:(%d)\n", iterno, residglob, t1, DIM); fclose(fd); return 0; }
static void cecrdr(void *vp) { Proc *up = externup(); Block *bp; Conn *cp; If *ifc; Pkt *p; ifc = vp; if(waserror()) goto exit; discover(ifc, 0); for(;;){ bp = ifc->d->bread(ifc->dc, 1514, 0); // do we care about making the MTU non magic? if(bp == nil) nexterror(); p = (Pkt *)bp->rp; if(p->etype[0] != 0xbc || p->etype[1] != 0xbc){ freeb(bp); continue; } trace(bp); cp = findconn(p->src, p->conn); if(cp == nil){ cecprint("cec: out of connection structures\n"); freeb(bp); continue; } if (waserror()){ freeb(bp); qunlock(cp); continue; } switch(p->type){ case Tinita: if(cp->bp){ cecprint("cec: reset with bp!? ask quanstro\n"); freeb(cp->bp); cp->bp = 0; } inita(cp, ifc, p); break; case Tinitb: cecprint("cec: unexpected initb\n"); break; case Tinitc: if(cp->state == Cinitb){ ack(cp); if(cp->passwd[0]){ cp->state = Clogin; conputs(cp, "password: "******"cec: unexpected offer\n"); from ourselves. break; case Treset: if(cp->bp) freeb(cp->bp); cp->bp = 0; cp->state = Cunused; break; default: cecprint("bad cec type: %d\n", p->type); break; } nexterror(); } exit: for(cp = conn; cp < conn+nelem(conn); cp++) if(cp->ifc == ifc){ if(cp->bp) freeb(cp->bp); memset(cp, 0, sizeof *cp); break; } memset(ifc, 0, sizeof *ifc); pexit("cec exiting", 1); }