src/ffs/ffs_direct.c
static int ffs_direct_close_up(ffs_ensemble_t * old, ffs_ensemble_t * new,
			       ffs_trial_arg_t * trial, int interface) {

  ffs_ensemble_t * list = NULL;
  int * list_nsuccess = NULL;
  int * displs = NULL;
  int nsuccess;
  int nexcess;
  int pid;
  int n, ntmp;

  const char * stub = NULL;

  dbg_return_if(old == NULL, -1);
  dbg_return_if(new == NULL, -1);

  dbg_err_if( proxy_id(trial->proxy, &pid) );

  /* Form the global list of successful trials from the (local) old ensemble */

  list_nsuccess = u_calloc(trial->nproxy, sizeof(int));
  dbg_err_if(list_nsuccess == NULL);

  MPI_Allgather(&old->nsuccess, 1, MPI_INT, list_nsuccess, 1, MPI_INT,
		trial->xcomm);

  nsuccess = 0;
  for (n = 0; n < trial->nproxy; n++) {
    nsuccess += list_nsuccess[n];
  }

  displs = u_calloc(trial->nproxy, sizeof(int));
  dbg_err_if(displs == NULL);
  dbg_err_if( ffs_ensemble_create(nsuccess, &list) );

  displs[0] = 0;
  for (n = 1; n < trial->nproxy; n++) {
    displs[n] = displs[n-1] + list_nsuccess[n-1];
  }

  MPI_Allgatherv(old->traj, old->nsuccess, MPI_INT,
		 list->traj, list_nsuccess, displs, MPI_INT, trial->xcomm);

  MPI_Allgatherv(old->wt, old->nsuccess, MPI_DOUBLE,
		 list->wt, list_nsuccess, displs, MPI_DOUBLE, trial->xcomm);

  /* Delete excess. Only one proxy is required to delete the files, but
   * all instance ranks delete their record of the state. */

  nexcess = nsuccess - new->nmax;

  for (n = 0; n < nexcess; n += 1) {
    ntmp = n*(nsuccess/nexcess);
    if (pid == 0) {
      stub = util_filename_stub(trial->inst_id, interface, list->traj[ntmp]);
      proxy_state(trial->proxy, SIM_STATE_DELETE, stub);
    }
    /* These are skipped in the next loop */
    list->traj[ntmp] = -1;
  }

  new->nsuccess = 0;
  for (n = 0; n < nsuccess; n++) {
    if (list->traj[n] != -1) {
      new->traj[new->nsuccess] = list->traj[n];
      new->wt[new->nsuccess] = list->traj[n];
      new->nsuccess += 1;
    }
  }

  u_free(displs);
  u_free(list_nsuccess);
  ffs_ensemble_free(list);

  return 0;

 err:

  mpilog(trial->log, "Problem in closing up states (maybe deadlock!)\n");

  if (displs) u_free(displs);
  if (list_nsuccess) u_free(list_nsuccess);
  if (list) ffs_ensemble_free(list);

  return  -1;
}
/*****************************************************************************
 *
 *  ffs_direct_close_up
 *
 *  Form a global list of successes in the instance cross comminucator,
 *  add up the total number of successes, and form a global list. If
 *  the number of successes is greater than the number of states
 *  required, we delete the excess.
 *
 *****************************************************************************/
