ssg.c 14.6 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (c) 2016 UChicago Argonne, LLC
 *
 * See COPYRIGHT in top-level directory.
 */

7
#include "ssg-config.h"
Shane Snyder's avatar
Shane Snyder committed
8

Jonathan Jenkins's avatar
Jonathan Jenkins committed
9 10 11 12
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
13
#include <errno.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
14 15
#include <stdlib.h>
#include <string.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
16
#include <assert.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
17

Shane Snyder's avatar
Shane Snyder committed
18
#include <mercury.h>
19
#include <abt.h>
Shane Snyder's avatar
Shane Snyder committed
20
#include <margo.h>
21

22
#include "ssg.h"
23
#include "ssg-internal.h"
24 25 26 27
#if USE_SWIM_FD
#include "swim-fd/swim-fd.h"
#endif

28 29 30
/* SSG helper routine prototypes */
static hg_return_t ssg_group_lookup(
    ssg_group_t * g, const char * const addr_strs[]);
31 32 33
static void ssg_generate_group_id(
    const char * name, const char * leader_addr_str,
    ssg_group_id_t *group_id);
34 35
static const char ** ssg_setup_addr_str_list(
    char * buf, int num_addrs);
36

37
/* XXX: is this right? can this be a global? */
38
margo_instance_id ssg_mid = MARGO_INSTANCE_NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
39

40 41 42
/* XXX: fix this */
ssg_group_t *the_group = NULL;

43 44 45
/***************************************************
 *** SSG runtime intialization/shutdown routines ***
 ***************************************************/
46

47 48
int ssg_init(
    margo_instance_id mid)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
49
{
50
    ssg_mid = mid;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
51

52 53
    return SSG_SUCCESS;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
54

55
int ssg_finalize()
56
{
57
    return SSG_SUCCESS;
58
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
59

60 61 62
/*************************************
 *** SSG group management routines ***
 *************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
63

64
int ssg_group_create(
65 66
    const char * group_name,
    const char * const group_addr_strs[],
67 68
    int group_size,
    ssg_group_id_t * group_id)
69
{
70
    hg_class_t *hgcl = NULL;
Shane Snyder's avatar
Shane Snyder committed
71
    hg_addr_t self_addr = HG_ADDR_NULL;
72
    char *self_addr_str = NULL;
Shane Snyder's avatar
Shane Snyder committed
73
    hg_size_t self_addr_size = 0;
74 75 76
    const char *self_addr_substr = NULL;
    const char *addr_substr = NULL;
    int i;
77
    hg_return_t hret;
78
    ssg_group_t *g = NULL;
79
    int sret = SSG_ERROR;
80

81 82
    hgcl = margo_get_class(ssg_mid);
    if (!hgcl) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
83

84
    /* get my address */
85 86 87 88 89 90 91 92 93
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto fini;
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_size, self_addr);
    if (hret != HG_SUCCESS) goto fini;
    self_addr_str = malloc(self_addr_size);
    if (self_addr_str == NULL) goto fini;
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_size, self_addr);
    if (hret != HG_SUCCESS) goto fini;

94 95 96 97
    /* strstr is used here b/c there may be inconsistencies in whether the class
     * is included in the address or not (it should not be in HG_Addr_to_string,
     * but it's possible that it is in the list of group address strings)
     */
98
    self_addr_substr = strstr(self_addr_str, "://");
Shane Snyder's avatar
Shane Snyder committed
99 100 101 102
    if (self_addr_substr == NULL)
        self_addr_substr = self_addr_str;
    else
        self_addr_substr += 3;
Shane Snyder's avatar
Shane Snyder committed
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    /* allocate an SSG group data structure and initialize some of it */
    g = malloc(sizeof(*g));
    if (!g) goto fini;
    memset(g, 0, sizeof(*g));
    g->name = strdup(group_name);
    if (!g->name) goto fini;
    g->self_rank = -1;
    g->view.group_size = group_size;
    g->view.member_states = malloc(group_size * sizeof(*g->view.member_states));
    if (!g->view.member_states) goto fini;
    memset(g->view.member_states, 0, group_size * sizeof(*g->view.member_states));

    /* resolve my rank within the group */
    for (i = 0; i < group_size; i++)
    {
        addr_substr = strstr(group_addr_strs[i], "://");
Shane Snyder's avatar
Shane Snyder committed
120 121 122 123
        if (addr_substr == NULL)
            addr_substr = group_addr_strs[i];
        else
            addr_substr += 3;
124 125 126 127 128 129 130 131 132 133 134 135 136 137
        if (strcmp(self_addr_substr, addr_substr) == 0)
        {
            /* this is my address -- my rank is the offset in the address array */
            g->self_rank = i;
            g->view.member_states[i].addr = self_addr;
        }
        else
        {
            /* initialize group member addresses to NULL before looking them up */
            g->view.member_states[i].addr = HG_ADDR_NULL;
        }
        g->view.member_states[i].is_member = 1;
    }
    /* if unable to resolve my rank within the group, error out */
