implemented distributing work using MPI, finding lowest cost node
git-svn-id: svn://anubis/gvsu@304 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
abeca4599d
commit
45d41d04ae
@ -4,6 +4,7 @@
|
||||
#include <sys/time.h>
|
||||
#include <mpi.h>
|
||||
#include <stdlib.h> /* exit() */
|
||||
#include <limits.h> /* INT_MAX */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
@ -13,6 +14,10 @@ using namespace std;
|
||||
|
||||
static vector<Sequence *> 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<Sequence *> 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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user