loggp.c 24.8 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#include <string.h>
#include <assert.h>
#include <ross.h>

#include "codes/lp-io.h"
#include "codes/jenkins-hash.h"
#include "codes/model-net-method.h"
#include "codes/model-net.h"
15
#include "codes/model-net-lp.h"
16
#include "codes/model-net-sched.h"
Philip Carns's avatar
Philip Carns committed
17 18
#include "codes/codes_mapping.h"
#include "codes/codes.h"
19
#include "codes/net/loggp.h"
Philip Carns's avatar
Philip Carns committed
20 21 22 23

#define CATEGORY_NAME_MAX 16
#define CATEGORY_MAX 12

24 25 26
#define LP_CONFIG_NM (model_net_lp_config_names[LOGGP])
#define LP_METHOD_NM (model_net_method_names[LOGGP])

27 28 29 30
// conditionally use the new recv-queue stuff. this is only here in the case
// that we want to collect results based on the old model
#define USE_RECV_QUEUE 1

Jonathan Jenkins's avatar
Jonathan Jenkins committed
31 32 33 34 35 36 37 38 39
#define LOGGP_MSG_TRACE 0

#if LOGGP_MSG_TRACE
#define dprintf(_fmt, ...) printf(_fmt, __VA_ARGS__);
#else
#define dprintf(_fmt, ...)
#endif


Philip Carns's avatar
Philip Carns committed
40 41 42
/*Define loggp data types and structs*/
typedef struct loggp_state loggp_state;

43 44 45
/* loggp parameters for a given msg size, as reported by netgauge */
struct param_table_entry
{
46
    uint64_t size;
47 48 49 50 51 52 53 54 55 56 57
    int n;
    double PRTT_10s;
    double PRTT_n0s;
    double PRTT_nPRTT_10ss;
    double L;
    double o_s;
    double o_r;
    double g;
    double G;
    double lsqu_gG;
};
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
typedef struct param_table_entry param_table_entry;

typedef struct loggp_param loggp_param;
// loggp parameters
struct loggp_param
{
    int table_size;
    param_table_entry table[100];
};


struct loggp_state
{
    /* next idle times for network card, both inbound and outbound */
    tw_stime net_send_next_idle;
    tw_stime net_recv_next_idle;
    const char * anno;
    const loggp_param *params;
    struct mn_stats loggp_stats_array[CATEGORY_MAX];
};

/* annotation-specific parameters (unannotated entry occurs at the 
 * last index) */
static uint64_t                  num_params = 0;
static loggp_param             * all_params = NULL;
static const config_anno_map_t * anno_map   = NULL;
84

Philip Carns's avatar
Philip Carns committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
static int loggp_magic = 0;

/* returns a pointer to the lptype struct to use for loggp LPs */
static const tw_lptype* loggp_get_lp_type(void);

/* retrieve the size of the portion of the event struct that is consumed by
 * the loggp module.  The caller should add this value to the size of
 * its own event structure to get the maximum total size of a message.
 */
static int loggp_get_msg_sz(void);

/* Returns the loggp magic number */
static int loggp_get_magic();

99 100 101 102 103 104
/* collective network calls */
static void loggp_collective();

/* collective network calls-- rc */
static void loggp_collective_rc();

Philip Carns's avatar
Philip Carns committed
105 106
/* Modelnet interface events */
/* sets up the loggp parameters through modelnet interface */
107 108 109
static void loggp_configure();

static void loggp_set_params(const char * config_file, loggp_param * params);
Philip Carns's avatar
Philip Carns committed
110 111

/* Issues a loggp packet event call */
112
static tw_stime loggp_packet_event(
Philip Carns's avatar
Philip Carns committed
113 114
     char* category, 
     tw_lpid final_dest_lp, 
115
     uint64_t packet_size, 
116 117
     int is_pull,
     uint64_t pull_size, /* only used when is_pull==1 */
118
     tw_stime offset,
119
     const mn_sched_params *sched_params,
Philip Carns's avatar
Philip Carns committed
120 121 122 123
     int remote_event_size, 
     const void* remote_event, 
     int self_event_size,
     const void* self_event,
124
     tw_lpid src_lp,
Philip Carns's avatar
Philip Carns committed
125 126 127 128
     tw_lp *sender,
     int is_last_pckt);
