static void backward_propagation2(float *cost, float *height, struct ortho_view *t, int n) { for (int k = 0; k < n; k++) fprintf(stderr, "bp2_%d %p %p %d\n", k, t+k, t[k].r, t[k].w); int neigs[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1}}; #ifdef _OPENMP #pragma omp parallel for #endif//OPENMP for (int i = t->w-1; i >= 0; i--) for (int j = t->h-1; j >= 0; j--) { int idx = j * t->w + i; for (int k = 0; k < 4; k++) { int ii = i + neigs[k][0]; int jj = j + neigs[k][1]; if (!insideP(t->w, t->h, ii, jj)) continue; float hh = height[jj*t->w+ii]; float new_cost = eval_cost(t, n, ii, jj, hh); if (new_cost < cost[idx]) { cost[idx] = new_cost; height[idx] = hh; } } } }
static void random_search(float *cost, float *out_h, float *init_h, struct ortho_view *t, int n) { for (int k = 0; k < n; k++) fprintf(stderr, "rs_%d %p %p %d\n", k, t+k, t[k].r, t[k].w); float min_off = PM_MIN(); float max_off = PM_MAX(); #ifdef _OPENMP #pragma omp parallel for #endif//OPENMP for (int j = 0; j < t->h; j++) for (int i = 0; i < t->w; i++) { int idx = j * t->w + i; for (int k = 0; k < PM_TRIALS(); k++) { float new_h = init_h[idx] + randu(min_off, max_off); float new_cost = eval_cost(t, n, i, j, new_h); if (new_cost < cost[idx]) { cost[idx] = new_cost; out_h[idx] = new_h; } } } }
// evaluate a candidate disparity between two planets static float orbital_cost(struct apm_orbit *a, struct apm_orbit *b, int i, int j, float disp, int aidx, int bidx) { assert(aidx >= 0); assert(aidx < a->n); assert(bidx >= 0); assert(bidx < a->n); struct apm_planet *pa = a->t[aidx] + 0; struct apm_planet *pb = b->t[bidx] + 0; float ai = pa->A[0] * i + pa->A[1] * j + pa->A[2]; float bi = pb->A[0] * (i + disp) + pb->A[1] * j + pb->A[2]; int xxa = lrint(ai * subpix) % subpix; int xxb = lrint(bi * subpix) % subpix; pa = a->t[aidx] + xxa; pb = b->t[bidx] + xxb; ai = pa->A[0] * i + pa->A[1] * j + pa->A[2]; bi = pb->A[0] * (i + disp) + pb->A[1] * j + pb->A[2]; if (!NDEBUG) { int xxa = lrint(ai * subpix) % subpix; int xxb = lrint(bi * subpix) % subpix; assert(!xxa); assert(!xxb); } int iai = lrint(ai); int ibi = lrint(bi); float *ax = pa->x; int aw = pa->w; int ah = pa->h; float *bx = pb->x; int bw = pb->w; int bh = pb->h; return eval_cost(ax,aw,ah, bx,bw,bh, pa->pd, iai,j, ibi,j); }
// This is the add_action hook handling movement, commands, emotes and // channels. Optimization is needed. // private nomask int command_hook(string arg) nomask int command_hook(string arg) { string verb, file; object me=this_object(); #ifdef PROFILE_COMMANDS int mem, utime, stime; mapping info; int ecost; // added by mon. 2/25/98 mem = memory_info(); info = rusage(); utime = info["utime"]; stime = info["stime"]; ecost=eval_cost(); #endif verb = query_verb(); //added by mon 11/23/97 if(userp(me)&& (me->query_temp("d_mana"))>0) { if(me->query_temp("is_living")!=1) { if(verb!="bian" && verb!="say" && verb!="tell" && verb!="reply" && verb!="look") { write("别忘了你现在是一"+ (undefinedp(me->query_temp("unit"))? "个":me->query_temp("unit")) +me->name()+"!\n"); return 1; } } } /* mon 10/10/98 if( !arg && (environment() && stringp(environment()->query("exits/" + verb))) && stringp(file = find_command("go")) && call_other(file, "main", this_object(), verb)) ; */ if( !arg && (environment() && environment()->query("exits/" + verb)) && stringp(file = find_command("go")) && call_other(file, "main", this_object(), verb)) ; else if( stringp(file = find_command(verb)) && call_other(file, "main", this_object(), arg)) ; else if( EMOTE_D->do_emote( this_object(), verb, arg ) ) ; else if( CHANNEL_D->do_channel( this_object(), verb, arg ) ) ; else return 0; #ifdef PROFILE_COMMANDS info = rusage(); PROFILE_D->log_command(verb, memory_info() - mem, info["stime"] - stime, info["utime"] - utime, ecost-eval_cost()); #endif return 1; }
int *kpartition_greedy(int k, com_mat_t *com_mat, int n, int *constraints, int nb_constraints) { int *res = NULL, *best_res=NULL, *size = NULL; int i,j,nb_trials; int max_size, max_val; double cost, best_cost = -1; int start, end; int dumb_id, nb_dumb; for( nb_trials = 0 ; nb_trials < MAX_TRIALS ; nb_trials++ ){ res = (int *)MALLOC(sizeof(int)*n); for ( i = 0 ; i < n ; i ++ ) res[i] = -1; size = (int *)CALLOC(k,sizeof(int)); max_size = n/k; /*printf("Constraints: ");print_1D_tab(constraints,nb_constraints);*/ /* put "dumb" vertices in the correct partition if there are any*/ if (nb_constraints){ start = 0; dumb_id = n-1; for( i = 0 ; i < k ; i ++){ max_val = (i+1)* (n/k); end = start; while( end < nb_constraints){ if(constraints[end] >= max_val) break; end++; } /* now end - start is the number of constarints for the ith subtree hence the number of dumb vertices is the differences between the number of leaves of the subtree (n/k) and the number of constraints */ nb_dumb = n/k - (end-start); /*printf("max_val: %d, nb_dumb=%d, start=%d, end=%d, size=%d\n",max_val, nb_dumb, start, end, n/k);*/ /* dumb vertices are the one with highest indices: put them in the ith partitions*/ for( j = 0; j < nb_dumb; j ++ ){ res[dumb_id] = i; dumb_id--; } /* increase the size of the ith partition accordingly*/ size[i] += nb_dumb; start=end; } } /*printf("After dumb vertices mapping: ");print_1D_tab(res,n);*/ /* choose k initial "true" vertices at random and put them in a different partition */ for ( i = 0 ; i < k ; i ++ ){ /* if the partition is full of dumb vertices go to next partition*/ if(size[i] >= max_size) continue; /* find a vertex not allready partitionned*/ do{ /* call the mersenne twister PRNG of tm_mt.c*/ j = genrand_int32() % n; } while ( res[j] != -1 ); /* allocate and update size of partition*/ res[j] = i; /* printf("random: %d -> %d\n",j,i); */ size[i]++; } /* allocate each unaloacted vertices in the partition that maximize the communication*/ for( i = 0 ; i < n ; i ++) if( res[i] == -1) allocate_vertex(i, res, com_mat, n, size, max_size); cost = eval_cost(res,com_mat); /*print_1D_tab(res,n); printf("cost=%.2f\n",cost);*/ if((cost<best_cost) || (best_cost == -1)){ best_cost=cost; FREE(best_res); best_res=res; }else FREE(res); FREE(size); } /*print_1D_tab(best_res,n); printf("best_cost=%.2f\n",best_cost); */ return best_res; }