Skip to content

Commit

Permalink
implemented solving aba
Browse files Browse the repository at this point in the history
  • Loading branch information
DamirJann committed Jun 8, 2022
1 parent 50b5a45 commit 0a4a3ec
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ project(cds)

include_directories(./)
link_directories(bin)
add_executable(libcds-debug main.cpp)
add_executable(libcds-debug main.cpp visualize.h)

target_link_libraries(libcds-debug cds pthread)

Expand Down
16 changes: 11 additions & 5 deletions cds/container/hamt.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace cds {
typedef GC gc;
typedef K key_type;
typedef V value_type;
protected:
public:
struct InsertResult {
enum class Status {
Inserted,
Expand Down Expand Up @@ -146,7 +146,7 @@ namespace cds {
NodeType type;
protected:

explicit Node(NodeType type) {
Node(NodeType type) {
this->type = type;
}
};
Expand Down Expand Up @@ -437,6 +437,11 @@ namespace cds {
}
}

public:
Node* getRoot(){
return root;
}

private:
INode *root;

Expand Down Expand Up @@ -534,7 +539,7 @@ namespace cds {
auto *updated = new CNode(*m);
uint8_t path = extractHashPartByLevel(hash, level);
Node *subNode = updated->getSubNode(path);

guard->assign(0, subNode);
RemoveResult res{};

if (subNode == nullptr) {
Expand Down Expand Up @@ -582,8 +587,7 @@ namespace cds {
}

InsertResult insert(INode *currentNode, INode *parent, SNode *newNode, uint8_t level,
typename gc::template GuardArray<2> *guard
) {
typename gc::template GuardArray<2> *guard) {
CNode *pm = parent ? parent->main.load() : nullptr;
CNode *m = currentNode->main.load();

Expand All @@ -601,6 +605,8 @@ namespace cds {
InsertResult res{};

Node *subNode = updated->getSubNode(path);
guard->assign(0, subNode);

if (subNode == nullptr) {
transformToWithInsertedChild(updated, newNode, path);
updated->isTomb = isTombed(updated, root, currentNode);
Expand Down
27 changes: 17 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
#include <iostream> //cds::gc::HP (Hazard Pointer)
#include <cds/container/optimistic_queue.h> // cds::gc::HP (Hazard Pointer)
#include <cds/container/hamt.h> // cds::gc::HP (Hazard Pointer)
#include <fstream>
#include "visualize.h"

using namespace std;

int main() {
cds::Initialize();

{
cds::gc::HP hpGC;

cds::threading::Manager::attachThread();
cds::container::Hamt<cds::gc::HP, int, int> hamt;


for (int j = 0; j < 10000000; j++) {
cds::container::Hamt<cds::gc::HP, int, int> hamt;
int count = 10000;
for (int j = 0; j < 1; j++) {
int count = 1000000;

for (int i = 0; i < count; i++) hamt.insert(i,i);
cout << "start\n";
int thread_count = 10;
vector<pthread_t> thread(thread_count);
Expand All @@ -30,8 +36,8 @@ int main() {
auto *hamt = (cds::container::Hamt<cds::gc::HP, int, int> *) (*static_cast<vector<void *> *>(args))[0];
int *id = (int *) (*static_cast<vector<void *> *>(args))[1];
int *averageIterationCount = (int *) (*static_cast<vector<void *> *>(args))[2];
for (int i = *id * (*averageIterationCount); i < (*id + 1) * (*averageIterationCount); i++) {
hamt->insert(i,i);
for (int i = *id * (*averageIterationCount); i < (*id + 1) * (*averageIterationCount) - 2; i++) {
hamt->remove(i);
}
cds::threading::Manager::detachThread();
pthread_exit(nullptr);
Expand All @@ -47,14 +53,15 @@ int main() {
delete (int *) attr[i][1];
delete (int *) attr[i][2];
}
cout << "finished\n";
// for (int i = 0; i < count; i++) {
// assert(hamt.lookup(i).value == 0);
// }

}
ofstream f = ofstream("graph.txt");
visualize(f, &hamt);
system("dot -Tpng ./graph.txt -o ../graph.png");

}


// cds::Terminate();
cds::Terminate();

}
129 changes: 129 additions & 0 deletions visualize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#define protected public
#define private public

#include "cds/container/hamt.h"
#include <cds/init.h> //cds::Initialize и cds::Terminate
#include <cds/gc/hp.h> //cds::gc::HP (Hazard Pointer)
#include <iostream> //cds::gc::HP (Hazard Pointer)
#include <cds/container/optimistic_queue.h> // cds::gc::HP (Hazard Pointer)
#include <cds/container/hamt.h> // cds::gc::HP (Hazard Pointer)
#include <fstream>

#undef protected
#undef private

#include <fstream>
#include <boost/graph/graphviz.hpp>

using namespace std;

int counter = 0;
const int MAX_EDGE_COUNT = 10000;
const int MAX_HEIGHT_COUNT = 10000;

string toString(int k) {
return to_string(k);
}

string toString(string k) {
return k;
}

template<class K, class V>
struct Height {
Height(typename cds::container::Hamt<cds::gc::HP, K, V>::Node *v) {
this->v = v;
id = counter++;
}

int id;
typename cds::container::Hamt<cds::gc::HP, K, V>::Node *v{};

string getLabel() {
switch (v->type) {
case cds::container::Hamt<cds::gc::HP, K, V>::INODE: {
return "I" + to_string(id);
}
case cds::container::Hamt<cds::gc::HP, K, V>::CNODE: {
string label = "C" + to_string(id);
if (static_cast<typename cds::container::Hamt<cds::gc::HP, K, V>::CNode *>(v)->isTomb) {
label += " - tombed";
}
return label;
}
case cds::container::Hamt<cds::gc::HP, K, V>::SNODE: {
string label = "";
label += "hs: " + to_string(static_cast<typename cds::container::Hamt<cds::gc::HP, K, V>::SNode *>(this->v)->getHash()) + "\n\n";
for (auto &p: static_cast<typename cds::container::Hamt<cds::gc::HP, K, V>::SNode *>(v)->pair) {
label += "(" + toString(p.key) + ", " + toString(p.value) + ") \n";
}
return label;
}
default:
return "Unknown";
}
}
};


struct Edge {
string label;
int from;
int to;
};

template<class K, class V>
void walk_and_collect(Height<K,V> currHeight, vector<Height<K,V>> &hs, vector<Edge> &es) {
if (currHeight.v->type == cds::container::Hamt<cds::gc::HP, K, V>::INODE) {
Height<K,V> c(static_cast<typename cds::container::Hamt<cds::gc::HP, K, V>::INode*>(currHeight.v)->main);
hs.push_back(c);

es.push_back({"", currHeight.id, c.id});

for (uint8_t path = 0b00000; path < 0b100000; path++) {
auto *n = reinterpret_cast<typename cds::container::Hamt<cds::gc::HP, K, V>::CNode *>(c.v);
if (n->getSubNode(path) != nullptr) {
Height<K,V> child(n->getSubNode(path));
hs.push_back(child);
es.push_back({to_string(path), c.id, child.id});
walk_and_collect(child, hs, es);
}
}
}
}


template<class K, class V>
void visualize(ofstream &f, cds::container::Hamt<cds::gc::HP, K, V> *trie) {
vector<Height<K,V>> heights = {trie->getRoot()};
vector<Edge> edges;
walk_and_collect(heights[0], heights, edges);

typedef pair<int, int> Edge;
Edge used_by[MAX_EDGE_COUNT];

const int nedges = edges.size();
int weights[MAX_EDGE_COUNT];
std::fill(weights, weights + nedges, 34);

using namespace boost;

string names[MAX_HEIGHT_COUNT];
for (size_t i = 0; i < heights.size(); i++) {
names[i] = heights[i].getLabel();
}

typedef adjacency_list<vecS, vecS, directedS,
property<vertex_color_t, default_color_type>,
property<edge_weight_t, int>
> Graph;
Graph g(used_by, used_by, weights, heights.size());

std::map<Graph::edge_descriptor, std::string> ename;
for (auto &edge: edges) {
ename[add_edge(edge.from, edge.to, g).first] = edge.label;
}

write_graphviz(f, g, make_label_writer(names), make_label_writer(boost::make_assoc_property_map(ename)));
}

0 comments on commit 0a4a3ec

Please sign in to comment.