ch3u_rma_sync.c 201 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
#ifdef USE_MPIU_INSTR
MPIU_INSTR_DURATION_DECL(winfence_clearlock);
MPIU_INSTR_DURATION_DECL(winfence_rs);
MPIU_INSTR_DURATION_DECL(winfence_issue);
MPIU_INSTR_DURATION_DECL(winfence_complete);
MPIU_INSTR_DURATION_DECL(winfence_wait);
MPIU_INSTR_DURATION_DECL(winfence_block);
17 18 19
MPIU_INSTR_COUNTER_DECL(winfence_reqs);
MPIU_INSTR_COUNTER_DECL(winunlock_reqs);
MPIU_INSTR_COUNTER_DECL(wincomplete_reqs);
20 21 22 23 24 25
MPIU_INSTR_DURATION_DECL(winpost_clearlock);
MPIU_INSTR_DURATION_DECL(winpost_sendsync);
MPIU_INSTR_DURATION_DECL(winstart_clearlock);
MPIU_INSTR_DURATION_DECL(wincomplete_issue);
MPIU_INSTR_DURATION_DECL(wincomplete_complete);
MPIU_INSTR_DURATION_DECL(wincomplete_recvsync);
26
MPIU_INSTR_DURATION_DECL(wincomplete_block);
27 28 29 30 31
MPIU_INSTR_DURATION_DECL(winwait_wait);
MPIU_INSTR_DURATION_DECL(winlock_getlocallock);
MPIU_INSTR_DURATION_DECL(winunlock_getlock);
MPIU_INSTR_DURATION_DECL(winunlock_issue);
MPIU_INSTR_DURATION_DECL(winunlock_complete);
32
MPIU_INSTR_DURATION_DECL(winunlock_block);
33 34 35 36
MPIU_INSTR_DURATION_DECL(lockqueue_alloc);
MPIU_INSTR_DURATION_DECL(rmapkt_acc);
MPIU_INSTR_DURATION_DECL(rmapkt_acc_predef);
MPIU_INSTR_DURATION_DECL(rmapkt_acc_immed);
37
MPIU_INSTR_DURATION_DECL(rmapkt_acc_immed_op);
38
MPIU_INSTR_DURATION_DECL(rmapkt_cas);
39
MPIU_INSTR_DURATION_DECL(rmapkt_fop);
40
MPIU_INSTR_DURATION_DECL(rmapkt_get_accum);
41
MPIU_INSTR_DURATION_EXTERN_DECL(rmaqueue_alloc);
42
MPIU_INSTR_DURATION_EXTERN_DECL(rmaqueue_set);
43 44 45 46 47 48 49 50 51 52 53
void MPIDI_CH3_RMA_InitInstr(void);

void MPIDI_CH3_RMA_InitInstr(void)
{
    MPIU_INSTR_DURATION_INIT(lockqueue_alloc,0,"Allocate Lock Queue element");
    MPIU_INSTR_DURATION_INIT(winfence_clearlock,1,"WIN_FENCE:Clear prior lock");
    MPIU_INSTR_DURATION_INIT(winfence_rs,0,"WIN_FENCE:ReduceScatterBlock");
    MPIU_INSTR_DURATION_INIT(winfence_issue,2,"WIN_FENCE:Issue RMA ops");
    MPIU_INSTR_DURATION_INIT(winfence_complete,1,"WIN_FENCE:Complete RMA ops");
    MPIU_INSTR_DURATION_INIT(winfence_wait,1,"WIN_FENCE:Wait for ops from other processes");
    MPIU_INSTR_DURATION_INIT(winfence_block,0,"WIN_FENCE:Wait for any progress");
54 55 56
    MPIU_INSTR_COUNTER_INIT(winfence_reqs,"WIN_FENCE:Pending requests");
    MPIU_INSTR_COUNTER_INIT(winunlock_reqs,"WIN_UNLOCK:Pending requests");
    MPIU_INSTR_COUNTER_INIT(wincomplete_reqs,"WIN_COMPLETE:Pending requests");
57 58 59 60 61 62
    MPIU_INSTR_DURATION_INIT(winpost_clearlock,1,"WIN_POST:Clear prior lock");
    MPIU_INSTR_DURATION_INIT(winpost_sendsync,1,"WIN_POST:Senc sync messages");
    MPIU_INSTR_DURATION_INIT(winstart_clearlock,1,"WIN_START:Clear prior lock");
    MPIU_INSTR_DURATION_INIT(wincomplete_recvsync,1,"WIN_COMPLETE:Recv sync messages");
    MPIU_INSTR_DURATION_INIT(wincomplete_issue,2,"WIN_COMPLETE:Issue RMA ops");
    MPIU_INSTR_DURATION_INIT(wincomplete_complete,1,"WIN_COMPLETE:Complete RMA ops");
63
    MPIU_INSTR_DURATION_INIT(wincomplete_block,0,"WIN_COMPLETE:Wait for any progress");
64 65 66 67 68
    MPIU_INSTR_DURATION_INIT(winwait_wait,1,"WIN_WAIT:Wait for ops from other processes");
    MPIU_INSTR_DURATION_INIT(winlock_getlocallock,0,"WIN_LOCK:Get local lock");
    MPIU_INSTR_DURATION_INIT(winunlock_issue,2,"WIN_UNLOCK:Issue RMA ops");
    MPIU_INSTR_DURATION_INIT(winunlock_complete,1,"WIN_UNLOCK:Complete RMA ops");
    MPIU_INSTR_DURATION_INIT(winunlock_getlock,0,"WIN_UNLOCK:Acquire lock");
69
    MPIU_INSTR_DURATION_INIT(winunlock_block,0,"WIN_UNLOCK:Wait for any progress");
70 71 72
    MPIU_INSTR_DURATION_INIT(rmapkt_acc,0,"RMA:PKTHANDLER for Accumulate");
    MPIU_INSTR_DURATION_INIT(rmapkt_acc_predef,0,"RMA:PKTHANDLER for Accumulate: predef dtype");
    MPIU_INSTR_DURATION_INIT(rmapkt_acc_immed,0,"RMA:PKTHANDLER for Accum immed");
73
    MPIU_INSTR_DURATION_INIT(rmapkt_acc_immed_op,0,"RMA:PKTHANDLER for Accum immed operation");
74
    MPIU_INSTR_DURATION_INIT(rmapkt_cas,0,"RMA:PKTHANDLER for Compare-and-swap");
75
    MPIU_INSTR_DURATION_INIT(rmapkt_fop,0,"RMA:PKTHANDLER for Fetch-and-op");
76
    MPIU_INSTR_DURATION_INIT(rmapkt_get_accum,0,"RMA:PKTHANDLER for Get-Accumulate");
77
}
78 79 80 81 82 83 84 85 86 87

