dcfa_poll.c 83.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
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
271
272
273
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/*
 *  (C) 2012 NEC Corporation
 *      Author: Masamichi Takagi
 *      See COPYRIGHT in top-level directory.
 */

#include "dcfa_impl.h"

//#define DEBUG_DCFA_POLL
#ifdef dprintf /* avoid redefinition with src/mpid/ch3/include/mpidimpl.h */
#undef dprintf
#endif
#ifdef DEBUG_DCFA_POLL
#define dprintf printf
#else
#define dprintf(...)
#endif

static int entered_drain_scq = 0;

#if 0
#define MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET MPID_nem_dcfa_send_progress(vc_dcfa);
#else
#define MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET {     \
   int i; \
   for(i = 0; i < MPID_nem_dcfa_npollingset; i++) { \
       MPIDI_VC_t *vc = MPID_nem_dcfa_pollingset[i]; \
       MPID_nem_dcfa_vc_area *_vc_dcfa = VC_DCFA(vc); \
       /*MPID_nem_dcfa_debug_current_vc_dcfa = vc_dcfa;*/   \
       MPID_nem_dcfa_send_progress(_vc_dcfa); \
   } \
}
#endif
#if 1
#define MPID_NEM_DCFA_CHECK_AND_SEND_PROGRESS \
    if(!MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq) && MPID_nem_dcfa_sendq_ready_to_send_head(vc_dcfa)) { \
    MPID_nem_dcfa_send_progress(vc_dcfa); \
}
#else
#define MPID_NEM_DCFA_CHECK_AND_SEND_PROGRESS MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET
#endif

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_drain_scq
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_drain_scq(int dont_call_progress) {

    int mpi_errno = MPI_SUCCESS;
    int result;
    int i;
    struct ibv_wc cqe[IBCOM_MAX_CQ_HEIGHT_DRAIN];

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ);

    /* prevent a call path drain_scq -> send_progress -> drain_scq */
    if(entered_drain_scq) { printf("drain_scq,re-enter\n"); goto fn_exit; }
    entered_drain_scq = 1;

#if 0 /*def DCFA*/
    result = ibv_poll_cq(rc_shared_scq, 1, &cqe[0]);
#else
    result = ibv_poll_cq(rc_shared_scq, /*3*/IBCOM_MAX_CQ_HEIGHT_DRAIN, &cqe[0]);
#endif

    MPIU_ERR_CHKANDJUMP(result < 0, mpi_errno, MPI_ERR_OTHER, "**netmod,dcfa,ibv_poll_cq");
    
    if(result > 0) {
        dprintf("poll,scq,result=%d\n", result); 
    }
    for(i = 0; i < result; i++)  {
        dprintf("drain_scq,i=%d\n", i);
        
        MPID_Request *req;
        MPID_Request_kind_t kind;
        int req_type, msg_type;
        
        /* Obtain sreq */
        req = (MPID_Request*)cqe[i].wr_id;

        kind = req->kind;
        req_type = MPIDI_Request_get_type(req);
        msg_type = MPIDI_Request_get_msg_type(req);

        dprintf("drain_scq,req=%p,req->ref_count=%d,cc_ptr=%d\n", req, req->ref_count, *req->cc_ptr);
        if(req->ref_count <= 0) {
            MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(req->ch.vc);
            printf("%d\n", *(int*)0); 
        }

#ifdef DCFA
        if(cqe[i].status != IBV_WC_SUCCESS) { 
            dprintf("drain_scq,kind=%d,req_type=%d,msg_type=%d,cqe.status=%08x\n", kind, req_type, msg_type, cqe[i].status); 
        }
#else
        if(cqe[i].status != IBV_WC_SUCCESS) { 
            dprintf("drain_scq,kind=%d,req_type=%d,msg_type=%d,comm=%p,cqe.status=%08x,%s\n", kind, req_type, msg_type, req->comm, cqe[i].status, ibv_wc_status_str(cqe[i].status)); 
        }
#endif
        MPIU_ERR_CHKANDJUMP(cqe[i].status != IBV_WC_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_drain_scq");

        /* 
           packets generated by MPIDI_CH3_iStartMsgv has req_type of RECV
           lmt_initiate_lmt, lmt_put_cts_to_sender, lmt_put_rts_to_receiver, lmt_send_put_done
        */
        if(
           //req_type == MPIDI_REQUEST_TYPE_SEND 
           (req_type == MPIDI_REQUEST_TYPE_SEND ||
            req_type == MPIDI_REQUEST_TYPE_RECV ||
            req_type == MPIDI_REQUEST_TYPE_SSEND)
           && msg_type == MPIDI_REQUEST_EAGER_MSG) {
            dprintf("drain_scq,send/recv,eager,req_type=%d,,comm=%p,opcode=%d\n", req_type, req->comm, cqe[i].opcode);
            
            MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(req->ch.vc);
            dprintf("drain_scq,MPIDI_REQUEST_EAGER_MSG,%d->%d,sendq_empty=%d,ncom=%d,ncqe=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_myrank, req->ch.vc->pg_rank, MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_ncqe, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail)); /* moved before MPID_Request_release because this references req->ch.vc */

            /* free temporal buffer for eager-send non-contiguous data.
               MPIDI_Request_create_sreq (in mpid_isend.c) sets req->dev.datatype 
               control message has a req_type of MPIDI_REQUEST_TYPE_RECV and 
               msg_type of MPIDI_REQUEST_EAGER_MSG because
               control message send follows 
               MPIDI_CH3_iStartMsg/v-->MPID_nem_dcfa_iStartContigMsg-->MPID_nem_dcfa_iSendContig
               and MPID_nem_dcfa_iSendContig set req->dev.state to zero.
               see MPID_Request_create (in src/mpid/ch3/src/ch3u_request.c) 
               eager-short message has req->comm of zero 
            */
            if(req_type == MPIDI_REQUEST_TYPE_SEND && req->comm) {
                /* exclude control messages by requiring MPIDI_REQUEST_TYPE_SEND
                   exclude eager-short by requiring req->comm != 0 */
                int is_contig;
                MPID_Datatype_is_contig(req->dev.datatype, &is_contig);
                if(!is_contig && REQ_FIELD(req, lmt_pack_buf)) {
                    dprintf("drain_scq,eager-send,non-contiguous,free lmt_pack_buf=%p\n", REQ_FIELD(req, lmt_pack_buf));
                    MPIU_Free(REQ_FIELD(req, lmt_pack_buf));       
                }
            }

            /* decrement the number of entries in IB command queue */
            vc_dcfa->ibcom->ncom -= 1;
            MPID_nem_dcfa_ncqe -= 1;
            dprintf("drain_scq,eager-send,ncqe=%d\n", MPID_nem_dcfa_ncqe);
            MPIU_Assert(req->ref_count == 1 || req->ref_count == 2);

            /* ref_count is decremented in drain_scq and wait */
            if (*req->cc_ptr > 0) {
                dprintf("drain_scq,MPID_nem_dcfa_ncqe_nces=%d,cc_ptr=%d,pending_sends=%d\n", MPID_nem_dcfa_ncqe_nces, *req->cc_ptr, VC_FIELD(req->ch.vc, pending_sends));
                MPID_nem_dcfa_ncqe_nces -= 1;

            int (*reqFn)(MPIDI_VC_t *, MPID_Request *, int *);
            
            (VC_FIELD(req->ch.vc, pending_sends)) -= 1;
            
            /* as in the template */
            reqFn = req->dev.OnDataAvail;
            if (!reqFn){
                MPIDI_CH3U_Request_complete(req);
                MPIU_DBG_MSG(CH3_CHANNEL, VERBOSE, ".... complete");
                dprintf("drain_scq,complete,req=%p,pcc incremented to %d\n", req, MPIDI_CH3I_progress_completion_count.v);
            } else {
                dprintf("drain_scq,reqFn isn't zero\n");
                MPIDI_VC_t *vc = req->ch.vc;
                int complete = 0;
                mpi_errno = reqFn(vc, req, &complete);
                if (mpi_errno) MPIU_ERR_POP(mpi_errno);
                /* not-completed case is not implemented */
                MPIU_Assert(complete == TRUE);
            }
            } else {
            MPID_Request_release(req); 
            }
            /* try to send from sendq */
            //dprintf("dcfa_poll,SCQ,!lmt,send_progress\n");
            if(!MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq)) {
                dprintf("drain_scq,eager-send,ncom=%d,ncqe=%d,diff=%d\n", vc_dcfa->ibcom->ncom < IBCOM_MAX_SQ_CAPACITY, MPID_nem_dcfa_ncqe < IBCOM_MAX_CQ_CAPACITY, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail) < IBCOM_RDMABUF_NSEG);
                
                MPID_Request *sreq = MPID_nem_dcfa_sendq_head(vc_dcfa->sendq);
                int msg_type = MPIDI_Request_get_msg_type(sreq);
                
                if(sreq->kind == MPID_REQUEST_SEND && msg_type == MPIDI_REQUEST_EAGER_MSG) {
                    dprintf("drain_scq,eager-send,head is eager-send\n");
                } else if(sreq->kind == MPID_REQUEST_RECV && msg_type == MPIDI_REQUEST_RNDV_MSG) {
                    dprintf("drain_scq,eager-send,head is lmt RDMA-read\n");
                } else if(sreq->kind == MPID_REQUEST_SEND && msg_type == MPIDI_REQUEST_RNDV_MSG) {
                    dprintf("drain_scq,eager-send,head is lmt RDMA-write\n");
                }
            }
            /*  call MPID_nem_dcfa_send_progress for all VCs in polling-set
                instead of VC which releases CQ, command
                when releasing them
                because commands for VC-A are blocked by the command
                for VC-B and waiting in the sendq
            */
                dprintf("drain_scq,eager-send,send_progress\n");
                //MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET;

            dprintf("drain_scq,eager-send,next\n");

        } else if(req_type == MPIDI_REQUEST_TYPE_GET_RESP && msg_type == MPIDI_REQUEST_EAGER_MSG) {
            dprintf("drain_scq,GET_RESP,eager,req_type=%d,,comm=%p,opcode=%d\n", req_type, req->comm, cqe[i].opcode);
            
            MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(req->ch.vc);
            dprintf("drain_scq,MPIDI_REQUEST_EAGER_MSG,%d->%d,sendq_empty=%d,ncom=%d,ncqe=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_myrank, req->ch.vc->pg_rank, MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_ncqe, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail)); /* moved before MPID_Request_release because this references req->ch.vc */

            /* decrement the number of entries in IB command queue */
            vc_dcfa->ibcom->ncom -= 1;
            MPID_nem_dcfa_ncqe -= 1;
            dprintf("drain_scq,GET_RESP,ncqe=%d\n", MPID_nem_dcfa_ncqe);
            MPIU_Assert(req->ref_count == 1 || req->ref_count == 2);

            /* ref_count is decremented in drain_scq and wait */
            dprintf("drain_scq,MPID_nem_dcfa_ncqe_nces=%d,cc_ptr=%d,pending_sends=%d\n", MPID_nem_dcfa_ncqe_nces, *req->cc_ptr, VC_FIELD(req->ch.vc, pending_sends));
            MPID_nem_dcfa_ncqe_nces -= 1;

            int (*reqFn)(MPIDI_VC_t *, MPID_Request *, int *);
            
            (VC_FIELD(req->ch.vc, pending_sends)) -= 1;
            
            /* as in the template */
            reqFn = req->dev.OnDataAvail;
            if (!reqFn){
                MPIDI_CH3U_Request_complete(req);
                MPIU_DBG_MSG(CH3_CHANNEL, VERBOSE, ".... complete");
                dprintf("drain_scq,complete,req=%p,pcc incremented to %d\n", req, MPIDI_CH3I_progress_completion_count.v);
            } else {
                dprintf("drain_scq,reqFn isn't zero\n");
                dprintf("drain_scq,GET_RESP,before dev.OnDataAvail,ref_count=%d\n", req->ref_count);
                MPIDI_VC_t *vc = req->ch.vc;
                int complete = 0;
                mpi_errno = reqFn(vc, req, &complete);
                if (mpi_errno) MPIU_ERR_POP(mpi_errno);
                /* not-completed case is not implemented */
                MPIU_Assert(complete == TRUE);
            }
                
            //MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET;

            dprintf("drain_scq,GET_RESP,next\n");

        } else if(req_type == MPIDI_REQUEST_TYPE_RECV && msg_type == MPIDI_REQUEST_RNDV_MSG && cqe[i].opcode == IBV_WC_RDMA_READ) {
            /* lmt get */
            /* the case for lmt-put-done or lmt-put where 
               (1) sender finds end-flag won't change (2) sender sends RTS to receiver
               (3) receiver gets (4) here 
               is distinguished by cqe[i].opcode
            */
            dprintf("drain_scq,recv,rndv,rdma-read,kind=%d,opcode=%d\n", kind, cqe[i].opcode);


           MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(req->ch.vc);
#if defined(LMT_GET_CQE)

           /* unpack non-contiguous dt */
           int is_contig;
           MPID_Datatype_is_contig(req->dev.datatype, &is_contig);
           if(!is_contig) {
               dprintf("drain_scq,lmt,GET_CQE,unpack noncontiguous data to user buffer\n");

               /* see MPIDI_CH3U_Request_unpack_uebuf (in /src/mpid/ch3/src/ch3u_request.c) */
               /* or MPIDI_CH3U_Receive_data_found (in src/mpid/ch3/src/ch3u_handle_recv_pkt.c) */
               MPIDI_msg_sz_t unpack_sz = req->ch.lmt_data_sz;
               MPID_Segment seg;
               MPI_Aint last;

               MPID_Segment_init(req->dev.user_buf, req->dev.user_count, req->dev.datatype, &seg, 0);
               last = unpack_sz;
               MPID_Segment_unpack(&seg, 0, &last, REQ_FIELD(req, lmt_pack_buf));
               if (last != unpack_sz) {
                   /* --BEGIN ERROR HANDLING-- */
                   /* received data was not entirely consumed by unpack() 
                      because too few bytes remained to fill the next basic
                      datatype */
274
                   MPIR_STATUS_SET_COUNT(req->status, last);
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
301
302
303
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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
431
432
433
434
435
436
437
438
439
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
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
580
581
582
583
584
585
586
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
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
                   req->status.MPI_ERROR = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_TYPE, "**MPID_nem_dcfa_poll", 0);
                   /* --END ERROR HANDLING-- */
               }
               dprintf("drain_scq,lmt,GET_CQE,ref_count=%d,lmt_pack_buf=%p\n", req->ref_count, REQ_FIELD(req, lmt_pack_buf));
               MPID_nem_dcfa_stfree(REQ_FIELD(req, lmt_pack_buf), (size_t)req->ch.lmt_data_sz);
           }
           dprintf("drain_scq,lmt,GET_CQE,lmt_send_GET_DONE,rsr_seq_num_tail=%d\n", vc_dcfa->ibcom->rsr_seq_num_tail);

           /* send done to sender. vc is stashed in MPID_nem_dcfa_lmt_start_recv (in dcfa_lmt.c) */
           MPID_nem_dcfa_lmt_send_GET_DONE(req->ch.vc, req);
#endif
            /* unmark "lmt is going on" */
            
            //dprintf("dcfa_poll,SCQ,lmt,%d->%d,sendq_empty=%d,ncom=%d,ncqe=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_myrank, req->ch.vc->pg_rank, MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_ncqe, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail)); /* moved before MPID_Request_release because this references req->ch.vc */

            /* decrement the number of entries in IB command queue */
            vc_dcfa->ibcom->ncom -= 1;
            MPID_nem_dcfa_ncqe -= 1;
            dprintf("drain_scq,rdma-read,ncqe=%d\n", MPID_nem_dcfa_ncqe);

#ifdef LMT_GET_CQE
            dprintf("drain_scq,GET_CQE,Request_complete\n");
            /* mark completion on rreq */
            MPIDI_CH3U_Request_complete(req);
#else /* GET, and !GET_CQE */

            int is_contig;
            MPID_Datatype_is_contig(req->dev.datatype, &is_contig);
            if(!is_contig) {
                //if(req->ref_count == 1) {
                dprintf("drain_scq,GET&&!GET_CQE,ref_count=%d,lmt_pack_buf=%p\n", req->ref_count, REQ_FIELD(req, lmt_pack_buf));
                    /* debug, polling waits forever when freeing here. */
                    //free(REQ_FIELD(req, lmt_pack_buf));
                    //MPID_nem_dcfa_stfree(REQ_FIELD(req, lmt_pack_buf), (size_t)req->ch.lmt_data_sz);
                    //dprintf("drain_scq,lmt,insert to free-list=%p\n", MPID_nem_dcfa_fl);
                    //} else {
                    //dprintf("drain_scq,GET&&!GET_CQE,ref_count=%d,lmt_pack_buf=%p\n", req->ref_count, REQ_FIELD(req, lmt_pack_buf));
                    //}
            }

           /* lmt_start_recv increments ref_count 
              drain_scq and dcfa_poll is not ordered, so both can decrement ref_count */
            MPID_Request_release(req);
#endif
            /* try to send from sendq */
            if(!MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq)) {
                dprintf("drain_scq,GET,ncom=%d,ncqe=%d,diff=%d\n", vc_dcfa->ibcom->ncom < IBCOM_MAX_SQ_CAPACITY, MPID_nem_dcfa_ncqe < IBCOM_MAX_CQ_CAPACITY, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail) < IBCOM_RDMABUF_NSEG);
                MPID_Request *sreq = MPID_nem_dcfa_sendq_head(vc_dcfa->sendq);
                int msg_type = MPIDI_Request_get_msg_type(sreq);
                
                if(sreq->kind == MPID_REQUEST_SEND && msg_type == MPIDI_REQUEST_EAGER_MSG) {
                    dprintf("drain_scq,eager-send,head is eager-send\n");
                } else if(sreq->kind == MPID_REQUEST_RECV && msg_type == MPIDI_REQUEST_RNDV_MSG) {
                    dprintf("drain_scq,eager-send,head is lmt\n");
                }
            }
            //if(!MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq) && MPID_nem_dcfa_sendq_ready_to_send_head(vc_dcfa)) {
                dprintf("drain_scq,GET,send_progress\n");fflush(stdout);
                //MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET 
                    //}
        } else {
            printf("drain_scq,unknown kind=%d,req_type=%d,msg_type=%d\n", kind, req_type, msg_type);
            assert(0);
#if 1 // lazy consulting of completion queue
            MPIU_ERR_CHKANDJUMP(1, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_drain_scq");
#else
            //printf("kind=%d\n", kind);
#endif
        }
    }
    if(!dont_call_progress) {
        MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET;
    }
 fn_exit:
    entered_drain_scq = 0;
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ);
    return mpi_errno;
 fn_fail:
   goto fn_exit;   
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_drain_scq_lmt_put
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_drain_scq_lmt_put() {

    int mpi_errno = MPI_SUCCESS;
    int result;
    int i;
    struct ibv_wc cqe[IBCOM_MAX_CQ_HEIGHT_DRAIN];

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ_LMT_PUT);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ_LMT_PUT);

