static void rel_load(FILE* f, vrel_t rel) { if (rel->bdd) sylvan_deref(rel->bdd); if (rel->vec_to_bddvar) RTfree(rel->vec_to_bddvar); if (rel->prime_vec_to_bddvar) RTfree(rel->prime_vec_to_bddvar); if (rel->variables) sylvan_deref(rel->variables); if (rel->prime_variables) sylvan_deref(rel->prime_variables); if (rel->all_variables) sylvan_deref(rel->all_variables); sylvan_serialize_fromfile(f); size_t bdd; fread(&bdd, sizeof(size_t), 1, f); rel->bdd = sylvan_ref(sylvan_serialize_get_reversed(bdd)); fread(&rel->vector_size, sizeof(size_t), 1, f); rel->vec_to_bddvar = (BDDVAR*)RTmalloc(sizeof(BDDVAR) * fddbits * rel->vector_size); rel->prime_vec_to_bddvar = (BDDVAR*)RTmalloc(sizeof(BDDVAR) * fddbits * rel->vector_size); fread(rel->vec_to_bddvar, sizeof(BDDVAR), rel->vector_size*fddbits, f); fread(rel->prime_vec_to_bddvar, sizeof(BDDVAR), rel->vector_size*fddbits, f); sylvan_gc_disable(); LACE_ME; rel->variables = sylvan_ref(sylvan_set_fromarray(rel->vec_to_bddvar, fddbits * rel->vector_size)); rel->prime_variables = sylvan_ref(sylvan_set_fromarray(rel->prime_vec_to_bddvar, fddbits * rel->vector_size)); rel->all_variables = sylvan_ref(sylvan_set_addall(rel->prime_variables, rel->variables)); sylvan_gc_enable(); }
/** * This means: create a new BDD set in the domain dom * dom = my domain (just copy) * k is the number of integers in proj * proj is a list of indices of the state vector in the projection */ static vset_t set_create(vdom_t dom, int k, int* proj) { vset_t set = (vset_t)RTmalloc(sizeof(struct vector_set)); LACE_ME; set->dom = dom; set->bdd = sylvan_false; // Initialize with an empty BDD if (k>=0 && k<dom->shared.size) { // We are creating a projection set->vector_size = k; set->vec_to_bddvar = (BDDVAR*)RTmalloc(sizeof(BDDVAR) * fddbits * set->vector_size); for (int i=0; i<k; i++) { for (int j=0; j<fddbits; j++) { set->vec_to_bddvar[i*fddbits + j] = dom->vec_to_bddvar[proj[i]*fddbits + j]; } } } else { // Use all variables set->vector_size = dom->shared.size; set->vec_to_bddvar = (BDDVAR*)RTmalloc(sizeof(BDDVAR) * fddbits * set->vector_size); memcpy(set->vec_to_bddvar, dom->vec_to_bddvar, sizeof(BDDVAR) * fddbits * set->vector_size); } sylvan_gc_disable(); set->variables = sylvan_ref(sylvan_set_fromarray(set->vec_to_bddvar, fddbits * set->vector_size)); set->projection = sylvan_ref(sylvan_set_removeall(dom->universe, set->variables)); sylvan_gc_enable(); return set; }
static void set_copy_match(vset_t dst, vset_t src, int p_len, int* proj, int*match) { LACE_ME; // Create bdd of "match" // Assumption: proj is ordered (if not, you get bad performance) sylvan_deref(dst->bdd); sylvan_gc_disable(); BDD match_bdd = sylvan_true; for (int i=p_len;i-->0;) { uint32_t b = match[i]; for (int j=fddbits;j-->0;) { BDD val = sylvan_ithvar(src->dom->vec_to_bddvar[proj[i]*fddbits+j]); if (!(b&1)) val = sylvan_not(val); match_bdd = sylvan_and(val, match_bdd); b >>= 1; } } sylvan_ref(match_bdd); sylvan_gc_enable(); dst->bdd = sylvan_ref(sylvan_and(match_bdd, src->bdd)); sylvan_deref(match_bdd); }
/** * Enumerate all states that match partial state <match> * <match> is p_len long * <proj> is a list of integers, containing indices of each match integer */ static void set_enum_match(vset_t set, int p_len, int* proj, int* match, vset_element_cb cb, void* context) { LACE_ME; // Create bdd of "match" // Assumption: proj is ordered (if not, you get bad performance) sylvan_gc_disable(); BDD match_bdd = sylvan_true; for (int i=p_len;i-->0;) { uint32_t b = match[i]; for (int j=fddbits;j-->0;) { BDD val = sylvan_ithvar(set->dom->vec_to_bddvar[proj[i]*fddbits+j]); if (!(b&1)) val = sylvan_not(val); match_bdd = sylvan_and(val, match_bdd); b >>= 1; } } sylvan_ref(match_bdd); sylvan_gc_enable(); BDD old = match_bdd; match_bdd = sylvan_ref(sylvan_and(match_bdd, set->bdd)); sylvan_deref(old); int vec[set->vector_size]; memset(vec, 0, sizeof(int)*set->vector_size); set_enum_do(match_bdd, set->variables, vec, 0, cb, context); sylvan_deref(match_bdd); }
/** * Create a "relation" (Dom x Dom) * 0 < k <= dom->shared.size * The integers in proj, a k-length array, are indices of the * state vector. */ static vrel_t rel_create(vdom_t dom, int k, int* proj) { LACE_ME; sylvan_gc_disable(); vrel_t rel = (vrel_t)RTmalloc(sizeof(struct vector_relation)); rel->dom = dom; rel->bdd = sylvan_false; // Initially, empty. // Relations are always projections. assert (k >= 0 && k<=dom->shared.size); rel->vector_size = k; rel->vec_to_bddvar = (BDDVAR*)RTmalloc(sizeof(BDDVAR) * fddbits * rel->vector_size); rel->prime_vec_to_bddvar = (BDDVAR*)RTmalloc(sizeof(BDDVAR) * fddbits * rel->vector_size); for (int i=0; i<k; i++) { for (int j=0; j<fddbits; j++) { rel->vec_to_bddvar[i*fddbits + j] = dom->vec_to_bddvar[proj[i]*fddbits + j]; rel->prime_vec_to_bddvar[i*fddbits + j] = dom->prime_vec_to_bddvar[proj[i]*fddbits + j]; } } sylvan_gc_disable(); rel->variables = sylvan_ref(sylvan_set_fromarray(rel->vec_to_bddvar, fddbits * rel->vector_size)); rel->prime_variables = sylvan_ref(sylvan_set_fromarray(rel->prime_vec_to_bddvar, fddbits * rel->vector_size)); rel->all_variables = sylvan_ref(sylvan_set_addall(rel->prime_variables, rel->variables)); sylvan_gc_enable(); return rel; }
JNIEXPORT void JNICALL Java_jsylvan_JSylvan_enableGC(JNIEnv *env, jclass cl) { sylvan_gc_enable(); return; (void)env; (void)cl; }
static void init_universe(vdom_t dom) { LACE_ME; int n = dom->shared.size; sylvan_gc_disable(); dom->universe = sylvan_ref(sylvan_set_fromarray(dom->vec_to_bddvar, fddbits * n)); dom->prime_universe = sylvan_ref(sylvan_set_fromarray(dom->prime_vec_to_bddvar, fddbits * n)); sylvan_gc_enable(); }
static vset_t set_load(FILE* f, vdom_t dom) { vset_t set = (vset_t)RTmalloc(sizeof(struct vector_set)); set->dom = dom; sylvan_serialize_fromfile(f); size_t bdd; fread(&bdd, sizeof(size_t), 1, f); set->bdd = sylvan_ref(sylvan_serialize_get_reversed(bdd)); fread(&set->vector_size, sizeof(size_t), 1, f); set->vec_to_bddvar = (BDDVAR*)RTmalloc(sizeof(BDDVAR) * fddbits * set->vector_size); fread(set->vec_to_bddvar, sizeof(BDDVAR), fddbits * set->vector_size, f); LACE_ME; sylvan_gc_disable(); set->variables = sylvan_ref(sylvan_set_fromarray(set->vec_to_bddvar, fddbits * set->vector_size)); set->projection = sylvan_ref(sylvan_set_removeall(dom->universe, set->variables)); sylvan_gc_enable(); return set; }