/* These are used to use a common routine to complete lists of RMA 
   operations with a single routine, while collecting data that 
   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.
*/
static MPIU_INSTR_Duration_count *list_complete;  /* outer */
static MPIU_INSTR_Duration_count *list_block;     /* Inner; while waiting */

88 89
#endif

90 91 92
/*
 * These routines provide a default implementation of the MPI RMA operations
 * in terms of the low-level, two-sided channel operations.  A channel
93 94
 * may override these functions, on a per-window basis, by overriding
 * the MPID functions in the RMAFns section of MPID_Win object.
95 96
 */

97 98
#define SYNC_POST_TAG 100

99
static int MPIDI_CH3I_Send_lock_msg(int dest, int lock_type, MPID_Win *win_ptr);
100
static int MPIDI_CH3I_Send_unlock_msg(int dest, MPID_Win *win_ptr);
Pavan Balaji's avatar
Pavan Balaji committed
101
/* static int MPIDI_CH3I_Send_flush_msg(int dest, MPID_Win *win_ptr); */
102
static int MPIDI_CH3I_Wait_for_lock_granted(MPID_Win *win_ptr, int target_rank);
103
static int MPIDI_CH3I_Acquire_local_lock(MPID_Win *win_ptr, int lock_mode);
104
static int MPIDI_CH3I_Send_rma_msg(MPIDI_RMA_Op_t * rma_op, MPID_Win * win_ptr,
105
                                   MPIDI_CH3_Pkt_flags_t flags,
106 107 108 109
				   MPI_Win source_win_handle, 
				   MPI_Win target_win_handle, 
				   MPIDI_RMA_dtype_info * dtype_info, 
				   void ** dataloop, MPID_Request ** request);
110
static int MPIDI_CH3I_Recv_rma_msg(MPIDI_RMA_Op_t * rma_op, MPID_Win * win_ptr,
111
                                   MPIDI_CH3_Pkt_flags_t flags,
112 113 114 115
				   MPI_Win source_win_handle, 
				   MPI_Win target_win_handle, 
				   MPIDI_RMA_dtype_info * dtype_info, 
				   void ** dataloop, MPID_Request ** request); 
116
static int MPIDI_CH3I_Send_contig_acc_msg(MPIDI_RMA_Op_t *, MPID_Win *,
117
                                          MPIDI_CH3_Pkt_flags_t flags,
118
					  MPI_Win, MPI_Win, MPID_Request ** );
119
static int MPIDI_CH3I_Send_immed_rmw_msg(MPIDI_RMA_Op_t *, MPID_Win *,
120
                                         MPIDI_CH3_Pkt_flags_t flags,
121
                                         MPI_Win, MPI_Win, MPID_Request ** );
122 123
static int MPIDI_CH3I_Do_passive_target_rma(MPID_Win *win_ptr, int target_rank,
                                            int *wait_for_rma_done_pkt,
James Dinan's avatar
James Dinan committed
124
                                            MPIDI_CH3_Pkt_flags_t sync_flags);
125 126
static int MPIDI_CH3I_Send_lock_put_or_acc(MPID_Win *, int);
static int MPIDI_CH3I_Send_lock_get(MPID_Win *, int);
127 128 129 130 131
static int MPIDI_CH3I_RMAListComplete(MPID_Win *win_ptr,
                                      MPIDI_RMA_Ops_list_t *ops_list);
static int MPIDI_CH3I_RMAListPartialComplete(MPID_Win *win_ptr,
                                             MPIDI_RMA_Ops_list_t *ops_list,
                                             MPIDI_RMA_Op_t *last_elm, int *nDone);
132

133 134
static int create_datatype(const MPIDI_RMA_dtype_info *dtype_info,
                           const void *dataloop, MPI_Aint dataloop_sz,
135 136
                           const void *o_addr, int o_count,
			   MPI_Datatype o_datatype,
137
                           MPID_Datatype **combined_dtp);
138

139 140 141
/* Issue an RMA operation -- Before calling this macro, you must define the
 * MPIDI_CH3I_TRACK_RMA_WRITE helper macro.  This macro defines any extra action
 * that should be taken when a write (put/acc) operation is encountered. */
