ssg.c 18.1 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>
Shane Snyder's avatar
Shane Snyder committed
17
#ifdef SSG_HAVE_MPI
18 19
#include <mpi.h>
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
20

Shane Snyder's avatar
Shane Snyder committed
21
#include <mercury.h>
22
#include <abt.h>
Shane Snyder's avatar
Shane Snyder committed
23
#include <margo.h>
24

25
#include "ssg.h"
Shane Snyder's avatar
Shane Snyder committed
26 27 28
#ifdef SSG_HAVE_MPI
#include "ssg-mpi.h"
#endif
29
#include "ssg-internal.h"
30
#ifdef SSG_USE_SWIM_FD
31 32
#include "swim-fd/swim-fd.h"
#endif
33
#include "uthash.h"
34

35
/* SSG helper routine prototypes */
36 37
static int ssg_group_destroy_internal(
    ssg_group_t *g);
38 39
static hg_return_t ssg_group_lookup(
    ssg_group_t * g, const char * const addr_strs[]);
40 41 42
static void ssg_generate_group_id(
    const char * name, const char * leader_addr_str,
    ssg_group_id_t *group_id);
43 44
static const char ** ssg_setup_addr_str_list(
    char * buf, int num_addrs);
45

46 47
/* XXX: i think we ultimately need per-mid ssg instances rather than 1 global? */
ssg_instance_t *ssg_inst = NULL;
48

49 50 51 52
DECLARE_MARGO_RPC_HANDLER(ssg_attach_recv_ult)

static hg_id_t ssg_attach_rpc_id;

53 54 55
/***************************************************
 *** SSG runtime intialization/shutdown routines ***
 ***************************************************/
56

57 58
int ssg_init(
    margo_instance_id mid)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
59
{
60 61
    hg_class_t *hg_cls = margo_get_class(mid);

62 63 64 65 66 67 68 69 70 71
    if (ssg_inst)
        return SSG_FAILURE;

    /* initialize an SSG instance for this margo instance */
    ssg_inst = malloc(sizeof(*ssg_inst));
    if (!ssg_inst)
        return SSG_FAILURE;
    memset(ssg_inst, 0, sizeof(*ssg_inst));
    ssg_inst->mid = mid;

72 73 74 75
    /* register HG RPCs for SSG */
    ssg_attach_rpc_id = MERCURY_REGISTER(hg_cls, "ssg_attach", void, void,
        ssg_attach_recv_ult_handler);

76 77
    return SSG_SUCCESS;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
78

79
int ssg_finalize()
80
{
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    ssg_group_t *g, *tmp;

    if (!ssg_inst)
        return SSG_FAILURE;

    /* destroy all active groups */
    HASH_ITER(hh, ssg_inst->group_table, g, tmp)
    {
        HASH_DELETE(hh, ssg_inst->group_table, g);
        ssg_group_destroy_internal(g);
    }

    free(ssg_inst);
    ssg_inst = NULL;

96
    return SSG_SUCCESS;
97
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
98

99 100 101
/*************************************
 *** SSG group management routines ***
 *************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
102

103
int ssg_group_create(
104 105
    const char * group_name,
    const char * const group_addr_strs[],
106 107
    int group_size,
    ssg_group_id_t * group_id)
108
{
109
    hg_class_t *hgcl = NULL;
Shane Snyder's avatar
Shane Snyder committed
110
    hg_addr_t self_addr = HG_ADDR_NULL;
111
    char *self_addr_str = NULL;
112
    hg_size_t self_addr_str_size = 0;
113 114 115 116
    const char *self_addr_substr = NULL;
    const char *addr_substr = NULL;
    int i;
    ssg_group_t *g = NULL;
117
    ssg_group_id_t new_gid;
118
    hg_return_t hret;
119
    int sret = SSG_FAILURE;
120

121 122 123
    if (!ssg_inst) goto fini;

    hgcl = margo_get_class(ssg_inst->mid);
124
    if (!hgcl) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    /* generate a unique ID for this group  */
    ssg_generate_group_id(group_name, group_addr_strs[0], &new_gid);

    /* make sure we aren't re-adding an existing group */
    HASH_FIND(hh, ssg_inst->group_table, &new_gid.name_hash, sizeof(uint64_t), g);
    if (g) goto fini;

    /* 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->group_name = strdup(group_name);
    if (!g->group_name) goto fini;
    memcpy(&g->group_id, &new_gid, sizeof(new_gid));
    // TODO? g->self_id = -1;
    g->group_view.size = group_size;
    g->group_view.member_states = malloc(
        group_size * sizeof(*g->group_view.member_states));
    if (!g->group_view.member_states) goto fini;
    memset(g->group_view.member_states, 0,
        group_size * sizeof(*g->group_view.member_states));

148
    /* get my address */
149 150
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto fini;
151
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_str_size, self_addr);
152
    if (hret != HG_SUCCESS) goto fini;
153
    self_addr_str = malloc(self_addr_str_size);
154
    if (self_addr_str == NULL) goto fini;
155
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_str_size, self_addr);
156 157
    if (hret != HG_SUCCESS) goto fini;

