Changeset: d3635f1531d7 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=d3635f1531d7
Added Files:
        monetdb5/optimizer/opt_gadget.c
        monetdb5/optimizer/opt_gadget.h
Modified Files:
        monetdb5/optimizer/Makefile.ag
        monetdb5/optimizer/opt_pipes.c
        monetdb5/optimizer/opt_prelude.c
        monetdb5/optimizer/opt_prelude.h
        monetdb5/optimizer/opt_support.c
        monetdb5/optimizer/opt_support.h
        monetdb5/optimizer/opt_wrapper.c
        monetdb5/optimizer/optimizer.mal
Branch: data-vaults
Log Message:

Add the gadget optimizer


diffs (truncated from 348 to 300 lines):

diff --git a/monetdb5/optimizer/Makefile.ag b/monetdb5/optimizer/Makefile.ag
--- a/monetdb5/optimizer/Makefile.ag
+++ b/monetdb5/optimizer/Makefile.ag
@@ -28,6 +28,7 @@ lib_optimizer = {
                opt_deadcode.c opt_deadcode.h \
                opt_evaluate.c opt_evaluate.h \
                opt_factorize.c opt_factorize.h \
+               opt_gadget.c opt_gadget.h \
                opt_garbageCollector.c opt_garbageCollector.h \
                opt_generator.c opt_generator.h \
                opt_querylog.c opt_querylog.h \
diff --git a/monetdb5/optimizer/opt_gadget.c b/monetdb5/optimizer/opt_gadget.c
new file mode 100644
--- /dev/null
+++ b/monetdb5/optimizer/opt_gadget.c
@@ -0,0 +1,169 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
+ */
+
+#include "monetdb_config.h"
+#include "opt_gadget.h"
+#include "mal_interpreter.h"   /* for showErrors() */
+#include "mal_builder.h"
+/*
+ * The instruction sent is produced with a variation of call2str
+ * from the debugger.
+ */
+
+#define DEFAULT_NUM_TABLES 512
+
+typedef enum {
+    NOT_LOADED,
+    SUBMITTED,
+    LOADED
+} STATUS;
+
+typedef struct TABLE_ {
+    str sname;
+    str tname;
+    int arg0;
+} TABLE;
+
+static str
+checkTable(int *res, int *action, MalBlkPtr mb, InstrPtr p, TABLE **tabs, int 
num_tabs, str sname, str tname) {
+    int i = 0, j = 0;
+    InstrPtr c, a, r;
+    int upd = (getFunctionId(p)==bindRef) && (p->argc == 7 || p->argc == 9);
+    (void) *action;
+
+    if ( num_tabs && ((num_tabs % DEFAULT_NUM_TABLES) == 0)) {
+               *tabs = (TABLE*) GDKrealloc(tabs, sizeof(TABLE) * (num_tabs*2));
+        if (!tabs) {
+            throw(MAL, "optimizer.gadget", "Realloc failed");
+        }
+    }
+    for(i = 0; i < num_tabs; i++) {
+        if ( tabs[i]->sname && (strcmp(tabs[i]->sname, sname) == 0) && 
(strcmp(tabs[i]->tname, tname) == 0)) { 
+            r = newInstruction(mb,ASSIGNsymbol);
+            setModuleId(r, gadgetRef);
+            setFunctionId(r, getFunctionId(p));
+            getArg(r,0) = getArg(p,0);
+            for (j = 1; j < p->retc; j++) {
+                r = pushArgument(mb, r, getArg(p,j));
+            }
+            r= pushArgument(mb, r, tabs[i]->arg0);
+            for (j = p->retc; j < p->argc; j++) {
+                r = pushArgument(mb, r, getArg(p,j));
+            }
+            r->retc = p->retc;
+            pushInstruction(mb,r);
+            *res = num_tabs;
+            *action = *action+1;
+            return MAL_SUCCEED;
+        }
+    }
+
+    c = newInstruction(mb,ASSIGNsymbol);
+    setModuleId(c, gadgetRef);
+    setFunctionId(c, checktableRef);
+    getArg(c,0) = newTmpVariable(mb, TYPE_int);
+    c = pushArgument(mb, c, getArg(p,1+upd));
+    c = pushArgument(mb, c, getArg(p,3+upd));
+    c->retc = 1;
+    pushInstruction(mb,c);
+
+    a = newInstruction(mb,ASSIGNsymbol);
+    setModuleId(a, gadgetRef);
+    setFunctionId(a, analyzetableRef);
+    getArg(a,0) = newTmpVariable(mb, TYPE_int);
+    a = pushArgument(mb, a, getArg(p,1+upd));
+    a = pushArgument(mb, a, getArg(c,0));
+    a = pushArgument(mb, a, getArg(p,3+upd));
+    a->retc = 1;
+    pushInstruction(mb,a);
+
+    r = newInstruction(mb,ASSIGNsymbol);
+    setModuleId(r, gadgetRef);
+    setFunctionId(r, getFunctionId(p));
+    getArg(r,0) = getArg(p,0);
+    for (j = 1; j < p->retc; j++) {
+        r = pushArgument(mb, r, getArg(p,j));
+    }
+    r= pushArgument(mb, r, getArg(a,0));
+    for (j = p->retc; j < p->argc; j++) {
+        r = pushArgument(mb, r, getArg(p,j));
+    }
+    //r->retc = p->retc;
+    printf("R argc %d p argx %d \n", r->argc, p->argc);
+    pushInstruction(mb,r);
+
+    /*Add info about the table*/
+    if (!tabs[num_tabs])
+        tabs[num_tabs] = (TABLE*) GDKmalloc(sizeof(TABLE));
+    tabs[num_tabs]->sname = GDKstrdup(sname);
+    tabs[num_tabs]->tname = GDKstrdup(tname);
+    tabs[num_tabs]->arg0 = getArg(a,0);
+
+    *res = ++num_tabs;
+    *action = *action+2;
+
+    return MAL_SUCCEED;
+}
+
+int
+OPTgadgetImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci)
+{
+    InstrPtr p, *old;
+    int i, limit, slimit, action=0;
+    TABLE **tabs = NULL;
+    int num_tabs = 0;
+
+       mnstr_printf(cntxt->fdout, "Gadget optimizer started\n");
+       (void) cntxt;
+       (void) stk;
+       (void) pci;
+
+       limit = mb->stop;
+       slimit = mb->ssize;
+       old = mb->stmt;
+
+       if ( newMalBlkStmt(mb, mb->ssize) < 0){
+               return 0;
+       }
+
+    /*Set auxiliar structures*/
+    tabs = (TABLE**) GDKzalloc(sizeof(TABLE*) * DEFAULT_NUM_TABLES);
+
+    for (i = 0; i < limit; i++) {
+        p = old[i];
+        if( getModuleId(p)== sqlRef && ((getFunctionId(p)==bindRef) || 
(getFunctionId(p)==tidRef)) ){
+            int upd = (getFunctionId(p)==bindRef) && (p->argc == 7 || p->argc 
== 9);
+            str sname = getVarConstant(mb, getArg(p,2 + upd)).val.sval;
+            str tname = getVarConstant(mb, getArg(p,3 + upd)).val.sval;
+            checkTable(&num_tabs, &action, mb, p, tabs, num_tabs, sname, 
tname);
+            continue;
+        }
+        pushInstruction(mb,p);
+    }
+
+    for(; i<slimit; i++)
+        if( old[i])
+            freeInstruction(old[i]);
+    GDKfree(old);
+
+    if (tabs) {
+        for (i = 0; i < num_tabs; i++) {
+            if (tabs[i])
+                GDKfree(tabs[i]);
+        }
+        GDKfree(tabs);
+    }
+
+#ifdef DEBUG_OPT_GADGET
+       if (0 && action) {
+               mnstr_printf(cntxt->fdout, "gadget %d\n", action);
+               printFunction(cntxt->fdout, mb, 0, LIST_MAL_ALL);
+       }
+#endif
+       return action;
+}
diff --git a/monetdb5/optimizer/opt_gadget.h b/monetdb5/optimizer/opt_gadget.h
new file mode 100644
--- /dev/null
+++ b/monetdb5/optimizer/opt_gadget.h
@@ -0,0 +1,19 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
+ */
+
+#ifndef _OPT_GADGET_
+#define _OPT_GADGET_
+#include "opt_prelude.h"
+#include "opt_support.h"
+
+opt_export int
+OPTgadgetImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr 
pci);
+
+#define OPTDEBUGgadget  if ( optDebug & ((lng)1 <<DEBUG_OPT_GADGET) )
+
+#endif
diff --git a/monetdb5/optimizer/opt_pipes.c b/monetdb5/optimizer/opt_pipes.c
--- a/monetdb5/optimizer/opt_pipes.c
+++ b/monetdb5/optimizer/opt_pipes.c
@@ -121,6 +121,8 @@ static struct PIPELINES {
         "optimizer.profiler();"
         "optimizer.garbageCollector();",
         "stable", NULL, NULL, 1},
