vic/drivers/shared_image/src/vic_mpi_support.c
void
gather_put_nc_field_int(int     nc_id,
                        int     var_id,
                        int     fillval,
                        size_t *start,
                        size_t *count,
                        int    *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;
    int                 *ivar = NULL;
    int                 *ivar_gathered = NULL;
    int                 *ivar_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;
        ivar = malloc(grid_size * sizeof(*ivar));
        check_alloc_status(ivar, "Memory allocation error.");

        for (i = 0; i < grid_size; i++) {
            ivar[i] = fillval;
        }

        ivar_gathered =
            malloc(global_domain.ncells_active * sizeof(*ivar_gathered));
        check_alloc_status(ivar_gathered, "Memory allocation error.");


        ivar_remapped =
            malloc(global_domain.ncells_active * sizeof(*ivar_remapped));
        check_alloc_status(ivar_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_INT,
                         ivar_gathered, mpi_map_local_array_sizes,
                         mpi_map_global_array_offsets, MPI_INT,
                         VIC_MPI_ROOT, MPI_COMM_VIC);
    check_mpi_status(status, "MPI error.");

    if (mpi_rank == VIC_MPI_ROOT) {
        // remap the array
        map(sizeof(int), global_domain.ncells_active, NULL,
            mpi_map_mapping_array,
            ivar_gathered, ivar_remapped);
        // expand to full grid size
        map(sizeof(int), global_domain.ncells_active, NULL, filter_active_cells,
            ivar_remapped, ivar);
        // write to file
        status = nc_put_vara_int(nc_id, var_id, start, count, ivar);
        check_nc_status(status, "Error writing values");

        // cleanup
        free(ivar);
        free(ivar_gathered);
        free(ivar_remapped);
    }
}
/******************************************************************************
 * @brief   Gather and write integer NetCDF field
 * @details Values are gathered to the master node and then written from the
 *          master node
 *****************************************************************************/
@brief   Gather and write integer NetCDF field
@details Values are gathered to the master node and then written from the
master node
@brief Gather and write integer NetCDF field @details Values are gathered to the master node and then written from the master node
void
gather_put_nc_field_int(int     nc_id,
                        int     var_id,
                        int     fillval,
                        size_t *start,
                        size_t *count,
                        int    *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;
    int                 *ivar = NULL;
    int                 *ivar_gathered = NULL;
    int                 *ivar_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;
        ivar = malloc(grid_size * sizeof(*ivar));
        check_alloc_status(ivar, "Memory allocation error.");
        for (i = 0; i < grid_size; i++) {
            ivar[i] = fillval;
        }
        ivar_gathered =
            malloc(global_domain.ncells_active * sizeof(*ivar_gathered));
        check_alloc_status(ivar_gathered, "Memory allocation error.");
        ivar_remapped =
            malloc(global_domain.ncells_active * sizeof(*ivar_remapped));
        check_alloc_status(ivar_remapped, "Memory allocation error.");
    }
    status = MPI_Gatherv(var, local_domain.ncells_active, MPI_INT,
                         ivar_gathered, mpi_map_local_array_sizes,
                         mpi_map_global_array_offsets, MPI_INT,
                         VIC_MPI_ROOT, MPI_COMM_VIC);
    check_mpi_status(status, "MPI error.");
    if (mpi_rank == VIC_MPI_ROOT) {
        map(sizeof(int), global_domain.ncells_active, NULL,
            mpi_map_mapping_array,
            ivar_gathered, ivar_remapped);
        map(sizeof(int), global_domain.ncells_active, NULL, filter_active_cells,
            ivar_remapped, ivar);
        status = nc_put_vara_int(nc_id, var_id, start, count, ivar);
        check_nc_status(status, "Error writing values");
        free(ivar);
        free(ivar_gathered);
        free(ivar_remapped);
    }
}
void gather_put_nc_field_int ( int nc_id , int var_id , int fillval , size_t * start , size_t * count , int * 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 ; int * ivar = NULL ; int * ivar_gathered = NULL ; int * ivar_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 ; ivar = malloc ( grid_size * sizeof ( * ivar ) ) ; check_alloc_status ( ivar , " " ) ; for ( i = 0 ; i < grid_size ; i ++ ) { ivar [ i ] = fillval ; } ivar_gathered = malloc ( global_domain . ncells_active * sizeof ( * ivar_gathered ) ) ; check_alloc_status ( ivar_gathered , " " ) ; ivar_remapped = malloc ( global_domain . ncells_active * sizeof ( * ivar_remapped ) ) ; check_alloc_status ( ivar_remapped , " " ) ; } status = MPI_Gatherv ( var , local_domain . ncells_active , MPI_INT , ivar_gathered , mpi_map_local_array_sizes , mpi_map_global_array_offsets , MPI_INT , VIC_MPI_ROOT , MPI_COMM_VIC ) ; check_mpi_status ( status , " " ) ; if ( mpi_rank == VIC_MPI_ROOT ) { map ( sizeof ( int ) , global_domain . ncells_active , NULL , mpi_map_mapping_array , ivar_gathered , ivar_remapped ) ; map ( sizeof ( int ) , global_domain . ncells_active , NULL , filter_active_cells , ivar_remapped , ivar ) ; status = nc_put_vara_int ( nc_id , var_id , start , count , ivar ) ; check_nc_status ( status , " " ) ; free ( ivar ) ; free ( ivar_gathered ) ; free ( ivar_remapped ) ; } }
@brief   Gather and write integer NetCDF field
 @details Values are gathered to the master node and then written from the
          master node
@brief Gather and write integer 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