added neighbors
This commit is contained in:
@@ -5,6 +5,29 @@
|
||||
#include <cstdio>
|
||||
#include <mpi.h>
|
||||
|
||||
class Neighbors {
|
||||
public:
|
||||
int top_left, top, top_right;
|
||||
int left, right;
|
||||
int bottom_left, bottom, bottom_right;
|
||||
|
||||
bool operator==(const Neighbors& other) const {
|
||||
return top_left == other.top_left &&
|
||||
top == other.top &&
|
||||
top_right == other.top_right &&
|
||||
left == other.left &&
|
||||
right == other.right &&
|
||||
bottom_left == other.bottom_left &&
|
||||
bottom == other.bottom &&
|
||||
bottom_right == other.bottom_right;
|
||||
}
|
||||
|
||||
Neighbors()
|
||||
: top_left(-1), top(-1), top_right(-1),
|
||||
left(-1), right(-1),
|
||||
bottom_left(-1), bottom(-1), bottom_right(-1) {}
|
||||
};
|
||||
|
||||
class SuperGrid {
|
||||
public:
|
||||
static SuperGrid zeros(int rows, int cols, MPI_Comm communicator);
|
||||
@@ -19,6 +42,7 @@ public:
|
||||
int rows() const;
|
||||
int cols() const;
|
||||
|
||||
Neighbors get_neighbors();
|
||||
void find_neighbors();
|
||||
|
||||
MPI_Comm &get_communicator();
|
||||
@@ -27,11 +51,13 @@ public:
|
||||
private:
|
||||
Matrix grid_;
|
||||
MPI_Comm comm_;
|
||||
Neighbors neighbors_;
|
||||
};
|
||||
|
||||
inline SuperGrid::SuperGrid(const Matrix &other)
|
||||
: grid_(
|
||||
Matrix::zeros(other.rows() + 2, other.cols() + 2)) // initialize grid_
|
||||
Matrix::zeros(other.rows() + 2, other.cols() + 2)),
|
||||
neighbors_(Neighbors()) // initialize grid_
|
||||
{
|
||||
for (int i = 0; i < other.rows(); i++) {
|
||||
for (int j = 0; j < other.cols(); j++) {
|
||||
@@ -77,12 +103,37 @@ inline void SuperGrid::set_communicator(MPI_Comm communicator) {
|
||||
}
|
||||
|
||||
inline void SuperGrid::find_neighbors() {
|
||||
int left, right;
|
||||
int top, bottom;
|
||||
MPI_Cart_shift(this->comm_, 0, 1, &left, &right);
|
||||
MPI_Cart_shift(this->comm_, 0, 1, &top, &bottom);
|
||||
if (this->comm_ == MPI_COMM_NULL) {
|
||||
std::cerr << "Communicator is NULL!\n";
|
||||
}
|
||||
int rank;
|
||||
MPI_Comm_rank(this->comm_, &rank);
|
||||
int coords[2];
|
||||
MPI_Cart_coords(this->comm_, rank, 2, coords);
|
||||
for (int dx = -1; dx <= 1; ++dx) {
|
||||
for (int dy = -1; dy <= 1; ++dy) {
|
||||
if (dx == 0 && dy == 0) continue; // skip self
|
||||
|
||||
printf("left: %d, right: %d \ntop: %d, bottom: %d", left, right, top, bottom);
|
||||
int neighbor_coords[2] = { coords[0] + dx, coords[1] + dy };
|
||||
int neighbor_rank;
|
||||
|
||||
int err = MPI_Cart_rank(this->comm_, neighbor_coords, &neighbor_rank);
|
||||
if (err != MPI_SUCCESS) continue; // skip invalid neighbors (if non-periodic)
|
||||
|
||||
if (dx == -1 && dy == -1) neighbors_.top_left = neighbor_rank;
|
||||
if (dx == -1 && dy == 0) neighbors_.top = neighbor_rank;
|
||||
if (dx == -1 && dy == 1) neighbors_.top_right = neighbor_rank;
|
||||
if (dx == 0 && dy == -1) neighbors_.left = neighbor_rank;
|
||||
if (dx == 0 && dy == 1) neighbors_.right = neighbor_rank;
|
||||
if (dx == 1 && dy == -1) neighbors_.bottom_left = neighbor_rank;
|
||||
if (dx == 1 && dy == 0) neighbors_.bottom = neighbor_rank;
|
||||
if (dx == 1 && dy == 1) neighbors_.bottom_right = neighbor_rank;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline Neighbors SuperGrid::get_neighbors() {
|
||||
return this->neighbors_;
|
||||
}
|
||||
|
||||
#endif // SUPER_GRID_H
|
||||
|
||||
@@ -5,20 +5,23 @@
|
||||
#include "super_grid.h"
|
||||
#include "utils.h"
|
||||
|
||||
Matrix init_step(SuperGrid start) {
|
||||
Matrix init_step(SuperGrid start)
|
||||
{
|
||||
GameOfLife gol = GameOfLife(start, MPIGridSize());
|
||||
gol.step();
|
||||
return gol.getGrid();
|
||||
}
|
||||
|
||||
TEST(test_underpopulation) {
|
||||
TEST(test_underpopulation)
|
||||
{
|
||||
SuperGrid start = SuperGrid::zeros(10, 10, nullptr);
|
||||
start(0, 0) = 1;
|
||||
Matrix end = init_step(start);
|
||||
check(end(0, 0), 0);
|
||||
}
|
||||
|
||||
TEST(test_survive_2) {
|
||||
TEST(test_survive_2)
|
||||
{
|
||||
SuperGrid start = SuperGrid::zeros(10, 10, nullptr);
|
||||
start(0, 0) = 1;
|
||||
start(0, 1) = 1;
|
||||
@@ -27,7 +30,8 @@ TEST(test_survive_2) {
|
||||
check(end(0, 0), 1);
|
||||
}
|
||||
|
||||
TEST(test_survive_3) {
|
||||
TEST(test_survive_3)
|
||||
{
|
||||
SuperGrid start = SuperGrid::zeros(10, 10, nullptr);
|
||||
start(0, 0) = 1;
|
||||
start(1, 1) = 1;
|
||||
@@ -37,7 +41,8 @@ TEST(test_survive_3) {
|
||||
check(end(0, 0), 1);
|
||||
}
|
||||
|
||||
TEST(test_overpopulation) {
|
||||
TEST(test_overpopulation)
|
||||
{
|
||||
SuperGrid start = SuperGrid::zeros(10, 10, nullptr);
|
||||
start(1, 1) = 1;
|
||||
start(0, 1) = 1;
|
||||
@@ -48,7 +53,8 @@ TEST(test_overpopulation) {
|
||||
check(end(1, 1), 0);
|
||||
}
|
||||
|
||||
TEST(test_reproduction) {
|
||||
TEST(test_reproduction)
|
||||
{
|
||||
SuperGrid start = SuperGrid::zeros(10, 10, nullptr);
|
||||
start(0, 1) = 1;
|
||||
start(1, 0) = 1;
|
||||
@@ -57,7 +63,8 @@ TEST(test_reproduction) {
|
||||
check(end(1, 1), 1);
|
||||
}
|
||||
|
||||
TEST(test_survive_edge) {
|
||||
TEST(test_survive_edge)
|
||||
{
|
||||
SuperGrid start = SuperGrid::zeros(10, 10, nullptr);
|
||||
start(0, 0) = 1;
|
||||
start(9, 0) = 1;
|
||||
@@ -66,26 +73,47 @@ TEST(test_survive_edge) {
|
||||
check(end(0, 0), 1);
|
||||
}
|
||||
|
||||
TEST(test_super_grid) {
|
||||
TEST(test_super_grid)
|
||||
{
|
||||
SuperGrid su_grid = SuperGrid::zeros(10, 10, nullptr);
|
||||
check(su_grid.rows(), 10);
|
||||
check(su_grid.cols(), 10);
|
||||
}
|
||||
|
||||
TEST(test_find_neighbors) {
|
||||
TEST(test_find_neighbors)
|
||||
{
|
||||
int a;
|
||||
char *b[10];
|
||||
MPI_Init(&a, nullptr);
|
||||
|
||||
MPI_Comm comm_;
|
||||
MPIGridSize mpiProcs = {0, 0};
|
||||
int num_procs = 16;
|
||||
MPIGridSize mpiProcs = {4, 4};
|
||||
MPI_Dims_create(num_procs, 2, mpiProcs.data());
|
||||
|
||||
std::array<int, 2> periods = {1, 1};
|
||||
MPI_Cart_create(MPI_COMM_WORLD, 2, mpiProcs.data(), periods.data(), true,
|
||||
&comm_);
|
||||
|
||||
SuperGrid su_grid = SuperGrid::zeros(10, 10, nullptr);
|
||||
SuperGrid su_grid = SuperGrid::zeros(10, 10, comm_);
|
||||
su_grid.find_neighbors();
|
||||
|
||||
int rank;
|
||||
MPI_Comm_rank(comm_, &rank);
|
||||
if (rank == 0)
|
||||
{
|
||||
Neighbors neighbors = su_grid.get_neighbors();
|
||||
Neighbors expected;
|
||||
expected.top_left = 15;
|
||||
expected.top = 12;
|
||||
expected.top_right = 13;
|
||||
expected.left = 3;
|
||||
expected.right = 1;
|
||||
expected.bottom_left = 7;
|
||||
expected.bottom = 4;
|
||||
expected.bottom_right = 5;
|
||||
check(neighbors == expected, true);
|
||||
}
|
||||
MPI_Finalize();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user