mona.c 45.6 KB
Newer Older
1
2
3
4
5
/*
 * (C) 2020 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
6
7
8
9
10
11
#include "mona-types.h"
#include <string.h>

// ------------------------------------------------------------------------------------
// Mona progress loop logic
// ------------------------------------------------------------------------------------
12

13
14
15
16
17
18
19
20
21
22
23
static void mona_progress_loop(void* uarg) {
    mona_instance_t mona = (mona_instance_t)uarg;
    na_return_t trigger_ret, na_ret;
    unsigned int actual_count = 0;
    size_t size;

    while(!mona->finalize_flag) {

        do {
            trigger_ret = NA_Trigger(mona->na_context, 0, 1, NULL, &actual_count);
        } while ((trigger_ret == NA_SUCCESS) && actual_count && !mona->finalize_flag);
24

25
26
27
28
29
30
31
        ABT_pool_get_size(mona->progress_pool, &size);
        if(size)
            ABT_thread_yield();

        // TODO put a high timeout value to avoid busy-spinning
        // if there is no other ULT in the pool that could run
        na_ret = NA_Progress(mona->na_class, mona->na_context, 0);
Matthieu Dorier's avatar
Matthieu Dorier committed
32
        if (na_ret != NA_SUCCESS && na_ret != NA_TIMEOUT) {
33
34
35
36
37
            fprintf(stderr, "WARNING: unexpected return value from NA_Progress (%d)\n", na_ret);
        }
    }
}

38
39
40
41
// ------------------------------------------------------------------------------------
// Mona initialization logic
// ------------------------------------------------------------------------------------

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
mona_instance_t mona_init(
        const char *info_string,
        na_bool_t listen,
        const struct na_init_info *na_init_info)
{
    return mona_init_thread(
            info_string,
            listen,
            na_init_info,
            NA_FALSE);
}

mona_instance_t mona_init_thread(
        const char *info_string,
        na_bool_t listen,
        const struct na_init_info *na_init_info,
        na_bool_t use_progress_es)
{
    int ret;
    ABT_xstream xstream = ABT_XSTREAM_NULL;
    ABT_pool progress_pool = ABT_POOL_NULL;
    mona_instance_t mona = MONA_INSTANCE_NULL;

    if(use_progress_es == NA_TRUE) {

        ret = ABT_pool_create_basic(ABT_POOL_FIFO, ABT_POOL_ACCESS_MPSC, ABT_FALSE, &progress_pool);
        if(ret != ABT_SUCCESS) goto error;

        ret = ABT_xstream_create_basic(ABT_SCHED_DEFAULT, 1, &progress_pool, ABT_SCHED_CONFIG_NULL, &xstream);
        if(ret != ABT_SUCCESS) goto error;

    } else {

        ret = ABT_xstream_self(&xstream);
        if(ret != ABT_SUCCESS) goto error;

        ret = ABT_xstream_get_main_pools(xstream, 1, &progress_pool);
        if(ret != ABT_SUCCESS) goto error;
    }

    mona = mona_init_pool(
            info_string,
            listen,
            na_init_info,
            progress_pool);
    if(!mona) goto error;

    if(use_progress_es == NA_TRUE) {
        mona->owns_progress_pool = NA_TRUE;
        mona->owns_progress_xstream = NA_TRUE;
    }

    mona->progress_xstream = xstream;

finish:
    return mona;

error:
    if(progress_pool != ABT_POOL_NULL && use_progress_es == NA_TRUE)
        ABT_pool_free(&progress_pool);
    if(xstream != ABT_XSTREAM_NULL && use_progress_es == NA_TRUE)
        ABT_xstream_free(&xstream);
    mona = MONA_INSTANCE_NULL;
    goto finish;
}

mona_instance_t mona_init_pool(
        const char *info_string,
        na_bool_t listen,
        const struct na_init_info *na_init_info,
        ABT_pool progress_pool)
{
    na_class_t* na_class = NULL;
    na_context_t* na_context = NULL;
    mona_instance_t mona = MONA_INSTANCE_NULL;

    na_class = NA_Initialize_opt(info_string, listen, na_init_info);
    if(!na_class) goto error;

    na_context = NA_Context_create(na_class);
    if(!na_context) goto error;

    mona = mona_init_na_pool(na_class, na_context, progress_pool);
    if(!mona) goto error;

    mona->owns_na_class_and_context = NA_TRUE;

finish:
    return mona;

error:
    if(na_context) NA_Context_destroy(na_class, na_context);
    if(na_class) NA_Finalize(na_class);
    mona = MONA_INSTANCE_NULL;
    goto finish;
}

mona_instance_t mona_init_na_pool(
        na_class_t *na_class,
        na_context_t *na_context,
        ABT_pool progress_pool)
{
144
    int ret, i;
145
146
    mona_instance_t mona = (mona_instance_t)calloc(1, sizeof(*mona));
    if(!mona) return MONA_INSTANCE_NULL;
Matthieu Dorier's avatar
Matthieu Dorier committed
147
148
149
    mona->na_class         = na_class;
    mona->na_context       = na_context;
    mona->progress_pool    = progress_pool;
150
    mona->progress_xstream = ABT_XSTREAM_NULL;
Matthieu Dorier's avatar
Matthieu Dorier committed
151
152
153
    mona->progress_thread  = ABT_THREAD_NULL;
    mona->op_id_cache_mtx  = ABT_MUTEX_NULL;
    mona->req_cache_mtx    = ABT_MUTEX_NULL;
154
    mona->msg_cache_mtx    = ABT_MUTEX_NULL;
155
156
    mona->pending_msg_mtx  = ABT_MUTEX_NULL;
    mona->pending_msg_cv   = ABT_COND_NULL;
157
158
    ret = ABT_mutex_create(&(mona->op_id_cache_mtx));
    if(ret != ABT_SUCCESS) goto error;
Matthieu Dorier's avatar
Matthieu Dorier committed
159
160
    ret = ABT_mutex_create(&(mona->req_cache_mtx));
    if(ret != ABT_SUCCESS) goto error;
161
162
    ret = ABT_mutex_create(&(mona->msg_cache_mtx));
    if(ret != ABT_SUCCESS) goto error;
163
164
165
166
    ret = ABT_mutex_create(&(mona->pending_msg_mtx));
    if(ret != ABT_SUCCESS) goto error;
    ret = ABT_cond_create(&(mona->pending_msg_cv));
    if(ret != ABT_SUCCESS) goto error;
167
168
169
170
171
172
173
174
175
176

    mona->op_id_cache = (cached_op_id_t)calloc(1, sizeof(*(mona->op_id_cache)));
    mona->op_id_cache->op_id = NA_Op_create(na_class);

    cached_op_id_t current = mona->op_id_cache;
    for(i=0; i < 15; i++) {
        current->next = (cached_op_id_t)calloc(1, sizeof(*current));
        current = current->next;
        current->op_id = NA_Op_create(na_class);
    }
177
178
179
180
181
182
183
184
185

    ret = ABT_thread_create(mona->progress_pool, mona_progress_loop, 
            (void*)mona, ABT_THREAD_ATTR_NULL, &(mona->progress_thread));
    if(ret != ABT_SUCCESS) goto error;

finish:
    return mona;

error:
186
187
    if(mona->op_id_cache_mtx != ABT_MUTEX_NULL)
        ABT_mutex_free(&(mona->op_id_cache_mtx));
Matthieu Dorier's avatar
Matthieu Dorier committed
188
189
    if(mona->req_cache_mtx != ABT_MUTEX_NULL)
        ABT_mutex_free(&(mona->req_cache_mtx));
190
191
192
193
194
195
    if(mona->msg_cache_mtx != ABT_MUTEX_NULL)
        ABT_mutex_free(&(mona->msg_cache_mtx));
    if(mona->pending_msg_mtx != ABT_MUTEX_NULL)
        ABT_mutex_free(&(mona->pending_msg_mtx));
    if(mona->pending_msg_cv != ABT_COND_NULL)
        ABT_cond_free(&(mona->pending_msg_cv));
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    free(mona);
    mona = MONA_INSTANCE_NULL;
    goto finish;
}

na_return_t mona_finalize(mona_instance_t mona)
{
    mona->finalize_flag = NA_TRUE;
    ABT_thread_join(mona->progress_thread);

    if(mona->owns_progress_xstream) {
        ABT_xstream_join(mona->progress_xstream);
        ABT_xstream_free(&(mona->progress_xstream));
    }
    if(mona->owns_progress_pool)
        ABT_pool_free(&(mona->progress_pool));
212

213
    clear_op_id_cache(mona);
214
215
    ABT_mutex_free(&(mona->op_id_cache_mtx));

216
    clear_req_cache(mona);
Matthieu Dorier's avatar
Matthieu Dorier committed
217
218
    ABT_mutex_free(&(mona->req_cache_mtx));

219
220
221
    clear_msg_cache(mona);
    ABT_mutex_free(&(mona->msg_cache_mtx));

222
223
224
    ABT_mutex_free(&(mona->pending_msg_mtx));
    ABT_cond_free(&(mona->pending_msg_cv));

225
226
227
228
229
230
231
232
233
234
235
    if(mona->owns_na_class_and_context) {
        NA_Context_destroy(
                mona->na_class,
                mona->na_context);
        NA_Finalize(mona->na_class);
    }
    free(mona);

    return NA_SUCCESS;
}

236
237
238
239
// ------------------------------------------------------------------------------------
// Mona info access logic
// ------------------------------------------------------------------------------------

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const char* mona_get_class_name(mona_instance_t mona)
{
    return NA_Get_class_name(mona->na_class);
}

const char* mona_get_class_protocol(mona_instance_t mona)
{
    return NA_Get_class_protocol(mona->na_class);
}

na_bool_t mona_is_listening(mona_instance_t mona)
{
    return NA_Is_listening(mona->na_class);
}

255
256
257
258
// ------------------------------------------------------------------------------------
// Mona addresses logic
// ------------------------------------------------------------------------------------

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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
na_return_t mona_addr_lookup(
        mona_instance_t mona,
        const char *name,
        na_addr_t *addr)
{
    return NA_Addr_lookup(mona->na_class, name, addr);
}

na_return_t mona_addr_free(
        mona_instance_t mona,
        na_addr_t addr)
{
    return NA_Addr_free(mona->na_class, addr);
}

na_return_t mona_addr_set_remove(
        mona_instance_t mona,
        na_addr_t addr)
{
    return NA_Addr_set_remove(mona->na_class, addr);
}

na_return_t mona_addr_self(
        mona_instance_t mona,
        na_addr_t* addr)
{
    return NA_Addr_self(mona->na_class, addr);
}

na_return_t mona_addr_dup(
        mona_instance_t mona,
        na_addr_t addr,
        na_addr_t* dup_addr)
{
    return NA_Addr_dup(mona->na_class, addr, dup_addr);
}

na_bool_t mona_addr_cmp(
        mona_instance_t mona,
        na_addr_t addr1,
        na_addr_t addr2)
{
301
302
303
304
305
306
307
308
309
310
    if(addr1 == NA_ADDR_NULL && addr2 == NA_ADDR_NULL)
        return NA_TRUE;
    if(addr1 == NA_ADDR_NULL || addr2 == NA_ADDR_NULL)
        return NA_FALSE;
    char str1[256], str2[256];
    na_size_t s = 256;
    mona_addr_to_string(mona, str1, &s, addr1);
    s = 256;
    mona_addr_to_string(mona, str2, &s, addr2);
    return strcmp(str1, str2) == 0 ? NA_TRUE : NA_FALSE;
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
}

na_bool_t mona_addr_is_self(
        mona_instance_t mona,
        na_addr_t addr)
{
    return NA_Addr_is_self(mona->na_class, addr);
}

na_return_t mona_addr_to_string(
        mona_instance_t mona,
        char *buf,
        na_size_t *buf_size,
        na_addr_t addr)
{
    return NA_Addr_to_string(mona->na_class, buf, buf_size, addr);
}

na_size_t mona_addr_get_serialize_size(
        mona_instance_t mona,
        na_addr_t addr)
{
    return NA_Addr_get_serialize_size(mona->na_class, addr);
}

na_return_t mona_addr_serialize(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size,
        na_addr_t addr)
{
    return NA_Addr_serialize(mona->na_class, buf, buf_size, addr);
}

na_return_t mona_addr_deserialize(
        mona_instance_t mona,
        na_addr_t *addr,
        const void *buf,
        na_size_t buf_size)
{
    return NA_Addr_deserialize(mona->na_class, addr, buf, buf_size);
}

354
355
356
357
// ------------------------------------------------------------------------------------
// Mona message information logic
// ------------------------------------------------------------------------------------

358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
na_size_t mona_msg_get_max_unexpected_size(
        mona_instance_t mona)
{
    return NA_Msg_get_max_unexpected_size(mona->na_class);
}

na_size_t mona_msg_get_max_expected_size(
        mona_instance_t mona)
{
    return NA_Msg_get_max_expected_size(mona->na_class);
}

na_size_t mona_msg_get_unexpected_header_size(
        mona_instance_t mona)
{
    return NA_Msg_get_unexpected_header_size(mona->na_class);
}

na_size_t mona_msg_get_expected_header_size(
        mona_instance_t mona)
{
    return NA_Msg_get_expected_header_size(mona->na_class);
}

na_tag_t mona_msg_get_max_tag(mona_instance_t mona)
{
    return NA_Msg_get_max_tag(mona->na_class);
}

387
388
389
390
// ------------------------------------------------------------------------------------
// Mona operation logic
// ------------------------------------------------------------------------------------

391
392
393
394
395
396
397
398
399
400
401
402
na_op_id_t mona_op_create(mona_instance_t mona)
{
    return NA_Op_create(mona->na_class);
}

na_return_t mona_op_destroy(
        mona_instance_t mona,
        na_op_id_t op_id)
{
    return NA_Op_destroy(mona->na_class, op_id);
}

403
404
405
// ------------------------------------------------------------------------------------
// Mona message buffer logic
// ------------------------------------------------------------------------------------
Matthieu Dorier's avatar
Matthieu Dorier committed
406

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
void* mona_msg_buf_alloc(
        mona_instance_t mona,
        na_size_t buf_size,
        void **plugin_data)
{
    return NA_Msg_buf_alloc(mona->na_class, buf_size, plugin_data);
}

na_return_t mona_msg_buf_free(
        mona_instance_t mona,
        void *buf,
        void *plugin_data)
{
    return NA_Msg_buf_free(mona->na_class, buf, plugin_data);
}

na_return_t mona_msg_init_unexpected(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size)
{
    return NA_Msg_init_unexpected(mona->na_class, buf, buf_size);
}

431
432
433
// ------------------------------------------------------------------------------------
// Mona request logic
// ------------------------------------------------------------------------------------
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

na_return_t mona_wait(mona_request_t req)
{
    na_return_t na_ret = mona_wait_internal(req);
    free(req);
    return na_ret;
}

int mona_test(mona_request_t req, int* flag)
{
    return ABT_eventual_test(req->eventual, NULL, flag);
}

static int mona_callback(const struct na_cb_info *info)
{
    na_return_t na_ret = info->ret;
    mona_request_t req = (mona_request_t)(info->arg);
451
452

    if(na_ret == NA_SUCCESS && info->type == NA_CB_RECV_UNEXPECTED) {
453
454
455
456
457
458
459
460
461
462
463
464
465
        na_addr_t source = info->info.recv_unexpected.source;
        na_tag_t tag     = info->info.recv_unexpected.tag;
        na_size_t size   = info->info.recv_unexpected.actual_buf_size;
        if(req->source_addr) {
            mona_addr_dup(req->mona, source, req->source_addr);
        }
        if(req->tag) {
            *(req->tag) = tag;
        }
        if(req->size) {
            *(req->size) = size;
        }
    }
466
467
468
469
    ABT_eventual_set(req->eventual, &na_ret, sizeof(na_ret));
    return NA_SUCCESS;
}

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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// ------------------------------------------------------------------------------------
// Mona high-level send/recv logic
// ------------------------------------------------------------------------------------

na_return_t mona_send(
        mona_instance_t mona,
        const void *buf,
        na_size_t buf_size,
        na_addr_t dest,
        na_uint8_t dest_id,
        na_tag_t tag)
{
    na_return_t na_ret         = NA_SUCCESS;
    na_mem_handle_t mem_handle = NA_MEM_HANDLE_NULL;
    na_size_t msg_size         = mona_msg_get_unexpected_header_size(mona) + 1 + buf_size;
    cached_msg_t msg           = get_msg_from_cache(mona);

    if(msg_size <= mona_msg_get_max_unexpected_size(mona)) {

        na_ret = mona_msg_init_unexpected(mona, msg->buffer, msg_size);
        if(na_ret != NA_SUCCESS) goto finish;

        char* p = msg->buffer + mona_msg_get_unexpected_header_size(mona);
        *p = HL_MSG_SMALL;
        p += 1;
        memcpy(p, buf, buf_size);

        na_ret = mona_msg_send_unexpected(
                mona, msg->buffer, msg_size,
                msg->plugin_data, dest,
                dest_id, tag);

    } else {

        // Expose user memory for RDMA
        na_ret = mona_mem_handle_create(mona, (void*)buf, buf_size, NA_MEM_READ_ONLY, &mem_handle);
        if(na_ret != NA_SUCCESS) goto finish;

        na_ret = mona_mem_register(mona, mem_handle);
        if(na_ret != NA_SUCCESS) goto finish;

        na_size_t mem_handle_size = mona_mem_handle_get_serialize_size(mona, mem_handle);

        // Initialize message to send
        msg_size = mona_msg_get_unexpected_header_size(mona) // NA header
                 + 1                                         // type of message (HL_MSG_*)
                 + sizeof(na_size_t)                         // size of the serialize handle
                 + sizeof(na_size_t)                         // size of the data
                 + mem_handle_size;

        na_ret = mona_msg_init_unexpected(mona, msg->buffer, msg_size);
        if(na_ret != NA_SUCCESS) {
            mona_mem_deregister(mona, mem_handle);
            goto finish;
        }

        // Fill in the message
        char* p = msg->buffer + mona_msg_get_unexpected_header_size(mona);
        *p = HL_MSG_LARGE;
        p += 1;
        memcpy(p, &mem_handle_size, sizeof(mem_handle_size));
        p += sizeof(mem_handle_size);
        memcpy(p, &buf_size, sizeof(buf_size));
        p += sizeof(buf_size);
        na_ret = mona_mem_handle_serialize(mona, p, mem_handle_size, mem_handle);
        if(na_ret != NA_SUCCESS) {
            mona_mem_deregister(mona, mem_handle);
            goto finish;
        }

        // Initialize ack message to receive
        cached_msg_t   ack_msg      = get_msg_from_cache(mona);
        na_size_t      ack_msg_size = mona_msg_get_unexpected_header_size(mona) + 1;
        mona_request_t ack_req      = MONA_REQUEST_NULL;
        cached_op_id_t ack_cache_id = get_op_id_from_cache(mona);
        na_op_id_t     ack_op_id    = ack_cache_id->op_id;

        // Issue non-blocking receive for ACK
        na_ret = mona_msg_irecv_expected(mona, ack_msg->buffer, ack_msg_size,
                ack_msg->plugin_data, dest, dest_id, tag, &ack_op_id, &ack_req);
        if(na_ret != NA_SUCCESS) {
            mona_mem_deregister(mona, mem_handle);
            return_op_id_to_cache(mona, ack_cache_id);
            goto finish;
        }

        // Issue send of message with mem handle
        na_ret = mona_msg_send_unexpected(
                mona, msg->buffer, msg_size,
                msg->plugin_data, dest,
                dest_id, tag);
        if(na_ret != NA_SUCCESS) {
            mona_mem_deregister(mona, mem_handle);
            mona_cancel(mona, ack_op_id);
            return_op_id_to_cache(mona, ack_cache_id);
            goto finish;
        }

        // Wait for acknowledgement
        na_ret = mona_wait(ack_req);
        return_op_id_to_cache(mona, ack_cache_id);
    }

finish:
    if(mem_handle != NA_MEM_HANDLE_NULL)
        mona_mem_handle_free(mona, mem_handle);
    return_msg_to_cache(mona, msg);
    return na_ret;
}

580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
struct isend_args {
    mona_instance_t mona;
    const void*     buf;
    na_size_t       buf_size;
    na_addr_t       dest;
    na_uint8_t      dest_id;
    na_tag_t        tag;
    mona_request_t  req;
};

static void isend_thread(void* x)
{
    struct isend_args* args = (struct isend_args*)x;
    na_return_t na_ret = mona_send(
        args->mona,
        args->buf,
        args->buf_size,
        args->dest,
        args->dest_id,
        args->tag);
    ABT_eventual_set(args->req->eventual, &na_ret, sizeof(na_ret));
    free(args);
}

604
605
606
607
608
609
610
611
612
na_return_t mona_isend(
        mona_instance_t mona,
        const void *buf,
        na_size_t buf_size,
        na_addr_t dest,
        na_uint8_t dest_id,
        na_tag_t tag,
        mona_request_t* req)
{
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
    ABT_eventual eventual;
    int ret = ABT_eventual_create(sizeof(na_return_t), &eventual);
    if(ret != 0)
        return NA_NOMEM;

    struct isend_args* args = (struct isend_args*)malloc(sizeof(*args));
    args->mona     = mona;
    args->buf      = buf;
    args->buf_size = buf_size;
    args->dest     = dest;
    args->dest_id  = dest_id;
    args->tag      = tag;

    mona_request_t tmp_req = get_req_from_cache(mona);
    tmp_req->eventual = eventual;
    args->req = tmp_req;

    ret = ABT_thread_create(mona->progress_pool, isend_thread, args, ABT_THREAD_ATTR_NULL, NULL);
    if(ret != ABT_SUCCESS) {
        return_req_to_cache(mona, tmp_req);
        return NA_NOMEM;
    } else {
        *req = tmp_req;
        ABT_thread_yield();
    }
    return NA_SUCCESS;
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
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
static cached_msg_t wait_for_matching_unexpected_message(
        mona_instance_t mona,
        na_addr_t src,
        na_tag_t tag,
        na_size_t* actual_size,
        na_addr_t* actual_src,
        na_tag_t* actual_tag)
{
    cached_msg_t msg       = NULL; /* result */
    na_size_t    msg_size  = mona_msg_get_max_unexpected_size(mona);
    na_return_t  na_ret    = NA_SUCCESS;

    // lock the queue of pending messages
    ABT_mutex_lock(mona->pending_msg_mtx);

    // search in the queue of pending messages for one matching
