ch3u_rma_sync.c 97.2 KB
Newer Older
1
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 3 4 5 6 7 8 9
/*
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

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

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
=== BEGIN_MPI_T_CVAR_INFO_BLOCK ===

categories:
    - name        : CH3
      description : cvars that control behavior of ch3

cvars:
    - name        : MPIR_CVAR_CH3_RMA_NREQUEST_THRESHOLD
      category    : CH3
      type        : int
      default     : 4000
      class       : none
      verbosity   : MPI_T_VERBOSITY_USER_BASIC
      scope       : MPI_T_SCOPE_ALL_EQ
      description : >-
        Threshold at which the RMA implementation attempts to complete requests
        while completing RMA operations and while using the lazy synchonization
        approach.  Change this value if programs fail because they run out of
        requests or other internal resources

    - name        : MPIR_CVAR_CH3_RMA_NREQUEST_NEW_THRESHOLD
      category    : CH3
      type        : int
34
      default     : 0
35 36 37 38 39 40 41 42 43
      class       : none
      verbosity   : MPI_T_VERBOSITY_USER_BASIC
      scope       : MPI_T_SCOPE_ALL_EQ
      description : >-
        Threshold for the number of new requests since the last attempt to
        complete pending requests.  Higher values can increase performance,
        but may run the risk of exceeding the available number of requests
        or other internal resources.

44 45 46
    - name        : MPIR_CVAR_CH3_RMA_GC_NUM_COMPLETED
      category    : CH3
      type        : int
47
      default     : (-1)
48 49 50 51 52 53 54
      class       : none
      verbosity   : MPI_T_VERBOSITY_USER_BASIC
      scope       : MPI_T_SCOPE_ALL_EQ
      description : >-
        Threshold for the number of completed requests the runtime finds
        before it stops trying to find more completed requests in garbage
        collection function.
55 56 57 58
        Note that it works with MPIR_CVAR_CH3_RMA_GC_NUM_TESTED as an OR
        relation, which means runtime will stop checking when either one
        of its following conditions is satisfied or one of conditions of
        MPIR_CVAR_CH3_RMA_GC_NUM_TESTED is satisfied.
59 60 61 62 63 64 65 66 67 68 69
        When it is set to negative value, it means runtime will not stop
        checking the operation list until it reaches the end of the list.
        When it is set to positive value, it means runtime will not stop
        checking the operation list until it finds certain number of
        completed requests. When it is set to zero value, the outcome is
        undefined.
        Note that in garbage collection function, if runtime finds a chain
        of completed RMA requests, it will temporarily ignore this CVAR
        and try to find continuous completed requests as many as possible,
        until it meets an incomplete request.

70 71 72
    - name        : MPIR_CVAR_CH3_RMA_GC_NUM_TESTED
      category    : CH3
      type        : int
73
      default     : 100
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
      class       : none
      verbosity   : MPI_T_VERBOSITY_USER_BASIC
      scope       : MPI_T_SCOPE_ALL_EQ
      description : >-
        Threshold for the number of RMA requests the runtime tests before
        it stops trying to check more requests in garbage collection
        routine.
        Note that it works with MPIR_CVAR_CH3_RMA_GC_NUM_COMPLETED as an
        OR relation, which means runtime will stop checking when either
        one of its following conditions is satisfied or one of conditions
        of MPIR_CVAR_CH3_RMA_GC_NUM_COMPLETED is satisfied.
        When it is set to negative value, runtime will not stop checking
        operation list until runtime reaches the end of the list. It has
        the risk of O(N) traversing overhead if there is no completed
        request in the list. When it is set to positive value, it means
        runtime will not stop checking the operation list until it visits
        such number of requests. Higher values may make more completed
        requests to be found, but it has the risk of visiting too many
        requests, leading to significant performance overhead. When it is
        set to zero value, runtime will stop checking the operation list
        immediately, which may cause weird performance in practice.
        Note that in garbage collection function, if runtime finds a chain
        of completed RMA requests, it will temporarily ignore this CVAR and
        try to find continuous completed requests as many as possible, until
        it meets an incomplete request.

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
    - name        : MPIR_CVAR_CH3_RMA_LOCK_IMMED
      category    : CH3
      type        : boolean
      default     : false
      class       : none
      verbosity   : MPI_T_VERBOSITY_USER_BASIC
      scope       : MPI_T_SCOPE_ALL_EQ
      description : >-
        Issue a request for the passive target RMA lock immediately.  Default
        behavior is to defer the lock request until the call to MPI_Win_unlock.

    - name        : MPIR_CVAR_CH3_RMA_MERGE_LOCK_OP_UNLOCK
      category    : CH3
      type        : boolean
      default     : true
      class       : none
      verbosity   : MPI_T_VERBOSITY_USER_BASIC
      scope       : MPI_T_SCOPE_ALL_EQ
      description : >-
        Enable/disable an optimization that merges lock, op, and unlock
        messages, for single-operation passive target epochs.

=== END_MPI_T_CVAR_INFO_BLOCK ===
*/

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 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
/* Notes for memory barriers in RMA synchronizations

   When SHM is allocated for RMA window, we need to add memory berriers at proper
   places in RMA synchronization routines to guarantee the ordering of read/write
   operations, so that any operations after synchronization calls will see the
   correct data.

   There are four kinds of operations involved in the following explanation:

   1. Local loads/stores: any operations happening outside RMA epoch and accessing
      each process's own window memory.

   2. SHM operations: any operations happening inside RMA epoch. They may access
      any processes' window memory, which include direct loads/stores, and
      RMA operations that are internally implemented as direct loads/stores in
      MPI implementation.

   3. PROC_SYNC: synchronzations among processes by sending/recving messages.

   4. MEM_SYNC: a full memory barrier. It ensures the ordering of read/write
      operations on each process.

   (1) FENCE synchronization

              RANK 0                           RANK 1

       (local loads/stores)             (local loads/stores)

           WIN_FENCE {                    WIN_FENCE {
               MEM_SYNC                       MEM_SYNC
               PROC_SYNC -------------------- PROC_SYNC
               MEM_SYNC                       MEM_SYNC
           }                              }

        (SHM operations)                  (SHM operations)

           WIN_FENCE {                     WIN_FENCE {
               MEM_SYNC                        MEM_SYNC
               PROC_SYNC --------------------- PROC_SYNC
               MEM_SYNC                        MEM_SYNC
           }                               }

      (local loads/stores)              (local loads/stores)

       We need MEM_SYNC before and after PROC_SYNC for both starting WIN_FENCE
       and ending WIN_FENCE, to ensure the ordering between local loads/stores
       and PROC_SYNC in starting WIN_FENCE (and vice versa in ending WIN_FENCE),
       and the ordering between PROC_SYNC and SHM operations in starting WIN_FENCE
       (and vice versa for ending WIN_FENCE).

       In starting WIN_FENCE, the MEM_SYNC before PROC_SYNC essentially exposes
       previous local loads/stores to other processes; after PROC_SYNC, each
       process knows that everyone else already exposed their local loads/stores;
       the MEM_SYNC after PROC_SYNC ensures that my following SHM operations will
       happen after PROC_SYNC and will see the latest data on other processes.

       In ending WIN_FENCE, the MEM_SYNC before PROC_SYNC essentially exposes
       previous SHM operations to other processes; after PROC_SYNC, each process
       knows everyone else already exposed their SHM operations; the MEM_SYNC
       after PROC_SYNC ensures that my following local loads/stores will happen
       after PROC_SYNC and will see the latest data in my memory region.

   (2) POST-START-COMPLETE-WAIT synchronization

              RANK 0                           RANK 1

                                          (local loads/stores)

           WIN_START {                      WIN_POST {
                                                MEM_SYNC
               PROC_SYNC ---------------------- PROC_SYNC
               MEM_SYNC
           }                                }

         (SHM operations)

           WIN_COMPLETE {                  WIN_WAIT/TEST {
               MEM_SYNC
               PROC_SYNC --------------------- PROC_SYNC
                                               MEM_SYNC
           }                               }

                                          (local loads/stores)

       We need MEM_SYNC before PROC_SYNC for WIN_POST and WIN_COMPLETE, and
       MEM_SYNC after PROC_SYNC in WIN_START and WIN_WAIT/TEST, to ensure the
       ordering between local loads/stores and PROC_SYNC in WIN_POST (and
       vice versa in WIN_WAIT/TEST), and the ordering between PROC_SYNC and SHM
       operations in WIN_START (and vice versa in WIN_COMPLETE).

       In WIN_POST, the MEM_SYNC before PROC_SYNC essentially exposes previous
       local loads/stores to group of origin processes; after PROC_SYNC, origin
       processes knows all target processes already exposed their local
       loads/stores; in WIN_START, the MEM_SYNC after PROC_SYNC ensures that
       following SHM operations will happen after PROC_SYNC and will see the
       latest data on target processes.

       In WIN_COMPLETE, the MEM_SYNC before PROC_SYNC essentailly exposes previous
       SHM operations to group of target processes; after PROC_SYNC, target
       processes knows all origin process already exposed their SHM operations;
       in WIN_WAIT/TEST, the MEM_SYNC after PROC_SYNC ensures that following local
       loads/stores will happen after PROC_SYNC and will see the latest data in
       my memory region.

   (3) Passive target synchronization

              RANK 0                          RANK 1

                                        WIN_LOCK(target=1) {
                                            PROC_SYNC (lock granted)
                                            MEM_SYNC
                                        }

                                        (SHM operations)

                                        WIN_UNLOCK(target=1) {
                                            MEM_SYNC
                                            PROC_SYNC (lock released)
                                        }

         PROC_SYNC -------------------- PROC_SYNC

         WIN_LOCK (target=1) {
             PROC_SYNC (lock granted)
             MEM_SYNC
         }

         (SHM operations)

         WIN_UNLOCK (target=1) {
             MEM_SYNC
             PROC_SYNC (lock released)
         }

         PROC_SYNC -------------------- PROC_SYNC

                                        WIN_LOCK(target=1) {
                                            PROC_SYNC (lock granted)
                                            MEM_SYNC
                                        }

                                        (SHM operations)

                                        WIN_UNLOCK(target=1) {
                                            MEM_SYNC
                                            PROC_SYNC (lock released)
                                        }

         We need MEM_SYNC after PROC_SYNC in WIN_LOCK, and MEM_SYNC before
         PROC_SYNC in WIN_UNLOCK, to ensure the ordering between SHM operations
         and PROC_SYNC and vice versa.

         In WIN_LOCK, the MEM_SYNC after PROC_SYNC guarantees two things:
         (a) it guarantees that following SHM operations will happen after
         lock is granted; (b) it guarantees that following SHM operations
         will happen after any PROC_SYNC with target before WIN_LOCK is called,
         which means those SHM operations will see the latest data on target
         process.

         In WIN_UNLOCK, the MEM_SYNC before PROC_SYNC also guarantees two
         things: (a) it guarantees that SHM operations will happen before
         lock is released; (b) it guarantees that SHM operations will happen
         before any PROC_SYNC with target after WIN_UNLOCK is returned, which
         means following SHM operations on that target will see the latest data.

         WIN_LOCK_ALL/UNLOCK_ALL are same with WIN_LOCK/UNLOCK.

              RANK 0                          RANK 1

         WIN_LOCK_ALL

         (SHM operations)

         WIN_FLUSH(target=1) {
             MEM_SYNC
         }

         PROC_SYNC ------------------------PROC_SYNC

                                           WIN_LOCK(target=1) {
                                               PROC_SYNC (lock granted)
                                               MEM_SYNC
                                           }

                                           (SHM operations)

                                           WIN_UNLOCK(target=1) {
                                               MEM_SYNC
                                               PROC_SYNC (lock released)
                                           }

         WIN_UNLOCK_ALL

         We need MEM_SYNC in WIN_FLUSH to ensure the ordering between SHM
         operations and PROC_SYNC.

         The MEM_SYNC in WIN_FLUSH guarantees that all SHM operations before
         this WIN_FLUSH will happen before any PROC_SYNC with target after
         this WIN_FLUSH, which means SHM operations on target process after
         PROC_SYNC with origin will see the latest data.
*/

