ch3u_rma_oplist.c 56.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
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/*
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

#include "mpidimpl.h"
#include "mpidrma.h"

/*
=== BEGIN_MPI_T_CVAR_INFO_BLOCK ===

cvars:
    - name        : MPIR_CVAR_CH3_RMA_ACC_IMMED
      category    : CH3
      type        : boolean
      default     : true
      class       : none
      verbosity   : MPI_T_VERBOSITY_USER_BASIC
      scope       : MPI_T_SCOPE_ALL_EQ
      description : >-
        Use the immediate accumulate optimization

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    - name        : MPIR_CVAR_CH3_RMA_ACTIVE_REQ_THRESHOLD
      category    : CH3
      type        : int
      default     : 2097152
      class       : none
      verbosity   : MPI_T_VERBOSITY_USER_BASIC
      scope       : MPI_T_SCOPE_ALL_EQ
      description : >-
         Threshold of number of active requests to trigger
         blocking waiting in operation routines. When the
         value is negative, we never blockingly wait in
         operation routines. When the value is zero, we always
         trigger blocking waiting in operation routines to
         wait until no. of active requests becomes zero. When the
         value is positive, we do blocking waiting in operation
         routines to wait until no. of active requests being
         reduced to this value.

42
43
44
=== END_MPI_T_CVAR_INFO_BLOCK ===
*/

45
46
47
48
static inline int issue_rma_op(MPIDI_RMA_Op_t * op_ptr, MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags);
static inline int issue_ops_target(MPID_Win * win_ptr, MPIDI_RMA_Target_t *target, int *made_progress);
static inline int issue_ops_win(MPID_Win * win_ptr, int *made_progress);

49
50
51
52
53
54
55
56
static int send_flush_msg(int dest, MPID_Win *win_ptr);
static int send_rma_msg(MPIDI_RMA_Op_t * rma_op, MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags);
static int send_contig_acc_msg(MPIDI_RMA_Op_t * rma_op,
                               MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags);
static int recv_rma_msg(MPIDI_RMA_Op_t * rma_op, MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags);
static int send_immed_rmw_msg(MPIDI_RMA_Op_t * rma_op,
                              MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags);

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
#undef FUNCNAME
#define FUNCNAME issue_rma_op
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
static inline int issue_rma_op(MPIDI_RMA_Op_t * op_ptr, MPID_Win * win_ptr,
                               MPIDI_CH3_Pkt_flags_t flags)
{
    int mpi_errno = MPI_SUCCESS;
    MPIDI_STATE_DECL(MPID_STATE_ISSUE_RMA_OP);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_ISSUE_RMA_OP);

    switch (op_ptr->pkt.type) {
    case (MPIDI_CH3_PKT_PUT):
    case (MPIDI_CH3_PKT_ACCUMULATE):
    case (MPIDI_CH3_PKT_GET_ACCUM):
        mpi_errno = send_rma_msg(op_ptr, win_ptr, flags);
        break;
    case (MPIDI_CH3_PKT_ACCUM_IMMED):
        mpi_errno = send_contig_acc_msg(op_ptr, win_ptr, flags);
        break;
    case (MPIDI_CH3_PKT_GET):
        mpi_errno = recv_rma_msg(op_ptr, win_ptr, flags);
        break;
    case (MPIDI_CH3_PKT_CAS):
    case (MPIDI_CH3_PKT_FOP):
        mpi_errno = send_immed_rmw_msg(op_ptr, win_ptr, flags);
        break;
        default:
            MPIU_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**winInvalidOp");
        }

    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);

  fn_exit:
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_ISSUE_RMA_OP);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}

#undef FUNCNAME
#define FUNCNAME check_window_state
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
static inline int check_window_state(MPID_Win *win_ptr, int *made_progress, int *cannot_issue)
{
    int i, mpi_errno = MPI_SUCCESS;
    MPIDI_STATE_DECL(MPID_STATE_CHECK_WINDOW_STATE);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_CHECK_WINDOW_STATE);

    (*made_progress) = 0;
    (*cannot_issue) = 0;

    if (win_ptr->states.access_state == MPIDI_RMA_NONE) {
        (*cannot_issue) = 1;
        goto fn_exit;
    }
    else if (win_ptr->states.access_state == MPIDI_RMA_FENCE_ISSUED) {
        MPID_Request *fence_req_ptr = NULL;
        MPID_Request_get_ptr(win_ptr->fence_sync_req, fence_req_ptr);
        if (MPID_Request_is_complete(fence_req_ptr)) {
            win_ptr->states.access_state = MPIDI_RMA_FENCE_GRANTED;
            MPID_Request_release(fence_req_ptr);
            win_ptr->fence_sync_req = MPI_REQUEST_NULL;

127
128
129
            num_active_issued_win--;
            MPIU_Assert(num_active_issued_win >= 0);

130
131
132
133
134
135
136
137
138
139
140
141
142
            (*made_progress) = 1;
        }
        else {
            (*cannot_issue) = 1;
            goto fn_exit;
        }
    }
    else if (win_ptr->states.access_state == MPIDI_RMA_PSCW_ISSUED) {
        if (win_ptr->start_req == NULL) {
            /* for MPI_MODE_NOCHECK and all targets on SHM,
               we do not create PSCW requests on window. */
            win_ptr->states.access_state = MPIDI_RMA_PSCW_GRANTED;

143
144
145
            num_active_issued_win--;
            MPIU_Assert(num_active_issued_win >= 0);

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
            (*made_progress) = 1;
        }
        else {
            for (i = 0; i < win_ptr->start_grp_size; i++) {
                MPID_Request *start_req_ptr = NULL;
                if (win_ptr->start_req[i] == MPI_REQUEST_NULL)
                    continue;
                MPID_Request_get_ptr(win_ptr->start_req[i], start_req_ptr);
                if (MPID_Request_is_complete(start_req_ptr)) {
                    MPID_Request_release(start_req_ptr);
                    win_ptr->start_req[i] = MPI_REQUEST_NULL;
                }
                else {
                    (*cannot_issue) = 1;
                    goto fn_exit;
                }
            }
            MPIU_Assert(i == win_ptr->start_grp_size);
            win_ptr->states.access_state = MPIDI_RMA_PSCW_GRANTED;

166
167
168
            num_active_issued_win--;
            MPIU_Assert(num_active_issued_win >= 0);

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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
            (*made_progress) = 1;

            MPIU_Free(win_ptr->start_req);
            win_ptr->start_req = NULL;
        }
    }
    else if (win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_ISSUED) {
        if (win_ptr->outstanding_locks == 0) {
            win_ptr->states.access_state = MPIDI_RMA_LOCK_ALL_GRANTED;
            (*made_progress) = 1;
        }
        else {
            (*cannot_issue) = 1;
            goto fn_exit;
        }
    }

  fn_exit:
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_CHECK_WINDOW_STATE);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}



#undef FUNCNAME
#define FUNCNAME issue_ops_target
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
static inline int issue_ops_target(MPID_Win * win_ptr, MPIDI_RMA_Target_t *target,
                                   int *made_progress)
{
    int rank = win_ptr->comm_ptr->rank;
    MPIDI_RMA_Op_t *curr_op = NULL;
    int first_op;
    int mpi_errno = MPI_SUCCESS;

    (*made_progress) = 0;

    if (win_ptr->non_empty_slots == 0 || target == NULL)
        goto fn_exit;

    /* check per-target state */
    if (win_ptr->states.access_state == MPIDI_RMA_PER_TARGET ||
        win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_CALLED) {
        if (target->access_state == MPIDI_RMA_LOCK_CALLED) {
            if (target->sync.sync_flag == MPIDI_RMA_SYNC_NONE ||
                target->sync.sync_flag == MPIDI_RMA_SYNC_FLUSH_LOCAL ||
                target->sync.sync_flag == MPIDI_RMA_SYNC_FLUSH) {
                if (target->pending_op_list != NULL &&
                    target->pending_op_list->piggyback_lock_candidate) {
                    /* Capable of piggybacking LOCK message with first operation. */
                }
                else {
                    target->access_state = MPIDI_RMA_LOCK_ISSUED;
                    target->outstanding_lock++;
                    MPIU_Assert(target->outstanding_lock == 1);
                    if (target->target_rank == rank) {
                        mpi_errno = acquire_local_lock(win_ptr, target->lock_type);
                        if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
                    }
                    else {
                        mpi_errno = send_lock_msg(target->target_rank,
                                                  target->lock_type, win_ptr);
                        if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
                    }
                    (*made_progress) = 1;
                    goto fn_exit;
                }
            }
            else if (target->sync.sync_flag == MPIDI_RMA_SYNC_UNLOCK) {
                if (target->pending_op_list != NULL) {
                    /* Capable of piggybacking LOCK message with first operation. */
                    MPIU_Assert(target->pending_op_list->piggyback_lock_candidate);
                }
                else {
                    /* No RMA operation has ever been posted to this target,
                       finish issuing, no need to acquire the lock. Cleanup
                       function will clean it up. */
                    target->sync.outstanding_acks--;
                    MPIU_Assert(target->sync.outstanding_acks == 0);
                    (*made_progress) = 1;

                    /* Unset target's sync_flag. */
                    target->sync.sync_flag = MPIDI_RMA_SYNC_NONE;
                    goto fn_exit;
                }
            }
        }
        else if (target->access_state == MPIDI_RMA_LOCK_ISSUED) {
            if (target->outstanding_lock == 0) {
                target->access_state = MPIDI_RMA_LOCK_GRANTED;
                (*made_progress) = 1;
            }
            else
                goto fn_exit;
        }
    }

    MPIU_Assert(win_ptr->states.access_state == MPIDI_RMA_FENCE_GRANTED ||
                win_ptr->states.access_state == MPIDI_RMA_PSCW_GRANTED ||
                win_ptr->states.access_state == MPIDI_RMA_PER_TARGET ||
                win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_CALLED ||
                win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_GRANTED);

     if (win_ptr->states.access_state == MPIDI_RMA_PER_TARGET ||
        win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_CALLED) {
        MPIU_Assert(target->access_state == MPIDI_RMA_LOCK_CALLED ||
                    target->access_state == MPIDI_RMA_LOCK_GRANTED);
    }

    /* Deal with when there is no operation in the list. */
    if (target->pending_op_list == NULL) {

        /* At this point, per-target state must be LOCK_GRANTED. */
        if (win_ptr->states.access_state == MPIDI_RMA_PER_TARGET ||
            win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_CALLED) {
            MPIU_Assert(target->access_state == MPIDI_RMA_LOCK_GRANTED);
        }

        if (target->sync.sync_flag == MPIDI_RMA_SYNC_FLUSH) {
            if (target->target_rank == rank) {
                target->sync.outstanding_acks--;
                MPIU_Assert(target->sync.outstanding_acks == 0);
            }
            else {
                mpi_errno = send_flush_msg(target->target_rank, win_ptr);
                if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
            }
        }
        else if (target->sync.sync_flag == MPIDI_RMA_SYNC_UNLOCK) {
            if (target->target_rank == rank) {
                mpi_errno = MPIDI_CH3I_Release_lock(win_ptr);
                if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
                target->sync.outstanding_acks--;
                MPIU_Assert(target->sync.outstanding_acks == 0);
            }
            else {
                mpi_errno = send_unlock_msg(target->target_rank, win_ptr);
                if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
            }
        }
        (*made_progress) = 1;
        goto finish_issue;
    }

    /* Issue out operations in the list. */
    first_op = 1;
    curr_op = target->next_op_to_issue;
    while (curr_op != NULL) {
        MPIDI_CH3_Pkt_flags_t flags = MPIDI_CH3_PKT_FLAG_NONE;

        if (target->access_state == MPIDI_RMA_LOCK_ISSUED)
            goto fn_exit;

        if (curr_op->next == NULL &&
            target->sync.sync_flag == MPIDI_RMA_SYNC_NONE) {
            /* skip last OP. */
            goto finish_issue;
        }

        if (first_op) {
            /* piggyback on first OP. */
            if (target->access_state == MPIDI_RMA_LOCK_CALLED) {
                MPIU_Assert(curr_op->piggyback_lock_candidate);
                flags |= MPIDI_CH3_PKT_FLAG_RMA_LOCK;
                target->access_state = MPIDI_RMA_LOCK_ISSUED;
                target->outstanding_lock++;
                MPIU_Assert(target->outstanding_lock == 1);
            }
            first_op = 0;
        }

        if (curr_op->next == NULL) {
            /* piggyback on last OP. */
            if (target->sync.sync_flag == MPIDI_RMA_SYNC_FLUSH) {
                flags |= MPIDI_CH3_PKT_FLAG_RMA_FLUSH;
                if (target->win_complete_flag)
                    flags |= MPIDI_CH3_PKT_FLAG_RMA_DECR_AT_COUNTER;
            }
            else if (target->sync.sync_flag == MPIDI_RMA_SYNC_UNLOCK) {
                flags |= MPIDI_CH3_PKT_FLAG_RMA_UNLOCK;
            }
        }

        target->next_op_to_issue = curr_op->next;

        mpi_errno = issue_rma_op(curr_op, win_ptr, target, flags);
        if (mpi_errno != MPI_SUCCESS)
            MPIU_ERR_POP(mpi_errno);

        if (!curr_op->request) {
            /* Sending is completed immediately. */
            MPIDI_CH3I_RMA_Ops_free_elem(win_ptr, &(target->pending_op_list),
                                         &(target->pending_op_list_tail), curr_op);
        }
        else {
            /* Sending is not completed immediately. */
            MPIDI_CH3I_RMA_Ops_unlink(&(target->pending_op_list),
                                      &(target->pending_op_list_tail), curr_op);
            if (curr_op->is_dt) {
                MPIDI_CH3I_RMA_Ops_append(&(target->dt_op_list),
                                          &(target->dt_op_list_tail), curr_op);
            }
            else if (curr_op->pkt.type == MPIDI_CH3_PKT_PUT ||
                     curr_op->pkt.type == MPIDI_CH3_PKT_ACCUMULATE ||
                     curr_op->pkt.type == MPIDI_CH3_PKT_ACCUM_IMMED) {
                MPIDI_CH3I_RMA_Ops_append(&(target->write_op_list),
                                          &(target->write_op_list_tail), curr_op);
            }
            else {
                MPIDI_CH3I_RMA_Ops_append(&(target->read_op_list),
                                          &(target->read_op_list_tail), curr_op);
            }
386
            win_ptr->active_req_cnt++;
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
        }

        curr_op = target->next_op_to_issue;

        (*made_progress) = 1;
    }

 finish_issue:
    /* Unset target's sync_flag. */
    target->sync.sync_flag = MPIDI_RMA_SYNC_NONE;

  fn_exit:
    return mpi_errno;
  fn_fail:
    goto fn_exit;
}

