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

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

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

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

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

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

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

47 48 49
/***************************************************
 *** SSG runtime intialization/shutdown routines ***
 ***************************************************/
50

51 52
int ssg_init(
    margo_instance_id mid)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
53
{
54 55 56 57 58 59 60 61 62 63
    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;

64
    ssg_register_rpcs();
65

66 67
    return SSG_SUCCESS;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
68

69
int ssg_finalize()
70
{
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    ssg_group_t *g, *tmp;

    if (!ssg_inst)
        return SSG_FAILURE;

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

    free(ssg_inst);
    ssg_inst = NULL;

86
    return SSG_SUCCESS;
87
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
88

89 90 91
/*************************************
 *** SSG group management routines ***
 *************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
92

93
int ssg_group_create(
94 95
    const char * group_name,
    const char * const group_addr_strs[],
96 97
    int group_size,
    ssg_group_id_t * group_id)
98
{
99
    hg_class_t *hgcl = NULL;
Shane Snyder's avatar
Shane Snyder committed
100
    hg_addr_t self_addr = HG_ADDR_NULL;
101
    char *self_addr_str = NULL;
102
    hg_size_t self_addr_str_size = 0;
103 104 105 106
    const char *self_addr_substr = NULL;
    const char *addr_substr = NULL;
    int i;
    ssg_group_t *g = NULL;
107
    ssg_group_id_t new_gid;
108
    hg_return_t hret;
109
    int sret = SSG_FAILURE;
110

111 112 113
    if (!ssg_inst) goto fini;

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

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    /* generate a unique ID for this group  */
    ssg_generate_group_id(group_name, group_addr_strs[0], &new_gid);

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

    /* allocate an SSG group data structure and initialize some of it */
    g = malloc(sizeof(*g));
    if (!g) goto fini;
    memset(g, 0, sizeof(*g));
    g->group_name = strdup(group_name);
    if (!g->group_name) goto fini;
    memcpy(&g->group_id, &new_gid, sizeof(new_gid));
    // TODO? g->self_id = -1;
    g->group_view.size = group_size;
    g->group_view.member_states = malloc(
        group_size * sizeof(*g->group_view.member_states));
    if (!g->group_view.member_states) goto fini;
    memset(g->group_view.member_states, 0,
        group_size * sizeof(*g->group_view.member_states));

138
    /* get my address */
139 140
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto fini;
141
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_str_size, self_addr);
142
    if (hret != HG_SUCCESS) goto fini;
143
    self_addr_str = malloc(self_addr_str_size);
144
    if (self_addr_str == NULL) goto fini;
145
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_str_size, self_addr);
146 147
    if (hret != HG_SUCCESS) goto fini;

148 149 150 151
    /* 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)
     */
152
    self_addr_substr = strstr(self_addr_str, "://");
Shane Snyder's avatar
Shane Snyder committed
153 154 155 156
    if (self_addr_substr == NULL)
        self_addr_substr = self_addr_str;
    else
        self_addr_substr += 3;
Shane Snyder's avatar
Shane Snyder committed
157

158 159 160 161
    /* resolve my rank within the group */
    for (i = 0; i < group_size; i++)
    {
        addr_substr = strstr(group_addr_strs[i], "://");
Shane Snyder's avatar
Shane Snyder committed
162 163 164 165
        if (addr_substr == NULL)
            addr_substr = group_addr_strs[i];
        else
            addr_substr += 3;
166 167 168
        if (strcmp(self_addr_substr, addr_substr) == 0)
        {
            /* this is my address -- my rank is the offset in the address array */
169 170
            g->self_id = i; // TODO 
            g->group_view.member_states[i].addr = self_addr;
171 172 173 174
        }
        else
        {
            /* initialize group member addresses to NULL before looking them up */
175
            g->group_view.member_states[i].addr = HG_ADDR_NULL;
176
        }
177
        g->group_view.member_states[i].is_member = 1;
178
    }
179
#if 0
180
    /* TODO: if unable to resolve my rank within the group, error out */
181
    if (g->self_id == -1)
182 183 184 185 186
    {
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
    }
187
#endif
188

189
    /* lookup hg address information for all group members */
190 191 192 193 194 195 196
    hret = ssg_group_lookup(g, group_addr_strs);
    if (hret != HG_SUCCESS)
    {
        fprintf(stderr, "Error: SSG unable to complete lookup for group %s\n",
            group_name);
        goto fini;
    }
Shane Snyder's avatar
Shane Snyder committed
197
    SSG_DEBUG(g, "group lookup successful (size=%d)\n", group_size);
198

199
#ifdef SSG_USE_SWIM_FD
200 201 202 203 204 205
    int swim_active = 1;
#ifdef SWIM_FORCE_FAIL
    if (g->self_rank == 1)
        swim_active = 0;
#endif

206
    /* initialize swim failure detector */
207 208
    // TODO: we should probably barrier or sync somehow to avoid rpc failures
    // due to timing skew of different ranks initializing swim
209
    g->fd_ctx = (void *)swim_init(g, swim_active);
210
    if (g->fd_ctx == NULL) goto fini;
211 212
#endif

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

216 217
    /* everything successful -- set the output for this call */
    memcpy(group_id, &new_gid, sizeof(new_gid));