search_in_queue:
    {
        pending_msg_t p_msg = mona->pending_msg_oldest;
        pending_msg_t p_prev_msg = NULL;
        while(p_msg) {
            if((tag == MONA_ANY_TAG || p_msg->recv_tag == tag)
            && (src == MONA_ANY_SOURCE || mona_addr_cmp(mona, src, p_msg->recv_addr))) {
                break;
            } else {
                p_prev_msg = p_msg;
                p_msg = p_msg->cached_msg->next;
            }
        }
        if(p_msg) { // matching message was found
            msg = p_msg->cached_msg;
            // remove it from the queue of pending messages
            if(p_prev_msg) p_prev_msg->cached_msg->next = p_msg->cached_msg->next;
            if(p_msg == mona->pending_msg_oldest)
                mona->pending_msg_oldest = p_msg->cached_msg->next;
            if(p_msg == mona->pending_msg_newest)
                mona->pending_msg_newest = p_prev_msg;
            // unlock the queue
            ABT_mutex_unlock(mona->pending_msg_mtx);
            // copy size, source, and tag
            if(actual_size) *actual_size = p_msg->recv_size;
            if(actual_src) mona_addr_dup(mona, p_msg->recv_addr, actual_src);
            if(actual_tag) *actual_tag = p_msg->recv_tag;
            // free the pending message object
            mona_addr_free(mona, p_msg->recv_addr);
            free(p_msg);
            // return the message
            return msg;
        }
    }
    // here the matching message wasn't found in the queue
    {
        // if another thread is actively issuing unexpected recv, wait for the queue to update
        if(mona->pending_msg_queue_active) {
            ABT_cond_wait(mona->pending_msg_cv, mona->pending_msg_mtx);
            if(mona->pending_msg_queue_active)
                goto search_in_queue;
        }
    }
    // here no matching message was found and there isn't any other threads updating the queue
    // so this thread will take the responsibility for actively listening for messages
    mona->pending_msg_queue_active = NA_TRUE;
    ABT_mutex_unlock(mona->pending_msg_mtx);
