This commit is contained in:
WickedJack99
2025-06-16 22:01:29 +02:00
parent ed7896349f
commit 44a3fcb943
4 changed files with 10 additions and 9 deletions

View File

@@ -7,7 +7,7 @@ void GameOfLife::step() {
SuperGrid next = SuperGrid::zeros(grid_.rows(), grid_.cols(), grid_.get_communicator());
const int rows = grid_.rows();
const int cols = grid_.cols();
//grid_.update();
grid_.update();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
const int numLiveNeighbors = countLiveNeighbors(i, j);

View File

@@ -1,8 +1,8 @@
#include "patterns.h"
Pattern::Pattern(int rows, int cols, MPIGridSize mpiProcs)
: mpiProcs_(mpiProcs),
grid_(SuperGrid::zeros(rows / np0(), cols / np1(), nullptr)) {
: mpiProcs_(mpiProcs),
grid_(SuperGrid(Matrix::zeros(rows / np0(), cols / np1()))) {
if (rows <= 0 || cols <= 0) {
throw std::invalid_argument("Rows and columns must be positive");
}
@@ -21,6 +21,7 @@ Pattern::Pattern(int rows, int cols, MPIGridSize mpiProcs)
MPI_Cart_create(MPI_COMM_WORLD, 2, mpiProcs.data(), periods.data(), true,
&comm_);
grid_.set_communicator(comm_);
grid_.find_neighbors();
}
int Pattern::np0() const { return mpiProcs_[0]; }

View File

@@ -1,10 +1,10 @@
#include <mpi.h>
#include "super_grid.h"
std::vector<MPI_Request> SuperGrid::receive_halos(HaloLayers halo_layers)
std::vector<MPI_Request> SuperGrid::receive_halos(HaloLayers& halo_layers)
{
std::vector<MPI_Request> recv_requests(8);
std::cout << halo_layers.top_halo.size() << std::endl;
std::cout << halo_layers.top_halo.size() << "top halo size" << std::endl;
MPI_Irecv(halo_layers.top_halo.data(), halo_layers.top_halo.size(), MPI_DOUBLE, neighbors_.top, neighbors_.top, comm_, &recv_requests[0]);
MPI_Irecv(halo_layers.right_halo.data(), halo_layers.right_halo.size(), MPI_DOUBLE, neighbors_.right, neighbors_.right, comm_, &recv_requests[1]);
MPI_Irecv(halo_layers.bottom_halo.data(), halo_layers.bottom_halo.size(), MPI_DOUBLE, neighbors_.bottom, neighbors_.bottom, comm_, &recv_requests[2]);
@@ -96,7 +96,7 @@ std::vector<MPI_Request> SuperGrid::inform_neighbors()
}
std::vector<MPI_Request> send_requests(8);
std::cout << inner_top_row.size() << "inform_neighbors" << std::endl;
std::cout << inner_bottom_row.size() << "inner_bottom_row" << std::endl;
MPI_Isend(inner_top_row.data(), inner_top_row.size(), MPI_DOUBLE, neighbors_.top, rank_, comm_, &send_requests[0]);
MPI_Isend(inner_right_column.data(), inner_right_column.size(), MPI_DOUBLE, neighbors_.right, rank_, comm_, &send_requests[1]);
MPI_Isend(inner_bottom_row.data(), inner_bottom_row.size(), MPI_DOUBLE, neighbors_.bottom, rank_, comm_, &send_requests[2]);

View File

@@ -79,7 +79,7 @@ public:
void set_communicator(MPI_Comm communicator);
void update();
std::vector<MPI_Request> receive_halos(HaloLayers halo_layers);
std::vector<MPI_Request> receive_halos(HaloLayers& halo_layers);
std::vector<MPI_Request> inform_neighbors();
std::vector<double> get_inner_top_row();
@@ -113,13 +113,13 @@ inline SuperGrid::SuperGrid(const Matrix &other)
grid_(i + 1, j + 1) = other(i, j); // copy into grid_ directly
}
}
MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
}
inline SuperGrid SuperGrid::zeros(int rows, int cols, MPI_Comm communicator)
{
SuperGrid grid = SuperGrid(Matrix::zeros(rows, cols));
grid.set_communicator(communicator);
MPI_Comm_rank(MPI_COMM_WORLD, &grid.rank_);
grid.find_neighbors();
return grid;
}
@@ -167,7 +167,7 @@ inline void SuperGrid::find_neighbors()
std::cerr << "Communicator is NULL!\n";
}
int rank;
MPI_Comm_rank(this->comm_, &rank);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int coords[2];
MPI_Cart_coords(this->comm_, rank, 2, coords);
for (int dx = -1; dx <= 1; ++dx)