Example #1
0
void cal_scalar2()
{
	for(int i=0;i<N;i++)
	{
		select_min(i);
		select_max(i);
	}
	scalar = 0;
	if(N%2==0)
	{
		for(int i=0;i<N/2;i++)
		{
		
			scalar+=a[i]*b[i];
		//	if(i!=N-i-1)
			scalar+=a[N-i-1]*b[N-i-1];
		}
	}
	else
	{
		for(int i=0;i<N/2+1;i++)
	{
		
		scalar+=a[i]*b[i];
		if(i!=N-i-1)
		scalar+=a[N-i-1]*b[N-i-1];
	}
	}
	
	min =scalar;
}
Example #2
0
void map_MPIPP(tm_topology_t *topology,int nb_seed,int N,int *Value,double **comm, double **arch)
{
  int *sol = NULL;
  int *state = NULL;
  double **gain = NULL;
  int **history = NULL;
  double *temp = NULL;
  int i,j,t,l=0,m=0,seed=0;
  double max,sum,best_eval,eval;

  gain = (double**)MALLOC(sizeof(double*)*N);
  history = (int**)MALLOC(sizeof(int*)*N);
  for( i = 0 ; i < N ; i++){
    gain[i] = (double*)MALLOC(sizeof(double)*N);
    history[i] = (int*)MALLOC(sizeof(int)*3);
  }

  state = (int*)MALLOC(sizeof(int)*N);
  temp = (double*)MALLOC(sizeof(double)*N);

  sol = generate_random_sol(topology,N,topology->nb_levels-1,seed++);
  for( i = 0 ; i < N ; i++)
    Value[i] = sol[i];

  best_eval = DBL_MAX;
  while(seed <= nb_seed){
    do{
      for( i =  0 ; i < N ; i++ ){
	state[i] = 0;
	/* printf("%d ",sol[i]); */
      }
      /* printf("\n"); */
      compute_gain(sol,N,gain,comm,arch);
      /*
      display_tab(gain,N);
      exit(-1);
      */
      for( i = 0 ; i < N/2 ; i++ ){
	select_max(&l,&m,gain,N,state);
	/* printf("%d: %d <=> %d : %f\n",i,l,m,gain[l][m]); */
	state[l] = 1;
	state[m] = 1;
	exchange(sol,l,m);
	history[i][1] = l;
	history[i][2] = m;
	temp[i] = gain[l][m];
	compute_gain(sol,N,gain,comm,arch);
      }

      t = -1;
      max = 0;
      sum = 0;
      for(i = 0 ; i < N/2 ; i++ ){
	sum += temp[i];
	if( sum > max ){
	  max = sum;
	  t = i;
	}
      }
      /*for(j=0;j<=t;j++)
	printf("exchanging: %d with %d for gain: %f\n",history[j][1],history[j][2],temp[j]); */
      for( j = t+1 ; j < N/2 ; j++ ){
	exchange(sol,history[j][1],history[j][2]);
	/* printf("Undoing: %d with %d for gain: %f\n",history[j][1],history[j][2],temp[j]);  */
      }
      /* printf("max=%f\n",max); */

      /*for(i=0;i<N;i++){
	printf("%d ",sol[i]);
	}
	printf("\n");*/
      eval = eval_sol(sol,N,comm,arch);
      if(eval < best_eval){
	best_eval = eval;
	for(i = 0 ; i < N ; i++)
	  Value[i] = sol[i];
	/* print_sol(N); */
      }
    }while( max > 0 );

    FREE(sol);
    sol=generate_random_sol(topology,N,topology->nb_levels-1,seed++);
  }
  FREE(sol);
  FREE(temp);
  FREE(state);
  for( i = 0 ; i < N ; i++){
    FREE(gain[i]);
    FREE(history[i]);
  }
  FREE(gain);
  FREE(history);
}
Example #3
0
 void map_MPIPP(int nb_seed,int N,int *Value,int **comm, int **arch){
   int *sol;
   int *state;
   double **gain;
   int **history;
   double *temp;
   int i,j,t,l=0,m=0,loop=0,seed=0;
   double max,sum,best_eval,eval;


   gain=(double**)malloc(sizeof(double*)*N);
   for(i=0;i<N;i++){
     gain[i]=(double*)malloc(sizeof(double)*N);
     if(!gain[i]){
     }
   }
   history=(int**)malloc(sizeof(int*)*N);
   for(i=0;i<N;i++)
     history[i]=(int*)malloc(sizeof(int)*3);

   state=(int*)malloc(sizeof(int)*N);
   temp=(double*)malloc(sizeof(double)*N);

   sol=generate_random_sol(N,0,seed++);
   for(i=0;i<N;i++)
     Value[i]=sol[i];

   best_eval=DBL_MAX;
while(seed<=nb_seed){
     loop=0;
     do{

       for(i=0;i<N;i++){
         state[i]=0;
         PRINTF(("%d ",sol[i]));
       }
       PRINTF(("\n"));
       compute_gain(sol,N,gain,comm,arch);

/*	for(i=0;i<N;i++){
             for(j=0;j<N;j++){
                printf("%d ",comm[i][j]);
        }
        printf("\n");
        }
	for(i=0;i<N;i++){
             for(j=0;j<N;j++){
                printf("%6.1f ",gain[i][j]);
        }
    	printf("\n");
        }
*/
//      display_tab(gain,N);
      //exit(-1);
       for(i=0;i<N/2;i++){
         select_max(&l,&m,gain,N,state);
         PRINTF(("%d: %d <=> %d : %f\n",i,l,m,gain[l][m]));
         state[l]=1;state[m]=1;
         exchange(sol,l,m);
	 for(j=0;j<N;j++){
                PRINTF(("%d ",sol[j]));
       }
	PRINTF(("\n"));
         history[i][1]=l;history[i][2]=m;
         temp[i]=gain[l][m];
         compute_gain(sol,N,gain,comm,arch);
       }

       t=-1;
       max=0;
       sum=0;
       for(i=0;i<N/2;i++){
         sum+=temp[i];
         if(sum>max){
           max=sum;
           t=i;
         }
       }
       for(j=0;j<=t;j++)
         PRINTF(("exchanging: %d with %d for gain: %f\n",history[j][1],history[j][2],temp[j])); 
       for(j=t+1;j<N/2;j++){
         exchange(sol,history[j][1],history[j][2]);
         PRINTF(("Undoing: %d with %d for gain: %f\n",history[j][1],history[j][2],temp[j]));
       }
       PRINTF(("max=%f\n",max));
      for(i=0;i<N;i++){
         PRINTF(("%d ",sol[i]));
         }
         PRINTF(("\n"));
       eval=eval_sol(sol,N,comm,arch);
       if(eval<best_eval){
         best_eval=eval;
         for(i=0;i<N;i++)
           Value[i]=sol[i];
       }


     }while(max>0);

     sol=generate_random_sol(N,0,seed++);

   }

   if (NULL != gain){
        for (i =0; i< N; i++){
            if (gain[i] != NULL){
                free(gain[i]);
            }
        }
        free(gain);
    }
    if (NULL != history){
        for (i =0; i< N; i++){
            if (history[i] != NULL){
                free(history[i]);
            }
        }
        free(history);
    }


 }