static void loggp_packet_event_rc(tw_lp *sender);

129 130 131 132 133 134 135 136 137 138 139 140 141
tw_stime loggp_recv_msg_event(
        const char * category,
        tw_lpid final_dest_lp,
        uint64_t msg_size,
        int is_pull,
        uint64_t pull_size,
        tw_stime offset,
        int remote_event_size,
        const void* remote_event,
        tw_lpid src_lp,
        tw_lp *sender);

void loggp_recv_msg_event_rc(tw_lp *sender);
Philip Carns's avatar
Philip Carns committed
142 143 144

static void loggp_report_stats();

145 146 147 148
static tw_lpid loggp_find_local_device(
        const char * annotation,
        int          ignore_annotations,
        tw_lp      * sender);
149

150 151 152
static const struct param_table_entry* find_params(
        uint64_t msg_size,
        const loggp_param *params);
153

Philip Carns's avatar
Philip Carns committed
154 155 156
/* data structure for model-net statistics */
struct model_net_method loggp_method =
{
157
    .mn_configure = loggp_configure,
158
    .mn_register = NULL,
Philip Carns's avatar
Philip Carns committed
159 160
    .model_net_method_packet_event = loggp_packet_event,
    .model_net_method_packet_event_rc = loggp_packet_event_rc,
161 162
    .model_net_method_recv_msg_event = loggp_recv_msg_event,
    .model_net_method_recv_msg_event_rc = loggp_recv_msg_event_rc,
Philip Carns's avatar
Philip Carns committed
163 164 165
    .mn_get_lp_type = loggp_get_lp_type,
    .mn_get_msg_sz = loggp_get_msg_sz,
    .mn_report_stats = loggp_report_stats,
166
    .model_net_method_find_local_device = NULL,
167 168
    .mn_collective_call = loggp_collective,
    .mn_collective_call_rc = loggp_collective_rc
Philip Carns's avatar
Philip Carns committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
};

static void loggp_init(
    loggp_state * ns,
    tw_lp * lp);
static void loggp_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp);
static void loggp_rev_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp);
static void loggp_finalize(
    loggp_state * ns,
    tw_lp * lp);

tw_lptype loggp_lp = {
189 190 191 192 193 194 195
    (init_f) loggp_init,
    (pre_run_f) NULL,
    (event_f) loggp_event,
    (revent_f) loggp_rev_event,
    (final_f) loggp_finalize,
    (map_f) codes_mapping,
    sizeof(loggp_state),
Philip Carns's avatar
Philip Carns committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
};

static void handle_msg_ready_rev_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp);
static void handle_msg_ready_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp);
static void handle_msg_start_rev_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp);
static void handle_msg_start_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp);

/* returns pointer to LP information for loggp module */
static const tw_lptype* loggp_get_lp_type()
{
    return(&loggp_lp);
}

/* returns number of bytes that the loggp module will consume in event
 * messages
 */
static int loggp_get_msg_sz(void)
{
    return(sizeof(loggp_message));
}

/* report network statistics */
static void loggp_report_stats()
{
   /* TODO: Do we have some loggp statistics to report like we have for torus and dragonfly? */
   return;
}
239 240 241 242 243 244 245 246 247 248 249 250 251 252

/* collective network calls */
static void loggp_collective()
{
/* collectives not supported */
    return;
}

static void loggp_collective_rc()
{
/* collectives not supported */
   return;
}

Philip Carns's avatar
Philip Carns committed
253 254 255 256 257 258 259 260 261 262 263
static void loggp_init(
    loggp_state * ns,
    tw_lp * lp)
{
    uint32_t h1 = 0, h2 = 0;
    memset(ns, 0, sizeof(*ns));

    /* all devices are idle to begin with */
    ns->net_send_next_idle = tw_now(lp);
    ns->net_recv_next_idle = tw_now(lp);

264 265 266 267 268 269 270 271
    ns->anno = codes_mapping_get_annotation_by_lpid(lp->gid);
    if (ns->anno == NULL)
        ns->params = &all_params[num_params-1];
    else{
        int id = configuration_get_annotation_index(ns->anno, anno_map);
        ns->params = &all_params[id];
    }

272
    bj_hashlittle2(LP_METHOD_NM, strlen(LP_METHOD_NM), &h1, &h2);
Philip Carns's avatar
Philip Carns committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    loggp_magic = h1+h2;
    /* printf("\n loggp_magic %d ", loggp_magic); */

    return;
}

