Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
javadsaberlatibari authored Feb 25, 2024
1 parent f0c60bb commit 3b685ed
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 0 deletions.
126 changes: 126 additions & 0 deletions wellcoordination/benchmark/twopset-benchmarkcpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>

#include "twopset.hpp"

int main(int argc, char* argv[]) {
std::string loc =
"/users/jsaber/binHamband/workload/";

int nr_procs = static_cast<int>(std::atoi(argv[1]));
int num_ops = static_cast<int>(std::atoi(argv[2]));
double write_percentage = static_cast<double>(std::atoi(argv[3]));

loc += std::to_string(nr_procs) + "-" + std::to_string(num_ops) + "-" +
std::to_string(static_cast<int>(write_percentage));
loc += "/twopset/";

std::ofstream* outfile = new std::ofstream[nr_procs];
std::vector<std::string>* calls = new std::vector<std::string>[nr_procs];
for (int i = 0; i < nr_procs; i++) {
remove((loc + std::to_string(i + 1) + ".txt").c_str());
outfile[i].open(loc + std::to_string(i + 1) + ".txt", std::ios_base::app);
calls[i] = std::vector<std::string>();
}
TWOPSet* test = new TWOPSet();

write_percentage /= 100;
int num_replicas = nr_procs;
double total_writes = num_ops * write_percentage;
int queries = num_ops - total_writes;

int num_nonconflicting_write_methods = 2;
int num_read_methods = 1;

std::cout << "ops: " << num_ops << std::endl;
std::cout << "write_perc: " << write_percentage << std::endl;
std::cout << "writes: " << total_writes << std::endl;
std::cout << "reads: " << queries << std::endl;

int expected_calls_per_update_method = total_writes /num_nonconflicting_write_methods;
std::cout << "expected #calls per update method "
<< expected_calls_per_update_method << std::endl;

int expected_calls_per_update_method_per_node = expected_calls_per_update_method / nr_procs;

std::cout << "expected #calls per nonconflicting writes in nodes "
<< expected_calls_per_update_method_per_node << std::endl;

int write_calls =
expected_calls_per_update_method * num_nonconflicting_write_methods;

int write_calls_issued = 0;
int reads_issues = 0;

// first allocating writes operations to the nodes
for (int i = 1; i <= num_replicas; i++) {
// non-conflicting write method
for (int count = 0;
count < expected_calls_per_update_method_per_node; count++) {
std::string element = std::to_string(std::rand() % 1000000);
std::string callStr;
callStr = "0 " + element;
calls[i - 1].push_back(callStr);
for(int x = 0; x < static_cast<int>(1/write_percentage)+15; x++)
{
if(calls[i - 1].size() == num_ops/nr_procs)
break;
calls[i - 1].push_back(std::string("2"));
reads_issues++;
}
callStr = "1 " + element;
calls[i - 1].push_back(callStr);
write_calls_issued += 2;
}

}


std::cout << "write calls issued: " << write_calls_issued << std::endl;
std::cout << "read calls issued: " << reads_issues << std::endl;

// allocate reads to the nodes
int q = num_ops - write_calls_issued;
int read_calls = q - reads_issues;
std::cout << "extra read calls needed: " << read_calls << std::endl;

int index = 0;

std::cout << "after adding writes to nodes" << std::endl;
for (int i = 0; i < nr_procs; i++)
std::cout << i + 1 << " size: " << calls[i].size() << std::endl;


if (read_calls != 0) {
for (int i = 0; i < nr_procs; i++){
for (int j = 0; j < read_calls / nr_procs; j++)
{
calls[i].push_back(std::string("2"));
}
}
}


std::cout << "after adding reads to all" << std::endl;
for (int i = 0; i < nr_procs; i++)
std::cout << i + 1 << " size: " << calls[i].size() << std::endl;


for (int i = 0; i < nr_procs; i++) {
calls[i].insert(calls[i].begin(),
std::string("#" + std::to_string(write_calls_issued)));
// std::random_shuffle(calls[i].begin() + 1, calls[i].end());
}

for (int i = 0; i < nr_procs; i++) {
for (int x = 0; x < calls[i].size(); x++)
outfile[i] << calls[i][x] << std::endl;
outfile[i].close();
}

std::cout << "expected calls to receive: " << write_calls_issued << std::endl;
return 0;
}
156 changes: 156 additions & 0 deletions wellcoordination/benchmark/twopset-crdt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdarg>
#include <cstring>
#include <unordered_set>
#include <mutex>

#include "../src/replicated_object_crdt.hpp"


typedef unsigned char uint8_t;

