void Type::genTypeInfo(Scope *sc) { //printf("Type::genTypeInfo() %p, %s\n", this, toChars()); if (!Type::dtypeinfo) { error(Loc(), "TypeInfo not found. object.d may be incorrectly installed or corrupt, compile with -v switch"); fatal(); } Type *t = merge2(); // do this since not all Type's are merge'd if (!t->vtinfo) { if (t->isShared()) // does both 'shared' and 'shared const' t->vtinfo = TypeInfoSharedDeclaration::create(t); else if (t->isConst()) t->vtinfo = TypeInfoConstDeclaration::create(t); else if (t->isImmutable()) t->vtinfo = TypeInfoInvariantDeclaration::create(t); else if (t->isWild()) t->vtinfo = TypeInfoWildDeclaration::create(t); else t->vtinfo = t->getTypeInfoDeclaration(); assert(t->vtinfo); /* If this has a custom implementation in std/typeinfo, then * do not generate a COMDAT for it. */ if (!t->builtinTypeInfo()) { // Generate COMDAT if (sc) // if in semantic() pass { if (sc->func && !sc->func->isInstantiated() && sc->func->inNonRoot()) { // Bugzilla 13043: Avoid linking TypeInfo if it's not // necessary for root module compilation } else { // Find module that will go all the way to an object file Module *m = sc->module->importedFrom; m->members->push(t->vtinfo); semanticTypeInfo(sc, t); } } else // if in obj generation pass { t->vtinfo->toObjFile(global.params.multiobj); } } } if (!vtinfo) vtinfo = t->vtinfo; // Types aren't merged, but we can share the vtinfo's assert(vtinfo); }
Expression *Type::getTypeInfo(Scope *sc) { //printf("Type::getTypeInfo() %p, %s\n", this, toChars()); if (!Type::typeinfo) { error(0, "TypeInfo not found. object.d may be incorrectly installed or corrupt, compile with -v switch"); fatal(); } Expression *e = 0; Type *t = merge2(); // do this since not all Type's are merge'd if (!t->vtinfo) { #if DMDV2 if (t->isShared()) t->vtinfo = new TypeInfoSharedDeclaration(t); else if (t->isConst()) t->vtinfo = new TypeInfoConstDeclaration(t); else if (t->isImmutable()) t->vtinfo = new TypeInfoInvariantDeclaration(t); else if (t->isWild()) t->vtinfo = new TypeInfoWildDeclaration(t); else #endif t->vtinfo = t->getTypeInfoDeclaration(); assert(t->vtinfo); /* If this has a custom implementation in std/typeinfo, then * do not generate a COMDAT for it. */ if (!t->builtinTypeInfo()) { // Generate COMDAT if (sc) // if in semantic() pass { // Find module that will go all the way to an object file Module *m = sc->module->importedFrom; m->members->push(t->vtinfo); } else // if in obj generation pass { #if IN_DMD t->vtinfo->toObjFile(0); // TODO: multiobj #else t->vtinfo->codegen(sir); #endif } } } e = new VarExp(0, t->vtinfo); e = e->addressOf(sc); e->type = t->vtinfo->type; // do this so we don't get redundant dereference return e; }
int sort2(int *numbers,int left, int right) { int mid; int inv=0; if (right > left) { mid = (right + left) / 2; inv+=sort2(numbers,left, mid); inv+=sort2(numbers,mid+1, right); inv+=merge2(numbers,left, mid+1, right); } return inv; }
void Damage::Merge (BoxObj& newb) { BoxObj* a1, *a2; int newArea, area1, area2, diff1, diff2, diff3, maximum; Iterator i; FirstArea(i); a1 = GetArea(i); Next(i); a2 = GetArea(i); BoxObj merge1(*a1 + newb); BoxObj merge2(*a2 + newb); BoxObj merge3(*a1 + *a2); newArea = Area(newb); area1 = Area(*a1); area2 = Area(*a2); diff1 = area1 + newArea - Area(merge1); diff2 = area2 + newArea - Area(merge2); diff3 = area1 + area2 - Area(merge3); maximum = max(max(diff1, diff2), diff3); if (maximum == diff1) { if (a2->Intersects(merge1)) { *a1 = merge1 + *a2; DeleteArea(a2); } else { *a1 = merge1; } } else if (maximum == diff2) { if (a1->Intersects(merge2)) { *a2 = merge2 + *a1; DeleteArea(a1); } else { *a2 = merge2; } } else { if (newb.Intersects(merge3)) { *a1 = merge3 + newb; DeleteArea(a2); } else { *a1 = merge3; *a2 = newb; } } }
ListNode* merge(vector<ListNode*> &lists) { int size = lists.size(); if(size==0) return NULL; queue<ListNode*> q; for(int i=0; i<size; i++) q.push(lists[i]); while(q.size()>1){ ListNode *n1 = q.front(); q.pop(); ListNode *n2 = q.front(); q.pop(); q.push(merge2(n1,n2)); } return q.front(); }
int main() { { int a[6] = {1,3,4,6,8,9}; int b[12] = {0,2,5,7,10,11}; merge1(a, b, 6); for(int i = 0;i < 12;++i) std::cout<<b[i]<<","; std::cout<<"\n"; } std::cout<<"---\n"; { int a[6] = {1,3,4,6,8,9}; int b[12] = {0,2,5,7,10,11}; merge2(a, b, 6); for(int i = 0;i < 12;++i) std::cout<<b[i]<<","; std::cout<<"\n"; } }
Expression *Type::getTypeInfo(Scope *sc) { //printf("Type::getTypeInfo() %p, %s\n", this, toChars()); if (!Type::dtypeinfo) { error(Loc(), "TypeInfo not found. object.d may be incorrectly installed or corrupt, compile with -v switch"); fatal(); } Type *t = merge2(); // do this since not all Type's are merge'd if (!t->vtinfo) { #if DMDV2 if (t->isShared()) // does both 'shared' and 'shared const' t->vtinfo = new TypeInfoSharedDeclaration(t); else if (t->isConst()) t->vtinfo = new TypeInfoConstDeclaration(t); else if (t->isImmutable()) t->vtinfo = new TypeInfoInvariantDeclaration(t); else if (t->isWild()) t->vtinfo = new TypeInfoWildDeclaration(t); else #endif t->vtinfo = t->getTypeInfoDeclaration(); assert(t->vtinfo); vtinfo = t->vtinfo; /* If this has a custom implementation in std/typeinfo, then * do not generate a COMDAT for it. */ if (!t->builtinTypeInfo()) { // Generate COMDAT if (sc) // if in semantic() pass { // Find module that will go all the way to an object file Module *m = sc->module->importedFrom; m->members->push(t->vtinfo); if (ty == Tstruct) { Dsymbol *s; StructDeclaration *sd = ((TypeStruct *)this)->sym; if ((sd->xeq && sd->xeq != sd->xerreq || sd->xcmp && sd->xcmp != sd->xerrcmp || search_toHash(sd) || search_toString(sd) ) && inNonRoot(sd)) { //printf("deferred sem3 for TypeInfo - sd = %s, inNonRoot = %d\n", sd->toChars(), inNonRoot(sd)); Module::addDeferredSemantic3(sd); } } } else // if in obj generation pass { t->vtinfo->toObjFile(global.params.multiobj); } } } if (!vtinfo) vtinfo = t->vtinfo; // Types aren't merged, but we can share the vtinfo's Expression *e = new VarExp(Loc(), t->vtinfo); e = e->addressOf(sc); e->type = t->vtinfo->type; // do this so we don't get redundant dereference return e; }
TypeInfoDeclaration *Type::buildTypeInfo(Scope *sc, bool checkNeedSemantic) { if (vtinfo) return vtinfo; if (sc && checkNeedSemantic && !typeInfoNeedsSemantic()) return 0; //printf("Type::getTypeInfo() %p, %s\n", this, toChars()); if (!Type::dtypeinfo) { error(Loc(), "TypeInfo not found. object.d may be incorrectly installed or corrupt, compile with -v switch"); fatal(); } Type *t = merge2(); // do this since not all Type's are merge'd if (!t->vtinfo) { if (t->isShared()) // does both 'shared' and 'shared const' t->vtinfo = new TypeInfoSharedDeclaration(t); else if (t->isConst()) t->vtinfo = new TypeInfoConstDeclaration(t); else if (t->isImmutable()) t->vtinfo = new TypeInfoInvariantDeclaration(t); else if (t->isWild()) t->vtinfo = new TypeInfoWildDeclaration(t); else t->vtinfo = t->getTypeInfoDeclaration(); assert(t->vtinfo); vtinfo = t->vtinfo; /* If this has a custom implementation in std/typeinfo, then * do not generate a COMDAT for it. */ if (t->ty == Terror) vtinfo->errors = 1; else if (!t->builtinTypeInfo()) { // Generate COMDAT if (sc) // if in semantic() pass { // Find module that will go all the way to an object file Module *m = sc->module->importedFrom; m->members->push(t->vtinfo); if(m->semanticRun >= 3) { Module::addDeferredSemantic3(t->vtinfo); t->vtinfo->deferredScope = sc; sc->setNoFree(); } if (ty == Tstruct) { Dsymbol *s; StructDeclaration *sd = ((TypeStruct *)this)->sym; if (sd->members && (sd->xeq && sd->xeq != sd->xerreq || sd->xcmp && sd->xcmp != sd->xerrcmp || search_toHash(sd) || search_toString(sd) ) && inNonRoot(sd)) { //printf("deferred sem3 for TypeInfo - sd = %s, inNonRoot = %d\n", sd->toChars(), inNonRoot(sd)); Module::addDeferredSemantic3(sd); } } } else // if in obj generation pass { // it is always a problem if this is called from the backend without // being added to the AST for semantic analysis (no RTInfo generated) // to ease transition, modifier types are just put out as they just forward // to the actual TypeInfo if (t->typeInfoNeedsSemantic()) error(Loc(), "ICE: unexpected type info request for %s", t->toChars()); t->vtinfo->toObjFile(global.params.multiobj); } } } if (!vtinfo) vtinfo = t->vtinfo; // Types aren't merged, but we can share the vtinfo's return vtinfo; }
int main(int argc, char **argv) { FILE *fp, *tp, *sp; Widget widgets[MAXW]; char key[16], *runpath, *wishpath, *scriptpath, *tkpath, *cp; char *scratchpath = "scratchrun.tk"; char cmd[256]; int i,nw,noexec; fprintf(stderr,"%s\n",VERSION); if (argc == 1) { usage(); } else { runpath = NULL; scriptpath = NULL; tkpath = NULL; wishpath = "wish"; noexec=0; for (i=1; i<argc; i++) { cp = argv[i]; if (*cp != '-') scriptpath = (char *) strdup(cp); else { cp++; switch (*cp) { case 'r': runpath = (char *) strdup(argv[++i]); break; case 'w': wishpath = (char *) strdup(argv[++i]); break; case 't': tkpath = (char *) strdup(argv[++i]); break; case 'x': noexec = 1; break; case 'e': example(); /* will exit the program */ break; case 'h': usage(); break; default: fprintf(stderr,"Option -%s not supported yet\n",cp); usage(); break; } } } } if (runpath == NULL) runpath = scriptpath; if (tkpath == NULL) { tkpath = (char *) malloc(strlen(scriptpath)+4); sprintf(tkpath,"%s.tk",scriptpath); } if (strcmp(scriptpath,tkpath)==0) { fprintf(stderr,"Error: Run and Script are the same (%s)\n",runpath); exit(1); } fp = fopen(scriptpath,"r"); if (fp == NULL) { fprintf(stderr,"Error: ScriptFile %s could not be opened\n",scriptpath); exit(1); } tp = fopen(tkpath,"w"); if (tp == NULL) { fprintf(stderr,"Error: TkFile %s could not be written\n",tkpath); exit(1); } sp = fopen(scratchpath,"w"); if (sp == NULL) { fprintf(stderr,"Error: ScratchFile %s could not be written\n",scratchpath); exit(1); } fprintf(tp,preamble,VERSION); nw = parse(fp, tp, sp, MAXW, widgets); fclose(fp); fclose(sp); fprintf(tp,"set keyList {"); for (i=0; i<nw; i++) { if (widgets[i].list) fprintf(tp," .keys.%s.frame.val",widgets[i].key); } fprintf(tp," }\n"); fprintf(tp,"proc my_save {} {\n"); fprintf(tp," puts \"my_save:\"\n"); fprintf(tp," set filename \"%s.key\"\n",runpath); fprintf(tp," set file [open $filename w]\n"); for (i=0; i<nw; i++) { sprintf(key,widgets[i].key); fprintf(tp," global var_%s\n",key); fprintf(tp," puts $file \"%s=$var_%s\"\n",key,key); } fprintf(tp," close $file\n"); fprintf(tp,"}\n"); fprintf(tp,"proc my_load {} {\n"); fprintf(tp," puts \"my_load:\"\n"); for (i=0; i<nw; i++) { sprintf(key,widgets[i].key); fprintf(tp," global var_%s\n",key); } fprintf(tp," set filename \"%s.key\"\n",runpath); fprintf(tp," set file [open $filename r]\n"); fprintf(tp," while {[gets $file line] >= 0} {\n"); fprintf(tp," set key [lindex [split $line =] 0]\n"); fprintf(tp," set val [lindex [split $line =] 1]\n"); for (i=0; i<nw; i++) { sprintf(key,widgets[i].key); if (widgets[i].type == W_CHECK) fprintf(tp," if {$key == \"%s\"} { set var_%s $val; load_var_%s }\n",key,key,key); else fprintf(tp," if {$key == \"%s\"} { set var_%s $val }\n",key,key); } fprintf(tp," }\n"); fprintf(tp," close $file\n"); fprintf(tp,"}\n"); fprintf(tp,"proc my_run {} {\n"); fprintf(tp," puts \"my_run:\"\n"); for (i=0; i<nw; i++) { sprintf(key,widgets[i].key); fprintf(tp," global var_%s\n",key); fprintf(tp," lappend args \"%s=$var_%s\"\n",key,key); } fprintf(tp," eval exec <@stdin >@stdout %s $args\n",runpath); fprintf(tp,"}\n"); sp = fopen(scratchpath,"r"); if (sp == NULL) { fprintf(stderr,"Error: ScratchFile %s could not be read\n",scratchpath); exit(1); } (void) merge2(tp, sp); fprintf(tp,"%s",postamble); fprintf(tp,"# Found %d widgets\n",nw); for (i=0; i<nw; i++) { fprintf(tp,"# %d: %s=%s\n",widgets[i].type, widgets[i].key, widgets[i].val); } fclose(tp); if (noexec) exit(0); sprintf(cmd,"%s -f %s",wishpath,tkpath); fprintf(stderr,"Running %s\n",cmd); system(cmd); return 0; }
void bspParSort(){ int Log2(int x); void mergeSort(int x, int *temp1); void merge2(int *arr1, int *arr2, int size); int *localArr; /* local array in each processor */ int i,j,k; /* index variables */ int n_divide_p; /* Avoid multiple computation */ int n; /* Number of elements to be sorted */ int szLocalArray; /* Size of local array */ double time0, time1; /* Time */ FILE *ifp = 0; /* Reader to read sequence of numbers to be sorted */ bsp_begin(P); int p= bsp_nprocs(); /* Number of processors obtained */ int s= bsp_pid(); /* Processor number */ //Get number of elements to be sorted if(s==0){ ifp = fopen("sort","r"); if(ifp == NULL){ fprintf(stderr, "Can't open input file!\n"); exit(1); } fscanf(ifp, "%i", &n); } // Make sure every processor knows everything bsp_push_reg(&n,sizeof(int)); bsp_sync(); bsp_get(0,&n,0,&n,sizeof(int)); bsp_sync(); bsp_pop_reg(&n); //Setup distribution n_divide_p = n/p; szLocalArray = n/pow(2,ceil(Log2(s+1))); localArr = vecalloci(szLocalArray); bsp_push_reg(localArr,sizeof(int)*szLocalArray); if(s==0){ printf("Distribution start\n"); fflush(stdout); } bsp_sync(); int value; if(s==0){ //allocate to array on proc 0 for(i=0; i< n_divide_p; i++){ fscanf(ifp, "%i", &value); localArr[i]=value; } //Send to arrays on other processors for(i=1; i< p; i++){ for(j=0;j<n_divide_p;j++){ fscanf(ifp, "%i", &value); bsp_put(i,&value,localArr,j*sizeof(int),sizeof(int)); } } fclose(ifp); } bsp_sync(); if(s==0){ printf("Distribution done\n"); fflush(stdout); } //Distribution done and we can start time measurement if(s==0){ printf("Time start\n"); fflush(stdout); } time0 = bsp_time(); //Locally sort each array if(s==0){ printf("Local sort\n"); fflush(stdout); } mergeSort(n_divide_p, localArr); bsp_sync(); //Merging int *temp = malloc(sizeof(int)*pow(2,Log2(p))*n_divide_p); for(j=1;j<Log2(p)+1;j++){ if(s<p/pow(2,j)){ for(k=0;k<pow(2,j-1)*n_divide_p;k++){ bsp_get(s+(p/pow(2,j)),localArr,k*sizeof(int),&(temp[k]),sizeof(int)); } } bsp_sync(); if(s<p/pow(2,j)){ merge2(localArr, temp, n_divide_p*pow(2,j-1)); } bsp_sync(); if(s==0){ printf("Round %i out of %i rounds of merging done (on proc 0)\n",j,Log2(p)); fflush(stdout); } } if(s==0){ printf("Sorting done\n"); fflush(stdout); } bsp_sync(); //Print sorted array - expensive if sample is big /* if(s==0){ printf("Sorted sequence is:\n"); for(i=0; i<szLocalArray; i++){ printf("%i ",localArr[i]); fflush(stdout); } printf("\n"); fflush(stdout); } */ //Parallel algorithm ends time1 = bsp_time(); if(s==0){ printf("Time stop\n"); fflush(stdout); } //Report time to user if(s==0){ printf("Sorting took %.6lf seconds.\n", time1-time0); fflush(stdout); } //Clean up free(temp); bsp_pop_reg(localArr); free(localArr); bsp_end(); } /* End bspParSort */