static void loggp_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp)
{
    assert(m->magic == loggp_magic);

    switch (m->event_type)
    {
289
        case LG_MSG_START:
Philip Carns's avatar
Philip Carns committed
290 291
            handle_msg_start_event(ns, b, m, lp);
            break;
292
        case LG_MSG_READY:
Philip Carns's avatar
Philip Carns committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
            handle_msg_ready_event(ns, b, m, lp);
            break;
        default:
            assert(0);
            break;
    }
}

static void loggp_rev_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp)
{
    assert(m->magic == loggp_magic);

    switch (m->event_type)
    {
311
        case LG_MSG_START:
Philip Carns's avatar
Philip Carns committed
312 313
            handle_msg_start_rev_event(ns, b, m, lp);
            break;
314
        case LG_MSG_READY:
Philip Carns's avatar
Philip Carns committed
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
            handle_msg_ready_rev_event(ns, b, m, lp);
            break;
        default:
            assert(0);
            break;
    }

    return;
}

static void loggp_finalize(
    loggp_state * ns,
    tw_lp * lp)
{
    model_net_print_stats(lp->gid, &ns->loggp_stats_array[0]);
    return;
}

int loggp_get_magic()
{
  return loggp_magic;
}

/* reverse computation for msg ready event */
static void handle_msg_ready_rev_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp)
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
345 346 347 348 349 350 351

    dprintf("%lu (mn): ready msg rc %lu->%lu, size %lu (%3s last)\n"
            "          now:%0.3le, idle[now:%0.3le, prev:%0.3le]\n",
            lp->gid, m->src_gid, m->final_dest_gid, m->net_msg_size_bytes,
            m->event_size_bytes+m->local_event_size_bytes > 0 ? "is" : "not",
            tw_now(lp), ns->net_recv_next_idle, m->net_recv_next_idle_saved);

Philip Carns's avatar
Philip Carns committed
352 353 354 355 356 357 358
    struct mn_stats* stat;

    ns->net_recv_next_idle = m->net_recv_next_idle_saved;
    
    stat = model_net_find_stats(m->category, ns->loggp_stats_array);
    stat->recv_count--;
    stat->recv_bytes -= m->net_msg_size_bytes;
359
    stat->recv_time -= m->recv_time_saved;
Philip Carns's avatar
Philip Carns committed
360

Jonathan Jenkins's avatar
Jonathan Jenkins committed
361 362 363 364
#if USE_RECV_QUEUE
    codes_local_latency_reverse(lp);
#endif

365
    if (m->event_size_bytes && m->is_pull){
366
        int net_id = model_net_get_id(LP_METHOD_NM);
367 368 369
        model_net_event_rc(net_id, lp, m->pull_size);
    }

Philip Carns's avatar
Philip Carns committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    return;
}

/* handler for msg ready event.  This indicates that a message is available
 * to recv, but we haven't checked to see if the recv queue is available yet
 */