327
void MPIDI_CH3_RMA_Init_Pvars(void)
328 329
{
}
330

331 332
/* These are used to use a common routine to complete lists of RMA
   operations with a single routine, while collecting data that
333 334 335 336
   distinguishes between different synchronization modes.  This is not
   thread-safe; the best choice for thread-safety is to eliminate this
   ability to discriminate between the different types of RMA synchronization.
*/
337

338 339 340
/*
 * These routines provide a default implementation of the MPI RMA operations
 * in terms of the low-level, two-sided channel operations.  A channel
341 342
 * may override these functions, on a per-window basis, by overriding
 * the MPID functions in the RMAFns section of MPID_Win object.
343 344
 */

345 346
#define SYNC_POST_TAG 100

347 348 349
static int wait_for_lock_granted(MPID_Win * win_ptr, int target_rank);
static int do_passive_target_rma(MPID_Win * win_ptr, int target_rank,
                                 int *wait_for_rma_done_pkt, MPIDI_CH3_Pkt_flags_t sync_flags);
Xin Zhao's avatar
Xin Zhao committed
350 351
static int send_lock_put_or_acc(MPID_Win *, int);
static int send_lock_get(MPID_Win *, int);
352 353
static inline int rma_list_complete(MPID_Win * win_ptr, MPIDI_RMA_Ops_list_t * ops_list,
                                    MPIDI_RMA_Ops_list_t * ops_list_tail);
