vic/drivers/shared_image/src/vic_mpi_support.c
void
create_MPI_global_struct_type(MPI_Datatype *mpi_type)
{
    extern MPI_Comm MPI_COMM_VIC;
    int             nitems; // number of elements in struct
    int             status;
    int            *blocklengths;
    size_t          i;
    MPI_Aint       *offsets;
    MPI_Datatype   *mpi_types;

    // nitems has to equal the number of elements in global_param_struct
    nitems = 32;
    blocklengths = malloc(nitems * sizeof(*blocklengths));
    check_alloc_status(blocklengths, "Memory allocation error.");

    offsets = malloc(nitems * sizeof(*offsets));
    check_alloc_status(offsets, "Memory allocation error.");

    mpi_types = malloc(nitems * sizeof(*mpi_types));
    check_alloc_status(mpi_types, "Memory allocation error.")

    // most of the elements in global_param_struct are not arrays. Use 1 as
    // the default block length and reset as needed
    for (i = 0; i < (size_t) nitems; i++) {
        blocklengths[i] = 1;
    }

    // reset i
    i = 0;

    // double wind_h;
    offsets[i] = offsetof(global_param_struct, wind_h);
    mpi_types[i++] = MPI_DOUBLE;

    // double resolution;
    offsets[i] = offsetof(global_param_struct, resolution);
    mpi_types[i++] = MPI_DOUBLE;

    // double dt;
    offsets[i] = offsetof(global_param_struct, dt);
    mpi_types[i++] = MPI_DOUBLE;

    // double snow_dt;
    offsets[i] = offsetof(global_param_struct, snow_dt);
    mpi_types[i++] = MPI_DOUBLE;

    // double runoff_dt;
    offsets[i] = offsetof(global_param_struct, runoff_dt);
    mpi_types[i++] = MPI_DOUBLE;

    // double atmos_dt;
    offsets[i] = offsetof(global_param_struct, atmos_dt);
    mpi_types[i++] = MPI_DOUBLE;

    // size_t model_steps_per_day;
    offsets[i] = offsetof(global_param_struct, model_steps_per_day);
    mpi_types[i++] = MPI_AINT;

    // size_t snow_steps_per_day;
    offsets[i] = offsetof(global_param_struct, snow_steps_per_day);
    mpi_types[i++] = MPI_AINT;

    // size_t runoff_steps_per_day;
    offsets[i] = offsetof(global_param_struct, runoff_steps_per_day);
    mpi_types[i++] = MPI_AINT;

    // size_t atmos_steps_per_day;
    offsets[i] = offsetof(global_param_struct, atmos_steps_per_day);
    mpi_types[i++] = MPI_AINT;

    // unsigned short endday;
    offsets[i] = offsetof(global_param_struct, endday);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short endmonth;
    offsets[i] = offsetof(global_param_struct, endmonth);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short endyear;
    offsets[i] = offsetof(global_param_struct, endyear);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short forceday[2];
    offsets[i] = offsetof(global_param_struct, forceday);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned int forcesec[2];
    offsets[i] = offsetof(global_param_struct, forcesec);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED;

    // unsigned short forcemonth[2];
    offsets[i] = offsetof(global_param_struct, forcemonth);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short forceoffset[2];
    offsets[i] = offsetof(global_param_struct, forceoffset);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned int forceskip[2];
    offsets[i] = offsetof(global_param_struct, forceskip);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED;

    // unsigned short int forceyear[2];
    offsets[i] = offsetof(global_param_struct, forceyear);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // size_t nrecs;
    offsets[i] = offsetof(global_param_struct, nrecs);
    mpi_types[i++] = MPI_AINT;

    // unsigned short int startday;
    offsets[i] = offsetof(global_param_struct, startday);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned int startsec;
    offsets[i] = offsetof(global_param_struct, startsec);
    mpi_types[i++] = MPI_UNSIGNED;

    // unsigned short int startmonth;
    offsets[i] = offsetof(global_param_struct, startmonth);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short int startyear;
    offsets[i] = offsetof(global_param_struct, startyear);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short int stateday;
    offsets[i] = offsetof(global_param_struct, stateday);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short int statemonth;
    offsets[i] = offsetof(global_param_struct, statemonth);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned int statesec;
    offsets[i] = offsetof(global_param_struct, statesec);
    mpi_types[i++] = MPI_UNSIGNED;

    // unsigned short int stateyear;
    offsets[i] = offsetof(global_param_struct, stateyear);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short int calendar;
    offsets[i] = offsetof(global_param_struct, calendar);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // unsigned short int time_units;
    offsets[i] = offsetof(global_param_struct, time_units);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;

    // double time_origin_num;
    offsets[i] = offsetof(global_param_struct, time_origin_num);
    mpi_types[i++] = MPI_DOUBLE;

    // char time_origin_str[MAXSTRING];
    offsets[i] = offsetof(global_param_struct, time_origin_str);
    blocklengths[i] = MAXSTRING;
    mpi_types[i++] = MPI_CHAR;

    // make sure that the we have the right number of elements
    if (i != (size_t) nitems) {
        log_err("Miscount: %zd not equal to %d.", i, nitems);
    }

    status = MPI_Type_create_struct(nitems, blocklengths, offsets, mpi_types,
                                    mpi_type);
    check_mpi_status(status, "MPI error.");

    status = MPI_Type_commit(mpi_type);
    check_mpi_status(status, "MPI error.");

    // cleanup
    free(blocklengths);
    free(offsets);
    free(mpi_types);
}
/******************************************************************************
 * @brief   Create an MPI_Datatype that represents the global_param_struct
 * @details This allows MPI operations in which the entire global_param_struct
 *          can be treated as an MPI_Datatype. NOTE: This function needs to be
 *          kept in-sync with the global_param_struct data type in vic_def.h.
 *
 * @param mpi_type MPI_Datatype that can be used in MPI operations
 *****************************************************************************/
@brief   Create an MPI_Datatype that represents the global_param_struct
@details This allows MPI operations in which the entire global_param_struct
can be treated as an MPI_Datatype. NOTE: This function needs to be
kept in-sync with the global_param_struct data type in vic_def.h.

@param mpi_type MPI_Datatype that can be used in MPI operations
@brief Create an MPI_Datatype that represents the global_param_struct @details This allows MPI operations in which the entire global_param_struct can be treated as an MPI_Datatype . NOTE : This function needs to be kept in - sync with the global_param_struct data type in vic_def . h . @param mpi_type MPI_Datatype that can be used in MPI operations
void
create_MPI_global_struct_type(MPI_Datatype *mpi_type)
{
    extern MPI_Comm MPI_COMM_VIC;
    int             nitems; 
    int             status;
    int            *blocklengths;
    size_t          i;
    MPI_Aint       *offsets;
    MPI_Datatype   *mpi_types;
    nitems = 32;
    blocklengths = malloc(nitems * sizeof(*blocklengths));
    check_alloc_status(blocklengths, "Memory allocation error.");
    offsets = malloc(nitems * sizeof(*offsets));
    check_alloc_status(offsets, "Memory allocation error.");
    mpi_types = malloc(nitems * sizeof(*mpi_types));
    check_alloc_status(mpi_types, "Memory allocation error.")
    for (i = 0; i < (size_t) nitems; i++) {
        blocklengths[i] = 1;
    }
    i = 0;
    offsets[i] = offsetof(global_param_struct, wind_h);
    mpi_types[i++] = MPI_DOUBLE;
    offsets[i] = offsetof(global_param_struct, resolution);
    mpi_types[i++] = MPI_DOUBLE;
    offsets[i] = offsetof(global_param_struct, dt);
    mpi_types[i++] = MPI_DOUBLE;
    offsets[i] = offsetof(global_param_struct, snow_dt);
    mpi_types[i++] = MPI_DOUBLE;
    offsets[i] = offsetof(global_param_struct, runoff_dt);
    mpi_types[i++] = MPI_DOUBLE;
    offsets[i] = offsetof(global_param_struct, atmos_dt);
    mpi_types[i++] = MPI_DOUBLE;
    offsets[i] = offsetof(global_param_struct, model_steps_per_day);
    mpi_types[i++] = MPI_AINT;
    offsets[i] = offsetof(global_param_struct, snow_steps_per_day);
    mpi_types[i++] = MPI_AINT;
    offsets[i] = offsetof(global_param_struct, runoff_steps_per_day);
    mpi_types[i++] = MPI_AINT;
    offsets[i] = offsetof(global_param_struct, atmos_steps_per_day);
    mpi_types[i++] = MPI_AINT;
    offsets[i] = offsetof(global_param_struct, endday);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, endmonth);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, endyear);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, forceday);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, forcesec);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED;
    offsets[i] = offsetof(global_param_struct, forcemonth);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, forceoffset);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, forceskip);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED;
    offsets[i] = offsetof(global_param_struct, forceyear);
    blocklengths[i] = 2;
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, nrecs);
    mpi_types[i++] = MPI_AINT;
    offsets[i] = offsetof(global_param_struct, startday);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, startsec);
    mpi_types[i++] = MPI_UNSIGNED;
    offsets[i] = offsetof(global_param_struct, startmonth);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, startyear);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, stateday);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, statemonth);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, statesec);
    mpi_types[i++] = MPI_UNSIGNED;
    offsets[i] = offsetof(global_param_struct, stateyear);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, calendar);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, time_units);
    mpi_types[i++] = MPI_UNSIGNED_SHORT;
    offsets[i] = offsetof(global_param_struct, time_origin_num);
    mpi_types[i++] = MPI_DOUBLE;
    offsets[i] = offsetof(global_param_struct, time_origin_str);
    blocklengths[i] = MAXSTRING;
    mpi_types[i++] = MPI_CHAR;
    if (i != (size_t) nitems) {
        log_err("Miscount: %zd not equal to %d.", i, nitems);
    }
    status = MPI_Type_create_struct(nitems, blocklengths, offsets, mpi_types,
                                    mpi_type);
    check_mpi_status(status, "MPI error.");
    status = MPI_Type_commit(mpi_type);
    check_mpi_status(status, "MPI error.");
    free(blocklengths);
    free(offsets);
    free(mpi_types);
}
void create_MPI_global_struct_type ( MPI_Datatype * mpi_type ) { extern MPI_Comm MPI_COMM_VIC ; int nitems ; int status ; int * blocklengths ; size_t i ; MPI_Aint * offsets ; MPI_Datatype * mpi_types ; nitems = 32 ; blocklengths = malloc ( nitems * sizeof ( * blocklengths ) ) ; check_alloc_status ( blocklengths , " " ) ; offsets = malloc ( nitems * sizeof ( * offsets ) ) ; check_alloc_status ( offsets , " " ) ; mpi_types = malloc ( nitems * sizeof ( * mpi_types ) ) ; check_alloc_status ( mpi_types , " " ) for ( i = 0 ; i < ( size_t ) nitems ; i ++ ) { blocklengths [ i ] = 1 ; } i = 0 ; offsets [ i ] = offsetof ( global_param_struct , wind_h ) ; mpi_types [ i ++ ] = MPI_DOUBLE ; offsets [ i ] = offsetof ( global_param_struct , resolution ) ; mpi_types [ i ++ ] = MPI_DOUBLE ; offsets [ i ] = offsetof ( global_param_struct , dt ) ; mpi_types [ i ++ ] = MPI_DOUBLE ; offsets [ i ] = offsetof ( global_param_struct , snow_dt ) ; mpi_types [ i ++ ] = MPI_DOUBLE ; offsets [ i ] = offsetof ( global_param_struct , runoff_dt ) ; mpi_types [ i ++ ] = MPI_DOUBLE ; offsets [ i ] = offsetof ( global_param_struct , atmos_dt ) ; mpi_types [ i ++ ] = MPI_DOUBLE ; offsets [ i ] = offsetof ( global_param_struct , model_steps_per_day ) ; mpi_types [ i ++ ] = MPI_AINT ; offsets [ i ] = offsetof ( global_param_struct , snow_steps_per_day ) ; mpi_types [ i ++ ] = MPI_AINT ; offsets [ i ] = offsetof ( global_param_struct , runoff_steps_per_day ) ; mpi_types [ i ++ ] = MPI_AINT ; offsets [ i ] = offsetof ( global_param_struct , atmos_steps_per_day ) ; mpi_types [ i ++ ] = MPI_AINT ; offsets [ i ] = offsetof ( global_param_struct , endday ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , endmonth ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , endyear ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , forceday ) ; blocklengths [ i ] = 2 ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , forcesec ) ; blocklengths [ i ] = 2 ; mpi_types [ i ++ ] = MPI_UNSIGNED ; offsets [ i ] = offsetof ( global_param_struct , forcemonth ) ; blocklengths [ i ] = 2 ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , forceoffset ) ; blocklengths [ i ] = 2 ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , forceskip ) ; blocklengths [ i ] = 2 ; mpi_types [ i ++ ] = MPI_UNSIGNED ; offsets [ i ] = offsetof ( global_param_struct , forceyear ) ; blocklengths [ i ] = 2 ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , nrecs ) ; mpi_types [ i ++ ] = MPI_AINT ; offsets [ i ] = offsetof ( global_param_struct , startday ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , startsec ) ; mpi_types [ i ++ ] = MPI_UNSIGNED ; offsets [ i ] = offsetof ( global_param_struct , startmonth ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , startyear ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , stateday ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , statemonth ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , statesec ) ; mpi_types [ i ++ ] = MPI_UNSIGNED ; offsets [ i ] = offsetof ( global_param_struct , stateyear ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , calendar ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , time_units ) ; mpi_types [ i ++ ] = MPI_UNSIGNED_SHORT ; offsets [ i ] = offsetof ( global_param_struct , time_origin_num ) ; mpi_types [ i ++ ] = MPI_DOUBLE ; offsets [ i ] = offsetof ( global_param_struct , time_origin_str ) ; blocklengths [ i ] = MAXSTRING ; mpi_types [ i ++ ] = MPI_CHAR ; if ( i != ( size_t ) nitems ) { log_err ( " " , i , nitems ) ; } status = MPI_Type_create_struct ( nitems , blocklengths , offsets , mpi_types , mpi_type ) ; check_mpi_status ( status , " " ) ; status = MPI_Type_commit ( mpi_type ) ; check_mpi_status ( status , " " ) ; free ( blocklengths ) ; free ( offsets ) ; free ( mpi_types ) ; }
@brief   Create an MPI_Datatype that represents the global_param_struct
 @details This allows MPI operations in which the entire global_param_struct
          can be treated as an MPI_Datatype.
@brief Create an MPI_Datatype that represents the global_param_struct @details This allows MPI operations in which the entire global_param_struct can be treated as an MPI_Datatype .
// number of elements in struct // nitems has to equal the number of elements in global_param_struct // most of the elements in global_param_struct are not arrays. Use 1 as // the default block length and reset as needed // reset i // double wind_h; // double resolution; // double dt; // double snow_dt; // double runoff_dt; // double atmos_dt; // size_t model_steps_per_day; // size_t snow_steps_per_day; // size_t runoff_steps_per_day; // size_t atmos_steps_per_day; // unsigned short endday; // unsigned short endmonth; // unsigned short endyear; // unsigned short forceday[2]; // unsigned int forcesec[2]; // unsigned short forcemonth[2]; // unsigned short forceoffset[2]; // unsigned int forceskip[2]; // unsigned short int forceyear[2]; // size_t nrecs; // unsigned short int startday; // unsigned int startsec; // unsigned short int startmonth; // unsigned short int startyear; // unsigned short int stateday; // unsigned short int statemonth; // unsigned int statesec; // unsigned short int stateyear; // unsigned short int calendar; // unsigned short int time_units; // double time_origin_num; // char time_origin_str[MAXSTRING]; // make sure that the we have the right number of elements // cleanup