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

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

Jonathan Jenkins's avatar
Jonathan Jenkins committed
9 10 11 12
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
13
#include <errno.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
14 15
#include <stdlib.h>
#include <string.h>
16
#include <time.h>
17
#include <linux/limits.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
18
#include <assert.h>
Shane Snyder's avatar
Shane Snyder committed
19
#ifdef SSG_HAVE_MPI
20 21
#include <mpi.h>
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
22

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

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

34 35 36
/* arguments for group lookup ULTs */
struct ssg_group_lookup_ult_args
{
37
    const char *addr_str;
38
    ssg_group_view_t *view;
Shane Snyder's avatar
Shane Snyder committed
39
    ABT_rwlock lock;
40
    int out;
41 42 43
};
static void ssg_group_lookup_ult(void * arg);

44
/* SSG helper routine prototypes */
Shane Snyder's avatar
Shane Snyder committed
45 46 47 48 49 50 51
static ssg_group_t * ssg_group_create_internal(
    const char * group_name, const char * const group_addr_strs[],
    int group_size, ssg_membership_update_cb update_cb, void *update_cb_dat);
static int ssg_group_view_create(
    const char * const group_addr_strs[], int group_size,
    const char * self_addr_str, ABT_rwlock view_lock,
    ssg_group_view_t * view, ssg_member_id_t * self_id);
52
static ssg_member_state_t * ssg_group_view_add_member(
53 54
    const char * addr_str, hg_addr_t addr, ssg_member_id_t member_id,
    ssg_group_view_t * view);
55
static ssg_group_descriptor_t * ssg_group_descriptor_create(
56
    uint64_t name_hash, const char * leader_addr_str, int owner_status);
57 58 59 60 61 62
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor);
static void ssg_group_destroy_internal(
    ssg_group_t * g);
static void ssg_attached_group_destroy(
    ssg_attached_group_t * ag);
Shane Snyder's avatar
Shane Snyder committed
63 64 65 66 67 68
static void ssg_group_view_destroy(
    ssg_group_view_t * view);
static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor);
static ssg_member_id_t ssg_gen_member_id(
    const char * addr_str);
69 70
static const char ** ssg_addr_str_buf_to_list(
    const char * buf, int num_addrs);
71

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

75 76 77
/***************************************************
 *** SSG runtime intialization/shutdown routines ***
 ***************************************************/
78

79 80
int ssg_init(
    margo_instance_id mid)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
81
{
82 83
    struct timespec ts;

84 85 86 87 88 89 90 91 92
    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;
93
    ABT_rwlock_create(&ssg_inst->lock);
94

95
    ssg_register_rpcs();
96

97 98 99 100
    /* seed RNG */
    clock_gettime(CLOCK_MONOTONIC, &ts);
    srand(ts.tv_nsec + getpid());

101 102
    return SSG_SUCCESS;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
103

104
int ssg_finalize()
105
{
106 107
    ssg_group_t *g, *g_tmp;
    ssg_attached_group_t *ag, *ag_tmp;
108 109 110 111

    if (!ssg_inst)
        return SSG_FAILURE;

112 113
    ABT_rwlock_wrlock(ssg_inst->lock);

114
    /* destroy all active groups */
115
    HASH_ITER(hh, ssg_inst->group_table, g, g_tmp)
116 117
    {
        HASH_DELETE(hh, ssg_inst->group_table, g);
118
        ABT_rwlock_unlock(ssg_inst->lock);
119
        ssg_group_destroy_internal(g);
120
        ABT_rwlock_wrlock(ssg_inst->lock);
121 122
    }

123 124 125 126 127 128
    /* detach from all attached groups */
    HASH_ITER(hh, ssg_inst->attached_group_table, ag, ag_tmp)
    {
        ssg_attached_group_destroy(ag);
    }

129 130 131
    ABT_rwlock_unlock(ssg_inst->lock);
    ABT_rwlock_free(&ssg_inst->lock);

132 133 134
    free(ssg_inst);
    ssg_inst = NULL;

135
    return SSG_SUCCESS;
136
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
137

138 139 140
/*************************************
 *** SSG group management routines ***
 *************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
141

Shane Snyder's avatar
Shane Snyder committed
142 143 144 145 146 147
ssg_group_id_t ssg_group_create(
    const char * group_name,
    const char * const group_addr_strs[],
    int group_size,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
148
{
Shane Snyder's avatar
Shane Snyder committed
149 150
    ssg_group_t *g;
    ssg_group_id_t g_id = SSG_GROUP_ID_NULL;
151

Shane Snyder's avatar
Shane Snyder committed
152 153 154
    g = ssg_group_create_internal(group_name, group_addr_strs,
            group_size, update_cb, update_cb_dat);
    if (g)
155
    {
Shane Snyder's avatar
Shane Snyder committed
156 157 158 159 160
        /* on successful creation, dup the group descriptor and return
         * it for the caller to hold on to
         */
        g_id = (ssg_group_id_t)ssg_group_descriptor_dup(g->descriptor);
        if (g_id == SSG_GROUP_ID_NULL)
161 162 163 164
        {
            ABT_rwlock_wrlock(ssg_inst->lock);
            HASH_DELETE(hh, ssg_inst->group_table, g);
            ABT_rwlock_unlock(ssg_inst->lock);
Shane Snyder's avatar
Shane Snyder committed
165
            ssg_group_destroy_internal(g);
166
        }
167
    }
Shane Snyder's avatar
Shane Snyder committed
168 169

    return g_id;
170
}
Shane Snyder's avatar
Shane Snyder committed
171

Shane Snyder's avatar
Shane Snyder committed
172 173 174 175 176
ssg_group_id_t ssg_group_create_config(
    const char * group_name,
    const char * file_name,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
177
{
Shane Snyder's avatar
Shane Snyder committed
178 179 180 181 182 183 184 185 186 187
    int fd;
    struct stat st;
    char *rd_buf = NULL;
    ssize_t rd_buf_size;
    char *tok;
    void *addr_str_buf = NULL;
    int addr_str_buf_len = 0, num_addrs = 0;
    int ret;
    const char **addr_strs = NULL;
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
188

Shane Snyder's avatar
Shane Snyder committed
189 190 191 192 193 194 195 196
    /* 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;
    }
197

Shane Snyder's avatar
Shane Snyder committed
198 199 200
    /* get file size and allocate a buffer to store it */
    ret = fstat(fd, &st);
    if (ret == -1)
Shane Snyder's avatar
Shane Snyder committed
201
    {
Shane Snyder's avatar
Shane Snyder committed
202 203 204
        fprintf(stderr, "Error: SSG unable to stat config file %s for group %s\n",
            file_name, group_name);
        goto fini;
Shane Snyder's avatar
Shane Snyder committed
205
    }
Shane Snyder's avatar
Shane Snyder committed
206 207
    rd_buf = malloc(st.st_size+1);
    if (rd_buf == NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
208

Shane Snyder's avatar
Shane Snyder committed
209 210 211
    /* load it all in one fell swoop */
    rd_buf_size = read(fd, rd_buf, st.st_size);
    if (rd_buf_size != st.st_size)
Shane Snyder's avatar
Shane Snyder committed
212
    {
Shane Snyder's avatar
Shane Snyder committed
213 214 215
        fprintf(stderr, "Error: SSG unable to read config file %s for group %s\n",
            file_name, group_name);
        goto fini;
Shane Snyder's avatar
Shane Snyder committed
216
    }
Shane Snyder's avatar
Shane Snyder committed
217 218 219 220 221 222 223
    rd_buf[rd_buf_size]='\0';

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

Shane Snyder's avatar
Shane Snyder committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    /* build up the address buffer */
    addr_str_buf = malloc(rd_buf_size);
    if (addr_str_buf == NULL) goto fini;
    do
    {
        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;
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
    if (addr_str_buf_len != rd_buf_size)
    {
        /* adjust buffer size if our initial guess was wrong */
        void *tmp = realloc(addr_str_buf, addr_str_buf_len);
        if (tmp == NULL) goto fini;
        addr_str_buf = tmp;
    }
Shane Snyder's avatar
Shane Snyder committed
243

Shane Snyder's avatar
Shane Snyder committed
244 245 246
    /* set up address string array for group members */
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, num_addrs);
    if (!addr_strs) goto fini;
Shane Snyder's avatar
Shane Snyder committed
247

Shane Snyder's avatar
Shane Snyder committed
248 249 250
    /* invoke the generic group create routine using our list of addrs */
    group_id = ssg_group_create(group_name, addr_strs, num_addrs,
        update_cb, update_cb_dat);
Shane Snyder's avatar
Shane Snyder committed
251

Shane Snyder's avatar
Shane Snyder committed
252 253 254 255 256 257
fini:
    /* cleanup before returning */
    if (fd != -1) close(fd);
    free(rd_buf);
    free(addr_str_buf);
    free(addr_strs);
Shane Snyder's avatar
Shane Snyder committed
258

Shane Snyder's avatar
Shane Snyder committed
259
    return group_id;
Shane Snyder's avatar
Shane Snyder committed
260 261
}

Shane Snyder's avatar
Shane Snyder committed
262 263 264 265 266 267
#ifdef SSG_HAVE_MPI
ssg_group_id_t ssg_group_create_mpi(
    const char * group_name,
    MPI_Comm comm,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
Shane Snyder's avatar
Shane Snyder committed
268
{
Shane Snyder's avatar
Shane Snyder committed
269 270
    int i;
    char *self_addr_str = NULL;
271
    int self_addr_str_size = 0;
Shane Snyder's avatar
Shane Snyder committed
272 273 274 275 276 277
    char *addr_str_buf = NULL;
    int *sizes = NULL;
    int *sizes_psum = NULL;
    int comm_size = 0, comm_rank = 0;
    const char **addr_strs = NULL;
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
Shane Snyder's avatar
Shane Snyder committed
278

Shane Snyder's avatar
Shane Snyder committed
279
    if (!ssg_inst) goto fini;
Shane Snyder's avatar
Shane Snyder committed
280

Shane Snyder's avatar
Shane Snyder committed
281
    /* get my address */
282
    SSG_GET_SELF_ADDR_STR(ssg_inst->mid, self_addr_str);
Shane Snyder's avatar
Shane Snyder committed
283
    if (self_addr_str == NULL) goto fini;
284
    self_addr_str_size = (int)strlen(self_addr_str) + 1;
285

Shane Snyder's avatar
Shane Snyder committed
286 287 288 289 290
    /* gather the buffer sizes */
    MPI_Comm_size(comm, &comm_size);
    MPI_Comm_rank(comm, &comm_rank);
    sizes = malloc(comm_size * sizeof(*sizes));
    if (sizes == NULL) goto fini;
291
    sizes[comm_rank] = self_addr_str_size;
Shane Snyder's avatar
Shane Snyder committed
292
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);
293

Shane Snyder's avatar
Shane Snyder committed
294 295
    /* compute a exclusive prefix sum of the data sizes, including the
     * total at the end
296
     */
Shane Snyder's avatar
Shane Snyder committed
297 298 299 300 301
    sizes_psum = malloc((comm_size+1) * sizeof(*sizes_psum));
    if (sizes_psum == NULL) goto fini;
    sizes_psum[0] = 0;
    for (i = 1; i < comm_size+1; i++)
        sizes_psum[i] = sizes_psum[i-1] + sizes[i-1];
302

Shane Snyder's avatar
Shane Snyder committed
303 304 305
    /* allgather the addresses */
    addr_str_buf = malloc(sizes_psum[comm_size]);
    if (addr_str_buf == NULL) goto fini;
306
    MPI_Allgatherv(self_addr_str, self_addr_str_size, MPI_BYTE,
Shane Snyder's avatar
Shane Snyder committed
307
            addr_str_buf, sizes, sizes_psum, MPI_BYTE, comm);
Shane Snyder's avatar
Shane Snyder committed
308

Shane Snyder's avatar
Shane Snyder committed
309 310 311
    /* set up address string array for group members */
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, comm_size);
    if (!addr_strs) goto fini;
312

Shane Snyder's avatar
Shane Snyder committed
313 314 315
    /* invoke the generic group create routine using our list of addrs */
    group_id = ssg_group_create(group_name, addr_strs, comm_size,
        update_cb, update_cb_dat);
Shane Snyder's avatar
Shane Snyder committed
316

Shane Snyder's avatar
Shane Snyder committed
317 318 319 320 321 322 323
fini:
    /* cleanup before returning */
    free(self_addr_str);
    free(sizes);
    free(sizes_psum);
    free(addr_str_buf);
    free(addr_strs);
Shane Snyder's avatar
Shane Snyder committed
324

Shane Snyder's avatar
Shane Snyder committed
325
    return group_id;
Shane Snyder's avatar
Shane Snyder committed
326
}
Shane Snyder's avatar
Shane Snyder committed
327
#endif
Shane Snyder's avatar
Shane Snyder committed
328

Shane Snyder's avatar
Shane Snyder committed
329 330
int ssg_group_destroy(
    ssg_group_id_t group_id)
331
{
Shane Snyder's avatar
Shane Snyder committed
332 333
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_group_t *g;
334

Shane Snyder's avatar
Shane Snyder committed
335
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return SSG_FAILURE;
336

Shane Snyder's avatar
Shane Snyder committed
337
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
338
    {
Shane Snyder's avatar
Shane Snyder committed
339 340
        fprintf(stderr, "Error: SSG unable to destroy a group it is not a member of\n");
        return SSG_FAILURE;
341
    }
342

343 344
    ABT_rwlock_wrlock(ssg_inst->lock);

Shane Snyder's avatar
Shane Snyder committed
345 346 347 348
    /* find the group structure and destroy it */
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
    if (!g)
349
    {
350
        ABT_rwlock_unlock(ssg_inst->lock);
Shane Snyder's avatar
Shane Snyder committed
351 352
        fprintf(stderr, "Error: SSG unable to find expected group reference\n");
        return SSG_FAILURE;
353
    }
354 355
    HASH_DELETE(hh, ssg_inst->group_table, g);
    ABT_rwlock_unlock(ssg_inst->lock);
Shane Snyder's avatar
Shane Snyder committed
356
    ssg_group_destroy_internal(g);
357

Shane Snyder's avatar
Shane Snyder committed
358
    return SSG_SUCCESS;
359 360
}

Shane Snyder's avatar
Shane Snyder committed
361 362
ssg_group_id_t ssg_group_join(
    ssg_group_id_t in_group_id,
363 364
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
365
{
Shane Snyder's avatar
Shane Snyder committed
366 367
    ssg_group_descriptor_t *in_group_descriptor = (ssg_group_descriptor_t *)in_group_id;
    char *self_addr_str = NULL;
368
    hg_addr_t group_target_addr = HG_ADDR_NULL;
Shane Snyder's avatar
Shane Snyder committed
369 370 371 372
    char *group_name = NULL;
    int group_size;
    void *view_buf = NULL;
    const char **addr_strs = NULL;
373
    hg_return_t hret;
374
    int sret;
Shane Snyder's avatar
Shane Snyder committed
375 376
    ssg_group_t *g = NULL;
    ssg_group_id_t g_id = SSG_GROUP_ID_NULL;
377

Shane Snyder's avatar
Shane Snyder committed
378
    if (!ssg_inst || in_group_id == SSG_GROUP_ID_NULL) goto fini;
379

Shane Snyder's avatar
Shane Snyder committed
380
    if (in_group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
381
    {
Shane Snyder's avatar
Shane Snyder committed
382
        fprintf(stderr, "Error: SSG unable to join a group it is already a member of\n");
383 384
        goto fini;
    }
Shane Snyder's avatar
Shane Snyder committed
385
    else if (in_group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
386
    {
Shane Snyder's avatar
Shane Snyder committed
387
        fprintf(stderr, "Error: SSG unable to join a group it is attached to\n");
388 389
        goto fini;
    }
390

391 392 393 394 395 396 397 398 399
    /* lookup the address of the target group member in the GID */
    hret = margo_addr_lookup(ssg_inst->mid, in_group_descriptor->addr_str,
        &group_target_addr);
    if (hret != HG_SUCCESS) goto fini;

    sret = ssg_group_join_send(in_group_descriptor, group_target_addr,
        &group_name, &group_size, &view_buf);
    if (sret != SSG_SUCCESS || !group_name || !view_buf) goto fini;

Shane Snyder's avatar
Shane Snyder committed
400
    /* get my address string */
401
    SSG_GET_SELF_ADDR_STR(ssg_inst->mid, self_addr_str);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
402 403
    if (self_addr_str == NULL) goto fini;

Shane Snyder's avatar
Shane Snyder committed
404 405
    /* set up address string array for all group members */
    addr_strs = ssg_addr_str_buf_to_list(view_buf, group_size);
406 407
    if (!addr_strs) goto fini;

Shane Snyder's avatar
Shane Snyder committed
408 409 410 411 412 413 414 415 416 417 418 419 420
    /* append self address string to list of group member address strings */
    addr_strs = realloc(addr_strs, (group_size+1)*sizeof(char *));
    if(!addr_strs) goto fini;
    addr_strs[group_size++] = self_addr_str;

    g = ssg_group_create_internal(group_name, addr_strs, group_size,
            update_cb, update_cb_dat);
    if (g)
    {
        /* on successful creation, dup the group descriptor and return
         * it for the caller to hold on to
         */
        g_id = (ssg_group_id_t)ssg_group_descriptor_dup(g->descriptor);
421 422 423 424 425 426 427 428 429 430 431
        if (g_id == SSG_GROUP_ID_NULL)
        {
            ABT_rwlock_wrlock(ssg_inst->lock);
            HASH_DELETE(hh, ssg_inst->group_table, g);
            ABT_rwlock_unlock(ssg_inst->lock);
            ssg_group_destroy_internal(g);
            goto fini;
        }

        /* don't free on success */
        group_name = NULL;
Shane Snyder's avatar
Shane Snyder committed
432
    }
Shane Snyder's avatar
Shane Snyder committed
433 434

fini:
435 436
    if (group_target_addr != HG_ADDR_NULL)
        margo_addr_free(ssg_inst->mid, group_target_addr);
Shane Snyder's avatar
Shane Snyder committed
437 438 439
    free(addr_strs);
    free(view_buf);
    free(group_name);
440
    free(self_addr_str);
441

Shane Snyder's avatar
Shane Snyder committed
442
    return g_id;
Shane Snyder's avatar
Shane Snyder committed
443 444
}

Shane Snyder's avatar
Shane Snyder committed
445
int ssg_group_leave(
446
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
447
{
Shane Snyder's avatar
Shane Snyder committed
448
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
449 450 451 452
    ssg_group_t *g = NULL;
    hg_addr_t group_target_addr = HG_ADDR_NULL;
    hg_return_t hret;
    int sret = SSG_FAILURE;
453

454
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) goto fini;
455

456 457
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
    {
Shane Snyder's avatar
Shane Snyder committed
458
        fprintf(stderr, "Error: SSG unable to leave group it is not a member of\n");
459
        goto fini;
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 493 494 495 496
    ABT_rwlock_rdlock(ssg_inst->lock);
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
    if (!g)
    {
        ABT_rwlock_unlock(ssg_inst->lock);
        goto fini;
    }

    /* send the leave req to the first member in the view */
    hret = margo_addr_dup(ssg_inst->mid, g->view.member_map->addr, &group_target_addr);
    if (hret != HG_SUCCESS)
    {
        ABT_rwlock_unlock(ssg_inst->lock);
        goto fini;
    }
    ABT_rwlock_unlock(ssg_inst->lock);

    sret = ssg_group_leave_send(group_descriptor, g->self_id, group_target_addr);
    if (sret != SSG_SUCCESS) goto fini;

    /* at least one group member knows of the leave request -- safe to
     * shutdown the group locally
     */

    /* re-lookup the group as we don't hold the lock while sending the leave req */
    ABT_rwlock_wrlock(ssg_inst->lock);
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
    if (g)
    {
        HASH_DELETE(hh, ssg_inst->group_table, g);
        ABT_rwlock_unlock(ssg_inst->lock);
        ssg_group_destroy_internal(g);
    }
Shane Snyder's avatar
Shane Snyder committed
497 498
    else
        ABT_rwlock_unlock(ssg_inst->lock);
499 500 501 502 503 504 505 506

    sret = SSG_SUCCESS;

fini:
    if (group_target_addr != HG_ADDR_NULL)
        margo_addr_free(ssg_inst->mid, group_target_addr);

    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
507 508
}

509
#if 0
510 511 512
int ssg_group_attach(
    ssg_group_id_t group_id)
{
Shane Snyder's avatar
Shane Snyder committed
513
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
514 515 516 517 518 519
    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;
520

521
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) goto fini;
522

523 524 525 526 527 528 529 530 531 532 533
    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;
    }
534

535 536 537 538 539 540 541
    /* 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;

Shane Snyder's avatar
Shane Snyder committed
542
    /* set up address string array for all group members */
543 544 545 546 547 548 549
    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));
Shane Snyder's avatar
Shane Snyder committed
550
    ag->name = strdup(group_name);
551 552 553 554 555
    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 */
Shane Snyder's avatar
Shane Snyder committed
556
    sret = ssg_group_view_create(addr_strs, group_size, NULL, ag->lock, &ag->view, NULL);
557 558 559 560 561 562 563 564 565
    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 */
Shane Snyder's avatar
Shane Snyder committed
566
    group_name = NULL;
567 568
    ag = NULL;
fini:
Shane Snyder's avatar
Shane Snyder committed
569
    if (ag) ssg_attached_group_destroy(ag);
570
    free(addr_strs);
Shane Snyder's avatar
Shane Snyder committed
571 572
    free(view_buf);
    free(group_name);
573 574

    return sret;
575 576 577 578 579
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{
580 581 582
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_attached_group_t *ag;

Shane Snyder's avatar
Shane Snyder committed
583 584
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return SSG_FAILURE;

585 586 587 588 589 590 591 592 593 594 595 596
    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)
    {
Shane Snyder's avatar
Shane Snyder committed
597
        fprintf(stderr, "Error: SSG unable to find expected group attached\n");
598 599 600 601 602
        return SSG_FAILURE;
    }
    HASH_DELETE(hh, ssg_inst->attached_group_table, ag);
    ssg_attached_group_destroy(ag);

603 604
    return SSG_SUCCESS;
}
605
#endif
606

607 608 609
/*********************************
 *** SSG group access routines ***
 *********************************/
Shane Snyder's avatar
Shane Snyder committed
610

611
ssg_member_id_t ssg_get_group_self_id(
612
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
613
{
Shane Snyder's avatar
Shane Snyder committed
614
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
615
    ssg_group_t *g;
616
    ssg_member_id_t self_id;
617

Shane Snyder's avatar
Shane Snyder committed
618
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return SSG_MEMBER_ID_INVALID;
619

620 621 622 623
    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");
624
        return SSG_MEMBER_ID_INVALID;
625 626
    }

627
    ABT_rwlock_rdlock(ssg_inst->lock);
Shane Snyder's avatar
Shane Snyder committed
628 629
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
630 631 632 633 634
    if (g)
        self_id = g->self_id;
    else
        self_id = SSG_MEMBER_ID_INVALID;
    ABT_rwlock_unlock(ssg_inst->lock);
635

636
    return self_id;
Shane Snyder's avatar
Shane Snyder committed
637 638
}

639 640
int ssg_get_group_size(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
641
{
Shane Snyder's avatar
Shane Snyder committed
642
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
Shane Snyder's avatar
Shane Snyder committed
643
    int group_size = 0;
644

Shane Snyder's avatar
Shane Snyder committed
645
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return 0;
646

647 648 649 650
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

651
        ABT_rwlock_rdlock(ssg_inst->lock);
652 653 654
        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
Shane Snyder's avatar
Shane Snyder committed
655 656 657 658 659
        {
            ABT_rwlock_rdlock(g->lock);
            group_size = g->view.size + 1; /* add ourself to view size */
            ABT_rwlock_unlock(g->lock);
        }
660
        ABT_rwlock_unlock(ssg_inst->lock);
661
    }
662
#if 0
663 664 665 666 667 668 669
    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)
Shane Snyder's avatar
Shane Snyder committed
670 671 672 673 674
        {
            ABT_rwlock_rdlock(ag->lock);
            group_size = ag->view.size;
            ABT_rwlock_unlock(ag->lock);
        }
675
    }
676
#endif
677 678 679 680
    else
    {
        fprintf(stderr, "Error: SSG can only obtain size of groups that the caller" \
            " is a member of or an attacher of\n");
681
        return 0;
682
    }
683

Shane Snyder's avatar
Shane Snyder committed
684
    return group_size;
Shane Snyder's avatar
Shane Snyder committed
685 686
}

687 688 689
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
690
{
Shane Snyder's avatar
Shane Snyder committed
691
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
692
    ssg_member_state_t *member_state;
Shane Snyder's avatar
Shane Snyder committed
693
    hg_addr_t member_addr = HG_ADDR_NULL;
694

695 696
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL ||
            member_id == SSG_MEMBER_ID_INVALID)
697 698
        return HG_ADDR_NULL;

699 700 701 702
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

703
        ABT_rwlock_rdlock(ssg_inst->lock);
704 705 706
        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
Shane Snyder's avatar
Shane Snyder committed
707 708 709 710 711 712 713 714
        {
            ABT_rwlock_rdlock(g->lock);
            HASH_FIND(hh, g->view.member_map, &member_id, sizeof(ssg_member_id_t),
                member_state);
            if (member_state) 
                member_addr = member_state->addr;
            ABT_rwlock_unlock(g->lock);
        }
715
        ABT_rwlock_unlock(ssg_inst->lock);
