ssg-test-simple.c 3.91 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>
12
13
14
#if HAVE_MPI
#include <mpi.h>
#endif
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <margo.h>
#include <mercury.h>
#include <abt.h>
#include <ssg.h>

#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)

static void usage()
{
    fprintf(stderr,
        "Usage: "
34
35
        "ssg-test-simple [-s <time>] <addr> <create mode> [config file]\n"
        "\t-s <time> - time to sleep between init/finalize\n"
36
37
38
39
        "\t<create mode> - \"mpi\" (if supported) or \"conf\"\n"
        "\tif \"conf\" is the mode, then [config file] is required\n");
}

40
static void parse_args(int argc, char *argv[], int *sleep_time, const char **addr_str,
41
42
    const char **mode, const char **conf_file)
{
43
44
    int ndx = 1;

45
46
47
48
49
50
    if (argc < 3)
    {
        usage();
        exit(1);
    }

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    if (strcmp(argv[ndx], "-s") == 0)
    {
        char *check = NULL;
        ndx++;

        *sleep_time = (int)strtol(argv[ndx++], &check, 0);
        if(*sleep_time < 0 || (check && *check != '\0') || argc < 5)
        {
            usage();
            exit(1);
        }
    }

    *addr_str = argv[ndx++];
    *mode = argv[ndx++];
66
67
68

    if (strcmp(*mode, "conf") == 0)
    {
69
        if (ndx != (argc - 1))
70
71
72
73
        {
            usage();
            exit(1);
        }
74
        *conf_file = argv[ndx];
75
76
77
78
    }
    else if (strcmp(*mode, "mpi") == 0)
    {
#ifdef HAVE_MPI
79
        if (ndx != argc)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        {
            usage();
            exit(1);
        }
#else
        fprintf(stderr, "Error: MPI support not built in\n");
        exit(1);
#endif
    
    }
    else
    {
        usage();
        exit(1);
    }

    return;   
}

int main(int argc, char *argv[])
{
    hg_class_t *hgcl = NULL;
    hg_context_t *hgctx = NULL;
    margo_instance_id mid = MARGO_INSTANCE_NULL;
104
    int sleep_time = 0;
105
106
107
108
    const char *addr_str;
    const char *mode;
    const char *conf_file;
    const char *group_name = "simple_group";
109
    ssg_group_id_t g_id;
110
111
    ssg_member_id_t my_id;
    int group_size;
112
    int sret;
113

114
    parse_args(argc, argv, &sleep_time, &addr_str, &mode, &conf_file);
115
116
117

    ABT_init(argc, argv);

118
#if HAVE_MPI
119
120
    if (strcmp(mode, "mpi") == 0)
        MPI_Init(&argc, &argv);
121
#endif
122
123
124
125
126
127
128
129
130
131
132
133

    /* init HG */
    hgcl = HG_Init(addr_str, HG_TRUE);
    DIE_IF(hgcl == NULL, "HG_Init");
    hgctx = HG_Context_create(hgcl);
    DIE_IF(hgctx == NULL, "HG_Context_create");

    /* init margo in single threaded mode */
    mid = margo_init(0, -1, hgctx);
    DIE_IF(mid == MARGO_INSTANCE_NULL, "margo_init");

    /* initialize SSG */
134
135
    sret = ssg_init(mid);
    DIE_IF(sret != SSG_SUCCESS, "ssg_init");
136
137

    if(strcmp(mode, "conf") == 0)
138
        sret = ssg_group_create_config(group_name, conf_file, &g_id);
139
#if HAVE_MPI
140
    else if(strcmp(mode, "mpi") == 0)
141
        sret = ssg_group_create_mpi(group_name, MPI_COMM_WORLD, &g_id);
142
#endif
143
    DIE_IF(sret != SSG_SUCCESS, "ssg_group_create");
144

145
    /* sleep to give all group members a chance to create the group */
146
    if (sleep_time > 0) margo_thread_sleep(mid, sleep_time * 1000.0);
147

148
149
150
151
152
153
154
155
    /* get my group id and the size of the group */
    my_id = ssg_get_group_self_id(g_id);
    DIE_IF(my_id == SSG_MEMBER_ID_INVALID, "ssg_get_group_self_id");
    group_size = ssg_get_group_size(g_id);
    DIE_IF(group_size == 0, "ssg_get_group_size");
    printf("group member %d of %d successfully created group\n",
        (int)my_id, group_size);

156
157
    /** cleanup **/

158
159
160
161
162
163
    ssg_group_destroy(g_id);
    ssg_finalize();

    margo_finalize(mid);

    if(hgctx) HG_Context_destroy(hgctx);
Shane Snyder's avatar
Shane Snyder committed
164
    if(hgcl) HG_Finalize(hgcl);
165

166
#if HAVE_MPI
167
168
    if (strcmp(mode, "mpi") == 0)
        MPI_Finalize();
169
#endif
170
171
172
173
174

    ABT_finalize();

    return 0;
}