added communicator

This commit is contained in:
WickedJack99
2025-06-15 15:36:33 +02:00
parent 5da5776d5c
commit 0e1cb31f2e
4 changed files with 35 additions and 10 deletions

View File

@@ -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) {

View File

@@ -4,6 +4,7 @@
#include <memory>
#include <mpi.h>
#include <string>
#include <array>
#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 <np0> <np1>\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;

View File

@@ -19,6 +19,7 @@ Pattern::Pattern(int rows, int cols, MPIGridSize mpiProcs)
std::array<int, 2> 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]; }

View File

@@ -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