#if 0 /*def DCFA*/
    result = ibv_poll_cq(rc_shared_scq_lmt_put, 1, &cqe[0]);
#else
    result = ibv_poll_cq(rc_shared_scq_lmt_put, IBCOM_MAX_CQ_HEIGHT_DRAIN, &cqe[0]);
#endif
    MPIU_ERR_CHKANDJUMP(result < 0, mpi_errno, MPI_ERR_OTHER, "**netmod,dcfa,ibv_poll_cq");
    
    if(result > 0) {
        dprintf("drain_scq_lmt_put,found,result=%d\n", result); 
    }
    for(i = 0; i < result; i++)  {
        
        MPID_Request *req;
        MPID_Request_kind_t kind;
        int req_type, msg_type;
        
#ifdef DCFA
        if(cqe[i].status != IBV_WC_SUCCESS) {
            dprintf("drain_scq_lmt_put,status=%08x\n", cqe[i].status); 
        }
#else
        if(cqe[i].status != IBV_WC_SUCCESS) {
            dprintf("drain_scq_lmt_put,status=%08x,%s\n", cqe[i].status, ibv_wc_status_str(cqe[i].status)); 
        }
#endif
        MPIU_ERR_CHKANDJUMP(cqe[i].status != IBV_WC_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_drain_scq_lmt_put");
        
        /* Obtain sreq */
        req = (MPID_Request*)cqe[i].wr_id;
        dprintf("drain_scq_lmt_put,req=%p,req->ref_count=%d\n", req, req->ref_count);
        MPIU_Assert(req->ref_count > 0);

        kind = req->kind;
        req_type = MPIDI_Request_get_type(req);
        msg_type = MPIDI_Request_get_msg_type(req);
        

        if(req_type == MPIDI_REQUEST_TYPE_RECV && msg_type == MPIDI_REQUEST_RNDV_MSG) {
            /* lmt-put */
            /* MPIDI_Request_set_type is not performed when
               MPID_Isend --> FDU_or_AEP --> recv_posted --> dcfa_poll --> PUTCTS packet-handler */
            
            dprintf("drain_scq_lmt_put,lmt-put found\n");
            
#if 0 /* moving to just after put */ /*implementing back-to-back put and done */
#endif            
            
            /* decrement the number of entries in IB command queue */
            MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(req->ch.vc);
            vc_dcfa->ibcom->ncom_lmt_put -= 1;
            MPID_nem_dcfa_ncqe_lmt_put -= 1;
            dprintf("drain_scq_lmt_put,rndv,ncqe=%d\n", MPID_nem_dcfa_ncqe_lmt_put);/*suspicious*/
            int (*reqFn)(MPIDI_VC_t *, MPID_Request *, int *);
            
            (VC_FIELD(req->ch.vc, pending_sends)) -= 1;
            
            /* as in the template */
            reqFn = req->dev.OnDataAvail;
            if (!reqFn){
                MPIDI_CH3U_Request_complete(req); /* decrement cc, signal_completion, decrement ref_count, free */
                dprintf("drain_scq,lmt-put,req=%p,cc incremented to %d\n", req, MPIDI_CH3I_progress_completion_count.v);
                MPIU_DBG_MSG(CH3_CHANNEL, VERBOSE, ".... complete");
            } else {
                MPIDI_VC_t *vc = req->ch.vc;
                int complete = 0;
                mpi_errno = reqFn(vc, req, &complete);
                if (mpi_errno) MPIU_ERR_POP(mpi_errno);
                /* not-completed case is not implemented */
                MPIU_Assert(complete == TRUE);
                MPIU_Assert(0); /* decrement ref_count and free sreq causes problem */
            }
        } else {
            dprintf("drain_scq_lmt_put,unknown kind=%d,req_type=%d,msg_type=%d\n", kind, req_type, msg_type);
#if 1 // lazy consulting of completion queue
            MPIU_ERR_CHKANDJUMP(1, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_drain_scq_lmt_put");
#else
            //printf("kind=%d\n", kind);
#endif
        }
    }
    
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ_LMT_PUT);
    return mpi_errno;
 fn_fail:
   goto fn_exit;   
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_drain_scq_scratch_pad
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_drain_scq_scratch_pad() {

    int mpi_errno = MPI_SUCCESS;
    int result;
    int i;
    struct ibv_wc cqe[IBCOM_MAX_CQ_HEIGHT_DRAIN];

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ_SCRATCH_PAD);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ_SCRATCH_PAD);

#if 0 /*def DCFA*/
    result = ibv_poll_cq(rc_shared_scq_scratch_pad, 1, &cqe[0]);
#else
    result = ibv_poll_cq(rc_shared_scq_scratch_pad, IBCOM_MAX_CQ_HEIGHT_DRAIN, &cqe[0]);
