vic/drivers/shared_image/src/vic_mpi_support.c
void
gather_put_nc_field_float(int     nc_id,
                          int     var_id,
                          float   fillval,
                          size_t *start,
                          size_t *count,
                          float  *var)
{
    extern MPI_Comm      MPI_COMM_VIC;
    extern domain_struct global_domain;
    extern domain_struct local_domain;
    extern int           mpi_rank;
    extern int          *mpi_map_global_array_offsets;
    extern int          *mpi_map_local_array_sizes;
    extern size_t       *filter_active_cells;
    extern size_t       *mpi_map_mapping_array;
    int                  status;
    float               *fvar = NULL;
    float               *fvar_gathered = NULL;
    float               *fvar_remapped = NULL;
    size_t               grid_size;
    size_t               i;

    if (mpi_rank == VIC_MPI_ROOT) {
        grid_size = global_domain.n_nx * global_domain.n_ny;
        fvar = malloc(grid_size * sizeof(*fvar));
        check_alloc_status(fvar, "Memory allocation error.");
        for (i = 0; i < grid_size; i++) {
            fvar[i] = fillval;
        }
        fvar_gathered =
            malloc(global_domain.ncells_active * sizeof(*fvar_gathered));
        check_alloc_status(fvar_gathered, "Memory allocation error.");

        fvar_remapped =
            malloc(global_domain.ncells_active * sizeof(*fvar_remapped));
        check_alloc_status(fvar_remapped, "Memory allocation error.");
    }
    // Gather the results from the nodes, result for the local node is in the
    // array *var (which is a function argument)
    status = MPI_Gatherv(var, local_domain.ncells_active, MPI_FLOAT,
                         fvar_gathered, mpi_map_local_array_sizes,
                         mpi_map_global_array_offsets, MPI_FLOAT,
                         VIC_MPI_ROOT, MPI_COMM_VIC);
    check_mpi_status(status, "Error with gather of floats");

    if (mpi_rank == VIC_MPI_ROOT) {
        // remap the array
        map(sizeof(float), global_domain.ncells_active, NULL,
            mpi_map_mapping_array, fvar_gathered, fvar_remapped);
        // expand to full grid size
        map(sizeof(float), global_domain.ncells_active, NULL,
            filter_active_cells, fvar_remapped, fvar);

        // write to file
        status = nc_put_vara_float(nc_id, var_id, start, count, fvar);
        check_nc_status(status, "Error writing values");

        // cleanup
        free(fvar);
        free(fvar_gathered);
        free(fvar_remapped);
    }
}
/******************************************************************************
 * @brief   Gather and write double precision NetCDF field
 * @details Values are gathered to the master node and then written from the
 *          master node
 *****************************************************************************/
@brief   Gather and write double precision NetCDF field
@details Values are gathered to the master node and then written from the
master node
@brief Gather and write double precision NetCDF field @details Values are gathered to the master node and then written from the master node
void
gather_put_nc_field_float(int     nc_id,
                          int     var_id,
                          float   fillval,
                          size_t *start,
                          size_t *count,
                          float  *var)
{
    extern MPI_Comm      MPI_COMM_VIC;
    extern domain_struct global_domain;
    extern domain_struct local_domain;
    extern int           mpi_rank;
    extern int          *mpi_map_global_array_offsets;
    extern int          *mpi_map_local_array_sizes;
    extern size_t       *filter_active_cells;
    extern size_t       *mpi_map_mapping_array;
    int                  status;
    float               *fvar = NULL;
    float               *fvar_gathered = NULL;
    float               *fvar_remapped = NULL;
    size_t               grid_size;
    size_t               i;
    if (mpi_rank == VIC_MPI_ROOT) {
        grid_size = global_domain.n_nx * global_domain.n_ny;
        fvar = malloc(grid_size * sizeof(*fvar));
        check_alloc_status(fvar, "Memory allocation error.");
        for (i = 0; i < grid_size; i++) {
            fvar[i] = fillval;
        }
        fvar_gathered =
            malloc(global_domain.ncells_active * sizeof(*fvar_gathered));
        check_alloc_status(fvar_gathered, "Memory allocation error.");
        fvar_remapped =
            malloc(global_domain.ncells_active * sizeof(*fvar_remapped));
        check_alloc_status(fvar_remapped, "Memory allocation error.");
    }
    status = MPI_Gatherv(var, local_domain.ncells_active, MPI_FLOAT,
                         fvar_gathered, mpi_map_local_array_sizes,
                         mpi_map_global_array_offsets, MPI_FLOAT,
                         VIC_MPI_ROOT, MPI_COMM_VIC);
    check_mpi_status(status, "Error with gather of floats");
    if (mpi_rank == VIC_MPI_ROOT) {
        map(sizeof(float), global_domain.ncells_active, NULL,
            mpi_map_mapping_array, fvar_gathered, fvar_remapped);
        map(sizeof(float), global_domain.ncells_active, NULL,
            filter_active_cells, fvar_remapped, fvar);
        status = nc_put_vara_float(nc_id, var_id, start, count, fvar);
        check_nc_status(status, "Error writing values");
        free(fvar);
        free(fvar_gathered);
        free(fvar_remapped);
    }
}
void gather_put_nc_field_float ( int nc_id , int var_id , float fillval , size_t * start , size_t * count , float * var ) { extern MPI_Comm MPI_COMM_VIC ; extern domain_struct global_domain ; extern domain_struct local_domain ; extern int mpi_rank ; extern int * mpi_map_global_array_offsets ; extern int * mpi_map_local_array_sizes ; extern size_t * filter_active_cells ; extern size_t * mpi_map_mapping_array ; int status ; float * fvar = NULL ; float * fvar_gathered = NULL ; float * fvar_remapped = NULL ; size_t grid_size ; size_t i ; if ( mpi_rank == VIC_MPI_ROOT ) { grid_size = global_domain . n_nx * global_domain . n_ny ; fvar = malloc ( grid_size * sizeof ( * fvar ) ) ; check_alloc_status ( fvar , " " ) ; for ( i = 0 ; i < grid_size ; i ++ ) { fvar [ i ] = fillval ; } fvar_gathered = malloc ( global_domain . ncells_active * sizeof ( * fvar_gathered ) ) ; check_alloc_status ( fvar_gathered , " " ) ; fvar_remapped = malloc ( global_domain . ncells_active * sizeof ( * fvar_remapped ) ) ; check_alloc_status ( fvar_remapped , " " ) ; } status = MPI_Gatherv ( var , local_domain . ncells_active , MPI_FLOAT , fvar_gathered , mpi_map_local_array_sizes , mpi_map_global_array_offsets , MPI_FLOAT , VIC_MPI_ROOT , MPI_COMM_VIC ) ; check_mpi_status ( status , " " ) ; if ( mpi_rank == VIC_MPI_ROOT ) { map ( sizeof ( float ) , global_domain . ncells_active , NULL , mpi_map_mapping_array , fvar_gathered , fvar_remapped ) ; map ( sizeof ( float ) , global_domain . ncells_active , NULL , filter_active_cells , fvar_remapped , fvar ) ; status = nc_put_vara_float ( nc_id , var_id , start , count , fvar ) ; check_nc_status ( status , " " ) ; free ( fvar ) ; free ( fvar_gathered ) ; free ( fvar_remapped ) ; } }
@brief   Gather and write double precision NetCDF field
 @details Values are gathered to the master node and then written from the
          master node
@brief Gather and write double precision NetCDF field @details Values are gathered to the master node and then written from the master node
// Gather the results from the nodes, result for the local node is in the // array *var (which is a function argument) // remap the array // expand to full grid size // write to file // cleanup