142
#define MPIDI_CH3I_ISSUE_RMA_OP(op_ptr_, win_ptr_, flags_, source_win_handle_, target_win_handle_,err_) \
143 144 145 146 147 148
    do {                                                                                        \
    switch ((op_ptr_)->type)                                                                    \
    {                                                                                           \
        case (MPIDI_RMA_PUT):                                                                   \
        case (MPIDI_RMA_ACCUMULATE):                                                            \
            MPIDI_CH3I_TRACK_RMA_WRITE(op_ptr_, win_ptr_);                                      \
149
            (err_) = MPIDI_CH3I_Send_rma_msg((op_ptr_), (win_ptr_), (flags_), (source_win_handle_), \
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                                                (target_win_handle_), &(op_ptr_)->dtype_info,   \
                                                &(op_ptr_)->dataloop, &(op_ptr_)->request);     \
            if (err_) { MPIU_ERR_POP(err_); }                                                   \
            break;                                                                              \
        case (MPIDI_RMA_GET_ACCUMULATE):                                                        \
            if ((op_ptr_)->op == MPI_NO_OP) {                                                   \
                /* Note: Origin arguments are ignored for NO_OP, so we don't                    \
                 * need to release a ref to the origin datatype. */                             \
                                                                                                \
                /* Convert the GAcc to a Get */                                                 \
                (op_ptr_)->type            = MPIDI_RMA_GET;                                     \
                (op_ptr_)->origin_addr     = (op_ptr_)->result_addr;                            \
                (op_ptr_)->origin_count    = (op_ptr_)->result_count;                           \
                (op_ptr_)->origin_datatype = (op_ptr_)->result_datatype;                        \
                                                                                                \
165
                (err_) = MPIDI_CH3I_Recv_rma_msg((op_ptr_), (win_ptr_), (flags_), (source_win_handle_), \
166 167 168 169
                                                    (target_win_handle_), &(op_ptr_)->dtype_info,\
                                                    &(op_ptr_)->dataloop, &(op_ptr_)->request); \
            } else {                                                                            \
                MPIDI_CH3I_TRACK_RMA_WRITE(op_ptr_, win_ptr_);                                  \
170
                (err_) = MPIDI_CH3I_Send_rma_msg((op_ptr_), (win_ptr_), (flags_), (source_win_handle_), \
171 172 173 174 175 176 177
                                                    (target_win_handle_), &(op_ptr_)->dtype_info,\
                                                    &(op_ptr_)->dataloop, &(op_ptr_)->request); \
            }                                                                                   \
            if (err_) { MPIU_ERR_POP(err_); }                                                   \
            break;                                                                              \
        case MPIDI_RMA_ACC_CONTIG:                                                              \
            MPIDI_CH3I_TRACK_RMA_WRITE(op_ptr_, win_ptr_);                                      \
178
            (err_) = MPIDI_CH3I_Send_contig_acc_msg((op_ptr_), (win_ptr_), (flags_),            \
179 180 181 182 183
                                                       (source_win_handle_), (target_win_handle_),\
                                                       &(op_ptr_)->request );                   \
            if (err_) { MPIU_ERR_POP(err_); }                                                   \
            break;                                                                              \
        case (MPIDI_RMA_GET):                                                                   \
184
            (err_) = MPIDI_CH3I_Recv_rma_msg((op_ptr_), (win_ptr_), (flags_),                   \
185 186 187 188 189 190 191 192
                                                (source_win_handle_), (target_win_handle_),     \
                                                &(op_ptr_)->dtype_info,                         \
                                                &(op_ptr_)->dataloop, &(op_ptr_)->request);     \
            if (err_) { MPIU_ERR_POP(err_); }                                                   \
            break;                                                                              \
        case (MPIDI_RMA_COMPARE_AND_SWAP):                                                      \
        case (MPIDI_RMA_FETCH_AND_OP):                                                          \
            MPIDI_CH3I_TRACK_RMA_WRITE(op_ptr_, win_ptr_);                                      \
193
            (err_) = MPIDI_CH3I_Send_immed_rmw_msg((op_ptr_), (win_ptr_), (flags_),             \
194 195 196 197 198 199 200 201 202 203 204
                                                      (source_win_handle_), (target_win_handle_),\
                                                      &(op_ptr_)->request );                    \
            if (err_) { MPIU_ERR_POP(err_); }                                                   \
            break;                                                                              \
                                                                                                \
        default:                                                                                \
            MPIU_ERR_SETANDJUMP(err_,MPI_ERR_OTHER,"**winInvalidOp");                           \
    }                                                                                           \
    } while (0)


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
/* Perform RMA operation asynchronously if window of processes on the same node
   is allocated on shared memory */
#define MPIDI_CH3I_DO_SHM_OP(op_ptr_, win_ptr_, err_)                                                                       \
    do {                                                                                                                    \
    switch ((op_ptr_)->type)                                                                                                \
    {                                                                                                                       \
        case (MPIDI_RMA_PUT):                                                                                               \
            (err_) = MPIDI_CH3I_Shm_put_op((op_ptr_)->origin_addr, (op_ptr_)->origin_count, (op_ptr_)->origin_datatype,     \
                                           (op_ptr_)->target_rank, (op_ptr_)->target_disp, (op_ptr_)->target_count,         \
                                           (op_ptr_)->target_datatype, (win_ptr_));                                         \
            if (err_) {MPIU_ERR_POP(err_);}                                                                                 \
            break;                                                                                                          \
        case (MPIDI_RMA_ACCUMULATE):                                                                                        \
        case (MPIDI_RMA_ACC_CONTIG):                                                                                        \
            (err_) = MPIDI_CH3I_Shm_acc_op((op_ptr_)->origin_addr, (op_ptr_)->origin_count, (op_ptr_)->origin_datatype,     \
                                           (op_ptr_)->target_rank, (op_ptr_)->target_disp, (op_ptr_)->target_count,         \
                                           (op_ptr_)->target_datatype, (op_ptr_)->op, (win_ptr_));                          \
            if (err_) {MPIU_ERR_POP(err_);}                                                                                 \
            break;                                                                                                          \
        case (MPIDI_RMA_GET_ACCUMULATE):                                                                                    \
            (err_) = MPIDI_CH3I_Shm_get_acc_op((op_ptr_)->origin_addr, (op_ptr_)->origin_count, (op_ptr_)->origin_datatype, \
                                               (op_ptr_)->result_addr, (op_ptr_)->result_count, (op_ptr_)->result_datatype, \
                                               (op_ptr_)->target_rank, (op_ptr_)->target_disp, (op_ptr_)->target_count,     \
                                               (op_ptr_)->target_datatype, (op_ptr_)->op, (win_ptr_));                      \
            if (err_) {MPIU_ERR_POP(err_);}                                                                                 \
            break;                                                                                                          \
        case (MPIDI_RMA_GET):                                                                                               \
            (err_) = MPIDI_CH3I_Shm_get_op((op_ptr_)->origin_addr, (op_ptr_)->origin_count, (op_ptr_)->origin_datatype,     \
                                           (op_ptr_)->target_rank, (op_ptr_)->target_disp, (op_ptr_)->target_count,         \
                                           (op_ptr_)->target_datatype, (win_ptr_));                                         \
            if (err_) {MPIU_ERR_POP(err_);}                                                                                 \
            break;                                                                                                          \
        case (MPIDI_RMA_COMPARE_AND_SWAP):                                                                                  \
            (err_) = MPIDI_CH3I_Shm_cas_op((op_ptr_)->origin_addr, (op_ptr_)->compare_addr, (op_ptr_)->result_addr,         \
                                           (op_ptr_)->origin_datatype, (op_ptr_)->target_rank, (op_ptr_)->target_disp,      \
                                           (win_ptr_));                                                                     \
            if (err_) {MPIU_ERR_POP(err_);}                                                                                 \
            break;                                                                                                          \
        case (MPIDI_RMA_FETCH_AND_OP):                                                                                      \
            (err_) = MPIDI_CH3I_Shm_fop_op((op_ptr_)->origin_addr, (op_ptr_)->result_addr, (op_ptr_)->origin_datatype,      \
                                           (op_ptr_)->target_rank, (op_ptr_)->target_disp, (op_ptr_)->op, (win_ptr_));      \
            if (err_) {MPIU_ERR_POP(err_);}                                                                                 \
            break;                                                                                                          \
        default:                                                                                                            \
            MPIU_ERR_SETANDJUMP(err_,MPI_ERR_OTHER,"**winInvalidOp");                                                       \
    }                                                                                                                       \
    } while (0)


