ssg-test-simple.c 3.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * 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>

#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: "
31
32
        "ssg-test-simple [-s <time>] <addr> <create mode> [config file]\n"
        "\t-s <time> - time to sleep between init/finalize\n"
33
34
35
36
        "\t<create mode> - \"mpi\" (if supported) or \"conf\"\n"
        "\tif \"conf\" is the mode, then [config file] is required\n");
}

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

42
43
44
45
46
47
    if (argc < 3)
    {
        usage();
        exit(1);
    }

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    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++];
63
64
65

    if (strcmp(*mode, "conf") == 0)
    {
66
        if (ndx != (argc - 1))
67
68
69
70
        {
            usage();
            exit(1);
        }
71
        *conf_file = argv[ndx];
72
73
74
75
    }
    else if (strcmp(*mode, "mpi") == 0)
    {
#ifdef HAVE_MPI
76
        if (ndx != argc)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        {
            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;
101
    int sleep_time = 0;
102
103
104
105
106
107
108
    const char *addr_str;
    const char *mode;
    const char *conf_file;
    const char *group_name = "simple_group";
    ssg_group_id_t g_id = SSG_GROUP_ID_NULL;
    int ret;

109
    parse_args(argc, argv, &sleep_time, &addr_str, &mode, &conf_file);
110
111
112

    ABT_init(argc, argv);

113
#if HAVE_MPI
114
115
    if (strcmp(mode, "mpi") == 0)
        MPI_Init(&argc, &argv);
116
#endif
117
118
119
120
121
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 */
    ret = ssg_init(mid);
    DIE_IF(ret != SSG_SUCCESS, "ssg_init");

    if(strcmp(mode, "conf") == 0)
        g_id = ssg_group_create_config(group_name, conf_file);
134
#if HAVE_MPI
135
136
    else if(strcmp(mode, "mpi") == 0)
        g_id = ssg_group_create_mpi(group_name, MPI_COMM_WORLD);
137
#endif
138
139
    // XXX DIE_IF(g_id == SSG_GROUP_ID_NULL, "ssg_group_create");

140
141
    if (sleep_time > 0) margo_thread_sleep(mid, sleep_time *1000.0);

142
143
    /** cleanup **/

144
145
146
147
148
149
    ssg_group_destroy(g_id);
    ssg_finalize();

    margo_finalize(mid);

    if(hgctx) HG_Context_destroy(hgctx);
Shane Snyder's avatar
Shane Snyder committed
150
    if(hgcl) HG_Finalize(hgcl);
151

152
#if HAVE_MPI
153
154
    if (strcmp(mode, "mpi") == 0)
        MPI_Finalize();
155
#endif
156
157
158
159
160

    ABT_finalize();

    return 0;
}