Changeset: 7b23894e5e18 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/7b23894e5e18 Removed Files: monetdb5/modules/kernel/vss2.c Modified Files: monetdb5/modules/kernel/vss.c sql/include/sql_catalog.h sql/server/sql_parser.y Branch: nested Log Message:
add field for vector/array dim size (for now no support for multi dimentional arrays/vectors) removed old vss2.c diffs (truncated from 932 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 @@ -473,7 +473,7 @@ topn(oid *cands, dbl *d, oid *hcand, dbl #define bond_upper_bound bond_upper_bound_flt #define bond_search_fast bond_search_fast_flt #define BONDknn BONDknn_flt -#include <bond.h> +#include "bond.h" #undef VS #undef BOND_ELEM_T #undef BOND_btype @@ -491,7 +491,7 @@ topn(oid *cands, dbl *d, oid *hcand, dbl #define bond_upper_bound bond_upper_bound_dbl #define bond_search_fast bond_search_fast_dbl #define BONDknn BONDknn_dbl -#include <bond.h> +#include "bond.h" #undef VS #undef BOND_ELEM_T #undef BOND_ELEM_T diff --git a/monetdb5/modules/kernel/vss2.c b/monetdb5/modules/kernel/vss2.c deleted file mode 100644 --- a/monetdb5/modules/kernel/vss2.c +++ /dev/null @@ -1,824 +0,0 @@ -/* - * SPDX-License-Identifier: MPL-2.0 - * - * 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/. - * - * For copyright information, see the file debian/copyright. - */ - -/* - * BOND: Branch-and-bound ON Decomposed data - * - * Vector columns are stored decomposed: each dimension is a separate BAT. - * BOND exploits this layout by computing partial L2 distances dimension - * by dimension, pruning candidates whose partial distance already exceeds - * the current k-th best after each batch of dimensions. - * Dimension ordering: dimensions are processed in decreasing order of - * |query[d] - mean[d]|, so that the most discriminating dimensions come - * first and pruning kicks in early. - */ - - -/* monetdb_config.h must be included as the first include file */ -#include "mal_errors.h" -#include <monetdb_config.h> -#include <gdk.h> - -/* mal_exception.h actually contains everything we need */ -#include <mal.h> -#include <mal_exception.h> -#include <mal_client.h> -#include <mal_arguments.h> - -/* system include files */ -#include <string.h> -#include <stdlib.h> - - -#include <math.h> -#include <stddef.h> -#include <stdint.h> - -#define DIMENSION_MISMATCH_ERR "Dimensions mismatch error" - -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) */ - oid *candidates; - oid *tcand; - oid *tc; - dbl *dists; - dbl *tdist; - dbl *td; - dbl kth_upper; /* kth upper bound */ -} bond_collection; - -/* Helper: comparison function for sorting dimensions by discriminating power */ -typedef struct { - int dim; - dbl score; -} dim_score; - -//static int dbl_cmp(const void *a, const void *b) { -// dbl fa = *(const dbl *)a; -// dbl fb = *(const dbl *)b; -// return (fa > fb) - (fa < fb); -//} - -static int -dim_score_cmp(const void *a, const void *b) -{ - dbl sa = ((const dim_score *)a)->score; - dbl sb = ((const dim_score *)b)->score; - /* descending order */ - if (sa > sb) - return -1; - if (sa < sb) - return 1; - return 0; -} - -/* - * bond_dim_order: return an array of dimension indices sorted by - * |query[d] - mean[d]| descending (most discriminating first). - */ -static int * -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)); - - if (!scores || !order) { - TRC_ERROR(MAL_SERVER, "bond_dim_order: allocation failed\n"); - return NULL; - } - - for (int d = 0; d < bc->ndims; d++) { - scores[d].dim = d; - 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; - - return order; -} - -#define SS 8 -#define VS (2048) -static bond_collection * -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)); - *bc = (bond_collection) { - .ndims = ndims, - .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_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; - } - return bc; - } - } - return NULL; -} - -static inline void -heap_down(oid *hcand, dbl *hd, int p, int k) -{ - 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); - } -} - -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(bond_collection *bc, const dbl *query_vals, BUN k) -{ - oid *cands = bc->candidates, *tc = bc->tc; - dbl *dists = bc->dists, *td = bc->td; - - // calc full distance for the sample across all dimensions - 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 (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; - - 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; - } _______________________________________________ checkin-list mailing list -- [email protected] To unsubscribe send an email to [email protected]
