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

Jonathan Jenkins's avatar
Jonathan Jenkins committed
7 8 9 10
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
11
#include <errno.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
12 13
#include <stdlib.h>
#include <string.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
14
#include <assert.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
15 16

#include <mercury_proc.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
17
#include <mercury_macros.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
18 19 20 21 22 23 24 25

#include <ssg.h>
#include <ssg-config.h>
#include "def.h"

#ifdef HAVE_MPI
#include <ssg-mpi.h>
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
26 27 28
#ifdef HAVE_MARGO
#include <ssg-margo.h>
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
29

30
#define DO_DEBUG 0
Jonathan Jenkins's avatar
Jonathan Jenkins committed
31 32 33 34 35 36 37 38
#define DEBUG(...) \
    do { \
        if(DO_DEBUG) { \
            printf(__VA_ARGS__); \
            fflush(stdout); \
        } \
    } while(0)

Shane Snyder's avatar
Shane Snyder committed
39 40 41 42
// internal initialization of ssg data structures
static ssg_t ssg_init_internal(hg_class_t *hgcl, hg_addr_t self_addr, int rank,
    int num_addrs, char *addr_buf, int addr_buf_size);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
43 44
// helpers for looking up a group member
static hg_return_t ssg_lookup_cb(const struct hg_cb_info *info);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
45 46 47

static char** setup_addr_str_list(int num_addrs, char * buf);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
// helper for hashing (don't want to pull in jenkins hash)
// see http://www.isthe.com/chongo/tech/comp/fnv/index.html
static uint64_t fnv1a_64(void *data, size_t size);

#if HAVE_MARGO

MERCURY_GEN_PROC(barrier_in_t,
        ((int32_t)(barrier_id)) \
        ((int32_t)(rank)))

// barrier RPC decls
static void proc_barrier(void *arg);
DEFINE_MARGO_RPC_HANDLER(proc_barrier)

#endif

Shane Snyder's avatar
Shane Snyder committed
64
ssg_t ssg_init_config(hg_class_t *hgcl, const char * fname, int is_member)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{
    // file to read
    int fd = -1;
    struct stat st;

    // file content to parse
    char *rdbuf = NULL;
    ssize_t rdsz;

    // parse metadata (strtok)
    char *tok;

    // vars to build up the addr string list
    int addr_cap = 128;
    int addr_len = 0;
    int num_addrs = 0;
    void *buf = NULL;

Shane Snyder's avatar
Shane Snyder committed
83 84 85 86 87 88 89 90 91 92
    // self rank/addr resolution helpers
    hg_addr_t self_addr = HG_ADDR_NULL;
    char * self_addr_str = NULL;
    const char * self_addr_substr = NULL;
    hg_size_t self_addr_size = 0;
    const char * addr_substr = NULL;
    // TODO: what is the purpose of an "external" rank (is_member flag)?
    int rank = is_member ? SSG_RANK_UNKNOWN : SSG_EXTERNAL_RANK;

    // return data
Jonathan Jenkins's avatar
Jonathan Jenkins committed
93 94 95 96
    ssg_t s = NULL;

    // misc return codes
    int ret;
Shane Snyder's avatar
Shane Snyder committed
97
    int hret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    // open file for reading
    fd = open(fname, O_RDONLY);
    if (fd == -1) goto fini;

    // get file size
    ret = fstat(fd, &st);
    if (ret == -1) goto fini;

    // slurp file in all at once
    rdbuf = malloc(st.st_size+1);
    if (rdbuf == NULL) goto fini;

    // load it all in one fell swoop
    rdsz = read(fd, rdbuf, st.st_size);
Shane Snyder's avatar
Shane Snyder committed
113
    if (rdsz != st.st_size) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
114 115
    rdbuf[rdsz]='\0';

Shane Snyder's avatar
Shane Snyder committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    if(is_member) {
        // get my address
        hret = HG_Addr_self(hgcl, &self_addr);
        if (hret != HG_SUCCESS) goto fini;
        hret = HG_Addr_to_string(hgcl, NULL, &self_addr_size, self_addr);
        if (hret != HG_SUCCESS) goto fini;
        self_addr_str = malloc(self_addr_size);
        if (self_addr_str == NULL) goto fini;
        hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_size, self_addr);
        if (hret != HG_SUCCESS) goto fini;

        // strstr is used here b/c there may be inconsistencies in whether the class
        // is included in the address or not (it's not in HG_Addr_to_string, it
        // should be in ssg_init_config)
        self_addr_substr = strstr(self_addr_str, "://");
        if (self_addr_substr == NULL) goto fini;
        self_addr_substr += 3;
    }