recv_new_message:
    {
        na_size_t recv_size = 0;
        na_addr_t recv_addr = NA_ADDR_NULL;
        na_tag_t  recv_tag  = 0;
        // get message from cache
        msg = get_msg_from_cache(mona);
        // issue unexpected recv
        na_ret = mona_msg_recv_unexpected(
            mona, msg->buffer, msg_size, msg->plugin_data,
            &recv_addr, &recv_tag, &recv_size);
        if(na_ret != NA_SUCCESS)
            goto error;
        // check is received message is matching
        if((tag == MONA_ANY_TAG || recv_tag == tag)
        && (src == MONA_ANY_SOURCE || mona_addr_cmp(mona, src, recv_addr))) {
            // received message matches
            // notify other threads that this thread won't be updating the queue anymore
            ABT_mutex_lock(mona->pending_msg_mtx);
            mona->pending_msg_queue_active = NA_FALSE;
            ABT_mutex_unlock(mona->pending_msg_mtx);
            ABT_cond_broadcast(mona->pending_msg_cv);
            // copy size, source, and tag
            if(actual_size) *actual_size = recv_size;
            if(actual_src) *actual_src = recv_addr;
            else mona_addr_free(mona, recv_addr);
            if(actual_tag) *actual_tag = recv_tag;
            // return the message
            return msg;

        } else {
            // received message doesn't match, create a pending message...
            pending_msg_t p_msg = (pending_msg_t)malloc(sizeof(*p_msg));
            p_msg->cached_msg   = msg;
            p_msg->recv_size    = recv_size;
            p_msg->recv_addr    = recv_addr;
            p_msg->recv_tag     = recv_tag;
            msg->next           = NULL;
            // ... and put it in the queue
            ABT_mutex_lock(mona->pending_msg_mtx);
            if(mona->pending_msg_oldest == NULL) {
                mona->pending_msg_oldest = p_msg;
                mona->pending_msg_newest = p_msg;
            } else {
                mona->pending_msg_newest->cached_msg->next = p_msg;
                mona->pending_msg_newest = p_msg;
            }
            // notify other threads that the queue has been updated
            ABT_mutex_unlock(mona->pending_msg_mtx);
            ABT_cond_broadcast(mona->pending_msg_cv);
            goto recv_new_message;
        }
    }
    // error handling
