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

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

26
#include "ssg.h"
Shane Snyder's avatar
Shane Snyder committed
27 28 29
#ifdef SSG_HAVE_MPI
#include "ssg-mpi.h"
#endif
30
#include "ssg-internal.h"
31

32 33 34
/* arguments for group lookup ULTs */
struct ssg_group_lookup_ult_args
{
35
    ssg_group_view_t *view;
36 37 38 39 40
    ssg_member_state_t *member_state;
    hg_return_t out;
};
static void ssg_group_lookup_ult(void * arg);

41
/* SSG helper routine prototypes */
42
static ssg_group_descriptor_t * ssg_group_descriptor_create(
43
    uint64_t name_hash, const char * leader_addr_str, int owner_status);
44 45 46 47 48
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor);
static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor);
static int ssg_group_view_create(
49 50 51
    const char * const group_addr_strs[], int group_size,
    hg_addr_t self_addr, ssg_member_id_t * self_id,
    ssg_group_view_t * view);
52 53
static ssg_member_id_t ssg_gen_member_id(
    const char * addr_str);
54 55 56 57 58 59 60 61
static void ssg_group_view_destroy(
    ssg_group_view_t * view);
static void ssg_group_destroy_internal(
    ssg_group_t * g);
static void ssg_attached_group_destroy(
    ssg_attached_group_t * ag);
static const char ** ssg_addr_str_buf_to_list(
    const char * buf, int num_addrs);
62

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

66 67 68
/***************************************************
 *** SSG runtime intialization/shutdown routines ***
 ***************************************************/
69

70 71
int ssg_init(
    margo_instance_id mid)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
