<commit_before>#include <config.h>
#include <stdlib.h>
#include <math.h>
#include <fdd.h>

#include <runtime.h>
#include <vdom_object.h>

static int fdd_bits=16;
static int cacheratio=64;
static int maxincrease=1000000;
static int minfreenodes=20;

static void vset_fdd_gbchandler(int pre, bddGbcStat *s) {
  if (!pre && RTverbosity>=2) {
    Warning(info,"Garbage collection #%d: %d nodes / %d free / %.1fs / %.1fs total",
	    s->num, s->nodes, s->freenodes,
	    (float)s->time/(float)(CLOCKS_PER_SEC),
	    (float)s->sumtime/(float)CLOCKS_PER_SEC);
  }
}

static void buddy_init(){
	static int initialized=0;
	if (!initialized) {
		bdd_init(1000000, 100000);
		Warning(info,"ratio %d, maximum increase %d, minimum free %d",cacheratio,maxincrease,minfreenodes);
		bdd_setcacheratio(cacheratio);
		bdd_setmaxincrease(maxincrease);
		bdd_setminfreenodes(minfreenodes);
                bdd_gbc_hook(vset_fdd_gbchandler);
		initialized=1;
	}
}

struct poptOption buddy_options[]= {
	{ "cache-ratio",0, POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &cacheratio , 0 , "set cache ratio","<nodes/slot>"},
	{ "max-increase" , 0 , POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &maxincrease , 0 , "set maximum increase","<number>"},
	{ "min-free-nodes", 0 , POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &minfreenodes , 0 , "set minimum free node percentage","<percentage>"},
	{ "fdd-bits" , 0 , POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &fdd_bits , 0 , "set the number of bits for each fdd variable","<number>"},
	POPT_TABLEEND
};

struct vector_domain {
	struct vector_domain_shared shared;
	//BDD **vals;
	int *vars;
	BDD varset;
	int *vars2;
	int *proj;
};

static BDD mkvar(vdom_t dom,int idx,int val){
  	return bdd_addref(fdd_ithvar(dom->vars[idx],val));
}
static void rmvar(BDD var) {
  	bdd_delref(var);
}
static BDD mkvar2(vdom_t dom,int idx,int val){
  	return bdd_addref(fdd_ithvar(dom->vars2[idx],val));
}
static void rmvar2(BDD var) {
  	bdd_delref(var);
}

struct vector_set {
	vdom_t dom;
	BDD bdd;
	BDD p_set; // variables in the projection.
	BDD c_set; // variables in the complement.
	int p_len;
	int* proj;
};

struct vector_relation {
	vdom_t dom;
	BDD bdd;
	BDD p_set; // variables in the projection.
	BDD p_prime_set; // primed variables in the projection
	int p_len;
	int* proj;
	BDD rel_set; // variables + primed variables in the projection.
	bddPair *pairs;
	bddPair *inv_pairs;
};

static vset_t set_create_fdd(vdom_t dom,int k,int* proj){
	vset_t set=(vset_t)RTmalloc(sizeof(struct vector_set));
	set->dom=dom;
	set->bdd=bddfalse;
	if (k && k<dom->shared.size) {
		int vars[k];
		int complement[dom->shared.size-k];
		set->p_len=k;
		set->proj=(int*)RTmalloc(k*sizeof(int));
		int i=0;
		int j=0;
		for(int v=0;v<dom->shared.size;v++){
			if (v==proj[i] && i<k) {
				// influenced
				vars[i]=dom->vars[v];
				set->proj[i]=v;
				i++;
			} else {
				// not influenced;
				complement[j]=dom->vars[v];
				j++;
			}
		}
		set->p_set=bdd_addref(fdd_makeset(vars,k));
		set->c_set=bdd_addref(fdd_makeset(complement,dom->shared.size-k));
	} else {
		set->p_len=dom->shared.size;
		set->p_set=dom->varset;
		set->c_set=bddtrue;
		set->proj=dom->proj;
	}
	return set;
}

