From 45d41d04ae73e6cef76efc56b92c3893c78054c6 Mon Sep 17 00:00:00 2001 From: josh Date: Mon, 1 Dec 2008 00:12:01 +0000 Subject: [PATCH] implemented distributing work using MPI, finding lowest cost node git-svn-id: svn://anubis/gvsu@304 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs677/pa4/maximum-parsimony.cc | 57 +++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/cs677/pa4/maximum-parsimony.cc b/cs677/pa4/maximum-parsimony.cc index 2546da1..9743e61 100644 --- a/cs677/pa4/maximum-parsimony.cc +++ b/cs677/pa4/maximum-parsimony.cc @@ -4,6 +4,7 @@ #include #include #include /* exit() */ +#include /* INT_MAX */ #include #include #include @@ -13,6 +14,10 @@ using namespace std; static vector sequences; static int num_seqs; +static int my_rank; +static int world_size; /* the number of processes */ +static int lowest_cost = INT_MAX; +static PTree * lowest_cost_tree = NULL; void permute(int * array, int index); void usage(); @@ -62,8 +67,6 @@ string getsequence(int fd) int main(int argc, char * argv[]) { - int my_rank; - int world_size; /* the number of processes */ char * filename = NULL; MPI_Init(&argc, &argv); @@ -102,6 +105,32 @@ int main(int argc, char * argv[]) indices[i] = 0; permute(&indices[0], 0); + /* after all processes have evaluated their trees, figure out + * which process discovered the lowest cost tree */ + int processes_lowest_costs[world_size]; + MPI_Gather(&lowest_cost, 1, MPI_INT, + &processes_lowest_costs[0], world_size, MPI_INT, + 0, MPI_COMM_WORLD); + int min_process = 0; + if (my_rank == 0) + { + cout << "Lowest costs determined by processes:" << endl; + for (int i = 0; i < world_size; i++) + { + cout << processes_lowest_costs[i] << " "; + if (processes_lowest_costs[i] < processes_lowest_costs[min_process]) + min_process = i; + } + cout << endl; + } + MPI_Bcast(&min_process, 1, MPI_INT, 0, MPI_COMM_WORLD); + if (my_rank == min_process) + { + cout << "Process " << my_rank << " lowest-cost tree:" << endl; + cout << (*lowest_cost_tree) << endl; + } + delete lowest_cost_tree; + MPI_Finalize(); return 0; @@ -109,11 +138,29 @@ int main(int argc, char * argv[]) void evalPermutation(int * array) { - for (int i = 0; i < num_seqs; i++) + static int invocation = 0; + bool save_tree = false; + PTree * tree; + if (invocation % world_size == my_rank) { - cout << array[i] << " "; + /* I am responsible for evaluating this permutation */ + vector theseLeaves(num_seqs); + for (int i = 0; i < num_seqs; i++) + theseLeaves[i] = sequences[array[i] - 1]; + tree = new PTree(theseLeaves); + int cost = tree->getCount(); + if (cost < lowest_cost) + { + lowest_cost = cost; + if (lowest_cost_tree != NULL) + delete lowest_cost_tree; + lowest_cost_tree = tree; + save_tree = true; + } } - cout << endl; + if (!save_tree) + delete tree; + invocation++; } void permute(int * array, int index)