+
+
 /* The no_mitosis pipe line is (and should be kept!) identical to the
  * default pipeline, except that optimizer mitosis is omitted.  It is
  * used mainly to make some tests work deterministically, and to check
@@ -223,6 +225,41 @@ static struct PIPELINES {
         "optimizer.garbageCollector();",
         "stable", NULL, NULL, 1},
  */
+/*The gadget pipe line is (and should be kept!) identical to the
+ * default pipeline, except that optimizer gadget is added.
+ *
+ * NOTE:
+ * If you change the gadget pipe, please also update the man page
+ * (see tools/mserver/mserver5.1) accordingly!
+ */
+
+       {"gadget_pipe",
+        "optimizer.inline();"
+        "optimizer.candidates();"
+        "optimizer.remap();"
+        "optimizer.costModel();"
+        "optimizer.coercions();"
+        "optimizer.evaluate();"
+        "optimizer.aliases();"
+        "optimizer.pushselect();"
+        "optimizer.mitosis();"
+        "optimizer.mergetable();"
+        "optimizer.deadcode();"
+        "optimizer.aliases();"
+        "optimizer.constants();"
+        "optimizer.commonTerms();"
+        "optimizer.reorder();"
+        "optimizer.deadcode();"
+        "optimizer.reduce();"
+        "optimizer.matpack();"
+        "optimizer.dataflow();"
+        "optimizer.querylog();"
+        "optimizer.multiplex();"
+        "optimizer.generator();"
+        "optimizer.profiler();"
+        "optimizer.gadget();"
+        "optimizer.garbageCollector();",
+        "stable", NULL, NULL, 1},
 /* sentinel */
        {NULL, NULL, NULL, NULL, NULL, 0}
 };