254 255 256 257 258 259 260
#undef FUNCNAME
#define FUNCNAME MPIDI_Win_fence
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
int MPIDI_Win_fence(int assert, MPID_Win *win_ptr)
{
    int mpi_errno = MPI_SUCCESS;
261
    int comm_size;
262
    int *rma_target_proc, *nops_to_proc, i, total_op_count, *curr_ops_cnt;
263
    MPIDI_RMA_Op_t *curr_ptr;
264
    MPIDI_RMA_Ops_list_t *ops_list;
265 266 267
    MPID_Comm *comm_ptr;
    MPI_Win source_win_handle, target_win_handle;
    MPID_Progress_state progress_state;
268
    int errflag = FALSE;
269
    MPIU_CHKLMEM_DECL(3);
270 271 272 273
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_WIN_FENCE);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_WIN_FENCE);

James Dinan's avatar
James Dinan committed
274 275 276 277
    MPIU_ERR_CHKANDJUMP(win_ptr->epoch_state != MPIDI_EPOCH_NONE &&
                        win_ptr->epoch_state != MPIDI_EPOCH_FENCE,
                        mpi_errno, MPI_ERR_RMA_SYNC, "**rmasync");

278 279 280 281 282 283 284 285 286
    /* In case this process was previously the target of passive target rma
     * operations, we need to take care of the following...
     * Since we allow MPI_Win_unlock to return without a done ack from
     * the target in the case of multiple rma ops and exclusive lock,
     * we need to check whether there is a lock on the window, and if
     * there is a lock, poke the progress engine until the operartions
     * have completed and the lock is released. */
    if (win_ptr->current_lock_type != MPID_LOCK_NONE)
    {
287
	MPIU_INSTR_DURATION_START(winfence_clearlock);
288 289 290 291 292 293
	MPID_Progress_start(&progress_state);
	while (win_ptr->current_lock_type != MPID_LOCK_NONE)
	{
	    /* poke the progress engine */
	    mpi_errno = MPID_Progress_wait(&progress_state);
	    /* --BEGIN ERROR HANDLING-- */
294
	    if (mpi_errno != MPI_SUCCESS) {
295
		MPID_Progress_end(&progress_state);
296
		MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER,"**winnoprogress");
297 298
	    }
	    /* --END ERROR HANDLING-- */
299
	    MPIU_INSTR_DURATION_INCR(winfence_clearlock,0,1);
300 301
	}
	MPID_Progress_end(&progress_state);
302
	MPIU_INSTR_DURATION_END(winfence_clearlock);
303
    }
304 305 306

    /* Note that the NOPRECEDE and NOSUCCEED must be specified by all processes
       in the window's group if any specify it */