error:
    if(msg) return_msg_to_cache(mona, msg);
    ABT_mutex_unlock(mona->pending_msg_mtx);
    ABT_cond_broadcast(mona->pending_msg_cv);
    return NULL;
}

765
766
767
na_return_t mona_recv(
        mona_instance_t mona,
        void* buf,
768
        na_size_t size,
769
770
        na_addr_t src,
        na_tag_t tag,
771
772
773
        na_size_t* actual_size,
        na_addr_t* actual_src,
        na_tag_t* actual_tag)
774
{
775

776
777
778
    na_return_t na_ret            = NA_SUCCESS;
    na_mem_handle_t mem_handle    = NA_MEM_HANDLE_NULL;
    na_mem_handle_t remote_handle = NA_MEM_HANDLE_NULL;
779
780
781
782
783
784
785
786
787
788
789
790
791
792
    na_size_t header_size         = mona_msg_get_unexpected_header_size(mona);
    cached_msg_t msg              = NULL;
    na_size_t recv_size           = 0;
    na_addr_t recv_addr           = NA_ADDR_NULL;
    na_tag_t  recv_tag            = 0;

    // wait for a matching unexpected message to come around
    msg = wait_for_matching_unexpected_message(mona, src, tag, &recv_size, &recv_addr, &recv_tag);
    if(!msg) return NA_PROTOCOL_ERROR;

    // At this point, we know msg is the message we are looking for
    // and the attributes are recv_size, recv_tag, and recv_addr

    char* p = msg->buffer + header_size;
793
794
795
796

    if(*p == HL_MSG_SMALL) { // small message, embedded data
        
        p += 1;
797
798
799
800
        recv_size -= header_size + 1;
        recv_size = recv_size < size ? recv_size : size;
        if(recv_size)
            memcpy(buf, p, recv_size);
801

802
    } else if(*p == HL_MSG_LARGE) { // large message, using RDMA transfer
803
804
805
806

        p += 1;
        na_size_t mem_handle_size;
        na_size_t data_size;
807
        // read the size of the serialize mem handle
808
809
        memcpy(&mem_handle_size, p, sizeof(mem_handle_size));
        p += sizeof(mem_handle_size);
810
        // read the size of the data associated with the mem handle
811
812
813
        memcpy(&data_size, p, sizeof(data_size));
        p += sizeof(data_size);

814
815
816
        // expose user memory for RDMA
        na_ret = mona_mem_handle_create(
                mona, (void*)buf, size, NA_MEM_WRITE_ONLY, &mem_handle);
817
818
819
820
821
        if(na_ret != NA_SUCCESS) goto finish;

        na_ret = mona_mem_register(mona, mem_handle);
        if(na_ret != NA_SUCCESS) goto finish;

822
        // Deserialize remote memory handle
823
824
825
826
827
828
        na_ret = mona_mem_handle_deserialize(
                mona, &remote_handle, p, mem_handle_size);
        if(na_ret != NA_SUCCESS) goto finish;

        // Issue RDMA operation
        // XXX how do we support a source id different from 0 ?
829
830
831
832
833
        data_size = data_size < size ? data_size : size;
        if(data_size) {
            na_ret = mona_get(mona, mem_handle, 0, remote_handle, 0, data_size, recv_addr, 0);
            if(na_ret != NA_SUCCESS) goto finish;
        }
834
835

        // Send ACK
836
        na_size_t msg_size = header_size + 1;
837
838
839
840
841
        msg->buffer[msg_size-1] = 0;
        na_ret = mona_msg_init_expected(mona, msg->buffer, msg_size);
        if(na_ret != NA_SUCCESS) goto finish;

        // XXX how do we support a source id different from 0 ?
842
843
        na_ret = mona_msg_send_expected(mona, msg->buffer, msg_size,
                msg->plugin_data, recv_addr, 0, recv_tag);
844
845
846
        if(na_ret != NA_SUCCESS) goto finish;
    }

847
848
849
850
851
852
853
854
855
856
    if(actual_size)
        *actual_size = recv_size;
    if(actual_tag)
        *actual_tag = recv_tag;
    if(actual_src)
        *actual_src = recv_addr;
    else
        mona_addr_free(mona, recv_addr);
    recv_addr = NA_ADDR_NULL;

857
finish:
858
859
    if(recv_addr != NA_ADDR_NULL)
        mona_addr_free(mona, recv_addr);
860
861
862
863
864
865
866
867
    if(mem_handle != NA_MEM_HANDLE_NULL)
        mona_mem_handle_free(mona, mem_handle);
    if(remote_handle != NA_MEM_HANDLE_NULL)
        mona_mem_handle_free(mona, remote_handle);
    return_msg_to_cache(mona, msg);
    return na_ret;
}

868
869
870
struct irecv_args {
    mona_instance_t mona;
    void*           buf;
871
    na_size_t       size;
872
873
874
    na_addr_t       src;
    na_tag_t        tag;
    na_size_t*      actual_size;
875
876
    na_addr_t*      actual_src;
    na_tag_t*       actual_tag;
877
878
879
880
881
882
883
884
885
    mona_request_t  req;
};

static void irecv_thread(void* x)
{
    struct irecv_args* args = (struct irecv_args*)x;
    na_return_t na_ret = mona_recv(
        args->mona,
        args->buf,
886
        args->size,
887
888
        args->src,
        args->tag,
889
890
891
        args->actual_size,
        args->actual_src,
        args->actual_tag);
892
893
894
895
    ABT_eventual_set(args->req->eventual, &na_ret, sizeof(na_ret));
    free(args);
}

896
897
898
na_return_t mona_irecv(
        mona_instance_t mona,
        void* buf,
899
        na_size_t size,
900
901
902
        na_addr_t src,
        na_tag_t tag,
        na_size_t* actual_size,
903
904
        na_addr_t* actual_src,
        na_tag_t* actual_tag,
905
906
        mona_request_t* req)
{
907
908
909
910
911
912
913
914
    ABT_eventual eventual;
    int ret = ABT_eventual_create(sizeof(na_return_t), &eventual);
    if(ret != 0)
        return NA_NOMEM;

    struct irecv_args* args = (struct irecv_args*)malloc(sizeof(*args));
    args->mona        = mona;
    args->buf         = buf;
915
    args->size        = size;
916
917
    args->src         = src;
    args->actual_size = actual_size;
918
919
    args->actual_src  = actual_src;
    args->actual_tag  = actual_tag;
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
    args->tag         = tag;

    mona_request_t tmp_req = get_req_from_cache(mona);
    args->req = tmp_req;
    tmp_req->eventual = eventual;

    ret = ABT_thread_create(mona->progress_pool, irecv_thread, args, ABT_THREAD_ATTR_NULL, NULL);
    if(ret != ABT_SUCCESS) {
        return_req_to_cache(mona, tmp_req);
        return NA_NOMEM;
    } else {
        *req = tmp_req;
        ABT_thread_yield();
    }
    return NA_SUCCESS;
935
936
937
938
939
940
}