218
    sret = SSG_SUCCESS;
219

220
    /* don't free these pointers on success */
221
    self_addr = HG_ADDR_NULL;
222 223
    g = NULL;
fini:
Shane Snyder's avatar
Shane Snyder committed
224 225
    if (hgcl && self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
226 227
    if (g)
    {
228 229
        free(g->group_name);
        free(g->group_view.member_states);
230 231 232
        free(g);
    }

233
    return sret;
234 235
}

236
int ssg_group_create_config(
237
    const char * group_name,
238 239
    const char * file_name,
    ssg_group_id_t * group_id)
240 241 242 243 244 245 246 247 248 249
{
    int fd;
    struct stat st;
    char *rd_buf = NULL;
    ssize_t rd_buf_sz;
    char *tok;
    void *addr_buf = NULL;
    int addr_buf_len = 0, num_addrs = 0;
    int ret;
    const char **addr_strs = NULL;
250
    int sret = SSG_FAILURE;
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

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

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

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

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

288 289
    /* build up the address buffer */
    addr_buf = malloc(rd_buf_sz);
290
    if (addr_buf == NULL) goto fini;
291 292
    do
    {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
293
        int tok_sz = strlen(tok);
294 295
        memcpy((char*)addr_buf + addr_buf_len, tok, tok_sz+1);
        addr_buf_len += tok_sz+1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
296 297 298
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
299 300 301 302 303 304 305
    if (addr_buf_len != rd_buf_sz)
    {
        /* adjust buffer size if our initial guess was wrong */
        void *tmp = realloc(addr_buf, addr_buf_len);
        if (tmp == NULL) goto fini;
        addr_buf = tmp;
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
306

307 308 309
    /* set up address string array for group members */
    addr_strs = ssg_setup_addr_str_list(addr_buf, num_addrs);
    if (!addr_strs) goto fini;
310

311
    /* invoke the generic group create routine using our list of addrs */
312
    sret = ssg_group_create(group_name, addr_strs, num_addrs, group_id);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
313 314

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

321
    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
322 323
}

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

343 344 345
    if (!ssg_inst) goto fini;

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

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

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

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

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

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

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

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

398
    return sret;
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
{
405 406
    ssg_group_t *g;
    int sret;
407

408 409
    if (!ssg_inst)
        return SSG_FAILURE;
410

411 412 413 414
    /* find the group structure and destroy it */
    HASH_FIND(hh, ssg_inst->group_table, &group_id.name_hash, sizeof(uint64_t), g);
    HASH_DELETE(hh, ssg_inst->group_table, g);
    sret = ssg_group_destroy_internal(g);
415

416
    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
417 418
}

419 420 421
int ssg_group_attach(
    ssg_group_id_t group_id)
{
422 423
    hg_return_t hret;

424 425
    if (!ssg_inst)
        return SSG_FAILURE;
426

427 428 429 430
    /* XXX: for now just send to the 1 member addr in the group id */
    hret = ssg_group_attach_send(group_id.addr_str);
    if (hret != HG_SUCCESS)
        return SSG_FAILURE;
431 432 433 434 435 436 437 438 439 440

    return SSG_SUCCESS;
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{
    return SSG_SUCCESS;
}

441 442 443
/*********************************
 *** SSG group access routines ***
 *********************************/
Shane Snyder's avatar
Shane Snyder committed
444

445
ssg_member_id_t ssg_get_group_self_id(
446
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
447
{
448 449 450 451 452 453 454 455 456 457
    ssg_group_t *g;

    if (!ssg_inst)
        return SSG_MEMBER_ID_INVALID;

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

    return g->self_id;
Shane Snyder's avatar
Shane Snyder committed
458 459
}

460 461
int ssg_get_group_size(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
462
{
463 464 465 466 467 468 469 470 471 472
    ssg_group_t *g;

    if (!ssg_inst)
        return 0;

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

    return g->group_view.size;
Shane Snyder's avatar
Shane Snyder committed
473 474
}

475 476 477
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
478
{
479 480 481 482 483 484 485 486 487
    ssg_group_t *g;

    if (!ssg_inst)
        return HG_ADDR_NULL;

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

488
    return g->group_view.member_states[member_id].addr;
Shane Snyder's avatar
Shane Snyder committed
489 490
}

491 492 493
/************************************
 *** SSG internal helper routines ***
 ************************************/
494

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
static void ssg_generate_group_id(
    const char * name, const char * leader_addr_str,
    ssg_group_id_t *group_id)
{
    uint32_t upper, lower;

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

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

    return;
}

511 512 513 514 515 516 517
static const char ** ssg_setup_addr_str_list(
    char * buf, int num_addrs)
{
    const char **ret = malloc(num_addrs * sizeof(*ret));
    if (ret == NULL) return NULL;

    ret[0] = buf;
518 519
    for (int i = 1; i < num_addrs; i++)
    {
520 521
        const char * a = ret[i-1];
        ret[i] = a + strlen(a) + 1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
522
    }
523
    return ret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
524
}
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

static int ssg_group_destroy_internal(ssg_group_t *g)
{
    int i;

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

    return SSG_SUCCESS;
}