354 355
static inline int rma_list_gc(MPID_Win * win_ptr,
                              MPIDI_RMA_Ops_list_t * ops_list,
356
                              MPIDI_RMA_Ops_list_t * ops_list_tail,
357
                              MPIDI_RMA_Op_t * last_elm, int *nDone);
358

359

360 361 362 363
#undef FUNCNAME
#define FUNCNAME MPIDI_Win_fence
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
364
int MPIDI_Win_fence(int assert, MPID_Win * win_ptr)
365 366
{
    int mpi_errno = MPI_SUCCESS;
367
    int comm_size;
368
    int *rma_target_proc, *nops_to_proc, i, total_op_count, *curr_ops_cnt;
369
    MPIDI_RMA_Op_t *curr_ptr;
370
    MPIDI_RMA_Ops_list_t *ops_list;
371
    MPIDI_RMA_Ops_list_t *ops_list_tail;
372 373
    MPID_Comm *comm_ptr;
    MPID_Progress_state progress_state;
374
    int errflag = FALSE;
375
    MPIU_CHKLMEM_DECL(3);
376 377 378 379
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_WIN_FENCE);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_WIN_FENCE);

James Dinan's avatar
James Dinan committed
380 381 382 383
    MPIU_ERR_CHKANDJUMP(win_ptr->epoch_state != MPIDI_EPOCH_NONE &&
                        win_ptr->epoch_state != MPIDI_EPOCH_FENCE,
                        mpi_errno, MPI_ERR_RMA_SYNC, "**rmasync");

384
    /* Note that the NOPRECEDE and NOSUCCEED must be specified by all processes
385 386
     * in the window's group if any specify it */
    if (assert & MPI_MODE_NOPRECEDE) {
387 388 389 390
        /* Error: Operations were issued and the user claimed NOPRECEDE */
        MPIU_ERR_CHKANDJUMP(win_ptr->epoch_state == MPIDI_EPOCH_FENCE,
                            mpi_errno, MPI_ERR_RMA_SYNC, "**rmasync");

391 392
        win_ptr->fence_issued = (assert & MPI_MODE_NOSUCCEED) ? 0 : 1;
        goto shm_barrier;
393
    }
394

395 396 397 398 399 400 401
    if (win_ptr->fence_issued == 0) {
        /* win_ptr->fence_issued == 0 means either this is the very first
         * call to fence or the preceding fence had the
         * MPI_MODE_NOSUCCEED assert.
         *
         * If this fence has MPI_MODE_NOSUCCEED, do nothing and return.
         * Otherwise just increment the fence count and return. */
402

403 404
        if (!(assert & MPI_MODE_NOSUCCEED))
            win_ptr->fence_issued = 1;
405
    }
