-
-
Save cra/3743229 to your computer and use it in GitHub Desktop.
Simple 2d array split method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void GameOfLife::split() | |
{ | |
// Strategy: | |
n_x_nodes_ = n_nodes_; | |
n_y_nodes_ = 1; | |
int workload_per_node = 0; | |
int zero_node_workload = 0; | |
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; | |
} else { | |
workload_per_node = 0; | |
zero_node_workload = field_->size_x(); | |
} | |
Matrix *jbuf = new Matrix(zero_node_workload + 2, field_->size_y() + 2); | |
// n = 0 | |
for (int y = -1; y < field_->size_y() + 1; y ++) { | |
//printf("Y#%2d: ", y); | |
for (int x = -1; x < zero_node_workload + 1; x ++) { | |
if (field_->get( x>=0? x : field_->size_x()-1, \ | |
y>=0? (y < field_->size_y()? y : 0) \ | |
: field_->size_y()-1) == ALIVE) { | |
jbuf->set(x + 1, y + 1, ALIVE); | |
} | |
else jbuf->set(x + 1, y + 1, DEAD); | |
} | |
} | |
//jbuf->print(); | |
(*node_x_size_)[std::pair<int, int>(0, 0)] = zero_node_workload + 2; | |
(*node_y_size_)[std::pair<int, int>(0, 0)] = field_->size_y() + 2; | |
send_init_data(0, 0, 0, zero_node_workload + 2, field_->size_y() + 2, jbuf->data()); | |
delete jbuf; | |
for (int n = 1; n < n_nodes_; n ++) { | |
jbuf = new Matrix(workload_per_node + 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_per_node - 1; | |
x < zero_node_workload + n * workload_per_node + 1; | |
loc_x ++, x ++) { | |
if (field_->get(x>field_->size_x()-1? 0 : x, | |
y>=0? (y < field_->size_y()? y : 0) | |
: field_->size_y()-1) == ALIVE) { | |
jbuf->set(loc_x, y + 1, ALIVE); | |
} else { | |
jbuf->set(loc_x, y + 1, DEAD); | |
} | |
} | |
} | |
//jbuf->print(); | |
(*node_x_size_)[std::pair<int, int>(n, 0)] = workload_per_node + 2; | |
(*node_y_size_)[std::pair<int, int>(n, 0)] = field_->size_y() + 2; | |
send_init_data(n, n, 0, workload_per_node + 2, field_->size_y() + 2, jbuf->data()); | |
delete jbuf; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment