added floyd-parallel.cc
git-svn-id: svn://anubis/gvsu@197 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
07c119d290
commit
9b33e87a49
122
cs677/hw7/src/floyd-parallel.cc
Normal file
122
cs677/hw7/src/floyd-parallel.cc
Normal file
@ -0,0 +1,122 @@
|
||||
|
||||
/* Josh Holtrop
|
||||
* 2008-10-15
|
||||
* CS 677
|
||||
* Grand Valley State University
|
||||
*/
|
||||
|
||||
#include <omp.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <sys/time.h> /* gettimeofday(), struct timeval */
|
||||
using namespace std;
|
||||
|
||||
void usage(char * progname);
|
||||
int readFile(char * fileName, vector<int> & v);
|
||||
void convertToMatrix(const vector<int> & v, int num_verts, int * vals);
|
||||
|
||||
void usage(char * progname)
|
||||
{
|
||||
cout << "Usage: " << progname << " <adjacency-file>" << endl;
|
||||
exit(42);
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
if (argc < 1)
|
||||
usage(argv[0]);
|
||||
|
||||
vector<int> 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<int> & 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<int> & 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user