406 407 408
    else {
        int nRequest = 0;
        int nRequestNew = 0;
409 410

        /* Ensure ordering of load/store operations. */
411
        if (win_ptr->shm_allocated == TRUE) {
412
            OPA_read_write_barrier();
413 414
        }

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
        /* This is the second or later fence. Do all the preceding RMA ops. */
        comm_ptr = win_ptr->comm_ptr;
        /* First inform every process whether it is a target of RMA
         * ops from this process */
        comm_size = comm_ptr->local_size;

        MPIU_CHKLMEM_MALLOC(rma_target_proc, int *, comm_size * sizeof(int),
                            mpi_errno, "rma_target_proc");
        for (i = 0; i < comm_size; i++)
            rma_target_proc[i] = 0;

        /* keep track of no. of ops to each proc. Needed for knowing
         * whether or not to decrement the completion counter. The
         * completion counter is decremented only on the last
         * operation. */
        MPIU_CHKLMEM_MALLOC(nops_to_proc, int *, comm_size * sizeof(int),
                            mpi_errno, "nops_to_proc");
        for (i = 0; i < comm_size; i++)
            nops_to_proc[i] = 0;
434

435
        /* Note, active target uses the following ops list, and passive
436
         * target uses win_ptr->targets[..] */
437
        ops_list = &win_ptr->at_rma_ops_list;
438
        ops_list_tail = &win_ptr->at_rma_ops_list_tail;
439

440 441 442
        /* set rma_target_proc[i] to 1 if rank i is a target of RMA
         * ops from this process */
        total_op_count = 0;
443
        curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
        while (curr_ptr != NULL) {
            total_op_count++;
            rma_target_proc[curr_ptr->target_rank] = 1;
            nops_to_proc[curr_ptr->target_rank]++;
            curr_ptr = curr_ptr->next;
        }

        MPIU_CHKLMEM_MALLOC(curr_ops_cnt, int *, comm_size * sizeof(int),
                            mpi_errno, "curr_ops_cnt");
        for (i = 0; i < comm_size; i++)
            curr_ops_cnt[i] = 0;
        /* do a reduce_scatter_block (with MPI_SUM) on rma_target_proc.
         * As a result,
         * each process knows how many other processes will be doing
         * RMA ops on its window */

        /* first initialize the completion counter. */
        win_ptr->at_completion_counter += comm_size;

        mpi_errno = MPIR_Reduce_scatter_block_impl(MPI_IN_PLACE, rma_target_proc, 1,
464
                                                   MPI_INT, MPI_SUM, comm_ptr, &errflag);
465 466 467 468
        /* result is stored in rma_target_proc[0] */
        if (mpi_errno) {
            MPIU_ERR_POP(mpi_errno);
        }
469
        MPIU_ERR_CHKANDJUMP(errflag, mpi_errno, MPI_ERR_OTHER, "**coll_fail");
470

471 472 473 474 475
        /* Ensure ordering of load/store operations. */
        if (win_ptr->shm_allocated == TRUE) {
            OPA_read_write_barrier();
        }

476 477 478
        /* Set the completion counter */
        /* FIXME: MT: this needs to be done atomically because other
         * procs have the address and could decrement it. */
479 480
        win_ptr->at_completion_counter -= comm_size;
        win_ptr->at_completion_counter += rma_target_proc[0];
481

482
        i = 0;
483
        curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
484
        while (curr_ptr != NULL) {
485 486
            MPIDI_CH3_Pkt_flags_t flags = MPIDI_CH3_PKT_FLAG_NONE;

487 488 489
            /* The completion counter at the target is decremented only on
             * the last RMA operation. */
            if (curr_ops_cnt[curr_ptr->target_rank] == nops_to_proc[curr_ptr->target_rank] - 1) {
490 491 492
                flags = MPIDI_CH3_PKT_FLAG_RMA_AT_COMPLETE;
            }

493
            mpi_errno = MPIDI_CH3I_Issue_rma_op(curr_ptr, win_ptr, flags);
494 495
            if (mpi_errno)
                MPIU_ERR_POP(mpi_errno);
496

497 498 499 500
            i++;
            curr_ops_cnt[curr_ptr->target_rank]++;
            /* If the request is null, we can remove it immediately */
            if (!curr_ptr->request) {
501
                MPIDI_CH3I_RMA_Ops_free_and_next(win_ptr, ops_list, ops_list_tail, &curr_ptr);
502 503 504 505 506 507 508 509 510 511 512 513
            }
            else {
                nRequest++;
                curr_ptr = curr_ptr->next;
                /* The test on the difference is to reduce the number
                 * of times the partial complete routine is called. Without
                 * this, significant overhead is added once the
                 * number of requests exceeds the threshold, since the
                 * number that are completed in a call may be small. */
                if (nRequest > MPIR_CVAR_CH3_RMA_NREQUEST_THRESHOLD &&
                    nRequest - nRequestNew > MPIR_CVAR_CH3_RMA_NREQUEST_NEW_THRESHOLD) {
                    int nDone = 0;
514
                    mpi_errno = poke_progress_engine();
515 516
                    if (mpi_errno != MPI_SUCCESS)
                        MPIU_ERR_POP(mpi_errno);
517

518
                    mpi_errno = rma_list_gc(win_ptr, ops_list, ops_list_tail, curr_ptr, &nDone);
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
                    if (mpi_errno != MPI_SUCCESS)
                        MPIU_ERR_POP(mpi_errno);
                    /* if (nDone > 0) printf("nDone = %d\n", nDone); */
                    nRequest -= nDone;
                    nRequestNew = nRequest;
                }
            }
        }

        /* We replaced a loop over an array of requests with a list of the
         * incomplete requests.  The reason to do
         * that is for long lists - processing the entire list until
         * all are done introduces a potentially n^2 time.  In
         * testing with test/mpi/perf/manyrma.c , the number of iterations
         * within the "while (total_op_count) was O(total_op_count).
         *
         * Another alternative is to create a more compressed list (storing
         * only the necessary information, reducing the number of cache lines
         * needed while looping through the requests.
         */
        if (total_op_count) {
540
            mpi_errno = rma_list_complete(win_ptr, ops_list, ops_list_tail);
541 542 543
            if (mpi_errno != MPI_SUCCESS)
                MPIU_ERR_POP(mpi_errno);
        }
544

545 546 547 548 549
        /* MT: avoid processing unissued operations enqueued by other threads
           in rma_list_complete() */
        curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
        if (curr_ptr && !curr_ptr->request)
            goto finish_up;
550
        MPIU_Assert(MPIDI_CH3I_RMA_Ops_isempty(ops_list));
551

552
 finish_up:
553
	/* wait for all operations from other processes to finish */
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
        if (win_ptr->at_completion_counter) {
            MPID_Progress_start(&progress_state);
            while (win_ptr->at_completion_counter) {
                mpi_errno = MPID_Progress_wait(&progress_state);
                /* --BEGIN ERROR HANDLING-- */
                if (mpi_errno != MPI_SUCCESS) {
                    MPID_Progress_end(&progress_state);
                    MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER,"**winnoprogress");
                }
                /* --END ERROR HANDLING-- */
            }
            MPID_Progress_end(&progress_state);
        }

        if (assert & MPI_MODE_NOSUCCEED) {
            win_ptr->fence_issued = 0;
        }
571

James Dinan's avatar
James Dinan committed
572 573 574
        win_ptr->epoch_state = MPIDI_EPOCH_NONE;
    }

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
 shm_barrier:
    if (!(assert & MPI_MODE_NOSUCCEED)) {
        /* In a FENCE without MPI_MODE_NOSUCCEED (which means this FENCE
           might start a new Active epoch), if SHM is allocated, perform
           a barrier among processes on the same node, to prevent one
           process modifying another process's memory before that process
           starts an epoch. */

        if (win_ptr->shm_allocated == TRUE) {
            MPID_Comm *node_comm_ptr = win_ptr->comm_ptr->node_comm;

            /* Ensure ordering of load/store operations. */
            OPA_read_write_barrier();

            mpi_errno = MPIR_Barrier_impl(node_comm_ptr, &errflag);
            if (mpi_errno) {goto fn_fail;}

            /* Ensure ordering of load/store operations. */
            OPA_read_write_barrier();
        }
    }