#endif
    MPIU_ERR_CHKANDJUMP(result < 0, mpi_errno, MPI_ERR_OTHER, "**netmod,dcfa,ibv_poll_cq");
    
    if(result > 0) {
        dprintf("drain_scq_scratch_pad,found,result=%d\n", result); 
    }
    for(i = 0; i < result; i++)  {
        
#ifdef DCFA
        if(cqe[i].status != IBV_WC_SUCCESS) {
            dprintf("drain_scq_scratch_pad,status=%08x\n", cqe[i].status);
        }
#else
        if(cqe[i].status != IBV_WC_SUCCESS) {
            dprintf("drain_scq_scratch_pad,status=%08x,%s\n", cqe[i].status, ibv_wc_status_str(cqe[i].status)); 
        }
#endif
        MPIU_ERR_CHKANDJUMP(cqe[i].status != IBV_WC_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_drain_scq_scratch_pad");
        
        IbCom* ibcom_scratch_pad = (IbCom*)cqe[i].wr_id;
        dprintf("drain_scq_scratch_pad,ibcom_scratch_pad=%p\n", ibcom_scratch_pad);
        ibcom_scratch_pad->ncom_scratch_pad -= 1;
        MPID_nem_dcfa_ncqe_scratch_pad -= 1;
    }
    
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_DRAIN_SCQ_SCRATCH_PAD);
    return mpi_errno;
 fn_fail:
   goto fn_exit;   
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_poll_eager
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_poll_eager(MPIDI_VC_t *vc) {

   int mpi_errno = MPI_SUCCESS;   
   int ibcom_errno;
   int result;
   struct ibv_wc cqe[IBCOM_MAX_CQ_HEIGHT_DRAIN];
   uint64_t tscs, tsce;

   MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_POLL_EAGER);
   MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_POLL_EAGER);

   //MPID_nem_dcfa_tsc_poll = MPID_nem_dcfa_rdtsc();

       MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
       //dprintf("dcfa_poll,ld,rsr_seq_num_poll=%d\n", vc_dcfa->ibcom->rsr_seq_num_poll);
       volatile void* buf = vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO] + IBCOM_RDMABUF_SZSEG * ((uint32_t)vc_dcfa->ibcom->rsr_seq_num_poll % IBCOM_RDMABUF_NSEG);
       volatile sz_hdrmagic_t* sz_hdrmagic = (sz_hdrmagic_t*)buf;
       if(sz_hdrmagic->magic != IBCOM_MAGIC) { goto fn_exit; }
       //dprintf("dcfa_poll_eager,buf=%p,sz=%d\n", buf, sz_hdrmagic->sz);

       /* unmark magic */
       sz_hdrmagic->magic = 0/*0xdead*/; 
#if 0
       ibcom_errno = ibcom_poll_cq(IBCOM_RC_SHARED_RCQ, &cqe, &result);
       MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_poll_cq");
#endif
       dprintf("dcfa_poll_eager,eager-send,found\n");
       
       //MPIU_ERR_CHKANDJUMP1(cqe.status != IBV_WC_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**ibcom_poll_cq", "**ibcom_poll_cq %s", ibcom_strerror(ibcom_errno));
       
       int sz_data_pow2;
       DCFA_NEM_SZ_DATA_POW2(sz_hdrmagic->sz);
       volatile tailmagic_t* tailmagic = (tailmagic_t*)(buf + sz_data_pow2);
       dprintf("poll,sz_data_pow2=%d,tailmagic=%p,sz=%d\n", sz_data_pow2, tailmagic, sz_hdrmagic->sz);
       int k = 0;
       //tsce = MPID_nem_dcfa_rdtsc(); printf("9,%ld\n", tsce - tscs); // 55 for 512-byte
       //tscs = MPID_nem_dcfa_rdtsc();
       //#define TLB_PREF_AMT_AHEAD 20
#ifdef TLB_PREF_AMT_AHEAD
       int tlb_pref_ahd = (uint64_t)tailmagic + 4096 * TLB_PREF_AMT_AHEAD - (uint64_t)buf;
#endif
       while(tailmagic->magic != IBCOM_MAGIC) {
           //k++; 
#if 0 /* pre-fetch next RDMA-write-buf slot to cover TLB miss latency */
           __asm__ __volatile__
               ("movq %0, %%rsi;"
                "movq 0(%%rsi), %%rsi;"
                : : "r"(vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO] + IBCOM_RDMABUF_SZSEG * ((vc_dcfa->ibcom->rsr_seq_num_poll + 1) % IBCOM_RDMABUF_NSEG)) : "%rsi");
#endif
#ifdef TLB_PREF_AMT_AHEAD
           __asm__ __volatile__
               ("movq %0, %%rsi;"
                "movq 0(%%rsi), %%rax;"
                : : "r"(buf + tlb_pref_ahd) : "%rsi", "%rax");
           tlb_pref_ahd = (tlb_pref_ahd + 4096 * 20) % IBCOM_RDMABUF_SZ;
#endif
       }
       //tsce = MPID_nem_dcfa_rdtsc(); printf("0,%ld\n", tsce - tscs); // 20-60 for 512-byte
       //tscs = MPID_nem_dcfa_rdtsc();
       //dprintf("magic wait=%d\n", k);
       

       /* this reduces memcpy in MPIDI_CH3U_Receive_data_found */
       /* MPIDI_CH3_PktHandler_EagerSend (in ch3u_eager.c)
            MPIDI_CH3U_Receive_data_found (in ch3u_handle_recv_pkt.c)
              MPIU_Memcpy((char*)(rreq->dev.user_buf) + dt_true_lb, buf, data_sz);
               600 cycle for 512B!!! --> 284 cycle with prefetch
       */

#if 1
       void* rsi;
       for(rsi = (void*)buf; rsi < buf + sz_hdrmagic->sz; rsi += 64*4) {
#ifdef __MIC__
           __asm__ __volatile__
               (
                "movq %0, %%rsi;"
                "vprefetch0 0x00(%%rsi);"
                "vprefetch0 0x40(%%rsi);"
                "vprefetch0 0x80(%%rsi);"
                "vprefetch0 0xc0(%%rsi);"
                : 
                : "r"(rsi)
                : "%rsi"); 
#else
           __asm__ __volatile__
               (
                "movq %0, %%rsi;"
                "prefetchnta 0x00(%%rsi);"
                "prefetchnta 0x40(%%rsi);"
                "prefetchnta 0x80(%%rsi);"
                "prefetchnta 0xc0(%%rsi);"
                : 
                : "r"(rsi)
                : "%rsi"); 
#endif
       }
#endif

       MPIDI_CH3_Pkt_eager_send_t *pkt = (MPIDI_CH3_Pkt_eager_send_t*)(buf + sizeof(sz_hdrmagic_t));
       MPIU_Assert(sz_hdrmagic->sz >= sizeof(sz_hdrmagic_t) + sizeof(MPIDI_CH3_Pkt_t) + sizeof(tailmagic_t));
       MPIDI_CH3_Pkt_eager_send_t *pkt2 = (MPIDI_CH3_Pkt_eager_send_t*)(buf + sizeof(sz_hdrmagic_t)+sizeof(MPID_nem_dcfa_pkt_prefix_t));
       dprintf("handle_pkt,before,%d<-%d,id=%d,pkt->type=%d,pcc=%d,MPIDI_NEM_PKT_END=%d,pkt=%p,subtype=%d\n", MPID_nem_dcfa_myrank, vc->pg_rank, vc_dcfa->ibcom->rsr_seq_num_poll, pkt->type, MPIDI_CH3I_progress_completion_count.v, MPIDI_NEM_PKT_END, pkt, ((MPID_nem_pkt_netmod_t*)pkt)->subtype);
       /* see MPIDI_CH3_PktHandler_EagerSend (in src/mpid/ch3/src/ch3u_eager.c) */
       mpi_errno = MPID_nem_handle_pkt(vc, (char *)(buf + sizeof(sz_hdrmagic_t)), (MPIDI_msg_sz_t)(sz_hdrmagic->sz - sizeof(sz_hdrmagic_t) - sizeof(tailmagic_t)));
       if(mpi_errno) { MPIU_ERR_POP(mpi_errno); }
       //tsce = MPID_nem_dcfa_rdtsc(); printf("0,%ld\n", tsce - tscs); // 512-byte, 900 cyc (1100 w/o prefetch)
       
       /* Update occupation status of remote SR (send request) queue */
       /* this includes local RDMA-wr-to buf occupation
          because MPID_nem_handle_pkt releases RDMA-wr-to buf by copying data out */
       /* responder releases resource and then embed largest sequence number into MPI message bound to initiator */
       //dprintf("after handle_pkt,rsr_seq_num_tail=%d\n", vc_dcfa->ibcom->rsr_seq_num_tail);
#if 1
       dprintf("handle_pkt,after,%d<-%d,id=%d,pkt->type=%d,eagershort=%d,close=%d,rts=%d,piggy-backed-eagersend=%d\n", MPID_nem_dcfa_myrank, vc->pg_rank, vc_dcfa->ibcom->rsr_seq_num_poll, pkt->type, MPIDI_CH3_PKT_EAGERSHORT_SEND, MPIDI_CH3_PKT_CLOSE, MPIDI_NEM_PKT_LMT_RTS, MPIDI_NEM_DCFA_PKT_EAGER_SEND);

       int notify_rate;
       ibcom_errno = ibcom_rdmabuf_occupancy_notify_rate_get(MPID_nem_dcfa_conns[vc->pg_rank].fd, &notify_rate);
       dprintf("poll_eager,sendq=%d,ncom=%d,ncqe=%d,ldiff=%d(%d-%d),rdiff=%d(%d-%d),rate=%d\n", MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom < IBCOM_MAX_SQ_CAPACITY, MPID_nem_dcfa_ncqe < IBCOM_MAX_CQ_CAPACITY, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail), vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->rsr_seq_num_tail, vc_dcfa->ibcom->rsr_seq_num_tail_last_sent), vc_dcfa->ibcom->rsr_seq_num_tail, vc_dcfa->ibcom->rsr_seq_num_tail_last_sent, notify_rate);

       //dprintf("dcfa_poll,current pcc=%d\n", MPIDI_CH3I_progress_completion_count.v);

       /* Don't forget to put lmt-cookie types here!! */
       if(
          1
          ) 
           { 
           /* lmt cookie messages or control message other than eager-short */
           
           /* eager-send with zero-length data is released here
              because there is no way to trace the RDMA-write-to buffer addr
              because rreq->dev.tmpbuf is set to zero in ch3_eager.c
           */
           dprintf("poll_eager,released,type=%d,MPIDI_NEM_DCFA_REPLY_SEQ_NUM=%d\n", pkt->type, MPIDI_NEM_DCFA_REPLY_SEQ_NUM);
           MPID_nem_dcfa_recv_buf_released(vc, (void*)buf + sizeof(sz_hdrmagic_t) + sizeof(MPIDI_CH3_Pkt_t));
       } else {
           if(sz_hdrmagic->sz == sizeof(sz_hdrmagic_t) + sizeof(MPIDI_CH3_Pkt_t) + sizeof(tailmagic_t)) { 
               if(pkt->type == MPIDI_CH3_PKT_EAGERSHORT_SEND 
                  //||                  pkt->type == MPIDI_CH3_PKT_GET
) {
               } else {
                   printf("dcfa_poll,unknown pkt->type=%d\n", pkt->type);
                   assert(0);
                   MPIU_ERR_INTERNALANDJUMP(mpi_errno, "MPI header only but not released");
               }
           }
       }
#endif

       vc_dcfa->ibcom->rsr_seq_num_poll += 1;
       dprintf("dcfa_poll,inc,rsr_seq_num_poll=%d\n", vc_dcfa->ibcom->rsr_seq_num_poll);

 out:
 fn_exit:
   MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_POLL_EAGER);
   return mpi_errno;
 fn_fail:
   goto fn_exit;   
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_poll
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_poll(int in_blocking_poll) {

   int mpi_errno = MPI_SUCCESS;   
   int ibcom_errno;
   uint32_t i;

   MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_POLL);
   MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_POLL);

#if 1
   unsigned int progress_completion_count_old = MPIDI_CH3I_progress_completion_count.v;
#endif

   /* poll lmt */
   /* when receiver side sends CTS to sender side 
      sender receives CTS and give up sending RTS
      sender initiates RDMA-write,
      sender sends RTS of the next epoch,
      to detect the end of RDMA-write first and DP the entry for CTS,
      you should perform lmt-poll first, next eager-poll
    */
   MPID_Request *rreq, *prev_rreq;
   rreq = MPID_nem_dcfa_lmtq_head(MPID_nem_dcfa_lmtq);
   if(rreq) {
#if defined (TIMER_WAIT_DCFA_POLL)
   if(in_blocking_poll) { tsc[0] = MPI_rdtsc(); }
#endif
   // dprintf("dcfa_poll,poll lmtq\n");
       prev_rreq = NULL;
       do {
           /* Obtain cookie. pkt_RTS_handler memcpy it (in mpid_nem_lmt.c) */
           /* MPID_IOV_BUF is macro, converted into iov_base (in src/include/mpiiov.h) */
           /* do not use s_cookie_buf because do_cts frees it */
           //MPID_nem_dcfa_lmt_cookie_t* s_cookie_buf = (MPID_nem_dcfa_lmt_cookie_t*)rreq->ch.lmt_tmp_cookie.iov_base;
           
           /* Wait for completion of DMA */
           /* do not use s_cookie_buf->sz because do_cts frees it */
           volatile void* write_to_buf;
           int is_contig;
           MPID_Datatype_is_contig(rreq->dev.datatype, &is_contig);
           if(is_contig) {
               write_to_buf = (void*)((char *)rreq->dev.user_buf /*+ REQ_FIELD(req, lmt_dt_true_lb)*/); 
           } else {
               write_to_buf = REQ_FIELD(rreq, lmt_pack_buf);
           }

           //assert(REQ_FIELD(rreq, lmt_dt_true_lb) == 0);
           volatile uint8_t* tailmagic = (uint8_t*)(write_to_buf /*+ REQ_FIELD(rreq, lmt_dt_true_lb)*/ + rreq->ch.lmt_data_sz - sizeof(uint8_t));
           
           uint8_t lmt_tail = REQ_FIELD(rreq, lmt_tail);
           if(*tailmagic != REQ_FIELD(rreq, lmt_tail)) { goto next; }
           dprintf("dcfa_poll,sz=%ld,old tail=%02x,new tail=%02x\n", rreq->ch.lmt_data_sz, REQ_FIELD(rreq, lmt_tail), *tailmagic);
           
           dprintf("dcfa_poll,lmt found,%d<-%d,req=%p,ref_count=%d,is_contig=%d,write_to_buf=%p,lmt_pack_buf=%p,user_buf=%p,tail=%p\n", MPID_nem_dcfa_myrank, rreq->ch.vc->pg_rank, rreq, rreq->ref_count, is_contig, write_to_buf, REQ_FIELD(rreq, lmt_pack_buf), rreq->dev.user_buf, tailmagic);

           /* unpack non-contiguous dt */
           if(!is_contig) {
               dprintf("dcfa_poll,copying noncontiguous data to user buffer\n");

               /* see MPIDI_CH3U_Request_unpack_uebuf (in /src/mpid/ch3/src/ch3u_request.c) */
               /* or MPIDI_CH3U_Receive_data_found (in src/mpid/ch3/src/ch3u_handle_recv_pkt.c) */
               MPIDI_msg_sz_t unpack_sz = rreq->ch.lmt_data_sz;
               MPID_Segment seg;
               MPI_Aint last;

               MPID_Segment_init(rreq->dev.user_buf, rreq->dev.user_count, rreq->dev.datatype, &seg, 0);
               last = unpack_sz;
               MPID_Segment_unpack(&seg, 0, &last, REQ_FIELD(rreq, lmt_pack_buf));
               if (last != unpack_sz) {
                   /* --BEGIN ERROR HANDLING-- */
                   /* received data was not entirely consumed by unpack() 
                      because too few bytes remained to fill the next basic
                      datatype */
748
                   MPIR_STATUS_SET_COUNT(rreq->status, last);
749
750
751
752
753
754
755
756
757
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
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
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
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
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
                   rreq->status.MPI_ERROR = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_TYPE, "**MPID_nem_dcfa_poll", 0);
                   /* --END ERROR HANDLING-- */
               }
#if 1 /* debug, enable again later, polling waits forever when freeing it here. */
               //if(rreq->ref_count == 1) {
               dprintf("dcfa_poll,lmt,ref_count=%d,lmt_pack_buf=%p\n", rreq->ref_count, REQ_FIELD(rreq, lmt_pack_buf));
                   //MPIU_Free(REQ_FIELD(rreq, lmt_pack_buf));
                   MPID_nem_dcfa_stfree(REQ_FIELD(rreq, lmt_pack_buf), (size_t)rreq->ch.lmt_data_sz);    
                   //} else {
                   // dprintf("dcfa_poll,lmt,ref_count=%d,lmt_pack_buf=%p\n", rreq->ref_count, REQ_FIELD(rreq, lmt_pack_buf));
                   //}
#endif
           }

           /* send done to sender. vc is stashed in MPID_nem_dcfa_lmt_start_recv (in dcfa_lmt.c) */
           MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(rreq->ch.vc);
           dprintf("dcfa_poll,GET,lmt_send_GET_DONE,rsr_seq_num_tail=%d\n", vc_dcfa->ibcom->rsr_seq_num_tail);
           MPID_nem_dcfa_lmt_send_GET_DONE(rreq->ch.vc, rreq);
           dprintf("dcfa_poll,prev_rreq=%p,rreq->lmt_next=%p\n", prev_rreq, MPID_nem_dcfa_lmtq_next(rreq));

           /* unlink rreq */
           if(prev_rreq != NULL) {
               MPID_nem_dcfa_lmtq_next(prev_rreq) = MPID_nem_dcfa_lmtq_next(rreq);
           } else {
               MPID_nem_dcfa_lmtq_head(MPID_nem_dcfa_lmtq) = MPID_nem_dcfa_lmtq_next(rreq);
           }
           if(MPID_nem_dcfa_lmtq_next(rreq) == NULL) { MPID_nem_dcfa_lmtq.tail = prev_rreq; }

           /* save rreq->dev.next (and rreq) because decrementing reference-counter might free rreq */
           MPID_Request *tmp_rreq = rreq;
           rreq = MPID_nem_dcfa_lmtq_next(rreq);

           /* decrement completion-counter */
           dprintf("dcfa_poll,%d<-%d,", MPID_nem_dcfa_myrank, tmp_rreq->ch.vc->pg_rank);
           int incomplete;
           MPIDI_CH3U_Request_decrement_cc(tmp_rreq, &incomplete);
           dprintf("lmt,complete,tmp_rreq=%p,rreq->ref_count=%d,comm=%p\n", tmp_rreq, tmp_rreq->ref_count, tmp_rreq->comm);

           if(!incomplete) { MPIDI_CH3_Progress_signal_completion(); }

           /* lmt_start_recv increments ref_count 
              drain_scq and dcfa_poll is not ordered, so both can decrement ref_count */
           /* ref_count is decremented
              get-lmt: dcfa_poll, drain_scq, wait 
              put-lmt: dcfa_poll, wait */
           MPID_Request_release(tmp_rreq); 
           dprintf("dcfa_poll,lmt,after release,tmp_rreq=%p,rreq->ref_count=%d,comm=%p\n", tmp_rreq, tmp_rreq->ref_count, tmp_rreq->comm);

    
           goto next_unlinked;
       next:
           prev_rreq = rreq;
           rreq = MPID_nem_dcfa_lmtq_next(rreq);
       next_unlinked:;
       } while(rreq);
#if defined (TIMER_WAIT_DCFA_POLL)
   if(in_blocking_poll) { stsc[0] += MPI_rdtsc() - tsc[0]; }
#endif
   }

#if defined (TIMER_WAIT_DCFA_POLL)
   if(in_blocking_poll) { tsc[1] = MPI_rdtsc(); }
#endif
   int ncom_almost_full = 0;
   for(i = 0; i < MPID_nem_dcfa_npollingset; i++) {
       //tscs = MPID_nem_dcfa_rdtsc();
       MPIDI_VC_t *vc = MPID_nem_dcfa_pollingset[i];
       mpi_errno = MPID_nem_dcfa_poll_eager(vc);
       if(mpi_errno) { MPIU_ERR_POP (mpi_errno); }

       MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);

       /* without this, command in sendq doesn't have a chance 
          to perform send_progress 
          when send and progress_send call drain_scq but asking it 
          for not performing send_progress and make the CQ empty */
       MPID_nem_dcfa_send_progress(vc_dcfa);

       ncom_almost_full |= (vc_dcfa->ibcom->ncom >= IBCOM_MAX_SQ_HEIGHT_DRAIN);
       
#if 0
       /* aggressively perform drain_scq */
       ncom_almost_full |= !(MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq));
#endif
   }
#if defined (TIMER_WAIT_DCFA_POLL)
   if(in_blocking_poll) { stsc[1] += MPI_rdtsc() - tsc[1]; }
#endif

   // lazy feching of completion queue entry because it causes cache-miss
#if !defined (LMT_PUT_DONE) && defined (LMT_GET_CQE)
   if(MPID_nem_dcfa_ncqe_to_drain > 0 || MPID_nem_dcfa_ncqe_nces > 0 || MPID_nem_dcfa_ncqe >= IBCOM_MAX_CQ_HEIGHT_DRAIN || ncom_almost_full) {
#endif
#if !defined (LMT_PUT_DONE) && !defined (LMT_GET_CQE)
   if(/*(in_blocking_poll && result == 0) ||*/ MPID_nem_dcfa_ncqe_nces > 0 || MPID_nem_dcfa_ncqe >= IBCOM_MAX_CQ_HEIGHT_DRAIN || ncom_almost_full) {

#endif
#if defined (TIMER_WAIT_DCFA_POLL)
   if(in_blocking_poll) { tsc[0] = MPI_rdtsc(); }
#endif
   //dprintf("dcfa_poll,calling drain_scq\n");
       ibcom_errno = MPID_nem_dcfa_drain_scq(0);
       MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_drain_scq");
#if defined (TIMER_WAIT_DCFA_POLL)
   if(in_blocking_poll) { stsc[0] += MPI_rdtsc() - tsc[0]; }
#endif
   }
#if 1
   /* aggressively perform drain_scq */
   ibcom_errno = MPID_nem_dcfa_drain_scq(0);
   MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_drain_scq");
#endif
   /* detect completion of lmt-put when MPI_Wait kicks dcfa_poll */
   if(MPID_nem_dcfa_ncqe_lmt_put > 0) {
       ibcom_errno = MPID_nem_dcfa_drain_scq_lmt_put();
       MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_drain_scq_lmt_put");
   }       

#ifdef DCFA_ONDEMAND
   /* process incoming connection request */
   MPID_nem_dcfa_cm_accept();

   /* process outgoing conncetion request */
   if(MPID_nem_dcfa_ncqe_connect >= IBCOM_MAX_CQ_HEIGHT_DRAIN) {
       ibcom_errno = MPID_nem_dcfa_cm_drain_scq(0);
       MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_cm_drain_scq");
   }       
#endif

#if 1
   /* if polling on eager-send and lmt would repeat frequently, perform "pause" to yield instruction issue bandwitdh to other logical-core */
   if(in_blocking_poll && progress_completion_count_old == MPIDI_CH3I_progress_completion_count.v) {
           __asm__ __volatile__ ("pause;" : : : "memory"); 
   }
#endif
   //if(in_blocking_poll) { goto prev; }

 out:
   fn_exit:
   MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_POLL);
   return mpi_errno;
 fn_fail:
   goto fn_exit;   
}

   /* new rreq is obtained in MPID_Irecv in mpid_irecv.c, 
    so we associate rreq with a receive request and ibv_post_recv it 
    so that we can obtain rreq by ibv_poll_cq
   */
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_recv_posted
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_recv_posted(struct MPIDI_VC *vc, struct MPID_Request *req) {

    int mpi_errno = MPI_SUCCESS;
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_RECV_POSTED);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_RECV_POSTED);
    dprintf("recv_posted,enter,%d->%d,req=%p\n", MPID_nem_dcfa_myrank, vc->pg_rank, req);

#if 0
    int ibcom_errno;
    ibcom_errno = ibcom_irecv(vc_dcfa->sc->fd, (uint64_t)vc->pg_rank);
    MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_irecv");
#endif
    
#if 1 /*takagi*/
    MPIDI_msg_sz_t data_sz;
    int dt_contig;
    MPI_Aint dt_true_lb;
    MPID_Datatype * dt_ptr;
    MPIDI_Datatype_get_info(req->dev.user_count, req->dev.datatype, dt_contig, data_sz, dt_ptr, dt_true_lb);

    /* poll when rreq is for lmt */
    /* anticipating received message finds maching request in the posted-queue */
    if(data_sz + sizeof(MPIDI_CH3_Pkt_eager_send_t) > vc->eager_max_msg_sz) {
        //if(MPID_nem_dcfa_tsc_poll - MPID_nem_dcfa_rdtsc() > MPID_NEM_DCFA_POLL_PERIOD_RECV_POSTED) {
#if 1
        mpi_errno = MPID_nem_dcfa_poll_eager(vc);
#else
            mpi_errno = MPID_nem_dcfa_poll(0);
#endif
            if(mpi_errno) { MPIU_ERR_POP (mpi_errno); }
            //}
    } else {
#if 1
    /* anticipating received message finds maching request in the posted-queue */
    //if(MPID_nem_dcfa_tsc_poll - MPID_nem_dcfa_rdtsc() > MPID_NEM_DCFA_POLL_PERIOD_RECV_POSTED) {
#if 1
        mpi_errno = MPID_nem_dcfa_poll_eager(vc);
#else
        mpi_errno = MPID_nem_dcfa_poll(0);
#endif
        if(mpi_errno) { MPIU_ERR_POP (mpi_errno); }
        //}
#endif
    }
#endif

 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_RECV_POSTED);
    return mpi_errno;
 fn_fail:
   goto fn_exit;   
}

/* (1) packet-handler memcpy RDMA-write-to buf data to MPI user-buffer when matching request is found in posted-queue
   (2) MPI_Irecv memcpy RDMA-write-to buf data to MPI user-buffer when matching request is found in unexpected-queue
   the latter case can't be dealt with when call this after poll-found and packet-handler
   (packet-handler memcpy RDMA-write-to buf to another buffer when
   matching request is not found in posted-queue, so calling this after poll-found and packet-handler
   suffices in original MPICH implementation )
*/
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_recv_buf_released
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_recv_buf_released(struct MPIDI_VC *vc, void* user_data) {
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_RECV_BUF_RELEASED);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_RECV_BUF_RELEASED);
    dprintf("recv_buf_released,%d<-%d,user_data=%p\n", MPID_nem_dcfa_myrank, vc->pg_rank, user_data);
#if 1 /* moving from dcfa_poll */
       /* unmark magic */
       /* magic is located at IBCOM_INLINE_DATA boundary and variable length entails multiple prospective locations for the future use */

    /* see MPIDI_CH3_PktHandler_EagerShortSend (in src/mpid/ch3/src/ch3u_eager.c */
    /* eager-send with zero-length data is released in poll
       because there is no way to trace the RDMA-write-to buffer addr
       because rreq->dev.tmpbuf is set to zero in ch3_eager.c
        */
    if(user_data == NULL) { goto fn_exit; }

    MPIU_Assert(vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO] <= user_data && user_data < vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO] + IBCOM_RDMABUF_SZ);
    unsigned long mod = (unsigned long)(user_data - vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO]) & (IBCOM_RDMABUF_SZSEG-1);

    void* buf = (void*)(user_data - mod);
    //dprintf("recv_buf_released,clearing,buf=%p\n", buf);
    sz_hdrmagic_t* sz_hdrmagic = (sz_hdrmagic_t*)buf;
    
    int sz_data_pow2;
    DCFA_NEM_SZ_DATA_POW2(sz_hdrmagic->sz);
    //dprintf("recv_buf_released,sz=%d,pow2=%d\n", sz_hdrmagic->sz, sz_data_pow2);
#if 1
    uint32_t offset;
    for(offset = 0; ; offset = offset ? ( (((offset + 1) << 1) - 1) > DCFA_NEM_MAX_DATA_POW2 ? DCFA_NEM_MAX_DATA_POW2 : (((offset + 1) << 1) - 1) ) : 15) {
        volatile tailmagic_t* ptr = (tailmagic_t*)(buf + offset);
        MPIU_Assert(vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO] <= ptr && ptr < vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO] + IBCOM_RDMABUF_SZ);
        ptr->magic = 0/*0xde*/; 
        if(offset == sz_data_pow2) { break; }
    }
#endif
#endif

#if 1 /* moving from dcfa_poll */
    /* mark that one eager-send RDMA-write-to buffer has been released */
    int index_slot = (unsigned long)(user_data - vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO]) / IBCOM_RDMABUF_SZSEG;
    MPIU_Assert(0 <= index_slot && index_slot < IBCOM_RDMABUF_NSEG);
    //dprintf("user_data=%p,mem=%p,sub=%08lx,index_slot=%d\n", user_data, vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO], (unsigned long)user_data - (unsigned long)vc_dcfa->ibcom->icom_mem[IBCOM_RDMAWR_TO], index_slot);
    //dprintf("index_slot=%d,released=%016lx\n", index_slot, vc_dcfa->ibcom->rsr_seq_num_released[index_slot / 64]);
    vc_dcfa->ibcom->rsr_seq_num_released[index_slot / 64] |= (1ULL << (index_slot & 63));
    //dprintf("released[index_slot/64]=%016lx\n", vc_dcfa->ibcom->rsr_seq_num_released[index_slot / 64]);
    //    int index_tail = (vc_dcfa->ibcom->rsr_seq_num_tail + 1) & (IBCOM_RDMABUF_NSEG-1);
    int index_tail = (vc_dcfa->ibcom->rsr_seq_num_tail + 1) % IBCOM_RDMABUF_NSEG;
    //dprintf("tail+1=%d,index_tail=%d\n", vc_dcfa->ibcom->rsr_seq_num_tail + 1, index_tail);
    //dprintf("released=%016lx\n", vc_dcfa->ibcom->rsr_seq_num_released[index_tail / 64]);
    if(1||(index_tail & 7) || MPID_nem_dcfa_diff32(index_slot, index_tail) >= IBCOM_RDMABUF_NSEG - 8) { /* avoid wrap-around */
        while(1) {
        if(((vc_dcfa->ibcom->rsr_seq_num_released[index_tail / 64] >> (index_tail & 63)) & 1) == 1) {
            vc_dcfa->ibcom->rsr_seq_num_tail += 1;
            vc_dcfa->ibcom->rsr_seq_num_released[index_tail / 64] &= ~(1ULL << (index_tail & 63));
            dprintf("rsr_seq_num_tail,incremented to %d\n", vc_dcfa->ibcom->rsr_seq_num_tail);
        } else {
            break;
        }
        }
    } else {
        if(((vc_dcfa->ibcom->rsr_seq_num_released[index_tail / 64] >> (index_tail & 63)) & 0xff) == 0xff) {
            vc_dcfa->ibcom->rsr_seq_num_tail += 8;
            vc_dcfa->ibcom->rsr_seq_num_released[index_tail / 64] &= ~(0xffULL << (index_tail & 63));
            //dprintf("released[index_tail/64]=%016lx\n", vc_dcfa->ibcom->rsr_seq_num_released[index_tail / 64]);
        }
    }
    
    //dprintf("recv_buf_released,%d->%d,rsr_seq_num_tail=%d,rsr_seq_num_tail_last_sent=%d\n", MPID_nem_dcfa_myrank, vc->pg_rank, vc_dcfa->ibcom->rsr_seq_num_tail, vc_dcfa->ibcom->rsr_seq_num_tail_last_sent);

    int notify_rate;
    ibcom_errno = ibcom_rdmabuf_occupancy_notify_rate_get(MPID_nem_dcfa_conns[vc->pg_rank].fd, &notify_rate);
    MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_rdmabuf_occupancy_notify_rate_get");

    /* if you missed the chance to make eager-send message piggy-back it */
    if(MPID_nem_dcfa_diff32(vc_dcfa->ibcom->rsr_seq_num_tail, vc_dcfa->ibcom->rsr_seq_num_tail_last_sent) > IBCOM_RDMABUF_OCCUPANCY_NOTIFY_RATE_DELAY_MULTIPLIER(notify_rate) 
       //|| MPID_nem_dcfa_diff32(lsr_seq_num_head, vc_dcfa->ibcom->lsr_seq_num_tail_last_sent) == IBCOM_RDMABUF_NSEG
       ) {
        MPID_Request *sreq;
        sreq = MPID_nem_dcfa_sendq_head(vc_dcfa->sendq);
        if(sreq) {
            int msg_type = MPIDI_Request_get_msg_type(sreq);
            MPIDI_CH3_Pkt_t* ch3_hdr = (MPIDI_CH3_Pkt_t *)sreq->dev.iov[0].MPID_IOV_BUF;
            if(msg_type == MPIDI_REQUEST_EAGER_MSG && /* guard for the following pointer dereference */
               ch3_hdr->type == MPIDI_NEM_DCFA_REPLY_SEQ_NUM) {
                goto skip;
            }
        }
        //printf("recv_buf_released,sending reply_seq_num,diff=%d,rate=%d,id=%d\n", MPID_nem_dcfa_diff32(vc_dcfa->ibcom->rsr_seq_num_tail, vc_dcfa->ibcom->rsr_seq_num_tail_last_sent), notify_rate + (notify_rate>>1), vc_dcfa->ibcom->sseq_num);
        MPID_nem_dcfa_send_reply_seq_num(vc);
    skip:;
    }
#endif

 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_RECV_BUF_RELEASED);
    return mpi_errno;
 fn_fail:
   goto fn_exit;   
}

#if 0
/* packet handler for wrapper packet of MPIDI_NEM_PKT_LMT_DONE */
/* see pkt_DONE_handler (in src/mpid/ch3/channels/nemesis/src/mpid_nem_lmt.c) */
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_lmt_done
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_lmt_done(MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen, MPID_Request **rreqp)
{
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;
    MPID_nem_dcfa_pkt_lmt_done_t * const done_pkt = (MPID_nem_dcfa_pkt_lmt_done_t *)pkt;
    MPID_Request *req;
    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_LMT_DONE);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_LMT_DONE);

    /* Check the assumption on sizeof(MPIDI_CH3_Pkt_t).
       It is utilized in pkt_DONE_handler (in src/mpid/ch3/channels/nemesis/src/mpid_nem_lmt.c) 
       that must be larger than sizeof(MPID_nem_dcfa_pkt_lmt_done_t) */
    if(sizeof(MPID_nem_dcfa_pkt_lmt_done_t) > sizeof(MPIDI_CH3_Pkt_t)) {
        MPIU_ERR_SETFATALANDJUMP(mpi_errno, MPI_ERR_INTERN, "**sizeof(MPIDI_CH3_Pkt_t)");
    }

    /* fall back to the original handler */
    /* we don't need to worry about the difference caused by embedding seq_num 
       because the handler does not use it (e.g. applying sizeof operator to it) */
    MPID_nem_pkt_lmt_done_t *pkt_parent_class = (MPID_nem_pkt_lmt_done_t *)pkt;
    pkt_parent_class->type = MPIDI_NEM_PKT_LMT_DONE;
#if 0
    mpi_errno = MPID_nem_handle_pkt(vc, (char *)pkt_parent_class, *buflen);
#else
    MPIU_ERR_CHKANDJUMP(1, mpi_errno, MPI_ERR_OTHER, "**notimplemented");
    /* you need to modify mpid_nem_lmt.c to make pkt_DONE_handler visible to me */
    //mpi_errno = pkt_DONE_handler(vc, pkt, buflen, rreqp);
#endif
    if(mpi_errno) { MPIU_ERR_POP(mpi_errno); }

 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_LMT_DONE);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}
#endif

