143 lines
2.6 KiB
C++
143 lines
2.6 KiB
C++
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <sys/time.h>
|
|
#include <mpi.h>
|
|
#include <stdlib.h> /* exit() */
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include "Sequence.h"
|
|
#include "PTree.h"
|
|
using namespace std;
|
|
|
|
static vector<Sequence *> sequences;
|
|
static int num_seqs;
|
|
|
|
void permute(int * array, int index);
|
|
void usage();
|
|
string getsequence(int fd);
|
|
void evalPermutation(int * array);
|
|
|
|
void usage()
|
|
{
|
|
cerr << "Usage: maximum-parsimony <sequence-file>" << endl;
|
|
exit(42);
|
|
}
|
|
|
|
string getsequence(int fd)
|
|
{
|
|
string s;
|
|
char chr;
|
|
bool saw_name = false;
|
|
for (;;)
|
|
{
|
|
int bytes_read = read(fd, &chr, 1);
|
|
if (bytes_read < 1)
|
|
{
|
|
if (s == "")
|
|
break;
|
|
else
|
|
{
|
|
cerr << "Unexpected EOF in the middle of a line!" << endl;
|
|
exit(2);
|
|
}
|
|
}
|
|
else if (chr == '\n')
|
|
{
|
|
break;
|
|
}
|
|
else if (chr == ' ' || chr == '\t')
|
|
{
|
|
saw_name = true;
|
|
}
|
|
else
|
|
{
|
|
if (saw_name)
|
|
s += chr;
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
|
|
int main(int argc, char * argv[])
|
|
{
|
|
int my_rank;
|
|
int world_size; /* the number of processes */
|
|
char * filename = NULL;
|
|
|
|
MPI_Init(&argc, &argv);
|
|
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
|
|
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
{
|
|
filename = argv[i];
|
|
}
|
|
|
|
if (filename == NULL)
|
|
usage();
|
|
|
|
int fd = open(filename, O_RDONLY);
|
|
if (fd < 0)
|
|
{
|
|
cerr << "Couldn't open '" << filename << "'!" << endl;
|
|
exit(1);
|
|
}
|
|
|
|
for (;;)
|
|
{
|
|
string seqstr = getsequence(fd);
|
|
if (seqstr == "")
|
|
break;
|
|
Sequence * seq = new Sequence(seqstr.c_str());
|
|
sequences.push_back(seq);
|
|
}
|
|
|
|
close(fd);
|
|
|
|
num_seqs = sequences.size();
|
|
int indices[num_seqs];
|
|
for (int i = 0; i < num_seqs; i++)
|
|
indices[i] = 0;
|
|
permute(&indices[0], 0);
|
|
|
|
MPI_Finalize();
|
|
|
|
return 0;
|
|
}
|
|
|
|
void evalPermutation(int * array)
|
|
{
|
|
for (int i = 0; i < num_seqs; i++)
|
|
{
|
|
cout << array[i] << " ";
|
|
}
|
|
cout << endl;
|
|
}
|
|
|
|
void permute(int * array, int index)
|
|
{
|
|
static int level = -1;
|
|
level++;
|
|
array[index] = level;
|
|
|
|
if (level == num_seqs)
|
|
{
|
|
evalPermutation(array);
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < num_seqs; i++)
|
|
{
|
|
if (array[i] == 0)
|
|
{
|
|
permute(array, i);
|
|
}
|
|
}
|
|
}
|
|
|
|
level--;
|
|
array[index] = 0;
|
|
}
|