static void handle_msg_ready_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp)
{
    tw_stime recv_queue_time = 0;
    tw_event *e_new;
    loggp_message *m_new;
    struct mn_stats* stat;
386
    double recv_time;
387
    const struct param_table_entry *param;
388

389
    param = find_params(m->net_msg_size_bytes, ns->params);
390

391 392 393
    recv_time = ((double)(m->net_msg_size_bytes-1)*param->G);
    /* scale to nanoseconds */
    recv_time *= 1000.0;
394
    m->recv_time_saved = recv_time;
Philip Carns's avatar
Philip Carns committed
395 396 397 398 399 400

    //printf("handle_msg_ready_event(), lp %llu.\n", (unsigned long long)lp->gid);
    /* add statistics */
    stat = model_net_find_stats(m->category, ns->loggp_stats_array);
    stat->recv_count++;
    stat->recv_bytes += m->net_msg_size_bytes;
401
    stat->recv_time += recv_time;
Philip Carns's avatar
Philip Carns committed
402 403 404 405 406 407 408

    /* are we available to recv the msg? */
    /* were we available when the transmission was started? */
    if(ns->net_recv_next_idle > tw_now(lp))
        recv_queue_time += ns->net_recv_next_idle - tw_now(lp);

    /* calculate transfer time based on msg size and bandwidth */
409
    recv_queue_time += recv_time;
Philip Carns's avatar
Philip Carns committed
410

411
    /* bump up input queue idle time accordingly, include gap (g) parameter */
Philip Carns's avatar
Philip Carns committed
412
    m->net_recv_next_idle_saved = ns->net_recv_next_idle;
Philip Carns's avatar
Philip Carns committed
413
    ns->net_recv_next_idle = recv_queue_time + tw_now(lp) + param->g*1000.0;
Philip Carns's avatar
Philip Carns committed
414

Jonathan Jenkins's avatar
Jonathan Jenkins committed
415 416 417 418 419 420 421 422
    dprintf("%lu (mn): ready msg    %lu->%lu, size %lu (%3s last)\n"
            "          now:%0.3le, idle[prev:%0.3le, next:%0.3le], "
            "q-time:%0.3le\n",
            lp->gid, m->src_gid, m->final_dest_gid, m->net_msg_size_bytes,
            m->event_size_bytes+m->local_event_size_bytes > 0 ? "is" : "not",
            tw_now(lp), m->net_recv_next_idle_saved, ns->net_recv_next_idle,
            recv_queue_time);

423 424 425 426 427 428 429
    // if we're using a recv-side queue, then we need to tell the scheduler we
    // are idle
#if USE_RECV_QUEUE
    model_net_method_idle_event(codes_local_latency(lp) +
            ns->net_recv_next_idle - tw_now(lp), 1, lp);
#endif

Philip Carns's avatar
Philip Carns committed
430 431 432 433 434 435
    /* copy only the part of the message used by higher level */
    if(m->event_size_bytes)
    {
      /* schedule event to final destination for when the recv is complete */
//      printf("\n Remote message to LP %d ", m->final_dest_gid); 

436
        void *tmp_ptr = model_net_method_get_edata(LOGGP, m);
437 438
        //char* tmp_ptr = (char*)m;
        //tmp_ptr += loggp_get_msg_sz();
439 440
        if (m->is_pull){
            /* call the model-net event */
441
            int net_id = model_net_get_id(LP_METHOD_NM);
442 443 444 445 446 447 448 449 450 451
            model_net_event(net_id, m->category, m->src_gid, m->pull_size,
                    recv_queue_time, m->event_size_bytes, tmp_ptr, 0, NULL,
                    lp);
        }
        else{
            e_new = tw_event_new(m->final_dest_gid, recv_queue_time, lp);
            m_new = tw_event_data(e_new);
            memcpy(m_new, tmp_ptr, m->event_size_bytes);
            tw_event_send(e_new);
        }
Philip Carns's avatar
Philip Carns committed
452 453 454 455 456 457 458 459 460 461 462 463
    }

    return;
}

/* reverse computation for msg start event */
static void handle_msg_start_rev_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp)
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
464 465 466 467 468 469 470

    dprintf("%lu (mn): start msg rc %lu->%lu, size %lu (%3s last)\n"
            "          now:%0.3le, idle[now:%0.3le, prev:%0.3le]\n",
            lp->gid, m->src_gid, m->final_dest_gid, m->net_msg_size_bytes,
            m->event_size_bytes+m->local_event_size_bytes > 0 ? "is" : "not",
            tw_now(lp), ns->net_send_next_idle, m->net_send_next_idle_saved);

Philip Carns's avatar
Philip Carns committed
471 472
    ns->net_send_next_idle = m->net_send_next_idle_saved;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
473 474 475 476
#if USE_RECV_QUEUE
    model_net_method_send_msg_recv_event_rc(lp);
#endif

477 478
    codes_local_latency_reverse(lp);

