All pastes #207233 Raw Edit

Something

public c v1 · immutable
#207233 ·published 2006-10-17 21:48 UTC
rendered paste body
#include <mpi.h>#include <stdio.h>#include <string.h>#include <stdlib.h>int main(int argc, char *argv[]){  char idstr[32];  char buff[128];  int numprocs;  int myid;  double *a, *b, *c;  int n = 6;                      // horyzon nsteps  double time = 0.1, tau = 0.001;  int i;  double k = 0.06;                // heat conduction  double h = 1.0 / (n - 1);       // horyzon step  int remain;  int ncur;                    // num of horyzon points in cur proc  MPI_Status stat;   MPI_Init (&argc, &argv);   MPI_Comm_size (MPI_COMM_WORLD, &numprocs);   MPI_Comm_rank (MPI_COMM_WORLD, &myid);     ncur = n / numprocs;  remain = n - numprocs * ncur;  ncur += 2;  while (remain--)    if (remain == myid)      ncur++;  h = 1.0 / (ncur - 2 - 1);   printf ("%d %d\n", myid, ncur);  a = calloc (ncur, sizeof (double));  b = calloc (ncur, sizeof (double));  bzero (a, ncur * sizeof (double));  bzero (b, ncur * sizeof (double));  if (myid == 0)    a[1] = 0.0;  if (myid == numprocs - 1)    a[ncur - 2] == 1.0;    while ((time -= tau) > 0) {//    printf ("%f", time);    if (!(myid % 2)) {      if (myid != numprocs - 1) {//        printf ("proc%d sending %d...\n", myid, myid + 1);        MPI_Send (&a[ncur - 2], 1, MPI_DOUBLE, myid + 1, 0, MPI_COMM_WORLD);      }      if (myid != 0) {//        printf ("proc%d sending %d...\n", myid, myid - 1);        MPI_Send (&a[1], 1, MPI_DOUBLE, myid - 1, 0, MPI_COMM_WORLD);      }      if (myid != numprocs - 1) {//        printf ("proc%d receiving %d...\n", myid, myid + 1);        MPI_Recv (&a[ncur - 1], 1, MPI_DOUBLE, myid + 1, 0, MPI_COMM_WORLD, &stat);      }      if (myid != 0) {//        printf ("proc%d receiving %d...\n", myid, myid - 1);        MPI_Recv (&a[0], 1, MPI_DOUBLE, myid - 1, 0, MPI_COMM_WORLD, &stat);      }    }    else {      if (myid != 0) {//        printf ("proc%d receiving %d...\n", myid, myid - 1);        MPI_Recv (&a[0], 1, MPI_DOUBLE, myid - 1, 0, MPI_COMM_WORLD, &stat);      }      if (myid != numprocs - 1) {//        printf ("proc%d receiving %d...\n", myid, myid + 1);        MPI_Recv (&a[ncur - 1], 1, MPI_DOUBLE, myid + 1, 0, MPI_COMM_WORLD, &stat);      }      if (myid != 0) {//        printf ("proc%d sending %d...\n", myid, myid - 1);        MPI_Send (&a[1], 1, MPI_DOUBLE, myid - 1, 0, MPI_COMM_WORLD);      }      if (myid != numprocs - 1) {//        printf ("proc%d sending %d...\n", myid, myid + 1);        MPI_Send (&a[ncur - 2], 1, MPI_DOUBLE, myid + 1, 0, MPI_COMM_WORLD);      }    }        printf ("b[0]=%f", b[0]);    for (i = 1; i < ncur - 1; ++i) {      printf ("b[%d]=%f ", i, b[i]);      b[i] = a[i] + k * tau / h / h * (a[i + 1] + a[i - 1] - 2 * a[i]);    }    printf ("b[%d]=%f", ncur - 1, b[ncur - 1]);    c = a; a = b; b = c;  }      MPI_Finalize();  return 0;}