updating hw.tex, added src from hw7

git-svn-id: svn://anubis/gvsu@227 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh committed 2008-11-01 16:56:50 +00:00
1 parent 0c6614e89e
commit 446b8464b3
6 files changed
+341 -43

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
TARGETS := gen_adj_matrix
TARGETS += floyd-sequential
TARGETS += floyd-parallel
CXXFLAGS := -fopenmp
#CXXFLAGS += -DPRINT_RESULT
OBJS := $(foreach target,$(TARGETS),$(target).o)
all: $(TARGETS)
$(TARGETS): $(OBJS)
$(CXX) -o $@ $@.o $(CXXFLAGS)
clean:
-rm -f *~ *.o $(TARGETS)
+126
View File
@@ -0,0 +1,126 @@
/* 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++)
{
#pragma omp parallel for
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 */
#ifdef PRINT_RESULT
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");
}
#endif
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;
}
}
}
+125
View File
@@ -0,0 +1,125 @@
/* 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 */
#ifdef PRINT_RESULT
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");
}
#endif
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;
}
}
}
+39
View File
@@ -0,0 +1,39 @@
// gen_adj_matrix.c
// generates random adjacency matrix of desired size and connectivity
// gw
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i, j;
FILE *fp;
int numVertices, avgConnectivity;
// get parameters
if (argc != 3) {
printf ("usage: progName numVertices avgConnectivity\n");
exit(-1);
}
else {
numVertices = atoi(argv[1]);
avgConnectivity = atoi(argv[2]);
}
// open/create output file
if ((fp = fopen ("adjacency.dat", "w")) == NULL) {
printf ("coulnd not create file\n");
exit(-1);
}
// generate random graph/matrix
for (i=0; i < numVertices; i++)
for (j=0; j < numVertices; j++)
if (((rand() % numVertices) < avgConnectivity) && i!=j)
fprintf (fp, "%d ", 1 + (rand() % 20));
else
fprintf (fp, "%d ", 0);
fclose (fp);
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
#!/usr/bin/perl
use strict;
use warnings;
for (my $n = 100; $n <= 1000; $n += 100)
{
print("./gen_adj_matrix $n 6\n");
system('./gen_adj_matrix', $n, 6);
print("./floyd-sequential adjacency.dat\n");
system('./floyd-sequential', 'adjacency.dat');
print("./floyd-parallel adjacency.dat\n");
system('./floyd-parallel', 'adjacency.dat');
}