class TWOPSet : public ReplicatedObject
{
private:

public:

enum MethodType{
ADD = 0,
REMOVE = 1,
QUERY = 2
};
std::recursive_mutex m;
std::set<std::string> setsourceadd;
std::set<std::string> setremoteadd;
std::set<std::string> setsourceremove;
std::set<std::string> setremoteremove;



TWOPSet() {
read_methods.push_back(static_cast<int>(MethodType::QUERY));

update_methods.push_back(static_cast<int>(MethodType::ADD));
update_methods.push_back(static_cast<int>(MethodType::REMOVE));

method_args.insert(std::make_pair(static_cast<int>(MethodType::ADD), 1));
method_args.insert(std::make_pair(static_cast<int>(MethodType::REMOVE), 1));
method_args.insert(std::make_pair(static_cast<int>(MethodType::QUERY), 0));
}

TWOPSet(TWOPSet &obj) : ReplicatedObject(obj)
{
//state
setsource = obj.setsource;
setsource = obj.setremote;
}

virtual void toString()
{
std::cout << "#elementssource: " << (setsourceadd.size()-setsourceremove.size()) << std::endl;
std::cout << "#elementsremote: " << (setremoteadd.size()-setremotearemove.size()) << std::endl;
}


// 0
std::string add(std::string a)
{
return "";
}
std::string remove(std::string a)
{
return "";
}
std::string addDownstream(std::string a, bool b)
{
//std::scoped_lock lock(m);
if (b==false)
setsourceadd.insert(a);
else
setremoteadd.insert(a);
return "";
}
std::string removeDownstream(std::string a, bool b)
{
//std::scoped_lock lock(m);
if (b==false)
setsourceremove.insert(a);
else
setremoteremove.insert(a);
return "";
}
// 1
TWOPSet query() { return *this; }


virtual std::string execute(MethodCall call)
{
switch (static_cast<MethodType>(call.method_type))
{
case MethodType::ADD:
return add(call.arg);
break;
case MethodType::REMOVE:
return remove(call.arg);
break;
case MethodType::QUERY:
return "";
break;
default:
std::cout << "wrong method name" << std::endl;
break;
}
return "";
}


virtual ReplicatedObject* executeDownstream(MethodCall call, bool b)
{
switch (static_cast<MethodType>(call.method_type))
{
case MethodType::ADD:
{
size_t index = call.arg.find_first_of('-');
if(index != std::string::npos){
std::string arg = call.arg.substr(0, index);
addDownstream((arg), b);
}
else
addDownstream((call.arg), b);

break;
}
case MethodType::REMOVE:
{
size_t index = call.arg.find_first_of('-');
if(index != std::string::npos){
std::string arg = call.arg.substr(0, index);
removeDownstream((arg), b);
}
else
removeDownstream((call.arg), b);

break;
}
case MethodType::QUERY:
return this;
break;
default:
std::cout << "wrong method name" << std::endl;
break;
}
return this;
}



virtual bool isPermissible(MethodCall call)
{
return true;
}
};
102 changes: 102 additions & 0 deletions wellcoordination/benchmark/twopset.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdarg>
#include <cstring>
#include <unordered_set>
#include <mutex>

#include "../src/replicated_object.hpp"


typedef unsigned char uint8_t;

class TWOPSet : public ReplicatedObject
{
private:

public:

enum MethodType{
ADD = 0,
REMOVE = 1,
QUERY = 2
};
std::atomic<bool> addlock, removelock;
std::set<std::string> addset;
std::set<std::string> removeset;



TWOPSet() {
read_methods.push_back(static_cast<int>(MethodType::QUERY));

update_methods.push_back(static_cast<int>(MethodType::ADD));
update_methods.push_back(static_cast<int>(MethodType::REMOVE));

method_args.insert(std::make_pair(static_cast<int>(MethodType::ADD), 1));
method_args.insert(std::make_pair(static_cast<int>(MethodType::REMOVE), 1));
method_args.insert(std::make_pair(static_cast<int>(MethodType::QUERY), 0));
}

TWOPSet(TWOPSet &obj) : ReplicatedObject(obj)
{
//state
addset = obj.addset;
removeset = obj.removeset;
}

virtual void toString()
{
std::cout << "#elements: " << (addset.size()-removeset.size()) << std::endl;
}


// 0
void add(std::string a)
{
while(addlock.load());
addlock.store(true);
addset.insert(a);
addlock.store(false);
}
void remove(std::string a)
{
while(removelock.load());
removelock.store(true);
removeset.insert(a);
removelock.store(false);
}
// 1
TWOPSet query() { return *this; }


virtual ReplicatedObject* execute(MethodCall call)
{
switch (static_cast<MethodType>(call.method_type))
{
case MethodType::ADD:
add(call.arg);
break;
case MethodType::REMOVE:
remove(call.arg);
break;
case MethodType::QUERY:
return this;
break;
default:
std::cout << "wrong method name" << std::endl;
break;
}
return this;
}



virtual bool isPermissible(MethodCall call)
{
return true;
}
};

0 comments on commit 3b685ed

Please sign in to comment.