716
    }
717
#if 0
718 719 720 721 722 723 724
    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)
Shane Snyder's avatar
Shane Snyder committed
725 726 727 728 729 730 731 732
        {
            ABT_rwlock_rdlock(ag->lock);
            HASH_FIND(hh, ag->view.member_map, &member_id, sizeof(ssg_member_id_t),
                member_state);
            if (member_state) 
                member_addr = member_state->addr;
            ABT_rwlock_unlock(ag->lock);
        }
733
    }
734
#endif
735 736 737 738 739 740 741
    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;
    }

Shane Snyder's avatar
Shane Snyder committed
742
    return member_addr;
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
}

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
760
    descriptor = SSG_GROUP_ID_NULL;
761 762 763
    return;
}

764 765 766 767 768 769 770 771
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);
}

772 773 774 775 776 777 778
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;
779
    char *gid_buf, *p; 
780 781 782 783 784 785 786 787 788 789 790 791 792

    *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 */
793 794 795 796 797 798
    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);
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 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 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 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 948 949 950 951 952 953
    /* 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;
}

954 955 956 957 958
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;
959 960
    ABT_rwlock group_view_lock;
    int group_size;
961 962 963 964 965 966 967 968
    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;

969
        ABT_rwlock_rdlock(ssg_inst->lock);
970 971 972 973 974
        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
        {
            group_view = &g->view;
975 976
            group_view_lock = g->lock;
            group_size = g->view.size + 1;
977 978
            group_name = g->name;
            strcpy(group_role, "member");
979
            sprintf(group_self_id, "%lu", g->self_id);
980
        }
981
        ABT_rwlock_unlock(ssg_inst->lock);
982
    }
983
#if 0
984 985 986 987 988 989 990 991 992
    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;
993
            group_size = ag->view.size;
994 995 996 997
            group_name = ag->name;
            strcpy(group_role, "attacher");
        }
    }
998
#endif
999 1000 1001 1002 1003 1004 1005 1006 1007
    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)
    {
1008
        ssg_member_state_t *member_state, *tmp_ms;
1009 1010 1011 1012 1013

        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);
1014
        printf("\tsize: %d\n", group_size);
1015
        printf("\tview:\n");
1016
        ABT_rwlock_rdlock(group_view_lock);
1017
        HASH_ITER(hh, group_view->member_map, member_state, tmp_ms)
1018
        {
Shane Snyder's avatar
Shane Snyder committed
1019
            printf("\t\tid: %20lu\taddr: %s\n", member_state->id,
1020
                member_state->addr_str);
1021
        }
1022
        ABT_rwlock_unlock(group_view_lock);
1023 1024 1025 1026
    }
    else
        fprintf(stderr, "Error: SSG unable to find group view associated" \
            " with the given group ID\n");
1027

1028
    return;
Shane Snyder's avatar
Shane Snyder committed
1029 1030
}

1031 1032 1033
/************************************
 *** SSG internal helper routines ***
 ************************************/