597
  fn_exit:
598 599 600 601
    MPIU_CHKLMEM_FREEALL();
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_MPIDI_WIN_FENCE);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
602
  fn_fail:
603 604 605 606 607 608
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}


#undef FUNCNAME
609
#define FUNCNAME MPIDI_Win_post
610 611
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
612
int MPIDI_Win_post(MPID_Group * post_grp_ptr, int assert, MPID_Win * win_ptr)
613
{
614
    int mpi_errno = MPI_SUCCESS;
615 616 617 618 619
    MPID_Group *win_grp_ptr;
    int i, post_grp_size, *ranks_in_post_grp, *ranks_in_win_grp, dst, rank;
    MPID_Comm *win_comm_ptr;
    MPIU_CHKLMEM_DECL(4);
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_WIN_POST);
620

621
    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_WIN_POST);
622

623 624 625
    MPIU_ERR_CHKANDJUMP(win_ptr->epoch_state != MPIDI_EPOCH_NONE &&
                        win_ptr->epoch_state != MPIDI_EPOCH_START,
                        mpi_errno, MPI_ERR_RMA_SYNC, "**rmasync");
626

627 628 629 630 631
    /* Track access epoch state */
    if (win_ptr->epoch_state == MPIDI_EPOCH_START)
        win_ptr->epoch_state = MPIDI_EPOCH_PSCW;
    else
        win_ptr->epoch_state = MPIDI_EPOCH_POST;
632

633 634 635 636 637
    /* Even though we would want to reset the fence counter to keep
     * the user from using the previous fence to mark the beginning of
     * a fence epoch if he switched from fence to lock-unlock
     * synchronization, we cannot do this because fence_issued must be
     * updated collectively */
638

639
    post_grp_size = post_grp_ptr->size;
640

641 642 643
    /* Ensure ordering of load/store operations. */
    if (win_ptr->shm_allocated == TRUE) {
        OPA_read_write_barrier();
644 645
    }

646 647
    /* initialize the completion counter */
    win_ptr->at_completion_counter += post_grp_size;
648

649 650 651
    if ((assert & MPI_MODE_NOCHECK) == 0) {
        MPI_Request *req;
        MPI_Status *status;
652

653 654
        /* NOCHECK not specified. We need to notify the source
         * processes that Post has been called. */
655

656 657 658
        /* We need to translate the ranks of the processes in
         * post_group to ranks in win_ptr->comm_ptr, so that we
         * can do communication */
659

660 661 662 663
        MPIU_CHKLMEM_MALLOC(ranks_in_post_grp, int *,
                            post_grp_size * sizeof(int), mpi_errno, "ranks_in_post_grp");
        MPIU_CHKLMEM_MALLOC(ranks_in_win_grp, int *,
                            post_grp_size * sizeof(int), mpi_errno, "ranks_in_win_grp");
664

665 666
        for (i = 0; i < post_grp_size; i++) {
            ranks_in_post_grp[i] = i;
667 668
        }

669
        win_comm_ptr = win_ptr->comm_ptr;
670

671
        mpi_errno = MPIR_Comm_group_impl(win_comm_ptr, &win_grp_ptr);
672 673
        if (mpi_errno)
            MPIU_ERR_POP(mpi_errno);
674 675


676 677 678 679
        mpi_errno = MPIR_Group_translate_ranks_impl(post_grp_ptr, post_grp_size, ranks_in_post_grp,
                                                    win_grp_ptr, ranks_in_win_grp);
        if (mpi_errno)
            MPIU_ERR_POP(mpi_errno);
680

681
        rank = win_ptr->comm_ptr->rank;
682

683 684 685 686
        MPIU_CHKLMEM_MALLOC(req, MPI_Request *, post_grp_size * sizeof(MPI_Request), mpi_errno,
                            "req");
        MPIU_CHKLMEM_MALLOC(status, MPI_Status *, post_grp_size * sizeof(MPI_Status), mpi_errno,
                            "status");
687

688 689 690
        /* Send a 0-byte message to the source processes */
        for (i = 0; i < post_grp_size; i++) {
            dst = ranks_in_win_grp[i];
691

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
            /* FIXME: Short messages like this shouldn't normally need a
             * request - this should consider using the ch3 call to send
             * a short message and return a request only if the message is
             * not delivered. */
            if (dst != rank) {
                MPID_Request *req_ptr;
                mpi_errno = MPID_Isend(&i, 0, MPI_INT, dst, SYNC_POST_TAG, win_comm_ptr,
                                       MPID_CONTEXT_INTRA_PT2PT, &req_ptr);
                if (mpi_errno)
                    MPIU_ERR_POP(mpi_errno);
                req[i] = req_ptr->handle;
            }
            else {
                req[i] = MPI_REQUEST_NULL;
            }
707
        }
708 709 710 711 712 713 714 715 716 717 718 719
        mpi_errno = MPIR_Waitall_impl(post_grp_size, req, status);
        if (mpi_errno && mpi_errno != MPI_ERR_IN_STATUS)
            MPIU_ERR_POP(mpi_errno);

        /* --BEGIN ERROR HANDLING-- */
        if (mpi_errno == MPI_ERR_IN_STATUS) {
            for (i = 0; i < post_grp_size; i++) {
                if (status[i].MPI_ERROR != MPI_SUCCESS) {
                    mpi_errno = status[i].MPI_ERROR;
                    MPIU_ERR_POP(mpi_errno);
                }
            }
720
        }
721
        /* --END ERROR HANDLING-- */
722

723 724 725
        mpi_errno = MPIR_Group_free_impl(win_grp_ptr);
        if (mpi_errno)
            MPIU_ERR_POP(mpi_errno);
726
    }
727

728
  fn_exit:
729 730
    MPIU_CHKLMEM_FREEALL();
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_MPIDI_WIN_POST);
731 732
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
733
  fn_fail:
734 735 736 737
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}

738 739

static int recv_post_msgs(MPID_Win * win_ptr, int *ranks_in_win_grp, int local)
740
{
741
    int mpi_errno = MPI_SUCCESS;
742 743 744 745 746 747 748
    int start_grp_size, src, rank, i, j;
    MPI_Request *req;
    MPI_Status *status;
    MPID_Comm *comm_ptr = win_ptr->comm_ptr;
    MPIDI_VC_t *orig_vc = NULL, *target_vc = NULL;
    MPIU_CHKLMEM_DECL(2);
    MPIDI_STATE_DECL(MPID_STATE_RECV_POST_MSGS);
749

750
    MPIDI_RMA_FUNC_ENTER(MPID_STATE_RECV_POST_MSGS);
751

752 753 754
    /* Wait for 0-byte messages from processes either on the same node
     * or not (depending on the "local" parameter), so we know they
     * have entered post. */
755

756
    start_grp_size = win_ptr->start_group_ptr->size;
757

758 759 760 761
    rank = win_ptr->comm_ptr->rank;
    MPIU_CHKLMEM_MALLOC(req, MPI_Request *, start_grp_size * sizeof(MPI_Request), mpi_errno, "req");
    MPIU_CHKLMEM_MALLOC(status, MPI_Status *, start_grp_size * sizeof(MPI_Status), mpi_errno,
                        "status");
762

763 764 765
    j = 0;
    for (i = 0; i < start_grp_size; i++) {
        src = ranks_in_win_grp[i];
766

767 768
        if (src == rank)
            continue;
769

770 771
        if (local && win_ptr->shm_allocated == TRUE) {
            MPID_Request *req_ptr;
772

773 774
            MPIDI_Comm_get_vc(win_ptr->comm_ptr, rank, &orig_vc);
            MPIDI_Comm_get_vc(win_ptr->comm_ptr, src, &target_vc);
775

776 777 778 779 780 781 782 783 784 785
            if (orig_vc->node_id == target_vc->node_id) {
                mpi_errno = MPID_Irecv(NULL, 0, MPI_INT, src, SYNC_POST_TAG,
                                       comm_ptr, MPID_CONTEXT_INTRA_PT2PT, &req_ptr);
                if (mpi_errno)
                    MPIU_ERR_POP(mpi_errno);
                req[j++] = req_ptr->handle;
            }
        }
        else if (!local) {
            MPID_Request *req_ptr;
786

787 788
            MPIDI_Comm_get_vc(win_ptr->comm_ptr, rank, &orig_vc);
            MPIDI_Comm_get_vc(win_ptr->comm_ptr, src, &target_vc);
789

790 791 792 793 794 795 796
            if (win_ptr->shm_allocated != TRUE ||
                orig_vc->node_id != target_vc->node_id) {
                mpi_errno = MPID_Irecv(NULL, 0, MPI_INT, src, SYNC_POST_TAG,
                                       comm_ptr, MPID_CONTEXT_INTRA_PT2PT, &req_ptr);
                if (mpi_errno) MPIU_ERR_POP(mpi_errno);
                req[j++] = req_ptr->handle;
            }
797
        }
798 799
    }

800 801 802 803 804 805 806 807 808 809 810
    if (j) {
        mpi_errno = MPIR_Waitall_impl(j, req, status);
        if (mpi_errno && mpi_errno != MPI_ERR_IN_STATUS)
            MPIU_ERR_POP(mpi_errno);
        /* --BEGIN ERROR HANDLING-- */
        if (mpi_errno == MPI_ERR_IN_STATUS) {
            for (i = 0; i < j; i++) {
                if (status[i].MPI_ERROR != MPI_SUCCESS) {
                    mpi_errno = status[i].MPI_ERROR;
                    MPIU_ERR_POP(mpi_errno);
                }
811
            }
812
        }
813 814 815 816 817 818 819 820
        /* --END ERROR HANDLING-- */
    }

  fn_fail:
    MPIU_CHKLMEM_FREEALL();
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_RECV_POST_MSGS);
    return mpi_errno;
}
821

822 823 824 825 826 827 828
static int fill_ranks_in_win_grp(MPID_Win * win_ptr, int *ranks_in_win_grp)
{
    int mpi_errno = MPI_SUCCESS;
    int i, *ranks_in_start_grp;
    MPID_Group *win_grp_ptr;
    MPIU_CHKLMEM_DECL(2);
    MPIDI_STATE_DECL(MPID_STATE_FILL_RANKS_IN_WIN_GRP);
829

830
    MPIDI_RMA_FUNC_ENTER(MPID_STATE_FILL_RANKS_IN_WIN_GRP);
831

832 833
    MPIU_CHKLMEM_MALLOC(ranks_in_start_grp, int *, win_ptr->start_group_ptr->size * sizeof(int),
                        mpi_errno, "ranks_in_start_grp");
834

835 836
    for (i = 0; i < win_ptr->start_group_ptr->size; i++)
        ranks_in_start_grp[i] = i;
837

838 839 840
    mpi_errno = MPIR_Comm_group_impl(win_ptr->comm_ptr, &win_grp_ptr);
    if (mpi_errno) {
        MPIU_ERR_POP(mpi_errno);
841 842
    }

843 844 845 846 847 848 849 850 851 852
    mpi_errno =
        MPIR_Group_translate_ranks_impl(win_ptr->start_group_ptr, win_ptr->start_group_ptr->size,
                                        ranks_in_start_grp, win_grp_ptr, ranks_in_win_grp);
    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);

    mpi_errno = MPIR_Group_free_impl(win_grp_ptr);
    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);

853
  fn_fail:
854 855 856
    MPIU_CHKLMEM_FREEALL();
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_FILL_RANKS_IN_WIN_GRP);
    return mpi_errno;
857 858 859
}


860
#undef FUNCNAME
861
#define FUNCNAME MPIDI_Win_start
862 863
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
864
int MPIDI_Win_start(MPID_Group * group_ptr, int assert, MPID_Win * win_ptr)
865
{
866
    int mpi_errno = MPI_SUCCESS;
867 868 869
    int *ranks_in_win_grp;
    MPIU_CHKLMEM_DECL(1);
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_WIN_START);
870

