ssg.c 28.4 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
#ifdef SSG_USE_SWIM_FD
32 33
#include "swim-fd/swim-fd.h"
#endif
34
#include "uthash.h"
35

36 37 38 39 40 41 42 43
/* arguments for group lookup ULTs */
struct ssg_group_lookup_ult_args
{
    ssg_member_state_t *member_state;
    hg_return_t out;
};
static void ssg_group_lookup_ult(void * arg);

44
/* SSG helper routine prototypes */
45
static ssg_group_descriptor_t * ssg_group_descriptor_create(
Shane Snyder's avatar
Shane Snyder committed
46
    const char * name, const char * leader_addr_str);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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(
    const char * const group_addr_strs[], const char * self_addr_str,
    int group_size, ssg_group_view_t * view, ssg_member_id_t * self_id);
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

Shane Snyder's avatar
Shane Snyder committed
126
ssg_group_id_t ssg_group_create(
127 128
    const char * group_name,
    const char * const group_addr_strs[],
129 130 131
    int group_size,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
132
{
133
    hg_class_t *hgcl = NULL;
Shane Snyder's avatar
Shane Snyder committed
134
    hg_addr_t self_addr = HG_ADDR_NULL;
135
    char *self_addr_str = NULL;
136
    hg_size_t self_addr_str_size = 0;
137
    ssg_group_descriptor_t *tmp_descriptor;
138
    ssg_group_t *g = NULL;
139
    hg_return_t hret;
140
    int sret;
Shane Snyder's avatar
Shane Snyder committed
141
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
142

143
    if (!ssg_inst) return group_id;
144 145

    hgcl = margo_get_class(ssg_inst->mid);
146
    if (!hgcl) return group_id;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
147

148
    /* generate a unique ID for this group  */
149 150
    tmp_descriptor = ssg_group_descriptor_create(group_name, group_addr_strs[0]);
    if (tmp_descriptor == NULL) return group_id;
151

Shane Snyder's avatar
Shane Snyder committed
152
    /* make sure we aren't re-creating an existing group */
153
    HASH_FIND(hh, ssg_inst->group_table, &tmp_descriptor->name_hash,
Shane Snyder's avatar
Shane Snyder committed
154
        sizeof(uint64_t), g);
155 156 157 158 159
    if (g)
    {
        g = NULL;
        goto fini;
    }
160

161
    /* get my address */
162 163
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto fini;
164
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_str_size, self_addr);
165
    if (hret != HG_SUCCESS) goto fini;
166
    self_addr_str = malloc(self_addr_str_size);
167
    if (self_addr_str == NULL) goto fini;
168
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_str_size, self_addr);
169 170
    if (hret != HG_SUCCESS) goto fini;

171 172 173 174 175 176 177
    /* 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;
178 179
    g->update_cb = update_cb;
    g->update_cb_dat = update_cb_dat;
180 181 182 183 184

    /* initialize the group view */
    sret = ssg_group_view_create(group_addr_strs, self_addr_str, group_size,
        &g->view, &g->self_id);
    if (sret != SSG_SUCCESS) goto fini;
185
    if (g->self_id == SSG_MEMBER_ID_INVALID)
186
    {
187
        /* if unable to resolve my rank within the group, error out */
188 189 190 191
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
    }
192
    g->view.member_states[g->self_id].addr = self_addr;
193

194
#ifdef SSG_USE_SWIM_FD
195
    /* initialize swim failure detector */
196 197
    // TODO: we should probably barrier or sync somehow to avoid rpc failures
    // due to timing skew of different ranks initializing swim
198
    g->fd_ctx = (void *)swim_init(g, 1);
199
    if (g->fd_ctx == NULL) goto fini;
200 201
#endif

Shane Snyder's avatar
Shane Snyder committed
202 203 204
    /* everything successful -- set the output group identifier, which is just
     * an opaque pointer to the group descriptor structure
     */
205 206
    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
207

208 209 210 211
    /* 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
212
    SSG_DEBUG(g, "group create successful (size=%d)\n", group_size);
213

214
    /* don't free these pointers on success */
215
    self_addr = HG_ADDR_NULL;
216 217
    g = NULL;
fini:
Shane Snyder's avatar
Shane Snyder committed
218 219
    if (hgcl && self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
220 221
    if (g)
    {
222
        ssg_group_view_destroy(&g->view);
223
        free(g->name);
224 225
        free(g);
    }
226 227
    if (group_id == SSG_GROUP_ID_NULL)
        ssg_group_descriptor_free(tmp_descriptor);
228

Shane Snyder's avatar
Shane Snyder committed
229
    return group_id;
230 231
}

Shane Snyder's avatar
Shane Snyder committed
232
ssg_group_id_t ssg_group_create_config(
233
    const char * group_name,
234 235 236
    const char * file_name,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
237 238 239 240
{
    int fd;
    struct stat st;
    char *rd_buf = NULL;
241
    ssize_t rd_buf_size;
242
    char *tok;
243 244
    void *addr_str_buf = NULL;
    int addr_str_buf_len = 0, num_addrs = 0;
245 246
    int ret;
    const char **addr_strs = NULL;
Shane Snyder's avatar
Shane Snyder committed
247
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

    /* 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 */
270 271
    rd_buf_size = read(fd, rd_buf, st.st_size);
    if (rd_buf_size != st.st_size)
272 273 274 275 276
    {
        fprintf(stderr, "Error: SSG unable to read config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }
277
    rd_buf[rd_buf_size]='\0';
278 279 280 281 282

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

285
    /* build up the address buffer */
286 287
    addr_str_buf = malloc(rd_buf_size);
    if (addr_str_buf == NULL) goto fini;
288 289
    do
    {
290 291 292
        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
293 294 295
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
296
    if (addr_str_buf_len != rd_buf_size)
297 298
    {
        /* adjust buffer size if our initial guess was wrong */
299
        void *tmp = realloc(addr_str_buf, addr_str_buf_len);
300
        if (tmp == NULL) goto fini;
301
        addr_str_buf = tmp;
302
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
303

304
    /* set up address string array for group members */
305
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, num_addrs);
306
    if (!addr_strs) goto fini;
307

308
    /* invoke the generic group create routine using our list of addrs */
309 310
    group_id = ssg_group_create(group_name, addr_strs, num_addrs,
        update_cb, update_cb_dat);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
311 312

fini:
313
    /* cleanup before returning */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
314
    if (fd != -1) close(fd);
315
    free(rd_buf);
316
    free(addr_str_buf);
317 318
    free(addr_strs);

Shane Snyder's avatar
Shane Snyder committed
319
    return group_id;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
320 321
}

Shane Snyder's avatar
Shane Snyder committed
322
#ifdef SSG_HAVE_MPI
Shane Snyder's avatar
Shane Snyder committed
323
ssg_group_id_t ssg_group_create_mpi(
324
    const char * group_name,
325 326 327
    MPI_Comm comm,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
328
{
329
    hg_class_t *hgcl = NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
330
    hg_addr_t self_addr = HG_ADDR_NULL;
331
    char *self_addr_str = NULL;
332 333
    hg_size_t self_addr_str_size = 0;
    int self_addr_str_size_int = 0; /* for mpi-friendly conversion */
334
    char *addr_str_buf = NULL;
335 336 337 338
    int *sizes = NULL;
    int *sizes_psum = NULL;
    int comm_size = 0, comm_rank = 0;
    const char **addr_strs = NULL;
339
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
340
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
341

342 343 344
    if (!ssg_inst) goto fini;

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

347
    /* get my address */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
348 349
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto fini;
350
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_str_size, self_addr);
Shane Snyder's avatar
Shane Snyder committed
351
    if (hret != HG_SUCCESS) goto fini;
352
    self_addr_str = malloc(self_addr_str_size);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
353
    if (self_addr_str == NULL) goto fini;
354
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_str_size, self_addr);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
355
    if (hret != HG_SUCCESS) goto fini;
356
    self_addr_str_size_int = (int)self_addr_str_size; /* null char included in call */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
357

358
    /* gather the buffer sizes */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
359 360 361 362
    MPI_Comm_size(comm, &comm_size);
    MPI_Comm_rank(comm, &comm_rank);
    sizes = malloc(comm_size * sizeof(*sizes));
    if (sizes == NULL) goto fini;
363
    sizes[comm_rank] = self_addr_str_size_int;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
364 365
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);

366 367 368
    /* compute a exclusive prefix sum of the data sizes, including the
     * total at the end
     */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
369 370 371 372 373 374
    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];

375
    /* allgather the addresses */
376 377
    addr_str_buf = malloc(sizes_psum[comm_size]);
    if (addr_str_buf == NULL) goto fini;
378
    MPI_Allgatherv(self_addr_str, self_addr_str_size_int, MPI_BYTE,
379
            addr_str_buf, sizes, sizes_psum, MPI_BYTE, comm);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
380

381
    /* set up address string array for group members */
382
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, comm_size);
383 384 385
    if (!addr_strs) goto fini;

    /* invoke the generic group create routine using our list of addrs */
386 387
    group_id = ssg_group_create(group_name, addr_strs, comm_size,
        update_cb, update_cb_dat);
Shane Snyder's avatar
Shane Snyder committed
388 389

fini:
390
    /* cleanup before returning */
391 392
    if (hgcl && self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
393 394 395
    free(sizes);
    free(sizes_psum);
    free(addr_str_buf);
396 397
    free(addr_strs);

Shane Snyder's avatar
Shane Snyder committed
398
    return group_id;
Shane Snyder's avatar
Shane Snyder committed
399 400 401
}
#endif

402 403
int ssg_group_destroy(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
404
{
Shane Snyder's avatar
Shane Snyder committed
405
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
406
    ssg_group_t *g;
407

Shane Snyder's avatar
Shane Snyder committed
408
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
409
        return SSG_FAILURE;
410

411 412 413 414 415 416
    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;
    }

417
    /* find the group structure and destroy it */
Shane Snyder's avatar
Shane Snyder committed
418 419
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
420 421 422 423 424
    if (!g)
    {
        fprintf(stderr, "Error: SSG unable to find expected group reference\n");
        return SSG_FAILURE;
    }
425
    HASH_DELETE(hh, ssg_inst->group_table, g);
426
    ssg_group_destroy_internal(g);
427

428
    return SSG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
429 430
}

431 432 433
int ssg_group_attach(
    ssg_group_id_t group_id)
{
Shane Snyder's avatar
Shane Snyder committed
434
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
435 436 437 438 439 440
    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;
441

442
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) goto fini;
443

444 445 446 447 448 449 450 451 452 453 454
    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;
    }
455

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    /* 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 */
    sret = ssg_group_view_create(addr_strs, NULL, group_size, &ag->view, NULL);
    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)
    {
493
        ssg_group_view_destroy(&ag->view);
494 495
        free(ag->name);
        free(ag);
496
        ssg_group_descriptor_free(ag->descriptor);
497 498 499
    }

    return sret;
500 501 502 503 504
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    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);

526 527 528
    return SSG_SUCCESS;
}

529 530 531
/*********************************
 *** SSG group access routines ***
 *********************************/
Shane Snyder's avatar
Shane Snyder committed
532

533
ssg_member_id_t ssg_get_group_self_id(
534
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
535
{
Shane Snyder's avatar
Shane Snyder committed
536
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
537 538
    ssg_group_t *g;

Shane Snyder's avatar
Shane Snyder committed
539
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
540
        return SSG_MEMBER_ID_INVALID;
541

542 543 544 545
    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");
546
        return SSG_MEMBER_ID_INVALID;
547 548
    }

Shane Snyder's avatar
Shane Snyder committed
549 550
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
551
    if (!g)
552
        return SSG_MEMBER_ID_INVALID;
553 554

    return g->self_id;
Shane Snyder's avatar
Shane Snyder committed
555 556
}

557 558
int ssg_get_group_size(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
559
{
Shane Snyder's avatar
Shane Snyder committed
560
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
561
    ssg_group_view_t *group_view = NULL;
562

Shane Snyder's avatar
Shane Snyder committed
563
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
564 565
        return 0;

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    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");
588
        return 0;
589
    }
590

591 592 593 594
    if (group_view)
        return group_view->size;
    else
        return 0;
Shane Snyder's avatar
Shane Snyder committed
595 596
}

597 598 599
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
600
{
Shane Snyder's avatar
Shane Snyder committed
601
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
602
    ssg_group_view_t *group_view = NULL;
603

Shane Snyder's avatar
Shane Snyder committed
604
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
605 606
        return HG_ADDR_NULL;

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
    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)
    {
        /* XXX for now we assume member ids are dense ranks and error out
         * if they are not within allowable range for the group view size
         */
        if (member_id >= group_view->size)
            return HG_ADDR_NULL;

        return group_view->member_states[member_id].addr;
    }
    else