static vrel_t rel_create_fdd(vdom_t dom,int k,int* proj){
    vrel_t rel=(vrel_t)RTmalloc(sizeof(struct vector_relation));
    rel->dom=dom;
    rel->bdd=bddfalse;
    if (k && k<dom->shared.size) {
        int vars[k];
        int vars2[k];
        int allvars[2*k];
        rel->p_len=k;
        rel->proj=(int*)RTmalloc(k*sizeof(int));
        for(int i=0;i<k;i++) {
            rel->proj[i]=proj[i];
            vars[i] = dom->vars[proj[i]];
            vars2[i]= dom->vars2[proj[i]];
            allvars[2*i]=vars[i];
            allvars[2*i+1]=vars2[i]; // hidden assumption on encoding
        }
        // for next function
        rel->pairs=bdd_newpair();
        int res=fdd_setpairs(rel->pairs,vars2,vars,k);
        if (res<0){
            Fatal(1,error,"BuDDy error: %s",bdd_errstring(res));
        }
        // for prev function
        rel->inv_pairs=bdd_newpair();
        res=fdd_setpairs(rel->inv_pairs,vars,vars2,k);
        if (res<0){
            Fatal(1,error,"BuDDy error: %s",bdd_errstring(res));
        }
        rel->p_set=bdd_addref(fdd_makeset(vars,k));
        rel->rel_set=bdd_addref(fdd_makeset(allvars,2*k));
        rel->p_prime_set=bdd_addref(bdd_replace(rel->p_set, rel->inv_pairs));
	} else {
        // TODO: why is this in here? is this correct?
        fprintf(stderr,"NEVER\n"); abort();
        rel->p_len=dom->shared.size;
        rel->p_set=dom->varset;
        rel->proj=dom->proj;
	}
	return rel;
}

static inline BDD fdd_element(vset_t set,const int* e){
	int N=set->p_len;
	BDD bdd=bddtrue;
	//for(int i=0;i<N;i++){
	  for(int i=N-1;i>=0;i--){
		BDD val=mkvar(set->dom,set->proj[i],e[i]);
		BDD tmp=bdd;
		bdd=bdd_addref(bdd_and(bdd,val));
		bdd_delref(tmp);
		rmvar(val);
	}
	//printf("element: ");
	//fdd_printset(bdd);
	//printf("\n");
	return bdd;
}

