Skip to content

Commit

Permalink
Basic circuit manipulation functions on mmapped circuits
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreSenellart committed Feb 24, 2024
1 parent 762d8ca commit 498ca32
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 12 deletions.
71 changes: 71 additions & 0 deletions src/MMappedCircuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,74 @@ void destroy_provsql_mmap()
{
delete c;
}

void MMappedCircuit::createGate(
pg_uuid_t token, gate_type type, const std::vector<pg_uuid_t> &children)
{
auto [idx, created] = mapping.add(token);
if(!created) // Was already existing, no need to do anything
return;

gates.add({type, static_cast<unsigned>(children.size()), wires.nbElements()});
for(const auto &c: children)
{
wires.add(c);
}
}

gate_type MMappedCircuit::getGateType(pg_uuid_t token) const
{
auto idx = mapping[token];
if(idx == MMappedUUIDHashTable::NOTHING)
return gate_invalid;
else
return gates[idx].type;
}

std::vector<pg_uuid_t> MMappedCircuit::getChildren(pg_uuid_t token) const
{
std::vector<pg_uuid_t> result;
auto idx = mapping[token];
if(idx != MMappedUUIDHashTable::NOTHING) {
const GateInformation &gi = gates[idx];
for(unsigned k=gi.children_idx; k<gi.children_idx+gi.nb_children; ++k)
result.push_back(wires[k]);
}
return result;
}

void MMappedCircuit::setProb(pg_uuid_t token, double prob)
{
auto idx = mapping[token];
if(idx != MMappedUUIDHashTable::NOTHING)
gates[idx].prob=prob;
}

double MMappedCircuit::getProb(pg_uuid_t token) const
{
auto idx = mapping[token];
if(idx == MMappedUUIDHashTable::NOTHING)
return -1;
else
return gates[idx].prob;
}

void MMappedCircuit::setInfos(pg_uuid_t token, unsigned info1, unsigned info2)
{
auto idx = mapping[token];
if(idx != MMappedUUIDHashTable::NOTHING) {
gates[idx].info1=info1;
gates[idx].info2=info2;
}
}

std::pair<unsigned, unsigned> MMappedCircuit::getInfos(pg_uuid_t token) const
{
auto idx = mapping[token];
if(idx == MMappedUUIDHashTable::NOTHING) {
return std::make_pair(0, 0);
} else {
const GateInformation &gi = gates[idx];
return std::make_pair(gi.info1, gi.info2);
}
}
15 changes: 15 additions & 0 deletions src/MMappedCircuit.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ typedef struct GateInformation
double prob;
unsigned info1;
unsigned info2;

GateInformation(gate_type t, unsigned n, unsigned i) :
type(t), nb_children(n), children_idx(i), prob(-1.), info1(0), info2(0) {
}
} GateInformation;

class MMappedCircuit {
Expand All @@ -32,6 +36,17 @@ static constexpr const char *MAPPING_FILENAME="provsql_mapping.mmap";
explicit MMappedCircuit() :
mapping(MAPPING_FILENAME), gates(GATES_FILENAME), wires(WIRES_FILENAME) {
}

void createGate(pg_uuid_t token, gate_type type, const std::vector<pg_uuid_t> &children);
gate_type getGateType(pg_uuid_t token) const;
std::vector<pg_uuid_t> getChildren(pg_uuid_t token) const;
void setProb(pg_uuid_t token, double prob);
double getProb(pg_uuid_t token) const;
void setInfos(pg_uuid_t token, unsigned info1, unsigned info2);
std::pair<unsigned, unsigned> getInfos(pg_uuid_t token) const;
inline unsigned getNbGates() const {
return gates.nbElements();
}
};

#endif /* MMAPPED_CIRCUIT_H */
8 changes: 4 additions & 4 deletions src/MMappedUUIDHashTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ unsigned MMappedUUIDHashTable::find(pg_uuid_t u) const
return k;
}