ffs_direct_close_up
Form a global list of successes in the instance cross comminucator,
add up the total number of successes, and form a global list. If
the number of successes is greater than the number of states
required, we delete the excess.
ffs_direct_close_up Form a global list of successes in the instance cross comminucator add up the total number of successes and form a global list . If the number of successes is greater than the number of states required we delete the excess .
static int ffs_direct_close_up(ffs_ensemble_t * old, ffs_ensemble_t * new,
			       ffs_trial_arg_t * trial, int interface) {
  ffs_ensemble_t * list = NULL;
  int * list_nsuccess = NULL;
  int * displs = NULL;
  int nsuccess;
  int nexcess;
  int pid;
  int n, ntmp;
  const char * stub = NULL;
  dbg_return_if(old == NULL, -1);
  dbg_return_if(new == NULL, -1);
  dbg_err_if( proxy_id(trial->proxy, &pid) );
  list_nsuccess = u_calloc(trial->nproxy, sizeof(int));
  dbg_err_if(list_nsuccess == NULL);
  MPI_Allgather(&old->nsuccess, 1, MPI_INT, list_nsuccess, 1, MPI_INT,
		trial->xcomm);
  nsuccess = 0;
  for (n = 0; n < trial->nproxy; n++) {
    nsuccess += list_nsuccess[n];
  }
  displs = u_calloc(trial->nproxy, sizeof(int));
  dbg_err_if(displs == NULL);
  dbg_err_if( ffs_ensemble_create(nsuccess, &list) );
  displs[0] = 0;
  for (n = 1; n < trial->nproxy; n++) {
    displs[n] = displs[n-1] + list_nsuccess[n-1];
  }
  MPI_Allgatherv(old->traj, old->nsuccess, MPI_INT,
		 list->traj, list_nsuccess, displs, MPI_INT, trial->xcomm);
  MPI_Allgatherv(old->wt, old->nsuccess, MPI_DOUBLE,
		 list->wt, list_nsuccess, displs, MPI_DOUBLE, trial->xcomm);
  nexcess = nsuccess - new->nmax;
  for (n = 0; n < nexcess; n += 1) {
    ntmp = n*(nsuccess/nexcess);
    if (pid == 0) {
      stub = util_filename_stub(trial->inst_id, interface, list->traj[ntmp]);
      proxy_state(trial->proxy, SIM_STATE_DELETE, stub);
    }
    list->traj[ntmp] = -1;
  }
  new->nsuccess = 0;
  for (n = 0; n < nsuccess; n++) {
    if (list->traj[n] != -1) {
      new->traj[new->nsuccess] = list->traj[n];
      new->wt[new->nsuccess] = list->traj[n];
      new->nsuccess += 1;
    }
  }
  u_free(displs);
  u_free(list_nsuccess);
  ffs_ensemble_free(list);
  return 0;
 err:
  mpilog(trial->log, "Problem in closing up states (maybe deadlock!)\n");
  if (displs) u_free(displs);
  if (list_nsuccess) u_free(list_nsuccess);
  if (list) ffs_ensemble_free(list);
  return  -1;
}
static int ffs_direct_close_up ( ffs_ensemble_t * old , ffs_ensemble_t * new , ffs_trial_arg_t * trial , int interface ) { ffs_ensemble_t * list = NULL ; int * list_nsuccess = NULL ; int * displs = NULL ; int nsuccess ; int nexcess ; int pid ; int n , ntmp ; const char * stub = NULL ; dbg_return_if ( old == NULL , -1 ) ; dbg_return_if ( new == NULL , -1 ) ; dbg_err_if ( proxy_id ( trial -> proxy , & pid ) ) ; list_nsuccess = u_calloc ( trial -> nproxy , sizeof ( int ) ) ; dbg_err_if ( list_nsuccess == NULL ) ; MPI_Allgather ( & old -> nsuccess , 1 , MPI_INT , list_nsuccess , 1 , MPI_INT , trial -> xcomm ) ; nsuccess = 0 ; for ( n = 0 ; n < trial -> nproxy ; n ++ ) { nsuccess += list_nsuccess [ n ] ; } displs = u_calloc ( trial -> nproxy , sizeof ( int ) ) ; dbg_err_if ( displs == NULL ) ; dbg_err_if ( ffs_ensemble_create ( nsuccess , & list ) ) ; displs [ 0 ] = 0 ; for ( n = 1 ; n < trial -> nproxy ; n ++ ) { displs [ n ] = displs [ n - 1 ] + list_nsuccess [ n - 1 ] ; } MPI_Allgatherv ( old -> traj , old -> nsuccess , MPI_INT , list -> traj , list_nsuccess , displs , MPI_INT , trial -> xcomm ) ; MPI_Allgatherv ( old -> wt , old -> nsuccess , MPI_DOUBLE , list -> wt , list_nsuccess , displs , MPI_DOUBLE , trial -> xcomm ) ; nexcess = nsuccess - new -> nmax ; for ( n = 0 ; n < nexcess ; n += 1 ) { ntmp = n * ( nsuccess / nexcess ) ; if ( pid == 0 ) { stub = util_filename_stub ( trial -> inst_id , interface , list -> traj [ ntmp ] ) ; proxy_state ( trial -> proxy , SIM_STATE_DELETE , stub ) ; } list -> traj [ ntmp ] = -1 ; } new -> nsuccess = 0 ; for ( n = 0 ; n < nsuccess ; n ++ ) { if ( list -> traj [ n ] != -1 ) { new -> traj [ new -> nsuccess ] = list -> traj [ n ] ; new -> wt [ new -> nsuccess ] = list -> traj [ n ] ; new -> nsuccess += 1 ; } } u_free ( displs ) ; u_free ( list_nsuccess ) ; ffs_ensemble_free ( list ) ; return 0 ; err : mpilog ( trial -> log , " \n " ) ; if ( displs ) u_free ( displs ) ; if ( list_nsuccess ) u_free ( list_nsuccess ) ; if ( list ) ffs_ensemble_free ( list ) ; return -1 ; }
ffs_direct_close_up
  Form a global list of successes in the instance cross comminucator,
  add up the total number of successes, and form a global list.
ffs_direct_close_up Form a global list of successes in the instance cross comminucator add up the total number of successes and form a global list .
/* Form the global list of successful trials from the (local) old ensemble */ /* Delete excess. Only one proxy is required to delete the files, but
   * all instance ranks delete their record of the state. */ /* These are skipped in the next loop */