307 308
    if (assert & MPI_MODE_NOPRECEDE)
    {
309 310 311 312
        /* 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");

313
	win_ptr->fence_issued = (assert & MPI_MODE_NOSUCCEED) ? 0 : 1;
314 315 316
	goto fn_exit;
    }
    
317
    if (win_ptr->fence_issued == 0)
318
    {
319
	/* win_ptr->fence_issued == 0 means either this is the very first
320 321
	   call to fence or the preceding fence had the
	   MPI_MODE_NOSUCCEED assert. 
322 323 324 325

           If this fence has MPI_MODE_NOSUCCEED, do nothing and return.
	   Otherwise just increment the fence count and return. */

326
	if (!(assert & MPI_MODE_NOSUCCEED)) win_ptr->fence_issued = 1;
327 328 329
    }
    else
    {
330 331
	int nRequest = 0;
	int nRequestNew = 0;
332
        MPIDI_VC_t *orig_vc, *target_vc;
333
	MPIU_INSTR_DURATION_START(winfence_rs);
334
	/* This is the second or later fence. Do all the preceding RMA ops. */
335
	comm_ptr = win_ptr->comm_ptr;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	/* 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;

352 353 354
        /* Note, active target uses the following ops list, and passive
           target uses win_ptr->targets[..] */
        ops_list = &win_ptr->at_rma_ops_list;
355

356 357 358
	/* set rma_target_proc[i] to 1 if rank i is a target of RMA
	   ops from this process */
	total_op_count = 0;
359
        curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
360
        MPIDI_Comm_get_vc(win_ptr->comm_ptr, win_ptr->comm_ptr->rank, &orig_vc);
361 362
	while (curr_ptr != NULL)
	{
363 364
            MPIDI_Comm_get_vc(win_ptr->comm_ptr, curr_ptr->target_rank, &target_vc);
	    if (!(win_ptr->shm_allocated == TRUE && orig_vc->node_id == target_vc->node_id)) {
365 366 367 368
		total_op_count++;
		rma_target_proc[curr_ptr->target_rank] = 1;
		nops_to_proc[curr_ptr->target_rank]++;
	    }
369 370 371 372 373 374
	    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;
375 376
	/* do a reduce_scatter_block (with MPI_SUM) on rma_target_proc. 
	   As a result,
377 378 379 380 381 382
	   each process knows how many other processes will be doing
	   RMA ops on its window */  
            
	/* first initialize the completion counter. */
	win_ptr->my_counter = comm_size;
            
383
	mpi_errno = MPIR_Reduce_scatter_block_impl(MPI_IN_PLACE, rma_target_proc, 1,
384
                                                   MPI_INT, MPI_SUM, comm_ptr, &errflag);
385
	MPIU_INSTR_DURATION_END(winfence_rs);
386 387
	/* result is stored in rma_target_proc[0] */
	if (mpi_errno) { MPIU_ERR_POP(mpi_errno); }
388
        MPIU_ERR_CHKANDJUMP(errflag, mpi_errno, MPI_ERR_OTHER, "**coll_fail");
389 390 391 392 393 394

	/* Set the completion counter */
	/* FIXME: MT: this needs to be done atomically because other
	   procs have the address and could decrement it. */
	win_ptr->my_counter = win_ptr->my_counter - comm_size + 
	    rma_target_proc[0];  
395 396 397 398

	MPIU_INSTR_DURATION_START(winfence_issue);
	MPIU_INSTR_DURATION_INCR(winfence_issue,0,total_op_count);
	MPIU_INSTR_DURATION_MAX(winfence_issue,1,total_op_count);
399
	MPIU_INSTR_COUNTER_RESET(winfence_reqs);
400
	i = 0;
401
        curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
402 403
	while (curr_ptr != NULL)
	{
404 405
          MPIDI_Comm_get_vc(win_ptr->comm_ptr, curr_ptr->target_rank, &target_vc);
          if (win_ptr->shm_allocated == TRUE && orig_vc->node_id == target_vc->node_id) {
406 407 408 409 410
            MPIDI_CH3I_DO_SHM_OP(curr_ptr, win_ptr, mpi_errno);
            MPIDI_CH3I_RMA_Ops_free_and_next(ops_list, &curr_ptr);
          }
          else {

411 412
            MPIDI_CH3_Pkt_flags_t flags = MPIDI_CH3_PKT_FLAG_NONE;

413
	    /* The completion counter at the target is decremented only on 
414
	       the last RMA operation. */
415
	    if (curr_ops_cnt[curr_ptr->target_rank] ==
416 417 418 419 420
                nops_to_proc[curr_ptr->target_rank] - 1) {
                flags = MPIDI_CH3_PKT_FLAG_RMA_AT_COMPLETE;
            }

            source_win_handle = win_ptr->handle;
421
	    target_win_handle = win_ptr->all_win_handles[curr_ptr->target_rank];
422

423
#define MPIDI_CH3I_TRACK_RMA_WRITE(op_ptr_, win_ptr_) /* Not used by active mode */
424
            MPIDI_CH3I_ISSUE_RMA_OP(curr_ptr, win_ptr, flags,
425
                                    source_win_handle, target_win_handle, mpi_errno);
426
#undef MPIDI_CH3I_TRACK_RMA_WRITE
427

428 429
	    i++;
	    curr_ops_cnt[curr_ptr->target_rank]++;
430 431
	    /* If the request is null, we can remove it immediately */
	    if (!curr_ptr->request) {
432
                MPIDI_CH3I_RMA_Ops_free_and_next(ops_list, &curr_ptr);
433 434
	    }
	    else  {
435 436
		nRequest++;
		MPIU_INSTR_COUNTER_INCR(winfence_reqs,1);
437
		curr_ptr    = curr_ptr->next;
438 439 440 441 442
		/* 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. */
443 444
		if (nRequest > MPIR_PARAM_CH3_RMA_NREQUEST_THRESHOLD &&
		    nRequest - nRequestNew > MPIR_PARAM_CH3_RMA_NREQUEST_NEW_THRESHOLD) {
445
		    int nDone = 0;
Xin Zhao's avatar
Xin Zhao committed
446
		    MPIU_INSTR_STMT(list_complete=MPIU_INSTR_GET_VAR(winfence_complete));
447
                    MPIDI_CH3I_RMAListPartialComplete(win_ptr, ops_list, curr_ptr, &nDone);
448 449 450 451
		    /* if (nDone > 0) printf( "nDone = %d\n", nDone ); */
		    nRequest -= nDone;
		    nRequestNew = nRequest;
		}
452
	    }
453
	  } /* end of else */
454
	}
455 456 457 458 459 460 461 462 463 464 465 466 467
	MPIU_INSTR_DURATION_END(winfence_issue);

	/* 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.
	*/
468 469
	if (total_op_count)
	{ 
470 471
	    MPIU_INSTR_STMT(list_complete=MPIU_INSTR_GET_VAR(winfence_complete));
	    MPIU_INSTR_STMT(list_block=MPIU_INSTR_GET_VAR(winfence_block));
472
            mpi_errno = MPIDI_CH3I_RMAListComplete(win_ptr, ops_list);
473
	}
474

475
        MPIU_Assert(MPIDI_CH3I_RMA_Ops_isempty(ops_list));
476 477 478 479
	
	/* wait for all operations from other processes to finish */
	if (win_ptr->my_counter)
	{
480
	    MPIU_INSTR_DURATION_START(winfence_wait);
481 482 483 484 485
	    MPID_Progress_start(&progress_state);
	    while (win_ptr->my_counter)
	    {
		mpi_errno = MPID_Progress_wait(&progress_state);
		/* --BEGIN ERROR HANDLING-- */
486
		if (mpi_errno != MPI_SUCCESS) {
487
		    MPID_Progress_end(&progress_state);
488
		    MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER,"**winnoprogress");
489 490
		}
		/* --END ERROR HANDLING-- */
491
		MPIU_INSTR_DURATION_INCR(winfence_wait,0,1);
492 493
	    }
	    MPID_Progress_end(&progress_state);
494
	    MPIU_INSTR_DURATION_END(winfence_wait);
495 496 497 498
	} 
	
	if (assert & MPI_MODE_NOSUCCEED)
	{
499
	    win_ptr->fence_issued = 0;
500 501
	}

James Dinan's avatar
James Dinan committed
502 503 504
        win_ptr->epoch_state = MPIDI_EPOCH_NONE;
    }

505
 fn_exit:
