ssg-launch-group.c 4.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * 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>
Shane Snyder's avatar
Shane Snyder committed
12
#ifdef SSG_HAVE_MPI
13 14
#include <mpi.h>
#endif
15 16 17

#include <margo.h>
#include <ssg.h>
Shane Snyder's avatar
Shane Snyder committed
18
#ifdef SSG_HAVE_MPI
19 20
#include <ssg-mpi.h>
#endif
21 22 23 24 25 26 27 28 29 30

#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(1); \
        } \
    } while(0)

Shane Snyder's avatar
Shane Snyder committed
31 32 33 34 35 36 37 38
struct group_launch_opts
{
    int duration;
    char *gid_file;
    char *group_mode;
    char *group_addr_conf_file;
};

39 40 41 42
static void usage()
{
    fprintf(stderr,
        "Usage: "
Shane Snyder's avatar
Shane Snyder committed
43
        "ssg-launch-group [OPTIONS] <addr> <create mode> [config file]\n"
Shane Snyder's avatar
Shane Snyder committed
44
        "\t<addr> - Mercury address string\n"
45 46 47 48
        "\t<create mode> - \"mpi\" (if supported) or \"conf\"\n"
        "\tif \"conf\" is the mode, then [config file] is required\n");
}

Shane Snyder's avatar
Shane Snyder committed
49
static void parse_args(int argc, char *argv[], struct group_launch_opts *opts)
50
{
Shane Snyder's avatar
Shane Snyder committed
51 52 53
    int c;
    const char *options = "d:f:";
    char *check = NULL;
54

Shane Snyder's avatar
Shane Snyder committed
55
    while ((c = getopt(argc, argv, options)) != -1)
56
    {
Shane Snyder's avatar
Shane Snyder committed
57
        switch (c)
58
        {
Shane Snyder's avatar
Shane Snyder committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72
            case 'd':
                opts->duration = (int)strtol(optarg, &check, 0);
                if (opts->duration < 0 || (check && *check != '\0'))
                {
                    usage();
                    exit(EXIT_FAILURE);
                }
                break;
            case 'f':
                opts->gid_file = optarg;
                break;
            default:
                usage();
                exit(EXIT_FAILURE);
73 74 75
        }
    }

Shane Snyder's avatar
Shane Snyder committed
76 77
    return;
#if 0
78 79
    *addr_str = argv[ndx++];
    *mode = argv[ndx++];
80 81 82

    if (strcmp(*mode, "conf") == 0)
    {
83
        if (ndx != (argc - 1))
84 85 86 87
        {
            usage();
            exit(1);
        }
88
        *conf_file = argv[ndx];
89 90 91
    }
    else if (strcmp(*mode, "mpi") == 0)
    {
Shane Snyder's avatar
Shane Snyder committed
92
#ifdef SSG_HAVE_MPI
93
        if (ndx != argc)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        {
            usage();
            exit(1);
        }
#else
        fprintf(stderr, "Error: MPI support not built in\n");
        exit(1);
#endif
    
    }
    else
    {
        usage();
        exit(1);
    }
Shane Snyder's avatar
Shane Snyder committed
109
#endif
110 111 112 113 114 115 116
}

int main(int argc, char *argv[])
{
    margo_instance_id mid = MARGO_INSTANCE_NULL;
    const char *addr_str;
    const char *group_name = "simple_group";
Shane Snyder's avatar
Shane Snyder committed
117
    ssg_group_id_t g_id = SSG_GROUP_ID_NULL;
118 119
    ssg_member_id_t my_id;
    int group_size;
120
    int sret;
121

Shane Snyder's avatar
Shane Snyder committed
122 123 124 125
    /* XXX default options? */
    struct group_launch_opts opts;
    parse_args(argc, argv, &opts);
    return 0;
126

Shane Snyder's avatar
Shane Snyder committed
127
#ifdef SSG_HAVE_MPI
128 129
    if (strcmp(mode, "mpi") == 0)
        MPI_Init(&argc, &argv);
130
#endif
131

132 133 134
    /* init margo */
    /* use the main xstream to drive progress & run handlers */
    mid = margo_init(addr_str, MARGO_SERVER_MODE, 0, -1);
135 136 137
    DIE_IF(mid == MARGO_INSTANCE_NULL, "margo_init");

    /* initialize SSG */
138 139
    sret = ssg_init(mid);
    DIE_IF(sret != SSG_SUCCESS, "ssg_init");
140 141

    if(strcmp(mode, "conf") == 0)
142
        g_id = ssg_group_create_config(group_name, conf_file, NULL, NULL);
Shane Snyder's avatar
Shane Snyder committed
143
#ifdef SSG_HAVE_MPI
144
    else if(strcmp(mode, "mpi") == 0)
145
        g_id = ssg_group_create_mpi(group_name, MPI_COMM_WORLD, NULL, NULL);
146
#endif
147
    DIE_IF(g_id == SSG_GROUP_ID_NULL, "ssg_group_create");
148

Shane Snyder's avatar
Shane Snyder committed
149 150 151
    /* sleep to give all group members a chance to create the group */
    if (sleep_time > 0) margo_thread_sleep(mid, sleep_time * 1000.0);

152 153
    /* get my group id and the size of the group */
    my_id = ssg_get_group_self_id(g_id);
154
    DIE_IF(my_id == SSG_MEMBER_ID_INVALID, "ssg_get_group_self_id");
155 156
    group_size = ssg_get_group_size(g_id);
    DIE_IF(group_size == 0, "ssg_get_group_size");
Shane Snyder's avatar
Shane Snyder committed
157 158
    printf("group member %lu successfully created group (size == %d)\n",
        my_id, group_size);
159

Shane Snyder's avatar
Shane Snyder committed
160 161
    /* print group at each member */
    ssg_group_dump(g_id);
162

163
    /** cleanup **/
164 165 166
    ssg_group_destroy(g_id);
    ssg_finalize();
    margo_finalize(mid);
Shane Snyder's avatar
Shane Snyder committed
167
#ifdef SSG_HAVE_MPI
168 169
    if (strcmp(mode, "mpi") == 0)
        MPI_Finalize();
170
#endif
171 172 173

    return 0;
}