Philip Carns's avatar
Philip Carns committed
479 480 481 482 483 484 485 486 487
    if(m->local_event_size_bytes > 0)
    {
        codes_local_latency_reverse(lp);
    }

    mn_stats* stat;
    stat = model_net_find_stats(m->category, ns->loggp_stats_array);
    stat->send_count--;
    stat->send_bytes -= m->net_msg_size_bytes;
488
    stat->send_time -= m->xmit_time_saved;
Philip Carns's avatar
Philip Carns committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

    return;
}

/* handler for msg start event; this indicates that the caller is trying to
 * transmit a message through this NIC
 */
static void handle_msg_start_event(
    loggp_state * ns,
    tw_bf * b,
    loggp_message * m,
    tw_lp * lp)
{
    tw_event *e_new;
    loggp_message *m_new;
    tw_stime send_queue_time = 0;
    mn_stats* stat;
    int mapping_grp_id, mapping_type_id, mapping_rep_id, mapping_offset;
    tw_lpid dest_id;
508
    char lp_group_name[MAX_NAME_LENGTH];
Philip Carns's avatar
Philip Carns committed
509
    int total_event_size;
510
    double xmit_time;
511
    const struct param_table_entry *param;
512

513
    param = find_params(m->net_msg_size_bytes, ns->params);
Philip Carns's avatar
Philip Carns committed
514

515 516
    total_event_size = model_net_get_msg_sz(LOGGP) + m->event_size_bytes +
        m->local_event_size_bytes;
Philip Carns's avatar
Philip Carns committed
517

518 519 520 521 522
    /* NOTE: we do not use the o_s or o_r parameters here; as indicated in
     * the netgauge paper those are typically overlapping with L (and the
     * msg xfer as well) and therefore are more important for overlapping
     * computation rather than simulating communication time.
     */
523
    xmit_time = ((double)(m->net_msg_size_bytes-1)*param->G);
524 525
    /* scale to nanoseconds */
    xmit_time *= 1000.0;
526 527
    m->xmit_time_saved = xmit_time;

Philip Carns's avatar
Philip Carns committed
528 529 530 531 532
    //printf("handle_msg_start_event(), lp %llu.\n", (unsigned long long)lp->gid);
    /* add statistics */
    stat = model_net_find_stats(m->category, ns->loggp_stats_array);
    stat->send_count++;
    stat->send_bytes += m->net_msg_size_bytes;
533
    stat->send_time += xmit_time;
Philip Carns's avatar
Philip Carns committed
534 535 536 537
    if(stat->max_event_size < total_event_size)
        stat->max_event_size = total_event_size;

    /* calculate send time stamp */
538
    send_queue_time = (param->L)*1000.0; 
Philip Carns's avatar
Philip Carns committed
539 540 541 542 543
    /* bump up time if the NIC send queue isn't idle right now */
    if(ns->net_send_next_idle > tw_now(lp))
        send_queue_time += ns->net_send_next_idle - tw_now(lp);

    /* move the next idle time ahead to after this transmission is
544 545
     * _complete_ from the sender's perspective, include gap paramater (g)
     * at this point. 
Philip Carns's avatar
Philip Carns committed
546 547
     */ 
    m->net_send_next_idle_saved = ns->net_send_next_idle;
548 549
    if(ns->net_send_next_idle < tw_now(lp))
        ns->net_send_next_idle = tw_now(lp);
Philip Carns's avatar
Philip Carns committed
550
    ns->net_send_next_idle += xmit_time + param->g*1000.0;
Philip Carns's avatar
Philip Carns committed
551 552

    /* create new event to send msg to receiving NIC */
553 554
    dest_id = model_net_find_local_device(LOGGP, ns->anno, 0,
            m->final_dest_gid);
Philip Carns's avatar
Philip Carns committed
555

Jonathan Jenkins's avatar
Jonathan Jenkins committed
556 557 558 559 560 561 562 563
    dprintf("%lu (mn): start msg    %lu->%lu, size %lu (%3s last)\n"
            "          now:%0.3le, idle[prev:%0.3le, next:%0.3le], "
            "q-time:%0.3le\n",
            lp->gid, m->src_gid, m->final_dest_gid, m->net_msg_size_bytes,
            m->event_size_bytes+m->local_event_size_bytes > 0 ? "is" : "not",
            tw_now(lp), m->net_send_next_idle_saved, ns->net_send_next_idle,
            send_queue_time);

564 565 566 567 568 569 570 571 572 573 574
#if USE_RECV_QUEUE
    model_net_method_send_msg_recv_event(m->final_dest_gid, dest_id, m->src_gid,
            m->net_msg_size_bytes, m->is_pull, m->pull_size,
            m->event_size_bytes, &m->sched_params, m->category, LOGGP, m, lp);
#else 
    void *m_data;
//    printf("\n msg start sending to %d ", dest_id);
    //e_new = tw_event_new(dest_id, send_queue_time, lp);
    //m_new = tw_event_data(e_new);
    e_new = model_net_method_event_new(dest_id, send_queue_time, lp, LOGGP,
            (void**)&m_new, &m_data);
Philip Carns's avatar
Philip Carns committed
575 576 577
    /* copy entire previous message over, including payload from user of
     * this module
     */
578 579 580 581 582 583 584
    memcpy(m_new, m, sizeof(loggp_message));
    if (m->event_size_bytes){
        memcpy(m_data, model_net_method_get_edata(LOGGP, m),
                m->event_size_bytes);
    }

    m_new->event_type = LG_MSG_READY;
Philip Carns's avatar
Philip Carns committed
585 586
    
    tw_event_send(e_new);
587
#endif
Philip Carns's avatar
Philip Carns committed
588

589 590 591
    // now that message is sent, issue an "idle" event to tell the scheduler
    // when I'm next available
    model_net_method_idle_event(codes_local_latency(lp) +
592
            ns->net_send_next_idle - tw_now(lp), 0, lp);
593

Philip Carns's avatar
Philip Carns committed
594 595 596 597 598
    /* if there is a local event to handle, then create an event for it as
     * well
     */
    if(m->local_event_size_bytes > 0)
    {
599
        //char* local_event;
Philip Carns's avatar
Philip Carns committed
600

601
        e_new = tw_event_new(m->src_gid, send_queue_time+codes_local_latency(lp), lp);
Philip Carns's avatar
Philip Carns committed
602 603
        m_new = tw_event_data(e_new);

604
        void * m_loc = (char*) model_net_method_get_edata(LOGGP, m) +
605 606 607
            m->event_size_bytes;
         //local_event = (char*)m;
         //local_event += loggp_get_msg_sz() + m->event_size_bytes;         	 
Philip Carns's avatar
Philip Carns committed
608
        /* copy just the local event data over */
609
        memcpy(m_new, m_loc, m->local_event_size_bytes);
Philip Carns's avatar
Philip Carns committed
610 611 612 613 614 615 616 617 618
        tw_event_send(e_new);
    }
    return;
}