506 507 508 509 510 511 512 513 514
    MPIU_CHKLMEM_FREEALL();
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_MPIDI_WIN_FENCE);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
 fn_fail:
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
/* 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);
    blocklens[1] = dataloop_sz;
    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);
559 560
    if (mpi_errno) MPIU_ERR_POP(mpi_errno);
   
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
    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;
}

596 597 598 599 600

#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_Send_rma_msg
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
601
static int MPIDI_CH3I_Send_rma_msg(MPIDI_RMA_Op_t *rma_op, MPID_Win *win_ptr,
602
                                   MPIDI_CH3_Pkt_flags_t flags,
603 604 605 606 607 608 609 610 611 612
				   MPI_Win source_win_handle, 
				   MPI_Win target_win_handle, 
				   MPIDI_RMA_dtype_info *dtype_info, 
				   void **dataloop, MPID_Request **request) 
{
    MPIDI_CH3_Pkt_t upkt;
    MPIDI_CH3_Pkt_put_t *put_pkt = &upkt.put;
    MPIDI_CH3_Pkt_accum_t *accum_pkt = &upkt.accum;
    MPID_IOV iov[MPID_IOV_LIMIT];
    int mpi_errno=MPI_SUCCESS, predefined;
613
    int origin_dt_derived, target_dt_derived, origin_type_size, iovcnt; 
614 615 616
    MPIDI_VC_t * vc;
    MPID_Comm *comm_ptr;
    MPID_Datatype *target_dtp=NULL, *origin_dtp=NULL;
617
    MPID_Request *resp_req=NULL;
618 619 620 621 622 623
    MPIU_CHKPMEM_DECL(1);
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3I_SEND_RMA_MSG);
    MPIDI_STATE_DECL(MPID_STATE_MEMCPY);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_CH3I_SEND_RMA_MSG);

624 625
    *request = NULL;

626 627 628 629 630
    if (rma_op->type == MPIDI_RMA_PUT)
    {
        MPIDI_Pkt_init(put_pkt, MPIDI_CH3_PKT_PUT);
        put_pkt->addr = (char *) win_ptr->base_addrs[rma_op->target_rank] +
            win_ptr->disp_units[rma_op->target_rank] * rma_op->target_disp;
631
        put_pkt->flags = flags;
632 633 634 635 636
        put_pkt->count = rma_op->target_count;
        put_pkt->datatype = rma_op->target_datatype;
        put_pkt->dataloop_size = 0;
        put_pkt->target_win_handle = target_win_handle;
        put_pkt->source_win_handle = source_win_handle;
637
        
638 639 640
        iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) put_pkt;
        iov[0].MPID_IOV_LEN = sizeof(*put_pkt);
    }
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
    else if (rma_op->type == MPIDI_RMA_GET_ACCUMULATE)
    {
        /* 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;
        resp_req->dev.target_win_handle = target_win_handle;
        resp_req->dev.source_win_handle = source_win_handle;

        MPIDI_CH3I_DATATYPE_IS_PREDEFINED(resp_req->dev.datatype, predefined);
        if (!predefined) {
            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. */
        }

        /* Note: Get_accumulate uses the same packet type as accumulate */
        MPIDI_Pkt_init(accum_pkt, MPIDI_CH3_PKT_GET_ACCUM);
        accum_pkt->addr = (char *) win_ptr->base_addrs[rma_op->target_rank] +
            win_ptr->disp_units[rma_op->target_rank] * rma_op->target_disp;
670
        accum_pkt->flags = flags;
671 672 673 674 675 676 677 678 679 680 681
        accum_pkt->count = rma_op->target_count;
        accum_pkt->datatype = rma_op->target_datatype;
        accum_pkt->dataloop_size = 0;
        accum_pkt->op = rma_op->op;
        accum_pkt->target_win_handle = target_win_handle;
        accum_pkt->source_win_handle = source_win_handle;
        accum_pkt->request_handle = resp_req->handle;

        iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) accum_pkt;
        iov[0].MPID_IOV_LEN = sizeof(*accum_pkt);
    }
