SHARE
TWEET

Laplas

a guest Dec 6th, 2019 104 in 133 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<mpi.h>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. void laplas(int argc, char **argv, int n, int noiters)
  6. {
  7.     int i, k;
  8.     int p, me, ln, tag, ierr, lm; // me -- current process id
  9.     MPI_Status status;
  10.  
  11.     int ln = 10000;
  12.     double* x = new double[ln];
  13.     double* newx = new double[ln];
  14.     double* z = new double[ln];
  15.  
  16.     double dx2 = (1. / n);
  17.     dx2 *= dx2;
  18.    
  19.     MPI_Init(&argc, &argv);
  20.     MPI_Comm_rank(MPI_COMM_WORLD, &me);
  21.     MPI_Comm_size(MPI_COMM_WORLD, &p);
  22.  
  23.     tag = 0;
  24.     n = n / p;
  25.  
  26.     for (i = 1; i < ln; i++) {
  27.         x[i] = 1.0;
  28.     }
  29.  
  30.     if (me == 0) {
  31.         x[0] = 0.0;
  32.         lm = 0;
  33.     } else {
  34.         lm = ln * me;
  35.     }
  36.  
  37.     if (me == p - 1) {
  38.         x[ln + 1] = 0.0;
  39.     }
  40.  
  41.     for (k = 1; k < noiters; k++) {
  42.         if (me - 1 >= 0)
  43.             MPI_Send(newx + 1, 1, MPI_REAL, me - 1, tag, MPI_COMM_WORLD);
  44.         if (me + 1 < p)
  45.             MPI_Recv(x + ln + 1, 1, MPI_REAL, me + 1, tag, MPI_COMM_WORLD, &status);
  46.         tag++;
  47.         if (me + 1 < p)
  48.             MPI_Send(newx + ln, 1, MPI_REAL, me + 1, tag, MPI_COMM_WORLD);
  49.         if (me - 1 >= 0)
  50.             MPI_Recv(x, 1, MPI_REAL, me - 1, tag, MPI_COMM_WORLD, &status);
  51.         tag++;
  52.         for (i = 1; i < n; i++) {
  53.             newx[i] = 0.5 * (x[i - 1] + x[i + 1] - dx2 * x[i]);
  54.         }
  55.         for (i = 1; i < n; i++) {
  56.             x[i] = newx[i];
  57.         }
  58.     }
  59.  
  60.     if (me == 0) {
  61.         for (i = 1, i < ln; i++) {
  62.             z[i] = x[i];
  63.         }
  64.         for (k = 1, k < p - 1; k++) {
  65.             lm = ln * k;
  66.             MPI_Recv(z + lm, ln, MPI_REAL, k, k, MPI_COMM_WORLD, &status);
  67.         }
  68.     } else {
  69.         MPI_Send(x + 1, ln, MPI_REAL, 0, me, MPI_COMM_WORLD);
  70.     }
  71.     MPI_Finalize();
  72.     if (me == 0) {
  73.         printf("Result ...");
  74.     }
  75. }
  76.  
  77. int main(int argc, char **argv)
  78. {
  79.      int rank, size;
  80.      cout << "The number of processes: " << size << " my number is " << rank << endl;
  81.      MPI_Finalize();
  82.      cin >> rank;
  83.      return 0;
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top