/* Model-net function calls */

/*This method will serve as an intermediate layer between loggp and modelnet. 
 * It takes the packets from modelnet layer and calls underlying loggp methods*/
619
static tw_stime loggp_packet_event(
Philip Carns's avatar
Philip Carns committed
620 621
		char* category,
		tw_lpid final_dest_lp,
622
		uint64_t packet_size,
623 624
                int is_pull,
                uint64_t pull_size, /* only used when is_pull==1 */
625
                tw_stime offset,
626
                const mn_sched_params *sched_params,
Philip Carns's avatar
Philip Carns committed
627 628 629 630
		int remote_event_size,
		const void* remote_event,
		int self_event_size,
		const void* self_event,
631
                tw_lpid src_lp,
Philip Carns's avatar
Philip Carns committed
632 633 634 635 636 637 638 639 640
		tw_lp *sender,
		int is_last_pckt)
{
     tw_event * e_new;
     tw_stime xfer_to_nic_time;
     loggp_message * msg;
     char* tmp_ptr;

     xfer_to_nic_time = codes_local_latency(sender);
641 642 643 644
     e_new = model_net_method_event_new(sender->gid, xfer_to_nic_time+offset,
             sender, LOGGP, (void**)&msg, (void**)&tmp_ptr);
     //e_new = tw_event_new(dest_id, xfer_to_nic_time+offset, sender);
     //msg = tw_event_data(e_new);
Philip Carns's avatar
Philip Carns committed
645 646
     strcpy(msg->category, category);
     msg->final_dest_gid = final_dest_lp;
647
     msg->src_gid = src_lp;
Philip Carns's avatar
Philip Carns committed
648 649 650 651
     msg->magic = loggp_get_magic();
     msg->net_msg_size_bytes = packet_size;
     msg->event_size_bytes = 0;
     msg->local_event_size_bytes = 0;
652
     msg->event_type = LG_MSG_START;
653 654
     msg->is_pull = is_pull;
     msg->pull_size = pull_size;
655
     msg->sched_params = *sched_params;
Philip Carns's avatar
Philip Carns committed
656

657 658
     //tmp_ptr = (char*)msg;
     //tmp_ptr += loggp_get_msg_sz();
Philip Carns's avatar
Philip Carns committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
      
    //printf("\n Sending to LP %d msg magic %d ", (int)dest_id, loggp_get_magic()); 
     /*Fill in loggp information*/     
     if(is_last_pckt) /* Its the last packet so pass in remote event information*/
      {
       if(remote_event_size)
	 {
           msg->event_size_bytes = remote_event_size;
           memcpy(tmp_ptr, remote_event, remote_event_size);
           tmp_ptr += remote_event_size;
	 }
       if(self_event_size)
       {
	   msg->local_event_size_bytes = self_event_size;
	   memcpy(tmp_ptr, self_event, self_event_size);
	   tmp_ptr += self_event_size;
       }
      // printf("\n Last packet size: %d ", loggp_get_msg_sz() + remote_event_size + self_event_size);
      }
     tw_event_send(e_new);
679
     return xfer_to_nic_time;
Philip Carns's avatar
Philip Carns committed
680 681
}

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
tw_stime loggp_recv_msg_event(
        const char * category,
        tw_lpid final_dest_lp,
        uint64_t msg_size,
        int is_pull,
        uint64_t pull_size,
        tw_stime offset,
        int remote_event_size,
        const void* remote_event,
        tw_lpid src_lp,
        tw_lp *sender){
    loggp_message *m;
    void *m_data;

    tw_stime moffset = offset + codes_local_latency(sender);

    // this message goes to myself
    tw_event *e = model_net_method_event_new(sender->gid, moffset, sender,
            LOGGP, (void**)&m, &m_data);

    m->magic = loggp_magic;
    m->event_type = LG_MSG_READY;
    m->src_gid = src_lp;
    m->final_dest_gid = final_dest_lp;
    m->net_msg_size_bytes = msg_size;
    m->event_size_bytes = remote_event_size;
    m->local_event_size_bytes = 0;
    strncpy(m->category, category, CATEGORY_NAME_MAX-1);
    m->category[CATEGORY_NAME_MAX-1]='\0';
    m->is_pull = is_pull;
    m->pull_size = pull_size;
    // default sched params for just calling the receiver (for now...)
    model_net_sched_set_default_params(&m->sched_params);

    // copy the remote event over if necessary
    if (remote_event_size > 0){
        memcpy(m_data, remote_event, remote_event_size);
    }

    tw_event_send(e);

    return moffset;
}