138
    if (g->self_rank == -1)
139 140 141 142 143 144 145 146 147 148 149 150 151 152
    {
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
    }

    /* lookup hg addresses information for all group members */
    hret = ssg_group_lookup(g, group_addr_strs);
    if (hret != HG_SUCCESS)
    {
        fprintf(stderr, "Error: SSG unable to complete lookup for group %s\n",
            group_name);
        goto fini;
    }
Shane Snyder's avatar
Shane Snyder committed
153
    SSG_DEBUG(g, "group lookup successful (size=%d)\n", group_size);
154 155

#if USE_SWIM_FD
156 157 158 159 160 161
    int swim_active = 1;
#ifdef SWIM_FORCE_FAIL
    if (g->self_rank == 1)
        swim_active = 0;
#endif

162
    /* initialize swim failure detector */
163 164
    // TODO: we should probably barrier or sync somehow to avoid rpc failures
    // due to timing skew of different ranks initializing swim
165
    g->fd_ctx = (void *)swim_init(g, swim_active);
166
    if (g->fd_ctx == NULL) goto fini;
167 168
#endif

169
    /* TODO: add reference to this group to SSG runtime state */
170
    the_group = g;
171

172 173 174 175 176 177
    /* generate a unique ID for this group that can be used by other
     * processes to join or attach to the group
     */    
    ssg_generate_group_id(group_name, group_addr_strs[0], group_id);

    sret = SSG_SUCCESS;
178
    /* don't free these pointers on success */
179
    self_addr = HG_ADDR_NULL;
180 181
    g = NULL;
fini:
Shane Snyder's avatar
Shane Snyder committed
182 183
    if (hgcl && self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
184 185 186 187 188 189 190
    if (g)
    {
        free(g->name);
        free(g->view.member_states);
        free(g);
    }

191
    return sret;
192 193
}

194
int ssg_group_create_config(
195
    const char * group_name,
196 197
    const char * file_name,
    ssg_group_id_t * group_id)