158 159 160 161
    /* 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)
     */
162
    self_addr_substr = strstr(self_addr_str, "://");
Shane Snyder's avatar
Shane Snyder committed
163 164 165 166
    if (self_addr_substr == NULL)
        self_addr_substr = self_addr_str;
    else
        self_addr_substr += 3;
Shane Snyder's avatar
Shane Snyder committed
167

168 169 170 171
    /* 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
172 173 174 175
        if (addr_substr == NULL)
            addr_substr = group_addr_strs[i];
        else
            addr_substr += 3;
176 177 178
        if (strcmp(self_addr_substr, addr_substr) == 0)
        {
            /* this is my address -- my rank is the offset in the address array */
179 180
            g->self_id = i; // TODO 
            g->group_view.member_states[i].addr = self_addr;
181 182 183 184
        }
        else
        {
            /* initialize group member addresses to NULL before looking them up */
185
            g->group_view.member_states[i].addr = HG_ADDR_NULL;
186
        }
187
        g->group_view.member_states[i].is_member = 1;
188
    }
189
#if 0
190
    /* TODO: if unable to resolve my rank within the group, error out */
191
    if (g->self_id == -1)
192 193 194 195 196
    {
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
    }
197
#endif
198

199
    /* lookup hg address information for all group members */
200 201 202 203 204 205 206
    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
207
    SSG_DEBUG(g, "group lookup successful (size=%d)\n", group_size);
208

209
#ifdef SSG_USE_SWIM_FD
210 211 212 213 214 215
    int swim_active = 1;
#ifdef SWIM_FORCE_FAIL
    if (g->self_rank == 1)
        swim_active = 0;
#endif

216
    /* initialize swim failure detector */
217 218
    // TODO: we should probably barrier or sync somehow to avoid rpc failures
    // due to timing skew of different ranks initializing swim
219
    g->fd_ctx = (void *)swim_init(g, swim_active);
220
    if (g->fd_ctx == NULL) goto fini;
221 222
#endif

223 224
    /* add this group reference to our group table */
    HASH_ADD(hh, ssg_inst->group_table, group_id.name_hash, sizeof(uint64_t), g);
225

226 227
    /* everything successful -- set the output for this call */
    memcpy(group_id, &new_gid, sizeof(new_gid));
228
    sret = SSG_SUCCESS;
229

230
    /* don't free these pointers on success */
231
    self_addr = HG_ADDR_NULL;
232 233
    g = NULL;
fini:
Shane Snyder's avatar
Shane Snyder committed
234 235
    if (hgcl && self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
236 237
    if (g)
    {
238 239
        free(g->group_name);
        free(g->group_view.member_states);
240 241 242
        free(g);
    }

243
    return sret;
244 245
}

246
int ssg_group_create_config(
247
    const char * group_name,
248 249
    const char * file_name,
    ssg_group_id_t * group_id)
