int main (int argc, char *argv[])
{
    graph g;
    int i;

    if(argc < 2)
    {
        printf("[main_maximum_partial] ERREUR : you have to specify a file containing a graph as the first argument of the main.\n");
        return EXIT_FAILURE;
    }

    else
    {
        gen_graph(argv[1], &g);
        draw_graph(g);

        subgraph desert;
        for(i=0 ; i<NB_VERTICES ; i++) desert[i]=0;

        maximum_partial(g,desert);

        draw_subgraph(desert,g.n);

        return EXIT_SUCCESS;
    }
}
int main (int argc, char** argv)
{
	int n 	= 200;
	double p = .05;
	const char* ifname = NULL;
	const char* ofname = NULL;

	//args
	extern char* optarg;
	const char* optstring = "hn:d:p:o:i:";
	int c;
    while ((c = getopt(argc, argv, optstring)) != -1) {
        switch (c) {
        case 'h':
            fprintf(stderr, "%s", usage);
            return -1;
        case 'n': n = atoi(optarg); break;
        case 'p': p = atof(optarg); break;
        case 'o': ofname = optarg;  break;
        case 'i': ifname = optarg;  break;
        }
    }
	
	 //make graph + output
	 int* l = gen_graph(n,p);
	 if(ifname)
		 write_matrix(ifname, n, l);

	 
	 double start = omp_get_wtime();
	 infinitize(n,l);
	 floyd(l,n); 
	 deinfinitize(n,l);
	 double end = omp_get_wtime();

	 printf("== OpenMP with %d threads\n", omp_get_max_threads());
	 printf("n:     %d\n", n);
	 printf("p:     %g\n", p);
	 printf("Time:  %g sec\n", (end-start));
	 printf("Check: %X\n", fletcher16(l, n*n));

	 //output
	 if(ofname)
		 write_matrix(ofname,n,l);

	 free(l);
	 return 0;
}
Esempio n. 3
0
int main(int argc,char*argv[])
{
	if(argc!=2)
	{
		cout<<"USAGE:SIS datafile  "<<endl;
		exit(1);
	}
	//define the entry

	string networkname(argv[1]); //network name

	double a=1;
	double alpha=0.4;
	double beta=0.1;
	int game_rounds=500; //MC rounds
	int aver_rounds=10;  //do average
	double delta=1;//delta


	string output_name_pre;  //the network name 
	for(int i=0;i!=networkname.size()-4;i++)
	{
		output_name_pre.push_back(networkname[i]);
	}

	//random generator
	//to initialize the random seed
	igraph_rng_init(mrng,&igraph_rngtype_mt19937);

	unsigned long idum;  // random number seed
	idum=(unsigned long)time(NULL);
	igraph_rng_seed(mrng,idum);

	//construct the graph
	igraph_t network;
   
	gen_graph(&network,networkname);
	int nwsize=igraph_vcount(&network);  //the size of the network
   
   
	//to generate the name of the output file
	//output file name
	string resname("res.txt");
	string totalname=output_name_pre+resname;	
	FILE *outfile;
	outfile = fopen(totalname.c_str(), "w");
	
	//construct the population
	for(a=-6;a<6.1;a+=0.1 )
	{
		population popu(output_name_pre,&network,delta,0.01,a,2,3*nwsize,3*nwsize);
		double res=0;
		popu.population_dynamics(&res,alpha,beta,1000);
		fprintf(outfile,"%f\t%f\n",a,res/ nwsize);
		fflush(outfile);
	}
	fclose(outfile);
/*  
	igraph_t network;
	int nwsize = igraph_vcount(&network);  //the size of the network
	igraph_watts_strogatz_game(&network, 1, 500, 4, 0.1, false, false);
	string output_name_pre("small_world");
	population test(output_name_pre,&network, 1, 0.01, 1, 0, 3 * nwsize, 3 * nwsize);
	test.initial_state();

	double res = 0;

	test.population_dynamics(&res,0.3,0.1,300);
*/
	//for (a=-5;a!=5;a+=0.2)
	//
	//{

 //       population test(&network,delta,0.01,a,type,3*nwsize,3*nwsize);
	//	vector<double> average_rres;

	//	for(int i=0;i!=aver_rounds;++i)
	//	{
	//		double rres=0;
	//		test.initial_state();
	//		test.population_dynamics(&rres,0.4,0.1,game_rounds);
	////		printf("%f\n", rres);
	//		average_rres.push_back(rres);

	//	}
	//	fprintf(outfile,"%f\t%f\n",a,double (accumulate(average_rres.begin(),\
	//		average_rres.end(),0)/average_rres.size()));
	//	fflush(outfile);
	//}
	//fclose(outfile);
	igraph_destroy(&network);
	return 0;
}
Esempio n. 4
0
void parse_data(packet_info* packetinfo, peer* p)
{
  ll* tmplist = create_ll();
  chunk_table* find = NULL;
  uint8_t n = packetinfo->numberHashes[0];
  uint8_t tempChunk[CHUNK];
  bzero(tempChunk, CHUNK);
  unsigned int seqNumber; unsigned int ackNumber;
  int headerLength;
  int totalPacketLength;
  int delno;

  switch (packetinfo->packetType[0]) {

  case 0:
    /*
    // IF WHOHAS
    extract chunk;
    check if chunk exists in has_chunks
    create linked list of request chunks that we have
    call gen_WHOIGET with IHAVE and pass in the linked list
    should return a linked list of packets to send
    use sendto to send the packets
    */
    for (uint8_t i = 0; i < n; i++){
      HASH_FIND(hh, has_chunks, packetinfo->body + CHUNK * i, CHUNK, find);

      if(find) add_node(tmplist, packetinfo->body + CHUNK * i, CHUNK, 0);
    }

    p->tosend = append(gen_WHOIGET(tmplist, 1), p->tosend);
    remove_ll(tmplist);
    break;

  case 1:
    /*
    // IF IHAVE
    extract chunk;
    add chunk to the hash table 'p->has_chunks';
    Lookup chunk in 'get_chunks';
    if that chunk has not yet been requested, change the whohas field to
    identify this peer.
    I'll send a GET to him later.
    I'll put in a timer to retransmit the GET after 5 seconds.
    }
    */
    for (uint8_t i = 0; i < n; i++) {
      find = calloc(1, sizeof(chunk_table));
      memmove(find->chunk, packetinfo->body + CHUNK * i, CHUNK);
      find->id = 0;
      HASH_ADD(hh, p->has_chunks, chunk, CHUNK, find);
    }

    break;

  case 2:
    /* IF GET
       extract the chunk;
       check if we have it;
       If we don't, deny him. eheheh.
       if we do, send it.
    */

    /* Free previous chunk state */
    del_all_nodes(p->tosend);

    p->LPAcked = 0;
    p->LPSent  = 0;
    p->LPAvail = p->LPAcked + p->window;

    n = 1;
    for(uint8_t i = 0; i < n; i++) {
      HASH_FIND(hh, has_chunks, packetinfo->body + CHUNK * i, CHUNK, find);

      if(find) {
        memcpy(tempChunk, packetinfo->body + CHUNK * i, CHUNK);
        p->tosend = append(p->tosend, gen_DATA(tempChunk));
        bzero(tempChunk, CHUNK);

      } else {
        //Generate a denial message.
      }
    }

    break;
    /*
    // IF DATA
    Gonna have to do some flow control logic here;
    extract the data and store it in a 512KB buffer.
    How are we gonna know we recevied the entire chunk of data?
    ANS: use a goddamn bytebuf. when pos = 512KB, we hit gold.
    Perform a checksum afterwards, if badhash, send GET again,
    else write the data into a file using fseek and all.
    */

  case 3:
    //If seq Number != Last Acked + 1, DUPACK
    //Else, copy data into bytebuf
    //Check, complete? If yes, gotcha = True
    //  Perform checksum. If passes, good, else, send GET again
    //Send ACK seqNumber, update Last acked.

    /* Ignore DATA packets from peers whom we haven't requested data from */
    if(!p->busy)
      break;

    seqNumber = (unsigned int) binary2int(packetinfo->sequenceNumber, 4);
    headerLength = binary2int(packetinfo->headerLength, 2);
    totalPacketLength = binary2int(packetinfo->totalPacketLength, 2);

    /* Check if we received a packet we got before */
    if (seqNumber <= p->LPRecv && p->LPRecv > 0) {
      p->tosend = append(gen_ACK(p->LPRecv, 1), p->tosend);
      break;
    }

    /* Check if we received an out of order packet  */
    if(seqNumber > p->LPRecv + 1 && p->LPRecv > 0) {
      p->tosend = append(gen_ACK(p->LPRecv, 3), p->tosend);
      break;
    }
    /* We received the next expected packet */
    else {
      HASH_FIND(hh, get_chunks, p->chunk, HASH_SIZE, find);

      // if(find == NULL)
      // Badstuff

      mmemcat(find->data, packetinfo->body, totalPacketLength - headerLength);

      p->LPRecv = seqNumber;

      if(find->data->pos == CHUNK_SIZE) { //Complete Chunk!

        find->gotcha = 1;
        find->requested = 1;
        p->busy = 0;
        p->LPRecv = 0; // Reset state.

        /* Copying is to ensure consistency within the Hash Table lib */
        chunk_table* copy = duptable(find);

        HASH_ADD(hh, has_chunks, chunk, CHUNK, copy);
        // Check sum here, resend if necessary
        // Save to file;
        save2file(find);
        finished++;
      }
      //Send ACK
      p->tosend = append(gen_ACK(seqNumber, 1), p->tosend);

      if(find->data->pos == CHUNK_SIZE)
        sliding_send(p, sock); // Send 512th ACK immediately.
    }

    break;

  case 4:
    //First, check if this is a DUPACK (use p->LPAcked)
    //If yes, increment dupack counter.
    //  If counter == 3, send approp. packet. Set counter to 0.
    //If not, delete packet from node and send next one.
    //Increment p->LPAcked, p->LPAvail
    //Sliding window stuff.
    ackNumber = (unsigned int) binary2int(packetinfo->ackNumber, 4);

    if(p->LPAcked == ackNumber)
      {
        p->dupCounter++;

        if(p->dupCounter >= 3)
          {
            /* Begin fast retransmit */
            p->dupCounter = 0;

            /* Lengthen time since start */
            p->start_time.tv_sec = 0;
            p->start_time.tv_nsec = 0;
          }
      }
    else
      {
        p->dupCounter = 0;
        delno = ackNumber - p->LPAcked;

        for (int i = 0; i < delno; i++)
          {
            delete_node(p->tosend);
          }

        if(p->window < p->ssthresh) { //slow start
          p->window++;
        } else { //Congestion avoidance
          p->rttcnt++;
          if(p->rttcnt >= p->window){
            p->window++;
            p->rttcnt = 0;
          }
        }
        gen_graph(p);
        if(!p->ttl) computeRTT(p); //Karn Partridge

        p->LPAcked = ackNumber;
        p->LPAvail = p->LPAcked + p->window;
      }

    if(p->LPAcked == 512) // We send 512 packets of a 1000 bytes each.
      {
        // Reset the sliding window state for this peer.
        p->LPAcked = 0;
        p->LPSent  = 0;
        p->LPAvail = p->LPAcked + p->window;
      }

    break;

  case 5:
    // Leave this for now...

    break;

  }
}
Esempio n. 5
0
void mainloop(){

//int init[N*N] = {0,3,8,1000,-4, 1000,0,1000,1,7,1000,4,0,1000,1000,
//2,1000,-5,0,1000,1000,1000,1000,6,0};
   
int nlr,nlc,s,t,i,j,k,l,li,lsize,tsize0, tsize1,tempp,tempoff,rpos,cpos, 
*lpart,*linter,*gindx,*lcol,*lrow,*lsrow, *lscol, *ltrow, *ltcol, *temp;

int* init = gen_graph(N, 0.05);  

bsp_begin(bsp_nprocs());

/**********Initialization SuperStep 0***************/

//Compute global row and column indeces for each element
int pm = sqrt(bsp_nprocs());
int pn = (bsp_nprocs())/pm;
/* Compute 2D processor numbering from 1D numbering 
 with failsafe if the number of processors are not enough, back to simple 1D cyclic distribution */ 
if ( pn  != pm ){
	pn = bsp_nprocs();
	pm = 1;
	t = bsp_pid();
	s = 0;
  
}else{
	s= bsp_pid()%pm;  /* 0 <= s < pm */
	t= bsp_pid()/pn;  /* 0 <= t < pn */
}

nlr=  nloc(pm,s,N); /* number of local rows */
nlc=  nloc(pn,t,N); /* number of local columns */

lsize = nlr*nlc;						  //interpret 2D size to array size
lpart = vecalloci(lsize);				  //Initialize local part of processor s
linter = vecalloci(lsize);				  //Intermidiate array used for the matrix "multiplication"
gindx = vecalloci(lsize);				  //Array to store the global indeces of the local elements
lcol  = vecalloci(lsize);				  //Array to store the glocal column index
lrow  = vecalloci(lsize);				  //Array to store the glocal row index
bsp_push_reg(lpart,lsize*SZINT);

//Distribute the Data
li=0;
for ( i= 0; i < N; i++){
	for ( j= 0; j < N; j++){
		if ((j % pn) == t){
			lpart[li] = init[N*i+j];
			lrow[li] = i;
			lcol[li] = j;
			gindx[li] = N*i+j;
			li++;	
		}
	}
}


/*for ( i= 0; i < N*N; i++) {

		if(bsp_pid() == (i % bsp_nprocs())){
   			lpart[li] = init[i];
			lrow[li] = i/N;
			lcol[li] = i % N;
			gindx[li] = i;
			li++;	
		}
		

}*/
vecfreei(init);//out of the shared space

tsize0 = tsize1 =lsize;
temp = lrow;

//find unique global rows for processor s
for(i=0;i<tsize0;i++){
    for(j=0;j<tsize0;j++){
         if(i==j){
             continue;
         }
         else if(*(temp+i)==*(temp+j)){
             k=j;
             tsize0--;
             while(k < tsize0){
                 *(temp+k)=*(temp+k+1);
                 k++;
             }
              j=0;
         }
    }
}
temp = lcol;

//find unique global column for processor s
for(i=0;i<tsize1;i++){
    for(j=0;j<tsize1;j++){
         if(i==j){
             continue;
         }
         else if(*(temp+i)==*(temp+j)){
             k=j;
             tsize1--;
             while(k < tsize1){
                 *(temp+k)=*(temp+k+1);
                 k++;
             }
              j=0;
         }
    }
}


//keep unique global rows and columns in arrays
//initialize arrays to hold the elements of those rows and columns(ltcol, ltrow)
lscol  = vecalloci(tsize1); 
lsrow  = vecalloci(tsize0);
ltcol  = vecalloci(N*tsize1);
ltrow  = vecalloci(N*tsize0);

for(i=0;i < tsize0;i++){
    lsrow[i] = lrow[i];
  }
for(i=0;i < tsize1;i++){
    lscol[i] = lcol[i];
  }


vecfreei(lcol);//not needed from this point on
vecfreei(lrow);//we use lscol, lsrow, ltrow, ltcol

//sort arrays
qsort (lsrow, tsize0, sizeof(int), compare_int);
qsort (lscol, tsize1, sizeof(int), compare_int);
bsp_sync();
/**********End Initialization SuperStep 0***************/

double time0= bsp_time();
/*********Repeated Squaring loop start*************/
j=1;
while ((N-1) > j) {

/*************Comm. SuperStep j0*************/
for(i=0;i < tsize1;i++){
	for(k=0; k<N;k++){
		tempp=((N*k+lscol[i]) % bsp_nprocs());
		tempoff = ((double)(N*k+lscol[i])/(double)bsp_nprocs());
		bsp_get(tempp, &lpart[0],tempoff*SZINT, &ltcol[N*i+k],SZINT);
	} 
}

for(i=0;i < tsize0;i++){
	for(k=0; k<N;k++){
		tempp=((N*lsrow[i]+k) % bsp_nprocs());
		tempoff = ((double)(N*lsrow[i]+k)/(double)bsp_nprocs());
		bsp_get(tempp, &lpart[0],tempoff*SZINT, &ltrow[N*i+k],SZINT);
	} 
}
bsp_sync();
/*************End Comm. SuperStep j0*************/

/*************Comp. SuperStep j1*************/
for ( i=0; i<lsize; i++) {
  
	int gcol = gindx[i] % N; //get global col indx of current element
	int grow = gindx[i]/N;	 //get global row indx of current element

    linter[i]=1000;//initiliaze array
	//find appropriate indx of the global rows and columns to perform "multiplication"
	/*for ( l=0; l < tsize0;l++){
		if(grow == lsrow[l]){
			rpos =l;
			break;
		}
	}*/
	int *rp = bsearch (&grow, lsrow, tsize0, sizeof (lsrow),compare_int);
	rpos = rp - lsrow;
	

	int *cp = bsearch (&gcol, lscol, tsize1, sizeof (lscol),compare_int);
	cpos = cp - lscol;
	
	/*for ( l=0; l < tsize1;l++){
		if(gcol == lscol[l]){
			cpos =l;
			break;
		}
	}*/

	//this is where the update is done
	for(k=0;k<N;k++){
		linter[i] = fmin(linter[i], ltrow[N*rpos + k]+ltcol[N*cpos + k]);
	}

}

memcpy(lpart,linter,lsize*SZINT);
j = 2*j;
bsp_sync();
/*************End Comp. SuperStep j1*************/

}
/*********Repeated Squaring loop end*************/
double time1= bsp_time();
bsp_sync();
/*********display matrices and time*********/
if(bsp_pid()==0){
	printf( " \n Block Cyclic Distr  calculation of APSP took: %f seconds \n", time1-time0 ); 
}
/*printf("\n The array is, proc %d \n ", bsp_pid());
  for(i=0;i < lsize;i++){
    	printf(" %d",lpart[i]);
	
}*/
printf("\n ");

//clean up
bsp_pop_reg(lpart);
vecfreei(lpart);
vecfreei(linter);
vecfreei(lscol);
vecfreei(lsrow);
vecfreei(ltcol);
vecfreei(ltrow);
vecfreei(gindx);

bsp_end();   
}
Esempio n. 6
0
void mainloop(){

//int init[N*N] = {0,3,8,1000,-4, 1000,0,1000,1,7,1000,4,0,1000,1000,
//2,1000,-5,0,1000,1000,1000,1000,6,0};

int i,j,k,l,v,t,lsize,*lsize_m,*lrow,*lcol, *linit, *linter,*startrow_m;
int li,lj,lk,startrow, endrow,g;

int* init = gen_graph(N, 0.05);  

bsp_begin(bsp_nprocs());


/**********Initialization***************/

/*******Comp. Superstep 0******/

lsize = nloc(bsp_nprocs(),bsp_pid(), N); //Get the number of rows of processor s
lrow = vecalloci(lsize*N);				 //The main storing array of processor s
lcol = vecalloci(N);					 //array to hold the column for the matrix squaring
startrow_m = vecalloci(bsp_nprocs());    //array to hold all processors starting global row
lsize_m = vecalloci(bsp_nprocs());		 //array to hold the number of rows of all processors
linter = vecalloci(lsize*N);			 //Intermidiate array used for the matrix "multiplication"

bsp_push_reg(startrow_m,bsp_nprocs()*SZINT);
bsp_push_reg(lsize_m,bsp_nprocs()*SZINT);
bsp_push_reg(lrow,lsize*N*SZINT);

/****Get the first and last global row of processor s***/
if(bsp_pid() == (bsp_nprocs() - 1)){
 startrow = (N - lsize);
 endrow = N;
}else{
 startrow = bsp_pid()*lsize;
 endrow = bsp_pid()*lsize + lsize;
}



//Distribute Data, according row block distribution
li=0;
for ( i= startrow; i < endrow; i++) {
	lj=0;
	 for(j=0; j < N; j++) {	
   		lrow[N*li+lj] = init[N*i+j];
		lj++;
   	 } 
 li++;
}
vecfreei(init); //out of the shared enviroment

//initialize arrays
for ( i=0; i<bsp_nprocs(); i++) {
			startrow_m[i] = 0;
			lsize_m[i] = 0;
}

bsp_sync();
/*******End Comp. Superstep 0******/


/*********Comm. Superstep 1********/
//Communicate the global starting rows of all processors
for(g=0; g<bsp_nprocs();g++){
	bsp_put(g,&startrow,&startrow_m[0],bsp_pid()*SZINT,SZINT);
	bsp_put(g,&lsize,&lsize_m[0],bsp_pid()*SZINT,SZINT);
}
/*********End Comm. Superstep 1*****/
bsp_sync();
/**********End Initialization***************/

double time0= bsp_time();
/*********Repeated Squaring loop start*************/
j=1;
while ((N-1) > j) {
 
		/****Comp. Superstep j0****/ 
		//initialize arrays
		for ( i=0; i<N*lsize; i++) {
			linter[i] = 1000;
		}
		for ( i=0; i<N; i++) {
			lcol[i] = 0;
		}
		bsp_sync();
		/****End Comp. Superstep j0****/ 
	   		
        	for ( lj=0; lj < N; lj++) {
				/***Comm. SuperStep jlj0*******/
				//get global column lj 
				t=0;
				for(g=0; g < bsp_nprocs();g++){
				  for(v=0; v<lsize_m[g]; v++){				
					bsp_get(g,&lrow[0],(lj+v*N)*SZINT,&lcol[t],SZINT);
					t++;
				  }
				}
				bsp_sync();
				/***End Comm. SuperStep jlj0***/
				/***Comp. SuperStep jlj1*******/
				//update the values that use global column lj
				for ( li = 0; li < lsize; li++){
					for ( lk=0; lk < N; lk++) {
						linter[N*li+lj] = fmin(linter[N*li+lj], lrow[N*li+lk]+lcol[lk]);
					} 
        		}
				bsp_sync();
				/***End Comp. SuperStep jlj1***/
    		}
 		/****Comp. Superstep j1****/ 
		memcpy(lrow,linter,N*lsize*SZINT);
  		j=2*j;
		bsp_sync();
		/****End Comp. Superstep j1****/ 
}
/*********Repeated Squaring loop end*************/
double time1= bsp_time();
bsp_sync();
/*********display matrices and time*********/
if(bsp_pid()==0){
	printf( " \n Block Row Distr (need to know basis) calculation of APSP took: %f seconds \n", time1-time0 ); 
}

/*for(g = 0; g < bsp_nprocs(); g++){
if(bsp_pid()==g){
 printf("\n i am proc %d and i have APSP Mat \n",bsp_pid());
  for(k=0;k<lsize;k++)
     {
	  printf("\n");
		 for(l=0;l<N;l++){
		    printf("\t %d",lrow[N*k+l]);
			  }
			printf("\n \n ");
		}
	}
	bsp_sync();
}*/


//Clean up
bsp_pop_reg(startrow_m);
bsp_pop_reg(lsize_m);
bsp_pop_reg(lrow);


vecfreei(lrow);
vecfreei(lcol);
vecfreei(startrow_m);
vecfreei(lsize_m);
vecfreei(linter);

bsp_end();   
}