void processmarriage(const char* ifname="marry.txt",const char* ofname="marry.root"){ vector<int> mage; vector<float> mpercent; vector<int> fage; vector<float> fpercent; mage.push_back(0); mpercent.push_back(0.0); fage.push_back(0); fpercent.push_back(0.0); ifstream marry(ifname); while(1){ int gender,year; float percent; marry >> gender >> year >> percent; if(!marry.good())break; if(gender == 1){ mage.push_back(year); mpercent.push_back(percent); } if(gender == 0){ fage.push_back(year); fpercent.push_back(percent); } } TFile* outfile = new TFile(ofname,"RECREATE"); TGraph* man = new TGraph(mage.size()); TGraph* woman = new TGraph(fage.size()); for(int i = 0; i < (int)mage.size(); i++){ man->SetPoint(i,mage[i],mpercent[i]); } for(int i = 0; i < (int)fage.size(); i++){ woman->SetPoint(i,fage[i],fpercent[i]); } man->SetName("man"); woman->SetName("woman"); man->Write(); woman->Write(); outfile->Write(); outfile->Close(); }
int main(int argc, char *argv[]) { if (argc < 7) { printf("Usage: ./%s first1 last1 age1 first2 last2 age2", argv[0]); } else { // Construct p1 from the command line arguments struct person p1 = { .first_name = argv[1], // note, no strdup (cf. bad3.c) .last_name = argv[2], .age = atoi(argv[3]) }; struct person p2 = { .first_name = argv[4], .last_name = argv[5], .age = atoi(argv[6]) }; // Print the two singles print_person(&p1); print_person(&p2); // Next line just initialises the random function used in marry srand(p2.age); // Marry p1 and p2 marry(&p1, &p2); // Print the two spouses print_person(&p1); print_person(&p2); } return 0; }
void ParallelPairs(void *objs, int Nmyobjs, int sizeobj, int (*numget)(const void *), void (*numset)(const void *, int ), int (*procget)(const void *), void (*marry)(const void *, const void *), int (*compare_objs)(const void *, const void *)){ char *myobjs = (char*) objs; int n, p, sk, cnt, num; int maxind = 0; int procid, nprocs; MPI_Comm_rank(MPI_COMM_WORLD, &procid); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* local sort */ qsort(myobjs, Nmyobjs, sizeobj, compare_objs); /* TW: homework replace from here --------> */ maxind = 0; for(n=0;n<Nmyobjs;++n){ num = numget(myobjs+sizeobj*n); maxind = max(maxind, num); } int globalmaxind; MPI_Allreduce(&maxind, &globalmaxind, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD); int binsize = ceil( (double)(globalmaxind)/(double)nprocs ) + 10; int *outN = (int*) calloc(nprocs, sizeof(int)); int *inN = (int*) calloc(nprocs, sizeof(int)); int *cumoutN = (int*) calloc(nprocs, sizeof(int)); int *cuminN = (int*) calloc(nprocs, sizeof(int)); sk = 0; /* count the number of objs in each bin */ int binup = binsize; for(p=0;p<nprocs;++p){ while( numget(myobjs+(sk*sizeobj) ) <= binup ){ ++(outN[p]); ++sk; if(sk==Nmyobjs){ break; } } binup += binsize; if(sk==Nmyobjs){ break; } } /* TW: <---------- replace to here */ /* communicate numbers to be sent to each bin */ MPI_Alltoall(outN, 1, MPI_INT, inN, 1, MPI_INT, MPI_COMM_WORLD); /* build incoming buffer */ int Notherobjs = 0; for(p=0;p<nprocs;++p) Notherobjs += inN[p]; for(p=0;p<nprocs;++p){ outN[p] *= sizeobj/sizeof(int); inN[p] *= sizeobj/sizeof(int); } for(p=1;p<nprocs;++p){ cumoutN[p] = cumoutN[p-1]+outN[p-1]; cuminN[p] = cuminN[p-1] + inN[p-1]; } /* fill up bins of objects from cloud */ char *otherobjs = (char*) calloc(Notherobjs*sizeobj, sizeof(char)); MPI_Alltoallv(myobjs, outN, cumoutN, MPI_INT, otherobjs, inN, cuminN, MPI_INT, MPI_COMM_WORLD); /* sort the bin */ qsort(otherobjs, Notherobjs, sizeobj, compare_objs); /* number unique objs consecutively in each bin */ for(n=1;n<Notherobjs;++n){ /* match ? */ if(!compare_objs(otherobjs+ n*sizeobj, otherobjs+(n-1)*sizeobj)){ marry(otherobjs+n*sizeobj, otherobjs+(n-1)*sizeobj); } } char *outobjs = (char*) calloc(Notherobjs*sizeobj, sizeof(char)); sk = 0; for(p=0;p<nprocs;++p) for(n=0;n<Notherobjs;++n) if(procget(otherobjs+n*sizeobj)==p){ memcpy(outobjs+sk*sizeobj, otherobjs+n*sizeobj, sizeobj); ++sk; } /* send results out */ MPI_Alltoallv(outobjs, inN, cuminN, MPI_INT, myobjs, outN, cumoutN, MPI_INT, MPI_COMM_WORLD); free(otherobjs); free(outN); free(inN); free(cumoutN); free(cuminN); }