1034

Shane Snyder's avatar
Shane Snyder committed
1035 1036 1037
static ssg_group_t * ssg_group_create_internal(
    const char * group_name, const char * const group_addr_strs[],
    int group_size, ssg_membership_update_cb update_cb, void *update_cb_dat)
1038
{
Shane Snyder's avatar
Shane Snyder committed
1039 1040 1041 1042
    uint64_t name_hash;
    char *self_addr_str = NULL;
    int sret;
    int success = 0;
1043
    ssg_group_t *g = NULL, *check_g;
1044

Shane Snyder's avatar
Shane Snyder committed
1045
    if (!ssg_inst) return NULL;
Shane Snyder's avatar
Shane Snyder committed
1046

Shane Snyder's avatar
Shane Snyder committed
1047 1048 1049
    name_hash = ssg_hash64_str(group_name);

    /* get my address string */
1050
    SSG_GET_SELF_ADDR_STR(ssg_inst->mid, self_addr_str);
Shane Snyder's avatar
Shane Snyder committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
    if (self_addr_str == NULL) 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->name = strdup(group_name);
    if (!g->name) goto fini;
    g->update_cb = update_cb;
    g->update_cb_dat = update_cb_dat;
    ABT_rwlock_create(&g->lock);

    /* generate unique descriptor for this group  */
    g->descriptor = ssg_group_descriptor_create(name_hash, self_addr_str,
        SSG_OWNER_IS_MEMBER);
    if (g->descriptor == NULL) goto fini;

    /* initialize the group view */
    sret = ssg_group_view_create(group_addr_strs, group_size, self_addr_str,
        g->lock, &g->view, &g->self_id);
    if (sret != SSG_SUCCESS) goto fini;
    if (g->self_id == SSG_MEMBER_ID_INVALID)
Shane Snyder's avatar
Shane Snyder committed
1073
    {
Shane Snyder's avatar
Shane Snyder committed
1074 1075 1076 1077
        /* if unable to resolve my rank within the group, error out */
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
Shane Snyder's avatar
Shane Snyder committed
1078
    }
1079

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
#ifdef DEBUG
    /* set debug output pointer */
    char *dbg_log_dir = getenv("SSG_DEBUG_LOGDIR");
    if (dbg_log_dir)
    {
        char dbg_log_path[PATH_MAX];
        snprintf(dbg_log_path, PATH_MAX, "%s/ssg-%s-%lu.log",
            dbg_log_dir, g->name, g->self_id);
        g->dbg_log = fopen(dbg_log_path, "a");
        if (!g->dbg_log) goto fini;
    }
    else
    {
        g->dbg_log = stdout;
    }
#endif

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    /* make sure we aren't re-creating an existing group */
    ABT_rwlock_wrlock(ssg_inst->lock);
    HASH_FIND(hh, ssg_inst->group_table, &name_hash, sizeof(uint64_t), check_g);
    if (check_g) goto fini;

    /* add this group reference to the group table */
    HASH_ADD(hh, ssg_inst->group_table, descriptor->name_hash,
        sizeof(uint64_t), g);
    ABT_rwlock_unlock(ssg_inst->lock);

    /* initialize swim failure detector */
Shane Snyder's avatar
Shane Snyder committed
1108 1109
    sret = swim_init(g, ssg_inst->mid, 1);
    if (sret != SSG_SUCCESS)
1110 1111 1112 1113 1114 1115 1116 1117
    {
        ABT_rwlock_wrlock(ssg_inst->lock);
        HASH_DELETE(hh, ssg_inst->group_table, g);
        ABT_rwlock_unlock(ssg_inst->lock);
        goto fini;
    }

    SSG_DEBUG(g, "group create successful (size=%d, self=%s)\n", group_size, self_addr_str);
Shane Snyder's avatar
Shane Snyder committed
1118
    success = 1;
1119

Shane Snyder's avatar
Shane Snyder committed
1120 1121
fini:
    if (!success && g)
1122
    {
1123 1124 1125 1126 1127
#ifdef DEBUG
        /* if using logfile debug output, close the stream */
        if (getenv("SSG_DEBUG_LOGDIR"))
            fclose(g->dbg_log);
#endif
Shane Snyder's avatar
Shane Snyder committed
1128 1129
        if (g->descriptor) ssg_group_descriptor_free(g->descriptor);
        ssg_group_view_destroy(&g->view);
1130
        ABT_rwlock_free(&g->lock);
Shane Snyder's avatar
Shane Snyder committed
1131 1132 1133
        free(g->name);
        free(g);
        g = NULL;
1134
    }
Shane Snyder's avatar
Shane Snyder committed
1135 1136 1137
    free(self_addr_str);

    return g;
1138
}
1139