250 251 252 253 254 255 256 257 258 259
{
    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;
260
    int sret = SSG_FAILURE;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

    /* 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
296 297
    if (tok == NULL) goto fini;

298 299
    /* build up the address buffer */
    addr_buf = malloc(rd_buf_sz);
300
    if (addr_buf == NULL) goto fini;
301 302
    do
    {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
303
        int tok_sz = strlen(tok);
304 305
        memcpy((char*)addr_buf + addr_buf_len, tok, tok_sz+1);
        addr_buf_len += tok_sz+1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
306 307 308
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
309 310 311 312 313 314 315
    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
316

317 318 319
    /* set up address string array for group members */
    addr_strs = ssg_setup_addr_str_list(addr_buf, num_addrs);
    if (!addr_strs) goto fini;
320

321
    /* invoke the generic group create routine using our list of addrs */
322
    sret = ssg_group_create(group_name, addr_strs, num_addrs, group_id);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
323 324

fini:
325
    /* cleanup before returning */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
326
    if (fd != -1) close(fd);
327
    free(rd_buf);
328
    free(addr_buf);
329 330
    free(addr_strs);

331
    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
332 333
}

Shane Snyder's avatar
Shane Snyder committed
334
#ifdef SSG_HAVE_MPI
335
int ssg_group_create_mpi(
336
    const char * group_name,
337 338
    MPI_Comm comm,
    ssg_group_id_t * group_id)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
339
{
340
    hg_class_t *hgcl = NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
341
    hg_addr_t self_addr = HG_ADDR_NULL;
342
    char *self_addr_str = NULL;
343 344
    hg_size_t self_addr_str_size = 0;
    int self_addr_str_size_int = 0; /* for mpi-friendly conversion */
345 346 347 348 349
    char *addr_buf = NULL;
    int *sizes = NULL;
    int *sizes_psum = NULL;
    int comm_size = 0, comm_rank = 0;
    const char **addr_strs = NULL;
350
    hg_return_t hret;
351
    int sret = SSG_FAILURE;
352

353 354 355
    if (!ssg_inst) goto fini;

    hgcl = margo_get_class(ssg_inst->mid);
356
    if (!hgcl) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
357

358
    /* get my address */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
359 360
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto fini;
361
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_str_size, self_addr);
Shane Snyder's avatar
Shane Snyder committed
362
    if (hret != HG_SUCCESS) goto fini;
363
    self_addr_str = malloc(self_addr_str_size);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
364
    if (self_addr_str == NULL) goto fini;
365
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_str_size, self_addr);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
366
    if (hret != HG_SUCCESS) goto fini;
367
    self_addr_str_size_int = (int)self_addr_str_size; /* null char included in call */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
368

369
    /* gather the buffer sizes */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
370 371 372 373
    MPI_Comm_size(comm, &comm_size);
    MPI_Comm_rank(comm, &comm_rank);
    sizes = malloc(comm_size * sizeof(*sizes));
    if (sizes == NULL) goto fini;
374
    sizes[comm_rank] = self_addr_str_size_int;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
375 376
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);

377 378 379
    /* compute a exclusive prefix sum of the data sizes, including the
     * total at the end
     */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
380 381 382 383 384 385
    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];

386
    /* allgather the addresses */
387 388
    addr_buf = malloc(sizes_psum[comm_size]);
    if (addr_buf == NULL) goto fini;
389
    MPI_Allgatherv(self_addr_str, self_addr_str_size_int, MPI_BYTE,
390
            addr_buf, sizes, sizes_psum, MPI_BYTE, comm);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
391

392 393 394 395 396
    /* 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 */
397
    sret = ssg_group_create(group_name, addr_strs, comm_size, group_id);
Shane Snyder's avatar
Shane Snyder committed
398 399

fini:
400
    /* cleanup before returning */
Shane Snyder's avatar
Shane Snyder committed
401 402
    free(sizes);
    free(sizes_psum);
403
    free(addr_buf);
404 405
    if (hgcl && self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
406 407
    free(addr_strs);

408
    return sret;
Shane Snyder's avatar
Shane Snyder committed
409 410 411
}
#endif