72
{
73 74
    struct timespec ts;

75 76 77 78 79 80 81 82 83 84
    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;

85
    ssg_register_rpcs();
86

87 88 89 90
    /* seed RNG */
    clock_gettime(CLOCK_MONOTONIC, &ts);
    srand(ts.tv_nsec + getpid());

91 92
    return SSG_SUCCESS;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
93

94
int ssg_finalize()
95
{
96 97
    ssg_group_t *g, *g_tmp;
    ssg_attached_group_t *ag, *ag_tmp;
98 99 100 101 102

    if (!ssg_inst)
        return SSG_FAILURE;

    /* destroy all active groups */
103
    HASH_ITER(hh, ssg_inst->group_table, g, g_tmp)
104 105 106 107 108
    {
        HASH_DELETE(hh, ssg_inst->group_table, g);
        ssg_group_destroy_internal(g);
    }

109 110 111 112 113 114 115
    /* detach from all attached groups */
    HASH_ITER(hh, ssg_inst->attached_group_table, ag, ag_tmp)
    {
        HASH_DELETE(hh, ssg_inst->attached_group_table, ag);
        ssg_attached_group_destroy(ag);
    }

116 117 118
    free(ssg_inst);
    ssg_inst = NULL;

119
    return SSG_SUCCESS;
120
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
121

122 123 124
/*************************************
 *** SSG group management routines ***
 *************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
125

126 127
static int ssg_get_swim_dping_target(
    void *group_data,
Shane Snyder's avatar
Shane Snyder committed
128 129 130 131 132
    swim_member_id_t *target_id,
    swim_member_inc_nr_t *target_inc_nr,
    hg_addr_t *target_addr);
static int ssg_get_swim_iping_targets(
    void *group_data,
133
    swim_member_id_t dping_target_id,
Shane Snyder's avatar
Shane Snyder committed
134
    int *num_targets,
Shane Snyder's avatar
Shane Snyder committed
135 136 137 138 139 140 141 142 143
    swim_member_id_t *target_ids,
    hg_addr_t *target_addrs);
static void ssg_get_swim_member_addr(
    void *group_data,
    swim_member_id_t id,
    hg_addr_t *target_addr);
static void ssg_get_swim_member_state(
    void *group_data,
    swim_member_id_t id,
144
    swim_member_state_t **state);
145 146 147
static void ssg_apply_swim_member_update(
    void *group_data,
    swim_member_update_t update);
Shane Snyder's avatar
Shane Snyder committed
148

149
static void ssg_shuffle_member_list(
Shane Snyder's avatar
Shane Snyder committed
150 151 152 153
    ssg_group_t *g);

static int ssg_get_swim_dping_target(
    void *group_data,
Shane Snyder's avatar
Shane Snyder committed
154 155 156
    swim_member_id_t *target_id,
    swim_member_inc_nr_t *target_inc_nr,
    hg_addr_t *target_addr)
157 158
{
    ssg_group_t *g = (ssg_group_t *)group_data;
159
    ssg_member_state_t **target_ms_p;
160 161 162

    assert(g != NULL);

163 164 165
    /* XXX MUTEX */

    if (g->dping_target_ndx == 0)
Shane Snyder's avatar
Shane Snyder committed
166
    {
167 168
        /* reshuffle member list */
        ssg_shuffle_member_list(g);
Shane Snyder's avatar
Shane Snyder committed
169
    }
Shane Snyder's avatar
Shane Snyder committed
170

171 172 173 174 175
    target_ms_p = (ssg_member_state_t **)utarray_eltptr(
        g->nondead_member_list, g->dping_target_ndx);
    *target_id = (swim_member_id_t)(*target_ms_p)->id;
    *target_inc_nr = (*target_ms_p)->swim_state.inc_nr;
    *target_addr = (*target_ms_p)->addr;
Shane Snyder's avatar
Shane Snyder committed
176

177 178 179 180
    /* point at next dping target in the view */
    g->dping_target_ndx++;
    if (g->dping_target_ndx == g->nondead_member_count)
        g->dping_target_ndx = 0;
Shane Snyder's avatar
Shane Snyder committed
181

182
    return 0;
Shane Snyder's avatar
Shane Snyder committed
183 184
}

Shane Snyder's avatar
Shane Snyder committed
185 186
static int ssg_get_swim_iping_targets(
    void *group_data,
187
    swim_member_id_t dping_target_id,
Shane Snyder's avatar
Shane Snyder committed
188
    int *num_targets,
189
    swim_member_id_t *target_ids,
Shane Snyder's avatar
Shane Snyder committed
190 191 192 193
    hg_addr_t *target_addrs)
{
    ssg_group_t *g = (ssg_group_t *)group_data;
    int max_targets = *num_targets;
194 195 196 197
    int iping_target_count = 0;
    int i = 0;
    unsigned int r_start, r_ndx;
    ssg_member_state_t **r_ms_p;
Shane Snyder's avatar
Shane Snyder committed
198 199 200 201 202

    assert(g != NULL);

    *num_targets = 0;

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    /* XXX MUTEX */

    /* pick random index in the nondead list, and pull out a set of iping
     * targets starting from that index
     */
    r_start = rand() % g->nondead_member_count;
    while (iping_target_count < max_targets)
    {
        r_ndx = (r_start + i) % g->nondead_member_count;
        /* if we've iterated through the entire nondead list, stop */
        if ((i > 0 ) && (r_ndx == r_start)) break;

        r_ms_p = (ssg_member_state_t **)utarray_eltptr(g->nondead_member_list, r_ndx);

        /* do not select the dping target as an iping target */
        if ((swim_member_id_t)(*r_ms_p)->id == dping_target_id)
        {
            i++;
            continue;
        }

        target_ids[iping_target_count] = (swim_member_id_t)(*r_ms_p)->id;
        target_addrs[iping_target_count] = (*r_ms_p)->addr;
        iping_target_count++;
        i++;
    }

    *num_targets = iping_target_count;

Shane Snyder's avatar
Shane Snyder committed
232 233 234
    return 0;
}

Shane Snyder's avatar
Shane Snyder committed
235 236 237 238 239 240 241 242 243 244 245 246
static void ssg_get_swim_member_addr(
    void *group_data,
    swim_member_id_t id,
    hg_addr_t *addr)
{
    ssg_group_t *g = (ssg_group_t *)group_data;
    ssg_member_id_t ssg_id = (ssg_member_id_t)id;
    ssg_member_state_t *ms;

    assert(g != NULL);

    HASH_FIND(hh, g->view.member_map, &ssg_id, sizeof(ssg_member_id_t), ms);
Shane Snyder's avatar
Shane Snyder committed
247
    assert(ms != NULL);
Shane Snyder's avatar
Shane Snyder committed
248 249 250 251 252 253 254 255 256

    *addr = ms->addr;

    return;
}

static void ssg_get_swim_member_state(
    void *group_data,
    swim_member_id_t id,
257
    swim_member_state_t **state)
Shane Snyder's avatar
Shane Snyder committed
258 259 260 261 262 263 264 265
{
    ssg_group_t *g = (ssg_group_t *)group_data;
    ssg_member_id_t ssg_id = (ssg_member_id_t)id;
    ssg_member_state_t *ms;

    assert(g != NULL);

    HASH_FIND(hh, g->view.member_map, &ssg_id, sizeof(ssg_member_id_t), ms);
Shane Snyder's avatar
Shane Snyder committed
266
    assert(ms != NULL);
Shane Snyder's avatar
Shane Snyder committed
267

268
    *state = &ms->swim_state;
Shane Snyder's avatar
Shane Snyder committed
269 270 271 272

    return;
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
static void ssg_apply_swim_member_update(
    void *group_data,
    swim_member_update_t update)
{
    ssg_group_t *g = (ssg_group_t *)group_data;
    ssg_member_id_t ssg_id = (ssg_member_id_t)update.id;
    ssg_member_state_t *ms;
    ssg_member_update_t ssg_update;

    assert(g != NULL);

    if (update.state.status == SWIM_MEMBER_DEAD)
    {
        HASH_FIND(hh, g->view.member_map, &ssg_id, sizeof(ssg_member_id_t), ms);
        if (ms)
        {
            /* update group, but don't completely remove state */
            margo_addr_free(ssg_inst->mid, ms->addr);
            ssg_update.id = ssg_id;
            ssg_update.type = SSG_MEMBER_REMOVE;
        }
    }
    else
    {
        assert(0); /* XXX: dynamic group joins aren't possible yet */
    }

    /* execute user-supplied membership update callback, if given */
    if (g->update_cb)
        g->update_cb(ssg_update, g->update_cb_dat);

    return;
}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
static void ssg_shuffle_member_list(
    ssg_group_t *g)
{
    unsigned int i, r;
    ssg_member_state_t **tmp_ms;
    UT_array *list = g->nondead_member_list;
    unsigned int len = g->nondead_member_count;

    /* run fisher-yates shuffle over list of nondead members */
    for (i = len - 1; i > 0; i--)
    {
        r = rand() % (i + 1);
        tmp_ms = (ssg_member_state_t **)utarray_eltptr(list, r);
        utarray_erase(list, r, 1);
        utarray_insert(list, *(ssg_member_state_t **)utarray_eltptr(list, i), r);
        utarray_erase(list, i, 1);
        utarray_insert(list, *tmp_ms, i);
    }

    return;
}

Shane Snyder's avatar
Shane Snyder committed
329
ssg_group_id_t ssg_group_create(
330 331
    const char * group_name,
    const char * const group_addr_strs[],
332 333 334
    int group_size,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
335
{
336
    uint64_t name_hash;
Shane Snyder's avatar
Shane Snyder committed
337
    hg_addr_t self_addr = HG_ADDR_NULL;
338
    ssg_group_descriptor_t *tmp_descriptor;
339
    ssg_group_t *g = NULL;
340
    ssg_member_state_t *ms, *tmp_ms;
341
    hg_return_t hret;
342
    int sret;
Shane Snyder's avatar
Shane Snyder committed
343
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
344

345
    if (!ssg_inst) return group_id;
346

347
    name_hash = ssg_hash64_str(group_name);
348

349
    /* generate a unique ID for this group  */
350 351
    tmp_descriptor = ssg_group_descriptor_create(name_hash, group_addr_strs[0],
        SSG_OWNER_IS_MEMBER);
352
    if (tmp_descriptor == NULL) return group_id;
353

Shane Snyder's avatar
Shane Snyder committed
354
    /* make sure we aren't re-creating an existing group */
355
    HASH_FIND(hh, ssg_inst->group_table, &tmp_descriptor->name_hash,
Shane Snyder's avatar
Shane Snyder committed
356
        sizeof(uint64_t), g);
357 358 359 360 361
    if (g)
    {
        g = NULL;
        goto fini;
    }
362

363 364 365 366 367 368 369
    /* 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->descriptor = tmp_descriptor;
370 371
    g->update_cb = update_cb;
    g->update_cb_dat = update_cb_dat;
372

373 374 375 376
    /* get my address */
    hret = margo_addr_self(ssg_inst->mid, &self_addr);
    if (hret != HG_SUCCESS) goto fini;

377
    /* initialize the group view */
378 379
    sret = ssg_group_view_create(group_addr_strs, group_size, self_addr,
        &g->self_id, &g->view);
380
    if (sret != SSG_SUCCESS) goto fini;
381
    if (g->self_id == SSG_MEMBER_ID_INVALID)
382
    {
383
        /* if unable to resolve my rank within the group, error out */
384 385 386 387 388
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
    }

389 390 391 392 393 394 395 396 397
    /* create a list of all nondead member states (used for shuffling) */
    UT_icd ms_icd = {sizeof(ssg_member_state_t *), NULL, NULL, NULL};
    utarray_new(g->nondead_member_list, &ms_icd);
    utarray_reserve(g->nondead_member_list, g->view.size);
    HASH_ITER(hh, g->view.member_map, ms, tmp_ms)
    {
        utarray_push_back(g->nondead_member_list, ms);
    }
    g->nondead_member_count = g->view.size;
Shane Snyder's avatar
Shane Snyder committed
398

399
    /* initialize swim failure detector */
400 401
    // TODO: we should probably barrier or sync somehow to avoid rpc failures
    // due to timing skew of different ranks initializing swim
402 403
    swim_group_mgmt_callbacks_t swim_callbacks = {
        .get_dping_target = &ssg_get_swim_dping_target,
Shane Snyder's avatar
Shane Snyder committed
404 405 406
        .get_iping_targets = &ssg_get_swim_iping_targets,
        .get_member_addr = ssg_get_swim_member_addr,
        .get_member_state = ssg_get_swim_member_state,
407
        .apply_member_update = ssg_apply_swim_member_update,
408
    };
409 410
    g->swim_ctx = swim_init(ssg_inst->mid, g, (swim_member_id_t)g->self_id,
        swim_callbacks, 1);
Shane Snyder's avatar
Shane Snyder committed
411
    if (g->swim_ctx == NULL) goto fini;
412

Shane Snyder's avatar
Shane Snyder committed
413 414 415
    /* everything successful -- set the output group identifier, which is just
     * an opaque pointer to the group descriptor structure
     */
416 417
    group_id = (ssg_group_id_t)ssg_group_descriptor_dup(g->descriptor);
    if (group_id == SSG_GROUP_ID_NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
418

419 420 421 422
    /* add this group reference to our group table */
    HASH_ADD(hh, ssg_inst->group_table, descriptor->name_hash,
        sizeof(uint64_t), g);

Shane Snyder's avatar
Shane Snyder committed
423
    SSG_DEBUG(g, "group create successful (size=%d)\n", group_size);
424

425
    /* don't free group pointer on success */
426 427
    g = NULL;
fini:
428
    if (self_addr != HG_ADDR_NULL) margo_addr_free(ssg_inst->mid, self_addr);
429 430
    if (g)
    {
431
        ssg_group_view_destroy(&g->view);
432
        free(g->name);
433 434
        free(g);
    }
435 436
    if (group_id == SSG_GROUP_ID_NULL)
        ssg_group_descriptor_free(tmp_descriptor);
437

Shane Snyder's avatar
Shane Snyder committed
438
    return group_id;
439 440
}

Shane Snyder's avatar
Shane Snyder committed
441
ssg_group_id_t ssg_group_create_config(
442
    const char * group_name,
443 444 445
    const char * file_name,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
446 447 448 449
{
    int fd;
    struct stat st;
    char *rd_buf = NULL;
450
    ssize_t rd_buf_size;
451
    char *tok;
452 453
    void *addr_str_buf = NULL;
    int addr_str_buf_len = 0, num_addrs = 0;
454 455
    int ret;
    const char **addr_strs = NULL;
Shane Snyder's avatar
Shane Snyder committed
456
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

    /* 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 */
479 480
    rd_buf_size = read(fd, rd_buf, st.st_size);
    if (rd_buf_size != st.st_size)
481 482 483 484 485
    {
        fprintf(stderr, "Error: SSG unable to read config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }
486
    rd_buf[rd_buf_size]='\0';
487 488 489 490 491

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

494
    /* build up the address buffer */
495 496
    addr_str_buf = malloc(rd_buf_size);
    if (addr_str_buf == NULL) goto fini;
497 498
    do
    {
499 500 501
        int tok_size = strlen(tok);
        memcpy((char*)addr_str_buf + addr_str_buf_len, tok, tok_size+1);
        addr_str_buf_len += tok_size+1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
502 503 504
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
505
    if (addr_str_buf_len != rd_buf_size)
506 507
    {
        /* adjust buffer size if our initial guess was wrong */
508
        void *tmp = realloc(addr_str_buf, addr_str_buf_len);
509
        if (tmp == NULL) goto fini;
510
        addr_str_buf = tmp;
511
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
512

513
    /* set up address string array for group members */
514
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, num_addrs);
515
    if (!addr_strs) goto fini;
516

517
    /* invoke the generic group create routine using our list of addrs */
518 519
    group_id = ssg_group_create(group_name, addr_strs, num_addrs,
        update_cb, update_cb_dat);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
520 521

fini:
522
    /* cleanup before returning */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
523
    if (fd != -1) close(fd);
524
    free(rd_buf);
525
    free(addr_str_buf);
526 527
    free(addr_strs);

Shane Snyder's avatar
Shane Snyder committed
528
    return group_id;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
529 530
}

Shane Snyder's avatar
Shane Snyder committed
531
#ifdef SSG_HAVE_MPI
Shane Snyder's avatar
Shane Snyder committed
532
ssg_group_id_t ssg_group_create_mpi(
533
    const char * group_name,
534 535 536
    MPI_Comm comm,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
537
{
538
    int i;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
539
    hg_addr_t self_addr = HG_ADDR_NULL;
540
    char *self_addr_str = NULL;
541 542
    hg_size_t self_addr_str_size = 0;
    int self_addr_str_size_int = 0; /* for mpi-friendly conversion */
543
    char *addr_str_buf = NULL;
544 545 546 547
    int *sizes = NULL;
    int *sizes_psum = NULL;
    int comm_size = 0, comm_rank = 0;
    const char **addr_strs = NULL;
548
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
549
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
550

551 552
    if (!ssg_inst) goto fini;

553
    /* get my address */
554
    hret = margo_addr_self(ssg_inst->mid, &self_addr);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
555
    if (hret != HG_SUCCESS) goto fini;
556
    hret = margo_addr_to_string(ssg_inst->mid, NULL, &self_addr_str_size, self_addr);
Shane Snyder's avatar
Shane Snyder committed
557
    if (hret != HG_SUCCESS) goto fini;
558
    self_addr_str = malloc(self_addr_str_size);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
559
    if (self_addr_str == NULL) goto fini;
560
    hret = margo_addr_to_string(ssg_inst->mid, self_addr_str, &self_addr_str_size, self_addr);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
561
    if (hret != HG_SUCCESS) goto fini;
562
    self_addr_str_size_int = (int)self_addr_str_size; /* null char included in call */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
563

564
    /* gather the buffer sizes */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
565 566 567 568
    MPI_Comm_size(comm, &comm_size);
    MPI_Comm_rank(comm, &comm_rank);
    sizes = malloc(comm_size * sizeof(*sizes));
    if (sizes == NULL) goto fini;
569
    sizes[comm_rank] = self_addr_str_size_int;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
570 571
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);

572 573 574
    /* compute a exclusive prefix sum of the data sizes, including the
     * total at the end
     */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
575 576 577
    sizes_psum = malloc((comm_size+1) * sizeof(*sizes_psum));
    if (sizes_psum == NULL) goto fini;
    sizes_psum[0] = 0;
578
    for (i = 1; i < comm_size+1; i++)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
579 580
        sizes_psum[i] = sizes_psum[i-1] + sizes[i-1];

581
    /* allgather the addresses */
582 583
    addr_str_buf = malloc(sizes_psum[comm_size]);
    if (addr_str_buf == NULL) goto fini;
584
    MPI_Allgatherv(self_addr_str, self_addr_str_size_int, MPI_BYTE,
585
            addr_str_buf, sizes, sizes_psum, MPI_BYTE, comm);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
586

587
    /* set up address string array for group members */
588
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, comm_size);
589 590 591
    if (!addr_strs) goto fini;

    /* invoke the generic group create routine using our list of addrs */
592 593
    group_id = ssg_group_create(group_name, addr_strs, comm_size,
        update_cb, update_cb_dat);
Shane Snyder's avatar
Shane Snyder committed
594 595

fini:
596
    /* cleanup before returning */
597
    if (self_addr != HG_ADDR_NULL) margo_addr_free(ssg_inst->mid, self_addr);
598
    free(self_addr_str);
599 600 601
    free(sizes);
    free(sizes_psum);
    free(addr_str_buf);
602 603
    free(addr_strs);

Shane Snyder's avatar
Shane Snyder committed
604
    return group_id;
Shane Snyder's avatar
Shane Snyder committed
605 606 607
}
#endif

608 609
int ssg_group_destroy(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
610
{
Shane Snyder's avatar
Shane Snyder committed
611
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
612
    ssg_group_t *g;
613

Shane Snyder's avatar
Shane Snyder committed
614
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
615
        return SSG_FAILURE;
616

617 618 619 620 621 622
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG unable to destroy a group it is not a member of\n");
        return SSG_FAILURE;
    }

623
    /* find the group structure and destroy it */
Shane Snyder's avatar
Shane Snyder committed
624 625
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
626 627 628 629 630
    if (!g)
    {
        fprintf(stderr, "Error: SSG unable to find expected group reference\n");
        return SSG_FAILURE;
    }
631
    HASH_DELETE(hh, ssg_inst->group_table, g);
632
    ssg_group_destroy_internal(g);
633

634
    return SSG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
635 636
}

637 638 639
int ssg_group_attach(
    ssg_group_id_t group_id)
{
Shane Snyder's avatar
Shane Snyder committed
640
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
641 642 643 644 645 646
    ssg_attached_group_t *ag = NULL;
    char *group_name = NULL;
    int group_size;
    void *view_buf = NULL;
    const char **addr_strs = NULL;
    int sret = SSG_FAILURE;
647

648
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) goto fini;
649

650 651 652 653 654 655 656 657 658 659 660
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG unable to attach a group it is a member of\n");
        goto fini;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        fprintf(stderr, "Error: SSG unable to attach a group it is" \
            " already attached to\n");
        goto fini;
    }
661

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    /* send the attach request to a group member to initiate a bulk transfer
     * of the group's membership view
     */
    sret = ssg_group_attach_send(group_descriptor, &group_name,
        &group_size, &view_buf);
    if (sret != SSG_SUCCESS || !group_name || !view_buf) goto fini;

    /* set up address string array for group members */
    addr_strs = ssg_addr_str_buf_to_list(view_buf, group_size);
    if (!addr_strs) goto fini;

    /* allocate an SSG attached group data structure and initialize some of it */
    ag = malloc(sizeof(*ag));
    if (!ag) goto fini;
    memset(ag, 0, sizeof(*ag));
    ag->name = group_name;
    ag->descriptor = ssg_group_descriptor_dup(group_descriptor);
    if (!ag->descriptor) goto fini;
    ag->descriptor->owner_status = SSG_OWNER_IS_ATTACHER;

    /* create the view for the group */
683 684
    sret = ssg_group_view_create(addr_strs, group_size, HG_ADDR_NULL,
        NULL, &ag->view);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
    if (sret != SSG_SUCCESS) goto fini;

    /* add this group reference to our group table */
    HASH_ADD(hh, ssg_inst->attached_group_table, descriptor->name_hash,
        sizeof(uint64_t), ag);

    sret = SSG_SUCCESS;

    /* don't free on success */
    ag = NULL;
fini:
    free(view_buf);
    free(addr_strs);
    if (ag)
    {
700
        ssg_group_view_destroy(&ag->view);
701 702
        free(ag->name);
        free(ag);
703
        ssg_group_descriptor_free(ag->descriptor);
704 705 706
    }

    return sret;
707 708 709 710 711
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_attached_group_t *ag;

    if (group_descriptor->owner_status != SSG_OWNER_IS_ATTACHER)
    {
        fprintf(stderr, "Error: SSG unable to detach from group that" \
            " was never attached\n");
        return SSG_FAILURE;
    }

    /* find the attached group structure and destroy it */
    HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), ag);
    if (!ag)
    {
        fprintf(stderr, "Error: SSG unable to find expected attached group reference\n");
        return SSG_FAILURE;
    }
    HASH_DELETE(hh, ssg_inst->attached_group_table, ag);
    ssg_attached_group_destroy(ag);

733 734 735
    return SSG_SUCCESS;
}

736 737 738
/*********************************
 *** SSG group access routines ***
 *********************************/
Shane Snyder's avatar
Shane Snyder committed
739

740
ssg_member_id_t ssg_get_group_self_id(
741
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
742
{
Shane Snyder's avatar
Shane Snyder committed
743
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
744 745
    ssg_group_t *g;

Shane Snyder's avatar
Shane Snyder committed
746
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
747
        return SSG_MEMBER_ID_INVALID;
748

749 750 751 752
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG can only obtain a self ID from a group the" \
            " caller is a member of\n");
753
        return SSG_MEMBER_ID_INVALID;
754 755
    }

Shane Snyder's avatar
Shane Snyder committed
756 757
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
758
    if (!g)
759
        return SSG_MEMBER_ID_INVALID;
760 761

    return g->self_id;
Shane Snyder's avatar
Shane Snyder committed
762 763
}

764 765
int ssg_get_group_size(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
766
{
Shane Snyder's avatar
Shane Snyder committed
767
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
768
    ssg_group_view_t *group_view = NULL;
769

Shane Snyder's avatar
Shane Snyder committed
770
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
771 772
        return 0;

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
            group_view = &g->view;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
            group_view = &ag->view;
    }
    else
    {
        fprintf(stderr, "Error: SSG can only obtain size of groups that the caller" \
            " is a member of or an attacher of\n");
795
        return 0;
796
    }
797

798 799 800 801
    if (group_view)
        return group_view->size;
    else
        return 0;
Shane Snyder's avatar
Shane Snyder committed
802 803
}

804 805 806
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
807
{
Shane Snyder's avatar
Shane Snyder committed
808
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
809
    ssg_group_view_t *group_view = NULL;
810
    ssg_member_state_t *member_state;
811

812 813
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL ||
            member_id == SSG_MEMBER_ID_INVALID)
814 815
        return HG_ADDR_NULL;

816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
            group_view = &g->view;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
            group_view = &ag->view;
    }
    else
    {
        fprintf(stderr, "Error: SSG can only obtain member addresses of groups" \
            " that the caller is a member of or an attacher of\n");
        return HG_ADDR_NULL;
    }

    if (group_view)
    {
843 844 845 846 847
        HASH_FIND(hh, group_view->member_map, &member_id, sizeof(ssg_member_id_t),
            member_state);
        if (member_state)
            return member_state->addr;
        else
848 849 850
            return HG_ADDR_NULL;
    }
    else
851
        return HG_ADDR_NULL;
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
}

ssg_group_id_t ssg_group_id_dup(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *dup;

    dup = ssg_group_descriptor_dup((ssg_group_descriptor_t *)group_id);
    return (ssg_group_id_t)dup;
}

void ssg_group_id_free(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *descriptor = (ssg_group_descriptor_t *)group_id;

    ssg_group_descriptor_free(descriptor);
Shane Snyder's avatar
Shane Snyder committed
869
    descriptor = SSG_GROUP_ID_NULL;
870 871 872
    return;
}

873 874 875 876 877 878 879 880 881
char *ssg_group_id_get_addr_str(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;

    return strdup(group_descriptor->addr_str);
}


882 883 884 885 886 887 888
void ssg_group_id_serialize(
    ssg_group_id_t group_id,
    char ** buf_p,
    size_t * buf_size_p)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    size_t alloc_size;
889
    char *gid_buf, *p; 
890 891 892 893 894 895 896 897 898 899 900 901 902

    *buf_p = NULL;
    *buf_size_p = 0;

    /* determine needed buffer size */
    alloc_size = (sizeof(group_descriptor->magic_nr) + sizeof(group_descriptor->name_hash) +
        strlen(group_descriptor->addr_str) + 1);

    gid_buf = malloc(alloc_size);
    if (!gid_buf)
        return;

    /* serialize */
903 904 905 906 907 908
    p = gid_buf;
    *(uint64_t *)p = group_descriptor->magic_nr;
    p += sizeof(uint64_t);
    *(uint64_t *)p = group_descriptor->name_hash;
    p += sizeof(uint64_t);
    strcpy(p, group_descriptor->addr_str);
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    /* the rest of the descriptor is stateful and not appropriate for serializing... */

    *buf_p = gid_buf;
    *buf_size_p = alloc_size;

    return;
}