1140
static int ssg_group_view_create(
1141
    const char * const group_addr_strs[], int group_size,
Shane Snyder's avatar
Shane Snyder committed
1142 1143
    const char * self_addr_str, ABT_rwlock view_lock,
    ssg_group_view_t * view, ssg_member_id_t * self_id)
1144 1145
{
    int i, j, r;
1146 1147
    ABT_thread *lookup_ults = NULL;
    struct ssg_group_lookup_ult_args *lookup_ult_args = NULL;
1148 1149 1150
    const char *self_addr_substr = NULL;
    const char *addr_substr = NULL;
    int aret;
1151
    int sret = SSG_FAILURE;
1152

1153 1154 1155
    if (self_id)
        *self_id = SSG_MEMBER_ID_INVALID;
        
Shane Snyder's avatar
Shane Snyder committed
1156
    if ((self_id != NULL && self_addr_str == NULL) || !view) goto fini;
Shane Snyder's avatar
Shane Snyder committed
1157

1158 1159
    /* allocate lookup ULTs */
    lookup_ults = malloc(group_size * sizeof(*lookup_ults));
1160 1161
    if (lookup_ults == NULL) goto fini;
    for (i = 0; i < group_size; i++) lookup_ults[i] = ABT_THREAD_NULL;
1162
    lookup_ult_args = malloc(group_size * sizeof(*lookup_ult_args));
1163
    if (lookup_ult_args == NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
1164

Shane Snyder's avatar
Shane Snyder committed
1165
    if(self_addr_str)
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    {
        /* 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;
    }

1178
    /* construct view using ULTs to lookup the address of each group member */
Shane Snyder's avatar
Shane Snyder committed
1179
    r = rand() % group_size;
1180 1181 1182 1183 1184
    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
         */
Shane Snyder's avatar
Shane Snyder committed
1185
        j = (r + i) % group_size;
1186

1187 1188
        if (group_addr_strs[j] == NULL || strlen(group_addr_strs[j]) == 0) continue;

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
        /* 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)
            {
1200
                if (self_id)               
1201
                    *self_id = ssg_gen_member_id(group_addr_strs[j]);
1202 1203 1204

                /* don't look up our own address, we already know it */
                continue;
1205 1206 1207 1208
            }
        }

        /* XXX limit outstanding lookups to some max */
1209
        lookup_ult_args[j].addr_str = group_addr_strs[j];
1210
        lookup_ult_args[j].view = view;
Shane Snyder's avatar
Shane Snyder committed
1211
        lookup_ult_args[j].lock = view_lock;
1212 1213
        ABT_pool pool;
        margo_get_handler_pool(ssg_inst->mid, &pool);
1214 1215
        aret = ABT_thread_create(pool, &ssg_group_lookup_ult,
            &lookup_ult_args[j], ABT_THREAD_ATTR_NULL,
1216
            &lookup_ults[j]);
1217
        if (aret != ABT_SUCCESS) goto fini;
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
    }

    /* 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;
1228
        if (aret != ABT_SUCCESS) goto fini;
1229
        else if (lookup_ult_args[i].out != SSG_SUCCESS)
1230
        {
1231 1232
            fprintf(stderr, "Error: SSG unable to lookup HG address %s\n",
                lookup_ult_args[i].addr_str);
1233
            goto fini;
1234 1235 1236
        }
    }

1237 1238 1239
    /* clean exit */
    sret = SSG_SUCCESS;

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
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
1257
}
1258

