From 4bdc11392eb0cea7a13f3d3eccc6f56c8c910b58 Mon Sep 17 00:00:00 2001 From: Hamed Date: Thu, 7 Sep 2017 21:40:26 -0700 Subject: [PATCH 1/1] scratch of (de)serializing --- src/AST/astops.h | 2 +- src/Makefile | 9 ++--- src/Serialize/deserializer.cc | 58 ++++++++++++++++++++++++++++++++ src/Serialize/deserializer.h | 34 +++++++++++++++++++ src/Serialize/serializable.h | 18 ++++++++++ src/Serialize/serializer.cc | 42 +++++++++++++++++++++++ src/Serialize/serializer.h | 37 ++++++++++++++++++++ src/Test/buildconstraintstest.cc | 2 +- src/classes.h | 2 ++ src/csolver.cc | 20 +++++++++++ src/csolver.h | 1 + 11 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 src/Serialize/deserializer.cc create mode 100644 src/Serialize/deserializer.h create mode 100644 src/Serialize/serializable.h create mode 100644 src/Serialize/serializer.cc create mode 100644 src/Serialize/serializer.h diff --git a/src/AST/astops.h b/src/AST/astops.h index cd2beaa..b92c0e4 100644 --- a/src/AST/astops.h +++ b/src/AST/astops.h @@ -7,7 +7,7 @@ typedef enum FunctionType FunctionType; enum PredicateType {TABLEPRED, OPERATORPRED}; typedef enum PredicateType PredicateType; -enum ASTNodeType {ORDERCONST, BOOLEANVAR, LOGICOP, PREDICATEOP, BOOLCONST, ELEMSET, ELEMFUNCRETURN, ELEMCONST}; +enum ASTNodeType {ORDERCONST, BOOLEANVAR, LOGICOP, PREDICATEOP, BOOLCONST, ELEMSET, ELEMFUNCRETURN, ELEMCONST, BOOLEANEDGE}; typedef enum ASTNodeType ASTNodeType; enum Polarity {P_UNDEFINED=0, P_TRUE=1, P_FALSE=2, P_BOTHTRUEFALSE=3}; diff --git a/src/Makefile b/src/Makefile index e6699e6..ee81df1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,16 +4,16 @@ PHONY += directories MKDIR_P = mkdir -p OBJ_DIR = bin -CPP_SOURCES := $(wildcard *.cc) $(wildcard AST/*.cc) $(wildcard ASTTransform/*.cc) $(wildcard Translator/*.cc) $(wildcard ASTAnalyses/*.cc) $(wildcard Tuner/*.cc) $(wildcard Collections/*.cc) $(wildcard Backend/*.cc) $(wildcard Encoders/*.cc) +CPP_SOURCES := $(wildcard *.cc) $(wildcard AST/*.cc) $(wildcard ASTTransform/*.cc) $(wildcard Translator/*.cc) $(wildcard ASTAnalyses/*.cc) $(wildcard Tuner/*.cc) $(wildcard Collections/*.cc) $(wildcard Backend/*.cc) $(wildcard Encoders/*.cc) $(wildcard Serialize/*.cc) -C_SOURCES := $(wildcard *.c) $(wildcard AST/*.c) $(wildcard ASTTransform/*.c) $(wildcard Translator/*.c) $(wildcard ASTAnalyses/*.c) $(wildcard Tuner/*.c) $(wildcard Collections/*.c) $(wildcard Backend/*.c) $(wildcard Encoders/*.c) +C_SOURCES := $(wildcard *.c) $(wildcard AST/*.c) $(wildcard ASTTransform/*.c) $(wildcard Translator/*.c) $(wildcard ASTAnalyses/*.c) $(wildcard Tuner/*.c) $(wildcard Collections/*.c) $(wildcard Backend/*.c) $(wildcard Encoders/*.c) $(wildcard Serialize/*.c) -HEADERS := $(wildcard *.h) $(wildcard AST/*.h) $(wildcard ASTTransform/*.h) $(wildcard Translator/*.h) $(wildcard ASTAnalyses/*.h) $(wildcard Tuner/*.h) $(wildcard Collections/*.h) $(wildcard Backend/*.h) $(wildcard Encoders/*.h) +HEADERS := $(wildcard *.h) $(wildcard AST/*.h) $(wildcard ASTTransform/*.h) $(wildcard Translator/*.h) $(wildcard ASTAnalyses/*.h) $(wildcard Tuner/*.h) $(wildcard Collections/*.h) $(wildcard Backend/*.h) $(wildcard Encoders/*.h) $(wildcard Serialize/*.h) OBJECTS := $(CPP_SOURCES:%.cc=$(OBJ_DIR)/%.o) $(C_SOURCES:%.c=$(OBJ_DIR)/%.o) CFLAGS := -Wall -g -O0 -CFLAGS += -IAST -IASTTransform -IASTAnalyses -ITranslator -ICollections -IBackend -I. -IEncoders -ITuner +CFLAGS += -IAST -IASTTransform -IASTAnalyses -ITranslator -ICollections -IBackend -I. -IEncoders -ITuner -ISerialize LDFLAGS := -ldl -lrt -rdynamic SHARED := -shared @@ -39,6 +39,7 @@ ${OBJ_DIR}: ${MKDIR_P} ${OBJ_DIR}/Collections ${MKDIR_P} ${OBJ_DIR}/Backend ${MKDIR_P} ${OBJ_DIR}/Encoders + ${MKDIR_P} ${OBJ_DIR}/Serialize debug: CFLAGS += -DCONFIG_DEBUG debug: all diff --git a/src/Serialize/deserializer.cc b/src/Serialize/deserializer.cc new file mode 100644 index 0000000..1c45a69 --- /dev/null +++ b/src/Serialize/deserializer.cc @@ -0,0 +1,58 @@ + +/* + * File: deserializer.cc + * Author: hamed + * + * Created on September 7, 2017, 6:08 PM + */ + +#include "deserializer.h" +#include "csolver.h" +#include "unistd.h" +#include "fcntl.h" + +Deserializer::Deserializer(const char* file): + solver(new CSolver()) +{ + filedesc = open(file, O_RDONLY); + + if (filedesc < 0) { + exit(-1); + } +} + +Deserializer::~Deserializer() { + delete solver; +} + +ssize_t Deserializer::myread(void* __buf, size_t __nbytes){ + ssize_t t = read (filedesc, __buf, __nbytes); + write (1, __buf, __nbytes); + model_print("read\n"); + return t; +} + +CSolver * Deserializer::deserialize(){ + ASTNodeType nodeType; + while(myread(&nodeType, sizeof(ASTNodeType) ) >0){ + switch(nodeType){ + case BOOLEANEDGE: + deserializeBooleanEdge(); + break; + default: + ASSERT(0); + } + } + return solver; +} + +void Deserializer::deserializeBooleanEdge(){ + Boolean *b; + myread(&b, sizeof(Boolean*)); + BooleanEdge tmp(b); + bool isNegated = tmp.isNegated(); + ASSERT(map.contains(tmp.getBoolean())); + b = (Boolean*) map.get(tmp.getBoolean()); + BooleanEdge res(b); + solver->addConstraint(isNegated?res.negate():res); +} diff --git a/src/Serialize/deserializer.h b/src/Serialize/deserializer.h new file mode 100644 index 0000000..8448dff --- /dev/null +++ b/src/Serialize/deserializer.h @@ -0,0 +1,34 @@ + +/* + * File: deserializer.h + * Author: hamed + * + * Created on September 7, 2017, 6:07 PM + */ + +#ifndef DESERIALIZER_H +#define DESERIALIZER_H +#include "classlist.h" +#include "mymemory.h" +#include "structs.h" +/** + * Style of serialized file: + * ASTNodeType#Pointer#ObjectDATA + * + * @param file + */ +class Deserializer { +public: + Deserializer(const char* file); + CSolver *deserialize(); + virtual ~Deserializer(); +private: + ssize_t myread (void *__buf, size_t __nbytes); + void deserializeBooleanEdge(); + CSolver *solver; + int filedesc; + CloneMap map; +}; + +#endif /* DESERIALIZER_H */ + diff --git a/src/Serialize/serializable.h b/src/Serialize/serializable.h new file mode 100644 index 0000000..cdaea04 --- /dev/null +++ b/src/Serialize/serializable.h @@ -0,0 +1,18 @@ + +/* + * File: serializable.h + * Author: hamed + * + * Created on September 7, 2017, 3:39 PM + */ + +#ifndef SERIALIZABLE_H +#define SERIALIZABLE_H +#include "classlist.h" + +class Serializable{ + virtual void serialize(Serializer* ) = 0; +}; + +#endif /* SERIALIZABLE_H */ + diff --git a/src/Serialize/serializer.cc b/src/Serialize/serializer.cc new file mode 100644 index 0000000..301a312 --- /dev/null +++ b/src/Serialize/serializer.cc @@ -0,0 +1,42 @@ + +/* + * File: serializer.cc + * Author: hamed + * + * Created on September 7, 2017, 3:38 PM + */ + +#include "serializer.h" +#include "unistd.h" +#include "fcntl.h" + +Serializer::Serializer(const char *file) { + filedesc = open(file, O_WRONLY | O_CREAT, 0666); + + if (filedesc < 0) { + exit(-1); + } +} + +Serializer::~Serializer() { + if (-1 == close(filedesc)){ + exit(-1); + } +} + +void Serializer::mywrite(const void *__buf, size_t __n){ + write (1, __buf, __n); + model_print("\n"); + write (filedesc, __buf, __n); +} + + +void serializeBooleanEdge(Serializer* serializer, BooleanEdge& be){ + if(serializer->isSerialized(be.getRaw())) + return; + serializer->addObject(be.getRaw()); +// b->seralize(serializer); + ASTNodeType type = BOOLEANEDGE; + serializer->mywrite(&type, sizeof(ASTNodeType)); + serializer->mywrite(be.getRaw(), sizeof(Boolean*)); +} \ No newline at end of file diff --git a/src/Serialize/serializer.h b/src/Serialize/serializer.h new file mode 100644 index 0000000..eb114dc --- /dev/null +++ b/src/Serialize/serializer.h @@ -0,0 +1,37 @@ + +/* + * File: serializer.h + * Author: hamed + * + * Created on September 7, 2017, 3:38 PM + */ + +#ifndef SERIALIZER_H +#define SERIALIZER_H +#include "mymemory.h" +#include "structs.h" + +class Serializer { +public: + Serializer(const char *file); + void mywrite(const void *__buf, size_t __n); + inline bool isSerialized(void *obj); + inline void addObject(void *obj) { map.put(obj, obj);} + virtual ~Serializer(); + CMEMALLOC; +private: + int filedesc; + CloneMap map; +}; + +inline bool Serializer::isSerialized(void* obj){ + return map.contains(obj); +} + + + + +void serializeBooleanEdge(Serializer* serializer, BooleanEdge& be); + +#endif /* SERIALIZER_H */ + diff --git a/src/Test/buildconstraintstest.cc b/src/Test/buildconstraintstest.cc index af17a4b..e01fb8f 100755 --- a/src/Test/buildconstraintstest.cc +++ b/src/Test/buildconstraintstest.cc @@ -50,7 +50,7 @@ int main(int numargs, char **argv) { Element *inputs2 [] = {e4, e3}; BooleanEdge pred = solver->applyPredicate(equal2, inputs2, 2); solver->addConstraint(pred); - + solver->serialize(); if (solver->solve() == 1) printf("e1=%" PRIu64 " e2=%" PRIu64 " \n", solver->getElementValue(e1), solver->getElementValue(e2)); else diff --git a/src/classes.h b/src/classes.h index 3769439..db98ab2 100644 --- a/src/classes.h +++ b/src/classes.h @@ -26,6 +26,8 @@ class Tuner; class Transformer; class Set; class BooleanLogic; +class Serializer; + typedef uint64_t VarType; typedef unsigned int uint; diff --git a/src/csolver.cc b/src/csolver.cc index 6454b37..bcd8f3f 100644 --- a/src/csolver.cc +++ b/src/csolver.cc @@ -19,6 +19,8 @@ #include "integerencoding.h" #include "qsort.h" #include "preprocess.h" +#include "serializer.h" +#include "deserializer.h" CSolver::CSolver() : boolTrue(BooleanEdge(new BooleanConst(true))), @@ -84,6 +86,24 @@ CSolver *CSolver::clone() { return copy; } +void CSolver::serialize() { + { + Serializer serializer("dump"); + SetIteratorBooleanEdge *it = getConstraints(); + while (it->hasNext()) { + BooleanEdge b = it->next(); + serializeBooleanEdge(&serializer, b); + } + delete it; + } + model_print("deserializing ...\n"); + { + Deserializer deserializer("dump"); + deserializer.deserialize(); + } + +} + Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) { Set *set = new Set(type, elements, numelements); allSets.push(set); diff --git a/src/csolver.h b/src/csolver.h index b53b77b..feec9e6 100644 --- a/src/csolver.h +++ b/src/csolver.h @@ -138,6 +138,7 @@ public: void replaceBooleanWithFalse(BooleanEdge bexpr); void replaceBooleanWithBoolean(BooleanEdge oldb, BooleanEdge newb); CSolver *clone(); + void serialize(); void autoTune(uint budget); void setTuner(Tuner *_tuner) { tuner = _tuner; } -- 2.34.1