#undef FUNCNAME
#define FUNCNAME issue_ops_win
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
static inline int issue_ops_win(MPID_Win *win_ptr, int *made_progress)
{
    int mpi_errno = MPI_SUCCESS;
    int start_slot, end_slot, i;
    MPIDI_RMA_Target_t *target = NULL;

    (*made_progress) = 0;

    if (win_ptr->non_empty_slots == 0)
        goto fn_exit;

    MPIU_Assert(win_ptr->states.access_state == MPIDI_RMA_FENCE_GRANTED ||
                win_ptr->states.access_state == MPIDI_RMA_PSCW_GRANTED ||
                win_ptr->states.access_state == MPIDI_RMA_PER_TARGET ||
                win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_CALLED ||
                win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_GRANTED);

    start_slot = win_ptr->comm_ptr->rank % win_ptr->num_slots;
    end_slot = start_slot + win_ptr->num_slots;

    for (i = start_slot; i < end_slot; i++) {
        int idx;
        if (i >= win_ptr->num_slots) idx = i - win_ptr->num_slots;
        else idx = i;

        target = win_ptr->slots[idx].target_list;
        while (target != NULL) {
            int temp = 0;
            mpi_errno = issue_ops_target(win_ptr, target, &temp);
            if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);

            if (temp)
                (*made_progress) = 1;

            target = target->next;
        }
    }

 fn_exit:
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}


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
#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_RMA_Free_ops_before_completion
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPIDI_CH3I_RMA_Free_ops_before_completion(MPID_Win * win_ptr)
{
    MPIDI_RMA_Op_t *curr_op = NULL;
    MPIDI_RMA_Target_t *curr_target = NULL;
    struct MPIDI_RMA_Op **op_list = NULL, **op_list_tail = NULL;
    int read_flag = 0;
    int i, made_progress = 0;
    int mpi_errno = MPI_SUCCESS;

    MPIU_ERR_CHKANDJUMP(win_ptr->non_empty_slots == 0, mpi_errno, MPI_ERR_OTHER,
                        "**rmanoop");

    /* make nonblocking progress once */
    if (win_ptr->states.access_state == MPIDI_RMA_FENCE_ISSUED ||
        win_ptr->states.access_state == MPIDI_RMA_PSCW_ISSUED) {
        mpi_errno = issue_ops_win(win_ptr, &made_progress);
        if (mpi_errno != MPI_SUCCESS) {MPIU_ERR_POP(mpi_errno);}
    }
    if (win_ptr->states.access_state != MPIDI_RMA_FENCE_GRANTED)
        goto fn_exit;

    /* find targets that have operations */
    for (i = 0; i < win_ptr->num_slots; i++) {
        if (win_ptr->slots[i].target_list != NULL) {
            curr_target = win_ptr->slots[i].target_list;
            while (curr_target != NULL && curr_target->read_op_list == NULL
                   && curr_target->write_op_list == NULL)
                curr_target = curr_target->next;
            if (curr_target != NULL) break;
        }
    }
    if (curr_target == NULL) goto fn_exit;

    curr_target->disable_flush_local = 1;

    if (curr_target->read_op_list != NULL) {
        op_list = &curr_target->read_op_list;
        op_list_tail = &curr_target->read_op_list_tail;
        read_flag = 1;
    }
    else {
        op_list = &curr_target->write_op_list;
        op_list_tail = &curr_target->write_op_list_tail;
    }

    /* free all ops in the list since we do not need to maintain them anymore */
    for (curr_op = *op_list; curr_op != NULL; ) {
        MPID_Request_release(curr_op->request);
        MPL_LL_DELETE(*op_list, *op_list_tail, curr_op);
        MPIDI_CH3I_Win_op_free(win_ptr, curr_op);
        if (*op_list == NULL) {
            if (read_flag == 1) {
                op_list = &curr_target->write_op_list;
                op_list = &curr_target->write_op_list_tail;
                read_flag = 0;
            }
        }
        curr_op = *op_list;
   }

 fn_exit:
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}


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
#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_RMA_Cleanup_ops_aggressive
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPIDI_CH3I_RMA_Cleanup_ops_aggressive(MPID_Win * win_ptr)
{
    int i, local_completed = 0, remote_completed = 0;
    int mpi_errno = MPI_SUCCESS;
    MPIDI_RMA_Target_t *curr_target = NULL;
    int made_progress = 0;

    /* If we are in an aggressive cleanup, the window must be holding
     * up resources.  If it isn't, we are in the wrong window and
     * incorrectly entered this function. */
    MPIU_ERR_CHKANDJUMP(win_ptr->non_empty_slots == 0, mpi_errno, MPI_ERR_OTHER,
                        "**rmanoop");

    /* find the first target that has something to issue */
    for (i = 0; i < win_ptr->num_slots; i++) {
        if (win_ptr->slots[i].target_list != NULL) {
            curr_target = win_ptr->slots[i].target_list;
            while (curr_target != NULL && curr_target->pending_op_list == NULL)
                curr_target = curr_target->next;
            if (curr_target != NULL) break;
        }
    }

    if (curr_target == NULL) goto fn_exit;

    if (curr_target->sync.sync_flag < MPIDI_RMA_SYNC_FLUSH_LOCAL)
        curr_target->sync.sync_flag = MPIDI_RMA_SYNC_FLUSH_LOCAL;

    /* Issue out all operations. */
    mpi_errno = MPIDI_CH3I_RMA_Make_progress_target(win_ptr, curr_target->target_rank,
                                                    &made_progress);
    if (mpi_errno != MPI_SUCCESS)
        MPIU_ERR_POP(mpi_errno);

    /* Wait for local completion. */
    do {
        mpi_errno = MPIDI_CH3I_RMA_Cleanup_ops_target(win_ptr, curr_target,
                                                      &local_completed,
                                                      &remote_completed);
        if (mpi_errno != MPI_SUCCESS)
            MPIU_ERR_POP(mpi_errno);
        if (!local_completed) {
            mpi_errno = wait_progress_engine();
            if (mpi_errno != MPI_SUCCESS)
                MPIU_ERR_POP(mpi_errno);
        }
    } while (!local_completed);

  fn_exit:
    return mpi_errno;
  fn_fail:
    goto fn_exit;
}


#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_RMA_Cleanup_target_aggressive
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPIDI_CH3I_RMA_Cleanup_target_aggressive(MPID_Win * win_ptr, MPIDI_RMA_Target_t ** target)
{
    int i, local_completed = 0, remote_completed = 0;
    int made_progress = 0;
    MPIDI_RMA_Target_t *curr_target = NULL;
    int mpi_errno = MPI_SUCCESS;

    (*target) = NULL;

    /* If we are in an aggressive cleanup, the window must be holding
     * up resources.  If it isn't, we are in the wrong window and
     * incorrectly entered this function. */
    MPIU_ERR_CHKANDJUMP(win_ptr->non_empty_slots == 0, mpi_errno, MPI_ERR_OTHER,
                        "**rmanotarget");

    if (win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_CALLED) {
        /* switch to window-wide protocol */
        MPIDI_VC_t *orig_vc = NULL, *target_vc = NULL;
        MPIDI_Comm_get_vc(win_ptr->comm_ptr, win_ptr->comm_ptr->rank, &orig_vc);
        for (i = 0; i < win_ptr->comm_ptr->local_size; i++) {
            if (i == win_ptr->comm_ptr->rank)
                continue;
            MPIDI_Comm_get_vc(win_ptr->comm_ptr, i, &target_vc);
            if (orig_vc->node_id != target_vc->node_id) {
                mpi_errno = MPIDI_CH3I_Win_find_target(win_ptr, i, &curr_target);
                if (mpi_errno) MPIU_ERR_POP(mpi_errno);
                if (curr_target == NULL) {
                    win_ptr->outstanding_locks++;
                    mpi_errno = send_lock_msg(i, MPI_LOCK_SHARED, win_ptr);
                    if (mpi_errno != MPI_SUCCESS)
                        MPIU_ERR_POP(mpi_errno);
                }
            }
        }
        win_ptr->states.access_state = MPIDI_RMA_LOCK_ALL_ISSUED;
    }

    do {
        /* find a non-empty slot and set the FLUSH flag on the first
         * target */
        /* TODO: we should think about better strategies on selecting the target */
        for (i = 0; i < win_ptr->num_slots; i++)
            if (win_ptr->slots[i].target_list != NULL)
                break;
        curr_target = win_ptr->slots[i].target_list;
        if (curr_target->sync.sync_flag < MPIDI_RMA_SYNC_FLUSH) {
            curr_target->sync.sync_flag = MPIDI_RMA_SYNC_FLUSH;
            curr_target->sync.have_remote_incomplete_ops = 0;
            curr_target->sync.outstanding_acks++;
        }

        /* Issue out all operations. */
        mpi_errno = MPIDI_CH3I_RMA_Make_progress_target(win_ptr, curr_target->target_rank,
                                                        &made_progress);
        if (mpi_errno != MPI_SUCCESS)
            MPIU_ERR_POP(mpi_errno);

        /* Wait for remote completion. */
        do {
            mpi_errno = MPIDI_CH3I_RMA_Cleanup_ops_target(win_ptr, curr_target,
                                                          &local_completed,
                                                          &remote_completed);
            if (mpi_errno != MPI_SUCCESS)
                MPIU_ERR_POP(mpi_errno);
            if (!remote_completed) {
                mpi_errno = wait_progress_engine();
                if (mpi_errno != MPI_SUCCESS)
                    MPIU_ERR_POP(mpi_errno);
            }
        } while (!remote_completed);

        /* Cleanup the target. */
        mpi_errno = MPIDI_CH3I_RMA_Cleanup_single_target(win_ptr, curr_target);
        if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);

        /* check if we got a target */
        (*target) = MPIDI_CH3I_Win_target_alloc(win_ptr);

    } while ((*target) == NULL);

  fn_exit:
    return mpi_errno;
  fn_fail:
    goto fn_exit;
}


