Changeset: a2f71b2cacb8 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/a2f71b2cacb8
Modified Files:
        monetdb5/modules/kernel/vss.c
        monetdb5/modules/kernel/vss2.c
Branch: nested
Log Message:

in case of width vectors use larger blocks.


diffs (truncated from 846 to 300 lines):

diff --git a/monetdb5/modules/kernel/vss.c b/monetdb5/modules/kernel/vss.c
--- a/monetdb5/modules/kernel/vss.c
+++ b/monetdb5/modules/kernel/vss.c
@@ -46,10 +46,15 @@
 typedef struct bond_collection {
        BAT **dims;        /* array of dimension BATs (not owned, just 
referenced) */
        int ndims;         /* number of dimensions */
+       int bsz;                           /* block size */
        BUN nvecs;         /* number of vectors (rows) */
        dbl *dim_means;    /* mean value per dimension (for dimension ordering) 
*/
-       dbl *dim_max;           /* max value per dimension */
-       dbl *dim_min;           /* min value per dimension */
+       oid *candidates;
+       oid *tcand;
+       oid *tc;
+       dbl *dists;
+       dbl *tdist;
+       dbl *td;
        dbl kth_upper;          /* kth upper bound */
 } bond_collection;
 
@@ -83,7 +88,7 @@ dim_score_cmp(const void *a, const void 
  * |query[d] - mean[d]| descending (most discriminating first).
  */
 static int *
-bond_dim_order(allocator *ma, bond_collection *bc, const dbl *query_vals)
+bond_dim_order(allocator *ma, bond_collection *bc)
 {
        dim_score *scores = ma_alloc(ma, bc->ndims * sizeof(dim_score));
        int *order = ma_alloc(ma, bc->ndims * sizeof(int));
@@ -95,11 +100,17 @@ bond_dim_order(allocator *ma, bond_colle
 
        for (int d = 0; d < bc->ndims; d++) {
                scores[d].dim = d;
-               dbl diff = query_vals[d] - bc->dim_means[d];
-               scores[d].score = diff < 0 ? -diff : diff;
+               scores[d].score = bc->dim_means[d];
        }
 
+       /* TODO use gdk order, with 2 arrays, ie order and bc->dim_means */
        qsort(scores, bc->ndims, sizeof(dim_score), dim_score_cmp);
+       /*
+       for (int d = 0; d < bc->ndims; d++) {
+               printf("%d %F ", scores[d].dim, scores[d].score);
+       }
+       printf("\n");
+       */
 
        for (int i = 0; i < bc->ndims; i++)
                order[i] = scores[i].dim;
@@ -107,8 +118,10 @@ bond_dim_order(allocator *ma, bond_colle
        return order;
 }
 
+#define SS 8
+#define VS (2048)
 static bond_collection *
-bond_create(allocator *ma, BAT **dim_bats, int ndims)
+bond_create(allocator *ma, BAT **dim_bats, int ndims, int k)
 {
        if (dim_bats && ndims > 0) {
                bond_collection *bc = ma_alloc(ma, sizeof(bond_collection));
@@ -117,24 +130,22 @@ bond_create(allocator *ma, BAT **dim_bat
                        .kth_upper = DBL_MAX,
                };
                if (bc) {
+                       bc->bsz = VS;
+                       if (ndims > 512)
+                               bc->bsz = 8*VS;
+                       bc->candidates = ma_alloc(ma, bc->bsz * sizeof(oid));
+                       bc->dists = ma_alloc(ma, bc->bsz * sizeof(dbl));
+
+                       bc->tcand = ma_alloc(ma, k * sizeof(oid));
+                       bc->tdist = ma_alloc(ma, k * sizeof(dbl));
+                       bc->tc = ma_alloc(ma, bc->bsz * sizeof(oid));
+                       bc->td = ma_alloc(ma, bc->bsz * sizeof(dbl));
                        bc->dims = ma_alloc(ma, ndims * sizeof(BAT *));
-                       bc->dim_means = ma_zalloc(ma, ndims * sizeof(dbl));
-                       bc->dim_max = ma_zalloc(ma, ndims * sizeof(dbl));
-                       bc->dim_min = ma_zalloc(ma, ndims * sizeof(dbl));
+                       bc->dim_means = ma_alloc(ma, ndims * sizeof(dbl));
                        bc->nvecs = BATcount(dim_bats[0]);
                        for (int d = 0; d < ndims; d++) {
                                BAT *b = dim_bats[d];
                                bc->dims[d] = b;
-                               // calc avg over the sample instead
-                               /*
-                               dbl avg;
-                               BUN cnt;
-                               if (BATcalcavg(b, NULL, &avg, &cnt, 0) != 
GDK_SUCCEED)
-                                       return NULL;
-                               bc->dim_means[d] = avg;
-                               bc->dim_max[d] = *(dbl*)BATmax(b, NULL);
-                               bc->dim_min[d] = *(dbl*)BATmin(b, NULL);
-                               */
                        }
                        return bc;
                }
@@ -142,175 +153,260 @@ bond_create(allocator *ma, BAT **dim_bat
        return NULL;
 }
 
-static dbl
-quickselect(dbl *a, BUN n, BUN k)
+static inline void
+heap_down(oid *hcand, dbl *hd, int p, int k)
 {
-       // Safety check for empty arrays or invalid k
-    if (n == 0 || k == 0 || k > n) return DBL_MAX;
-       if (k == 1) {
-               dbl min_dist = a[0];
-               for (BUN i = 1; i < n; i++) {
-                       if (a[i] < min_dist) min_dist = a[i];
-               }
-               return min_dist;
+       int l = p*2+1, r = p*2+2, q = p;
+       if (l < k && hd[q] < hd[l])
+               q = l;
+       if (r < k && hd[q] < hd[r])
+               q = r;
+       if (q != p) {
+               oid s = hcand[q];
+               hcand[q] = hcand[p];
+               hcand[p] = s;
+               dbl d = hd[q];
+               hd[q] = hd[p];
+               hd[p] = d;
+               heap_down(hcand, hd, q, k);
        }
-    BUN left = 0, right = n - 1, index = k - 1;
-    while (left < right) {
-        dbl pivot = a[(left + right) / 2];
-        BUN i = left, j = right;
-        while (i <= j) {
-            while (a[i] < pivot) i++;
-            while (a[j] > pivot) j--;
-            if (i <= j) {
-                dbl tmp = a[i]; a[i] = a[j]; a[j] = tmp;
-                i++; j--;
-            }
-        }
-        if (index <= j) right = j;
-        else if (i <= index) left = i;
-        else break;
-    }
-    return a[index];
+}
+
+static inline void
+heap_del(oid *hcand, dbl *hd, int k)
+{
+       hcand[0] = hcand[k-1];
+       hd[0] = hd[k-1];
+       heap_down(hcand, hd, 0, k-1);
+}
+
+static inline void
+heap_up(oid *hcand, dbl *hd, int p)
+{
+       if (p == 0)
+               return;
+       size_t q = (p-1)/2;
+       if (hd[q] < hd[p]) { /* max heap */
+               oid s = hcand[q];
+               hcand[q] = hcand[p];
+               hcand[p] = s;
+               dbl d = hd[q];
+               hd[q] = hd[p];
+               hd[p] = d;
+               heap_up(hcand, hd, q);
+       }
 }
 
+/* max heap */
+static dbl
+topn_merge(oid *cands, dbl *d, oid *icands, dbl *id, BUN n, BUN k)
+{
+       for (BUN i = 0; i < n; i++) {
+               if (id[i] < d[0]) {
+                       heap_del(cands, d, k);
+                       cands[k-1] = icands[i];
+                       d[k-1] = id[i];
+                       heap_up(cands, d, k-1);
+               }
+       }
+       /*
+       for(i = 0; i < k; i++)
+               printf("%d %F\n", (int)hcand[i], hd[i]);
+               */
+       return d[0];
+}
+
+/* max heap */
+static dbl
+topn(oid *cands, dbl *d, bond_collection *bc, BUN n, BUN k)
+{
+       oid *hcand = bc->tcand;
+       dbl *hd = bc->tdist;
+       BUN i = 0;
+       for (; i < k && i < n; i++) {
+               hcand[i] = cands[i];
+               hd[i] = d[i];
+               heap_up(hcand, hd, i);
+       }
+
+       for (; i < n; i++) {
+               if (d[i] < hd[0]) {
+                       heap_del(hcand, hd, k);
+                       hcand[k-1] = cands[i];
+                       hd[k-1] = d[i];
+                       heap_up(hcand, hd, k-1);
+               }
+       }
+       /*
+       for(i = 0; i < k; i++)
+               printf("%d %F\n", (int)hcand[i], hd[i]);
+               */
+       return hd[0];
+}
 
 static dbl
-bond_upper_bound_sampled(allocator *ma, bond_collection *bc, const dbl 
*query_vals, BUN k)
+bond_upper_bound(bond_collection *bc, const dbl *query_vals, BUN k)
 {
-       BUN sample_size = 10000;
-    if (bc->nvecs < sample_size) sample_size = bc->nvecs;
-
-       dbl *sample_dists = ma_zalloc(ma, sample_size * sizeof(dbl));
-
-    // random sample of OIDs
-    BAT *sample_oids = BATsample(bc->dims[0], sample_size);
-    if (!sample_oids) return DBL_MAX;
+       oid *cands = bc->candidates, *tc = bc->tc;
+       dbl *dists = bc->dists, *td = bc->td;
 
        // calc full distance for the sample across all dimensions
-    for (int d = 0; d < bc->ndims; d++) {
-        BAT *pd = BATproject(sample_oids, bc->dims[d]);
-        const dbl *vals = (const dbl *) Tloc(pd, 0);
-        dbl q = query_vals[d];
+       dbl res = 0;
+       for (int nr = 0; nr < SS; nr++) {
+               oid base = nr * bc->bsz;
+               for (int i = 0; i < bc->bsz; i++) {
+                       tc[i] = base + i;
+                       td[i] = 0;
+               }
 
-        for (BUN i = 0; i < sample_size; i++) {
-            dbl diff = vals[i] - q;
-            sample_dists[i] += diff * diff;
-        }
+               for (int d = 0; d < bc->ndims; d++) {
+                       const dbl *vals = (const dbl *) Tloc(bc->dims[d], 0);
+                       dbl q = query_vals[d];
+                       dbl sum = 0;
 
-               dbl avg;
-               if (BATcalcavg(pd, NULL, &avg, NULL, 0) == GDK_SUCCEED)
-                       bc->dim_means[d] = avg;
-               //bc->dim_min[d] = *(dbl*)BATmin(pd, NULL);
-               //bc->dim_max[d] = *(dbl*)BATmax(pd, NULL);
-        BBPreclaim(pd);
-    }
-
-       //qsort(sample_dists, sample_size, sizeof(dbl), dbl_cmp);
-    //dbl result = (k < sample_size) ? sample_dists[k-1] : 
sample_dists[sample_size-1];
-       dbl result = quickselect(sample_dists, sample_size, k);
-    BBPreclaim(sample_oids);
-    return result;
+                       for (int i = 0; i < bc->bsz; i++) {
+                               dbl diff = vals[tc[i]] - q;
+                               dbl m = diff * diff;
+                               td[i] += m;
+                               sum += m;
+                       }
+                       bc->dim_means[d] += sum;
+               }
+               if (nr == 0) {
+                       res = topn(tc, td, bc, bc->bsz, k);
+                       for(BUN i = 0; i < k; i++) {
+                               cands [i] = bc->tcand[i];
+                               dists[i] = bc->tdist[i];
+                       }
+               } else {
+                       res = topn_merge(cands, dists, bc->tc, bc->td, bc->bsz, 
k);
+                       for(BUN i = 0; i < k; i++) {
+                               cands [i] = bc->tcand[i];
+                               dists[i] = bc->tdist[i];
+                       }
+               }
+       }
+       for (int d = 0; d < bc->ndims; d++) {
+               bc->dim_means[d] /= (bc->bsz*SS);
+       }
+       return res;
 }
 
-
 static char*
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to