682 683 684 685 686
    else
    {
        MPIDI_Pkt_init(accum_pkt, MPIDI_CH3_PKT_ACCUMULATE);
        accum_pkt->addr = (char *) win_ptr->base_addrs[rma_op->target_rank] +
            win_ptr->disp_units[rma_op->target_rank] * rma_op->target_disp;
687
        accum_pkt->flags = flags;
688 689 690 691 692 693 694 695 696 697 698
        accum_pkt->count = rma_op->target_count;
        accum_pkt->datatype = rma_op->target_datatype;
        accum_pkt->dataloop_size = 0;
        accum_pkt->op = rma_op->op;
        accum_pkt->target_win_handle = target_win_handle;
        accum_pkt->source_win_handle = source_win_handle;

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

699 700 701 702
    /*    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);
    */
703

704
    comm_ptr = win_ptr->comm_ptr;
705
    MPIDI_Comm_get_vc_set_active(comm_ptr, rma_op->target_rank, &vc);
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

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

    MPIDI_CH3I_DATATYPE_IS_PREDEFINED(rma_op->target_datatype, predefined);
    if (!predefined)
    {
        target_dt_derived = 1;
        MPID_Datatype_get_ptr(rma_op->target_datatype, target_dtp);
    }
    else
    {
        target_dt_derived = 0;
    }

    if (target_dt_derived)
    {
        /* derived datatype on target. fill derived datatype info */
        dtype_info->is_contig = target_dtp->is_contig;
733
        dtype_info->max_contig_blocks = target_dtp->max_contig_blocks;
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
        dtype_info->size = target_dtp->size;
        dtype_info->extent = target_dtp->extent;
        dtype_info->dataloop_size = target_dtp->dataloop_size;
        dtype_info->dataloop_depth = target_dtp->dataloop_depth;
        dtype_info->eltype = target_dtp->eltype;
        dtype_info->dataloop = target_dtp->dataloop;
        dtype_info->ub = target_dtp->ub;
        dtype_info->lb = target_dtp->lb;
        dtype_info->true_ub = target_dtp->true_ub;
        dtype_info->true_lb = target_dtp->true_lb;
        dtype_info->has_sticky_ub = target_dtp->has_sticky_ub;
        dtype_info->has_sticky_lb = target_dtp->has_sticky_lb;

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

	MPIDI_FUNC_ENTER(MPID_STATE_MEMCPY);
751
        MPIU_Memcpy(*dataloop, target_dtp->dataloop, target_dtp->dataloop_size);
752
	MPIDI_FUNC_EXIT(MPID_STATE_MEMCPY);
753 754
        /* 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 */
755
        MPL_VG_MAKE_MEM_DEFINED(*dataloop, target_dtp->dataloop_size);
756 757 758 759 760 761 762 763 764 765 766 767 768

        if (rma_op->type == MPIDI_RMA_PUT)
	{
            put_pkt->dataloop_size = target_dtp->dataloop_size;
	}
        else
	{
            accum_pkt->dataloop_size = target_dtp->dataloop_size;
	}
    }

    MPID_Datatype_get_size_macro(rma_op->origin_datatype, origin_type_size);

769
    if (!target_dt_derived)
770
    {
771 772 773 774
        /* basic datatype on target */
        if (!origin_dt_derived)
        {
            /* basic datatype on origin */
775 776 777
            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;
778
	    MPIU_THREAD_CS_ENTER(CH3COMM,vc);
779
            mpi_errno = MPIDI_CH3_iStartMsgv(vc, iov, iovcnt, request);
780
	    MPIU_THREAD_CS_EXIT(CH3COMM,vc);
781
            MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
782 783
        }
        else
784 785 786
        {
            /* derived datatype on origin */
            *request = MPID_Request_create();
787
            MPIU_ERR_CHKANDJUMP(*request == NULL,mpi_errno,MPI_ERR_OTHER,"**nomemreq");
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
            
            MPIU_Object_set_ref(*request, 2);
            (*request)->kind = MPID_REQUEST_SEND;
            
            (*request)->dev.segment_ptr = MPID_Segment_alloc( );
            MPIU_ERR_CHKANDJUMP1((*request)->dev.segment_ptr == NULL, mpi_errno, MPI_ERR_OTHER, "**nomem", "**nomem %s", "MPID_Segment_alloc");

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

            (*request)->dev.OnFinal = 0;
            (*request)->dev.OnDataAvail = 0;

807
	    MPIU_THREAD_CS_ENTER(CH3COMM,vc);
808
            mpi_errno = vc->sendNoncontig_fn(vc, *request, iov[0].MPID_IOV_BUF, iov[0].MPID_IOV_LEN);
809
	    MPIU_THREAD_CS_EXIT(CH3COMM,vc);
810
            MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
811 812 813 814
        }
    }
    else
    {
815
        /* derived datatype on target */
816
        MPID_Datatype *combined_dtp = NULL;
817 818 819

        *request = MPID_Request_create();
        if (*request == NULL) {
820
	    MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER,"**nomemreq");
821 822 823 824 825 826
        }

        MPIU_Object_set_ref(*request, 2);
        (*request)->kind = MPID_REQUEST_SEND;

	(*request)->dev.segment_ptr = MPID_Segment_alloc( );
827 828 829 830
        MPIU_ERR_CHKANDJUMP1((*request)->dev.segment_ptr == NULL, mpi_errno, MPI_ERR_OTHER, "**nomem", "**nomem %s", "MPID_Segment_alloc");

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

831 832
        mpi_errno = create_datatype(dtype_info, *dataloop, target_dtp->dataloop_size, rma_op->origin_addr,
                                    rma_op->origin_count, rma_op->origin_datatype, &combined_dtp);
833
        if (mpi_errno) MPIU_ERR_POP(mpi_errno);
834 835 836 837 838

        (*request)->dev.datatype_ptr = combined_dtp;
        /* combined_datatype will be freed when request is freed */

        MPID_Segment_init(MPI_BOTTOM, 1, combined_dtp->handle,
839 840
                          (*request)->dev.segment_ptr, 0);
        (*request)->dev.segment_first = 0;
841 842 843 844 845
        (*request)->dev.segment_size = combined_dtp->size;

        (*request)->dev.OnFinal = 0;
        (*request)->dev.OnDataAvail = 0;

846
	MPIU_THREAD_CS_ENTER(CH3COMM,vc);
847
        mpi_errno = vc->sendNoncontig_fn(vc, *request, iov[0].MPID_IOV_BUF, iov[0].MPID_IOV_LEN);
848
	MPIU_THREAD_CS_EXIT(CH3COMM,vc);
849
        MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
850

851 852 853
        /* we're done with the datatypes */
        if (origin_dt_derived)
            MPID_Datatype_release(origin_dtp);
854
        MPID_Datatype_release(target_dtp);
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
    }

    /* This operation can generate two requests; one for inbound and one for
       outbound data. */
    if (resp_req != NULL) {
        if (*request != NULL) {
            /* 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.
             */

876
            MPID_Request_release(*request);
877 878 879 880 881 882 883 884 885
            *request = resp_req;

        } else {
            *request = resp_req;
        }

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

 fn_exit:
888
    MPIU_CHKPMEM_COMMIT();
889 890 891 892
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_MPIDI_CH3I_SEND_RMA_MSG);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
 fn_fail:
893 894 895 896
    if (resp_req) {
        MPIU_Object_set_ref(resp_req, 0);
        MPIDI_CH3_Request_destroy(resp_req);
    }
897 898 899
    if (*request)
    {
        MPIU_CHKPMEM_REAP();
900 901
        if ((*request)->dev.datatype_ptr)
            MPID_Datatype_release((*request)->dev.datatype_ptr);
902 903 904 905
        MPIU_Object_set_ref(*request, 0);
        MPIDI_CH3_Request_destroy(*request);
    }
    *request = NULL;
906 907 908 909
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}

910 911 912 913 914 915 916
/*
 * Use this for contiguous accumulate operations
 */
#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_Send_contig_acc_msg
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
917
static int MPIDI_CH3I_Send_contig_acc_msg(MPIDI_RMA_Op_t *rma_op,
918
					  MPID_Win *win_ptr,
919
                                          MPIDI_CH3_Pkt_flags_t flags,
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
					  MPI_Win source_win_handle, 
					  MPI_Win target_win_handle, 
					  MPID_Request **request) 
{
    MPIDI_CH3_Pkt_t upkt;
    MPIDI_CH3_Pkt_accum_t *accum_pkt = &upkt.accum;
    MPID_IOV iov[MPID_IOV_LIMIT];
    int mpi_errno=MPI_SUCCESS;
    int origin_type_size, iovcnt; 
    MPIDI_VC_t * vc;
    MPID_Comm *comm_ptr;
    int len;
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3I_SEND_CONTIG_ACC_MSG);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_CH3I_SEND_CONTIG_ACC_MSG);

    *request = NULL;

    MPID_Datatype_get_size_macro(rma_op->origin_datatype, origin_type_size);
    /* FIXME: Make this size check efficient and match the packet type */
    len = rma_op->origin_count * origin_type_size;