674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
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
#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_RMA_Make_progress_target
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPIDI_CH3I_RMA_Make_progress_target(MPID_Win * win_ptr, int target_rank, int *made_progress)
{
    int mpi_errno = MPI_SUCCESS;
    int cannot_issue = 0, temp_progress = 0;
    MPIDI_RMA_Slot_t *slot;
    MPIDI_RMA_Target_t *target;

    (*made_progress) = 0;

    if (win_ptr->num_slots < win_ptr->comm_ptr->local_size) {
        slot = &(win_ptr->slots[target_rank % win_ptr->num_slots]);
        for (target = slot->target_list;
             target && target->target_rank != target_rank; target = target->next);
    }
    else {
        slot = &(win_ptr->slots[target_rank]);
        target = slot->target_list;
    }

    if (target != NULL) {

        /* check window state */
        mpi_errno = check_window_state(win_ptr, &temp_progress, &cannot_issue);
        if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);

        if (temp_progress)
            (*made_progress) = 1;

        if (cannot_issue)
            goto fn_exit;

        mpi_errno = issue_ops_target(win_ptr, target, &temp_progress);
        if (mpi_errno)
            MPIU_ERR_POP(mpi_errno);

        if (temp_progress)
            (*made_progress) = 1;
    }

  fn_exit:
    return mpi_errno;
  fn_fail:
    goto fn_exit;
}


#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_RMA_Make_progress_win
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPIDI_CH3I_RMA_Make_progress_win(MPID_Win * win_ptr, int *made_progress)
{
    int temp_progress = 0, cannot_issue = 0;
    int mpi_errno = MPI_SUCCESS;

    (*made_progress) = 0;

    /* check window state */
    mpi_errno = check_window_state(win_ptr, &temp_progress, &cannot_issue);
    if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);

    if (temp_progress)
        (*made_progress) = 1;

    if (cannot_issue)
        goto fn_exit;

    mpi_errno = issue_ops_win(win_ptr, &temp_progress);
    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);

    if (temp_progress)
        (*made_progress) = 1;

  fn_exit:
    return mpi_errno;
  fn_fail:
    goto fn_exit;
}


#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_RMA_Make_progress_global
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPIDI_CH3I_RMA_Make_progress_global(int *made_progress)
{
    MPIDI_RMA_Win_list_t *win_elem = MPIDI_RMA_Win_list;
    int tmp = 0, cannot_issue = 0;
    int mpi_errno = MPI_SUCCESS;

    (*made_progress) = 0;

    for (win_elem = MPIDI_RMA_Win_list; win_elem; win_elem = win_elem->next) {
        if (win_elem->win_ptr->states.access_state == MPIDI_RMA_FENCE_ISSUED ||
            win_elem->win_ptr->states.access_state == MPIDI_RMA_PSCW_ISSUED ||
            win_elem->win_ptr->states.access_state == MPIDI_RMA_PER_TARGET ||
            win_elem->win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_CALLED ||
            win_elem->win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_ISSUED ||
            win_elem->win_ptr->states.access_state == MPIDI_RMA_LOCK_ALL_GRANTED) {

            /* check window state */
            mpi_errno = check_window_state(win_elem->win_ptr, &tmp, &cannot_issue);
            if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);

            if (tmp)
                (*made_progress) = 1;

            if (cannot_issue)
                continue;

            mpi_errno = issue_ops_win(win_elem->win_ptr, &tmp);
            if (mpi_errno)
                MPIU_ERR_POP(mpi_errno);
            if (tmp)
                (*made_progress) = 1;
        }
    }

  fn_exit:
    return mpi_errno;
  fn_fail:
    goto fn_exit;
}


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
/* create_datatype() creates a new struct datatype for the dtype_info
   and the dataloop of the target datatype together with the user data */
#undef FUNCNAME
#define FUNCNAME create_datatype
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
static int create_datatype(const MPIDI_RMA_dtype_info * dtype_info,
                           const void *dataloop, MPI_Aint dataloop_sz,
                           const void *o_addr, int o_count, MPI_Datatype o_datatype,
                           MPID_Datatype ** combined_dtp)
{
    int mpi_errno = MPI_SUCCESS;
    /* datatype_set_contents wants an array 'ints' which is the
     * blocklens array with count prepended to it.  So blocklens
     * points to the 2nd element of ints to avoid having to copy
     * blocklens into ints later. */
    int ints[4];
    int *blocklens = &ints[1];
    MPI_Aint displaces[3];
    MPI_Datatype datatypes[3];
    const int count = 3;
    MPI_Datatype combined_datatype;
    MPIDI_STATE_DECL(MPID_STATE_CREATE_DATATYPE);

    MPIDI_FUNC_ENTER(MPID_STATE_CREATE_DATATYPE);

    /* create datatype */
    displaces[0] = MPIU_PtrToAint(dtype_info);
    blocklens[0] = sizeof(*dtype_info);
    datatypes[0] = MPI_BYTE;

    displaces[1] = MPIU_PtrToAint(dataloop);
    MPIU_Assign_trunc(blocklens[1], dataloop_sz, int);
    datatypes[1] = MPI_BYTE;

    displaces[2] = MPIU_PtrToAint(o_addr);
    blocklens[2] = o_count;
    datatypes[2] = o_datatype;

    mpi_errno = MPID_Type_struct(count, blocklens, displaces, datatypes, &combined_datatype);
    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);

    ints[0] = count;

    MPID_Datatype_get_ptr(combined_datatype, *combined_dtp);
    mpi_errno = MPID_Datatype_set_contents(*combined_dtp, MPI_COMBINER_STRUCT, count + 1,       /* ints (cnt,blklen) */
                                           count,       /* aints (disps) */
                                           count,       /* types */
                                           ints, displaces, datatypes);
    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);

    /* Commit datatype */

    MPID_Dataloop_create(combined_datatype,
                         &(*combined_dtp)->dataloop,
                         &(*combined_dtp)->dataloop_size,
                         &(*combined_dtp)->dataloop_depth, MPID_DATALOOP_HOMOGENEOUS);

    /* create heterogeneous dataloop */
    MPID_Dataloop_create(combined_datatype,
                         &(*combined_dtp)->hetero_dloop,
                         &(*combined_dtp)->hetero_dloop_size,
                         &(*combined_dtp)->hetero_dloop_depth, MPID_DATALOOP_HETEROGENEOUS);

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


#undef FUNCNAME
#define FUNCNAME send_rma_msg
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
882
static int send_rma_msg(MPIDI_RMA_Op_t * rma_op, MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags)
883
{
884
885
    MPIDI_CH3_Pkt_put_t *put_pkt = &rma_op->pkt.put;
    MPIDI_CH3_Pkt_accum_t *accum_pkt = &rma_op->pkt.accum;
Xin Zhao's avatar
Xin Zhao committed
886
    MPIDI_CH3_Pkt_get_accum_t *get_accum_pkt = &rma_op->pkt.get_accum;
887
888
889
890
891
892
893
    MPID_IOV iov[MPID_IOV_LIMIT];
    int mpi_errno = MPI_SUCCESS;
    int origin_dt_derived, target_dt_derived, iovcnt;
    MPI_Aint origin_type_size;
    MPIDI_VC_t *vc;
    MPID_Comm *comm_ptr;
    MPID_Datatype *target_dtp = NULL, *origin_dtp = NULL;
894
    MPI_Datatype target_datatype;
895
896
897
898
899
900
901
    MPID_Request *resp_req = NULL;
    MPIU_CHKPMEM_DECL(1);
    MPIDI_STATE_DECL(MPID_STATE_SEND_RMA_MSG);
    MPIDI_STATE_DECL(MPID_STATE_MEMCPY);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_SEND_RMA_MSG);