static BDD fdd_pair(vrel_t rel,const int* e1,const int*e2){
//	Warning(info,"args %x %x %x",rel,e1,e2);
	int N=rel->p_len;
//	Warning(info,"N: %d %d",N,rel->p_len);
	BDD bdd=bddtrue;
	//for(int i=0;i<N;i++){
	  for(int i=N-1;i>=0;i--){<commit_msg>Fixed vset fdd init bug when k=#cols of matrix<commit_after>#include <config.h>
#include <stdlib.h>
#include <math.h>
#include <fdd.h>
#include <assert.h>

#include <runtime.h>
#include <vdom_object.h>

static int fdd_bits=16;
static int cacheratio=64;
static int maxincrease=1000000;
static int minfreenodes=20;

static void vset_fdd_gbchandler(int pre, bddGbcStat *s) {
  if (!pre && RTverbosity>=2) {
    Warning(info,"Garbage collection #%d: %d nodes / %d free / %.1fs / %.1fs total",
	    s->num, s->nodes, s->freenodes,
	    (float)s->time/(float)(CLOCKS_PER_SEC),
	    (float)s->sumtime/(float)CLOCKS_PER_SEC);
  }
}

static void buddy_init(){
	static int initialized=0;
	if (!initialized) {
		bdd_init(1000000, 100000);
		Warning(info,"ratio %d, maximum increase %d, minimum free %d",cacheratio,maxincrease,minfreenodes);
		bdd_setcacheratio(cacheratio);
		bdd_setmaxincrease(maxincrease);
		bdd_setminfreenodes(minfreenodes);
                bdd_gbc_hook(vset_fdd_gbchandler);
		initialized=1;
	}
}

struct poptOption buddy_options[]= {
	{ "cache-ratio",0, POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &cacheratio , 0 , "set cache ratio","<nodes/slot>"},
	{ "max-increase" , 0 , POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &maxincrease , 0 , "set maximum increase","<number>"},
	{ "min-free-nodes", 0 , POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &minfreenodes , 0 , "set minimum free node percentage","<percentage>"},
	{ "fdd-bits" , 0 , POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &fdd_bits , 0 , "set the number of bits for each fdd variable","<number>"},
	POPT_TABLEEND
};

struct vector_domain {
	struct vector_domain_shared shared;
	//BDD **vals;
	int *vars;
	BDD varset;
	int *vars2;
	int *proj;
};

static BDD mkvar(vdom_t dom,int idx,int val){
  	return bdd_addref(fdd_ithvar(dom->vars[idx],val));
}
static void rmvar(BDD var) {
  	bdd_delref(var);
}
static BDD mkvar2(vdom_t dom,int idx,int val){
  	return bdd_addref(fdd_ithvar(dom->vars2[idx],val));
}
static void rmvar2(BDD var) {
  	bdd_delref(var);
}

struct vector_set {
	vdom_t dom;
	BDD bdd;
	BDD p_set; // variables in the projection.
	BDD c_set; // variables in the complement.
	int p_len;
	int* proj;
};

struct vector_relation {
	vdom_t dom;
	BDD bdd;
	BDD p_set; // variables in the projection.
	BDD p_prime_set; // primed variables in the projection
	int p_len;
	int* proj;
	BDD rel_set; // variables + primed variables in the projection.
	bddPair *pairs;
	bddPair *inv_pairs;
};

static vset_t set_create_fdd(vdom_t dom,int k,int* proj){
	vset_t set=(vset_t)RTmalloc(sizeof(struct vector_set));
	set->dom=dom;
	set->bdd=bddfalse;
	if (k && k<dom->shared.size) {
		int vars[k];
		int complement[dom->shared.size-k];
		set->p_len=k;
		set->proj=(int*)RTmalloc(k*sizeof(int));
		int i=0;
		int j=0;
		for(int v=0;v<dom->shared.size;v++){
			if (v==proj[i] && i<k) {
				// influenced
				vars[i]=dom->vars[v];
				set->proj[i]=v;
				i++;
			} else {
				// not influenced;
				complement[j]=dom->vars[v];
				j++;
			}
		}
		set->p_set=bdd_addref(fdd_makeset(vars,k));
		set->c_set=bdd_addref(fdd_makeset(complement,dom->shared.size-k));
	} else {
		set->p_len=dom->shared.size;
		set->p_set=dom->varset;
		set->c_set=bddtrue;
		set->proj=dom->proj;
	}
	return set;
}

static vrel_t rel_create_fdd(vdom_t dom,int k,int* proj){
    vrel_t rel=(vrel_t)RTmalloc(sizeof(struct vector_relation));
    rel->dom=dom;
    rel->bdd=bddfalse;

    assert (k > 0 && k<=dom->shared.size);

    int vars[k];
    int vars2[k];
    int allvars[2*k];
    rel->p_len=k;
    if (k==dom->shared.size) {
        rel->proj = dom->proj;
    } else {
        rel->proj=(int*)RTmalloc(k*sizeof(int));
    }
    for(int i=0;i<k;i++) {
        if (k!=dom->shared.size) rel->proj[i]=proj[i];
        vars[i] = dom->vars[proj[i]];
        vars2[i]= dom->vars2[proj[i]];
        allvars[2*i]=vars[i];
        allvars[2*i+1]=vars2[i]; // hidden assumption on encoding
    }
    // for next function
    rel->pairs=bdd_newpair();
    int res=fdd_setpairs(rel->pairs,vars2,vars,k);
    if (res<0){
        Fatal(1,error,"BuDDy error: %s",bdd_errstring(res));
    }
    // for prev function
    rel->inv_pairs=bdd_newpair();
    res=fdd_setpairs(rel->inv_pairs,vars,vars2,k);
    if (res<0){
        Fatal(1,error,"BuDDy error: %s",bdd_errstring(res));
    }
    if (k==dom->shared.size) {
        rel->p_set = dom->varset;
    } else {
        rel->p_set=bdd_addref(fdd_makeset(vars,k));
    }
    rel->rel_set=bdd_addref(fdd_makeset(allvars,2*k));
    rel->p_prime_set=bdd_addref(bdd_replace(rel->p_set, rel->inv_pairs));
	return rel;
}

static inline BDD fdd_element(vset_t set,const int* e){
	int N=set->p_len;
	BDD bdd=bddtrue;
	//for(int i=0;i<N;i++){
	  for(int i=N-1;i>=0;i--){
		BDD val=mkvar(set->dom,set->proj[i],e[i]);
		BDD tmp=bdd;
		bdd=bdd_addref(bdd_and(bdd,val));
		bdd_delref(tmp);
		rmvar(val);
	}
	//printf("element: ");
	//fdd_printset(bdd);
	//printf("\n");
	return bdd;
}

static BDD fdd_pair(vrel_t rel,const int* e1,const int*e2){
//	Warning(info,"args %x %x %x",rel,e1,e2);
	int N=rel->p_len;
//	Warning(info,"N: %d %d",N,rel->p_len);
	BDD bdd=bddtrue;
	//for(int i=0;i<N;i++){
	  for(int i=N-1;i>=0;i--){
{'fraction_non_alphanumeric': 0.17428984111699566, 'fraction_numerical': 0.013962445835339432, 'mean_word_length': 3.880639097744361, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 142, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5127805', 'n_tokens_mistral': 4648, 'n_tokens_neox': 4430, 'n_words': 777}