void ssg_group_id_deserialize(
    const char * buf,
    size_t buf_size,
    ssg_group_id_t * group_id_p)
{
    size_t min_buf_size;
    uint64_t magic_nr;
    uint64_t name_hash;
    const char *addr_str;
    ssg_group_descriptor_t *group_descriptor;

    *group_id_p = SSG_GROUP_ID_NULL;

    /* check to ensure the buffer contains enough data to make a group ID */
    min_buf_size = (sizeof(group_descriptor->magic_nr) +
        sizeof(group_descriptor->name_hash) + 1);
    if (buf_size < min_buf_size)
    {
        fprintf(stderr, "Error: Serialized buffer does not contain a valid SSG group ID\n");
        return;
    }

    /* deserialize */
    magic_nr = *(uint64_t *)buf;
    if (magic_nr != SSG_MAGIC_NR)
    {
        fprintf(stderr, "Error: Magic number mismatch when deserializing SSG group ID\n");
        return;
    }
    buf += sizeof(uint64_t);
    name_hash = *(uint64_t *)buf;
    buf += sizeof(uint64_t);
    addr_str = buf;

    group_descriptor = ssg_group_descriptor_create(name_hash, addr_str,
        SSG_OWNER_IS_UNASSOCIATED);
    if (!group_descriptor)
        return;

    *group_id_p = (ssg_group_id_t)group_descriptor;

    return;
}