// ------------------------------------------------------------------------------------
// Mona low-level unexpected send/recv logic
// ------------------------------------------------------------------------------------

941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
static na_return_t mona_msg_isend_unexpected_internal(
        mona_instance_t mona,
        const void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t dest_addr,
        na_uint8_t dest_id,
        na_tag_t tag,
        na_op_id_t *op_id,
        mona_request_t req)
{
    int ret;
    na_return_t na_ret;
    ABT_eventual eventual = ABT_EVENTUAL_NULL;

    ret = ABT_eventual_create(sizeof(na_ret), &eventual);
    if(ret != 0)
        return NA_NOMEM;
959

960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
    req->eventual = eventual;
    return NA_Msg_send_unexpected(
            mona->na_class, mona->na_context,
            mona_callback, (void*)req,
            buf, buf_size, plugin_data,
            dest_addr, dest_id, tag, op_id);
}

na_return_t mona_msg_send_unexpected(
        mona_instance_t mona,
        const void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t dest_addr,
        na_uint8_t dest_id,
975
        na_tag_t tag)
976
{
977
978
    cached_op_id_t id = get_op_id_from_cache(mona);
    na_op_id_t op_id = id->op_id;
979
    mona_request req = MONA_REQUEST_INITIALIZER;
980
    na_return_t na_ret = mona_msg_isend_unexpected_internal(
981
982
983
984
985
986
            mona, buf, buf_size, plugin_data, dest_addr, dest_id, tag, &op_id, &req);
    if(na_ret != NA_SUCCESS) goto finish;
    na_ret = mona_wait_internal(&req);
finish:
    return_op_id_to_cache(mona, id);
    return na_ret;
987
988
989
990
991
992
993
994
995
996
997
998
999
}

na_return_t mona_msg_isend_unexpected(
        mona_instance_t mona,
        const void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t dest_addr,
        na_uint8_t dest_id,
        na_tag_t tag,
        na_op_id_t *op_id,
        mona_request_t* req)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
1000
    mona_request_t tmp_req = get_req_from_cache(mona);
1001
1002
    na_return_t na_ret = mona_msg_isend_unexpected_internal(
            mona, buf, buf_size, plugin_data, dest_addr, dest_id, tag, op_id, tmp_req);
Matthieu Dorier's avatar
Matthieu Dorier committed
1003
1004
1005
    if(na_ret != NA_SUCCESS) {
        return_req_to_cache(mona, tmp_req);
    } else {
1006
        *req = tmp_req;
Matthieu Dorier's avatar
Matthieu Dorier committed
1007
    }
1008
1009
1010
1011
1012
1013
1014
1015
    return na_ret;
}

static na_return_t mona_msg_irecv_unexpected_internal(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size,
        void *plugin_data,
1016
1017
1018
        na_addr_t* source_addr,
        na_tag_t* tag,
        na_size_t* size,
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
        na_op_id_t *op_id,
        mona_request_t req)
{
    int ret;
    na_return_t na_ret;
    ABT_eventual eventual = ABT_EVENTUAL_NULL;

    ret = ABT_eventual_create(sizeof(na_ret), &eventual);
    if(ret != 0)
        return NA_NOMEM;
1029

1030
1031
1032
1033
1034
1035
    req->eventual    = eventual;
    req->mona        = mona;
    req->source_addr = source_addr;
    req->tag         = tag;
    req->size        = size;

1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
    return NA_Msg_recv_unexpected(
            mona->na_class, mona->na_context,
            mona_callback, (void*)req,
            buf, buf_size, plugin_data,
            op_id);
}

na_return_t mona_msg_recv_unexpected(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size,
1047
1048
1049
1050
        void *plugin_data,
        na_addr_t* source_addr,
        na_tag_t* tag,
        na_size_t* size)
1051
{
1052
    mona_request req = MONA_REQUEST_INITIALIZER;
1053
1054
    cached_op_id_t id = get_op_id_from_cache(mona);
    na_op_id_t op_id = id->op_id;
1055
    na_return_t na_ret = mona_msg_irecv_unexpected_internal(
1056
1057
            mona, buf, buf_size, plugin_data,
            source_addr, tag, size, &op_id, &req);
1058
1059
1060
1061
1062
    if(na_ret != NA_SUCCESS) goto finish;
    na_ret = mona_wait_internal(&req);
finish:
    return_op_id_to_cache(mona, id);
    return na_ret;
1063
1064
1065
1066
1067
1068
1069
}

na_return_t mona_msg_irecv_unexpected(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size,
        void *plugin_data,
1070
1071
1072
        na_addr_t* source_addr,
        na_tag_t* tag,
        na_size_t* size,
1073
1074
1075
        na_op_id_t *op_id,
        mona_request_t* req)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
1076
    mona_request_t tmp_req = get_req_from_cache(mona);
1077
    na_return_t na_ret = mona_msg_irecv_unexpected_internal(
1078
1079
            mona, buf, buf_size, plugin_data, 
            source_addr, tag, size, op_id, tmp_req);
Matthieu Dorier's avatar
Matthieu Dorier committed
1080
1081
1082
    if(na_ret != NA_SUCCESS) {
        return_req_to_cache(mona, tmp_req);
    } else {
1083
        *req = tmp_req;
Matthieu Dorier's avatar
Matthieu Dorier committed
1084
    }
1085
1086
1087
    return na_ret;
}

1088
1089
1090
1091
// ------------------------------------------------------------------------------------
// Mona low-level expected send/recv logic
// ------------------------------------------------------------------------------------

1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
na_return_t mona_msg_init_expected(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size)
{
    return NA_Msg_init_expected(mona->na_class, buf, buf_size);
}

static na_return_t mona_msg_isend_expected_internal(
        mona_instance_t mona,
        const void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t dest_addr,
        na_uint8_t dest_id,
        na_tag_t tag,
        na_op_id_t *op_id,
        mona_request_t req)
{
    int ret;
    na_return_t na_ret;
    ABT_eventual eventual = ABT_EVENTUAL_NULL;

    ret = ABT_eventual_create(sizeof(na_ret), &eventual);
    if(ret != 0)
        return NA_NOMEM;
1118

1119
1120
1121
1122
1123
1124
    req->eventual    = eventual;
    req->mona        = mona;
    req->source_addr = NULL;
    req->tag         = NULL;
    req->size        = NULL;

1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
    return NA_Msg_send_expected(
            mona->na_class, mona->na_context,
            mona_callback, (void*)req,
            buf, buf_size, plugin_data,
            dest_addr, dest_id, tag, op_id);
}

na_return_t mona_msg_send_expected(
        mona_instance_t mona,
        const void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t dest_addr,
        na_uint8_t dest_id,
1139
        na_tag_t tag)
