void do_fdecomp_(int* mmax_in, int* nmax_in, char* outname) { int mmax; int nmax; int i,j; double cmx =0.0, cmy=0.0; FILE* fptr; gsl_matrix * amnRe = NULL; gsl_matrix * amnIm = NULL; char buffer[256]; if(*mmax_in <= 0 || *mmax_in > MAXMDECOMP){ mmax = MMAXDEFAULT; } else { mmax = *mmax_in; } if(*nmax_in <=0 || *nmax_in > MAXNDECOMP){ nmax = NMAXDEFAULT; } else { nmax = *nmax_in; } amnRe = gsl_matrix_alloc(2*mmax+1, nmax); amnIm = gsl_matrix_alloc(2*mmax+1, nmax); compute_com(grid, nptsGrid, &cmx, &cmy); fprintf(stderr, "# cm (%lf %lf)\n", cmx, cmy); fprintf(stderr, "# started computing fdecomp\n"); compute_amn(mmax, nmax, grid, nptsGrid, amnRe, amnIm, cmx, cmy); sprintf(buffer, "%s", outname); fptr = fopen(buffer, "a"); fprintf(stderr, "# started writing fdecomp to: %s\n", buffer); // dump the decomp to file fprintf(fptr, "#\n"); // sep events with a # for(i = 0; i < (2*mmax+1); i++){ for(j = 0; j < nmax; j++){ fprintf(fptr, "%d %d %lf %lf\n", -mmax+i, j, gsl_matrix_get(amnRe, i, j), gsl_matrix_get(amnIm, i,j)); } } fclose(fptr); gsl_matrix_free(amnRe); gsl_matrix_free(amnIm); }
int main (int argc, char* argv[]){ FILE *fptr = NULL; char buffer[256]; gsl_matrix *midplane; gsl_matrix *AmnRe, *AmnIm; int i, j; int mmax=2, nmax=4; int npts = 200; int xtemp, ytemp; double etemp = 0.0; double cmx = 0.0, cmy = 0.0; double dx = 0.0; double mod = 0.0; if(argc < 5){ fprintf(stderr, "# requries args:\n# <s:input-file> <i:npts> <i:nmax> <i:mmax> "); exit(-1); } sprintf(buffer, "%s", argv[1]); printf("# reading from: %s\n", buffer); npts = atoi(argv[2]); assert(npts > 0); printf("# input grid size: (%d x %d)\n", npts, npts); mmax = atoi(argv[3]); assert(mmax > 0 && mmax < MAXMDECOMP); nmax = atoi(argv[4]); assert(nmax > 0 && nmax < MAXNDECOMP); printf("# moments computed up to: %d %d\n", mmax, nmax); dx = 2*(fabs(xmin))/((double)npts-1); /* allocate a 2d gsl matrix that will store the event plane energy density * each cell in this grid should correspond to a hydro cell or similar */ midplane = gsl_matrix_alloc(npts, npts); fptr = fopen(buffer, "r"); if(!fptr){ fprintf(stderr, "# cannot open %s\n", buffer); exit(-1); } while(fscanf(fptr, "%d %d %lf", &xtemp, &ytemp, &etemp) != EOF){ gsl_matrix_set(midplane, xtemp - 1, ytemp -1, etemp); }; fclose(fptr); /* allocate matrices to hold the real and im parts of the coeffs */ AmnRe = gsl_matrix_alloc((2*mmax+1), nmax); AmnIm = gsl_matrix_alloc((2*mmax+1), nmax); /* get the cm of this event */ compute_com(midplane, npts, &cmx, &cmy); /* print out the cm in the scaled coors and the indicies of the actual grid cell */ printf("# com: %lf %lf (%d %d)\n", cmx, cmy, (int)((cmx+(fabs(xmin)))/dx), (int)((cmy+(fabs(xmin)))/dx)); /* do the decomp */ compute_amn(mmax, nmax, midplane, npts, AmnRe, AmnIm, cmx, cmy); // first compute with cm at origin of coords /* print out the coeffs and compute the L2 norm */ for(i = 0; i < (2*mmax+1); i++){ for(j = 0; j < nmax; j++){ printf("(%d %d)[%lf %lf] ", -mmax+i, j, gsl_matrix_get(AmnRe, i, j), gsl_matrix_get(AmnIm, i,j)); } printf("\n"); } printf("L2: %lf\n", compute_l2(mmax, nmax, AmnRe, AmnIm)); printf("M1: %lf\n", compute_m1(mmax, nmax, AmnRe, AmnIm)); printf("H1: %lf\n", compute_h1(mmax, nmax, AmnRe, AmnIm)); printf("Rsq: %lf\n", compute_rsq(mmax, nmax, AmnRe, AmnIm)); gsl_matrix_free(AmnRe); gsl_matrix_free(AmnIm); gsl_matrix_free(midplane); return EXIT_SUCCESS; }
void GreedyCommLB::work(LDStats* stats) { int pe,obj,com; ObjectRecord *x; int i; if (_lb_args.debug()) CkPrintf("In GreedyCommLB strategy\n",CkMyPe()); npe = stats->nprocs(); nobj = stats->n_objs; // nmigobj is calculated as the number of migratable objects // ObjectHeap maxh is of size nmigobj nmigobj = stats->n_migrateobjs; stats->makeCommHash(); assigned_array = new int[nobj]; object_graph = new graph[nobj]; init_data(assigned_array,object_graph,npe,nobj); #define MAXDOUBLE 1e10; // processor heap processors = new processorInfo[npe]; for (int p=0; p<npe; p++) { processors[p].Id = p; processors[p].backgroundLoad = stats->procs[p].bg_walltime; processors[p].computeLoad = 0; processors[p].pe_speed = stats->procs[p].pe_speed; if (!stats->procs[p].available) { processors[p].load = MAXDOUBLE; } else { processors[p].load = 0; if (!_lb_args.ignoreBgLoad()) processors[p].load = processors[p].backgroundLoad; } } // assign communication graph for(com =0; com< stats->n_comm;com++) { int xcoord=0,ycoord=0; LDCommData &commData = stats->commData[com]; if((!commData.from_proc())&&(commData.recv_type()==LD_OBJ_MSG)) { xcoord = stats->getHash(commData.sender); ycoord = stats->getHash(commData.receiver.get_destObj()); if((xcoord == -1)||(ycoord == -1)) if (_lb_args.ignoreBgLoad() || stats->complete_flag==0) continue; else CkAbort("Error in search\n"); add_graph(xcoord,ycoord,commData.bytes, commData.messages); } else if (commData.recv_type()==LD_OBJLIST_MSG) { int nobjs; LDObjKey *objs = commData.receiver.get_destObjs(nobjs); xcoord = stats->getHash(commData.sender); for (int i=0; i<nobjs; i++) { ycoord = stats->getHash(objs[i]); if((xcoord == -1)||(ycoord == -1)) if (_lb_args.migObjOnly()) continue; else CkAbort("Error in search\n"); //printf("Multicast: %d => %d %d %d\n", xcoord, ycoord, commData.bytes, commData.messages); add_graph(xcoord,ycoord,commData.bytes, commData.messages); } } } // only build heap with migratable objects, // mapping nonmigratable objects to the same processors ObjectHeap maxh(nmigobj+1); for(obj=0; obj < stats->n_objs; obj++) { LDObjData &objData = stats->objData[obj]; int onpe = stats->from_proc[obj]; if (!objData.migratable) { if (!stats->procs[onpe].available) { CmiAbort("Load balancer is not be able to move a nonmigratable object out of an unavailable processor.\n"); } alloc(onpe, obj, objData.wallTime); update(stats, obj, onpe); // update communication cost on other pes } else { x = new ObjectRecord; x->id = obj; x->pos = obj; x->val = objData.wallTime; x->pe = onpe; maxh.insert(x); } } minHeap *lightProcessors = new minHeap(npe); for (i=0; i<npe; i++) if (stats->procs[i].available) lightProcessors->insert((InfoRecord *) &(processors[i])); int id,maxid,minpe=0; double temp,total_time,min_temp; // for(pe=0;pe < count;pe++) // CkPrintf("avail for %d = %d\n",pe,stats[pe].available); double *pe_comm = new double[npe]; for (int i=0; i<npe; i++) pe_comm[i] = 0.0; for(id = 0;id<nmigobj;id++){ x = maxh.deleteMax(); maxid = x->id; processorInfo *donor = (processorInfo *) lightProcessors->deleteMin(); CmiAssert(donor); int first_avail_pe = donor->Id; temp = compute_com(stats, maxid, first_avail_pe); min_temp = temp; //total_time = temp + alloc_array[first_avail_pe][nobj]; total_time = temp + donor->load; minpe = first_avail_pe; // search all procs for best // optimization: only search processors that it communicates // and the minimum of all others CkVec<int> commPes; graph * ptr = object_graph[maxid].next; // find out all processors that this obj communicates double commload = 0.0; // total comm load for(int com=0;(com<2*nobj)&&(ptr != NULL);com++,ptr=ptr->next){ int destObj = ptr->id; if(assigned_array[destObj] == 0) // this obj has not been assigned continue; int destPe = stats->to_proc[destObj]; if(stats->procs[destPe].available == 0) continue; double cload = alpha*ptr->nmsg + beeta*ptr->data; pe_comm[destPe] += cload; commload += cload; int exist = 0; for (int pp=0; pp<commPes.size(); pp++) if (destPe == commPes[pp]) { exist=1; break; } // duplicated if (!exist) commPes.push_back(destPe); } int k; for(k = 0; k < commPes.size(); k++){ pe = commPes[k]; processorInfo *commpe = (processorInfo *) &processors[pe]; temp = commload - pe_comm[pe]; //CkPrintf("check id = %d, processor = %d,com = %lf, pro = %lf, comp=%lf\n", maxid,pe,temp,alloc_array[pe][nobj],total_time); if(total_time > (temp + commpe->load)){ minpe = pe; total_time = temp + commpe->load; min_temp = temp; } } /* CkPrintf("check id = %d, processor = %d, obj = %lf com = %lf, pro = %lf, comp=%lf\n", maxid,minpe,x->load,min_temp,alloc_array[minpe][nobj],total_time); */ // CkPrintf("before 2nd alloc\n"); stats->assign(maxid, minpe); alloc(minpe, maxid, x->val + min_temp); // now that maxid assigned to minpe, update other pes load update(stats, maxid, minpe); // update heap lightProcessors->insert(donor); for(k = 0; k < commPes.size(); k++) { pe = commPes[k]; processorInfo *commpe = (processorInfo *) &processors[pe]; lightProcessors->update(commpe); pe_comm[pe] = 0.0; // clear } delete x; } // free up memory delete [] pe_comm; delete [] processors; delete [] assigned_array; delete lightProcessors; for(int oindex= 0; oindex < nobj; oindex++){ graph * ptr = &object_graph[oindex]; ptr = ptr->next; while(ptr != NULL){ graph *cur = ptr; ptr = ptr->next; delete cur; } } delete [] object_graph; }