871
    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_WIN_START);
872

873 874 875
    MPIU_ERR_CHKANDJUMP(win_ptr->epoch_state != MPIDI_EPOCH_NONE &&
                        win_ptr->epoch_state != MPIDI_EPOCH_POST,
                        mpi_errno, MPI_ERR_RMA_SYNC, "**rmasync");
876

877 878 879 880 881
    /* Track access epoch state */
    if (win_ptr->epoch_state == MPIDI_EPOCH_POST)
        win_ptr->epoch_state = MPIDI_EPOCH_PSCW;
    else
        win_ptr->epoch_state = MPIDI_EPOCH_START;
882

883 884 885 886 887
    /* Even though we would want to reset the fence counter to keep
     * the user from using the previous fence to mark the beginning of
     * a fence epoch if he switched from fence to lock-unlock
     * synchronization, we cannot do this because fence_issued must be
     * updated collectively */
888

889 890 891
    win_ptr->start_group_ptr = group_ptr;
    MPIR_Group_add_ref(group_ptr);
    win_ptr->start_assert = assert;
892

893 894 895
    /* wait for messages from local processes */
    MPIU_CHKLMEM_MALLOC(ranks_in_win_grp, int *, win_ptr->start_group_ptr->size * sizeof(int),
                        mpi_errno, "ranks_in_win_grp");
896

897 898 899
    mpi_errno = fill_ranks_in_win_grp(win_ptr, ranks_in_win_grp);
    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);
900

901 902 903 904 905 906 907
    /* If MPI_MODE_NOCHECK was not specified, we need to check if
       Win_post was called on the target processes on SHM window.
       Wait for a 0-byte sync message from each target process. */
    if ((win_ptr->start_assert & MPI_MODE_NOCHECK) == 0)
    {
        mpi_errno = recv_post_msgs(win_ptr, ranks_in_win_grp, 1);
        if (mpi_errno) MPIU_ERR_POP(mpi_errno);
908 909
    }

910 911 912
    /* Ensure ordering of load/store operations */
    if (win_ptr->shm_allocated == TRUE) {
        OPA_read_write_barrier();
913 914
    }

915
  fn_fail:
916 917 918
    MPIU_CHKLMEM_FREEALL();
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_MPIDI_WIN_START);
    return mpi_errno;
919 920 921
}


922

923
#undef FUNCNAME
924
#define FUNCNAME MPIDI_Win_complete
925 926
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
927
int MPIDI_Win_complete(MPID_Win * win_ptr)
928
{
929
    int mpi_errno = MPI_SUCCESS;
930 931 932 933
    int comm_size, *nops_to_proc, new_total_op_count;
    int i, j, dst, total_op_count, *curr_ops_cnt;
    MPIDI_RMA_Op_t *curr_ptr;
    MPIDI_RMA_Ops_list_t *ops_list;
934
    MPIDI_RMA_Ops_list_t *ops_list_tail;
935 936 937 938 939 940
    MPID_Comm *comm_ptr;
    int start_grp_size, *ranks_in_win_grp, rank;
    int nRequest = 0;
    int nRequestNew = 0;
    MPIU_CHKLMEM_DECL(6);
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_WIN_COMPLETE);
941

942
    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_WIN_COMPLETE);
943

944
    MPIU_ERR_CHKANDJUMP(win_ptr->epoch_state != MPIDI_EPOCH_PSCW &&
945
                        win_ptr->epoch_state != MPIDI_EPOCH_START,
James Dinan's avatar
James Dinan committed
946 947 948
                        mpi_errno, MPI_ERR_RMA_SYNC, "**rmasync");

    /* Track access epoch state */
949
    if (win_ptr->epoch_state == MPIDI_EPOCH_PSCW)
950
        win_ptr->epoch_state = MPIDI_EPOCH_POST;
951 952
    else
        win_ptr->epoch_state = MPIDI_EPOCH_NONE;
James Dinan's avatar
James Dinan committed
953

954 955
    comm_ptr = win_ptr->comm_ptr;
    comm_size = comm_ptr->local_size;
Xin Zhao's avatar
Xin Zhao committed
956 957 958 959 960

    /* Ensure ordering of load/store operations. */
    if (win_ptr->shm_allocated == TRUE) {
        OPA_read_write_barrier();
    }
961

962 963
    /* Translate the ranks of the processes in
     * start_group to ranks in win_ptr->comm_ptr */
964

965
    start_grp_size = win_ptr->start_group_ptr->size;
966

967 968
    MPIU_CHKLMEM_MALLOC(ranks_in_win_grp, int *, start_grp_size * sizeof(int),
                        mpi_errno, "ranks_in_win_grp");
969

970 971 972
    mpi_errno = fill_ranks_in_win_grp(win_ptr, ranks_in_win_grp);
    if (mpi_errno)
        MPIU_ERR_POP(mpi_errno);
973

974
    rank = win_ptr->comm_ptr->rank;
975

976 977 978 979 980 981
    /* If MPI_MODE_NOCHECK was not specified, we need to check if
     * Win_post was called on the target processes. Wait for a 0-byte sync
     * message from each target process */
    if ((win_ptr->start_assert & MPI_MODE_NOCHECK) == 0) {
        /* wait for messages from non-local processes */
        mpi_errno = recv_post_msgs(win_ptr, ranks_in_win_grp, 0);
982 983
        if (mpi_errno)
            MPIU_ERR_POP(mpi_errno);
984
    }
985

986 987 988 989
    /* keep track of no. of ops to each proc. Needed for knowing
     * whether or not to decrement the completion counter. The
     * completion counter is decremented only on the last
     * operation. */
990

991 992 993
    /* Note, active target uses the following ops list, and passive
     * target uses win_ptr->targets[..] */
    ops_list = &win_ptr->at_rma_ops_list;
994
    ops_list_tail = &win_ptr->at_rma_ops_list_tail;
995

996 997 998
    MPIU_CHKLMEM_MALLOC(nops_to_proc, int *, comm_size * sizeof(int), mpi_errno, "nops_to_proc");
    for (i = 0; i < comm_size; i++)
        nops_to_proc[i] = 0;