Jonathan Jenkins's avatar
Jonathan Jenkins committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    // strtok the result - each space-delimited address is assumed to be
    // a unique mercury address
    tok = strtok(rdbuf, "\r\n\t ");
    if (tok == NULL) goto fini;

    // build up the address buffer
    buf = malloc(addr_cap);
    if (buf == NULL) goto fini;
    do {
        int tok_sz = strlen(tok);
        if (tok_sz + addr_len + 1 > addr_cap) {
            void * tmp;
            addr_cap *= 2;
            tmp = realloc(buf, addr_cap);
            if (tmp == NULL) goto fini;
            buf = tmp;
        }
Shane Snyder's avatar
Shane Snyder committed
152 153 154 155 156 157 158 159
        if(is_member) {
            // check if this is my addr to resolve rank
            addr_substr = strstr(tok, "://");
            if (addr_substr == NULL) goto fini;
            addr_substr+= 3;
            if (strcmp(self_addr_substr, addr_substr) == 0)
                rank = num_addrs;
        }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
160 161 162 163 164 165
        memcpy((char*)buf + addr_len, tok, tok_sz+1);
        addr_len += tok_sz+1;
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);

Shane Snyder's avatar
Shane Snyder committed
166 167
    // init ssg internal structures
    s = ssg_init_internal(hgcl, self_addr, rank, num_addrs, buf, addr_len);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
168 169
    if (s == NULL) goto fini;

Shane Snyder's avatar
Shane Snyder committed
170 171 172
    // don't free this on success
    buf = NULL;
    self_addr = HG_ADDR_NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
173 174 175 176
fini:
    if (fd != -1) close(fd);
    free(rdbuf);
    free(buf);
Shane Snyder's avatar
Shane Snyder committed
177 178
    if (self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
179 180 181 182
    return s;
}

#ifdef HAVE_MPI
Jonathan Jenkins's avatar
Jonathan Jenkins committed
183
ssg_t ssg_init_mpi(hg_class_t *hgcl, MPI_Comm comm)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
184 185
{
    // my addr
Jonathan Jenkins's avatar
Jonathan Jenkins committed
186
    hg_addr_t self_addr = HG_ADDR_NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
187
    char * self_addr_str = NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
188
    hg_size_t self_addr_size = 0;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
189 190 191 192 193 194 195 196 197 198 199 200 201
    int self_addr_size_int = 0; // for mpi-friendly conversion

    // collective helpers
    char * buf = NULL;
    int * sizes = NULL;
    int * sizes_psum = NULL;
    int comm_size = 0;
    int comm_rank = 0;

    // return data
    ssg_t s = NULL;

    // misc return codes
Jonathan Jenkins's avatar
Jonathan Jenkins committed
202
    hg_return_t hret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
203 204

    // get my address
Jonathan Jenkins's avatar
Jonathan Jenkins committed
205 206 207
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto fini;
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_size, self_addr);
Shane Snyder's avatar
Shane Snyder committed
208
    if (hret != HG_SUCCESS) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
209 210
    self_addr_str = malloc(self_addr_size);
    if (self_addr_str == NULL) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
211 212
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_size, self_addr);
    if (hret != HG_SUCCESS) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    self_addr_size_int = (int)self_addr_size; // null char included in call

    // 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;
    sizes[comm_rank] = self_addr_size_int;
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);

    // compute a exclusive prefix sum of the data sizes,
    // including the total at the end
    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];

    // allgather the addresses
    buf = malloc(sizes_psum[comm_size]);
    if (buf == NULL) goto fini;
    MPI_Allgatherv(self_addr_str, self_addr_size_int, MPI_BYTE,
            buf, sizes, sizes_psum, MPI_BYTE, comm);

Shane Snyder's avatar
Shane Snyder committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    // init ssg internal structures
    s = ssg_init_internal(hgcl, self_addr, comm_rank, comm_size,
        buf, sizes_psum[comm_size]);
    if (s == NULL) goto fini;

    // don't free these on success
    self_addr = HG_ADDR_NULL;
    buf = NULL;
fini:
    if (self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);
    free(sizes);
    free(sizes_psum);
    free(buf);
    return s;
}
#endif