941
    if (MPIR_PARAM_CH3_RMA_ACC_IMMED && len <= MPIDI_RMA_IMMED_INTS*sizeof(int)) {
942 943 944 945 946 947
	MPIDI_CH3_Pkt_accum_immed_t * accumi_pkt = &upkt.accum_immed;
	void *dest = accumi_pkt->data, *src = rma_op->origin_addr;
	
	MPIDI_Pkt_init(accumi_pkt, MPIDI_CH3_PKT_ACCUM_IMMED);
	accumi_pkt->addr = (char *) win_ptr->base_addrs[rma_op->target_rank] +
	    win_ptr->disp_units[rma_op->target_rank] * rma_op->target_disp;
948
        accumi_pkt->flags = flags;
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
	accumi_pkt->count = rma_op->target_count;
	accumi_pkt->datatype = rma_op->target_datatype;
	accumi_pkt->op = rma_op->op;
	accumi_pkt->target_win_handle = target_win_handle;
	accumi_pkt->source_win_handle = source_win_handle;
	
	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);
966
	mpi_errno = MPIDI_CH3_iStartMsg(vc, accumi_pkt, sizeof(*accumi_pkt), request);
967 968 969 970 971 972 973 974
	MPIU_THREAD_CS_EXIT(CH3COMM,vc);
	MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
	goto fn_exit;
    }

    MPIDI_Pkt_init(accum_pkt, MPIDI_CH3_PKT_ACCUMULATE);
    accum_pkt->addr = (char *) win_ptr->base_addrs[rma_op->target_rank] +
	win_ptr->disp_units[rma_op->target_rank] * rma_op->target_disp;
975
    accum_pkt->flags = flags;
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    accum_pkt->count = rma_op->target_count;
    accum_pkt->datatype = rma_op->target_datatype;
    accum_pkt->dataloop_size = 0;
    accum_pkt->op = rma_op->op;
    accum_pkt->target_win_handle = target_win_handle;
    accum_pkt->source_win_handle = source_win_handle;
    
    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);
1005
    mpi_errno = MPIDI_CH3_iStartMsgv(vc, iov, iovcnt, request);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    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_MPIDI_CH3I_SEND_CONTIG_ACC_MSG);
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
 fn_fail:
    if (*request)
    {
        MPIU_Object_set_ref(*request, 0);
        MPIDI_CH3_Request_destroy(*request);
    }
    *request = NULL;
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}

1024

1025 1026 1027 1028 1029 1030 1031
/*
 * Initiate an immediate RMW accumulate operation
 */
#undef FUNCNAME
#define FUNCNAME MPIDI_CH3I_Send_immed_rmw_msg
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
1032
static int MPIDI_CH3I_Send_immed_rmw_msg(MPIDI_RMA_Op_t *rma_op,
1033
                                         MPID_Win *win_ptr,
1034
                                         MPIDI_CH3_Pkt_flags_t flags,
1035 1036 1037 1038 1039
                                         MPI_Win source_win_handle, 
                                         MPI_Win target_win_handle, 
                                         MPID_Request **request) 
{
    int mpi_errno = MPI_SUCCESS;
1040
    MPID_Request *rmw_req = NULL, *resp_req = NULL;
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    MPIDI_VC_t *vc;
    MPID_Comm *comm_ptr;
    int len;
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3I_SEND_IMMED_RMW_MSG);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_CH3I_SEND_IMMED_RMW_MSG);

    *request = NULL;

    /* 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. */
    resp_req = MPID_Request_create();
    MPIU_ERR_CHKANDJUMP(resp_req == NULL, mpi_errno, MPI_ERR_OTHER, "**nomemreq");
    *request = resp_req;

    /* Set refs on the request to 2: one for the response message, and one for
       the partial completion handler */
    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;
1064
    resp_req->dev.target_win_handle = target_win_handle;
1065 1066 1067 1068 1069
    resp_req->dev.source_win_handle = source_win_handle;

    /* 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);
1070
    comm_ptr = win_ptr->comm_ptr;
1071 1072 1073 1074 1075 1076 1077

    if (rma_op->type == MPIDI_RMA_COMPARE_AND_SWAP) {
        MPIDI_CH3_Pkt_t upkt;
        MPIDI_CH3_Pkt_cas_t *cas_pkt = &upkt.cas;

        MPIU_Assert(len <= sizeof(MPIDI_CH3_CAS_Immed_u));

1078
        MPIDI_Pkt_init(cas_pkt, MPIDI_CH3_PKT_CAS);
1079

1080 1081
        cas_pkt->addr = (char *) win_ptr->base_addrs[rma_op->target_rank] +
            win_ptr->disp_units[rma_op->target_rank] * rma_op->target_disp;
1082
        cas_pkt->flags = flags;
1083 1084 1085 1086 1087 1088 1089 1090 1091
        cas_pkt->datatype = rma_op->target_datatype;
        cas_pkt->target_win_handle = target_win_handle;
        cas_pkt->request_handle = resp_req->handle;

        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);
1092
        mpi_errno = MPIDI_CH3_iStartMsg(vc, cas_pkt, sizeof(*cas_pkt), &rmw_req);
1093 1094
        MPIU_THREAD_CS_EXIT(CH3COMM,vc);
        MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
1095 1096 1097 1098

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

1101 1102 1103 1104 1105 1106
    else if (rma_op->type == MPIDI_RMA_FETCH_AND_OP) {
        MPIDI_CH3_Pkt_t upkt;
        MPIDI_CH3_Pkt_fop_t *fop_pkt = &upkt.fop;

        MPIU_Assert(len <= sizeof(MPIDI_CH3_FOP_Immed_u));

1107
        MPIDI_Pkt_init(fop_pkt, MPIDI_CH3_PKT_FOP);
1108

1109 1110
        fop_pkt->addr = (char *) win_ptr->base_addrs[rma_op->target_rank] +
            win_ptr->disp_units[rma_op->target_rank] * rma_op->target_disp;
1111
        fop_pkt->flags = flags;
1112 1113 1114 1115 1116
        fop_pkt->datatype = rma_op->target_datatype;
        fop_pkt->target_win_handle = target_win_handle;
        fop_pkt->request_handle = resp_req->handle;
        fop_pkt->op = rma_op->op;

1117
        if (len <= sizeof(fop_pkt->origin_data) || rma_op->op == MPI_NO_OP) {
1118
            /* Embed FOP data in the packet header */
1119
            if (rma_op->op != MPI_NO_OP) {
James Dinan's avatar