Skip to content

Commit

Permalink
Have iniitalization of graph and metric obtainment
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Jenkins committed Jan 4, 2021
1 parent 9219c0e commit 7a0efdf
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 99 deletions.
7 changes: 7 additions & 0 deletions src/RGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class RGraph : public Rideable{
* @param fn Predicate function that performs work on outgoing edge and returns whether or not to continue processing.
*/
virtual void for_each_incoming(int vid, std::function<bool(int)> fn) = 0;

/**
* @brief Obtains statistics including (|V|, |E|, average degree, vertex degrees, vertex degrees length)
*
* @return std::tuple<int, int, double, int *> Tuple of |V|, |E|, average degree, and histogram
*/
virtual std::tuple<int, int, double, int *, int> grab_stats() = 0;
};


Expand Down
26 changes: 15 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <thread>
#include <random>
#include <time.h>
#include <ratio>
#include <sys/time.h>

#include "ConcurrentPrimitives.hpp"
Expand Down Expand Up @@ -44,9 +45,9 @@
#include "NatarajanTree.hpp"

#include "TGraph.hpp"
#include "NVMGraph.hpp"
#include "DLGraph.hpp"
#include "MontageGraph.hpp"
// #include "NVMGraph.hpp"
// #include "DLGraph.hpp"
// #include "MontageGraph.hpp"
#endif

#ifdef MNEMOSYNE
Expand All @@ -73,18 +74,21 @@
#include "SyncTest.hpp"
#ifndef MNEMOSYNE
#include "RecoverVerifyTest.hpp"
#include "GraphRecoveryTest.hpp"
// #include "GraphRecoveryTest.hpp"
#include "TGraphConstructionTest.hpp"
#include "ToyTest.hpp"
#endif /* !MNEMOSYNE */
#include "CustomTypes.hpp"

using namespace std;