/* packet handler for wrapper packet of MPIDI_CH3_PKT_EAGER_SEND */
/* see MPIDI_CH3_PktHandler_EagerSend (in src/mpid/ch3/src/ch3u_eager.c) */
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_EagerSend
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_EagerSend( MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen /* out */, MPID_Request **rreqp /* out */) {
    MPID_nem_dcfa_pkt_prefix_t *netmod_pkt = (MPID_nem_dcfa_pkt_prefix_t*)pkt;
    MPIDI_CH3_Pkt_eager_send_t *ch3_pkt = (MPIDI_CH3_Pkt_eager_send_t*)((void*)pkt + sizeof(MPID_nem_dcfa_pkt_prefix_t));
    MPID_Request * rreq;
    int found;
    int complete;
    char *data_buf;
    MPIDI_msg_sz_t data_len;
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_EAGERSEND);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_EAGERSEND);

    printf("dcfa_pkthandler_eagersend,tag=%d\n", ch3_pkt->match.parts.tag);

    /* Check the assumption on sizeof(MPIDI_CH3_Pkt_t).
       It is utilized to point the payload location in MPIDI_CH3_PktHandler_EagerSend
       (src/mpid/ch3/src/ch3u_eager.c) that must be larger than sizeof(MPID_nem_dcfa_pkt_eager_send_t) */
    //if(sizeof(MPID_nem_dcfa_pkt_eager_send_t) > sizeof(MPIDI_CH3_Pkt_t)) {
    //MPIU_ERR_SETFATALANDJUMP(mpi_errno, MPI_ERR_INTERN, "**sizeof(MPIDI_CH3_Pkt_t)");
    //}

    /* Update occupation status of local SR (send request) queue */
    int *lsr_seq_num_tail;
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    ibcom_errno = ibcom_lsr_seq_num_tail_get(vc_dcfa->sc->fd, &lsr_seq_num_tail);
    MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_lsr_seq_num_tail_get");
    dprintf("MPID_nem_dcfa_PktHandler_EagerSend,lsr_seq_num_tail=%d,netmod_pkt->seq_num_tail=%d\n", *lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    *lsr_seq_num_tail = DCFA_MAX(*lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    dprintf("MPID_nem_dcfa_PktHandler_EagerSend,lsr_seq_num_tail updated to %d\n", *lsr_seq_num_tail);

#ifndef DISABLE_VAR_OCC_NOTIFY_RATE
    /* change remote notification policy of RDMA-write-to buf */
    dprintf("pkthandler,eagersend,old rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
    MPID_nem_dcfa_change_rdmabuf_occupancy_notify_policy_lw(vc_dcfa, lsr_seq_num_tail);
    dprintf("pkthandler,eagersend,new rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
#endif
    
    dprintf("pkthandler,eagersend,sendq_empty=%d,ncom=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail));
    /* try to send from sendq because at least one RDMA-write-to buffer has been released */
    /* calling drain_scq from progress_send derpives of chance
       for dcfa_poll to drain sendq using ncqe
       however transfers events to
       (not to reply_seq_num because it's regulated by the rate)
       fire on dcfa_poll using nces (e.g. MPI_Put) so we need to perform 
       progress_send for all of VCs using nces in dcfa_poll.*/
        dprintf("pkthandler,eagersend,send_progress\n");fflush(stdout);
        MPID_NEM_DCFA_CHECK_AND_SEND_PROGRESS

    /* fall back to the original handler */
    /* we don't need to worry about the difference caused by embedding seq_num
       because size of MPI-header of MPIDI_CH3_PKT_EAGER_SEND equals to sizeof(MPIDI_CH3_Pkt_t) 
       see MPID_nem_dcfa_iSendContig
     */
    //ch3_pkt->type = MPIDI_CH3_PKT_EAGER_SEND;
#if 0
    mpi_errno = MPID_nem_handle_pkt(vc, (char *)pkt_parent_class, *buflen);
#else
    printf("dcfa_poll.c,before PktHandler_EagerSend,buflen=%ld\n", *buflen);
    MPIDI_msg_sz_t ch3_buflen = *buflen - sizeof(MPID_nem_dcfa_pkt_prefix_t);
    mpi_errno = MPIDI_CH3_PktHandler_EagerSend(vc, (MPIDI_CH3_Pkt_t*)ch3_pkt, &ch3_buflen, rreqp);
    printf("dcfa_poll.c,after PktHandler_EagerSend,buflen=%ld\n", ch3_buflen);
    *buflen = ch3_buflen + sizeof(MPID_nem_dcfa_pkt_prefix_t); 
    printf("dcfa_poll.c,after addition,buflen=%ld\n", *buflen);
#endif
    if(mpi_errno) { MPIU_ERR_POP(mpi_errno); }
 
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_EAGERSEND);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

#if 1
/* packet handler for wrapper packet of MPIDI_CH3_PKT_PUT */
/* see MPIDI_CH3_PktHandler_EagerSend (in src/mpid/ch3/src/ch3u_rma_sync.c) */
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_Put
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_Put( MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen /* out */, MPID_Request **rreqp /* out */) {
    MPID_nem_dcfa_pkt_prefix_t *netmod_pkt = (MPID_nem_dcfa_pkt_prefix_t*)pkt;
    MPIDI_CH3_Pkt_put_t *ch3_pkt = (MPIDI_CH3_Pkt_put_t*)((void*)pkt + sizeof(MPID_nem_dcfa_pkt_prefix_t));
    MPID_Request * rreq;
    int found;
    int complete;
    char *data_buf;
    MPIDI_msg_sz_t data_len;
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_PUT);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_PUT);

    /* Update occupation status of local SR (send request) queue */
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    dprintf("MPID_nem_dcfa_Pkthandler_Put,lsr_seq_num_tail=%d,put_pkt->seq_num_tail=%d\n", vc_dcfa->ibcom->lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    vc_dcfa->ibcom->lsr_seq_num_tail = DCFA_MAX(vc_dcfa->ibcom->lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    dprintf("MPID_nem_dcfa_Pkthandler_Put,lsr_seq_num_tail updated to %d\n", vc_dcfa->ibcom->lsr_seq_num_tail);

#ifndef DISABLE_VAR_OCC_NOTIFY_RATE
    /* change remote notification policy of RDMA-write-to buf */
    dprintf("pkthandler,put,old rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
    MPID_nem_dcfa_change_rdmabuf_occupancy_notify_policy_lw(vc_dcfa, &vc_dcfa->ibcom->lsr_seq_num_tail);
    dprintf("pkthandler,put,new rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
#endif    
    dprintf("pkthandler,put,sendq_empty=%d,ncom=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail));
    /* try to send from sendq because at least one RDMA-write-to buffer has been released */
    dprintf("pkthandler,put,send_progress\n");fflush(stdout);
    MPID_NEM_DCFA_CHECK_AND_SEND_PROGRESS;
    
    /* fall back to the original handler */
    /* we don't need to worry about the difference caused by embedding seq_num
       because size of MPI-header of MPIDI_CH3_PKT_PUT equals to sizeof(MPIDI_CH3_Pkt_t) 
       see MPID_nem_dcfa_iSendContig
    */
    MPIDI_msg_sz_t ch3_buflen = *buflen - sizeof(MPID_nem_dcfa_pkt_prefix_t);
    mpi_errno = MPIDI_CH3_PktHandler_Put(vc, (MPIDI_CH3_Pkt_t *)ch3_pkt, &ch3_buflen, rreqp);
    *buflen = ch3_buflen + sizeof(MPID_nem_dcfa_pkt_prefix_t);
    if(mpi_errno) { MPIU_ERR_POP(mpi_errno); }
 
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_PUT);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}
#endif

/* packet handler for wrapper packet of MPIDI_CH3_PKT_ACCUMULATE */
/* see MPIDI_CH3_PktHandler_Accumulate (in src/mpid/ch3/src/ch3u_rma_sync.c) */
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_Accumulate
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_Accumulate( MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen /* out */, MPID_Request **rreqp /* out */) {
    MPID_nem_dcfa_pkt_prefix_t *netmod_pkt = (MPID_nem_dcfa_pkt_prefix_t*)pkt;
    MPIDI_CH3_Pkt_accum_t *ch3_pkt = (MPIDI_CH3_Pkt_accum_t*)((void*)pkt + sizeof(MPID_nem_dcfa_pkt_prefix_t));
    MPID_Request * rreq;
    int found;
    int complete;
    char *data_buf;
    MPIDI_msg_sz_t data_len;
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_ACCUMULATE);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_ACCUMULATE);

    /* Update occupation status of local SR (send request) queue */
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    dprintf("MPID_nem_dcfa_Pkthandler_Accumulate,lsr_seq_num_tail=%d,accum_pkt->seq_num_tail=%d\n", vc_dcfa->ibcom->lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    vc_dcfa->ibcom->lsr_seq_num_tail = DCFA_MAX(vc_dcfa->ibcom->lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    dprintf("MPID_nem_dcfa_Pkthandler_Accumulate,lsr_seq_num_tail updated to %d\n", vc_dcfa->ibcom->lsr_seq_num_tail);

#ifndef DISABLE_VAR_OCC_NOTIFY_RATE
    /* change remote notification policy of RDMA-write-to buf */
    dprintf("pkthandler,put,old rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
    MPID_nem_dcfa_change_rdmabuf_occupancy_notify_policy_lw(vc_dcfa, &vc_dcfa->ibcom->lsr_seq_num_tail);
    dprintf("pkthandler,put,new rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
#endif    
    dprintf("pkthandler,put,sendq_empty=%d,ncom=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail));
    /* try to send from sendq because at least one RDMA-write-to buffer has been released */
        dprintf("pkthandler,put,send_progress\n");fflush(stdout);
        MPID_NEM_DCFA_CHECK_AND_SEND_PROGRESS


    /* fall back to the original handler */
    /* we don't need to worry about the difference caused by embedding seq_num
       because size of MPI-header of MPIDI_CH3_PKT_PUT equals to sizeof(MPIDI_CH3_Pkt_t) 
       see MPID_nem_dcfa_iSendContig
     */
    MPIDI_msg_sz_t ch3_buflen = *buflen - sizeof(MPID_nem_dcfa_pkt_prefix_t);
    mpi_errno = MPIDI_CH3_PktHandler_Accumulate(vc, (MPIDI_CH3_Pkt_t *)ch3_pkt, &ch3_buflen, rreqp);
    *buflen = ch3_buflen + sizeof(MPID_nem_dcfa_pkt_prefix_t); 
    if(mpi_errno) { MPIU_ERR_POP(mpi_errno); }
 
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_ACCUMULATE);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

/* packet handler for wrapper packet of MPIDI_CH3_PKT_GET */
/* see MPIDI_CH3_PktHandler_Get (in src/mpid/ch3/src/ch3u_rma_sync.c) */
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_Get
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_Get( MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen /* out */, MPID_Request **rreqp /* out */) {
    MPID_nem_dcfa_pkt_prefix_t *netmod_pkt = (MPID_nem_dcfa_pkt_prefix_t*)pkt;
    MPIDI_CH3_Pkt_get_t *ch3_pkt = (MPIDI_CH3_Pkt_get_t*)((void*)pkt + sizeof(MPID_nem_dcfa_pkt_prefix_t));
    MPID_Request * rreq;
    int found;
    int complete;
    char *data_buf;
    MPIDI_msg_sz_t data_len;
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_GET);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_GET);

    /* Update occupation status of local SR (send request) queue */
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    dprintf("MPID_nem_dcfa_Pkthandler_Get,lsr_seq_num_tail=%d,get_pkt->seq_num_tail=%d\n", vc_dcfa->ibcom->lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    vc_dcfa->ibcom->lsr_seq_num_tail = DCFA_MAX(vc_dcfa->ibcom->lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    dprintf("MPID_nem_dcfa_Pkthandler_Get,lsr_seq_num_tail updated to %d\n", vc_dcfa->ibcom->lsr_seq_num_tail);

#ifndef DISABLE_VAR_OCC_NOTIFY_RATE
    /* change remote notification policy of RDMA-write-to buf */
    dprintf("pkthandler,put,old rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
    MPID_nem_dcfa_change_rdmabuf_occupancy_notify_policy_lw(vc_dcfa, &vc_dcfa->ibcom->lsr_seq_num_tail);
    dprintf("pkthandler,put,new rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
#endif    
    dprintf("pkthandler,put,sendq_empty=%d,ncom=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail));
    /* try to send from sendq because at least one RDMA-write-to buffer has been released */
        dprintf("pkthandler,get,send_progress\n");fflush(stdout);
        MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET 

    /* fall back to the original handler */
    /* we don't need to worry about the difference caused by embedding seq_num
       because size of MPI-header of MPIDI_CH3_PKT_PUT equals to sizeof(MPIDI_CH3_Pkt_t) 
       see MPID_nem_dcfa_iSendContig
     */
    MPIDI_msg_sz_t ch3_buflen = *buflen - sizeof(MPID_nem_dcfa_pkt_prefix_t);
    mpi_errno = MPIDI_CH3_PktHandler_Get(vc, (MPIDI_CH3_Pkt_t *)ch3_pkt, &ch3_buflen, rreqp);
    *buflen = ch3_buflen + sizeof(MPID_nem_dcfa_pkt_prefix_t);
    if(mpi_errno) { MPIU_ERR_POP(mpi_errno); }
 
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_GET);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

/* packet handler for wrapper packet of MPIDI_CH3_PKT_GET_RESP */
/* see MPIDI_CH3_PktHandler_GetResp (in src/mpid/ch3/src/ch3u_rma_sync.c) */
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_GetResp
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_GetResp( MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen /* out */, MPID_Request **rreqp /* out */) {
    MPID_nem_dcfa_pkt_prefix_t *netmod_pkt = (MPID_nem_dcfa_pkt_prefix_t*)pkt;
    MPIDI_CH3_Pkt_get_t *ch3_pkt = (MPIDI_CH3_Pkt_get_t*)((void*)pkt + sizeof(MPID_nem_dcfa_pkt_prefix_t));
    MPID_Request * rreq;
    int found;
    int complete;
    char *data_buf;
    MPIDI_msg_sz_t data_len;
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_GETRESP);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_GETRESP);

    /* Update occupation status of local SR (send request) queue */
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    dprintf("MPID_nem_dcfa_Pkthandler_GetResp,lsr_seq_num_tail=%d,get_pkt->seq_num_tail=%d\n", vc_dcfa->ibcom->lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    vc_dcfa->ibcom->lsr_seq_num_tail = DCFA_MAX(vc_dcfa->ibcom->lsr_seq_num_tail, netmod_pkt->seq_num_tail);
    dprintf("MPID_nem_dcfa_Pkthandler_GetResp,lsr_seq_num_tail updated to %d\n", vc_dcfa->ibcom->lsr_seq_num_tail);

#ifndef DISABLE_VAR_OCC_NOTIFY_RATE
    /* change remote notification policy of RDMA-write-to buf */
    dprintf("pkthandler,put,old rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
    MPID_nem_dcfa_change_rdmabuf_occupancy_notify_policy_lw(vc_dcfa, &vc_dcfa->ibcom->lsr_seq_num_tail);
    dprintf("pkthandler,put,new rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
#endif    
    dprintf("pkthandler,put,sendq_empty=%d,ncom=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail));
    /* try to send from sendq because at least one RDMA-write-to buffer has been released */
        dprintf("pkthandler,get,send_progress\n");fflush(stdout);
        MPID_NEM_DCFA_SEND_PROGRESS_POLLINGSET 

    /* fall back to the original handler */
    /* we don't need to worry about the difference caused by embedding seq_num
       because size of MPI-header of MPIDI_CH3_PKT_PUT equals to sizeof(MPIDI_CH3_Pkt_t) 
       see MPID_nem_dcfa_iSendContig
     */
    MPIDI_msg_sz_t ch3_buflen = *buflen - sizeof(MPID_nem_dcfa_pkt_prefix_t);
    mpi_errno = MPIDI_CH3_PktHandler_GetResp(vc, (MPIDI_CH3_Pkt_t *)ch3_pkt, &ch3_buflen, rreqp);
    *buflen = ch3_buflen + sizeof(MPID_nem_dcfa_pkt_prefix_t);
    if(mpi_errno) { MPIU_ERR_POP(mpi_errno); }
 
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_GETRESP);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

/* MPI_Isend set req-type to MPIDI_REQUEST_TYPE_RECV */
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_pkt_GET_DONE_handler
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_pkt_GET_DONE_handler(MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen, MPID_Request **rreqp)
{
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;
    MPID_nem_dcfa_pkt_lmt_get_done_t * const done_pkt = (MPID_nem_dcfa_pkt_lmt_get_done_t *)pkt;
    MPID_Request *req;
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKT_GET_DONE_HANDLER);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKT_GET_DONE_HANDLER);

    dprintf("get_done_handler,enter\n");

    *buflen = sizeof(MPIDI_CH3_Pkt_t);
    MPID_Request_get_ptr(done_pkt->req_id, req);

    MPIU_THREAD_CS_ENTER(LMT,);

    switch (MPIDI_Request_get_type(req))
    {
    /* MPIDI_Request_set_type is not performed when
       MPID_Isend --> FDU_or_AEP --> recv_posted --> dcfa_poll --> PUTCTS packet-handler */
    case MPIDI_REQUEST_TYPE_RECV:
        MPIU_ERR_INTERNALANDJUMP(mpi_errno, "unexpected request type");
        break;
    case MPIDI_REQUEST_TYPE_SEND:
    case MPIDI_REQUEST_TYPE_RSEND:
    case MPIDI_REQUEST_TYPE_SSEND:
    case MPIDI_REQUEST_TYPE_BSEND:
        /* extract embeded RDMA-write-to buffer occupancy information */
        dprintf("get_done_handler,old lsr_seq_num_tail=%d,done_pkt->seq_num_tail=%d\n", vc_dcfa->ibcom->lsr_seq_num_tail, done_pkt->seq_num_tail);
        vc_dcfa->ibcom->lsr_seq_num_tail = DCFA_MAX(vc_dcfa->ibcom->lsr_seq_num_tail, done_pkt->seq_num_tail);
        //dprintf("lmt_start_recv,new lsr_seq_num=%d\n", vc_dcfa->ibcom->lsr_seq_num_tail);
        
#ifndef DISABLE_VAR_OCC_NOTIFY_RATE
        /* change remote notification policy of RDMA-write-to buf */
        //dprintf("lmt_start_recv,reply_seq_num,old rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
        MPID_nem_dcfa_change_rdmabuf_occupancy_notify_policy_lw(vc_dcfa, &vc_dcfa->ibcom->lsr_seq_num_tail);
        //dprintf("lmt_start_recv,reply_seq_num,new rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
#endif        
        //dprintf("lmt_start_recv,reply_seq_num,sendq_empty=%d,ncom=%d,ncqe=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_ncqe, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail));
        /* try to send from sendq because at least one RDMA-write-to buffer has been released */
        //dprintf("lmt_start_recv,reply_seq_num,send_progress\n");
        if(!MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq)) {
            dprintf("get_done_handler,ncom=%d,ncqe=%d,diff=%d(%d-%d)\n", vc_dcfa->ibcom->ncom < IBCOM_MAX_SQ_CAPACITY, MPID_nem_dcfa_ncqe < IBCOM_MAX_CQ_CAPACITY, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail) < IBCOM_RDMABUF_NSEG,
                   vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail);
        }
            dprintf("get_done_handler,send_progress\n");fflush(stdout);
            MPID_NEM_DCFA_CHECK_AND_SEND_PROGRESS

        mpi_errno = vc->ch.lmt_done_send(vc, req);
        if (mpi_errno) MPIU_ERR_POP(mpi_errno);
        break;
    default:
        MPIU_ERR_INTERNALANDJUMP(mpi_errno, "unexpected request type");
        break;
    }

    *rreqp = NULL;

 fn_exit:
    MPIU_THREAD_CS_EXIT(LMT,);
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKT_GET_DONE_HANDLER);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_req_seq_num
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_req_seq_num(MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen, MPID_Request **rreqp)
{
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;
    MPID_nem_dcfa_pkt_req_seq_num_t * const req_pkt = (MPID_nem_dcfa_pkt_req_seq_num_t *)pkt;
    MPID_Request *req;
    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_REQ_SEQ_NUM);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_REQ_SEQ_NUM);

    /* mark as all of the message is read */
    *buflen = sizeof(MPIDI_CH3_Pkt_t);

    /* mark as I don't need continuation read request */
    *rreqp = NULL;

    /* update occupancy info of SR */
    /* request piggy-backs seq_num although it's requesting responder's seq_num */
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    vc_dcfa->ibcom->lsr_seq_num_tail = DCFA_MAX(vc_dcfa->ibcom->lsr_seq_num_tail, req_pkt->seq_num_tail);

    dprintf("PktHandler_req_seq_num,sendq=%d,ncom=%d,ncqe=%d,diff=%d(%d-%d)\n",
           MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq),
           vc_dcfa->ibcom->ncom < IBCOM_MAX_SQ_CAPACITY,
           MPID_nem_dcfa_ncqe < IBCOM_MAX_CQ_CAPACITY,
           MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail) < IBCOM_RDMABUF_NSEG,
           vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail
           );

    /* send reply */
    dprintf("PktHandler_req_seq_num,sending reply_seq_num,id=%d\n", vc_dcfa->ibcom->sseq_num);
    MPID_nem_dcfa_send_reply_seq_num(vc);

 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_REQ_SEQ_NUM);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_reply_seq_num
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_reply_seq_num(MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen, MPID_Request **rreqp)
{
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;
    MPID_nem_dcfa_pkt_reply_seq_num_t * const reply_pkt = (MPID_nem_dcfa_pkt_reply_seq_num_t *)pkt;
    MPID_Request *req;
    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_REPLY_SEQ_NUM);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_REPLY_SEQ_NUM);


    /* mark as all of the message is consumed */
    *buflen = sizeof(MPIDI_CH3_Pkt_t);

    /* mark as I don't need continuation read request */
    *rreqp = NULL;

    /* update occupancy info of RDMA-write-buf */
    int *lsr_seq_num_tail;
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    dprintf("pkthandler,reply_seq_num,old lsr_seq_num=%d,reply_pkt->seq_num_tail=%d\n", vc_dcfa->ibcom->lsr_seq_num_tail, reply_pkt->seq_num_tail);
    ibcom_errno = ibcom_lsr_seq_num_tail_get(vc_dcfa->sc->fd, &lsr_seq_num_tail);
    MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_lsr_seq_num_tail_get");
    *lsr_seq_num_tail = DCFA_MAX(*lsr_seq_num_tail, reply_pkt->seq_num_tail);
    //dprintf("pkthandler,reply_seq_num,new lsr_seq_num=%d\n", vc_dcfa->ibcom->lsr_seq_num_tail);

#ifndef DISABLE_VAR_OCC_NOTIFY_RATE
    /* change remote notification policy of RDMA-write-to buf */
    //dprintf("pkthandler,reply_seq_num,old rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
    MPID_nem_dcfa_change_rdmabuf_occupancy_notify_policy_lw(vc_dcfa, lsr_seq_num_tail);
    //dprintf("pkthandler,reply_seq_num,new rstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_rstate);   
#endif

    //dprintf("pkthandler,reply_seq_num,sendq_empty=%d,ncom=%d,ncqe=%d,rdmabuf_occ=%d\n", MPID_nem_dcfa_sendq_empty(vc_dcfa->sendq), vc_dcfa->ibcom->ncom, MPID_nem_dcfa_ncqe, MPID_nem_dcfa_diff32(vc_dcfa->ibcom->sseq_num, vc_dcfa->ibcom->lsr_seq_num_tail));
    /* try to send from sendq because at least one RDMA-write-to buffer has been released */
    //dprintf("pkthandler,reply_seq_num,send_progress\n");
        dprintf("pkthandler,reply_seq_num,send_progress\n");
        MPID_NEM_DCFA_CHECK_AND_SEND_PROGRESS

 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_REPLY_SEQ_NUM);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_PktHandler_change_rdmabuf_occupancy_notify_state
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_PktHandler_change_rdmabuf_occupancy_notify_state(MPIDI_VC_t *vc, MPIDI_CH3_Pkt_t *pkt, MPIDI_msg_sz_t *buflen, MPID_Request **rreqp)
{
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;
    MPID_nem_dcfa_pkt_change_rdmabuf_occupancy_notify_state_t * const reply_pkt = (MPID_nem_dcfa_pkt_change_rdmabuf_occupancy_notify_state_t *)pkt;
    MPID_Request *req;
    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_CHANGE_RDMABUF_OCCUPANCY_NOTIFY_STATE);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_CHANGE_RDMABUF_OCCUPANCY_NOTIFY_STATE);

    /* mark as all of the message is read */
    *buflen = sizeof(MPIDI_CH3_Pkt_t);

    /* mark as I don't need continuation read request */
    *rreqp = NULL;


    /* update occupancy info of SR */
    MPID_nem_dcfa_vc_area *vc_dcfa = VC_DCFA(vc);
    dprintf("pkthandler,change notify state,old lstate=%d,pkt->state=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_lstate, reply_pkt->state);
    int *rdmabuf_occupancy_notify_lstate;
    ibcom_errno = ibcom_rdmabuf_occupancy_notify_lstate_get(vc_dcfa->sc->fd, &rdmabuf_occupancy_notify_lstate);
    MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_rdmabuf_occupancy_notify_lstate_get"); 
    *rdmabuf_occupancy_notify_lstate = reply_pkt->state;
    dprintf("pkthandler,change notify state,new lstate=%d\n", vc_dcfa->ibcom->rdmabuf_occupancy_notify_lstate);

 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_PKTHANDLER_CHANGE_RDMABUF_OCCUPANCY_NOTIFY_STATE);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

#ifdef DCFA_ONDEMAND
#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_cm_drain_scq
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_cm_drain_scq() {

    int mpi_errno = MPI_SUCCESS;
    int result;
    int i;
    struct ibv_wc cqe[IBCOM_MAX_CQ_HEIGHT_DRAIN];

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_CM_DRAIN_SCQ);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_CM_DRAIN_SCQ);

    result = ibv_poll_cq(rc_shared_scq_lmt_put, IBCOM_MAX_CQ_HEIGHT_DRAIN, &cqe[0]);
    MPIU_ERR_CHKANDJUMP(result < 0, mpi_errno, MPI_ERR_OTHER, "**netmod,dcfa,ibv_poll_cq");
    
    if(result > 0) { dprintf("cm_drain_scq,found,result=%d\n", result); }
    for(i = 0; i < result; i++)  {
        
#ifdef DCFA
        if(cqe[i].status != IBV_WC_SUCCESS) { dprintf("cm_drain_scq,status=%08x\n", cqe[i].status); }
#else
        if(cqe[i].status != IBV_WC_SUCCESS) { dprintf("cm_drain_scq,status=%08x,%s\n", cqe[i].status, ibv_wc_status_str(cqe[i].status)); }
#endif
        MPIU_ERR_CHKANDJUMP(cqe[i].status != IBV_WC_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_cm_drain_scq");
        /* TODO retry a connection request when it timed out */

        if(cqe.wr_id == MPID_NEM_DCFA_SYN ||
           cqe.wr_id == MPID_NEM_DCFA_SYNACK) {

            MPID_nem_dcfa_conn_ud_ibcom->ncom_lmt_put -= 1;
            MPID_nem_dcfa_ncqe_connect -= 1;
            
            /* Try to send from sendq_connect */
            if(!MPID_nem_dcfa_sendq_empty(sendq_connect) &&
               MPID_nem_dcfa_ncom_lmt_put < IBCOM_MAX_SQ_CAPACITY &&
               MPID_nem_dcfa_ncqe_lmt_put < IBCOM_MAX_CQ_CAPACITY) {
                MPID_nem_dcfa_send_progress_connect(); 
            }
        } else {
            printf("unknown command=%d\n", cqe.wr_id);
            MPIU_ERR_CHKANDJUMP(1, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_cm_drain_scq");
        }
    }
    
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_CM_DRAIN_SCQ);
    return mpi_errno;
 fn_fail:
   goto fn_exit;   
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_cm_poll
#undef FCNAME
#define FCNAME MPIU_QUOTE(FUNCNAME)
int MPID_nem_dcfa_cm_poll()
{
    int mpi_errno = MPI_SUCCESS;

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_CM_POLL);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_CM_POLL);

    dprintf("cm_poll,enter\n");


    volatile uint32_t* owner = (uint32_t*)(ibcom_scratch_pad->icom_mem[IBCOM_SCRATCH_PAD_TO]);
    if(*owner == (uint32_t)-1) { goto fn_exit; } /* not acquired */

    IbCom* ibcom_scratch_pad;
    ibcom_errno = ibcom_obtain_pointer(MPID_nem_dcfa_scratch_pad_fds[*owner], &ibcom_scratch_pad); 
    MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_obtain_pointer");

    MPID_nem_dcfa_cm_cmd_t* received = (MPID_nem_dcfa_cm_cmd_t*)(ibcom_scratch_pad->icom_mem[IBCOM_SCRATCH_PAD_TO] + sizeof(uint32_t));
    MPID_nem_dcfa_cm_cmd_t cmd;
    MPID_nem_dcfa_vc_area *vc_dcfa;
    switch(received->type) {
    case MPID_NEM_DCFA_CM_SYN:
        ibcom_errno = ibcomOpen(ib_port, IBCOM_OPEN_RC, &MPID_nem_dcfa_conns[*owner].fd);
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcomOpen");
        cmd.type = MPID_NEM_DCFA_CM_SYNACK;
        goto common_tail;
        break;
    case MPID_NEM_DCFA_CM_BUSINESSCARD: {
        ibcom_errno = ibcom_rts(MPID_nem_dcfa_conns[*owner].fd, received->qpnum, received->lid, &(received->gid));
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_rts");
        ibcom_errno = ibcom_reg_mr_connect(MPID_nem_dcfa_conns[*owner].fd, received->rmem, received->rkey);
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_reg_mr_connect");
        VC_FIELD(MPID_nem_dcfa_conns[owner].vc,is_connected) = 1;

        cmd.type = MPID_NEM_DCFA_CM_ACK;
        common_tail:        
        ibcom_errno = ibcom_get_info_conn(MPID_nem_dcfa_conns[*owner].fd, IBCOM_INFOKEY_PORT_LID, &(cmd.lid), sizeof(uint16_t));
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_get_info_conn");

        ibcom_errno = ibcom_get_info_conn(MPID_nem_dcfa_conns[*owner].fd, IBCOM_INFOKEY_PORT_GID, &(cmd.gid), sizeof(union ibv_gid));
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_get_info_conn");

        ibcom_errno = ibcom_get_info_conn(MPID_nem_dcfa_conns[*owner].fd, IBCOM_INFOKEY_QP_QPN, &(cmd.qpnum), sizeof(uint32_t));
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_get_info_conn");

        ibcom_errno = ibcom_get_info_mr(MPID_nem_dcfa_conns[*owner].fd, IBCOM_SCRATCH_PAD_TO, IBCOM_INFOKEY_MR_ADDR, &(cmd.rmem), sizeof(void*));
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_get_info_mr");

        ibcom_errno = ibcom_get_info_mr(MPID_nem_dcfa_conns[*owner].fd, IBCOM_SCRATCH_PAD_TO, IBCOM_INFOKEY_MR_RKEY, &(cmd.rkey), sizeof(int));
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_get_info_mr");

        *owner = (uint32_t)-1; /* release */

        mpi_errno = MPID_nem_dcfa_cm_send_core(rank, &cmd);
Masamichi Takagi's avatar
Masamichi Takagi committed
1728
        MPIU_ERR_CHKANDJUMP(mp_errno, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_cm_send_core"); 
1729
1730
1731
        break;
    default:
        printf("unknown connection command\n");
Masamichi Takagi's avatar
Masamichi Takagi committed
1732
        MPIU_ERR_CHKANDJUMP(1, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_cm_poll");
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
    }

 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_CM_POLL);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

#undef FUNCNAME
#define FUNCNAME MPID_nem_dcfa_cm_accept
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPID_nem_dcfa_cm_accept() {
    int mpi_errno = MPI_SUCCESS;
    int ibcom_errno;
    int result;
    int i;
    struct ibv_wc cqe[IBCOM_MAX_CQ_HEIGHT_DRAIN];

    MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_DCFA_CM_ACCEPT);
    MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_DCFA_CM_ACCEPT);

    result = ibv_poll_cq(ud_shared_rcq, IBCOM_MAX_CQ_HEIGHT_DRAIN, &cqe);
    MPIU_ERR_CHKANDJUMP(result < 0, mpi_errno, MPI_ERR_OTHER, "**netmod,dcfa,ibv_poll_cq");

    if(result > 0) {
        dprintf("accept,result=%d\n", result); 
    }
    for(i = 0; i < result; i++)  {
        dprintf("accept,i=%d\n", i);

        MPIU_ERR_CHKANDJUMP(cqe.status != IBV_WC_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**MPID_nem_dcfa_cm_accept");
        
        void* rbuf;
        ibcom_errno = ibcom_mem_udwr_to(MPID_nem_dcfa_conn_ud_fd, &rbuf);
        MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_mem_udwr_to");
        MPID_nem_dcfa_conn_pkt_t* rpkt = (MPID_nem_dcfa_conn_pkt_t*)(rbuf + 40);
        if(rpkt->type == MPID_NEM_DCFA_SYN) {

            dprintf("accept,%d<-%d,type=%08x\n", MPID_nem_dcfa_myrank, rpkt->remote_rank, rpkt->type);                            

            void* sbuf;
            ibcom_errno = ibcom_mem_udwr_from(MPID_nem_dcfa_conn_ud_fd, &sbuf);
            MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_mem_udwr_from");
            MPID_nem_dcfa_conn_pkt_t* spkt = (MPID_nem_dcfa_conn_pkt_t*)(sbuf + 40);
            spkt->remote_rank = MPID_nem_dcfa_myrank;
            spkt->type = MPID_NEM_DCFA_SYNACK;

            ibcom_errno = ibcom_get_info_conn(MPID_nem_dcfa_conns[rpkt->remote_rank].fd, IBCOM_INFOKEY_QP_QPN, &spkt->qpn, sizeof(uint32_t));
            MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_get_info_conn");

            ibcom_errno = ibcom_get_info_mr(MPID_nem_dcfa_conns[remote_rank].fd, IBCOM_RDMAWR_TO, IBCOM_INFOKEY_MR_ADDR, &spkt->rmem, sizeof(void*));
            MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_get_info_mr");

            ibcom_errno = ibcom_get_info_mr(MPID_nem_dcfa_conns[remote_rank].fd, IBCOM_RDMAWR_TO, IBCOM_INFOKEY_MR_RKEY, &spkt->rkey, sizeof(int));
            MPIU_ERR_CHKANDJUMP(ibcom_errno, mpi_errno, MPI_ERR_OTHER, "**ibcom_get_info_mr");
            
            /* kokomade. add udsend_core(synack) */
            if(MPID_nem_dcfa_conn_ibcom->ncom < &&
               MPID_nem_dcfa_ncqe_connect < ) {
                MPID_nem_dcfa_conn_send_core(rpkt->remote_rank);
            } else {
                MPID_nem_dcfa_sendq_conn_entry_t* entry = MPIU_Malloc(sizeof(MPID_nem_dcfa_sendq_conn_entry_t));
                MPIU_ERR_CHKANDJUMP(!entry, mpi_errno, MPI_ERR_OTHER, "**outofmemory");
                entry->pending_pkt = *spkt;
                MPID_nem_dcfa_conn_sendq_enqueue(MPID_nem_dcfa_conn_sendq, entry);
            }


        } else {
            dprintf("accept,unknown type=%08x\n", *((uint32_t*)(rbuf + 44)));
        }                        
    }
 fn_exit:
    MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_DCFA_CM_ACCEPT);
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}
#endif