static ssg_t ssg_init_internal(hg_class_t *hgcl, hg_addr_t self_addr, int rank,
    int num_addrs, char *addr_buf, int addr_buf_size)
{
    // arrays of peer address strings and addresses
    char **addr_strs = NULL;
    hg_addr_t *addrs = NULL;

    // return data
    ssg_t s = NULL;

    if(rank == SSG_RANK_UNKNOWN) goto fini;
    if(self_addr == HG_ADDR_NULL && rank != SSG_EXTERNAL_RANK) goto fini;

    // set peer address strings
    addr_strs = setup_addr_str_list(num_addrs, addr_buf);
    if(addr_strs == NULL) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
271 272

    // init peer addresses
Shane Snyder's avatar
Shane Snyder committed
273
    addrs = malloc(num_addrs*sizeof(*addrs));
Jonathan Jenkins's avatar
Jonathan Jenkins committed
274
    if (addrs == NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
275
    for (int i = 0; i < num_addrs; i++) addrs[i] = HG_ADDR_NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
276 277 278 279

    // set up the output
    s = malloc(sizeof(*s));
    if (s == NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
280
    s->hgcl = hgcl;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
281 282
    s->addr_strs = addr_strs; addr_strs = NULL;
    s->addrs = addrs; addrs = NULL;
Shane Snyder's avatar
Shane Snyder committed
283 284 285 286 287
    s->backing_buf = addr_buf;
    s->num_addrs = num_addrs;
    s->buf_size = addr_buf_size;
    s->rank = rank;
    s->addrs[rank] = self_addr; // NOTE: remaining addrs are set in ssg_lookup
288 289 290 291 292 293 294 295 296
#if HAVE_MARGO
    s->mid = MARGO_INSTANCE_NULL;
    s->barrier_rpc_id = 0;
    s->barrier_id = 0;
    s->barrier_count = 0;
    s->barrier_mutex = ABT_MUTEX_NULL;
    s->barrier_cond  = ABT_COND_NULL;
    s->barrier_eventual = ABT_EVENTUAL_NULL;
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
297 298 299 300 301 302 303

fini:
    free(addr_strs);
    free(addrs);
    return s;
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
hg_return_t ssg_resolve_rank(ssg_t s, hg_class_t *hgcl)
{
    if (s->rank == SSG_EXTERNAL_RANK ||
            s->rank != SSG_RANK_UNKNOWN)
        return HG_SUCCESS;

    // helpers
    hg_addr_t self_addr = HG_ADDR_NULL;
    char * self_addr_str = NULL;
    const char * self_addr_substr = NULL;
    hg_size_t self_addr_size = 0;
    const char * addr_substr = NULL;
    int rank = 0;
    hg_return_t hret;

    // get my address
    hret = HG_Addr_self(hgcl, &self_addr);
    if (hret != HG_SUCCESS) goto end;
    hret = HG_Addr_to_string(hgcl, NULL, &self_addr_size, self_addr);
    if (self_addr == NULL) { hret = HG_NOMEM_ERROR; goto end; }
    self_addr_str = malloc(self_addr_size);
    if (self_addr_str == NULL) { hret = HG_NOMEM_ERROR; goto end; }
    hret = HG_Addr_to_string(hgcl, self_addr_str, &self_addr_size, self_addr);
    if (hret != HG_SUCCESS) goto end;

    // strstr is used here b/c there may be inconsistencies in whether the class
    // is included in the address or not (it's not in HG_Addr_to_string, it
    // should be in ssg_init_config)
    self_addr_substr = strstr(self_addr_str, "://");
    if (self_addr_substr == NULL) { hret = HG_INVALID_PARAM; goto end; }
    self_addr_substr += 3;
    for (rank = 0; rank < s->num_addrs; rank++) {
        addr_substr = strstr(s->addr_strs[rank], "://");
        if (addr_substr == NULL) { hret = HG_INVALID_PARAM; goto end; }
        addr_substr+= 3;
        if (strcmp(self_addr_substr, addr_substr) == 0)
            break;
    }
    if (rank == s->num_addrs) {
        hret = HG_INVALID_PARAM;
        goto end;
    }

    // success - set
    s->rank = rank;
    s->addrs[rank] = self_addr; self_addr = HG_ADDR_NULL;

end:
    if (self_addr != HG_ADDR_NULL) HG_Addr_free(hgcl, self_addr);
    free(self_addr_str);

    return hret;
}

Jonathan Jenkins's avatar
Jonathan Jenkins committed
358 359 360 361 362 363 364
typedef struct ssg_lookup_out
{
    hg_return_t hret;
    hg_addr_t addr;
    int *cb_count;
} ssg_lookup_out_t;

365
hg_return_t ssg_lookup(ssg_t s, hg_context_t *hgctx)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
366
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
367 368 369
    // set of outputs
    ssg_lookup_out_t *out = NULL;
    int cb_count = 0;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
370 371 372
    // "effective" rank for the lookup loop
    int eff_rank = 0;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
373 374 375
    // set the hg class up front - need for destructing addrs
    s->hgcl = HG_Context_get_class(hgctx);
    if (s->hgcl == NULL) return HG_INVALID_PARAM;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
376 377 378

    // perform search for my rank if not already set
    if (s->rank == SSG_RANK_UNKNOWN) {
379
        hg_return_t hret = ssg_resolve_rank(s, s->hgcl);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
380
        if (hret != HG_SUCCESS) return hret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
381 382 383 384 385
    }

    if (s->rank == SSG_EXTERNAL_RANK) {
        // do a completely arbitrary effective rank determination to try and
        // prevent everyone talking to the same member at once
Jonathan Jenkins's avatar
Jonathan Jenkins committed
386
        eff_rank = (((intptr_t)hgctx)/sizeof(hgctx)) % s->num_addrs;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
387
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
388
    else {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
389
        eff_rank = s->rank;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
390 391 392 393 394 395 396 397
        cb_count++;
    }

    // init addr metadata
    out = malloc(s->num_addrs * sizeof(*out));
    if (out == NULL) return HG_NOMEM_ERROR;
    // FIXME: lookups don't have a cancellation path, so in an intermediate
    // error we can't free the memory, lest we cause a segfault
Jonathan Jenkins's avatar
Jonathan Jenkins committed
398 399

    // rank is set, perform lookup
Jonathan Jenkins's avatar
Jonathan Jenkins committed
400
    hg_return_t hret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
401
    for (int i = (s->rank != SSG_EXTERNAL_RANK); i < s->num_addrs; i++) {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
402 403 404 405 406
        int r = (eff_rank+i) % s->num_addrs;
        out[r].cb_count = &cb_count;
        hret = HG_Addr_lookup(hgctx, &ssg_lookup_cb, &out[r],
                s->addr_strs[r], HG_OP_ID_IGNORE);
        if (hret != HG_SUCCESS) return hret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
407
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

    // lookups posted, enter the progress loop until finished
    do {
        unsigned int count = 0;
        do {
            hret = HG_Trigger(hgctx, 0, 1, &count);
        } while (hret == HG_SUCCESS && count > 0);
        if (hret != HG_SUCCESS && hret != HG_TIMEOUT) return hret;

        hret = HG_Progress(hgctx, 100);
    } while (cb_count < s->num_addrs &&
            (hret == HG_SUCCESS || hret == HG_TIMEOUT));

    if (hret != HG_SUCCESS && hret != HG_TIMEOUT)
        return hret;

    for (int i = 0; i < s->num_addrs; i++) {
        if (i != s->rank) {
            if (out[i].hret != HG_SUCCESS)
                return out[i].hret;
            else
                s->addrs[i] = out[i].addr;
        }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
431 432
    }

Jonathan Jenkins's avatar
Jonathan Jenkins committed
433 434
    free(out);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
435
    return HG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
436 437
}

Jonathan Jenkins's avatar
Jonathan Jenkins committed
438
#ifdef HAVE_MARGO
439

Jonathan Jenkins's avatar
Jonathan Jenkins committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
// TODO: handle hash collision, misc errors
void ssg_register_barrier(ssg_t s, hg_class_t *hgcl)
{
    if (s->num_addrs == 1) return;

    s->barrier_rpc_id = fnv1a_64(s->backing_buf, s->buf_size);
    hg_return_t hret = HG_Register(hgcl, s->barrier_rpc_id,
            hg_proc_barrier_in_t, NULL, &proc_barrier_handler);
    assert(hret == HG_SUCCESS);
    hret = HG_Register_data(hgcl, s->barrier_rpc_id, s, NULL);
    assert(hret == HG_SUCCESS);

    int aret = ABT_mutex_create(&s->barrier_mutex);
    assert(aret == ABT_SUCCESS);
    aret = ABT_cond_create(&s->barrier_cond);
    assert(aret == ABT_SUCCESS);
    aret = ABT_eventual_create(0, &s->barrier_eventual);
    assert(aret == ABT_SUCCESS);
}

void ssg_set_margo_id(ssg_t s, margo_instance_id mid)
{
    s->mid = mid;
}

margo_instance_id ssg_get_margo_id(ssg_t s)
{
    return s->mid;
}

470 471 472 473 474 475 476 477 478 479 480 481
struct lookup_ult_args
{
    ssg_t ssg;
    margo_instance_id mid;
    int rank;
    hg_return_t out;
};

static void lookup_ult(void *arg)
{
    struct lookup_ult_args *l = arg;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
482
    DEBUG("%d (ult): looking up rank %d\n", l->ssg->rank, l->rank);
483 484
    l->out = margo_addr_lookup(l->mid, l->ssg->addr_strs[l->rank],
            &l->ssg->addrs[l->rank]);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
485
    DEBUG("%d (ult): looked up rank %d\n", l->ssg->rank, l->rank);
486 487
}

Jonathan Jenkins's avatar
Jonathan Jenkins committed
488
// TODO: refactor - code is mostly a copy of ssg_lookup
Jonathan Jenkins's avatar
Jonathan Jenkins committed
489
hg_return_t ssg_lookup_margo(ssg_t s)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
490 491
{
    hg_context_t *hgctx;
492 493 494
    ABT_thread *ults;
    struct lookup_ult_args *args;
    hg_return_t hret = HG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
495 496 497 498

    // "effective" rank for the lookup loop
    int eff_rank = 0;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
499 500
    DEBUG("%d: entered lookup\n", s->rank);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
501
    // set the hg class up front - need for destructing addrs
Jonathan Jenkins's avatar
Jonathan Jenkins committed
502
    hgctx = margo_get_context(s->mid);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
503 504 505 506
    if (hgctx == NULL) return HG_INVALID_PARAM;

    // perform search for my rank if not already set
    if (s->rank == SSG_RANK_UNKNOWN) {
507
        hret = ssg_resolve_rank(s, s->hgcl);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
508 509 510 511 512 513 514 515 516 517 518
        if (hret != HG_SUCCESS) return hret;
    }

    if (s->rank == SSG_EXTERNAL_RANK) {
        // do a completely arbitrary effective rank determination to try and
        // prevent everyone talking to the same member at once
        eff_rank = (((intptr_t)hgctx)/sizeof(hgctx)) % s->num_addrs;
    }
    else
        eff_rank = s->rank;

519 520 521 522 523 524 525 526 527 528 529
    // initialize ULTs
    ults = malloc(s->num_addrs * sizeof(*ults));
    if (ults == NULL) return HG_NOMEM_ERROR;
    args = malloc(s->num_addrs * sizeof(*args));
    if (args == NULL) {
        free(ults);
        return HG_NOMEM_ERROR;
    }
    for (int i = 0; i < s->num_addrs; i++)
        ults[i] = ABT_THREAD_NULL;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
530 531 532 533 534 535 536 537 538 539 540
    DEBUG("%d: beginning thread create loop (%d...%d)\n", s->rank,
            (s->rank!=SSG_EXTERNAL_RANK), s->num_addrs);
    for (int i = (s->rank!=SSG_EXTERNAL_RANK); i < s->num_addrs; i++) {
        int r = (eff_rank+i) % s->num_addrs;
        args[r].ssg = s;
        args[r].mid = s->mid;
        args[r].rank = r;

        DEBUG("%d: lookup: create thread for rank %d\n", s->rank, r);
        int aret = ABT_thread_create(*margo_get_handler_pool(s->mid), &lookup_ult,
                &args[r], ABT_THREAD_ATTR_NULL, &ults[r]);
541 542 543 544
        if (aret != ABT_SUCCESS) {
            hret = HG_OTHER_ERROR;
            goto fin;
        }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
545
    }
546

Jonathan Jenkins's avatar
Jonathan Jenkins committed
547 548 549 550 551 552 553 554
    // wait on all
    for (int i = (s->rank!=SSG_EXTERNAL_RANK); i < s->num_addrs; i++) {
        int r = (eff_rank+i) % s->num_addrs;
        DEBUG("%d: lookup: join thread for rank %d\n", s->rank, r);
        int aret = ABT_thread_join(ults[r]);
        DEBUG("%d: lookup: join thread for rank %d fin\n", s->rank, r);
        ABT_thread_free(&ults[r]);
        ults[r] = ABT_THREAD_NULL; // in case of cascading failure from join
555 556
        if (aret != ABT_SUCCESS) {
            hret = HG_OTHER_ERROR;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
557
            break;
558
        }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
559 560 561
        else if (args[r].out != HG_SUCCESS) {
            hret = args[r].out;
            break;
562 563 564
        }
    }

Shane Snyder's avatar
Shane Snyder committed
565
#ifdef HAVE_SWIM_FD
566
    // initialize swim failure detector
567 568
    if(s->rank != SSG_EXTERNAL_RANK)
        s->swim_ctx = swim_init(s->mid, s, 1);
Shane Snyder's avatar
Shane Snyder committed
569 570
#endif
    
571 572 573 574 575
fin:
    // cleanup
    if (ults != NULL) {
        for (int i = 0; i < s->num_addrs; i++) {
            if (ults[i] != ABT_THREAD_NULL) {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
576
                DEBUG("%d: lookup failed, cancelling ULT %d\n", s->rank, i);
577 578 579 580 581 582 583 584 585
                ABT_thread_cancel(ults[i]);
                ABT_thread_free(ults[i]);
            }
        }
        free(ults);
    }
    if (args != NULL) free(args);

    return hret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
586
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 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

// TODO: process errors in a sane manner
static void proc_barrier(void *arg)
{
    barrier_in_t in;
    hg_return_t hret;
    int aret;
    hg_handle_t h = arg;
    struct hg_info *info = HG_Get_info(h);
    ssg_t s = HG_Registered_data(info->hg_class, info->id);

    assert(s->rank == 0);

    hret = HG_Get_input(h, &in);
    assert(hret == HG_SUCCESS);

    DEBUG("%d: barrier ult: rx round %d from %d\n", s->rank, in.barrier_id,
            in.rank);
    // first wait until the nth barrier has been processed
    aret = ABT_mutex_lock(s->barrier_mutex);
    assert(aret == ABT_SUCCESS);
    while (s->barrier_id < in.barrier_id) {
        DEBUG("%d: barrier ult: waiting to enter round %d\n", s->rank,
                in.barrier_id);
        aret = ABT_cond_wait(s->barrier_cond, s->barrier_mutex);
        assert(aret == ABT_SUCCESS);
    }

    // inform all other ULTs waiting on this
    aret = ABT_cond_signal(s->barrier_cond);
    assert(aret == ABT_SUCCESS);
    // now wait until all barriers have been rx'd
    DEBUG("%d: barrier ult: out, incr count to %d\n", s->rank,
            s->barrier_count+1);
    s->barrier_count++;
    while (s->barrier_count < s->num_addrs-1) {
        DEBUG("%d: barrier ult: waiting (count at %d)\n", s->rank,
                s->barrier_count);
        aret = ABT_cond_wait(s->barrier_cond, s->barrier_mutex);
        assert(aret == ABT_SUCCESS);
    }
    DEBUG("%d: barrier ult: count compl, signal and respond\n", s->rank);
    // all barriers rx'd, inform other ULTs
    ABT_cond_signal(s->barrier_cond);
    ABT_mutex_unlock(s->barrier_mutex);

    hret = margo_respond(s->mid, h, NULL);
    assert(hret == HG_SUCCESS);
    HG_Destroy(h);

    DEBUG("%d: barrier ult: respond compl, count at %d\n", s->rank,
            s->barrier_count);

    aret = ABT_mutex_lock(s->barrier_mutex);
    assert(aret == ABT_SUCCESS);
    // done -> I'm the last ULT to enter, I do the eventual set
    int is_done = (++s->barrier_count) == 2*(s->num_addrs-1);
    if (is_done) s->barrier_count = 0;
    aret = ABT_mutex_unlock(s->barrier_mutex);
    assert(aret == ABT_SUCCESS);
    if (is_done) {
        aret = ABT_eventual_set(s->barrier_eventual, NULL, 0);
        assert(aret == ABT_SUCCESS);
    }
}

hg_return_t ssg_barrier_margo(ssg_t s)
{
    // non-members can't barrier
    if (s->rank < 0) return HG_INVALID_PARAM;

    // return immediately if a singleton group
    if (s->num_addrs == 1) return HG_SUCCESS;

    int aret = ABT_eventual_reset(s->barrier_eventual);
    if (aret != ABT_SUCCESS) return HG_OTHER_ERROR;

    DEBUG("%d: barrier: lock and incr id to %d\n", s->rank, s->barrier_id+1);
    int bid;
    // init the barrier state
    aret = ABT_mutex_lock(s->barrier_mutex);
    if (aret != ABT_SUCCESS) return HG_OTHER_ERROR;
    bid = ++s->barrier_id;
    aret = ABT_cond_broadcast(s->barrier_cond);
    if (aret != ABT_SUCCESS) {
        ABT_mutex_unlock(s->barrier_mutex); return HG_OTHER_ERROR;
    }
    aret = ABT_mutex_unlock(s->barrier_mutex);
    if (aret != ABT_SUCCESS) return HG_OTHER_ERROR;

    if (s->rank > 0) {
        DEBUG("%d: barrier: create and forward to 0\n", s->rank);
        barrier_in_t in;
        hg_handle_t h;
        hg_return_t hret = HG_Create(margo_get_context(s->mid),
                ssg_get_addr(s, 0), s->barrier_rpc_id, &h);
        if (hret != HG_SUCCESS) return hret;

        in.rank = s->rank;
        in.barrier_id = bid;
        hret = margo_forward(s->mid, h, &in);
        DEBUG("%d: barrier: finish\n", s->rank);
        HG_Destroy(h);
        if (hret != HG_SUCCESS) return hret;
    }
    else {
        DEBUG("%d: barrier: wait on eventual\n", s->rank);
        aret = ABT_eventual_wait(s->barrier_eventual, NULL);
        if (aret != ABT_SUCCESS) return HG_OTHER_ERROR;
    }

    return HG_SUCCESS;
}

Jonathan Jenkins's avatar
Jonathan Jenkins committed
701 702
#endif

Jonathan Jenkins's avatar
Jonathan Jenkins committed
703 704
void ssg_finalize(ssg_t s)
{
705 706
    if (s == SSG_NULL) return;

707 708 709 710 711
#ifdef HAVE_SWIM_FD
    if(s->swim_ctx)
        swim_finalize(s->swim_ctx);
#endif

Jonathan Jenkins's avatar
Jonathan Jenkins committed
712 713 714 715 716 717 718 719
#ifdef HAVE_MARGO
    if (s->barrier_mutex != ABT_MUTEX_NULL)
        ABT_mutex_free(&s->barrier_mutex);
    if (s->barrier_cond != ABT_COND_NULL)
        ABT_cond_free(&s->barrier_cond);
    if (s->barrier_eventual != ABT_EVENTUAL_NULL)
        ABT_eventual_free(&s->barrier_eventual);
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
720
    for (int i = 0; i < s->num_addrs; i++) {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
721
        if (s->addrs[i] != HG_ADDR_NULL) HG_Addr_free(s->hgcl, s->addrs[i]);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    }
    free(s->backing_buf);
    free(s->addr_strs);
    free(s->addrs);
    free(s);
}

int ssg_get_rank(const ssg_t s)
{
    return s->rank;
}

int ssg_get_count(const ssg_t s)
{
    return s->num_addrs;
}

Jonathan Jenkins's avatar
Jonathan Jenkins committed
739
hg_addr_t ssg_get_addr(const ssg_t s, int rank)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
740
{
741
    if (rank >= 0 && rank < s->num_addrs)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
742 743
        return s->addrs[rank];
    else
Jonathan Jenkins's avatar
Jonathan Jenkins committed
744
        return HG_ADDR_NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
745 746 747 748
}

const char * ssg_get_addr_str(const ssg_t s, int rank)
{
749
    if (rank >= 0 && rank < s->num_addrs)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
750 751 752 753 754 755 756
        return s->addr_strs[rank];
    else
        return NULL;
}

// serialization format looks like:
// < num members, buffer size, buffer... >
Jonathan Jenkins's avatar
Jonathan Jenkins committed
757
// doesn't attempt to grab hg_addr's, string buffers, etc. - client will be
Jonathan Jenkins's avatar
Jonathan Jenkins committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 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
// responsible for doing a separate address lookup routine
hg_return_t hg_proc_ssg_t(hg_proc_t proc, ssg_t *s)
{
    // error and return handling
    hg_return_t hret = HG_SUCCESS;
    char * err_str = NULL;

    // input/output vars + helpers for ssg decode setup
    ssg_t ss = NULL;

    switch(hg_proc_get_op(proc)) {
        case HG_ENCODE:
            ss = *s;
            // encode address count
            hret = hg_proc_int32_t(proc, &ss->num_addrs);
            if (hret != HG_SUCCESS) { err_str = "ssg num addrs"; goto end; }
            // encode addr
            hret = hg_proc_int32_t(proc, &ss->buf_size);
            if (hret != HG_SUCCESS) { err_str = "ssg buf size"; goto end; }
            // encode addr string, simple as blitting the backing buffer
            hret = hg_proc_memcpy(proc, ss->backing_buf, ss->buf_size);
            if (hret != HG_SUCCESS) { err_str = "ssg addr buf"; goto end; }
            break;

        case HG_DECODE:
            // create the output
            *s = NULL;
            ss = malloc(sizeof(*ss));
            if (ss == NULL) {
                err_str = "ssg alloc";
                hret = HG_NOMEM_ERROR;
                goto end;
            }
            ss->addr_strs = NULL;
            ss->addrs = NULL;
            ss->backing_buf = NULL;
            // get address count
            hret = hg_proc_int32_t(proc, &ss->num_addrs);
            if (hret != HG_SUCCESS) { err_str = "ssg num addrs"; goto end; }
            // get number of bytes for the address
            hret = hg_proc_int32_t(proc, &ss->buf_size);
            if (hret != HG_SUCCESS) { err_str = "ssg buf size"; goto end; }
            // allocate output buffer
            ss->backing_buf = malloc(ss->buf_size);
            if (hret != HG_SUCCESS) {
                err_str = "ssg buf alloc";
                hret = HG_NOMEM_ERROR;
                goto end;
            }
            hret = hg_proc_memcpy(proc, ss->backing_buf, ss->buf_size);
            if (hret != HG_SUCCESS) { err_str = "ssg addr buf"; goto end; }

            // set the remaining ssg vars

            ss->addr_strs = NULL; ss->addrs = NULL;
            ss->rank = -1; // receivers aren't part of the group

            ss->addr_strs = setup_addr_str_list(ss->num_addrs, ss->backing_buf);
            if (ss->addr_strs == NULL) {
                err_str = "ssg addr strs alloc";
                hret = HG_NOMEM_ERROR;
                goto end;
            }

            ss->addrs = malloc(ss->num_addrs * sizeof(*ss->addrs));
            if (ss->addrs == NULL) {
                err_str = "ssg addrs alloc";
                hret = HG_NOMEM_ERROR;
                goto end;
            }
            for (int i = 0; i < ss->num_addrs; i++) {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
829
                ss->addrs[i] = HG_ADDR_NULL;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
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
            }

            // success: set the output
            *s = ss;
            break;

        case HG_FREE:
            if (s != NULL && *s != NULL) {
                err_str = "ssg shouldn't be freed via HG_Free_*";
                hret = HG_INVALID_PARAM;
            }
            goto end;

        default:
            err_str = "bad proc mode";
            hret = HG_INVALID_PARAM;
    }
end:
    if (err_str) {
        HG_LOG_ERROR("Proc error: %s", err_str);
        if (hg_proc_get_op(proc) == HG_DECODE) {
            free(ss->addr_strs);
            free(ss->addrs);
            free(ss->backing_buf);
            free(ss);
        }
    }
    return hret;
}

860 861 862 863 864 865 866 867 868 869 870 871 872 873
int ssg_dump(const ssg_t s, const char *fname)
{
    // file to write to
    int fd = -1;
    ssize_t written;

    // string to xform and dump
    char * addrs_dup = NULL;
    char * tok = NULL;
    char * addrs_dup_end = NULL;

    // return code
    int ret = 0;

874 875
    // copy the backing buffer, replacing all null chars with
    // newlines
876 877 878 879 880
    addrs_dup = malloc(s->buf_size);
    if (addrs_dup == NULL) { errno = ENOMEM; ret = -1; goto end; }
    memcpy(addrs_dup, s->backing_buf, s->buf_size);
    tok = addrs_dup;
    addrs_dup_end = addrs_dup + s->buf_size;
881
    for (int i = 0; i < s->num_addrs; i++) {
882 883 884 885 886 887
        tok = memchr(tok, '\0', addrs_dup_end - tok);
        if (tok == NULL) { errno = EINVAL; ret = -1; goto end; }
        *tok = '\n';
    }

    // open the file and dump in a single call
888
    fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0644);
889 890
    if (fd == -1) { ret = -1; goto end; }
    // don't include the null char at the end
891 892
    written = write(fd, addrs_dup, s->buf_size);
    if (written != s->buf_size) ret = -1;
893 894 895 896 897 898 899 900

end:
    free(addrs_dup);
    if (fd != -1) close(fd);

    return ret;
}

Jonathan Jenkins's avatar
Jonathan Jenkins committed
901
static hg_return_t ssg_lookup_cb(const struct hg_cb_info *info)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
902
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
903 904 905 906 907 908 909
    ssg_lookup_out_t *out = info->arg;
    *out->cb_count += 1;
    out->hret = info->ret;
    if (out->hret != HG_SUCCESS)
        out->addr = HG_ADDR_NULL;
    else
        out->addr = info->info.lookup.addr;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
910
    return HG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
911 912 913 914 915 916 917 918 919 920 921 922 923 924
}

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

    ret[0] = buf;
    for (int i = 1; i < num_addrs; i++) {
        char * a = ret[i-1];
        ret[i] = a + strlen(a) + 1;
    }
    return ret;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
925 926 927 928 929 930 931 932 933 934 935 936

static uint64_t fnv1a_64(void *data, size_t size)
{
    uint64_t hash = 14695981039346656037ul;
    unsigned char *d = data;

    for (size_t i = 0; i < size; i++) {
        hash ^= (uint64_t)*d++;
        hash *= 1099511628211;
    }
    return hash;
}