643
        return HG_ADDR_NULL;
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
}

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
661
    descriptor = SSG_GROUP_ID_NULL;
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    return;
}

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");
            sprintf(group_self_id, "%"PRIu64, g->self_id);
        }
    }
    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)
    {
        unsigned int i;

        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
716
        printf("\tsize: %d\n", group_view->size);
717 718
        printf("\tview:\n");
        for (i = 0; i < group_view->size; i++)
719 720 721 722 723
        {
            if (group_view->member_states[i].is_member)
                printf("\t\tid: %d\taddr: %s\n", i,
                    group_view->member_states[i].addr_str);
        }
724 725 726 727
    }
    else
        fprintf(stderr, "Error: SSG unable to find group view associated" \
            " with the given group ID\n");
728

729
    return;
Shane Snyder's avatar
Shane Snyder committed
730 731
}

732 733 734
/************************************
 *** SSG internal helper routines ***
 ************************************/
735

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
void ssg_apply_membership_update(
    ssg_group_t *g,
    ssg_membership_update_t update)
{
    if (!g) return;

    if (update.type == SSG_MEMBER_REMOVE)
    {
        g->view.member_states[update.member].is_member = 0;
    }
    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(update, g->update_cb_dat);

    return;
}

758
static ssg_group_descriptor_t * ssg_group_descriptor_create(
Shane Snyder's avatar
Shane Snyder committed
759
    const char * name, const char * leader_addr_str)
760
{
761
    ssg_group_descriptor_t *descriptor;
762 763
    uint32_t upper, lower;

764 765
    descriptor = malloc(sizeof(*descriptor));
    if (!descriptor) return NULL;
Shane Snyder's avatar
Shane Snyder committed
766

767 768
    /* hash the group name to obtain an 64-bit unique ID */
    ssg_hashlittle2(name, strlen(name), &lower, &upper);
769 770 771 772
    descriptor->magic_nr = SSG_MAGIC_NR;
    descriptor->name_hash = lower + (((uint64_t)upper)<<32);
    descriptor->addr_str = strdup(leader_addr_str);
    if (!descriptor->addr_str)
Shane Snyder's avatar
Shane Snyder committed
773
    {
774
        free(descriptor);
Shane Snyder's avatar
Shane Snyder committed
775 776
        return NULL;
    }
777
    descriptor->owner_status = SSG_OWNER_IS_MEMBER;
Shane Snyder's avatar
Shane Snyder committed
778
    descriptor->ref_count = 0;
779 780
    return descriptor;
}
781

782 783 784
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor)
{
Shane Snyder's avatar
Shane Snyder committed
785 786
    descriptor->ref_count++;
    return descriptor;
787 788
}