198 199 200 201 202 203 204 205 206 207
{
    int fd;
    struct stat st;
    char *rd_buf = NULL;
    ssize_t rd_buf_sz;
    char *tok;
    void *addr_buf = NULL;
    int addr_buf_len = 0, num_addrs = 0;
    int ret;
    const char **addr_strs = NULL;
208
    int sret = SSG_ERROR;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    /* open config file for reading */
    fd = open(file_name, O_RDONLY);
    if (fd == -1)
    {
        fprintf(stderr, "Error: SSG unable to open config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }

    /* get file size and allocate a buffer to store it */
    ret = fstat(fd, &st);
    if (ret == -1)
    {
        fprintf(stderr, "Error: SSG unable to stat config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }
    rd_buf = malloc(st.st_size+1);
    if (rd_buf == NULL) goto fini;

    /* load it all in one fell swoop */
    rd_buf_sz = read(fd, rd_buf, st.st_size);
    if (rd_buf_sz != st.st_size)
    {
        fprintf(stderr, "Error: SSG unable to read config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }
    rd_buf[rd_buf_sz]='\0';

    /* strtok the result - each space-delimited address is assumed to be
     * a unique mercury address
     */
    tok = strtok(rd_buf, "\r\n\t ");
Jonathan Jenkins's avatar
Jonathan Jenkins committed
244 245
    if (tok == NULL) goto fini;

246 247
    /* build up the address buffer */
    addr_buf = malloc(rd_buf_sz);
248
    if (addr_buf == NULL) goto fini;
249 250
    do
    {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
251
        int tok_sz = strlen(tok);
252 253
        memcpy((char*)addr_buf + addr_buf_len, tok, tok_sz+1);
        addr_buf_len += tok_sz+1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
254 255 256
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
257 258 259 260 261 262 263
    if (addr_buf_len != rd_buf_sz)
    {
        /* adjust buffer size if our initial guess was wrong */
        void *tmp = realloc(addr_buf, addr_buf_len);
        if (tmp == NULL) goto fini;
        addr_buf = tmp;
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
264

265 266 267
    /* set up address string array for group members */
    addr_strs = ssg_setup_addr_str_list(addr_buf, num_addrs);
    if (!addr_strs) goto fini;
268

269
    /* invoke the generic group create routine using our list of addrs */
270
    sret = ssg_group_create(group_name, addr_strs, num_addrs, group_id);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
271 272

fini:
273
    /* cleanup before returning */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
274
    if (fd != -1) close(fd);
275
    free(rd_buf);
276
    free(addr_buf);
277 278
    free(addr_strs);

279
    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
280 281 282
}

#ifdef HAVE_MPI
283
int ssg_group_create_mpi(
284
    const char * group_name,
285 286
    MPI_Comm comm,
    ssg_group_id_t * group_id)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
287
{
288
    hg_class_t *hgcl = NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
289
    hg_addr_t self_addr = HG_ADDR_NULL;
290
    char *self_addr_str = NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
291
    hg_size_t self_addr_size = 0;
292 293 294 295 296 297
    int self_addr_size_int = 0; /* for mpi-friendly conversion */
    char *addr_buf = NULL;
    int *sizes = NULL;
    int *sizes_psum = NULL;
    int comm_size = 0, comm_rank = 0;
    const char **addr_strs = NULL;
298
    hg_return_t hret;
299
    int sret = SSG_ERROR;
300

301 302
    hgcl = margo_get_class(ssg_mid);
    if (!hgcl) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
303

304
    /* get my address */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
305 306 307
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto fini;
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_size, self_addr);
Shane Snyder's avatar
Shane Snyder committed
308
    if (hret != HG_SUCCESS) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
309 310
    self_addr_str = malloc(self_addr_size);
    if (self_addr_str == NULL) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
311 312
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_size, self_addr);
    if (hret != HG_SUCCESS) goto fini;
313
    self_addr_size_int = (int)self_addr_size; /* null char included in call */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
314

315
    /* gather the buffer sizes */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
316 317 318 319 320 321 322
    MPI_Comm_size(comm, &comm_size);
    MPI_Comm_rank(comm, &comm_rank);
    sizes = malloc(comm_size * sizeof(*sizes));
    if (sizes == NULL) goto fini;
    sizes[comm_rank] = self_addr_size_int;
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);

323 324 325
    /* compute a exclusive prefix sum of the data sizes, including the
     * total at the end
     */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
326 327 328 329 330 331
    sizes_psum = malloc((comm_size+1) * sizeof(*sizes_psum));
    if (sizes_psum == NULL) goto fini;
    sizes_psum[0] = 0;
    for (int i = 1; i < comm_size+1; i++)
        sizes_psum[i] = sizes_psum[i-1] + sizes[i-1];

332
    /* allgather the addresses */
333 334
    addr_buf = malloc(sizes_psum[comm_size]);
    if (addr_buf == NULL) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
335
    MPI_Allgatherv(self_addr_str, self_addr_size_int, MPI_BYTE,
336
            addr_buf, sizes, sizes_psum, MPI_BYTE, comm);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
337

338 339 340 341 342
    /* set up address string array for group members */
    addr_strs = ssg_setup_addr_str_list(addr_buf, comm_size);
    if (!addr_strs) goto fini;

    /* invoke the generic group create routine using our list of addrs */
343
    sret = ssg_group_create(group_name, addr_strs, comm_size, group_id);
Shane Snyder's avatar
Shane Snyder committed
344 345

fini:
346
    /* cleanup before returning */
Shane Snyder's avatar
Shane Snyder committed
347 348
    free(sizes);
    free(sizes_psum);
349
    free(addr_buf);
350 351
    if (hgcl && self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
352 353
    free(addr_strs);

354
    return sret;
Shane Snyder's avatar
Shane Snyder committed
355 356 357
}
#endif

358 359
int ssg_group_destroy(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
360
{
361 362 363 364
    int i;
    ssg_group_t *g = the_group;
    assert(g);

365
#if USE_SWIM_FD
366 367
    swim_context_t *swim_ctx = (swim_context_t *)g->fd_ctx;
    assert(swim_ctx);
368 369
    if(swim_ctx)
        swim_finalize(swim_ctx);
370
#endif
371

372 373
    for (i = 0; i < g->view.group_size; i++)
    {
374 375
        if (g->view.member_states[i].addr != HG_ADDR_NULL)
            HG_Addr_free(margo_get_class(ssg_mid), g->view.member_states[i].addr);
376
    }
377 378 379
    free(g->view.member_states);
    free(g);

380
    return SSG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
381 382
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396
int ssg_group_attach(
    ssg_group_id_t group_id)
{

    return SSG_SUCCESS;
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{

    return SSG_SUCCESS;
}

Shane Snyder's avatar
Shane Snyder committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
#if 0
/*** SSG group membership view access routines */

int ssg_get_group_rank(const ssg_t s)
{
    return s->view.self_rank;
}

int ssg_get_group_size(const ssg_t s)
{
    return s->view.group_size;
}

hg_addr_t ssg_get_addr(const ssg_t s, int rank)
{
    if (rank >= 0 && rank < s->view.group_size)
        return s->view.member_states[rank].addr;
    else
        return HG_ADDR_NULL;
}
#endif

419 420 421 422 423
/***************************
 *** SSG helper routines ***
 ***************************/

static void ssg_lookup_ult(void * arg);
424 425
struct lookup_ult_args
{
426
    ssg_group_t *g;
427
    int rank;
428
    const char *addr_str;
429 430 431
    hg_return_t out;
};

432 433
static hg_return_t ssg_group_lookup(
    ssg_group_t * g, const char * const addr_strs[])
Jonathan Jenkins's avatar
Jonathan Jenkins committed
434
{
435 436
    ABT_thread *ults;
    struct lookup_ult_args *args;
437
    int i, r;
438
    int aret;
439
    hg_return_t hret = HG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
440

441
    if (g == NULL) return HG_INVALID_PARAM;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
442

443 444
    /* initialize ULTs */
    ults = malloc(g->view.group_size * sizeof(*ults));
445
    if (ults == NULL) return HG_NOMEM_ERROR;
446
    args = malloc(g->view.group_size * sizeof(*args));
447 448
    if (args == NULL)
    {
449 450 451
        free(ults);
        return HG_NOMEM_ERROR;
    }
452
    for (i = 0; i < g->view.group_size; i++)
453 454
        ults[i] = ABT_THREAD_NULL;

455 456
    for (i = 1; i < g->view.group_size; i++)
    {
457 458
        r = (g->self_rank + i) % g->view.group_size;
        args[r].g = g;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
459
        args[r].rank = r;
460
        args[r].addr_str = addr_strs[r];
461
#if 0
462
        aret = ABT_thread_create(*margo_get_handler_pool(ssg_mid), &ssg_lookup_ult,
Jonathan Jenkins's avatar
Jonathan Jenkins committed
463
                &args[r], ABT_THREAD_ATTR_NULL, &ults[r]);
464 465
        if (aret != ABT_SUCCESS) {
            hret = HG_OTHER_ERROR;
466
            goto fini;
467
        }
468
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
469
    }
470

471
    /* wait on all */
472 473
    for (i = 1; i < g->view.group_size; i++)
    {
474
        r = (g->self_rank + i) % g->view.group_size;
475
#if 1
476
        aret = ABT_thread_create(*margo_get_handler_pool(ssg_mid), &ssg_lookup_ult,
477
                &args[r], ABT_THREAD_ATTR_NULL, &ults[r]);
478 479
        if (aret != ABT_SUCCESS)
        {
480 481 482
            hret = HG_OTHER_ERROR;
            goto fini;
        }
483
#endif
484
        aret = ABT_thread_join(ults[r]);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
485 486
        ABT_thread_free(&ults[r]);
        ults[r] = ABT_THREAD_NULL; // in case of cascading failure from join
487 488
        if (aret != ABT_SUCCESS)
        {
489
            hret = HG_OTHER_ERROR;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
490
            break;
491
        }
492 493
        else if (args[r].out != HG_SUCCESS)
        {
Shane Snyder's avatar
Shane Snyder committed
494
            fprintf(stderr, "Error: SSG unable to lookup HG address for rank %d"
495
                "(err=%d)\n", r, args[r].out);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
496 497
            hret = args[r].out;
            break;
498 499 500
        }
    }

501
fini:
502
    /* cleanup */
503 504 505 506
    for (i = 0; i < g->view.group_size; i++)
    {
        if (ults[i] != ABT_THREAD_NULL)
        {
507 508
            ABT_thread_cancel(ults[i]);
            ABT_thread_free(ults[i]);
509 510
        }
    }
511 512
    free(ults);
    free(args);
513 514

    return hret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
515
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
516

517 518
static void ssg_lookup_ult(
    void * arg)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
519
{
520 521
    struct lookup_ult_args *l = arg;
    ssg_group_t *g = l->g;
522

523 524 525 526
    l->out = margo_addr_lookup(ssg_mid, l->addr_str,
        &g->view.member_states[l->rank].addr);
    return;
}
527

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
static void ssg_generate_group_id(
    const char * name, const char * leader_addr_str,
    ssg_group_id_t *group_id)
{
    uint32_t upper, lower;

    /* hash the group name to obtain an 64-bit unique ID */
    ssg_hashlittle2(name, strlen(name), &lower, &upper);

    group_id->magic_nr = SSG_MAGIC_NR;
    group_id->name_hash = lower + (((uint64_t)upper)<<32);
    strcpy(group_id->addr_str, leader_addr_str);

    return;
}

544 545 546 547 548 549 550
static const char ** ssg_setup_addr_str_list(
    char * buf, int num_addrs)
{
    const char **ret = malloc(num_addrs * sizeof(*ret));
    if (ret == NULL) return NULL;

    ret[0] = buf;
551 552
    for (int i = 1; i < num_addrs; i++)
    {
553 554
        const char * a = ret[i-1];
        ret[i] = a + strlen(a) + 1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
555
    }
556
    return ret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
557
}