999

1000 1001 1002 1003 1004 1005
    total_op_count = 0;
    curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
    while (curr_ptr != NULL) {
        nops_to_proc[curr_ptr->target_rank]++;
        total_op_count++;
        curr_ptr = curr_ptr->next;
1006 1007
    }

1008 1009 1010
    /* We allocate a few extra requests because if there are no RMA
     * ops to a target process, we need to send a 0-byte message just
     * to decrement the completion counter. */
1011

1012 1013 1014
    MPIU_CHKLMEM_MALLOC(curr_ops_cnt, int *, comm_size * sizeof(int), mpi_errno, "curr_ops_cnt");
    for (i = 0; i < comm_size; i++)
        curr_ops_cnt[i] = 0;
1015

1016 1017 1018 1019
    i = 0;
    curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
    while (curr_ptr != NULL) {
        MPIDI_CH3_Pkt_flags_t flags = MPIDI_CH3_PKT_FLAG_NONE;
1020

1021 1022 1023 1024 1025
        /* The completion counter at the target is decremented only on
         * the last RMA operation. */
        if (curr_ops_cnt[curr_ptr->target_rank] == nops_to_proc[curr_ptr->target_rank] - 1) {
            flags = MPIDI_CH3_PKT_FLAG_RMA_AT_COMPLETE;
        }
1026

1027
        mpi_errno = MPIDI_CH3I_Issue_rma_op(curr_ptr, win_ptr, flags);
1028 1029
        if (mpi_errno)
            MPIU_ERR_POP(mpi_errno);
1030

1031 1032 1033 1034
        i++;
        curr_ops_cnt[curr_ptr->target_rank]++;
        /* If the request is null, we can remove it immediately */
        if (!curr_ptr->request) {
1035
            MPIDI_CH3I_RMA_Ops_free_and_next(win_ptr, ops_list, ops_list_tail, &curr_ptr);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        }
        else {
            nRequest++;
            curr_ptr = curr_ptr->next;
            if (nRequest > MPIR_CVAR_CH3_RMA_NREQUEST_THRESHOLD &&
                nRequest - nRequestNew > MPIR_CVAR_CH3_RMA_NREQUEST_NEW_THRESHOLD) {
                int nDone = 0;
                mpi_errno = poke_progress_engine();
                if (mpi_errno != MPI_SUCCESS)
                    MPIU_ERR_POP(mpi_errno);
1046
                mpi_errno = rma_list_gc(win_ptr, ops_list, ops_list_tail, curr_ptr, &nDone);
1047 1048 1049 1050 1051 1052
                if (mpi_errno != MPI_SUCCESS)
                    MPIU_ERR_POP(mpi_errno);
                nRequest -= nDone;
                nRequestNew = nRequest;
            }
        }
1053 1054
    }

1055 1056 1057 1058
    /* If the start_group included some processes that did not end up
     * becoming targets of  RMA operations from this process, we need
     * to send a dummy message to those processes just to decrement
     * the completion counter */
1059

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
    j = i;
    new_total_op_count = total_op_count;
    for (i = 0; i < start_grp_size; i++) {
        dst = ranks_in_win_grp[i];
        if (dst == rank) {
            /* FIXME: MT: this has to be done atomically */
            win_ptr->at_completion_counter -= 1;
        }
        else if (nops_to_proc[dst] == 0) {
            MPIDI_CH3_Pkt_t upkt;
            MPIDI_CH3_Pkt_put_t *put_pkt = &upkt.put;
            MPIDI_VC_t *vc;
            MPID_Request *request;
1073

1074 1075 1076 1077 1078 1079 1080
            MPIDI_Pkt_init(put_pkt, MPIDI_CH3_PKT_PUT);
            put_pkt->flags = MPIDI_CH3_PKT_FLAG_RMA_AT_COMPLETE;
            put_pkt->addr = NULL;
            put_pkt->count = 0;
            put_pkt->datatype = MPI_INT;
            put_pkt->target_win_handle = win_ptr->all_win_handles[dst];
            put_pkt->source_win_handle = win_ptr->handle;
1081

1082
            MPIDI_Comm_get_vc_set_active(comm_ptr, dst, &vc);
1083

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
            MPIU_THREAD_CS_ENTER(CH3COMM, vc);
            mpi_errno = MPIDI_CH3_iStartMsg(vc, put_pkt, sizeof(*put_pkt), &request);
            MPIU_THREAD_CS_EXIT(CH3COMM, vc);
            if (mpi_errno != MPI_SUCCESS) {
                MPIU_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
            }
            /* In the unlikely event that a request is returned (the message
             * is not sent yet), add it to the list of pending operations */
            if (request) {
                MPIDI_RMA_Op_t *new_ptr = NULL;
1094

1095
                mpi_errno = MPIDI_CH3I_RMA_Ops_alloc_tail(win_ptr, ops_list, ops_list_tail, &new_ptr);
1096 1097 1098
                if (mpi_errno) {
                    MPIU_ERR_POP(mpi_errno);
                }
1099

1100
                new_ptr->request = request;
1101
            }
1102 1103
            j++;
            new_total_op_count++;
1104 1105
        }
    }
1106

1107
    if (new_total_op_count) {
1108
        mpi_errno = rma_list_complete(win_ptr, ops_list, ops_list_tail);
1109 1110
        if (mpi_errno != MPI_SUCCESS)
            MPIU_ERR_POP(mpi_errno);
1111
    }
1112

1113
    MPIU_Assert(MPIDI_CH3I_RMA_Ops_isempty(ops_list));
1114

1115 1116 1117
    /* free the group stored in window */
    MPIR_Group_release(win_ptr->start_group_ptr);
    win_ptr->start_group_ptr = NULL;
1118

1119 1120 1121
  fn_exit:
    MPIU_CHKLMEM_FREEALL();
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_MPIDI_WIN_COMPLETE);
1122
    return mpi_errno;
1123 1124 1125 1126
    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
    goto fn_exit;
    /* --END ERROR HANDLING-- */
1127
}
1128