789 790
static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor)
791
{
792 793
    if (descriptor)
    {
Shane Snyder's avatar
Shane Snyder committed
794 795 796 797 798 799 800 801 802
        if(descriptor->ref_count == 1)
        {
            free(descriptor->addr_str);
            free(descriptor);
        }
        else
        {
            descriptor->ref_count--;
        }
803 804 805
    }
    return;
}
806

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
static int ssg_group_view_create(
    const char * const group_addr_strs[], const char * self_addr_str,
    int group_size, ssg_group_view_t * view, ssg_member_id_t * self_id)
{
    int i, j, r;
    ABT_thread *lookup_ults;
    struct ssg_group_lookup_ult_args *lookup_ult_args;
    const char *self_addr_substr = NULL;
    const char *addr_substr = NULL;
    int aret;
    int sret = SSG_SUCCESS;

    if (!view || (self_id != NULL && self_addr_str == NULL)) return SSG_FAILURE;

    /* allocate lookup ULTs */
    lookup_ults = malloc(group_size * sizeof(*lookup_ults));
    if (lookup_ults == NULL) return SSG_FAILURE;
    lookup_ult_args = malloc(group_size * sizeof(*lookup_ult_args));
    if (lookup_ult_args == NULL)
826
    {
827 828
        free(lookup_ults);
        return SSG_FAILURE;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
829
    }
830 831 832 833 834 835 836 837 838 839

    /* allocate and initialize the view */
    view->size = group_size;
    view->member_states = malloc(group_size * sizeof(*view->member_states));
    if (!view->member_states)
    {
        free(lookup_ults);
        free(lookup_ult_args);
        return SSG_FAILURE;
    }
Shane Snyder's avatar
Shane Snyder committed
840

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    for (i = 0; i < group_size; i++)
    {
        view->member_states[i].addr_str = NULL;
        view->member_states[i].addr = HG_ADDR_NULL;
        view->member_states[i].is_member = 1;
        lookup_ults[i] = ABT_THREAD_NULL;
    }

    if (self_addr_str && self_id)
    {
        /* 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;

861
        *self_id = SSG_MEMBER_ID_INVALID;
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 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
    }

    /* kickoff ULTs to lookup the address of each group member */
    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;

        view->member_states[j].addr_str = strdup(group_addr_strs[j]);
        if (!view->member_states[j].addr_str)
        {
            sret = SSG_FAILURE;
            goto fini;   
        }

        /* 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)
            {
                
                *self_id = j;
                continue; /* don't look up our own address, we already know it */
            }
        }

        /* XXX limit outstanding lookups to some max */
        lookup_ult_args[j].member_state = &view->member_states[j];
        aret = ABT_thread_create(*margo_get_handler_pool(ssg_inst->mid),
            &ssg_group_lookup_ult, &lookup_ult_args[j], ABT_THREAD_ATTR_NULL,
            &lookup_ults[j]);
        if (aret != ABT_SUCCESS)
        {
            sret = SSG_FAILURE;
            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;
        if (aret != ABT_SUCCESS)
        {
            sret = SSG_FAILURE;
            break;
        }
        else if (lookup_ult_args[i].out != HG_SUCCESS)
        {
            fprintf(stderr, "Error: SSG unable to lookup HG address for member %d"
                "(err=%d)\n", i, lookup_ult_args[i].out);
            sret = SSG_FAILURE;
            break;
        }
    }

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);

    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
948
}
949

950 951 952 953 954
static void ssg_group_lookup_ult(
    void * arg)
{
    struct ssg_group_lookup_ult_args *l = arg;

955
    /* XXX: should be a timeout here? */
956 957 958 959 960 961 962
    l->out = margo_addr_lookup(ssg_inst->mid, l->member_state->addr_str,
        &l->member_state->addr);
    return;
}

static void ssg_group_view_destroy(
    ssg_group_view_t * view)
963
{
Shane Snyder's avatar
Shane Snyder committed
964
    unsigned int i;
965

966 967 968 969 970 971 972 973 974 975 976 977 978
    for (i = 0; i < view->size; i++)
    {
        free(view->member_states[i].addr_str);
        HG_Addr_free(margo_get_class(ssg_inst->mid), view->member_states[i].addr);
    }
    free(view->member_states);

    return;
}

static void ssg_group_destroy_internal(
    ssg_group_t * g)
{
979 980 981 982 983 984 985 986 987
    /* TODO: send a leave message to the group ? */

#ifdef SSG_USE_SWIM_FD
    /* free up failure detector state */
    if(g->fd_ctx)
        swim_finalize(g->fd_ctx);
#endif

    /* destroy group state */
988
    ssg_group_view_destroy(&g->view);
Shane Snyder's avatar
Shane Snyder committed
989
    g->descriptor->owner_status = SSG_OWNER_IS_UNASSOCIATED;
990 991
    ssg_group_descriptor_free(g->descriptor);
    free(g->name);
992 993
    free(g);

994 995 996 997 998 999
    return;
}

static void ssg_attached_group_destroy(
    ssg_attached_group_t * ag)
{
Shane Snyder's avatar
Shane Snyder committed
1000 1001
    ssg_group_view_destroy(&ag->view);
    ag->descriptor->owner_status = SSG_OWNER_IS_UNASSOCIATED;
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    ssg_group_descriptor_free(ag->descriptor);
    free(ag->name);
    free(ag);
    return;
}

static const char ** ssg_addr_str_buf_to_list(
    const char * buf, int num_addrs)
{
    const char **ret = malloc(num_addrs * sizeof(*ret));
    if (ret == NULL) return NULL;

    ret[0] = buf;
    for (int i = 1; i < num_addrs; i++)
    {
        const char * a = ret[i-1];
        ret[i] = a + strlen(a) + 1;
    }
    return ret;
1021
}