1259 1260 1261 1262
static void ssg_group_lookup_ult(
    void * arg)
{
    struct ssg_group_lookup_ult_args *l = arg;
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
    ssg_member_id_t member_id = ssg_gen_member_id(l->addr_str);
    hg_addr_t member_addr;
    ssg_member_state_t *ms;
    hg_return_t hret;

    hret = margo_addr_lookup(ssg_inst->mid, l->addr_str, &member_addr);
    if (hret != HG_SUCCESS)
    {
        l->out = SSG_FAILURE;
        return;
    }
1274

1275 1276 1277
    ABT_rwlock_wrlock(l->lock);
    ms = ssg_group_view_add_member(l->addr_str, member_addr, member_id, l->view);
    if (ms)
1278 1279 1280
        l->out = SSG_SUCCESS;
    else
        l->out = SSG_FAILURE;
1281
    ABT_rwlock_unlock(l->lock);
1282 1283 1284 1285 1286

    return;
}

static ssg_member_state_t * ssg_group_view_add_member(
1287 1288
    const char * addr_str, hg_addr_t addr, ssg_member_id_t member_id,
    ssg_group_view_t * view)
1289 1290 1291 1292 1293 1294
{
    ssg_member_state_t *ms;

    ms = calloc(1, sizeof(*ms));
    if (!ms) return NULL;
    ms->addr_str = strdup(addr_str);
1295
    ms->addr = addr;
1296
    if (!ms->addr_str)
1297
    {
1298 1299
        free(ms);