412 413
int ssg_group_destroy(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
414
{
415 416
    ssg_group_t *g;
    int sret;
417

418 419
    if (!ssg_inst)
        return SSG_FAILURE;
420

421 422 423 424
    /* find the group structure and destroy it */
    HASH_FIND(hh, ssg_inst->group_table, &group_id.name_hash, sizeof(uint64_t), g);
    HASH_DELETE(hh, ssg_inst->group_table, g);
    sret = ssg_group_destroy_internal(g);
425

426
    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
427 428
}

429 430 431
int ssg_group_attach(
    ssg_group_id_t group_id)
{
432
#if 0
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    hg_class_t *hgcl = NULL;
    hg_addr_t srvr_addr = HG_ADDR_NULL;
    hg_handle_t handle = HG_HANDLE_NULL;
    hg_return_t hret;

    hgcl = margo_get_class(ssg_mid);
    if (!hgcl) goto fini;

    /* lookup the address of the given group's leader server */
    hret = margo_addr_lookup(ssg_mid, group_id.addr_str, &srvr_addr);
    if (hret != HG_SUCCESS) goto fini;

    hret = HG_Create(margo_get_context(ssg_mid), srvr_addr, ssg_attach_rpc_id,
        &handle);
    if (hret != HG_SUCCESS) goto fini;

    /* XXX: send a request to the leader addr to attach to the group */
    hret = margo_forward(ssg_mid, handle, NULL);
    if (hret != HG_SUCCESS) goto fini;

    /* XXX: store the obtained view locally to refer to */

    /* TODO: hold on to leader addr so we don't have to look it up again? */
fini:
    if (hgcl && srvr_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, srvr_addr);
    if (handle != HG_HANDLE_NULL) HG_Destroy(handle);
459

460
#endif
461 462 463 464 465 466 467 468 469
    return SSG_SUCCESS;
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{
    return SSG_SUCCESS;
}

470 471 472
/*********************************
 *** SSG group access routines ***
 *********************************/
Shane Snyder's avatar
Shane Snyder committed
473

474
ssg_member_id_t ssg_get_group_self_id(
475
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
476
{
477 478 479 480 481 482 483 484 485 486
    ssg_group_t *g;

    if (!ssg_inst)
        return SSG_MEMBER_ID_INVALID;

    HASH_FIND(hh, ssg_inst->group_table, &group_id.name_hash, sizeof(uint64_t), g);
    if (!g)
        return SSG_MEMBER_ID_INVALID;

    return g->self_id;
Shane Snyder's avatar
Shane Snyder committed
487 488
}

489 490
int ssg_get_group_size(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
491
{
492 493 494 495 496 497 498 499 500 501
    ssg_group_t *g;

    if (!ssg_inst)
        return 0;

    HASH_FIND(hh, ssg_inst->group_table, &group_id.name_hash, sizeof(uint64_t), g);
    if (!g)
        return 0;

    return g->group_view.size;
Shane Snyder's avatar
Shane Snyder committed
502 503
}

504 505 506
hg_addr_t ssg_get_addr(
    ssg_group_id_t group_id,
    ssg_member_id_t member_id)
Shane Snyder's avatar
Shane Snyder committed
507
{
508 509 510 511 512 513 514 515 516
    ssg_group_t *g;

    if (!ssg_inst)
        return HG_ADDR_NULL;

    HASH_FIND(hh, ssg_inst->group_table, &group_id.name_hash, sizeof(uint64_t), g);
    if (!g)
        return HG_ADDR_NULL;

517
    return g->group_view.member_states[member_id].addr;
Shane Snyder's avatar
Shane Snyder committed
518 519
}

520 521 522
/************************************
 *** SSG internal helper routines ***
 ************************************/
523

524 525 526 527 528 529
static int ssg_group_destroy_internal(ssg_group_t *g)
{
    int i;

    /* TODO: send a leave message to the group ? */

530
#ifdef SSG_USE_SWIM_FD
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    /* free up failure detector state */
    if(g->fd_ctx)
        swim_finalize(g->fd_ctx);
#endif

    /* destroy group state */
    for (i = 0; i < g->group_view.size; i++)
    {
        if (g->group_view.member_states[i].addr != HG_ADDR_NULL)
        {
            HG_Addr_free(margo_get_class(ssg_inst->mid),
                g->group_view.member_states[i].addr);
        }
    }
    free(g->group_name);
    free(g->group_view.member_states);
    free(g);

    return SSG_SUCCESS;
}

552
static void ssg_lookup_ult(void * arg);
553 554
struct lookup_ult_args
{
555
    ssg_group_t *g;
556
    ssg_member_id_t member_id;
557
    const char *addr_str;
558 559 560
    hg_return_t out;
};

561 562
static hg_return_t ssg_group_lookup(
    ssg_group_t * g, const char * const addr_strs[])
Jonathan Jenkins's avatar
Jonathan Jenkins committed
563
{
564 565
    ABT_thread *ults;
    struct lookup_ult_args *args;
566
    int i, r;
567
    int aret;
568
    hg_return_t hret = HG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
569

570
    if (g == NULL) return HG_INVALID_PARAM;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
571

572
    /* initialize ULTs */
573
    ults = malloc(g->group_view.size * sizeof(*ults));
574
    if (ults == NULL) return HG_NOMEM_ERROR;
575
    args = malloc(g->group_view.size * sizeof(*args));
576 577
    if (args == NULL)
    {
578 579 580
        free(ults);
        return HG_NOMEM_ERROR;
    }
581
    for (i = 0; i < g->group_view.size; i++)
582 583
        ults[i] = ABT_THREAD_NULL;

584
    for (i = 1; i < g->group_view.size; i++)
585
    {
586
        r = (g->self_id + i) % g->group_view.size;
587
        args[r].g = g;
588
        args[r].member_id = r;
589
        args[r].addr_str = addr_strs[r];
590 591
        aret = ABT_thread_create(*margo_get_handler_pool(ssg_inst->mid),
                &ssg_lookup_ult, &args[r], ABT_THREAD_ATTR_NULL, &ults[r]);
592 593
        if (aret != ABT_SUCCESS) {
            hret = HG_OTHER_ERROR;
594
            goto fini;
595
        }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
596
    }
597

598
    /* wait on all */
599
    for (i = 1; i < g->group_view.size; i++)
600
    {
601
        r = (g->self_id + i) % g->group_view.size;
602
        aret = ABT_thread_join(ults[r]);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
603 604
        ABT_thread_free(&ults[r]);
        ults[r] = ABT_THREAD_NULL; // in case of cascading failure from join
605 606
        if (aret != ABT_SUCCESS)
        {
607
            hret = HG_OTHER_ERROR;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
608
            break;
609
        }
610 611
        else if (args[r].out != HG_SUCCESS)
        {
Shane Snyder's avatar
Shane Snyder committed
612
            fprintf(stderr, "Error: SSG unable to lookup HG address for rank %d"
613
                "(err=%d)\n", r, args[r].out);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
614 615
            hret = args[r].out;
            break;
616 617 618
        }
    }

619
fini:
620
    /* cleanup */
621
    for (i = 0; i < g->group_view.size; i++)
622 623 624
    {
        if (ults[i] != ABT_THREAD_NULL)
        {
625 626
            ABT_thread_cancel(ults[i]);
            ABT_thread_free(ults[i]);
627 628
        }
    }
629 630
    free(ults);
    free(args);
631 632

    return hret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
633
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
634

635 636
static void ssg_lookup_ult(
    void * arg)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
637
{
638 639
    struct lookup_ult_args *l = arg;
    ssg_group_t *g = l->g;
640

641
    l->out = margo_addr_lookup(ssg_inst->mid, l->addr_str,
642
        &g->group_view.member_states[l->member_id].addr);
643 644
    return;
}
645

646 647 648 649 650 651 652
static void ssg_attach_recv_ult(hg_handle_t handle)
{
    HG_Destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(ssg_attach_recv_ult)

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
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;
}

669 670 671 672 673 674 675
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;
676 677
    for (int i = 1; i < num_addrs; i++)
    {
678 679
        const char * a = ret[i-1];
        ret[i] = a + strlen(a) + 1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
680
    }
681
    return ret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
682
}