blocking.cpp (1452B)
1 #include "IO.h" 2 #include <mpi.h> 3 #include <vector> 4 5 int main(int argc, char *argv[]) 6 { 7 int rank, size; 8 MPI_Init(&argc, &argv); 9 MPI_Comm_rank(MPI_COMM_WORLD, &rank); 10 MPI_Comm_size(MPI_COMM_WORLD, &size); 11 12 // source and destination (periodic boundaries) 13 const int left = (rank + size - 1) % size; 14 const int right = (rank + size + 1) % size; 15 16 const int nsteps = 1000; // number of time steps 17 const int N = 128; // number of cells in subdomain 18 const double Fo = 0.45; // Fourier number 19 std::vector<double> curr(N + 2); // +2 ghost cells 20 std::vector<double> next(N + 2); // +2 ghost cells 21 initialize(curr); 22 for (int step = 0; step < nsteps; ++step) { 23 // clang-format off 24 MPI_Sendrecv(&curr[1], 1, MPI_DOUBLE, left, 123, 25 &curr[N + 1], 1, MPI_DOUBLE, right, 123, 26 MPI_COMM_WORLD, MPI_STATUS_IGNORE); 27 MPI_Sendrecv(&curr[N], 1, MPI_DOUBLE, right, 123, 28 &curr[0], 1, MPI_DOUBLE, left, 123, 29 MPI_COMM_WORLD, MPI_STATUS_IGNORE); 30 // clang-format on 31 32 // stencil scheme 33 for (int i = 1; i < N + 1; ++i) { 34 next[i] = 35 curr[i] + Fo * (curr[i - 1] - 2.0 * curr[i] + curr[i + 1]); 36 } 37 if (step % 200 == 0) { 38 dump(curr); 39 } 40 curr.swap(next); 41 } 42 43 MPI_Finalize(); 44 return 0; 45 }