void loggp_recv_msg_event_rc(tw_lp *sender){
    codes_local_latency_reverse(sender);
}

730 731 732 733 734 735 736 737 738 739 740 741
static void loggp_configure(){
    char config_file[MAX_NAME_LENGTH];

    anno_map = codes_mapping_get_lp_anno_map(LP_CONFIG_NM);
    assert(anno_map);
    num_params = anno_map->num_annos + (anno_map->has_unanno_lp > 0);
    all_params = malloc(num_params * sizeof(*all_params));

    for (uint64_t i = 0; i < anno_map->num_annos; i++){
        const char * anno = anno_map->annotations[i];
        int rc = configuration_get_value_relpath(&config, "PARAMS",
                "net_config_file", anno, config_file, MAX_NAME_LENGTH);
742
        if (rc <= 0){
743 744 745 746 747 748 749 750
            tw_error(TW_LOC, "unable to read PARAMS:net_config_file@%s",
                    anno);
        }
        loggp_set_params(config_file, &all_params[i]);
    }
    if (anno_map->has_unanno_lp > 0){
        int rc = configuration_get_value_relpath(&config, "PARAMS",
                "net_config_file", NULL, config_file, MAX_NAME_LENGTH);
751
        if (rc <= 0){
752 753 754 755 756 757 758
            tw_error(TW_LOC, "unable to read PARAMS:net_config_file");
        }
        loggp_set_params(config_file, &all_params[anno_map->num_annos]);
    }
}

