diff --git a/lab11/game_of_life/game_of_life.cpp b/lab11/game_of_life/game_of_life.cpp index f3ccb9a..6c6ca21 100644 --- a/lab11/game_of_life/game_of_life.cpp +++ b/lab11/game_of_life/game_of_life.cpp @@ -4,7 +4,7 @@ GameOfLife::GameOfLife(const SuperGrid &grid, MPIGridSize mpiProcs) : grid_(grid), mpiProcs_(mpiProcs) {} void GameOfLife::step() { - SuperGrid next = SuperGrid::zeros(grid_.rows(), grid_.cols()); + SuperGrid next = SuperGrid::zeros(grid_.rows(), grid_.cols(), grid_.get_communicator()); const int rows = grid_.rows(); const int cols = grid_.cols(); for (int i = 0; i < rows; ++i) { diff --git a/lab11/game_of_life/main.cpp b/lab11/game_of_life/main.cpp index 4ce3971..296e764 100644 --- a/lab11/game_of_life/main.cpp +++ b/lab11/game_of_life/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "common.h" #include "game_of_life.h" @@ -15,7 +16,8 @@ /** * Main function to run the simulation of the game of life */ -void gameOfLife(MPIGridSize mpiProcs) { +void gameOfLife(MPIGridSize mpiProcs) +{ int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); @@ -34,7 +36,8 @@ void gameOfLife(MPIGridSize mpiProcs) { print(game); - for (int i = 0; i < 50; ++i) { + for (int i = 0; i < 50; ++i) + { game.step(); } if (rank == 0) @@ -49,9 +52,11 @@ void gameOfLife(MPIGridSize mpiProcs) { * Initializes MPI, checks command line arguments, and starts the game of life * simulation. */ -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ MPI_Init(&argc, &argv); - if (argc != 3) { + if (argc != 3) + { std::cout << "Specify number of processes in x and y as arguments\n"; std::cout << "jacobi \n"; return 1; @@ -63,15 +68,19 @@ int main(int argc, char *argv[]) { int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &numProc); - if (np0 * np1 != numProc) { + if (np0 * np1 != numProc) + { std::cout << "Error: nproc != np0 x np1 (" << numProc << "!= " << np0 << "x" << np1 << ")\n"; return 2; } - try { + try + { gameOfLife({np0, np1}); - } catch (const std::exception &e) { + } + catch (const std::exception &e) + { std::cerr << "Error: " << e.what() << std::endl; MPI_Abort(MPI_COMM_WORLD, 1); return 1; diff --git a/lab11/game_of_life/patterns.cpp b/lab11/game_of_life/patterns.cpp index a0eafa4..406106a 100644 --- a/lab11/game_of_life/patterns.cpp +++ b/lab11/game_of_life/patterns.cpp @@ -19,6 +19,7 @@ Pattern::Pattern(int rows, int cols, MPIGridSize mpiProcs) std::array periods = {1, 1}; MPI_Cart_create(MPI_COMM_WORLD, 2, mpiProcs.data(), periods.data(), true, &comm_); + grid_.set_communicator(comm_); } int Pattern::np0() const { return mpiProcs_[0]; } diff --git a/lab11/game_of_life/super_grid.h b/lab11/game_of_life/super_grid.h index 97135f4..90b3152 100644 --- a/lab11/game_of_life/super_grid.h +++ b/lab11/game_of_life/super_grid.h @@ -17,8 +17,12 @@ public: int rows() const; int cols() const; + MPI_Comm& get_communicator(); + void set_communicator(MPI_Comm &communicator); + private: Matrix grid_; + MPI_Comm comm_; }; inline SuperGrid::SuperGrid(const Matrix &other) @@ -32,8 +36,10 @@ inline SuperGrid::SuperGrid(const Matrix &other) } } -inline SuperGrid SuperGrid::zeros(int rows, int cols) { - return SuperGrid(Matrix::zeros(rows, cols)); +inline SuperGrid SuperGrid::zeros(int rows, int cols, MPI_Comm communicator) { + SuperGrid grid = SuperGrid(Matrix::zeros(rows, cols)); + grid.set_communicator(communicator); + return grid; } inline double &SuperGrid::operator()(int i, int j) { @@ -60,4 +66,13 @@ inline const Matrix SuperGrid::get_matrix() const { return mat; } +inline MPI_Comm& SuperGrid::get_communicator() { + return this->comm_; +} + +void set_communicator(MPI_Comm& communicator) +{ + this->comm_ = communicator; +} + #endif // SUPER_GRID_H