void optimize_arity(int **arity, int *nb_levels,int n) { int a,i; int *new_arity = NULL; if( n < 0 ) return; /* printf("n=%d\tnb_levels=%d\n",n,*nb_levels); */ /* for(i=0;i<*nb_levels;i++) */ /* printf("%d:",(*arity)[i]); */ /* printf("\n"); */ /* if(n==(*nb_levels)-3) */ /* exit(-1); */ a = (*arity)[n]; if( (a%3 == 0) && (a > 3) ){ /* check if the a rity of level n devides 3 If this is the case: Add a level */ (*nb_levels)++; /* Build a new arity array */ new_arity = (int*)MALLOC(sizeof(int)*(*nb_levels)); /* Copy the begining if the old array */ for( i = 0 ; i < n ; i++) new_arity[i] = (*arity)[i]; /* set the nth level to arity 3 */ new_arity[n] = 3; /* printf("a=%d\n",a); */ /* Set the (n+1) level to arity a/3 */ new_arity[n+1] = a/3; /* Copy the end of the array */ for( i = n+2 ; i < *nb_levels ; i++) new_arity[i] = (*arity)[i-1]; FREE(*arity); /* if a/3 =3 then go to the next level */ if(new_arity[n+1] == 3) optimize_arity(&new_arity,nb_levels,n); else /* continue to this level (remember we just add a new level */ optimize_arity(&new_arity,nb_levels,n+1); *arity=new_arity; }else if( (a%2==0) && (a>2) ){/* same as above but for arity == 2 instead of 3 */ (*nb_levels)++; new_arity = (int*)MALLOC(sizeof(int)*(*nb_levels)); for( i = 0 ; i < n ; i++ ) new_arity[i] = (*arity)[i]; new_arity[n] = 2; /* printf("a=%d\n",a); */ new_arity[n+1] = a/2; for( i = n+2 ; i < *nb_levels ; i++ ) new_arity[i] = (*arity)[i-1]; FREE(*arity); if(new_arity[n+1] == 2) optimize_arity(&new_arity,nb_levels,n); else optimize_arity(&new_arity,nb_levels,n+1); *arity = new_arity; }else /* if nothing works go to next level. */ optimize_arity(arity,nb_levels,n-1); }
tm_topology_t *optimize_topology(tm_topology_t *topology){ int *arity = NULL,nb_levels; int *numbering = NULL,nb_nodes; tm_topology_t *new_topo; topology_arity(topology,&arity,&nb_levels); /* printf("nb_levels=%d\n",nb_levels); */ /* for(i=0;i<nb_levels;i++) */ /* printf("%d:",arity[i]); */ /* printf("\n"); */ topology_numbering(topology,&numbering,&nb_nodes); /* printf("nb_nodes=%d\n",nb_nodes); */ /* for(i=0;i<nb_nodes;i++) */ /* printf("%d,",numbering[i]); */ /* printf("\n"); */ optimize_arity(&arity,&nb_levels,nb_levels-2); new_topo = build_synthetic_topology(arity,nb_levels,numbering,nb_nodes); /* display_topology(*topology); */ FREE(arity); FREE(numbering); return new_topo; /* exit(-1); */ }
void tm_optimize_topology(tm_topology_t **topology){ int *arity = NULL,nb_levels; int *numbering = NULL,nb_nodes; tm_topology_t *new_topo; double *cost; unsigned int vl = tm_get_verbose_level(); int *constraints = NULL, nb_constraints; int i; if(vl >= DEBUG) tm_display_arity(*topology); topology_arity_cpy(*topology,&arity,&nb_levels); topology_numbering_cpy(*topology,&numbering,&nb_nodes); topology_constraints_cpy(*topology,&constraints,&nb_constraints); topology_cost_cpy(*topology,&cost); optimize_arity(&arity,&cost,&nb_levels,nb_levels-2); new_topo = tm_build_synthetic_topology(arity, NULL, nb_levels,numbering,nb_nodes); new_topo->cost = cost; new_topo->constraints = constraints; new_topo->nb_constraints = nb_constraints; new_topo->nb_proc_units = (*topology)->nb_proc_units; new_topo->oversub_fact = (*topology)->oversub_fact; if(vl >= DEBUG){ if(constraints){ printf("Constraints: "); for(i=0;i<nb_constraints;i++) printf("%d - ",constraints[i]); printf("\n"); } tm_display_arity(new_topo); } FREE(arity); FREE(numbering); tm_free_topology(*topology); *topology = new_topo; /* exit(-1); */ }