added maximum value-finding logic and threaded version and run-threaded script

git-svn-id: svn://anubis/gvsu@165 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-09-25 15:38:46 +00:00
parent e85aa39946
commit 5696656386
4 changed files with 202 additions and 5 deletions

View File

@ -1,8 +1,10 @@
all: sequential PROGS := sequential threaded
all: $(PROGS)
%: %.cc %: %.cc
$(CXX) -o $@ -pthread -O3 $< $(CXX) -o $@ -pthread -O3 $<
clean: clean:
-rm -f sequential *~ *.o -rm -f $(PROGS) *~ *.o

2
cs677/pa2/run-threaded Executable file
View File

@ -0,0 +1,2 @@
#!/bin/sh
time ./threaded -n 2 HIV-1_db.fasta HIV-1_Polymerase.txt

View File

@ -1,13 +1,24 @@
/*
* Josh Holtrop
* 2008-10-01
* Grand Valley State University
* CS677
* Programming Assignment #2
*/
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
using namespace std; using namespace std;
#define eq(x, y) ( ( (x) == (y) ) || ( (x) == '?' ) || ( (y) == '?' ) )
void usage(char * prog); void usage(char * prog);
void similarityMatrix(vector<char> & s, vector<char> & t); void similarityMatrix(vector<char> & s, vector<char> & t);
bool readFile(char * fileName, vector<char> & v); bool readFile(char * fileName, vector<char> & v);
/* Print basic usage information */
void usage(char * prog) void usage(char * prog)
{ {
cout << "Usage: " << prog << " <file1> <file2>" << endl; cout << "Usage: " << prog << " <file1> <file2>" << endl;
@ -27,6 +38,7 @@ int main(int argc, char * argv[])
similarityMatrix(file1, file2); similarityMatrix(file1, file2);
} }
/* Read a file into a vector of non-space characters */
bool readFile(char * fileName, vector<char> & v) bool readFile(char * fileName, vector<char> & v)
{ {
ifstream in(fileName); ifstream in(fileName);
@ -41,30 +53,50 @@ bool readFile(char * fileName, vector<char> & v)
return true; return true;
} }
/* Compute the similarity matrix between two character arrays */
void similarityMatrix(vector<char> & s, vector<char> & t) void similarityMatrix(vector<char> & s, vector<char> & t)
{ {
int s_size = s.size(); int s_size = s.size();
int t_size = t.size(); int t_size = t.size();
int F[s_size][t_size]; int F[s_size][t_size];
for (int i = 0; i < t_size; i++) int max_x = 0, max_y = 0, max_val = 0;
for (int i = 0; i < t_size; i++) /* set first row to 0's */
F[0][i] = 0; F[0][i] = 0;
for (int i = 0; i < s_size; i++) for (int i = 0; i < s_size; i++) /* set first column to 0's */
F[i][0] = 0; F[i][0] = 0;
for (int i = 1; i < s_size; i++) for (int i = 1; i < s_size; i++)
{ {
for (int j = 1; j < t_size; j++) for (int j = 1; j < t_size; j++)
{ {
/* Compute the value for the matrix */
F[i][j] = F[i][j] =
max( max(
max( max(
F[i][j-1] - 2, F[i][j-1] - 2,
F[i-1][j-1] + (s[i] == t[j] ? 1 : -1) F[i-1][j-1] + (eq(s[i], t[j]) ? 1 : -1)
), ),
max( max(
F[i-1][j] - 2, F[i-1][j] - 2,
0 0
) )
); );
/* See if we found a new maximum value */
if (F[i][j] > max_val)
{
max_val = F[i][j];
max_x = i;
max_y = j;
}
else if (F[i][j] == max_val)
{
/* If we found a value the same as our current maximum
* value, see if it has a greater i+j value */
if ( (i + j) > (max_x + max_y) )
{
max_x = i;
max_y = j;
}
}
} }
} }
#if 0 #if 0
@ -78,4 +110,6 @@ void similarityMatrix(vector<char> & s, vector<char> & t)
printf("\n"); printf("\n");
} }
#endif #endif
cout << "Maximum value is " << max_val << " at position ("
<< max_x << ", " << max_y << ")" << endl;
} }

159
cs677/pa2/threaded.cc Normal file
View File

@ -0,0 +1,159 @@
/*
* Josh Holtrop
* 2008-10-01
* Grand Valley State University
* CS677
* Programming Assignment #2
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <pthread.h>
using namespace std;
#define eq(x, y) ( ( (x) == (y) ) || ( (x) == '?' ) || ( (y) == '?' ) )
typedef struct
{
vector<char> * s;
vector<char> * t;
int id;
} SMArg;
void usage(char * prog);
void * similarityMatrix(void * arg);
bool readFile(char * fileName, vector<char> & v);
/* Print basic usage information */
void usage(char * prog)
{
cout << "Usage: " << prog << " [-n <num_threads>] <file1> <file2>" << endl;
exit(42);
}
int main(int argc, char * argv[])
{
vector<char> files[2];
int num_threads = 1;
int file_to_read = 0;
for (int i = 1; i < argc; i++)
{
if ( ! strcmp("-n", argv[i]) )
{
if (i == argc - 1)
usage(argv[0]);
i++;
num_threads = atoi(argv[i]);
}
else
{
if (file_to_read < 2)
readFile(argv[i], files[file_to_read]);
else
usage(argv[0]);
file_to_read++;
}
}
if (file_to_read != 2)
usage(argv[0]);
pthread_t * threads = new pthread_t[num_threads];
for (int i = 0; i < num_threads; i++)
{
SMArg * arg = new SMArg;
arg->s = &files[0];
arg->t = &files[1];
arg->id = i;
int ret = pthread_create(&threads[i], NULL, &similarityMatrix, arg);
if (ret)
{
cerr << "Error " << ret << " when creating thread!" << endl;
return -4;
}
}
for (int i = 0; i < num_threads; i++)
{
pthread_join(threads[i], NULL);
}
delete[] threads;
return 0;
}
/* Read a file into a vector of non-space characters */
bool readFile(char * fileName, vector<char> & v)
{
ifstream in(fileName);
if (!in.is_open())
return false;
while (!in.eof())
{
char chr;
in >> chr;
v.push_back(chr);
}
return true;
}
/* Compute the similarity matrix between two character arrays */
void * similarityMatrix(void * arg)
{
SMArg * smarg = (SMArg *) arg;
int s_size = smarg->s->size();
int t_size = smarg->t->size();
int F[s_size][t_size];
int max_x = 0, max_y = 0, max_val = 0;
for (int i = 0; i < t_size; i++) /* set first row to 0's */
F[0][i] = 0;
for (int i = 0; i < s_size; i++) /* set first column to 0's */
F[i][0] = 0;
for (int i = 1; i < s_size; i++)
{
for (int j = 1; j < t_size; j++)
{
/* Compute the value for the matrix */
F[i][j] =
max(
max(
F[i][j-1] - 2,
F[i-1][j-1] +
(eq(smarg->s->at(i), smarg->t->at(j)) ? 1 : -1)
),
max(
F[i-1][j] - 2,
0
)
);
/* See if we found a new maximum value */
if (F[i][j] > max_val)
{
max_val = F[i][j];
max_x = i;
max_y = j;
}
else if (F[i][j] == max_val)
{
/* If we found a value the same as our current maximum
* value, see if it has a greater i+j value */
if ( (i + j) > (max_x + max_y) )
{
max_x = i;
max_y = j;
}
}
}
}
cout << "Maximum value is " << max_val << " at position ("
<< max_x << ", " << max_y << ")" << endl;
delete smarg;
return NULL;
}