902
    rma_op->request = NULL;
903

904
    if (rma_op->pkt.type == MPIDI_CH3_PKT_PUT) {
905
906
907
908
909
        put_pkt->flags = flags;

        iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) put_pkt;
        iov[0].MPID_IOV_LEN = sizeof(*put_pkt);
    }
910
    else if (rma_op->pkt.type == MPIDI_CH3_PKT_GET_ACCUM) {
911
912
913
914
915
916
917
918
919
920
921
        /* Create a request for the GACC response.  Store the response buf, count, and
         * datatype in it, and pass the request's handle in the GACC packet. When the
         * response comes from the target, it will contain the request handle. */
        resp_req = MPID_Request_create();
        MPIU_ERR_CHKANDJUMP(resp_req == NULL, mpi_errno, MPI_ERR_OTHER, "**nomemreq");

        MPIU_Object_set_ref(resp_req, 2);

        resp_req->dev.user_buf = rma_op->result_addr;
        resp_req->dev.user_count = rma_op->result_count;
        resp_req->dev.datatype = rma_op->result_datatype;
Xin Zhao's avatar
Xin Zhao committed
922
923
        resp_req->dev.target_win_handle = get_accum_pkt->target_win_handle;
        resp_req->dev.source_win_handle = get_accum_pkt->source_win_handle;
924
925
926
927
928
929
930
931
932

        if (!MPIR_DATATYPE_IS_PREDEFINED(resp_req->dev.datatype)) {
            MPID_Datatype *result_dtp = NULL;
            MPID_Datatype_get_ptr(resp_req->dev.datatype, result_dtp);
            resp_req->dev.datatype_ptr = result_dtp;
            /* this will cause the datatype to be freed when the
             * request is freed. */
        }

Xin Zhao's avatar
Xin Zhao committed
933
        get_accum_pkt->request_handle = resp_req->handle;
934

Xin Zhao's avatar
Xin Zhao committed
935
936
937
        get_accum_pkt->flags = flags;
        iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) get_accum_pkt;
        iov[0].MPID_IOV_LEN = sizeof(*get_accum_pkt);
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
    }
    else {
        accum_pkt->flags = flags;

        iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) accum_pkt;
        iov[0].MPID_IOV_LEN = sizeof(*accum_pkt);
    }

    /*    printf("send pkt: type %d, addr %d, count %d, base %d\n", rma_pkt->type,
     * rma_pkt->addr, rma_pkt->count, win_ptr->base_addrs[rma_op->target_rank]);
     * fflush(stdout);
     */

    comm_ptr = win_ptr->comm_ptr;
    MPIDI_Comm_get_vc_set_active(comm_ptr, rma_op->target_rank, &vc);

    if (!MPIR_DATATYPE_IS_PREDEFINED(rma_op->origin_datatype)) {
        origin_dt_derived = 1;
        MPID_Datatype_get_ptr(rma_op->origin_datatype, origin_dtp);
    }
    else {
        origin_dt_derived = 0;
    }

962
963
    MPIDI_CH3_PKT_RMA_GET_TARGET_DATATYPE(rma_op->pkt, target_datatype, mpi_errno);
    if (!MPIR_DATATYPE_IS_PREDEFINED(target_datatype)) {
964
        target_dt_derived = 1;
965
        MPID_Datatype_get_ptr(target_datatype, target_dtp);
966
967
968
969
970
971
972
    }
    else {
        target_dt_derived = 0;
    }

    if (target_dt_derived) {
        /* derived datatype on target. fill derived datatype info */
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
        rma_op->dtype_info.is_contig = target_dtp->is_contig;
        rma_op->dtype_info.max_contig_blocks = target_dtp->max_contig_blocks;
        rma_op->dtype_info.size = target_dtp->size;
        rma_op->dtype_info.extent = target_dtp->extent;
        rma_op->dtype_info.dataloop_size = target_dtp->dataloop_size;
        rma_op->dtype_info.dataloop_depth = target_dtp->dataloop_depth;
        rma_op->dtype_info.eltype = target_dtp->eltype;
        rma_op->dtype_info.dataloop = target_dtp->dataloop;
        rma_op->dtype_info.ub = target_dtp->ub;
        rma_op->dtype_info.lb = target_dtp->lb;
        rma_op->dtype_info.true_ub = target_dtp->true_ub;
        rma_op->dtype_info.true_lb = target_dtp->true_lb;
        rma_op->dtype_info.has_sticky_ub = target_dtp->has_sticky_ub;
        rma_op->dtype_info.has_sticky_lb = target_dtp->has_sticky_lb;

        MPIU_CHKPMEM_MALLOC(rma_op->dataloop, void *, target_dtp->dataloop_size,
                            mpi_errno, "dataloop");
990
991

        MPIDI_FUNC_ENTER(MPID_STATE_MEMCPY);
992
        MPIU_Memcpy(rma_op->dataloop, target_dtp->dataloop, target_dtp->dataloop_size);
993
994
995
        MPIDI_FUNC_EXIT(MPID_STATE_MEMCPY);
        /* the dataloop can have undefined padding sections, so we need to let
         * valgrind know that it is OK to pass this data to writev later on */
996
        MPL_VG_MAKE_MEM_DEFINED(rma_op->dataloop, target_dtp->dataloop_size);
997

998
        if (rma_op->pkt.type == MPIDI_CH3_PKT_PUT) {
999
1000
            put_pkt->dataloop_size = target_dtp->dataloop_size;
        }
Xin Zhao's avatar
Xin Zhao committed
1001
        else if (rma_op->pkt.type == MPIDI_CH3_PKT_ACCUMULATE) {
1002
1003
            accum_pkt->dataloop_size = target_dtp->dataloop_size;
        }
Xin Zhao's avatar
Xin Zhao committed
1004
1005
1006
        else {
            get_accum_pkt->dataloop_size = target_dtp->dataloop_size;
        }
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
    }

    MPID_Datatype_get_size_macro(rma_op->origin_datatype, origin_type_size);

    if (!target_dt_derived) {
        /* basic datatype on target */
        if (!origin_dt_derived) {
            /* basic datatype on origin */
            iov[1].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) rma_op->origin_addr;
            iov[1].MPID_IOV_LEN = rma_op->origin_count * origin_type_size;
            iovcnt = 2;
            MPIU_THREAD_CS_ENTER(CH3COMM, vc);
1019
            mpi_errno = MPIDI_CH3_iStartMsgv(vc, iov, iovcnt, &rma_op->request);
1020
1021
1022
1023
1024
            MPIU_THREAD_CS_EXIT(CH3COMM, vc);
            MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
        }
        else {
            /* derived datatype on origin */
1025
1026
            rma_op->request = MPID_Request_create();
            MPIU_ERR_CHKANDJUMP(rma_op->request == NULL, mpi_errno, MPI_ERR_OTHER, "**nomemreq");
1027

1028
1029
            MPIU_Object_set_ref(rma_op->request, 2);
            rma_op->request->kind = MPID_REQUEST_SEND;
1030

1031
1032
            rma_op->request->dev.segment_ptr = MPID_Segment_alloc();
            MPIU_ERR_CHKANDJUMP1(rma_op->request->dev.segment_ptr == NULL, mpi_errno, MPI_ERR_OTHER,
1033
1034
                                 "**nomem", "**nomem %s", "MPID_Segment_alloc");

1035
            rma_op->request->dev.datatype_ptr = origin_dtp;
1036
1037
1038
            /* this will cause the datatype to be freed when the request
             * is freed. */
            MPID_Segment_init(rma_op->origin_addr, rma_op->origin_count,
1039
1040
1041
                              rma_op->origin_datatype, rma_op->request->dev.segment_ptr, 0);
            rma_op->request->dev.segment_first = 0;
            rma_op->request->dev.segment_size = rma_op->origin_count * origin_type_size;
1042

1043
1044
            rma_op->request->dev.OnFinal = 0;
            rma_op->request->dev.OnDataAvail = 0;
1045
1046
1047

            MPIU_THREAD_CS_ENTER(CH3COMM, vc);
            mpi_errno =
1048
                vc->sendNoncontig_fn(vc, rma_op->request, iov[0].MPID_IOV_BUF, iov[0].MPID_IOV_LEN);
1049
1050
1051
1052
1053
1054
1055
1056
            MPIU_THREAD_CS_EXIT(CH3COMM, vc);
            MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
        }
    }
    else {
        /* derived datatype on target */
        MPID_Datatype *combined_dtp = NULL;

1057
1058
        rma_op->request = MPID_Request_create();
        if (rma_op->request == NULL) {
1059
1060
1061
            MPIU_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**nomemreq");
        }

1062
1063
        MPIU_Object_set_ref(rma_op->request, 2);
        rma_op->request->kind = MPID_REQUEST_SEND;
1064

1065
1066
        rma_op->request->dev.segment_ptr = MPID_Segment_alloc();
        MPIU_ERR_CHKANDJUMP1(rma_op->request->dev.segment_ptr == NULL, mpi_errno, MPI_ERR_OTHER,
1067
1068
1069
1070
1071
                             "**nomem", "**nomem %s", "MPID_Segment_alloc");

        /* create a new datatype containing the dtype_info, dataloop, and origin data */

        mpi_errno =
1072
1073
1074
            create_datatype(&rma_op->dtype_info, rma_op->dataloop, target_dtp->dataloop_size,
                            rma_op->origin_addr, rma_op->origin_count, rma_op->origin_datatype,
                            &combined_dtp);
1075
1076
1077
        if (mpi_errno)
            MPIU_ERR_POP(mpi_errno);

1078
        rma_op->request->dev.datatype_ptr = combined_dtp;
1079
1080
        /* combined_datatype will be freed when request is freed */

1081
1082
1083
        MPID_Segment_init(MPI_BOTTOM, 1, combined_dtp->handle, rma_op->request->dev.segment_ptr, 0);
        rma_op->request->dev.segment_first = 0;
        rma_op->request->dev.segment_size = combined_dtp->size;
1084

1085
1086
        rma_op->request->dev.OnFinal = 0;
        rma_op->request->dev.OnDataAvail = 0;
1087
1088

        MPIU_THREAD_CS_ENTER(CH3COMM, vc);
1089
1090
        mpi_errno =
            vc->sendNoncontig_fn(vc, rma_op->request, iov[0].MPID_IOV_BUF, iov[0].MPID_IOV_LEN);
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
        MPIU_THREAD_CS_EXIT(CH3COMM, vc);
        MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");

        /* we're done with the datatypes */
        if (origin_dt_derived)
            MPID_Datatype_release(origin_dtp);
        MPID_Datatype_release(target_dtp);
    }

    /* This operation can generate two requests; one for inbound and one for
     * outbound data. */
    if (resp_req != NULL) {
1103
        if (rma_op->request != NULL) {
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
            /* If we have both inbound and outbound requests (i.e. GACC
             * operation), we need to ensure that the source buffer is
             * available and that the response data has been received before
             * informing the origin that this operation is complete.  Because
             * the update needs to be done atomically at the target, they will
             * not send back data until it has been received.  Therefore,
             * completion of the response request implies that the send request
             * has completed.
             *
             * Therefore: refs on the response request are set to two: one is
             * held by the progress engine and the other by the RMA op
             * completion code.  Refs on the outbound request are set to one;
             * it will be completed by the progress engine.
             */

1119
1120
            MPID_Request_release(rma_op->request);
            rma_op->request = resp_req;
1121
1122
1123

        }
        else {
1124
            rma_op->request = resp_req;
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
        }

        /* For error checking */
        resp_req = NULL;
    }

  fn_exit:
    MPIU_CHKPMEM_COMMIT();
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_SEND_RMA_MSG);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
    if (resp_req) {
        MPID_Request_release(resp_req);
    }