void loggp_set_params(const char * config_file, loggp_param * params){
759 760 761 762
    FILE *conf;
    int ret;
    char buffer[512];
    int line_nr = 0;
763
    printf("Loggp configured to use parameters from file %s\n", config_file);
764

765
    conf = fopen(config_file, "r");
766 767 768 769 770 771
    if(!conf)
    {
        perror("fopen");
        assert(0);
    }

772
    params->table_size = 0;
773 774 775 776 777
    while(fgets(buffer, 512, conf))
    {
        line_nr++;
        if(buffer[0] == '#')
            continue;
778
        ret = sscanf(buffer, "%llu %d %lf %lf %lf %lf %lf %lf %lf %lf %lf", 
779 780 781 782 783 784 785 786 787 788 789
            &params->table[params->table_size].size,
            &params->table[params->table_size].n,
            &params->table[params->table_size].PRTT_10s,
            &params->table[params->table_size].PRTT_n0s,
            &params->table[params->table_size].PRTT_nPRTT_10ss,
            &params->table[params->table_size].L,
            &params->table[params->table_size].o_s,
            &params->table[params->table_size].o_r,
            &params->table[params->table_size].g,
            &params->table[params->table_size].G,
            &params->table[params->table_size].lsqu_gG);
790 791 792
        if(ret != 11)
        {
            fprintf(stderr, "Error: malformed line %d in %s\n", line_nr, 
793
                config_file);
794 795
            assert(0);
        }
796
        params->table_size++;
797 798
    }

799
    printf("Parsed %d loggp table entries.\n", params->table_size);
Philip Carns's avatar
Philip Carns committed
800

801
    fclose(conf);
Philip Carns's avatar
Philip Carns committed
802 803 804 805 806 807 808 809 810 811
 
    return;
}

static void loggp_packet_event_rc(tw_lp *sender)
{
    codes_local_latency_reverse(sender);
    return;
}

812 813
/* find the parameters corresponding to the message size we are transmitting
 */
814 815 816
static const struct param_table_entry* find_params(
        uint64_t msg_size,
        const loggp_param *params) {
817 818 819 820 821 822
    int i;

    /* pick parameters based on the next smallest size in the table, but
     * default to beginning or end of table if we are out of range
     */

823
    for(i=0; i < params->table_size; i++)
824
    {
825
        if(params->table[i].size > msg_size)
826 827 828 829
        {
            break;
        }
    }
830 831
    if(i>=params->table_size)
        i = params->table_size-1;
832

833
    return(&params->table[i]);
834 835
}

836 837 838 839
static tw_lpid loggp_find_local_device(
        const char * annotation,
        int          ignore_annotations,
        tw_lp      * sender)
840
{
841
     char lp_group_name[MAX_NAME_LENGTH];
842 843 844
     int mapping_grp_id, mapping_rep_id, mapping_type_id, mapping_offset;
     tw_lpid dest_id;

845 846 847
     //TODO: be annotation-aware
     codes_mapping_get_lp_info(sender->gid, lp_group_name, &mapping_grp_id,
             NULL, &mapping_type_id, NULL, &mapping_rep_id, &mapping_offset);
848 849
     codes_mapping_get_lp_id(lp_group_name, LP_CONFIG_NM, annotation,
             ignore_annotations, mapping_rep_id, mapping_offset, &dest_id);
850 851 852 853 854

    return(dest_id);
}


Philip Carns's avatar
Philip Carns committed
855 856 857 858 859 860 861 862
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ft=c ts=8 sts=4 sw=4 expandtab
 */