1140
{
1141
    mona_request req = MONA_REQUEST_INITIALIZER;
1142
1143
    cached_op_id_t id = get_op_id_from_cache(mona);
    na_op_id_t op_id = id->op_id;
1144
    na_return_t na_ret = mona_msg_isend_expected_internal(
1145
1146
1147
1148
1149
1150
            mona, buf, buf_size, plugin_data, dest_addr, dest_id, tag, &op_id, &req);
    if(na_ret != NA_SUCCESS) goto finish;
    na_ret = mona_wait_internal(&req);
finish:
    return_op_id_to_cache(mona, id);
    return na_ret;
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
}

na_return_t mona_msg_isend_expected(
        mona_instance_t mona,
        const void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t dest_addr,
        na_uint8_t dest_id,
        na_tag_t tag,
        na_op_id_t *op_id,
        mona_request_t* req)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
1164
    mona_request_t tmp_req = get_req_from_cache(mona);
1165
1166
    na_return_t na_ret = mona_msg_isend_expected_internal(
            mona, buf, buf_size, plugin_data, dest_addr, dest_id, tag, op_id, tmp_req);
Matthieu Dorier's avatar
Matthieu Dorier committed
1167
1168
1169
    if(na_ret != NA_SUCCESS) {
        return_req_to_cache(mona, tmp_req);
    } else {
1170
        *req = tmp_req;
Matthieu Dorier's avatar
Matthieu Dorier committed
1171
    }
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
    return na_ret;
}

static na_return_t mona_msg_irecv_expected_internal(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t source_addr,
        na_uint8_t source_id,
        na_tag_t tag,
        na_op_id_t *op_id,
        mona_request_t req)
{
    int ret;
    na_return_t na_ret;
    ABT_eventual eventual = ABT_EVENTUAL_NULL;

    ret = ABT_eventual_create(sizeof(na_ret), &eventual);
    if(ret != 0)
        return NA_NOMEM;
1193

1194
1195
1196
1197
1198
1199
    req->eventual    = eventual;
    req->mona        = mona;
    req->source_addr = NULL;
    req->tag         = NULL;
    req->size        = NULL;

1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
    return NA_Msg_recv_expected(
            mona->na_class, mona->na_context,
            mona_callback, (void*)req,
            buf, buf_size, plugin_data,
            source_addr, source_id, tag, op_id);
}

na_return_t mona_msg_recv_expected(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t source_addr,
        na_uint8_t source_id,
1214
        na_tag_t tag)
1215
{
1216
    mona_request req = MONA_REQUEST_INITIALIZER;
1217
1218
    cached_op_id_t id = get_op_id_from_cache(mona);
    na_op_id_t op_id = id->op_id;
1219
    na_return_t na_ret = mona_msg_irecv_expected_internal(
1220
1221
1222
1223
1224
1225
            mona, buf, buf_size, plugin_data, source_addr, source_id, tag, &op_id, &req);
    if(na_ret != NA_SUCCESS) goto finish;
    na_ret = mona_wait_internal(&req);
finish:
    return_op_id_to_cache(mona, id);
    return na_ret;
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
}

na_return_t mona_msg_irecv_expected(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size,
        void *plugin_data,
        na_addr_t source_addr,
        na_uint8_t source_id,
        na_tag_t tag,
        na_op_id_t *op_id,
        mona_request_t* req)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
1239
    mona_request_t tmp_req = get_req_from_cache(mona); 
1240
1241
    na_return_t na_ret = mona_msg_irecv_expected_internal(
            mona, buf, buf_size, plugin_data, source_addr, source_id, tag, op_id, tmp_req);
Matthieu Dorier's avatar
Matthieu Dorier committed
1242
1243
1244
    if(na_ret != NA_SUCCESS) {
        return_req_to_cache(mona, tmp_req);
    } else {
1245
        *req = tmp_req;
Matthieu Dorier's avatar
Matthieu Dorier committed
1246
    }
1247
1248
1249
    return na_ret;
}

1250
1251
1252
1253
// ------------------------------------------------------------------------------------
// Mona RDMA logic
// ------------------------------------------------------------------------------------

1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
na_return_t mona_mem_handle_create(
        mona_instance_t mona,
        void *buf,
        na_size_t buf_size,
        unsigned long flags,
        na_mem_handle_t *mem_handle)
{
    return NA_Mem_handle_create(
            mona->na_class,
            buf, buf_size, flags,
            mem_handle);
}

na_return_t mona_mem_handle_create_segments(
        mona_instance_t mona,
        struct na_segment *segments,
        na_size_t segment_count,
        unsigned long flags,
        na_mem_handle_t *mem_handle)
{
    return NA_Mem_handle_create_segments(
            mona->na_class,
            segments, segment_count, flags,
            mem_handle);
}

na_return_t mona_mem_handle_free(
        mona_instance_t mona,
        na_mem_handle_t mem_handle)
{
    return NA_Mem_handle_free(mona->na_class, mem_handle);
}

na_return_t mona_mem_register(
        mona_instance_t mona,
        na_mem_handle_t mem_handle)
{
    return NA_Mem_register(mona->na_class, mem_handle);
}

na_return_t mona_mem_deregister(
        mona_instance_t mona,
        na_mem_handle_t mem_handle)
{
    return NA_Mem_deregister(mona->na_class, mem_handle);
}

na_return_t mona_mem_publish(
        mona_instance_t mona,
        na_mem_handle_t mem_handle)
{
    return NA_Mem_publish(mona->na_class, mem_handle);
}

na_return_t mona_mem_unpublish(
        mona_instance_t mona,
        na_mem_handle_t mem_handle)
{
    return NA_Mem_unpublish(mona->na_class, mem_handle);
}

na_size_t mona_mem_handle_get_serialize_size(
        mona_instance_t mona,
        na_mem_handle_t mem_handle)
{
    return NA_Mem_handle_get_serialize_size(
            mona->na_class, mem_handle);
}

na_return_t mona_mem_handle_serialize(
        mona_instance_t mona,
        void *buf, na_size_t buf_size,
        na_mem_handle_t mem_handle)
{
    return NA_Mem_handle_serialize(
            mona->na_class,
            buf, buf_size,
            mem_handle);
}

na_return_t mona_mem_handle_deserialize(
        mona_instance_t mona,
        na_mem_handle_t *mem_handle,
        const void *buf,
        na_size_t buf_size)
{
    return NA_Mem_handle_deserialize(
            mona->na_class,
            mem_handle, buf, buf_size);
}