1140
    if (rma_op->request) {
1141
        MPIU_CHKPMEM_REAP();
1142
1143
1144
        if (rma_op->request->dev.datatype_ptr)
            MPID_Datatype_release(rma_op->request->dev.datatype_ptr);
        MPID_Request_release(rma_op->request);
1145
    }
1146
    rma_op->request = NULL;
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}

/*
 * Use this for contiguous accumulate operations
 */
#undef FUNCNAME
#define FUNCNAME send_contig_acc_msg
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
static int send_contig_acc_msg(MPIDI_RMA_Op_t * rma_op,
1159
                               MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags)
1160
{
1161
    MPIDI_CH3_Pkt_accum_t *accum_pkt = &rma_op->pkt.accum;
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
    MPID_IOV iov[MPID_IOV_LIMIT];
    int mpi_errno = MPI_SUCCESS;
    int iovcnt;
    MPI_Aint origin_type_size;
    MPIDI_VC_t *vc;
    MPID_Comm *comm_ptr;
    size_t len;
    MPIDI_STATE_DECL(MPID_STATE_SEND_CONTIG_ACC_MSG);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_SEND_CONTIG_ACC_MSG);

1173
    rma_op->request = NULL;
1174
1175
1176
1177
1178

    MPID_Datatype_get_size_macro(rma_op->origin_datatype, origin_type_size);
    /* FIXME: Make this size check efficient and match the packet type */
    MPIU_Assign_trunc(len, rma_op->origin_count * origin_type_size, size_t);
    if (MPIR_CVAR_CH3_RMA_ACC_IMMED && len <= MPIDI_RMA_IMMED_INTS * sizeof(int)) {
1179
        MPIDI_CH3_Pkt_accum_immed_t *accumi_pkt = &rma_op->pkt.accum_immed;
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
        void *dest = accumi_pkt->data, *src = rma_op->origin_addr;

        accumi_pkt->flags = flags;

        switch (len) {
        case 1:
            *(uint8_t *) dest = *(uint8_t *) src;
            break;
        case 2:
            *(uint16_t *) dest = *(uint16_t *) src;
            break;
        case 4:
            *(uint32_t *) dest = *(uint32_t *) src;
            break;
        case 8:
            *(uint64_t *) dest = *(uint64_t *) src;
            break;
        default:
            MPIU_Memcpy(accumi_pkt->data, (void *) rma_op->origin_addr, len);
        }
        comm_ptr = win_ptr->comm_ptr;
        MPIDI_Comm_get_vc_set_active(comm_ptr, rma_op->target_rank, &vc);
        MPIU_THREAD_CS_ENTER(CH3COMM, vc);
1203
        mpi_errno = MPIDI_CH3_iStartMsg(vc, accumi_pkt, sizeof(*accumi_pkt), &rma_op->request);
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
        MPIU_THREAD_CS_EXIT(CH3COMM, vc);
        MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
        goto fn_exit;
    }

    accum_pkt->flags = flags;

    iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) accum_pkt;
    iov[0].MPID_IOV_LEN = sizeof(*accum_pkt);

    /*    printf("send pkt: type %d, addr %d, count %d, base %d\n", rma_pkt->type,
     * rma_pkt->addr, rma_pkt->count, win_ptr->base_addrs[rma_op->target_rank]);
     * fflush(stdout);
     */

    comm_ptr = win_ptr->comm_ptr;
    MPIDI_Comm_get_vc_set_active(comm_ptr, rma_op->target_rank, &vc);


    /* basic datatype on target */
    /* basic datatype on origin */
    /* FIXME: This is still very heavyweight for a small message operation,
     * such as a single word update */
    /* One possibility is to use iStartMsg with a buffer that is just large
     * enough, though note that nemesis has an optimization for this */
    iov[1].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) rma_op->origin_addr;
    iov[1].MPID_IOV_LEN = rma_op->origin_count * origin_type_size;
    iovcnt = 2;
    MPIU_THREAD_CS_ENTER(CH3COMM, vc);
