示例#1
0
文件: tourn.c 项目: rbs11/spring14
void tournament_barrier(int *my_id, int *num_processes, int round)
{
	int id = *my_id;
	int nprocs = *num_processes;
	int winner = 0;
	int bufval = 0, src, dest;
	MPI_Request rec_handle;


	print_dbg("Round %d Id %d", round, id);
	if (round > ceil(log2(nprocs))){
		print_dbg("Exit\n");
		return;
	}
	print_dbg("\n");
	if ((id % (int)pow(2,round)) == 0)
	{
		winner = 1;
		src = id + (int)pow(2,round-1);
		if (src >= nprocs){
			print_dbg("Bye for id %d, ignore %d and move up\n", id, src);
			tournament_barrier(my_id, num_processes, round+1);
		}
		else{
			MPI_Irecv( &bufval, 1 ,MPI_INT, src, 1, MPI_COMM_WORLD, &rec_handle );
			MPI_Wait(&rec_handle,NULL);
			print_dbg("Winner %d woken up by <%d>\n", id, src);
			tournament_barrier(my_id, num_processes, round+1);
			/*Wake up corresponding loser*/
			MPI_Send( &bufval, 1, MPI_INT, src, 1 , MPI_COMM_WORLD );
			print_dbg("Winner %d shakes hand with loser <%d>\n", id, src);
		}
	}
	else
	{
		winner = 0;
		dest = id - (int)pow(2,round-1);
		print_dbg("Loser <%d> wake up Winner %d\n", id, dest);
		MPI_Send( &bufval, 1, MPI_INT, dest, 1 , MPI_COMM_WORLD );
		/*Wait for wake up by winner*/
		MPI_Irecv( &bufval, 1 ,MPI_INT, dest, 1, MPI_COMM_WORLD, &rec_handle );
		MPI_Wait(&rec_handle,NULL);
		print_dbg("Loser %d woken up by <%d>\n", id, dest);
	}
}
int main(int argc, char* argv[])
{
	if (argc == 3){
        if (sscanf (argv[1], "%d", &P)!=1)
        	printf ("P - not an integer\n");
        if (sscanf (argv[2], "%d", &N)!=1)
        	printf ("N - not an integer\n");
    }
    else{
    	//Number of processors
		P = 4;
		//Number of loops
		N = 5;
    }
    struct timeval tv1, tv2;
    double total_time;
	int pid_sense = 1;
	barrier_init();
	omp_set_num_threads(P);
	gettimeofday(&tv1, NULL);
	#pragma omp parallel shared (players,N) firstprivate(pid_sense)
	{
		int i;
		for(i=0;i<N;i++)
		{
			// printf("==============BARRIER %d=================\n", i);
			tournament_barrier(&pid_sense);
			tournament_barrier(&pid_sense);
			tournament_barrier(&pid_sense);
			tournament_barrier(&pid_sense);
			tournament_barrier(&pid_sense);
		}	
	}
	gettimeofday(&tv2, NULL);
	total_time = (double) (tv2.tv_usec - tv1.tv_usec) + (double) (tv2.tv_sec - tv1.tv_sec)*1000000;
    printf("\nSUMMARY:\nTotal run-time for %d "
            "loops with 5 barriers per loop: %fs\n"
            "The average time per barrier: %fus\n",
            N, total_time/1000000, (double)(total_time/(N*5)));
	free(players);
	return 0;
}
示例#3
0
文件: tourn.c 项目: rbs11/spring14
int main(int argc, char **argv)
{
  int my_id, num_processes, i;
  double tstart, tend, tfinal;

  MPI_Init(&argc, &argv);

  MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
  if (my_id == 0)
	  tstart = MPI_Wtime();
  for( i=0; i < NUM_BARRIERS; i++ ){

	  tournament_barrier(&my_id, &num_processes, 1);
	  print_dbg("Process %d completed barrier %d\n", my_id, i);
  }
  if (my_id == 0){
	  tfinal = MPI_Wtime();
	  tend =  (double)((tfinal - tstart)/NUM_BARRIERS);
	  printf("[Tournament]Time for %d Processes [%f]\n", num_processes, tend);
  }
  MPI_Finalize();
  return 0;
}
int main(int argc, char **argv)
{
  int my_id, my_dst, my_src, num_processes;
  int i,j,k;
  int test;
  char ch;

  MPI_Init(&argc, &argv);
  int count;
  int myrank;
  struct timeval tv;
  double curr_time_s;
  double curr_time_us;
  struct timeval tv1, tv2;
  int N=100000;
  double total_time;
  
  tournament_barrier_init();
  MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
  
  if(rank == 0)
  {
    gettimeofday(&tv1, NULL);
  }

  for(k=0;k<N;k++)
  {
    
  /*  fflush(stdout);
    gettimeofday(&tv, NULL);
    curr_time_s=(double) tv.tv_usec + (double) tv.tv_sec*1000000;
    printf("Barrier %d reached by %d at time %f\n", count++, myrank,curr_time_s); */
    tournament_barrier();

  /*  fflush(stdout);
    gettimeofday(&tv, NULL);
    curr_time_s=(double) tv.tv_usec + (double) tv.tv_sec*1000000;
    printf("Barrier %d reached by %d at time %f\n", count++, myrank,curr_time_s); */

    tournament_barrier();
  /*  fflush(stdout);
    gettimeofday(&tv, NULL);
    curr_time_s=(double) tv.tv_usec + (double) tv.tv_sec*1000000;
    printf("Barrier %d reached by %d at time %f\n", count++, myrank,curr_time_s);
  */  
    tournament_barrier();
  
  /*  fflush(stdout);
    gettimeofday(&tv, NULL);
    curr_time_s=(double) tv.tv_usec + (double) tv.tv_sec*1000000;
    printf("Barrier %d reached by %d at time %f\n", count++, myrank,curr_time_s);
  */
    tournament_barrier();

    tournament_barrier();

  }

  if(rank==0)
  {
    gettimeofday(&tv2, NULL);

    total_time = (double) (tv2.tv_usec - tv1.tv_usec) + (double) (tv2.tv_sec - tv1.tv_sec)*1000000;
    printf("\nSUMMARY:\nNumber of processes: %d\n Total run-time for %d "
            "loops with 5 barriers per loop: %fs\n"
            "The average time per barrier: %fus\n",
            P, N, total_time/1000000, (double)(total_time/(N*5)));
  }
  
 tournament_barrier_finish();

  MPI_Finalize();
//  while(1);
  return 0;
}