static na_return_t mona_iput_internal(
        mona_instance_t mona,
        na_mem_handle_t local_mem_handle,
        na_offset_t local_offset,
        na_mem_handle_t remote_mem_handle,
        na_offset_t remote_offset,
        na_size_t data_size,
        na_addr_t remote_addr,
        na_uint8_t remote_id,
        na_op_id_t *op_id,
        mona_request_t req)
{
    int ret;
    na_return_t na_ret;
    ABT_eventual eventual = ABT_EVENTUAL_NULL;

    ret = ABT_eventual_create(sizeof(na_ret), &eventual);
    if(ret != 0)
        return NA_NOMEM;
1364

1365
1366
1367
1368
1369
1370
    req->eventual    = eventual;
    req->mona        = mona;
    req->source_addr = NULL;
    req->tag         = NULL;
    req->size        = NULL;

1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
    return NA_Put(mona->na_class, mona->na_context,
            mona_callback, (void*)req,
            local_mem_handle, local_offset,
            remote_mem_handle, remote_offset,
            data_size, remote_addr,
            remote_id, op_id);
}

na_return_t mona_put(
        mona_instance_t mona,
        na_mem_handle_t local_mem_handle,
        na_offset_t local_offset,
        na_mem_handle_t remote_mem_handle,
        na_offset_t remote_offset,
        na_size_t data_size,
        na_addr_t remote_addr,
1387
        na_uint8_t remote_id)
1388
{
1389
    mona_request req = MONA_REQUEST_INITIALIZER;
1390
1391
    cached_op_id_t id = get_op_id_from_cache(mona);
    na_op_id_t op_id = id->op_id;
1392
1393
1394
    na_return_t na_ret = mona_iput_internal(
            mona, local_mem_handle, local_offset,
            remote_mem_handle, remote_offset,
1395
1396
1397
1398
1399
1400
            data_size, remote_addr, remote_id, &op_id, &req);
    if(na_ret != NA_SUCCESS) goto finish;
    na_ret = mona_wait_internal(&req);
finish:
    return_op_id_to_cache(mona, id);
    return na_ret;
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
}

na_return_t mona_iput(
        mona_instance_t mona,
        na_mem_handle_t local_mem_handle,
        na_offset_t local_offset,
        na_mem_handle_t remote_mem_handle,
        na_offset_t remote_offset,
        na_size_t data_size,
        na_addr_t remote_addr,
        na_uint8_t remote_id,
        na_op_id_t *op_id,
        mona_request_t* req)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
1415
    mona_request_t tmp_req = get_req_from_cache(mona);
1416
1417
1418
1419
1420
    tmp_req->eventual = ABT_EVENTUAL_NULL;
    na_return_t na_ret = mona_iput_internal(
            mona, local_mem_handle, local_offset,
            remote_mem_handle, remote_offset,
            data_size, remote_addr, remote_id, op_id, tmp_req);
Matthieu Dorier's avatar
Matthieu Dorier committed
1421
1422
1423
    if(na_ret != NA_SUCCESS) {
        return_req_to_cache(mona, tmp_req);
    } else {
1424
        *req = tmp_req;
Matthieu Dorier's avatar
Matthieu Dorier committed
1425
    }
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
    return na_ret;
}

static na_return_t mona_iget_internal(
        mona_instance_t mona,
        na_mem_handle_t local_mem_handle,
        na_offset_t local_offset,
        na_mem_handle_t remote_mem_handle,
        na_offset_t remote_offset,
        na_size_t data_size,
        na_addr_t remote_addr,
        na_uint8_t remote_id,
        na_op_id_t *op_id,
        mona_request_t req)
{
    int ret;
    na_return_t na_ret;
    ABT_eventual eventual = ABT_EVENTUAL_NULL;

    ret = ABT_eventual_create(sizeof(na_ret), &eventual);
    if(ret != 0)
        return NA_NOMEM;
1448

1449
1450
1451
1452
1453
1454
    req->eventual    = eventual;
    req->mona        = mona;
    req->source_addr = NULL;
    req->tag         = NULL;
    req->size        = NULL;

1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
    return NA_Get(mona->na_class, mona->na_context,
            mona_callback, (void*)req,
            local_mem_handle, local_offset,
            remote_mem_handle, remote_offset,
            data_size, remote_addr,
            remote_id, op_id);
}

na_return_t mona_get(
        mona_instance_t mona,
        na_mem_handle_t local_mem_handle,
        na_offset_t local_offset,
        na_mem_handle_t remote_mem_handle,
        na_offset_t remote_offset,
        na_size_t data_size,
        na_addr_t remote_addr,
1471
        na_uint8_t remote_id)
1472
{
1473
    mona_request req = MONA_REQUEST_INITIALIZER;
1474
1475
    cached_op_id_t id = get_op_id_from_cache(mona);
    na_op_id_t op_id = id->op_id;
1476
1477
1478
1479
1480
    na_return_t na_ret = mona_iget_internal(
            mona, local_mem_handle,
            local_offset, remote_mem_handle,
            remote_offset, data_size,
            remote_addr, remote_id,
1481
1482
1483
1484
1485
1486
            &op_id, &req);
    if(na_ret != NA_SUCCESS) goto finish;
    na_ret = mona_wait_internal(&req);
finish:
    return_op_id_to_cache(mona, id);
    return na_ret;
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
}

na_return_t mona_iget(
        mona_instance_t mona,
        na_mem_handle_t local_mem_handle,
        na_offset_t local_offset,
        na_mem_handle_t remote_mem_handle,
        na_offset_t remote_offset,
        na_size_t data_size,
        na_addr_t remote_addr,
        na_uint8_t remote_id,
        na_op_id_t *op_id,
        mona_request_t* req)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
1501
    mona_request_t tmp_req = get_req_from_cache(mona);
1502
1503
1504
1505
1506
1507
1508
    tmp_req->eventual = ABT_EVENTUAL_NULL;
    na_return_t na_ret = mona_iget_internal(
            mona, local_mem_handle,
            local_offset, remote_mem_handle,
            remote_offset, data_size,
            remote_addr, remote_id,
            op_id, tmp_req);
Matthieu Dorier's avatar
Matthieu Dorier committed
1509
1510
1511
    if(na_ret != NA_SUCCESS) {
        return_req_to_cache(mona, tmp_req);
    } else {
1512
        *req = tmp_req;
Matthieu Dorier's avatar
Matthieu Dorier committed
1513
    }
1514
1515
1516
    return na_ret;
}

1517
1518
1519
1520
// ------------------------------------------------------------------------------------
// Other functions 
// ------------------------------------------------------------------------------------

1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
int mona_poll_get_fd(mona_instance_t mona)
{
    return NA_Poll_get_fd(mona->na_class, mona->na_context);
}

na_bool_t mona_poll_try_wait(mona_instance_t mona)
{
    return NA_Poll_try_wait(mona->na_class, mona->na_context);
}

na_return_t mona_cancel(
        mona_instance_t mona,
        na_op_id_t op_id)
{
    return NA_Cancel(mona->na_class, mona->na_context, op_id);
}

const char* mona_error_to_string(int errnum)
{
    return NA_Error_to_string((na_return_t)errnum);
}