1 #include "encodinggraph.h"
11 #include "elementencoding.h"
13 EncodingGraph::EncodingGraph(CSolver *_solver) :
17 EncodingGraph::~EncodingGraph() {
18 subgraphs.resetAndDelete();
19 encodingMap.resetAndDeleteVals();
20 edgeMap.resetAndDeleteVals();
23 int sortEncodingEdge(const void *p1, const void *p2) {
24 const EncodingEdge *e1 = *(const EncodingEdge **) p1;
25 const EncodingEdge *e2 = *(const EncodingEdge **) p2;
26 uint64_t v1 = e1->getValue();
27 uint64_t v2 = e2->getValue();
36 void EncodingGraph::buildGraph() {
37 ElementIterator it(solver);
38 while (it.hasNext()) {
39 Element *e = it.next();
51 bsdqsort(edgeVector.expose(), edgeVector.getSize(), sizeof(EncodingEdge *), sortEncodingEdge);
56 void EncodingGraph::validate() {
57 SetIteratorBooleanEdge* it= solver->getConstraints();
59 BooleanEdge be = it->next();
60 if(be->type == PREDICATEOP){
61 BooleanPredicate *b = (BooleanPredicate *)be.getBoolean();
62 if(b->predicate->type == OPERATORPRED){
63 PredicateOperator* predicate = (PredicateOperator*) b->predicate;
64 if(predicate->getOp() == SATC_EQUALS){
65 ASSERT(b->inputs.getSize() == 2);
66 Element* e1= b->inputs.get(0);
67 Element* e2= b->inputs.get(1);
68 if(e1->type == ELEMCONST || e1->type == ELEMCONST)
70 ElementEncoding *enc1 = e1->getElementEncoding();
71 ElementEncoding *enc2 = e2->getElementEncoding();
72 ASSERT(enc1->getElementEncodingType() != ELEM_UNASSIGNED);
73 ASSERT(enc2->getElementEncodingType() != ELEM_UNASSIGNED);
74 if(enc1->getElementEncodingType() == enc2->getElementEncodingType() && enc1->getElementEncodingType() == BINARYINDEX && b->getFunctionEncoding()->type == CIRCUIT){
75 for(uint i=0; i<enc1->encArraySize; i++){
76 if(enc1->isinUseElement(i)){
77 uint64_t val1 = enc1->encodingArray[i];
78 if(enc2->isinUseElement(i)){
79 ASSERT(val1 == enc2->encodingArray[i]);
81 for(uint j=0; j< enc2->encArraySize; j++){
82 if(enc2->isinUseElement(j)){
83 ASSERT(val1 != enc2->encodingArray[j]);
90 //Now make sure that all the elements in the set are appeared in the encoding array!
91 for(uint k=0; k< b->inputs.getSize(); k++){
92 Element *e = b->inputs.get(k);
93 ElementEncoding *enc = e->getElementEncoding();
94 Set *s = e->getRange();
95 for (uint i = 0; i < s->getSize(); i++) {
96 uint64_t value = s->getElement(i);
98 for(uint j=0; j< enc->encArraySize; j++){
99 if(enc->isinUseElement(j) && enc->encodingArray[j] == value){
115 void EncodingGraph::encode() {
116 SetIteratorEncodingSubGraph *itesg = subgraphs.iterator();
117 model_print("#SubGraph = %u", subgraphs.getSize());
118 while (itesg->hasNext()) {
119 EncodingSubGraph *sg = itesg->next();
124 ElementIterator it(solver);
125 while (it.hasNext()) {
126 Element *e = it.next();
129 case ELEMFUNCRETURN: {
130 ElementEncoding *encoding = e->getElementEncoding();
131 if (encoding->getElementEncodingType() == ELEM_UNASSIGNED) {
132 EncodingNode *n = getNode(e);
135 ElementEncodingType encodetype = n->getEncoding();
136 encoding->setElementEncodingType(encodetype);
137 if (encodetype == UNARY || encodetype == ONEHOT) {
138 encoding->encodingArrayInitialization();
139 } else if (encodetype == BINARYINDEX) {
140 EncodingSubGraph *subgraph = graphMap.get(n);
141 DEBUG("graphMap.get(subgraph=%p, n=%p)\n", subgraph, n);
142 if (subgraph == NULL) {
143 encoding->encodingArrayInitialization();
146 uint encodingSize = subgraph->getEncodingMaxVal(n) + 1;
147 uint paddedSize = encoding->getSizeEncodingArray(encodingSize);
148 encoding->allocInUseArrayElement(paddedSize);
149 encoding->allocEncodingArrayElement(paddedSize);
150 Set *s = e->getRange();
151 for (uint i = 0; i < s->getSize(); i++) {
152 uint64_t value = s->getElement(i);
153 uint encodingIndex = subgraph->getEncoding(n, value);
154 encoding->setInUseElement(encodingIndex);
155 ASSERT(encoding->isinUseElement(encodingIndex));
156 encoding->encodingArray[encodingIndex] = value;
169 void EncodingGraph::encodeParent(Element *e) {
170 uint size = e->parents.getSize();
171 for (uint i = 0; i < size; i++) {
172 ASTNode *n = e->parents.get(i);
173 if (n->type == PREDICATEOP) {
174 BooleanPredicate *b = (BooleanPredicate *)n;
175 FunctionEncoding *fenc = b->getFunctionEncoding();
176 if (fenc->getFunctionEncodingType() != FUNC_UNASSIGNED)
178 Predicate *p = b->getPredicate();
179 if (p->type == OPERATORPRED) {
180 PredicateOperator *po = (PredicateOperator *)p;
181 ASSERT(b->inputs.getSize() == 2);
182 EncodingNode *left = createNode(b->inputs.get(0));
183 EncodingNode *right = createNode(b->inputs.get(1));
184 if (left == NULL || right == NULL)
186 EncodingEdge *edge = getEdge(left, right, NULL);
187 if (edge != NULL && edge->getEncoding() == EDGE_MATCH) {
188 fenc->setFunctionEncodingType(CIRCUIT);
195 void EncodingGraph::mergeNodes(EncodingNode *first, EncodingNode *second) {
196 EncodingSubGraph *graph1 = graphMap.get(first);
197 DEBUG("graphMap.get(first=%p, graph1=%p)\n", first, graph1);
198 EncodingSubGraph *graph2 = graphMap.get(second);
199 DEBUG("graphMap.get(second=%p, graph2=%p)\n", second, graph2);
201 first->setEncoding(BINARYINDEX);
203 second->setEncoding(BINARYINDEX);
205 if (graph1 == NULL && graph2 == NULL) {
206 graph1 = new EncodingSubGraph();
207 subgraphs.add(graph1);
208 DEBUG("graphMap.put(first=%p, graph1=%p)\n", first, graph1);
209 graphMap.put(first, graph1);
210 graph1->addNode(first);
212 if (graph1 == NULL && graph2 != NULL) {
215 EncodingNode *tmp = second;
219 if (graph1 != NULL && graph2 != NULL) {
220 SetIteratorEncodingNode *nodeit = graph2->nodeIterator();
221 while (nodeit->hasNext()) {
222 EncodingNode *node = nodeit->next();
223 graph1->addNode(node);
224 DEBUG("graphMap.put(node=%p, graph1=%p)\n", node, graph1);
225 graphMap.put(node, graph1);
227 subgraphs.remove(graph2);
229 DEBUG("Deleting graph2 =%p \n", graph2);
232 ASSERT(graph1 != NULL && graph2 == NULL);
233 graph1->addNode(second);
234 DEBUG("graphMap.put(first=%p, graph1=%p)\n", first, graph1);
235 graphMap.put(second, graph1);
239 void EncodingGraph::processElement(Element *e) {
240 uint size = e->parents.getSize();
241 for (uint i = 0; i < size; i++) {
242 ASTNode *n = e->parents.get(i);
245 processPredicate((BooleanPredicate *)n);
248 processFunction((ElementFunction *)n);
256 void EncodingGraph::processFunction(ElementFunction *ef) {
257 Function *f = ef->getFunction();
258 if (f->type == OPERATORFUNC) {
259 FunctionOperator *fo = (FunctionOperator *)f;
260 ASSERT(ef->inputs.getSize() == 2);
261 EncodingNode *left = createNode(ef->inputs.get(0));
262 EncodingNode *right = createNode(ef->inputs.get(1));
263 if (left == NULL && right == NULL)
265 EncodingNode *dst = createNode(ef);
266 EncodingEdge *edge = createEdge(left, right, dst);
271 void EncodingGraph::processPredicate(BooleanPredicate *b) {
272 Predicate *p = b->getPredicate();
273 if (p->type == OPERATORPRED) {
274 PredicateOperator *po = (PredicateOperator *)p;
275 ASSERT(b->inputs.getSize() == 2);
276 EncodingNode *left = createNode(b->inputs.get(0));
277 EncodingNode *right = createNode(b->inputs.get(1));
278 if (left == NULL || right == NULL)
280 EncodingEdge *edge = createEdge(left, right, NULL);
281 CompOp op = po->getOp();
290 edge->numComparisons++;
298 uint convertSize(uint cost) {
299 cost = FUDGEFACTOR * cost;// fudge factor
300 return NEXTPOW2(cost);
303 void EncodingGraph::decideEdges() {
304 uint size = edgeVector.getSize();
305 for (uint i = 0; i < size; i++) {
306 EncodingEdge *ee = edgeVector.get(i);
307 EncodingNode *left = ee->left;
308 EncodingNode *right = ee->right;
310 if (ee->encoding != EDGE_UNASSIGNED ||
311 !left->couldBeBinaryIndex() ||
312 !right->couldBeBinaryIndex())
315 uint64_t eeValue = ee->getValue();
319 EncodingSubGraph *leftGraph = graphMap.get(left);
320 DEBUG("graphMap.get(left=%p, leftgraph=%p)\n", left, leftGraph);
321 EncodingSubGraph *rightGraph = graphMap.get(right);
322 DEBUG("graphMap.get(right=%p, rightgraph=%p)\n", right, rightGraph);
323 if (leftGraph == NULL && rightGraph != NULL) {
324 EncodingNode *tmp = left; left = right; right = tmp;
325 EncodingSubGraph *tmpsg = leftGraph; leftGraph = rightGraph; rightGraph = tmpsg;
328 uint leftSize = 0, rightSize = 0, newSize = 0;
329 uint64_t totalCost = 0;
331 // model_print("**************decideEdge*************\n");
332 // model_print("LeftNode Size = %u\n", left->getSize());
333 // model_print("rightNode Size = %u\n", right->getSize());
334 // model_print("UnionSize = %u\n", left->s->getUnionSize(right->s));
336 if (leftGraph == NULL && rightGraph == NULL) {
337 leftSize = convertSize(left->getSize());
338 rightSize = convertSize(right->getSize());
339 newSize = convertSize(left->s->getUnionSize(right->s));
340 newSize = (leftSize > newSize) ? leftSize : newSize;
341 newSize = (rightSize > newSize) ? rightSize : newSize;
342 totalCost = (newSize - leftSize) * left->elements.getSize() +
343 (newSize - rightSize) * right->elements.getSize();
344 if(leftSize == newSize && rightSize == newSize){
347 } else if (leftGraph != NULL && rightGraph == NULL) {
348 leftSize = convertSize(leftGraph->encodingSize);
349 rightSize = convertSize(right->getSize());
350 newSize = convertSize(leftGraph->estimateNewSize(right));
351 newSize = (leftSize > newSize) ? leftSize : newSize;
352 newSize = (rightSize > newSize) ? rightSize : newSize;
353 totalCost = (newSize - leftSize) * leftGraph->numElements +
354 (newSize - rightSize) * right->elements.getSize();
355 if(leftSize == newSize && rightSize == newSize){
360 //Are we already merged?
361 if (leftGraph == rightGraph)
363 leftSize = convertSize(leftGraph->encodingSize);
364 rightSize = convertSize(rightGraph->encodingSize);
365 newSize = convertSize(leftGraph->estimateNewSize(rightGraph));
366 newSize = (leftSize > newSize) ? leftSize : newSize;
367 newSize = (rightSize > newSize) ? rightSize : newSize;
368 totalCost = (newSize - leftSize) * leftGraph->numElements +
369 (newSize - rightSize) * rightGraph->numElements;
370 // model_print("LeftGraph size=%u\n", leftGraph->encodingSize);
371 // model_print("RightGraph size=%u\n", rightGraph->encodingSize);
372 // model_print("UnionGraph size = %u\n", leftGraph->estimateNewSize(rightGraph));
373 if(rightSize < 64 && leftSize < 64){
377 // model_print("******************************\n");
380 mergeNodes(left, right);
385 static TunableDesc EdgeEncodingDesc(EDGE_UNASSIGNED, EDGE_MATCH, EDGE_UNASSIGNED);
387 EncodingEdge *EncodingGraph::getEdge(EncodingNode *left, EncodingNode *right, EncodingNode *dst) {
388 EncodingEdge e(left, right, dst);
389 EncodingEdge *result = edgeMap.get(&e);
393 EncodingEdge *EncodingGraph::createEdge(EncodingNode *left, EncodingNode *right, EncodingNode *dst) {
394 EncodingEdge e(left, right, dst);
395 EncodingEdge *result = edgeMap.get(&e);
396 if (result == NULL) {
397 result = new EncodingEdge(left, right, dst);
398 VarType v1 = left->getType();
399 VarType v2 = right->getType();
406 if ((left != NULL && left->couldBeBinaryIndex()) &&
407 (right != NULL) && right->couldBeBinaryIndex()) {
408 EdgeEncodingType type = (EdgeEncodingType)solver->getTuner()->getVarTunable(v1, v2, EDGEENCODING, &EdgeEncodingDesc);
409 result->setEncoding(type);
410 if (type == EDGE_MATCH) {
411 mergeNodes(left, right);
414 edgeMap.put(result, result);
415 edgeVector.push(result);
417 left->edges.add(result);
419 right->edges.add(result);
421 dst->edges.add(result);
426 EncodingNode::EncodingNode(Set *_s) :
430 uint EncodingNode::getSize() const {
434 VarType EncodingNode::getType() const {
438 static TunableDesc NodeEncodingDesc(ELEM_UNASSIGNED, BINARYINDEX, ELEM_UNASSIGNED);
440 EncodingNode *EncodingGraph::createNode(Element *e) {
441 if (e->type == ELEMCONST)
443 Set *s = e->getRange();
444 EncodingNode *n = encodingMap.get(s);
446 n = new EncodingNode(s);
447 n->setEncoding((ElementEncodingType)solver->getTuner()->getVarTunable(n->getType(), NODEENCODING, &NodeEncodingDesc));
449 encodingMap.put(s, n);
455 EncodingNode *EncodingGraph::getNode(Element *e) {
456 if (e->type == ELEMCONST)
458 Set *s = e->getRange();
459 EncodingNode *n = encodingMap.get(s);
463 void EncodingNode::addElement(Element *e) {
467 EncodingEdge::EncodingEdge(EncodingNode *_l, EncodingNode *_r) :
471 encoding(EDGE_UNASSIGNED),
478 EncodingEdge::EncodingEdge(EncodingNode *_left, EncodingNode *_right, EncodingNode *_dst) :
482 encoding(EDGE_UNASSIGNED),
489 uint hashEncodingEdge(EncodingEdge *edge) {
490 uintptr_t hash = (((uintptr_t) edge->left) >> 2) ^ (((uintptr_t)edge->right) >> 4) ^ (((uintptr_t)edge->dst) >> 6);
494 bool equalsEncodingEdge(EncodingEdge *e1, EncodingEdge *e2) {
495 return e1->left == e2->left && e1->right == e2->right && e1->dst == e2->dst;
498 uint64_t EncodingEdge::getValue() const {
499 uint lSize = (left != NULL) ? left->getSize() : 1;
500 uint rSize = (right != NULL) ? right->getSize() : 1;
501 uint min = (lSize < rSize) ? lSize : rSize;
502 return numEquals * min + numComparisons * lSize * rSize;