int ssg_group_id_store(
    const char * file_name,
    ssg_group_id_t group_id)
{
    int fd;
    char *buf;
    size_t buf_size;
    ssize_t bytes_written;

    fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0)
    {
        fprintf(stderr, "Error: Unable to open file %s for storing SSG group ID\n",
            file_name);
        return SSG_FAILURE;
    }

    ssg_group_id_serialize(group_id, &buf, &buf_size);
    if (buf == NULL)
    {
        fprintf(stderr, "Error: Unable to serialize SSG group ID.\n");
        close(fd);
        return SSG_FAILURE;
    }

    bytes_written = write(fd, buf, buf_size);
    if (bytes_written != (ssize_t)buf_size)
    {
        fprintf(stderr, "Error: Unable to write SSG group ID to file %s\n", file_name);
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    close(fd);
    free(buf);
    return SSG_SUCCESS;
}


int ssg_group_id_load(
    const char * file_name,
    ssg_group_id_t * group_id_p)
{
    int fd;
    struct stat fstats;
    char *buf;
    ssize_t bytes_read;
    int ret;

    *group_id_p = SSG_GROUP_ID_NULL;

    fd = open(file_name, O_RDONLY);
    if (fd < 0)
    {
        fprintf(stderr, "Error: Unable to open file %s for loading SSG group ID\n",
            file_name);
        return SSG_FAILURE;
    }

    ret = fstat(fd, &fstats);
    if (ret != 0)
    {
        fprintf(stderr, "Error: Unable to stat file %s\n", file_name);
        close(fd);
        return SSG_FAILURE;
    }
    if (fstats.st_size == 0)
    {
        fprintf(stderr, "Error: SSG group ID file %s is empty\n", file_name);
        close(fd);
        return SSG_FAILURE;
    }

    buf = malloc(fstats.st_size);
    if (buf == NULL)
    {
        close(fd);
        return SSG_FAILURE;
    }

    bytes_read = read(fd, buf, fstats.st_size);
    if (bytes_read != (ssize_t)fstats.st_size)
    {
        fprintf(stderr, "Error: Unable to read SSG group ID from file %s\n", file_name);
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    ssg_group_id_deserialize(buf, (size_t)bytes_read, group_id_p);
    if (*group_id_p == SSG_GROUP_ID_NULL)
    {
        fprintf(stderr, "Error: Unable to deserialize SSG group ID\n");
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    close(fd);
    free(buf);
    return SSG_SUCCESS;
}

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
void ssg_group_dump(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_group_view_t *group_view = NULL;
    char *group_name = NULL;
    char group_role[32];
    char group_self_id[32];

    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
        {
            group_view = &g->view;
            group_name = g->name;
            strcpy(group_role, "member");
1085
            sprintf(group_self_id, "%lu", g->self_id);
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
        }
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
        {
            group_view = &ag->view;
            group_name = ag->name;
            strcpy(group_role, "attacher");
        }
    }
    else
    {
        fprintf(stderr, "Error: SSG can only dump membership information for" \
            " groups that the caller is a member of or an attacher of\n");
        return;
    }

    if (group_view)
    {
1110
        ssg_member_state_t *member_state, *tmp_ms;
1111 1112 1113 1114 1115

        printf("SSG membership information for group '%s':\n", group_name);
        printf("\trole: '%s'\n", group_role);
        if (strcmp(group_role, "member") == 0)
            printf("\tself_id: %s\n", group_self_id);
Shane Snyder's avatar
Shane Snyder committed
1116
        printf("\tsize: %d\n", group_view->size);
1117
        printf("\tview:\n");
1118
        HASH_ITER(hh, group_view->member_map, member_state, tmp_ms)
1119
        {
Shane Snyder's avatar
Shane Snyder committed
1120
            printf("\t\tid: %20lu\taddr: %s\n", member_state->id,
1121
                member_state->addr_str);
1122
        }
1123 1124 1125 1126
    }
    else
        fprintf(stderr, "Error: SSG unable to find group view associated" \
            " with the given group ID\n");
1127

1128
    return;
Shane Snyder's avatar
Shane Snyder committed
1129 1130
}

1131 1132 1133
/************************************
 *** SSG internal helper routines ***
 ************************************/
1134

1135
static ssg_group_descriptor_t * ssg_group_descriptor_create(
1136
    uint64_t name_hash, const char * leader_addr_str, int owner_status)
1137
{
1138
    ssg_group_descriptor_t *descriptor;
1139

1140 1141
    descriptor = malloc(sizeof(*descriptor));
    if (!descriptor) return NULL;
Shane Snyder's avatar
Shane Snyder committed
1142

1143
    /* hash the group name to obtain an 64-bit unique ID */
1144
    descriptor->magic_nr = SSG_MAGIC_NR;
1145
    descriptor->name_hash = name_hash;
1146 1147
    descriptor->addr_str = strdup(leader_addr_str);
    if (!descriptor->addr_str)
Shane Snyder's avatar
Shane Snyder committed
1148
    {
1149
        free(descriptor);
Shane Snyder's avatar
Shane Snyder committed
1150 1151
        return NULL;
    }
1152
    descriptor->owner_status = owner_status;
1153
    descriptor->ref_count = 1;
1154 1155
    return descriptor;
}
1156

1157 1158 1159
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor)
{
Shane Snyder's avatar
Shane Snyder committed
1160 1161
    descriptor->ref_count++;
    return descriptor;
1162 1163
}

