vic/drivers/shared_image/src/vic_mpi_support.c
void
gather_put_nc_field_schar(int     nc_id,
                          int     var_id,
                          char    fillval,
                          size_t *start,
                          size_t *count,
                          char   *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;
    signed char         *cvar = NULL;
    signed char         *cvar_gathered = NULL;
    signed char         *cvar_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;
        cvar = malloc(grid_size * sizeof(*cvar));
        check_alloc_status(cvar, "Memory allocation error.");

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

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


        cvar_remapped =
            malloc(global_domain.ncells_active * sizeof(*cvar_remapped));
        check_alloc_status(cvar_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_CHAR,
                         cvar_gathered, mpi_map_local_array_sizes,
                         mpi_map_global_array_offsets, MPI_CHAR,
                         VIC_MPI_ROOT, MPI_COMM_VIC);
    check_mpi_status(status, "MPI error.");

    if (mpi_rank == VIC_MPI_ROOT) {
        // remap the array
        map(sizeof(char), global_domain.ncells_active, NULL,
            mpi_map_mapping_array,
            cvar_gathered, cvar_remapped);
        // expand to full grid size
        map(sizeof(char), global_domain.ncells_active, NULL,
            filter_active_cells, cvar_remapped, cvar);
        // write to file
        status = nc_put_vara_schar(nc_id, var_id, start, count, cvar);
        check_nc_status(status, "Error writing values");

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