From 9b33e87a4933bc83a01a192d56d2fa24d1479c93 Mon Sep 17 00:00:00 2001 From: josh Date: Mon, 13 Oct 2008 22:22:22 +0000 Subject: [PATCH] added floyd-parallel.cc git-svn-id: svn://anubis/gvsu@197 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs677/hw7/src/floyd-parallel.cc | 122 ++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 cs677/hw7/src/floyd-parallel.cc diff --git a/cs677/hw7/src/floyd-parallel.cc b/cs677/hw7/src/floyd-parallel.cc new file mode 100644 index 0000000..f1effab --- /dev/null +++ b/cs677/hw7/src/floyd-parallel.cc @@ -0,0 +1,122 @@ + +/* Josh Holtrop + * 2008-10-15 + * CS 677 + * Grand Valley State University + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include /* gettimeofday(), struct timeval */ +using namespace std; + +void usage(char * progname); +int readFile(char * fileName, vector & v); +void convertToMatrix(const vector & v, int num_verts, int * vals); + +void usage(char * progname) +{ + cout << "Usage: " << progname << " " << endl; + exit(42); +} + +int main(int argc, char * argv[]) +{ + if (argc < 1) + usage(argv[0]); + + vector v; + + int num_verts = readFile(argv[1], v); + int D[2][num_verts][num_verts]; + convertToMatrix(v, num_verts, (int *) &D[0]); + + struct timeval before, after; + gettimeofday(&before, NULL); /* Start timing */ + + /* Run Floyd's Algorithm on D */ + for (int k = 1; k <= num_verts; k++) + { + for (int i = 0; i < num_verts; i++) + { + for (int j = 0; j < num_verts; j++) + { + int distWithoutK = D[(k-1) & 1][i][j]; + int distItoK = D[(k-1) & 1][i][k-1]; + int distKtoJ = D[(k-1) & 1][k-1][j]; + int distWithK = + (distItoK == INT_MAX || distKtoJ == INT_MAX) + ? INT_MAX + : distItoK + distKtoJ; + D[k & 1][i][j] = min( + distWithoutK, + distWithK + ); + } + } + } + + gettimeofday(&after, NULL); /* Stop timing */ + cout << "Result:" << endl; + /* Print out the final matrix */ + for (int i = 0; i < num_verts; i++) + { + for (int j = 0; j < num_verts; j++) + { + if (D[num_verts & 1][i][j] == INT_MAX) + printf("-- "); + else + printf("%2d ", D[num_verts & 1][i][j]); + } + printf("\n"); + } + + double time_before = before.tv_sec + before.tv_usec / 1000000.0; + double time_after = after.tv_sec + after.tv_usec / 1000000.0; + double diff = time_after - time_before; + cout << "Elapsed time: " << diff << " seconds." << endl; + + return 0; +} + +int readFile(char * fileName, vector & v) +{ + ifstream in(fileName); + if (!in.is_open()) + { + cerr << "Error opening " << fileName << endl; + return -1; + } + + for (;;) + { + int weight; + in >> weight; + if (in.eof()) + break; + v.push_back(weight); + } + + return (int) sqrt(v.size()); +} + +void convertToMatrix(const vector & v, int num_verts, int * vals) +{ + int vidx = 0; + int (*V)[num_verts][num_verts] = (int (*)[num_verts][num_verts]) vals; + for (int i = 0; i < num_verts; i++) + { + for (int j = 0; j < num_verts; j++) + { + (*V)[i][j] = v[vidx++]; + if ((*V)[i][j] == 0) + (*V)[i][j] = INT_MAX; + } + } +}