unsigned MMappedUUIDHashTable::get(pg_uuid_t u) const
unsigned MMappedUUIDHashTable::operator[](pg_uuid_t u) const
{
unsigned k = find(u);

return table->t[k].value;
}

unsigned MMappedUUIDHashTable::get(pg_uuid_t u)
std::pair<unsigned,bool> MMappedUUIDHashTable::add(pg_uuid_t u)
{
unsigned k = find(u);

Expand All @@ -115,9 +115,9 @@ unsigned MMappedUUIDHashTable::get(pg_uuid_t u)
k = find(u);
++table->nb_elements;
table->t[k].uuid = u;
return table->t[k].value = table->next_value++;
return std::make_pair(table->t[k].value = table->next_value++, true);
} else
return table->t[k].value;
return std::make_pair(table->t[k].value, false);
}

// Only used when growing the table, so no need to check/update nb_elements
Expand Down
7 changes: 4 additions & 3 deletions src/MMappedUUIDHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MMAPPED_UUID_HASH_TABLE_H

#include <cstddef>
#include <utility>
#include "provsql_utils.h"

/*
Expand Down Expand Up @@ -46,7 +47,6 @@ table_t *table;

static constexpr unsigned STARTING_LOG_SIZE=16;
static constexpr double MAXIMUM_LOAD_FACTOR=.5;
static constexpr unsigned NOTHING=static_cast<unsigned>(-1);

inline unsigned hash(pg_uuid_t u) const {
return *reinterpret_cast<unsigned*>(&u) % (1 << table->log_size);
Expand All @@ -58,11 +58,12 @@ void grow();
void set(pg_uuid_t u, unsigned i);

public:
static constexpr unsigned NOTHING=static_cast<unsigned>(-1);
explicit MMappedUUIDHashTable(const char *filename);
~MMappedUUIDHashTable();

unsigned get(pg_uuid_t u) const;
unsigned get(pg_uuid_t u);
unsigned operator[](pg_uuid_t u) const;
std::pair<unsigned,bool> add(pg_uuid_t u);
inline unsigned nbElements() const {
return table->nb_elements;
}
Expand Down
5 changes: 3 additions & 2 deletions src/MMappedVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ void set(pg_uuid_t u, unsigned i);
explicit MMappedVector(const char *filename);
~MMappedVector();

T get(unsigned k) const;
void add(T value);
const T &operator[](unsigned k) const;
T &operator[](unsigned k);
void add(const T& value);
inline unsigned nbElements() const {
return data->nb_elements;
}
Expand Down
10 changes: 8 additions & 2 deletions src/MMappedVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,19 @@ MMappedVector<T>::~MMappedVector()
}

template <typename T>
T MMappedVector<T>::get(unsigned k) const
inline const T &MMappedVector<T>::operator[](unsigned k) const
{
return data->d[k];
}

template <typename T>
void MMappedVector<T>::add(T value)
inline T &MMappedVector<T>::operator[](unsigned k)
{
return data->d[k];
}

template <typename T>
void MMappedVector<T>::add(const T &value)
{
if(data->nb_elements == data->capacity)
grow();
Expand Down
2 changes: 1 addition & 1 deletion src/provsql_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct pg_uuid_t
#include "nodes/pg_list.h"

typedef enum gate_type {
gate_input, gate_plus, gate_times, gate_monus, gate_project, gate_zero, gate_one, gate_eq, gate_agg, gate_semimod, gate_cmp, gate_delta, gate_value, gate_mulinput, nb_gate_types
gate_input, gate_plus, gate_times, gate_monus, gate_project, gate_zero, gate_one, gate_eq, gate_agg, gate_semimod, gate_cmp, gate_delta, gate_value, gate_mulinput, gate_invalid, nb_gate_types
} gate_type;

typedef struct constants_t {
Expand Down

0 comments on commit 498ca32

Please sign in to comment.