ssg-launch-double-mid-group.c 7.01 KB
Newer Older
Shane Snyder's avatar
Shane Snyder committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2016 UChicago Argonne, LLC
 *
 * See COPYRIGHT in top-level directory.
 */

#include <ssg-config.h>

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#ifdef SSG_HAVE_MPI
#include <mpi.h>
#endif
Shane Snyder's avatar
Shane Snyder committed
15 16 17
#ifdef SSG_HAVE_PMIX
#include <pmix.h>
#endif
Shane Snyder's avatar
Shane Snyder committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

#include <margo.h>
#include <ssg.h>
#ifdef SSG_HAVE_MPI
#include <ssg-mpi.h>
#endif
#ifdef SSG_HAVE_PMIX
#include <ssg-pmix.h>
#endif

#define DIE_IF(cond_expr, err_fmt, ...) \
    do { \
        if (cond_expr) { \
            fprintf(stderr, "ERROR at %s:%d (" #cond_expr "): " \
                    err_fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
            exit(EXIT_FAILURE); \
        } \
    } while(0)

struct group_launch_opts
{
    char *addr_str1;
    char *addr_str2;
    char *group_mode;
    int shutdown_time;
    char *gid_file;
    char *group_name;
};

static void usage()
{
    fprintf(stderr,
        "Usage: "
        "ssg-launch-group [OPTIONS] <ADDR1> <ADDR2> <MODE> [CONFFILE]\n"
        "Create and launch two groups using two Margo instnaces given Mercury ADDR1 and ADDR2 string and group create MODE (\"mpi\" or \"pmix\").\n"
        "\n"
        "OPTIONS:\n"
        "\t-s <TIME>\t\tTime duration (in seconds) to run the group before shutting down\n"
        "\t-f <FILE>\t\tFile path to store group ID in\n"
        "\t-n <NAME>\t\tName of the group to launch\n");
}

static void parse_args(int argc, char *argv[], struct group_launch_opts *opts)
{
    int c;
    const char *options = "s:f:n:";
    char *check = NULL;

    while ((c = getopt(argc, argv, options)) != -1)
    {
        switch (c)
        {
            case 's':
                opts->shutdown_time = (int)strtol(optarg, &check, 0);
                if (opts->shutdown_time < 0 || (check && *check != '\0'))
                {
                    usage();
                    exit(EXIT_FAILURE);
                }
                break;
            case 'f':
                opts->gid_file = optarg;
                break;
            case 'n':
                opts->group_name = optarg;
                break;
            default:
                usage();
                exit(EXIT_FAILURE);
        }
    }

    if ((argc - optind) < 3)
    {
        usage();
        exit(EXIT_FAILURE);
    }

    opts->addr_str1 = argv[optind++];
    opts->addr_str2 = argv[optind++];
    opts->group_mode = argv[optind++];
    if (strcmp(opts->group_mode, "mpi") == 0)
    {
#ifdef SSG_HAVE_MPI
        if (optind != argc)
        {
            usage();
            exit(EXIT_FAILURE);
        }
#else
        fprintf(stderr, "Error: MPI support not built in\n");
        exit(EXIT_FAILURE);
#endif
    }
    else if (strcmp(opts->group_mode, "pmix") == 0)
    {
#ifdef SSG_HAVE_PMIX
        if (optind != argc)
        {
            usage();
            exit(EXIT_FAILURE);
        }
#else
        fprintf(stderr, "Error: PMIx support not built in\n");
        exit(EXIT_FAILURE);
#endif
    }
    else
    {
        usage();
        exit(EXIT_FAILURE);
    }

    return;
}

int main(int argc, char *argv[])
{
    struct group_launch_opts opts;
    margo_instance_id mid1 = MARGO_INSTANCE_NULL;
    margo_instance_id mid2 = MARGO_INSTANCE_NULL;
    ssg_group_id_t g1_id = SSG_GROUP_ID_INVALID;
    ssg_group_id_t g2_id = SSG_GROUP_ID_INVALID;
    ssg_member_id_t my_id1;
    ssg_member_id_t my_id2;
    int group1_size;
    int group2_size;
    char scratch[1024] = {0};
    int sret;

    /* set any default options (that may be overwritten by cmd args) */
    opts.shutdown_time = 10; /* default to running group for 10 seconds */
    opts.group_name = "simple_group";
    opts.gid_file = NULL;

    /* parse cmdline arguments */
    parse_args(argc, argv, &opts);

#ifdef SSG_HAVE_MPI
    int mpi_rank, mpi_size;
    if (strcmp(opts.group_mode, "mpi") == 0)
    {
        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
        MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
    }
#endif
#ifdef SSG_HAVE_PMIX
    pmix_status_t ret;
    pmix_proc_t proc;
    if (strcmp(opts.group_mode, "pmix") == 0)
    {
        ret = PMIx_Init(&proc, NULL, 0);
        DIE_IF(ret != PMIX_SUCCESS, "PMIx_Init");
    }
#endif

    /* init first margo instance */
    /* NOTE: dedicated progress thread needed to avoid MPI/margo deadlock
     * possible when running multiple groups
     */
    mid1 = margo_init(opts.addr_str1, MARGO_SERVER_MODE, 1, -1);
    DIE_IF(mid1 == MARGO_INSTANCE_NULL, "margo_init");

    /* init second margo instance */
    /* NOTE: dedicated progress thread needed to avoid MPI/margo deadlock
     * possible when running multiple groups
     */
    mid2 = margo_init(opts.addr_str2, MARGO_SERVER_MODE, 1, -1);
    DIE_IF(mid2 == MARGO_INSTANCE_NULL, "margo_init");

    /* initialize SSG */
    sret = ssg_init();
    DIE_IF(sret != SSG_SUCCESS, "ssg_init");

    /* XXX do we want to use callback for testing anything about group??? */
#ifdef SSG_HAVE_MPI
    if(strcmp(opts.group_mode, "mpi") == 0)
    {
        snprintf(scratch, 1024, "%s-%d", opts.group_name, 1);
198
        g1_id = ssg_group_create_mpi(mid1, scratch, MPI_COMM_WORLD, NULL, NULL, NULL);
Shane Snyder's avatar
Shane Snyder committed
199
        snprintf(scratch, 1024, "%s-%d", opts.group_name, 2);
200
        g2_id = ssg_group_create_mpi(mid2, scratch, MPI_COMM_WORLD, NULL, NULL, NULL);
Shane Snyder's avatar
Shane Snyder committed
201 202 203 204 205 206
    }
#endif
#ifdef SSG_HAVE_PMIX
    if(strcmp(opts.group_mode, "pmix") == 0)
    {
        snprintf(scratch, 1024, "%s-%d", opts.group_name, 1);
207
        g1_id = ssg_group_create_pmix(mid1, scratch, proc, NULL, NULL, NULL);
Shane Snyder's avatar
Shane Snyder committed
208
        snprintf(scratch, 1024, "%s-%d", opts.group_name, 2);
209
        g2_id = ssg_group_create_pmix(mid2, scratch, proc, NULL, NULL, NULL);
Shane Snyder's avatar
Shane Snyder committed
210 211 212 213 214 215 216 217 218
    }
#endif
    DIE_IF(g1_id == SSG_GROUP_ID_INVALID, "ssg_group_create");
    DIE_IF(g2_id == SSG_GROUP_ID_INVALID, "ssg_group_create");

    /* store the gid if requested */
    if (opts.gid_file)
    {
        snprintf(scratch, 1024, "%s-%d", opts.gid_file, 1);
219
        ssg_group_id_store(scratch, g1_id, 1);
Shane Snyder's avatar
Shane Snyder committed
220
        snprintf(scratch, 1024, "%s-%d", opts.gid_file, 2);
221
        ssg_group_id_store(scratch, g2_id, 1);
Shane Snyder's avatar
Shane Snyder committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    }

    /* sleep for given duration to allow group time to run */
    if (opts.shutdown_time > 0)
        margo_thread_sleep(mid1, opts.shutdown_time * 1000.0);

    /* get my group id and the size of the group */
    my_id1 = ssg_get_self_id(mid1);
    my_id2 = ssg_get_self_id(mid2);
    DIE_IF(my_id1 == SSG_MEMBER_ID_INVALID, "ssg_get_group_self_id");
    DIE_IF(my_id2 == SSG_MEMBER_ID_INVALID, "ssg_get_group_self_id");
    group1_size = ssg_get_group_size(g1_id);
    group2_size = ssg_get_group_size(g2_id);
    DIE_IF(group1_size == 0, "ssg_get_group_size");
    DIE_IF(group2_size == 0, "ssg_get_group_size");

    /* print group at each member */
    ssg_group_dump(g1_id);
    ssg_group_destroy(g1_id);
    ssg_group_dump(g2_id);
    ssg_group_destroy(g2_id);

    /** cleanup **/
    ssg_finalize();
    margo_finalize(mid1);
    margo_finalize(mid2);
#ifdef SSG_HAVE_MPI
    if (strcmp(opts.group_mode, "mpi") == 0)
        MPI_Finalize();
#endif
#ifdef SSG_HAVE_PMIX
    if (strcmp(opts.group_mode, "pmix") == 0)
        PMIx_Finalize(NULL, 0);
#endif

    return 0;
}