From 6cd778f70bc36a964db17f9b546caaab3cdbd736 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Wed, 9 Jan 2013 09:21:07 +0000 Subject: [PATCH] exo stuff --- C/absmi.c | 7 ++-- C/adtdefs.c | 4 +-- C/exo.c | 96 ++++++++++++++++++++++++++++----------------------- C/grow.c | 2 +- H/Yatom.h | 1 + H/dlocals.h | 3 ++ H/hlocals.h | 2 ++ H/ilocals.h | 2 ++ H/rlocals.h | 2 ++ misc/LOCALS | 3 ++ pl/dbload.yap | 12 +++---- 11 files changed, 78 insertions(+), 56 deletions(-) diff --git a/C/absmi.c b/C/absmi.c index c625e0333..392e5bee3 100755 --- a/C/absmi.c +++ b/C/absmi.c @@ -3668,8 +3668,9 @@ Yap_absmi(int inp) SREG++; BEGP(pt0); - if (IsVarTerm(d0)) - goto gatom_exo_var; + deref_head(d0, gatom_exo_unk); + /* argument is nonvar */ + gatom_exo_nonvar: if (d0 == d1) { PREG = NEXTOP(PREG, x); GONext(); @@ -3678,7 +3679,7 @@ Yap_absmi(int inp) FAIL(); } - gatom_exo_var: + deref_body(d0, pt0, gatom_exo_unk, gatom_exo_nonvar); /* argument is a variable */ pt0 = (CELL *)d0; PREG = NEXTOP(PREG, x); diff --git a/C/adtdefs.c b/C/adtdefs.c index 922001d91..e05a898a3 100755 --- a/C/adtdefs.c +++ b/C/adtdefs.c @@ -264,8 +264,8 @@ LookupWideAtom(wchar_t *atom) wae->NextOfPE = NIL; wae->KindOfPE = WideAtomProperty; wae->SizeOfAtom = sz; - if (ae->StrOfAE != (char *)atom) - wcscpy((wchar_t *)(ae->StrOfAE), atom); + if (ae->WStrOfAE != atom) + wcscpy(ae->WStrOfAE, atom); NOfAtoms++; ae->NextOfAE = a; WideHashChain[hash].Entry = na; diff --git a/C/exo.c b/C/exo.c index e83ab5fd0..4147e68de 100644 --- a/C/exo.c +++ b/C/exo.c @@ -36,21 +36,25 @@ #define MAX_ARITY 256 + /* Simple hash function */ static UInt -HASH(UInt j, CELL *cl, struct index_t *it) +HASH(UInt hash0, UInt j, CELL *cl, struct index_t *it) { - return ((cl[j] >> 3) + (3*j*it->nels)/2) % (it->nels*2); + Term t = cl[j]; + if (IsIntTerm(t)) + return (IntOfTerm(t) *257 + hash0 ) % (it->nels*2); + return ((t >> 3) *257 + hash0 ) % (it->nels*2); } /* search for matching elements */ static int -MATCH(CELL *clp, CELL *kvp, UInt j, UInt bnds[], struct index_t *it) +MATCH(CELL *clp, CELL *kvp, UInt j, struct index_t *it) { if ((kvp - it->cls)%it->arity != j) return FALSE; do { - if ( bnds[j] && *clp != *kvp) + if ( LOCAL_ibnds[j] && *clp != *kvp) return FALSE; clp--; kvp--; @@ -78,7 +82,7 @@ ADD_TO_TRY_CHAIN(CELL *kvp, CELL *cl, struct index_t *it) static UInt NEXT(UInt hash, struct index_t *it, UInt j) { - return (hash+3) % (it->nels*2); + return (hash+hash/3+j*257+1) % (it->nels*2); } /* This is the critical routine, it builds the hash table * @@ -99,19 +103,20 @@ NEXT(UInt hash, struct index_t *it, UInt j) * else */ static void -INSERT(CELL *cl, struct index_t *it, UInt arity, UInt base, UInt bnds[]) +INSERT(CELL *cl, struct index_t *it, UInt arity, UInt base, UInt hash0) { UInt j = base; CELL *kvp; UInt hash; /* skip over argument */ - while (!bnds[j]) { + while (!LOCAL_ibnds[j]) { j++; } /* j is the firs bound element */ /* check if we match */ - hash = HASH(j, cl, it); + hash = hash0 = HASH(hash0, j, cl, it); + // printf("h=%ld j=%ld %lx\n", hash, j, cl[j]); next: /* loop to insert element */ kvp = it->key[hash]; @@ -119,56 +124,53 @@ INSERT(CELL *cl, struct index_t *it, UInt arity, UInt base, UInt bnds[]) /* simple case, new entry */ it->key[hash] = cl+j; return; - } else if (MATCH(cl+j, kvp, j, bnds, it)) { + } else if (MATCH(cl+j, kvp, j, it)) { /* collision */ UInt k; CELL *target; for (k =j+1, target = kvp+1; k < arity; k++,target++ ) { - if (bnds[k]) { + if (LOCAL_ibnds[k]) { if (*target != cl[k]) { /* found a new forking point */ - INSERT(cl, it, arity, k, bnds); + INSERT(cl, it, arity, k, hash0); return; } } } + it->ncols++; ADD_TO_TRY_CHAIN(kvp, cl, it); return; } else { hash = NEXT(hash, it, j); + // printf("N=%ld\n", hash); goto next; } } static yamop * -LOOKUP(struct index_t *it, UInt arity, UInt bnds[]) +LOOKUP(struct index_t *it, UInt arity, UInt j) { - UInt j = 0; CELL *kvp; - UInt hash; + UInt hash, hash0 = 0; - /* skip over argument */ - while (!bnds[j]) { - j++; - } /* j is the firs bound element */ /* check if we match */ hash: - hash = HASH(j, XREGS+1, it); + hash = hash0 = HASH(hash0, j, XREGS+1, it); next: /* loop to insert element */ kvp = it->key[hash]; if (kvp == NULL) { /* simple case, no element */ return FAILCODE; - } else if (MATCH(XREGS+(j+1), kvp, j, bnds, it)) { + } else if (MATCH(XREGS+(j+1), kvp, j, it)) { /* found element */ UInt k; CELL *target; for (k =j+1, target = kvp+1; k < arity; k++ ) { - if (bnds[k]) { + if (LOCAL_ibnds[k]) { if (*target != XREGS[k+1]) { j = k; goto hash; @@ -177,7 +179,7 @@ LOOKUP(struct index_t *it, UInt arity, UInt bnds[]) target++; } S = target-arity; - if (it->links[(S-it->cls)/arity]) + if (!it->is_key && it->links[(S-it->cls)/arity]) return it->code; else return NEXTOP(NEXTOP(it->code,lp),lp); @@ -189,14 +191,14 @@ LOOKUP(struct index_t *it, UInt arity, UInt bnds[]) } static void -fill_hash(UInt bmap, UInt bnds[], struct index_t *it) +fill_hash(UInt bmap, struct index_t *it) { UInt i; UInt arity = it->arity; CELL *cl = it->cls; for (i=0; i < it->nels; i++) { - INSERT(cl, it, arity, 0, bnds); + INSERT(cl, it, arity, 0, 0); cl += arity; } for (i=0; i < it->nels*2; i++) { @@ -213,7 +215,7 @@ fill_hash(UInt bmap, UInt bnds[], struct index_t *it) } static struct index_t * -add_index(struct index_t **ip, UInt bmap, UInt bndsf[], PredEntry *ap, UInt count) +add_index(struct index_t **ip, UInt bmap, PredEntry *ap, UInt count) { UInt ncls = ap->cs.p_code.NOfClauses, j; CELL *base = NULL; @@ -221,16 +223,6 @@ add_index(struct index_t **ip, UInt bmap, UInt bndsf[], PredEntry *ap, UInt coun size_t sz; yamop *ptr; - if (count) { - if (!(base = (CELL *)Yap_AllocCodeSpace(3*sizeof(CELL)*ncls))) { - CACHE_REGS - save_machine_regs(); - LOCAL_Error_Size = 3*ncls*sizeof(CELL); - LOCAL_ErrorMessage = "not enough space to index"; - Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage); - return NULL; - } - } sz = (CELL)NEXTOP(NEXTOP((yamop*)NULL,lp),lp)+ap->ArityOfPE*(CELL)NEXTOP((yamop *)NULL,x) +(CELL)NEXTOP(NEXTOP((yamop *)NULL,p),l); if (!(i = (struct index_t *)Yap_AllocCodeSpace(sizeof(struct index_t)+sz))) { CACHE_REGS @@ -240,8 +232,17 @@ add_index(struct index_t **ip, UInt bmap, UInt bndsf[], PredEntry *ap, UInt coun Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage); return NULL; } - if (count) + if (count) { + if (!(base = (CELL *)Yap_AllocCodeSpace(3*sizeof(CELL)*ncls))) { + CACHE_REGS + save_machine_regs(); + LOCAL_Error_Size = 3*ncls*sizeof(CELL); + LOCAL_ErrorMessage = "not enough space to index"; + Yap_Error(OUT_OF_HEAP_ERROR, TermNil, LOCAL_ErrorMessage); + return NULL; + } bzero(base, 3*sizeof(CELL)*ncls); + } i->next = *ip; i->prev = NULL; i->nels = ncls; @@ -252,10 +253,17 @@ add_index(struct index_t **ip, UInt bmap, UInt bndsf[], PredEntry *ap, UInt coun i->hsize = 2*ncls; i->key = (CELL **)base; i->links = (CELL *)(base+2*ncls); + i->hsize = 2*ncls; + i->ncols = 0; i->cls = (CELL *)((ADDR)ap->cs.p_code.FirstClause+2*sizeof(struct index_t *)); *ip = i; if (count) { - fill_hash(bmap, bndsf, i); + fill_hash(bmap, i); + if (!i->ncols) { + i->is_key = TRUE; + if (base != realloc(base, 2*sizeof(CELL)*ncls)) + return FALSE; + } } ptr = (yamop *)(i+1); i->code = ptr; @@ -294,19 +302,19 @@ yamop * Yap_ExoLookup(PredEntry *ap) { UInt arity = ap->ArityOfPE; - UInt bmap = 0L, bit = 1, count = 0, j; + UInt bmap = 0L, bit = 1, count = 0, j, j0 = 0; struct index_t **ip = (struct index_t **)(ap->cs.p_code.FirstClause); struct index_t *i = *ip; - UInt bnds[MAX_ARITY]; for (j=0; j< arity; j++, bit<<=1) { Term t = Deref(XREGS[j+1]); if (!IsVarTerm(t)) { bmap += bit; - bnds[j] = TRUE; + LOCAL_ibnds[j] = TRUE; + if (!count) j0= j; count++; } else { - bnds[j] = FALSE; + LOCAL_ibnds[j] = FALSE; } XREGS[j+1] = t; } @@ -325,10 +333,10 @@ Yap_ExoLookup(PredEntry *ap) i = i->next; } if (!i) { - i = add_index(ip, bmap, bnds, ap, count); + i = add_index(ip, bmap, ap, count); } if (count) - return LOOKUP(i, arity, bnds); + return LOOKUP(i, arity, j0); else return i->code; } @@ -391,7 +399,7 @@ p_exodb_get_space( USES_REGS1 ) return FALSE; } - required = ncls*sizeof(CELL)+sizeof(MegaClause)+2*sizeof(struct index_t *); + required = ncls*arity*sizeof(CELL)+sizeof(MegaClause)+2*sizeof(struct index_t *); while (!(mcl = (MegaClause *)Yap_AllocCodeSpace(required))) { if (!Yap_growheap(FALSE, required, NULL)) { /* just fail, the system will keep on going */ diff --git a/C/grow.c b/C/grow.c index eb53ba5ce..8145b3765 100644 --- a/C/grow.c +++ b/C/grow.c @@ -1318,7 +1318,7 @@ init_new_table(AtomHashEntry *ntb, UInt nsize) { UInt i; - for (i = 0; i < nsize; ++i) { + for (i = 0; i < nsize; i++) { INIT_RWLOCK(ntb[i].AERWLock); ntb[i].Entry = NIL; } diff --git a/H/Yatom.h b/H/Yatom.h index a9227091f..f4546bcad 100644 --- a/H/Yatom.h +++ b/H/Yatom.h @@ -806,6 +806,7 @@ IsPredProperty (int flags) /* There are several flags for code and data base entries */ typedef enum { + ExoMask = 0x1000000, /* is exo code */ FuncSwitchMask = 0x800000, /* is a switch of functors */ HasDBTMask = 0x400000, /* includes a pointer to a DBTerm */ MegaMask = 0x200000, /* mega clause */ diff --git a/H/dlocals.h b/H/dlocals.h index fe264da61..222fa65bd 100644 --- a/H/dlocals.h +++ b/H/dlocals.h @@ -430,3 +430,6 @@ #define LOCAL_FunctorVar LOCAL->FunctorVar_ #define REMOTE_FunctorVar(wid) REMOTE(wid)->FunctorVar_ +#define LOCAL_ibnds LOCAL->ibnds_ +#define REMOTE_ibnds(wid) REMOTE(wid)->ibnds_ + diff --git a/H/hlocals.h b/H/hlocals.h index ee4d4179d..842454563 100644 --- a/H/hlocals.h +++ b/H/hlocals.h @@ -241,4 +241,6 @@ typedef struct worker_local { FILE* FPreds_; #endif /* LOW_PROF */ Functor FunctorVar_; + + UInt ibnds_[256]; } w_local; diff --git a/H/ilocals.h b/H/ilocals.h index 5fd0d935a..4e564a40a 100644 --- a/H/ilocals.h +++ b/H/ilocals.h @@ -241,4 +241,6 @@ static void InitWorker(int wid) { REMOTE_FPreds(wid) = NULL; #endif /* LOW_PROF */ REMOTE_FunctorVar(wid) = FunctorVar; + + } diff --git a/H/rlocals.h b/H/rlocals.h index 90a66d373..652d98e32 100644 --- a/H/rlocals.h +++ b/H/rlocals.h @@ -241,4 +241,6 @@ static void RestoreWorker(int wid USES_REGS) { #endif /* LOW_PROF */ + + } diff --git a/misc/LOCALS b/misc/LOCALS index f47243e85..0ffdb50cd 100644 --- a/misc/LOCALS +++ b/misc/LOCALS @@ -271,4 +271,7 @@ FILE* FPreds =NULL Functor FunctorVar =FunctorVar +// exo indexing +UInt ibnds[256] void + END_WORKER_LOCAL diff --git a/pl/dbload.yap b/pl/dbload.yap index edd6c81f1..e97181f52 100644 --- a/pl/dbload.yap +++ b/pl/dbload.yap @@ -52,7 +52,7 @@ do_dbload(F0, M0, G) :- check_dbload_stream(R, M0) :- repeat, - read(R,T), + catch(read(R,T), _, fail), ( T = end_of_file -> !; dbload_count(T, M0), fail @@ -79,7 +79,7 @@ get_module(T,M,T,M). load_facts :- - yap_flag(exo_compilation, on), !. + !, % yap_flag(exo_compilation, on), !. load_exofacts. load_facts :- retract(dbloading(Na,Arity,M,T,NaAr,_)), @@ -98,7 +98,7 @@ load_facts. dbload_add_facts(R, M) :- repeat, - read(R,T), + catch(read(R,T), _, fail), ( T = end_of_file -> !; dbload_add_fact(T, M), fail @@ -120,17 +120,17 @@ load_exofacts :- assertz(dbloading(Na,Arity,M,T,NaAr,Handle)), nb_setval(NaAr,0), fail. -load_rxofacts :- +load_exofacts :- dbprocess(F, M), open(F, read, R), exodb_add_facts(R, M), close(R), fail. -load_facts. +load_exofacts. exodb_add_facts(R, M) :- repeat, - read(R,T), + catch(read(R,T), _, fail), ( T = end_of_file -> !; exodb_add_fact(T, M), fail