int main(int argc, char *argv[])
{
const size_t numVertices = 1024 * 1024;
GlobalTestConfig gtc;
const size_t numVertices = 1024;
const size_t meanEdgesPerVertex = 32;
const size_t vertexLoad = 50;

/* queues */
// gtc.addRideableOption(new MSQueueFactory<string>(), "MSQueue");//transient
Expand All @@ -111,13 +115,13 @@ int main(int argc, char *argv[])
gtc.addRideableOption(new MontageNatarajanTreeFactory<string>(), "MontageNataTree");

/* graphs */
gtc.addRideableOption(new TGraphFactory<numVertices>(), "TGraph");
gtc.addRideableOption(new NVMGraphFactory<numVertices>(), "NVMGraph");
gtc.addRideableOption(new TGraphFactory<numVertices, meanEdgesPerVertex, vertexLoad>(), "TGraph");
// gtc.addRideableOption(new NVMGraphFactory<numVertices>(), "NVMGraph");
// gtc.addRideableOption(new DLGraphFactory<numVertices>(), "DLGraph");
gtc.addRideableOption(new MontageGraphFactory<numVertices>(), "MontageGraph");
// gtc.addRideableOption(new MontageGraphFactory<numVertices>(), "MontageGraph");

gtc.addRideableOption(new MontageGraphFactory<3072627>(), "Orkut");
gtc.addRideableOption(new TGraphFactory<3076727>(), "TransientOrkut");
// gtc.addRideableOption(new MontageGraphFactory<3072627>(), "Orkut");
gtc.addRideableOption(new TGraphFactory<3076727, 0, 100>(), "TransientOrkut");
#endif /* !defined(MNEMOSYNE) and !defined(PRONTO) */
#ifdef MNEMOSYNE
gtc.addRideableOption(new MneQueueFactory<string>(), "MneQueue");
Expand All @@ -142,7 +146,7 @@ int main(int argc, char *argv[])

gtc.addTestOption(new GraphTest(1000000,numVertices,33,33,33, 1), "GraphTest:1m:i33r33l33:c1");
gtc.addTestOption(new GraphTest(1000000,numVertices,25,25,25,25), "GraphTest:1m:i25r25l25:c25");
gtc.addTestOption(new GraphRecoveryTest("graph_data/", "orkut-edge-list_", 28610, 5, true), "GraphRecoveryTest:Orkut:verify");
// gtc.addTestOption(new GraphRecoveryTest("graph_data/", "orkut-edge-list_", 28610, 5, true), "GraphRecoveryTest:Orkut:verify");
// gtc.addTestOption(new GraphRecoveryTest("graph_data/", "orkut-edge-list_", 28610, 5, false), "GraphRecoveryTest:Orkut:noverify");
gtc.addTestOption(new TGraphConstructionTest("graph_data/", "orkut-edge-list_", 28610, 5), "TGraphConstructionTest:Orkut");
#endif /* !MNEMOSYNE */
Expand Down
147 changes: 105 additions & 42 deletions src/rideables/TGraph.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Author: Louis Jenkins & Benjamin Valpey
* Date: 31 Mar 2020
* Filename: PGraph.hpp
* Filename: TGraph.hpp
* Description: A simple implementation of a Transient Graph
*/

Expand All @@ -18,19 +18,47 @@
#include <iterator>
#include <unordered_set>
#include "RCUTracker.hpp"
#include <ratio>
#include <cstdlib>

// #pragma GCC optimize ("O0")

/**
* SimpleGraph class. Labels are of templated type K.
*/
template <size_t numVertices = 1024>
template <size_t numVertices = 1024, size_t meanEdgesPerVertex=20, size_t vertexLoad = 50>
class TGraph : public RGraph{

public:

// We use smart pointers in the unordered_set, but we can only lookup by a key allocated
// on the stack if and only if it is also wrapped into a smart pointer. We create a custom
// 'deleter' function to control whether or not it will try to delete the wrapped pointer below
// https://stackoverflow.com/a/17853770/4111188

template<class T>
struct maybe_deleter {
bool _delete;
explicit maybe_deleter(bool doIt = true) : _delete(doIt){}

void operator()(T *p) {
if (_delete) delete p;
}
};

template <class T>
using set_shared_ptr = std::shared_ptr<T>;

template <class T>
set_shared_ptr<T> make_find_ptr(T *raw) {
return set_shared_ptr<T>(raw, maybe_deleter<T>(false));
}

class Relation;
class Vertex {
public:
std::unordered_set<std::shared_ptr<Relation>> adjacency_list;
std::unordered_set<std::shared_ptr<Relation>> dest_list;
std::unordered_set<set_shared_ptr<Relation>> adjacency_list;
std::unordered_set<set_shared_ptr<Relation>> dest_list;
int id;
int lbl;
Vertex(int id, int lbl): id(id), lbl(lbl){}
Expand All @@ -45,15 +73,6 @@ class TGraph : public RGraph{
int get_id() {
return id;
}

void lock() {
lck.lock();
}

void unlock() {
lck.unlock();
}

};

class Relation {
Expand All @@ -77,11 +96,59 @@ class TGraph : public RGraph{
idxToVertex = new Vertex*[numVertices];
vertexLocks = new std::atomic<bool>[numVertices];
vertexSeqs = new uint32_t[numVertices];
std::mt19937_64 gen(0xDEADBEEF);
std::uniform_int_distribution<> verticesRNG(0, numVertices - 1);
std::uniform_int_distribution<> coinflipRNG(0, 100);
std::cout << "Allocated core..." << std::endl;
// Fill to vertexLoad
for (int i = 0; i < numVertices; i++) {
if (coinflipRNG(gen) <= vertexLoad) {
idxToVertex[i] = new Vertex(i,i);
} else {
idxToVertex[i] = nullptr;
}
vertexLocks[i] = false;
vertexSeqs = 0;
}

std::cout << "Filled vertexLoad" << std::endl;

// Initialize...
for (size_t i = 0; i < numVertices; i++) {
idxToVertex[i] = new Vertex(i, -1);
// Fill to mean edges per vertex
for (int i = 0; i < numVertices; i++) {
for (int i = 0; i < meanEdgesPerVertex; i++) {
if (idxToVertex[i] == nullptr) continue;
int j = verticesRNG(gen);
while (j == i) {
j = verticesRNG(gen);
}
if (idxToVertex[j] != nullptr) {
auto r = make_shared<Relation>(i,j,-1);
source(i).insert(r);
destination(j).insert(r);
}
}
}
std::cout << "Filled mean edges per vertex" << std::endl;
}

// Obtain statistics of graph (|V|, |E|, average degree, vertex degrees)
// Not concurrent safe...
std::tuple<int, int, double, int *, int> grab_stats() {
int numV = 0;
int numE = 0;
int *degrees = new int[numVertices];
double averageEdgeDegree = 0;
for (auto i = 0; i < numVertices; i++) {
if (idxToVertex[i] != nullptr) {
numV++;
numE += source(i).size();
degrees[i] = source(i).size() + destination(i).size();
} else {
degrees[i] = 0;
}
}
averageEdgeDegree = numE / ((double) numV);
return std::make_tuple(numV, numE, averageEdgeDegree, degrees, numVertices);
}

Vertex** idxToVertex; // Transient set of transient vertices to index map
Expand All @@ -90,10 +157,10 @@ class TGraph : public RGraph{

// Thread-safe and does not leak edges
void clear() {
for (int i = 0; i < numVertices; i++) {
for (auto i = 0; i < numVertices; i++) {
lock(i);
}
for (int i = 0; i < numVertices; i++) {
for (auto i = 0; i < numVertices; i++) {
for (Relation *r : idxToVertex[i]->adjacency_list) {
delete r;
}
Expand Down Expand Up @@ -128,7 +195,7 @@ class TGraph : public RGraph{
}

{
Relation *rel = new Relation(src, dest, weight);
std::shared_ptr<Relation> rel = std::make_shared<Relation>(src, dest, weight);
srcSet.insert(rel);
destSet.insert(rel);
inc_seq(src);
Expand Down Expand Up @@ -170,7 +237,7 @@ class TGraph : public RGraph{
if (src == dest) return false;
if (src > dest) {
lock(dest);
lock(src)
lock(src);
} else {
lock(src);
lock(dest);
Expand Down Expand Up @@ -201,10 +268,10 @@ class TGraph : public RGraph{
std::vector<int> vertices;
lock(vid);
uint32_t seq = get_seq(vid);
for (Relation *r : source(vid)) {
for (auto r : source(vid)) {
vertices.push_back(r->dest);
}
for (Relation *r : destination(vid)) {
for (auto r : destination(vid)) {
vertices.push_back(r->src);
}

Expand Down Expand Up @@ -246,14 +313,8 @@ class TGraph : public RGraph{
}

// Step 4: Delete edges, clear set of src and dest edges, then delete the vertex itself
std::vector<Relation*> garbageList(source(vid).size() + destination(vid).size());
garbageList.insert(garbageList.begin(), source(vid).begin(), source(vid).end());
garbageList.insert(garbageList.begin(), destination(vid).begin(), destination(vid).end());
source(vid).clear();
destination(vid).clear();
for (Relation *r : garbageList) {
delete r;
}
destroy(vid);

// Step 5: Release in reverse order
Expand All @@ -267,23 +328,23 @@ class TGraph : public RGraph{
}

void for_each_outgoing(int vid, std::function<bool(int)> fn) {
lock(v);
for (Relation *r : source(v)) {
lock(vid);
for (auto r : source(vid)) {
if (!fn(r->dest)) {
break;
}
}
unlock(v);
unlock(vid);
}

void for_each_incoming(int vid, std::function<bool(int)> fn) {
lock(v);
for (Relation *r : destination(v)) {
lock(vid);
for (auto r : destination(vid)) {
if (!fn(r->src)) {
break;
}
}
unlock(v);
unlock(vid);
}

private:
Expand Down Expand Up @@ -315,33 +376,35 @@ class TGraph : public RGraph{
}

// Incoming edges
std::unordered_set<Relation*>& source(size_t idx) {
std::unordered_set<set_shared_ptr<Relation>>& source(int idx) {
return idxToVertex[idx]->adjacency_list;
}

// Outgoing edges
std::unordered_set<Relation*>& destination(size_t idx) {
std::unordered_set<set_shared_ptr<Relation>>& destination(int idx) {
return idxToVertex[idx]->dest_list;
}

bool has_relation(std::unordered_set<Relation*>& set, Relation *r) {
auto search = set.find(r);
bool has_relation(std::unordered_set<set_shared_ptr<Relation>>& set, Relation *r) {
auto search = set.find(make_find_ptr(r));
return search != set.end();
}

void remove_relation(std::unordered_set<Relation*>& set, Relation *r) {
auto search = set.find(r);
void remove_relation(std::unordered_set<set_shared_ptr<Relation>>& set, Relation *r) {
auto search = set.find(make_find_ptr(r));
if (search != set.end()) {
set.erase(search);
}
}
};

template <size_t numVertices = 1024>
template <size_t numVertices = 1024, size_t meanEdgesPerVertex=20, size_t vertexLoad = 50>
class TGraphFactory : public RideableFactory{
Rideable *build(GlobalTestConfig *gtc){
return new TGraph<numVertices>(gtc);
return new TGraph<numVertices, meanEdgesPerVertex, vertexLoad>(gtc);
}
};
// #pragma GCC reset_options

#endif

Loading

0 comments on commit 7a0efdf

Please sign in to comment.