Skip to content

Instantly share code, notes, and snippets.

@aterga
Created September 18, 2012 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aterga/3743216 to your computer and use it in GitHub Desktop.
Save aterga/3743216 to your computer and use it in GitHub Desktop.
Simple 2d array split method
void GameOfLife::split()
{
// Strategy:
n_x_nodes_ = n_nodes_;
n_y_nodes_ = 1;
int workload_per_node = 0;
int zero_node_workload = 0;
int n_actual_nodes = n_nodes_;
// Taking into account the marginal cases:
if (n_nodes_ > field_->size_x())
{
workload_per_node = (int) ( field_->size_x() / n_nodes_ );
zero_node_workload = 0;
printf("\t\tI'm here! (1)\n");
}
else if (n_nodes_ > 1)
{
workload_per_node = (int) (field_->size_x() / n_nodes_) * (n_nodes_ / (n_nodes_ - 1));
zero_node_workload = field_->size_x() - (n_nodes_ - 1) * workload_per_node;
printf("\t\tI'm here! (2)\n");
}
else
{
workload_per_node = 1;
zero_node_workload = field_->size_x();
n_actual_nodes = field_->size_x() - zero_node_workload + 1;
printf("\t\tI'm here! (3)\n");
}
if (workload_per_node == 0)
{
workload_per_node = 1;
n_actual_nodes = field_->size_x() - zero_node_workload;
printf("\t\tI'm here! (4)\n");
}
else if (zero_node_workload == 0)
{
n_actual_nodes --;
printf("\t\tI'm here! (5)\n");
}
printf("n_nodes_ = %d\nn_actual_nodes = %d\n", n_nodes_, n_actual_nodes);
// Filling the Game's hyper torus with 2-cell-wide boarder overlap:
std::map<int, Matrix *> jbuf;
for (int n = 0; n < n_nodes_; n ++)
{
int workload = n == 0 ? zero_node_workload : workload_per_node;
// Don't forget to init the redundant nodes as well:
if (workload == 0)
{
(*redundant_nodes_)[n] = true;
jbuf[n] = new Matrix;
continue;
}
jbuf[n] = new Matrix(workload + 2, field_->size_y() + 2);
for (int y = -1; y < field_->size_y() + 1; y ++)
{
for (int loc_x = 0,
x = zero_node_workload + (n - 1) * workload - 1;
x < zero_node_workload + n * workload + 1;
loc_x ++, x ++)
{
if (field_->get(x>=0? (x < field_->size_x()? x : 0)
: field_->size_x() - 1,
y>=0? (y < field_->size_y()? y : 0)
: field_->size_y() - 1) == ALIVE)
jbuf[n]->set(loc_x, y + 1, ALIVE);
else
jbuf[n]->set(loc_x, y + 1, DEAD);
}
}
(*node_x_size_)[std::pair<int, int>(n, 0)] = workload;
(*node_y_size_)[std::pair<int, int>(n, 0)] = field_->size_y();
(*redundant_nodes_)[n] = false;
}
// Generate per-node neighbor lists and send all per-node init data:
for (int n = 0; n < n_nodes_; n ++)
{
// The zero node takes the extra-work-load.
send_init_data(n, (n == 0 ? zero_node_workload : workload_per_node) + 2, field_->size_y() + 2, jbuf[n]->data());
delete jbuf[n];
}
}
@aterga
Copy link
Author

aterga commented Nov 7, 2012

This code is part of my GameOfLife project.
Please look into the final sexy implementation here:
https://github.com/WirelessWizard/GameOfLife/blob/master/src/parallel/game_of_life.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment