Skip to content

Commit

Permalink
Separate Boost and Postgres code to avoid incompatibility linked to s…
Browse files Browse the repository at this point in the history
…nprintf
  • Loading branch information
PierreSenellart committed Nov 6, 2023
1 parent 69bb897 commit a4ca7bd
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 110 deletions.
101 changes: 0 additions & 101 deletions src/BooleanCircuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
extern "C" {
#include <unistd.h>
#include <math.h>
#ifndef TDKC
#include "provsql_shmem.h"
#endif
}

#include <cassert>
Expand Down Expand Up @@ -749,101 +746,3 @@ void BooleanCircuit::rewriteMultivaluedGates()
rewriteMultivaluedGatesRec(muls, cumulated_probs, 0, n-1, prefix);
}
}

#ifndef TDKC
bool operator<(const pg_uuid_t a, const pg_uuid_t b)
{
return memcmp(&a, &b, sizeof(pg_uuid_t))<0;
}

BooleanCircuit::BooleanCircuit(pg_uuid_t token)
{
std::set<pg_uuid_t> to_process, processed;
to_process.insert(token);

LWLockAcquire(provsql_shared_state->lock, LW_SHARED);
while(!to_process.empty()) {
pg_uuid_t uuid = *to_process.begin();
to_process.erase(to_process.begin());
processed.insert(uuid);
std::string f{uuid2string(uuid)};

bool found;
provsqlHashEntry *entry = reinterpret_cast<provsqlHashEntry *>(hash_search(provsql_hash, &uuid, HASH_FIND, &found));

if(!found)
setGate(f, BooleanGate::MULVAR);
else {
gate_t id;

switch(entry->type) {
case gate_input:
if(isnan(entry->prob)) {
LWLockRelease(provsql_shared_state->lock);
elog(ERROR, "Missing probability for input token");
}
id = setGate(f, BooleanGate::IN, entry->prob);
break;

case gate_mulinput:
if(isnan(entry->prob)) {
LWLockRelease(provsql_shared_state->lock);
elog(ERROR, "Missing probability for input token");
}
id = setGate(f, BooleanGate::MULIN, entry->prob);
addWire(
id,
getGate(uuid2string(provsql_shared_state->wires[entry->children_idx])));
setInfo(id, entry->info1);
break;

case gate_times:
case gate_project:
case gate_eq:
case gate_monus:
case gate_one:
id = setGate(f, BooleanGate::AND);
break;

case gate_plus:
case gate_zero:
id = setGate(f, BooleanGate::OR);
break;

default:
elog(ERROR, "Wrong type of gate in circuit");
}

if(entry->nb_children > 0) {
if(entry->type == gate_monus) {
auto id_not = setGate(BooleanGate::NOT);
auto child1 = provsql_shared_state->wires[entry->children_idx];
auto child2 = provsql_shared_state->wires[entry->children_idx+1];
addWire(
id,
getGate(uuid2string(child1)));
addWire(id, id_not);
addWire(
id_not,
getGate(uuid2string(child2)));
if(processed.find(child1)==processed.end())
to_process.insert(child1);
if(processed.find(child2)==processed.end())
to_process.insert(child2);
} else {
for(unsigned i=0; i<entry->nb_children; ++i) {
auto child = provsql_shared_state->wires[entry->children_idx+i];

addWire(
id,
getGate(uuid2string(child)));
if(processed.find(child)==processed.end())
to_process.insert(child);
}
}
}
}
}
LWLockRelease(provsql_shared_state->lock);
}
#endif
7 changes: 0 additions & 7 deletions src/BooleanCircuit.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

#include "Circuit.hpp"

#ifndef TDKC
#include "provsql_utils_cpp.h"
#endif

enum class BooleanGate { UNDETERMINED, AND, OR, NOT, IN, MULIN, MULVAR };