1164 1165
static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor)
1166
{
1167 1168
    if (descriptor)
    {
Shane Snyder's avatar
Shane Snyder committed
1169 1170 1171 1172 1173 1174 1175 1176 1177
        if(descriptor->ref_count == 1)
        {
            free(descriptor->addr_str);
            free(descriptor);
        }
        else
        {
            descriptor->ref_count--;
        }
1178 1179 1180
    }
    return;
}
1181

1182
static int ssg_group_view_create(
1183 1184 1185
    const char * const group_addr_strs[], int group_size,
    hg_addr_t self_addr, ssg_member_id_t * self_id,
    ssg_group_view_t * view)
1186 1187
{
    int i, j, r;
1188 1189 1190 1191
    ABT_thread *lookup_ults = NULL;
    struct ssg_group_lookup_ult_args *lookup_ult_args = NULL;
    char *self_addr_str = NULL;
    hg_size_t self_addr_str_size = 0;
1192 1193
    const char *self_addr_substr = NULL;
    const char *addr_substr = NULL;
1194 1195
    ssg_member_state_t *tmp_ms;
    hg_return_t hret;
1196
    int aret;
1197
    int sret = SSG_FAILURE;
1198

1199 1200 1201 1202
    if (self_id)
        *self_id = SSG_MEMBER_ID_INVALID;
        
    if ((self_id != NULL && self_addr == HG_ADDR_NULL) || !view) goto fini;
1203 1204 1205

    /* allocate lookup ULTs */
    lookup_ults = malloc(group_size * sizeof(*lookup_ults));
1206 1207
    if (lookup_ults == NULL) goto fini;
    for (i = 0; i < group_size; i++) lookup_ults[i] = ABT_THREAD_NULL;
1208
    lookup_ult_args = malloc(group_size * sizeof(*lookup_ult_args));
1209
    if (lookup_ult_args == NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
1210

1211
    if(self_addr)
1212
    {
1213 1214 1215 1216 1217 1218
        hret = margo_addr_to_string(ssg_inst->mid, NULL, &self_addr_str_size, self_addr);
        if (hret != HG_SUCCESS) goto fini;
        self_addr_str = malloc(self_addr_str_size);
        if (self_addr_str == NULL) goto fini;
        hret = margo_addr_to_string(ssg_inst->mid, self_addr_str, &self_addr_str_size, self_addr);
        if (hret != HG_SUCCESS) goto fini;
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

        /* 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)
         */
        self_addr_substr = strstr(self_addr_str, "://");
        if (self_addr_substr == NULL)
            self_addr_substr = self_addr_str;
        else
            self_addr_substr += 3;
    }

1231 1232
    /* construct view using ULTs to lookup the address of each group member */
    view->size = group_size;
1233 1234 1235 1236 1237 1238 1239 1240
    r = rand() % view->size;
    for (i = 0; i < group_size; i++)
    {
        /* randomize our starting index so all group members aren't looking
         * up other group members in the same order
         */
        j = (r + i) % view->size;

1241 1242
        if (group_addr_strs[j] == NULL || strlen(group_addr_strs[j]) == 0) continue;

1243 1244 1245 1246 1247 1248 1249
        tmp_ms = malloc(sizeof(*tmp_ms));
        if (!tmp_ms) goto fini;

        /* generate a unique member ID for this address */
        tmp_ms->id = ssg_gen_member_id(group_addr_strs[j]);
        tmp_ms->addr_str = strdup(group_addr_strs[j]);
        if (!tmp_ms->addr_str)
1250
        {
1251 1252
            free(tmp_ms);
            goto fini;
1253
        }
1254
        SWIM_MEMBER_STATE_INIT(tmp_ms->swim_state);
1255
        
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
        /* resolve self id in group if caller asked for it */
        if (self_addr_substr)
        {
            addr_substr = strstr(group_addr_strs[j], "://");
            if (addr_substr == NULL)
                addr_substr = group_addr_strs[j];
            else
                addr_substr += 3;

            if (strcmp(self_addr_substr, addr_substr) == 0)
            {
1267 1268 1269 1270
                if (self_id)               
                    *self_id = tmp_ms->id;

                /* don't look up our own address, we already know it */
Shane Snyder's avatar
Shane Snyder committed
1271 1272
                free(tmp_ms->addr_str);
                free(tmp_ms);
1273
                continue;
1274 1275 1276 1277
            }
        }

        /* XXX limit outstanding lookups to some max */
1278 1279
        lookup_ult_args[j].view = view;
        lookup_ult_args[j].member_state = tmp_ms;
1280 1281
        ABT_pool pool;
        margo_get_handler_pool(ssg_inst->mid, &pool);
1282 1283
        aret = ABT_thread_create(pool, &ssg_group_lookup_ult,
            &lookup_ult_args[j], ABT_THREAD_ATTR_NULL,
1284 1285 1286
            &lookup_ults[j]);
        if (aret != ABT_SUCCESS)
        {
1287 1288
            free(tmp_ms->addr_str);
            free(tmp_ms);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
            goto fini;
        }
    }

    /* wait on all lookup ULTs to terminate */
    for (i = 0; i < group_size; i++)
    {
        if (lookup_ults[i] == ABT_THREAD_NULL) continue;

        aret = ABT_thread_join(lookup_ults[i]);
        ABT_thread_free(&lookup_ults[i]);
        lookup_ults[i] = ABT_THREAD_NULL;
1301
        if (aret != ABT_SUCCESS) goto fini;
1302 1303
        else if (lookup_ult_args[i].out != HG_SUCCESS)
        {
1304
            fprintf(stderr, "Error: SSG unable to lookup HG address for member %lu"
1305 1306 1307
                "(err=%d)\n", lookup_ult_args[i].member_state->id,
                lookup_ult_args[i].out);
            goto fini;
1308 1309 1310
        }
    }

1311 1312 1313
    /* clean exit */
    sret = SSG_SUCCESS;

1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
fini:
    if (sret != SSG_SUCCESS)
    {
        for (i = 0; i < group_size; i++)
        {
            if (lookup_ults[i] != ABT_THREAD_NULL)
            {
                ABT_thread_cancel(lookup_ults[i]);
                ABT_thread_free(&lookup_ults[i]);
            }
        }
        ssg_group_view_destroy(view);
    }
    free(lookup_ults);
    free(lookup_ult_args);
1329
    free(self_addr_str);
1330 1331

    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
1332
}
1333

1334 1335