diff --git a/monetdb5/optimizer/opt_prelude.c b/monetdb5/optimizer/opt_prelude.c
--- a/monetdb5/optimizer/opt_prelude.c
+++ b/monetdb5/optimizer/opt_prelude.c
@@ -228,6 +228,9 @@ str ilikesubselectRef;
 str userRef;
 str vectorRef;
 str zero_or_oneRef;
+str gadgetRef;
+str checktableRef;
+str analyzetableRef;
 
 void optimizerInit(void)
 {
@@ -444,6 +447,9 @@ void optimizerInit(void)
        vectorRef = putName("vector",6);
        zero_or_oneRef = putName("zero_or_one",11);
        userRef = putName("user",4);
+  gadgetRef = putName("gadget",6);
+  checktableRef = putName("checktable",10);
+  analyzetableRef = putName("analyzetable",12);
 
        /*
         * Set the optimizer debugging flag
diff --git a/monetdb5/optimizer/opt_prelude.h b/monetdb5/optimizer/opt_prelude.h
--- a/monetdb5/optimizer/opt_prelude.h
+++ b/monetdb5/optimizer/opt_prelude.h
@@ -228,4 +228,7 @@ opt_export  str not_ilikeRef;
 opt_export  str userRef;
 opt_export  str vectorRef;
 opt_export  str zero_or_oneRef;
+opt_export  str gadgetRef;
+opt_export  str checktableRef;
+opt_export  str analyzetableRef;
 #endif
diff --git a/monetdb5/optimizer/opt_support.c b/monetdb5/optimizer/opt_support.c
--- a/monetdb5/optimizer/opt_support.c
+++ b/monetdb5/optimizer/opt_support.c
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to