1233
    mpi_errno = MPIDI_CH3_iStartMsgv(vc, iov, iovcnt, &rma_op->request);
1234
1235
1236
1237
1238
1239
1240
1241
    MPIU_THREAD_CS_EXIT(CH3COMM, vc);
    MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");

  fn_exit:
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_SEND_CONTIG_ACC_MSG);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
1242
1243
    if (rma_op->request) {
        MPID_Request_release(rma_op->request);
1244
    }
1245
    rma_op->request = NULL;
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}


/*
 * Initiate an immediate RMW accumulate operation
 */
#undef FUNCNAME
#define FUNCNAME send_immed_rmw_msg
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
static int send_immed_rmw_msg(MPIDI_RMA_Op_t * rma_op,
1259
                              MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags)
1260
1261
{
    int mpi_errno = MPI_SUCCESS;
1262
    MPID_Request *rmw_req = NULL;
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
    MPIDI_VC_t *vc;
    MPID_Comm *comm_ptr;
    MPI_Aint len;
    MPIDI_STATE_DECL(MPID_STATE_SEND_IMMED_RMW_MSG);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_SEND_IMMED_RMW_MSG);

    /* Create a request for the RMW response.  Store the origin buf, count, and
     * datatype in it, and pass the request's handle RMW packet. When the
     * response comes from the target, it will contain the request handle. */
1273
1274
    rma_op->request = MPID_Request_create();
    MPIU_ERR_CHKANDJUMP(rma_op->request == NULL, mpi_errno, MPI_ERR_OTHER, "**nomemreq");
1275
1276
1277

    /* Set refs on the request to 2: one for the response message, and one for
     * the partial completion handler */
1278
    MPIU_Object_set_ref(rma_op->request, 2);
1279

1280
1281
1282
    rma_op->request->dev.user_buf = rma_op->result_addr;
    rma_op->request->dev.user_count = rma_op->result_count;
    rma_op->request->dev.datatype = rma_op->result_datatype;
1283
1284
1285
1286
1287
1288

    /* REQUIRE: All datatype arguments must be of the same, builtin
     * type and counts must be 1. */
    MPID_Datatype_get_size_macro(rma_op->origin_datatype, len);
    comm_ptr = win_ptr->comm_ptr;

1289
1290
    if (rma_op->pkt.type == MPIDI_CH3_PKT_CAS) {
        MPIDI_CH3_Pkt_cas_t *cas_pkt = &rma_op->pkt.cas;
1291
1292
1293

        MPIU_Assert(len <= sizeof(MPIDI_CH3_CAS_Immed_u));

1294
1295
        rma_op->request->dev.target_win_handle = cas_pkt->target_win_handle;
        rma_op->request->dev.source_win_handle = cas_pkt->source_win_handle;
1296

1297
        cas_pkt->request_handle = rma_op->request->handle;
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
        cas_pkt->flags = flags;

        MPIU_Memcpy((void *) &cas_pkt->origin_data, rma_op->origin_addr, len);
        MPIU_Memcpy((void *) &cas_pkt->compare_data, rma_op->compare_addr, len);

        MPIDI_Comm_get_vc_set_active(comm_ptr, rma_op->target_rank, &vc);
        MPIU_THREAD_CS_ENTER(CH3COMM, vc);
        mpi_errno = MPIDI_CH3_iStartMsg(vc, cas_pkt, sizeof(*cas_pkt), &rmw_req);
        MPIU_THREAD_CS_EXIT(CH3COMM, vc);
        MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");

        if (rmw_req != NULL) {
            MPID_Request_release(rmw_req);
        }
    }

1314
1315
    else if (rma_op->pkt.type == MPIDI_CH3_PKT_FOP) {
        MPIDI_CH3_Pkt_fop_t *fop_pkt = &rma_op->pkt.fop;
1316
1317
1318

        MPIU_Assert(len <= sizeof(MPIDI_CH3_FOP_Immed_u));

1319
1320
        rma_op->request->dev.target_win_handle = fop_pkt->target_win_handle;
        rma_op->request->dev.source_win_handle = fop_pkt->source_win_handle;
1321

1322
        fop_pkt->request_handle = rma_op->request->handle;
1323
1324
        fop_pkt->flags = flags;

1325
        if (len <= sizeof(fop_pkt->origin_data) || fop_pkt->op == MPI_NO_OP) {
1326
            /* Embed FOP data in the packet header */
1327
            if (fop_pkt->op != MPI_NO_OP) {
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
                MPIU_Memcpy(fop_pkt->origin_data, rma_op->origin_addr, len);
            }

            MPIDI_Comm_get_vc_set_active(comm_ptr, rma_op->target_rank, &vc);
            MPIU_THREAD_CS_ENTER(CH3COMM, vc);
            mpi_errno = MPIDI_CH3_iStartMsg(vc, fop_pkt, sizeof(*fop_pkt), &rmw_req);
            MPIU_THREAD_CS_EXIT(CH3COMM, vc);
            MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");

            if (rmw_req != NULL) {
                MPID_Request_release(rmw_req);
            }
        }
        else {
            /* Data is too big to copy into the FOP header, use an IOV to send it */
            MPID_IOV iov[MPID_IOV_LIMIT];

            rmw_req = MPID_Request_create();
            MPIU_ERR_CHKANDJUMP(rmw_req == NULL, mpi_errno, MPI_ERR_OTHER, "**nomemreq");
            MPIU_Object_set_ref(rmw_req, 1);

            rmw_req->dev.OnFinal = 0;
            rmw_req->dev.OnDataAvail = 0;

            iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) fop_pkt;
            iov[0].MPID_IOV_LEN = sizeof(*fop_pkt);
            iov[1].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) rma_op->origin_addr;
            iov[1].MPID_IOV_LEN = len;  /* count == 1 */

            MPIDI_Comm_get_vc_set_active(comm_ptr, rma_op->target_rank, &vc);
            MPIU_THREAD_CS_ENTER(CH3COMM, vc);
            mpi_errno = MPIDI_CH3_iSendv(vc, rmw_req, iov, 2);
            MPIU_THREAD_CS_EXIT(CH3COMM, vc);

            MPIU_ERR_CHKANDJUMP(mpi_errno != MPI_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
        }
    }
    else {
        MPIU_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
    }

  fn_exit:
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_SEND_IMMED_RMW_MSG);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
1374
1375
    if (rma_op->request) {
        MPID_Request_release(rma_op->request);
1376
    }
1377
    rma_op->request = NULL;
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
    if (rmw_req) {
        MPID_Request_release(rmw_req);
    }
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}



#undef FUNCNAME
#define FUNCNAME recv_rma_msg
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
1391
static int recv_rma_msg(MPIDI_RMA_Op_t * rma_op, MPID_Win * win_ptr, MPIDI_CH3_Pkt_flags_t flags)
1392
{
1393
    MPIDI_CH3_Pkt_get_t *get_pkt = &rma_op->pkt.get;
1394
1395
1396
1397
    int mpi_errno = MPI_SUCCESS;
    MPIDI_VC_t *vc;
    MPID_Comm *comm_ptr;
    MPID_Datatype *dtp;
1398
1399
    MPI_Datatype target_datatype;
    MPID_Request *req = NULL;
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
    MPID_IOV iov[MPID_IOV_LIMIT];
    MPIU_CHKPMEM_DECL(1);
    MPIDI_STATE_DECL(MPID_STATE_RECV_RMA_MSG);
    MPIDI_STATE_DECL(MPID_STATE_MEMCPY);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_RECV_RMA_MSG);

    /* create a request, store the origin buf, cnt, datatype in it,
     * and pass a handle to it in the get packet. When the get
     * response comes from the target, it will contain the request
     * handle. */
1411
1412
    rma_op->request = MPID_Request_create();
    if (rma_op->request == NULL) {
1413
1414
1415
        MPIU_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**nomemreq");
    }

1416
    MPIU_Object_set_ref(rma_op->request, 2);
1417

