Changeset: dc16f874e0d3 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=dc16f874e0d3
Modified Files:
        monetdb5/extras/bwd/91_bwd.sql
        monetdb5/extras/bwd/bwd.c
        monetdb5/extras/bwd/opt_bwd.mal
        monetdb5/extras/bwd/utilities.c
        monetdb5/extras/bwd/utilities.h
Branch: bwd
Log Message:

* implementing foreign-key decomposition
* had to implement 8-byte datatype decomposition (allowing only 4-byte values, 
though)


Unterschiede (230 Zeilen):

diff --git a/monetdb5/extras/bwd/91_bwd.sql b/monetdb5/extras/bwd/91_bwd.sql
--- a/monetdb5/extras/bwd/91_bwd.sql
+++ b/monetdb5/extras/bwd/91_bwd.sql
@@ -20,6 +20,9 @@ returns varchar(4096) external name bwd.
 create function bwdecomposeint(col integer, bits integer)
 returns varchar(4096) external name bwd.decompose;
 
+create function bwdecomposefk(schema varchar(24), tabl varchar(24), columnn 
varchar(24), bits integer)
+returns varchar(4096) external name bwd.decomposefk;
+
 
 
 create function bwdevices()
diff --git a/monetdb5/extras/bwd/bwd.c b/monetdb5/extras/bwd/bwd.c
--- a/monetdb5/extras/bwd/bwd.c
+++ b/monetdb5/extras/bwd/bwd.c
@@ -118,6 +118,9 @@ str bwdecompose(bat * res, bat * subject
        } else if (Tsize(subject) == 4){
                BATsetprop(subject, batRegistryIndex, TYPE_int, 
(int[]){decomposeIntArray((int*)Tloc(subject, BUNfirst(subject)), 
subject->batCount, *approximationBits)});
                snprintf(buffer, 4096, "successfully decomposed integer bat 
%d", subject->batCacheid);
+       } else if (Tsize(subject) == 8){
+               BATsetprop(subject, batRegistryIndex, TYPE_lng, 
(lng[]){decomposeLngArray((lng*)Tloc(subject, BUNfirst(subject)), 
subject->batCount, *approximationBits)});
+               snprintf(buffer, 4096, "successfully decomposed long integer 
bat %d", subject->batCacheid);
        } else if(Tsize(subject) == 1){
                        BATsetprop(subject, batRegistryIndex, TYPE_int, 
(int[]){decomposeVarchar1Array(Tloc(subject, BUNfirst(subject)), 
subject->batCount, Tsize(subject), Tbase(subject), *approximationBits)});
                        snprintf(buffer, 4096, "successfully decomposed char 
bat %d", subject->batCacheid);
@@ -131,6 +134,61 @@ str bwdecompose(bat * res, bat * subject
        return MAL_SUCCEED;
 }
 
+str bwdecomposeFK (Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) {
+       ValRecord* result = getArgReference(stk,pci,0);
+       ValRecord* schema = getArgReference(stk,pci,1);
+       ValRecord* table = getArgReference(stk,pci,2);
+       ValRecord* column = getArgReference(stk,pci,3);
+       ValRecord* approximationBits = getArgReference(stk,pci,4);
+
+       assert(approximationBits->val.ival == sizeof(oid)*8);
+
+       printf ("transfering FK to GPU: %s.%s\n",table->val.sval, 
column->val.sval);
+       { // this is an ad-hoc copy of Niels' code in sql.mx
+               mvc *m = NULL;
+               str msg = getSQLContext(cntxt,mb, &m, NULL);
+               sql_trans *tr = m->session->tr;
+               node *nsch, *ntab, *ncol;
+               assert(msg == MAL_SUCCEED);
+
+               for( nsch= tr->schemas.set->h; nsch; nsch= nsch->next){
+                       sql_schema *s= (sql_schema*) nsch->data;
+                       if (s->tables.set){
+                               ntab= (s)->tables.set->h;
+                               if (s->tables.set){
+                                       for(ntab= (s)->tables.set->h ;ntab; 
ntab= ntab->next){
+                                               sql_table* t = ntab->data;
+                                               
+                                               if (isTable(t)){
+                                                       if(t->idxs.set){
+                                                               for (ncol= 
(t)->idxs.set->h; ncol; ncol= ncol->next){
+                                                                       sql_idx 
*c=  ncol->data;
+                                                                       
if(strcmp(schema->val.sval, s->base.name)==0 
+                                                                               
 && strcmp(table->val.sval, t->base.name)==0 
+                                                                               
 && strcmp(column->val.sval, c->base.name)==0){
+                                                                               
BAT *bn = store_funcs.bind_idx(m->session->tr, c, 0);
+                                                                               
bat result;
+                                                                               
bwdecompose(& result, (bat[]){bn->batCacheid}, &(approximationBits->val.ival), 
cntxt);
+                                                                               
BBPunfix(bn->batCacheid);
+                                                                               
BBPdecref(result,1);
+                                                                       }
+                               
+                                                               }
+                                                       }
+                                               }                               
                
+                                       }
+                               }
+
+                       }
+               }
+       }
+       result->vtype = TYPE_str;
+       result->len = 8;
+       result->val.sval = GDKmalloc(64);
+       sprintf(result->val.sval, "oki");
+       return MAL_SUCCEED;
+}
+
 
 
 char* resolveBatToAttribute(bat bid, Client cntxt){
@@ -166,9 +224,6 @@ char* resolveBatToAttribute(bat bid, Cli
        }
        snprintf(buffer, 32, "%d", bid);
        return buffer;
-               
-               /* BAT *bn = store_funcs.bind_col(tr, c, 0); */
-               
 }
 
 
diff --git a/monetdb5/extras/bwd/opt_bwd.mal b/monetdb5/extras/bwd/opt_bwd.mal
--- a/monetdb5/extras/bwd/opt_bwd.mal
+++ b/monetdb5/extras/bwd/opt_bwd.mal
@@ -222,6 +222,9 @@ address bwdecomposeScalar;
 command decompose(column:str, approximationBits: int):void
 address bwdecomposeScalar;
 
+pattern decomposefk(schema:str, table:str, column:str, approximationBits: 
int):str
+address bwdecomposeFK;
+
 command init():void
 address bwdinit;
 
diff --git a/monetdb5/extras/bwd/utilities.c b/monetdb5/extras/bwd/utilities.c
--- a/monetdb5/extras/bwd/utilities.c
+++ b/monetdb5/extras/bwd/utilities.c
@@ -320,8 +320,8 @@ DecomposedBATSlot* getDecomposedBATSlotF
 /* } */
 
 typedef struct bounds__ {
-       int min;
-       int max;
+       long min;
+       long max;
 } bounds;
 
 bounds findBounds(const int* subject, const size_t size){
@@ -337,6 +337,19 @@ bounds findBounds(const int* subject, co
        }
 }
 
+bounds findLngBounds(const lng* subject, const size_t size){
+       assert(size>0);
+       {
+               bounds result = {subject[0], subject[0]};
+               unsigned int i;
+               for (i = 0; i < size; ++i) {
+                       result.min = MIN(result.min, subject[i]);
+                       result.max = MAX(result.max, subject[i]);
+               }
+               return result;
+       }
+}
+
 
 
 void forceLoadOntoGPU(clTail* approximation, const size_t approximationSize, 
DecomposedBATSlot* slot){ 
@@ -397,7 +410,63 @@ unsigned int decomposeIntArray(const int
        slot->tailOffsetBits = 32-neededBits;
        slot->approximationBits = approximationBits;
        slot->isPersistentBAT = 1;
-       printf ("bounds: %d, %d, offset %zd bits\n",subjectBounds.min, 
subjectBounds.max, slot->tailOffsetBits);
+       printf ("bounds: %ld, %ld, offset %zd bits\n",subjectBounds.min, 
subjectBounds.max, slot->tailOffsetBits);
+               
+       printf ("using %s for approximation and %s for residuals \n", 
humanreadablesize(approximationSize, (char[64]){}, 64), 
humanreadablesize(ceil(size*residualBits/8.0) + 8, (char[64]){}, 64));
+       slot->residuals = GDKzalloc(ceil(size*residualBits/8.0) + 8);
+       approximation->base = subjectBounds.min;
+       slot->tailOffsetValue = approximation->base;
+       approximation->count = size;
+       
+       for (i = 0; i < size; ++i) {
+               { // approximation
+                       const size_t slotI = (i*approximationBits)/32;
+                       const unsigned int offset = (i*approximationBits)%32;
+                       if(offset+approximationBits > 32) {
+                               ((unsigned int*)approximation->elements)[slotI] 
|= (((subject[i]-approximation->base) >> 
residualBits)>>(approximationBits-(32-offset)));
+                               ((unsigned 
int*)approximation->elements)[slotI+1] |= (((subject[i]-approximation->base) >> 
residualBits)<<(32-(approximationBits-(32-offset))));
+                       } else {
+                               ((unsigned int*)approximation->elements)[slotI] 
|= (((subject[i]-approximation->base) >> 
residualBits)<<(32-offset-approximationBits));
+                       }
+               }
+               { // residual
+                       const size_t slotI = (i*residualBits)/32;
+                       const unsigned int offset = (i*residualBits)%32;
+                       if(offset+residualBits <= 32) {
+                               ((unsigned int*)slot->residuals)[slotI] |= 
(((subject[i]-approximation->base)&residualMask)<<(32-offset-residualBits));
+                       } else {
+                               ((unsigned int*)slot->residuals)[slotI] |= 
(((subject[i]-approximation->base)&residualMask)>>(residualBits-(32-offset)));
+                               ((unsigned int*)slot->residuals)[slotI+1] |= 
(((subject[i]-approximation->base)&residualMask)<<(32-(residualBits-(32-offset))));
+                       }
+               }
+       }
+
+       forceLoadOntoGPU(approximation, approximationSize, slot);
+       
+       GDKfree(approximation);
+       return newIndex;
+}
+
+unsigned int decomposeLngArray(const lng* subject, const size_t size, const 
size_t baseBitsPlusApproximationBits){
+       const bounds subjectBounds = findLngBounds(subject,size);
+       const int neededBits = ceil(log2(subjectBounds.max - 
subjectBounds.min));
+
+       const unsigned int newIndex = getNextFreeDecomposedBATSlotIndex();
+       DecomposedBATSlot* slot = getDecomposedBATSlotForIndex(newIndex);
+       const unsigned int residualBits = 64-baseBitsPlusApproximationBits;
+       const unsigned int residualMask = (1 << residualBits)-1;
+       const unsigned int approximationBits = neededBits-residualBits;
+       const size_t approximationSize = ceil(size*approximationBits/8.0) + 
sizeof(long) + sizeof(clTail);
+       clTail* approximation = GDKzalloc(approximationSize);
+       unsigned int i;
+
+       if(neededBits>32)
+                       abort();
+
+       slot->tailOffsetBits = 64-neededBits;
+       slot->approximationBits = approximationBits;
+       slot->isPersistentBAT = 1;
+       printf ("bounds: %ld, %ld, offset %zd bits\n",subjectBounds.min, 
subjectBounds.max, slot->tailOffsetBits);
                
        printf ("using %s for approximation and %s for residuals \n", 
humanreadablesize(approximationSize, (char[64]){}, 64), 
humanreadablesize(ceil(size*residualBits/8.0) + 8, (char[64]){}, 64));
        slot->residuals = GDKzalloc(ceil(size*residualBits/8.0) + 8);
@@ -471,7 +540,7 @@ unsigned int decomposeVarchar1Array(cons
        slot->tailOffsetBits = 8-neededBits;
        slot->approximationBits = approximationBits;
        slot->isPersistentBAT = 1;
-       printf ("bounds: %d, %d, offset %zd bits\n",subjectBounds.min, 
subjectBounds.max, slot->tailOffsetBits);
+       printf ("bounds: %ld, %ld, offset %zd bits\n",subjectBounds.min, 
subjectBounds.max, slot->tailOffsetBits);
                
        printf ("using %s for approximation and %s for residuals \n", 
humanreadablesize(approximationSize, (char[64]){}, 64), 
humanreadablesize(ceil(size*residualBits/8.0) + 8, (char[64]){}, 64));
        slot->residuals = GDKzalloc(ceil(size*residualBits/8.0) + 8);
diff --git a/monetdb5/extras/bwd/utilities.h b/monetdb5/extras/bwd/utilities.h
--- a/monetdb5/extras/bwd/utilities.h
+++ b/monetdb5/extras/bwd/utilities.h
@@ -51,6 +51,7 @@ int getGPUDeviceForThisThread();
 
 unsigned int decomposeIntArray(const int* subject, const size_t size, const 
size_t approximationBits);
 unsigned int decomposeVarchar1Array(const char* subject, const size_t size, 
const size_t width, const char* dictionary, const size_t 
baseBitsPlusApproximationBits);
+unsigned int decomposeLngArray(const lng* subject, const size_t size, const 
size_t baseBitsPlusApproximationBits);
 
 const char* clError(int) __attribute__((pure));
 cl_device_id getDeviceID();
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to