ssg-test-simple.c 3.47 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
    const char *addr_str;
    const char *mode;
    const char *conf_file;
    const char *group_name = "simple_group";
106
107
    ssg_group_id_t g_id;
    int sret;
108

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

    /* 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 */
129
130
    sret = ssg_init(mid);
    DIE_IF(sret != SSG_SUCCESS, "ssg_init");
131
132

    if(strcmp(mode, "conf") == 0)
133
        sret = ssg_group_create_config(group_name, conf_file, &g_id);
134
#if HAVE_MPI
135
    else if(strcmp(mode, "mpi") == 0)
136
        sret = ssg_group_create_mpi(group_name, MPI_COMM_WORLD, &g_id);
137
#endif
138
    DIE_IF(sret != SSG_SUCCESS, "ssg_group_create");
139

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

142
143
    /** cleanup **/

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

    margo_finalize(mid);

149
#ifndef SWIM_FORCE_FAIL
150
    if(hgctx) HG_Context_destroy(hgctx);
Shane Snyder's avatar
Shane Snyder committed
151
    if(hgcl) HG_Finalize(hgcl);
152
#endif
153

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

159
#ifndef SWIM_FORCE_FAIL
160
    ABT_finalize();
161
#endif
162
163
164

    return 0;
}