class BooleanCircuit : public Circuit<BooleanGate> {
Expand All @@ -36,9 +32,6 @@ std::map<gate_t, unsigned> info;
public:
BooleanCircuit() {
}
#ifndef TDKC
explicit BooleanCircuit(pg_uuid_t token);
#endif
gate_t addGate() override;
gate_t setGate(BooleanGate t) override;
gate_t setGate(const uuid &u, BooleanGate t) override;
Expand Down
108 changes: 108 additions & 0 deletions src/CircuitFromShMem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <cmath>

#include "BooleanCircuit.h"
#include "provsql_utils_cpp.h"

extern "C" {
#include "provsql_shmem.h"
}

bool operator<(const pg_uuid_t a, const pg_uuid_t b)
{
return memcmp(&a, &b, sizeof(pg_uuid_t))<0;
}

BooleanCircuit createBooleanCircuit(pg_uuid_t token)
{
std::set<pg_uuid_t> to_process, processed;
to_process.insert(token);

BooleanCircuit result;

LWLockAcquire(provsql_shared_state->lock, LW_SHARED);
while(!to_process.empty()) {
pg_uuid_t uuid = *to_process.begin();
to_process.erase(to_process.begin());
processed.insert(uuid);
std::string f{uuid2string(uuid)};

bool found;
provsqlHashEntry *entry = reinterpret_cast<provsqlHashEntry *>(hash_search(provsql_hash, &uuid, HASH_FIND, &found));

if(!found)
result.setGate(f, BooleanGate::MULVAR);
else {
gate_t id;

switch(entry->type) {
case gate_input:
if(std::isnan(entry->prob)) {
LWLockRelease(provsql_shared_state->lock);
elog(ERROR, "Missing probability for input token");
}
id = result.setGate(f, BooleanGate::IN, entry->prob);
break;

case gate_mulinput:
if(std::isnan(entry->prob)) {
LWLockRelease(provsql_shared_state->lock);
elog(ERROR, "Missing probability for input token");
}
id = result.setGate(f, BooleanGate::MULIN, entry->prob);
result.addWire(
id,
result.getGate(uuid2string(provsql_shared_state->wires[entry->children_idx])));
result.setInfo(id, entry->info1);
break;

case gate_times:
case gate_project:
case gate_eq:
case gate_monus:
case gate_one:
id = result.setGate(f, BooleanGate::AND);
break;

case gate_plus:
case gate_zero:
id = result.setGate(f, BooleanGate::OR);
break;

default:
elog(ERROR, "Wrong type of gate in circuit");
}

if(entry->nb_children > 0) {
if(entry->type == gate_monus) {
auto id_not = result.setGate(BooleanGate::NOT);
auto child1 = provsql_shared_state->wires[entry->children_idx];
auto child2 = provsql_shared_state->wires[entry->children_idx+1];
result.addWire(
id,
result.getGate(uuid2string(child1)));
result.addWire(id, id_not);
result.addWire(
id_not,
result.getGate(uuid2string(child2)));
if(processed.find(child1)==processed.end())
to_process.insert(child1);
if(processed.find(child2)==processed.end())
to_process.insert(child2);
} else {
for(unsigned i=0; i<entry->nb_children; ++i) {
auto child = provsql_shared_state->wires[entry->children_idx+i];

result.addWire(
id,
result.getGate(uuid2string(child)));
if(processed.find(child)==processed.end())
to_process.insert(child);
}
}
}
}
}
LWLockRelease(provsql_shared_state->lock);

return result;
}
12 changes: 12 additions & 0 deletions src/CircuitFromShMem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CIRCUIT_FROM_SH_MEM
#define CIRCUIT_FROM_SH_MEM

extern "C" {
#include <postgres.h>
}

#include "BooleanCircuit.h"

BooleanCircuit createBooleanCircuit(pg_uuid_t token);

#endif /* CIRCUIT_FROM_SH_MEM */
3 changes: 2 additions & 1 deletion src/probability_evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ PG_FUNCTION_INFO_V1(probability_evaluate);
#include "BooleanCircuit.h"
#include "provsql_utils_cpp.h"
#include "dDNNFTreeDecompositionBuilder.h"
#include "CircuitFromShMem.h"

using namespace std;

Expand All @@ -28,7 +29,7 @@ static void provsql_sigint_handler (int)
static Datum probability_evaluate_internal
(pg_uuid_t token, const string &method, const string &args)
{
BooleanCircuit c(token);
BooleanCircuit c = createBooleanCircuit(token);

double result;
auto gate = c.getGate(uuid2string(token));
Expand Down
3 changes: 2 additions & 1 deletion src/shapley.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ PG_FUNCTION_INFO_V1(shapley);
#include "BooleanCircuit.h"
#include "provsql_utils_cpp.h"
#include "dDNNFTreeDecompositionBuilder.h"
#include "CircuitFromShMem.h"
#include <fstream>

using namespace std;

static double shapley_internal
(pg_uuid_t token, pg_uuid_t variable)
{
BooleanCircuit c(token);
BooleanCircuit c = createBooleanCircuit(token);

if(c.getGateType(c.getGate(uuid2string(variable))) != BooleanGate::IN)
return 0.;
Expand Down

0 comments on commit a4ca7bd

Please sign in to comment.