1418
1419
1420
1421
1422
1423
1424
1425
    rma_op->request->dev.user_buf = rma_op->origin_addr;
    rma_op->request->dev.user_count = rma_op->origin_count;
    rma_op->request->dev.datatype = rma_op->origin_datatype;
    rma_op->request->dev.target_win_handle = MPI_WIN_NULL;
    rma_op->request->dev.source_win_handle = get_pkt->source_win_handle;
    if (!MPIR_DATATYPE_IS_PREDEFINED(rma_op->request->dev.datatype)) {
        MPID_Datatype_get_ptr(rma_op->request->dev.datatype, dtp);
        rma_op->request->dev.datatype_ptr = dtp;
1426
1427
1428
1429
        /* this will cause the datatype to be freed when the
         * request is freed. */
    }

1430
    get_pkt->request_handle = rma_op->request->handle;
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
    get_pkt->flags = flags;

/*    printf("send pkt: type %d, addr %d, count %d, base %d\n", rma_pkt->type,
           rma_pkt->addr, rma_pkt->count, win_ptr->base_addrs[rma_op->target_rank]);
    fflush(stdout);
*/

    comm_ptr = win_ptr->comm_ptr;
    MPIDI_Comm_get_vc_set_active(comm_ptr, rma_op->target_rank, &vc);

1441
1442
    MPIDI_CH3_PKT_RMA_GET_TARGET_DATATYPE(rma_op->pkt, target_datatype, mpi_errno);
    if (MPIR_DATATYPE_IS_PREDEFINED(target_datatype)) {
1443
1444
1445
1446
1447
1448
1449
1450
1451
        /* basic datatype on target. simply send the get_pkt. */
        MPIU_THREAD_CS_ENTER(CH3COMM, vc);
        mpi_errno = MPIDI_CH3_iStartMsg(vc, get_pkt, sizeof(*get_pkt), &req);
        MPIU_THREAD_CS_EXIT(CH3COMM, vc);
    }
    else {
        /* derived datatype on target. fill derived datatype info and
         * send it along with get_pkt. */

1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
        MPID_Datatype_get_ptr(target_datatype, dtp);
        rma_op->dtype_info.is_contig = dtp->is_contig;
        rma_op->dtype_info.max_contig_blocks = dtp->max_contig_blocks;
        rma_op->dtype_info.size = dtp->size;
        rma_op->dtype_info.extent = dtp->extent;
        rma_op->dtype_info.dataloop_size = dtp->dataloop_size;
        rma_op->dtype_info.dataloop_depth = dtp->dataloop_depth;
        rma_op->dtype_info.eltype = dtp->eltype;
        rma_op->dtype_info.dataloop = dtp->dataloop;
        rma_op->dtype_info.ub = dtp->ub;
        rma_op->dtype_info.lb = dtp->lb;
        rma_op->dtype_info.true_ub = dtp->true_ub;
        rma_op->dtype_info.true_lb = dtp->true_lb;
        rma_op->dtype_info.has_sticky_ub = dtp->has_sticky_ub;
        rma_op->dtype_info.has_sticky_lb = dtp->has_sticky_lb;

        MPIU_CHKPMEM_MALLOC(rma_op->dataloop, void *, dtp->dataloop_size, mpi_errno, "dataloop");
1469
1470

        MPIDI_FUNC_ENTER(MPID_STATE_MEMCPY);
1471
        MPIU_Memcpy(rma_op->dataloop, dtp->dataloop, dtp->dataloop_size);
1472
1473
1474
1475
        MPIDI_FUNC_EXIT(MPID_STATE_MEMCPY);

        /* the dataloop can have undefined padding sections, so we need to let
         * valgrind know that it is OK to pass this data to writev later on */
1476
        MPL_VG_MAKE_MEM_DEFINED(rma_op->dataloop, dtp->dataloop_size);
1477
1478
1479
1480
1481

        get_pkt->dataloop_size = dtp->dataloop_size;

        iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) get_pkt;
        iov[0].MPID_IOV_LEN = sizeof(*get_pkt);
1482
1483
1484
        iov[1].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) & rma_op->dtype_info;
        iov[1].MPID_IOV_LEN = sizeof(rma_op->dtype_info);
        iov[2].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) rma_op->dataloop;
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
        iov[2].MPID_IOV_LEN = dtp->dataloop_size;

        MPIU_THREAD_CS_ENTER(CH3COMM, vc);
        mpi_errno = MPIDI_CH3_iStartMsgv(vc, iov, 3, &req);
        MPIU_THREAD_CS_EXIT(CH3COMM, vc);

        /* release the target datatype */
        MPID_Datatype_release(dtp);
    }

    if (mpi_errno != MPI_SUCCESS) {
        MPIU_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
    }

    /* release the request returned by iStartMsg or iStartMsgv */
    if (req != NULL) {
        MPID_Request_release(req);
    }

  fn_exit:
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_RECV_RMA_MSG);
    return mpi_errno;

    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
    MPIU_CHKPMEM_REAP();
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}


#undef FUNCNAME
#define FUNCNAME send_flush_msg
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
static int send_flush_msg(int dest, MPID_Win * win_ptr)
{
    int mpi_errno = MPI_SUCCESS;
    MPIDI_CH3_Pkt_t upkt;
    MPIDI_CH3_Pkt_flush_t *flush_pkt = &upkt.flush;
    MPID_Request *req = NULL;
    MPIDI_VC_t *vc;
    MPIDI_STATE_DECL(MPID_STATE_SEND_FLUSH_MSG);
    MPIDI_RMA_FUNC_ENTER(MPID_STATE_SEND_FLUSH_MSG);

    MPIDI_Comm_get_vc_set_active(win_ptr->comm_ptr, dest, &vc);

    MPIDI_Pkt_init(flush_pkt, MPIDI_CH3_PKT_FLUSH);
    flush_pkt->target_win_handle = win_ptr->all_win_handles[dest];
    flush_pkt->source_win_handle = win_ptr->handle;

    MPIU_THREAD_CS_ENTER(CH3COMM, vc);
    mpi_errno = MPIDI_CH3_iStartMsg(vc, flush_pkt, sizeof(*flush_pkt), &req);
    MPIU_THREAD_CS_EXIT(CH3COMM, vc);
    MPIU_ERR_CHKANDJUMP(mpi_errno != MPI_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**ch3|rma_msg");

    /* Release the request returned by iStartMsg */
    if (req != NULL) {
        MPID_Request_release(req);
    }

  fn_exit:
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_SEND_FLUSH_MSG);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}


int MPIDI_CH3I_Issue_rma_op(MPIDI_RMA_Op_t * op_ptr, MPID_Win * win_ptr,
1557
                            MPIDI_CH3_Pkt_flags_t flags)
1558
1559
1560
1561
1562
1563
{
    int mpi_errno = MPI_SUCCESS;
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3I_ISSUE_RMA_OP);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_CH3I_ISSUE_RMA_OP);

1564
1565
1566
1567
1568
    switch (op_ptr->pkt.type) {
    case (MPIDI_CH3_PKT_PUT):
    case (MPIDI_CH3_PKT_ACCUMULATE):
    case (MPIDI_CH3_PKT_GET_ACCUM):
        mpi_errno = send_rma_msg(op_ptr, win_ptr, flags);
1569
        break;
1570
1571
    case (MPIDI_CH3_PKT_ACCUM_IMMED):
        mpi_errno = send_contig_acc_msg(op_ptr, win_ptr, flags);
1572
        break;
1573
1574
    case (MPIDI_CH3_PKT_GET):
        mpi_errno = recv_rma_msg(op_ptr, win_ptr, flags);
1575
        break;
1576
1577
1578
    case (MPIDI_CH3_PKT_CAS):
    case (MPIDI_CH3_PKT_FOP):
        mpi_errno = send_immed_rmw_msg(op_ptr, win_ptr, flags);
1579
1580
1581
1582
1583
        break;
    default:
        MPIU_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**winInvalidOp");
    }

1584
1585
1586
    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);

1587
1588
1589
1590
1591
1592
1593
1594
  fn_exit:
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_MPIDI_CH3I_ISSUE_RMA_OP);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}