Skip to content

Commit

Permalink
Minor changes and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaletap committed Dec 2, 2019
1 parent 231db3f commit e277449
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/cpu/bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using namespace std;


void bfsCPU(int start, Graph &G, std::vector<int> &distance, std::vector<bool> &visited) {
fill(distance.begin(), distance.end(), INT_MAX);
distance[start] = 0;
visited[start] = true;
queue<int> to_visit;
Expand Down
4 changes: 3 additions & 1 deletion src/cpu/bfs.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef BFS_CPU_H
#define BFS_CPU_H

#include "../graph/graph.h"
#include <queue>
#include <bits/stdc++.h>

#include "../graph/graph.h"

/*
* start - vertex number from which traversing a graph starts
Expand Down
3 changes: 1 addition & 2 deletions src/gpu/simple/bfs.cu → src/gpu/simple/bfs_simple.cu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "bfs.cuh"
#include "bfs_simple.cuh"

using namespace std;

Expand Down Expand Up @@ -102,7 +102,6 @@ void bfsGPU(int start, Graph &G, vector<int> &distance, vector<bool> &visited) {
}
computeNextQueue<<<N, 1>>> (d_adjacencyList, d_edgesOffset, d_edgesSize, d_distance,
currentQueueSize, d_currentQueue, d_nextQueueSize, d_nextQueue, level);
cudaDeviceSynchronize();
++level;
cudaMemcpy(&currentQueueSize, d_nextQueueSize, sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(d_nextQueueSize, &nextQueueSize, sizeof(int), cudaMemcpyHostToDevice);
Expand Down
File renamed without changes.
34 changes: 28 additions & 6 deletions src/graph/graph.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
#include <ctime>

#include "graph.h"


using namespace std;


void print_vector(string title, vector<int> &v);


Graph::Graph(Format format, Direction direction) {
switch (format) {
case Empty:
return;
break;
case Edges: {
int numVertices, numEdges;
vector<vector<int>> adjacencyList(numVertices);
cout << "Started reading graph" << endl;
cin >> numVertices >> numEdges;
vector<vector<int>> adjacencyList(numVertices);
int v, w;
for (int i = 0; i < numEdges; ++i) {
cin >> v >> w;
adjacencyList[v].push_back(w);
if (direction == Undirected)
if (direction == Undirected) {
adjacencyList[w].push_back(v);
}
}
this->init(adjacencyList, numEdges);
cout << "Finished reading graph" << endl;
break;
}
case AdjacencyList: {
int numVertices, numEdges;
cout << "Started reading graph" << endl;
cin >> numVertices >> numEdges;
vector<vector<int>> adjacencyList(numVertices);
string line;
Expand All @@ -36,11 +41,14 @@ Graph::Graph(Format format, Direction direction) {
int w;
while (splitter >> v) {
adjacencyList[v].push_back(w);
if (direction == Undirected)
if (direction == Undirected) {
cerr << "Warning: You are reading graph using adjacency list as undirected";
adjacencyList[w].push_back(v);
}
}
}
this->init(adjacencyList, numEdges);
cout << "Finished reading graph" << endl;
break;
}
}
Expand Down Expand Up @@ -76,7 +84,6 @@ Graph::Graph(vector<vector<int>> adjacencyList) {
}



void Graph::init(vector<vector<int>> adjacencyList, int numEdges) {
const int numVertices = adjacencyList.size();
// Creation of single vector adjacency list
Expand All @@ -92,5 +99,20 @@ void Graph::init(vector<vector<int>> adjacencyList, int numEdges) {
}


void Graph::print() {
printf("Graph(numVertices = %i, numEdges = %i)\n", numVertices, numEdges);
print_vector("AdjacencyList:", adjacencyList);
print_vector("edgesOffset:", edgesOffset);
print_vector("edgesSize:", edgesSize);
}


void print_vector(string title, vector<int> &v) {
cout << title << " { ";
for (int i = 0; i < v.size(); ++i) {
cout << v[i];
if (i < v.size() - 1)
cout << ", ";
}
cout << " }" << endl;
}
3 changes: 3 additions & 0 deletions src/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GRAPH_H

#include <vector>
#include <string>
#include <iostream>
#include <sstream>

Expand All @@ -19,6 +20,8 @@ class Graph {
int numVertices;
int numEdges;

void print();

private:
Graph(std::vector<std::vector<int>> adjacencyList);
void init(std::vector<std::vector<int>> adjacencyList, int numEdges);
Expand Down
51 changes: 48 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,48 @@

#include "graph/graph.h"
#include "cpu/bfs.h"
#include "gpu/simple/bfs.cuh"
#include "gpu/simple/bfs_simple.cuh"

using namespace std;


void print(vector<int> &v);


class Checker {
vector<int> expected_answer;
public:
Checker(vector<int> exp_ans): expected_answer(exp_ans) {}
void check(vector<int> answer) {
assert(answer.size() == expected_answer.size());
bool is_ok = true;
int position_wrong = -1;
for (int i = 0; i < answer.size(); ++i) {
if (answer.at(i) != expected_answer.at(i)) {
is_ok = false;
position_wrong = i;
}
}
if (is_ok) {
printf("CHECKED SUCCESSFULY!\n");
}
else {
printf("Something went wrong!\n");
printf("Answer at %i equals %i but should be equal to %i", position_wrong, answer[position_wrong], expected_answer[position_wrong]);
}
}
};


// Tests speed of a BFS algorithm
int main() {
Graph G(AdjacencyList, Directed);
int main() { // TODO: Add arguments to main program (type of graph, file path)
Graph G(Edges, Undirected);
int startVertex;
vector<int> distance;
vector<bool> visited;

G.print();

startVertex = 1;

// CPU
Expand All @@ -26,6 +56,8 @@ int main() {
long duration = chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count();
printf("Elapsed time for CPU implementation : %li ms.\n", duration);

Checker checker(distance);

// Simple GPU
distance = vector<int>(G.numVertices);
visited = vector<bool>(G.numVertices);
Expand All @@ -35,5 +67,18 @@ int main() {
duration = chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count();
printf("Elapsed time for naive linear GPU implementation : %li ms.\n", duration);

checker.check(distance);

return 0;
}


void print(vector<int> &v) {
cout << "{ ";
for (int i = 0; i < v.size(); ++i) {
cout << v[i];
if (i < v.size() - 1)
cout << ", ";
}
cout << " }" << endl;
}
14 changes: 12 additions & 2 deletions test/test_bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include <vector>

#include "../src/cpu/bfs.h"
#include "../src/gpu/simple/bfs.cuh"
#include "util.h"
#include "../src/gpu/simple/bfs_simple.cuh"

#define DEBUG(x)

Expand All @@ -27,6 +26,17 @@ class BFSTest : public ::testing::Test {
};


void print(vector<int> &v) {
cout << "{ ";
for (int i = 0; i < v.size(); ++i) {
cout << v[i];
if (i < v.size() - 1)
cout << ", ";
}
cout << " }" << endl;
}


TEST_F(BFSTest, BFS_CPU_Test_start0) {
start = 0;
expectedDistance = vector<int>( {0, 1, 1, 2, 3} );
Expand Down
1 change: 0 additions & 1 deletion test/test_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <iostream>

#include "../src/graph/graph.h"
#include "util.h"

#define DEBUG(x)

Expand Down
35 changes: 0 additions & 35 deletions test/util.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions test/util.h

This file was deleted.

0 comments on commit e277449

Please sign in to comment.