added ElementSet, Sequence, skeleton maximum-parsimony.cc, updated Makefile

git-svn-id: svn://anubis/gvsu@298 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-11-30 18:26:53 +00:00
parent 7b61c6fca9
commit 2e3b5abd0f
6 changed files with 81 additions and 134 deletions

12
cs677/pa4/ElementSet.cc Normal file
View File

@ -0,0 +1,12 @@
#include "ElementSet.h"
using namespace std;
ElementSet::ElementSet()
{
}
ElementSet::ElementSet(char initial)
{
mySet.insert(initial);
}

17
cs677/pa4/ElementSet.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef ELEMENTSET_H
#define ELEMENTSET_H ELEMENTSET_H
#include <set>
class ElementSet
{
private:
std::set<char> mySet;
public:
ElementSet();
ElementSet(char initial);
};
#endif

View File

@ -1,11 +1,17 @@
FILE := maximum-parsimony
TARGET := $(FILE)
CXX := mpiCC
TARGET := maximum-parsimony
OBJS := Sequence.o
OBJS += ElementSet.o
OBJS += maximum-parsimony.o
all: $(TARGET)
$(TARGET): $(FILE).cc
mpiCC -o $@ $<
$(TARGET): $(OBJS)
mpiCC -o $@ $^
%.o: %.cc
$(CXX) -c -o $@ $<
clean:
-rm -f *.o *~ $(TARGET)

21
cs677/pa4/Sequence.cc Normal file
View File

@ -0,0 +1,21 @@
#include <cstring>
#include "Sequence.h"
#include "ElementSet.h"
using namespace std;
Sequence::Sequence()
{
myMutationCount = 0;
}
Sequence::Sequence(char * initializer)
{
myMutationCount = 0;
int len = strlen(initializer);
myElements = vector<ElementSet>(len);
for (int i = 0; i < len; i++)
{
myElements[i] = ElementSet(initializer[i]);
}
}

19
cs677/pa4/Sequence.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef SEQUENCE_H
#define SEQUENCE_H SEQUENCE_H
#include <vector>
#include "ElementSet.h"
class Sequence
{
private:
int myMutationCount;
std::vector<ElementSet> myElements;
public:
Sequence();
Sequence(char * initializer);
};
#endif

View File

@ -2,144 +2,16 @@
#include <iostream>
#include <sys/time.h>
#include <mpi.h>
#include <unistd.h> /* usleep() */
using namespace std;
/*
* taskAllocate() will divide a set of total_tasks tasks into
* total_workers groups, as evenly as possible
* Parameters:
* total_tasks : IN : the total number of tasks to divide up
* total_workers : IN : the total number of workers to allocate tasks to (>0)
* this_id : IN : the id (0-based) of the task calling us for work
* first_task_id : OUT : the id (0-based) of the first task for this worker
* num : OUT : the number of tasks assigned to this worker
*/
void taskAllocate(int total_tasks, int total_workers, int this_id,
int * first_task_id, int * num)
{
int l_num;
int leftovers = total_tasks % total_workers; /* num of "leftover" tasks */
if (this_id < leftovers)
{
l_num = total_tasks / total_workers + 1; /* do one of the leftovers */
*first_task_id = l_num * this_id;
}
else
{
l_num = total_tasks / total_workers;
*first_task_id = l_num * this_id + leftovers;
}
*num = l_num;
}
void printMatrix(int * matrix, int width, int height)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
cout << *matrix++ << " ";
}
cout << endl;
}
}
int main(int argc, char * argv[])
{
int my_rank;
int p; /* the number of processes */
int n = 10; /* the size of the matrix */
int world_size; /* the number of processes */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
/* Initialize the matrices */
int matrix[n][n];
int recvmatrix[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
matrix[i][j] = 0; /* zero the matrices for prettier */
recvmatrix[i][j] = 0; /* printing */
}
}
/* Determine which rows I am responsible for and initialize them */
int my_first_row;
int my_num_rows;
taskAllocate(n, p, my_rank, &my_first_row, &my_num_rows);
for (int row = my_first_row; row < my_first_row + my_num_rows; row++)
{
for (int j = 0; j < n; j++)
{
matrix[row][j] = 100 * (row + 1) + (j + 1);
}
}
/* Print the initial matrices */
if (my_rank == 0)
cout << " *** Initial Matrices ***" << endl;
for (int i = 0; i < p; i++)
{
if (my_rank == i)
{
cout << "Process " << i << " matrix:" << endl;
printMatrix(&matrix[0][0], n, n);
cout << endl;
}
MPI_Barrier(MPI_COMM_WORLD); /* just for printing coherently */
usleep(100000);
MPI_Barrier(MPI_COMM_WORLD);
}
/* Populate the displacements array */
int displs[p];
int counts[p];
for (int i = 0, total = 0; i < p; i++)
{
int first;
int count;
taskAllocate(n, p, i, &first, &count);
displs[i] = total;
counts[i] = count;
total += count;
}
/* Transpose the matrix with n gather operations */
for (int i = 0, toproc = 0, proccount = counts[0]; i < n; i++)
{
int my_col_i_vals[my_num_rows];
for (int row_offset = 0; row_offset < my_num_rows; row_offset++)
my_col_i_vals[row_offset] = matrix[my_first_row + row_offset][i];
MPI_Gatherv(&my_col_i_vals[0], my_num_rows, MPI_INT,
&recvmatrix[i][0], &counts[0], &displs[0],
MPI_INT, toproc, MPI_COMM_WORLD);
proccount--;
if (proccount <= 0)
{
toproc++;
proccount = counts[toproc];
}
}
/* Print the transposed matrices */
if (my_rank == 0)
cout << " *** Final Transposed Matrices ***" << endl;
for (int i = 0; i < p; i++)
{
if (my_rank == i)
{
cout << "Process " << i << " matrix:" << endl;
printMatrix(&recvmatrix[0][0], n, n);
cout << endl;
}
MPI_Barrier(MPI_COMM_WORLD); /* just for printing coherently */
usleep(100000);
MPI_Barrier(MPI_COMM_WORLD);
}
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
MPI_Finalize();