ch3u_rma_sync.c 213 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
=== BEGIN_MPI_T_CVAR_INFO_BLOCK ===

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

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

    - 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
      default     : 128
      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.

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    - name        : MPIR_CVAR_CH3_RMA_GC_NUM_COMPLETED
      category    : CH3
      type        : int
      default     : 1
      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.
        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.

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    - 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 ===
*/

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_lockqueue_alloc);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winfence_clearlock);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winfence_clearlock_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winfence_rs);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winfence_issue);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winfence_issue_aux);
MPIR_T_PVAR_ULONG2_HIGHWATERMARK_DECL(RMA, rma_winfence_issue_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winfence_complete);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winfence_complete_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winfence_wait);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winfence_wait_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winfence_block);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winfence_block_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winpost_clearlock);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winpost_clearlock_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winpost_sendsync);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winpost_sendsync_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winstart_clearlock);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winstart_clearlock_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_wincomplete_issue);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_wincomplete_issue_aux);
MPIR_T_PVAR_ULONG2_HIGHWATERMARK_DECL(RMA, rma_wincomplete_issue_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_wincomplete_complete);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_wincomplete_complete_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_wincomplete_recvsync);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_wincomplete_recvsync_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_wincomplete_block);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_wincomplete_block_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winwait_wait);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winwait_wait_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winlock_getlocallock);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winunlock_getlock);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winunlock_issue);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winunlock_complete);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winunlock_complete_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winunlock_block);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winunlock_block_aux);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmapkt_acc);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmapkt_acc_predef);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmapkt_acc_immed);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmapkt_acc_immed_op);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmapkt_cas);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmapkt_fop);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmapkt_get_accum);

MPIR_T_PVAR_ULONG2_LEVEL_DECL(RMA, rma_winfence_reqs);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winfence_reqs);
MPIR_T_PVAR_ULONG2_HIGHWATERMARK_DECL(RMA, rma_winfence_reqs);

MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_winunlock_reqs);
MPIR_T_PVAR_ULONG2_COUNTER_DECL(RMA, rma_wincomplete_reqs);

MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_wincreate_allgather);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winfree_rs);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_winfree_complete);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmaqueue_alloc);
MPIR_T_PVAR_DOUBLE_TIMER_DECL(RMA, rma_rmaqueue_set);

void MPIDI_CH3_RMA_Init_Pvars(void)
178
{
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
    /* rma_lockqueue_alloc */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_lockqueue_alloc,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "Allocate Lock Queue element (in seconds)");

    /* rma_winfence_clearlock */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winfence_clearlock,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Clear prior lock (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_clearlock_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Clear prior lock");

    /* rma_winfence_rs */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winfence_rs,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:ReduceScatterBlock (in seconds)");

    /* rma_winfence_issue and auxiliaries */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winfence_issue,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Issue RMA ops (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_issue_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Issue RMA ops");

    MPIR_T_PVAR_HIGHWATERMARK_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_issue_aux,
        0, /* init value */
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Issue RMA ops");

    /* rma_winfence_complete */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winfence_complete,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Complete RMA ops (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_complete_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Complete RMA ops");

    /* rma_winfence_wait */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winfence_wait,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Wait for ops from other processes (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_wait_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Wait for ops from other processes");

    /* rma_winfence_block */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winfence_block,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Wait for any progress (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_block_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Wait for any progress");


    /* rma_winpost_clearlock */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winpost_clearlock,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_POST:Clear prior lock  (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winpost_clearlock_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_POST:Clear prior lock");

    /* rma_winpost_sendsync */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winpost_sendsync,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_POST:Senc sync messages (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winpost_sendsync_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_POST:Senc sync messages");

    /* rma_winstart_clearlock */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winstart_clearlock,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_START:Clear prior lock (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winstart_clearlock_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_START:Clear prior lock");

    /* rma_wincomplete_issue and auxiliaries */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_wincomplete_issue,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Issue RMA ops (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_wincomplete_issue_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Issue RMA ops");

    MPIR_T_PVAR_HIGHWATERMARK_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_wincomplete_issue_aux,
        0, /* init value */
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Issue RMA ops");

    /* rma_wincomplete_complete */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_wincomplete_complete,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Complete RMA ops (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_wincomplete_complete_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Complete RMA ops");

    /* rma_wincomplete_recvsync */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_wincomplete_recvsync,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Recv sync messages (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_wincomplete_recvsync_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Recv sync messages");

    /* rma_wincomplete_block */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_wincomplete_block,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Wait for any progress (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_wincomplete_block_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Wait for any progress");

    /* rma_winwait_wait */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winwait_wait,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_WAIT:Wait for ops from other processes (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winwait_wait_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_WAIT:Wait for ops from other processes");

    /* rma_winlock_getlocallock */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winlock_getlocallock,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_LOCK:Get local lock (in seconds)");

    /* rma_winunlock_getlock */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winunlock_getlock,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_UNLOCK:Acquire lock (in seconds)");

    /* rma_winunlock_issue */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winunlock_issue,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_UNLOCK:Issue RMA ops (in seconds)");

    /* rma_winunlock_complete */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winunlock_complete,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_UNLOCK:Complete RMA ops (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winunlock_complete_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_UNLOCK:Complete RMA ops (in seconds)");

    /* rma_winunlock_block */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winunlock_block,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_UNLOCK:Wait for any progress (in seconds)");

    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winunlock_block_aux,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_UNLOCK:Wait for any progress");

    /* rma_rmapkt_acc */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmapkt_acc,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "RMA:PKTHANDLER for Accumulate (in seconds)");

    /* rma_rmapkt_acc_predef */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmapkt_acc_predef,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "RMA:PKTHANDLER for Accumulate: predef dtype (in seconds)");

    /* rma_rmapkt_acc_immed */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmapkt_acc_immed,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "RMA:PKTHANDLER for Accum immed (in seconds)");

    /* rma_rmapkt_acc_immed_op */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmapkt_acc_immed_op,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "RMA:PKTHANDLER for Accum immed operation (in seconds)");

    /* rma_rmapkt_cas */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmapkt_cas,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "RMA:PKTHANDLER for Compare-and-swap (in seconds)");

    /* rma_rmapkt_fop */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmapkt_fop,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "RMA:PKTHANDLER for Fetch-and-op (in seconds)");

    /* rma_rmapkt_get_accum */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmapkt_get_accum,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "RMA:PKTHANDLER for Get-Accumulate (in seconds)");

    /* Level, counter and highwatermark for rma_winfence_reqs */
    MPIR_T_PVAR_LEVEL_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_reqs,
        0, /* init value */
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY | MPIR_T_PVAR_FLAG_CONTINUOUS,
        "RMA",
        "WIN_FENCE:Pending requests");

      MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_reqs,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Pending requests");

      MPIR_T_PVAR_HIGHWATERMARK_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winfence_reqs,
        0, /* init value */
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FENCE:Pending requests");

    /* rma_winunlock_reqs */
    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_winunlock_reqs,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_UNLOCK:Pending requests");

    /* rma_wincomplete_reqs */
    MPIR_T_PVAR_COUNTER_REGISTER_STATIC(
        RMA,
        MPI_UNSIGNED_LONG_LONG,
        rma_wincomplete_reqs,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_COMPLETE:Pending requests");

    /* rma_wincreate_allgather */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_wincreate_allgather,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_CREATE:Allgather (in seconds)");

    /* rma_winfree_rs */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winfree_rs,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FREE:ReduceScatterBlock (in seconds)");

    /* rma_winfree_complete */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_winfree_complete,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "WIN_FREE:Complete (in seconds)");

    /* rma_rmaqueue_alloc */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmaqueue_alloc,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "Allocate RMA Queue element (in seconds)");

    /* rma_rmaqueue_set */
    MPIR_T_PVAR_TIMER_REGISTER_STATIC(
        RMA,
        MPI_DOUBLE,
        rma_rmaqueue_set,
        MPI_T_VERBOSITY_MPIDEV_DETAIL,
        MPI_T_BIND_NO_OBJECT,
        MPIR_T_PVAR_FLAG_READONLY,
        "RMA",
        "Set fields in RMA Queue element (in seconds)");
758
}
759
760
761
762
763
764
765

/* 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.
*/
766
767
768
static MPIR_T_pvar_timer_t *list_complete_timer;  /* outer */
static unsigned long long *list_complete_counter;
static MPIR_T_pvar_timer_t *list_block_timer;     /* Inner; while waiting */
769

770
771
772
/*
 * These routines provide a default implementation of the MPI RMA operations
 * in terms of the low-level, two-sided channel operations.  A channel
773
774
 * may override these functions, on a per-window basis, by overriding
 * the MPID functions in the RMAFns section of MPID_Win object.
775
776
 */

777
778
#define SYNC_POST_TAG 100

Xin Zhao's avatar
Xin Zhao committed
779
780
781
782
783
784
static int send_lock_msg(int dest, int lock_type, MPID_Win *win_ptr);
static int send_unlock_msg(int dest, MPID_Win *win_ptr);
/* static int send_flush_msg(int dest, MPID_Win *win_ptr); */
static int wait_for_lock_granted(MPID_Win *win_ptr, int target_rank);
static int acquire_local_lock(MPID_Win *win_ptr, int lock_mode);
static int send_rma_msg(MPIDI_RMA_Op_t * rma_op, MPID_Win * win_ptr,
785
                                   MPIDI_CH3_Pkt_flags_t flags,
786
787
788
789
				   MPI_Win source_win_handle, 
				   MPI_Win target_win_handle, 
				   MPIDI_RMA_dtype_info * dtype_info, 
				   void ** dataloop, MPID_Request ** request);
Xin Zhao's avatar
Xin Zhao committed
790
static int recv_rma_msg(MPIDI_RMA_Op_t * rma_op, MPID_Win * win_ptr,
791
                                   MPIDI_CH3_Pkt_flags_t flags,
792
793
794
795
				   MPI_Win source_win_handle, 
				   MPI_Win target_win_handle, 
				   MPIDI_RMA_dtype_info * dtype_info, 
				   void ** dataloop, MPID_Request ** request); 
Xin Zhao's avatar
Xin Zhao committed
796
static int send_contig_acc_msg(MPIDI_RMA_Op_t *, MPID_Win *,
797
                                          MPIDI_CH3_Pkt_flags_t flags,
798
					  MPI_Win, MPI_Win, MPID_Request ** );
Xin Zhao's avatar
Xin Zhao committed
799
static int send_immed_rmw_msg(MPIDI_RMA_Op_t *, MPID_Win *,
800
                                         MPIDI_CH3_Pkt_flags_t flags,
801
                                         MPI_Win, MPI_Win, MPID_Request ** );
Xin Zhao's avatar
Xin Zhao committed
802
static int do_passive_target_rma(MPID_Win *win_ptr, int target_rank,
803
                                            int *wait_for_rma_done_pkt,
James Dinan's avatar
James Dinan committed
804
                                            MPIDI_CH3_Pkt_flags_t sync_flags);
Xin Zhao's avatar
Xin Zhao committed
805
806
static int send_lock_put_or_acc(MPID_Win *, int);
static int send_lock_get(MPID_Win *, int);
807
static inline int poke_progress_engine(void);
808
static inline int rma_list_complete(MPID_Win *win_ptr,
809
                                      MPIDI_RMA_Ops_list_t *ops_list);
810
static inline int rma_list_gc(MPID_Win *win_ptr,
811
812
                                             MPIDI_RMA_Ops_list_t *ops_list,
                                             MPIDI_RMA_Op_t *last_elm, int *nDone);
813

814
815
static int create_datatype(const MPIDI_RMA_dtype_info *dtype_info,
                           const void *dataloop, MPI_Aint dataloop_sz,
816
817
                           const void *o_addr, int o_count,
			   MPI_Datatype o_datatype,
818
                           MPID_Datatype **combined_dtp);
819

820
821
822
/* 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. */
823
#define MPIDI_CH3I_ISSUE_RMA_OP(op_ptr_, win_ptr_, flags_, source_win_handle_, target_win_handle_,err_) \
824
825
826
827
828
829
    do {                                                                                        \
    switch ((op_ptr_)->type)                                                                    \
    {                                                                                           \
        case (MPIDI_RMA_PUT):                                                                   \
        case (MPIDI_RMA_ACCUMULATE):                                                            \
            MPIDI_CH3I_TRACK_RMA_WRITE(op_ptr_, win_ptr_);                                      \
Xin Zhao's avatar
Xin Zhao committed
830
            (err_) = send_rma_msg((op_ptr_), (win_ptr_), (flags_), (source_win_handle_),        \
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
                                                (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;                        \
                                                                                                \
Xin Zhao's avatar
Xin Zhao committed
846
                (err_) = recv_rma_msg((op_ptr_), (win_ptr_), (flags_), (source_win_handle_),    \
847
848
849
850
                                                    (target_win_handle_), &(op_ptr_)->dtype_info,\
                                                    &(op_ptr_)->dataloop, &(op_ptr_)->request); \
            } else {                                                                            \
                MPIDI_CH3I_TRACK_RMA_WRITE(op_ptr_, win_ptr_);                                  \
Xin Zhao's avatar
Xin Zhao committed
851
                (err_) = send_rma_msg((op_ptr_), (win_ptr_), (flags_), (source_win_handle_),    \
852
853
854
855
856
857
858
                                                    (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_);                                      \
Xin Zhao's avatar
Xin Zhao committed
859
            (err_) = send_contig_acc_msg((op_ptr_), (win_ptr_), (flags_),                       \
860
861
862
863
864
                                                       (source_win_handle_), (target_win_handle_),\
                                                       &(op_ptr_)->request );                   \
            if (err_) { MPIU_ERR_POP(err_); }                                                   \
            break;                                                                              \
        case (MPIDI_RMA_GET):                                                                   \
Xin Zhao's avatar
Xin Zhao committed
865
            (err_) = recv_rma_msg((op_ptr_), (win_ptr_), (flags_),                              \
866
867
868
869
870
871
872
873
                                                (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_);                                      \
Xin Zhao's avatar
Xin Zhao committed
874
            (err_) = send_immed_rmw_msg((op_ptr_), (win_ptr_), (flags_),                        \
875
876
877
878
879
880
881
882
883
884
885
                                                      (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)


886
887
888
889
890
891
892
#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;
893
    int comm_size;
894
    int *rma_target_proc, *nops_to_proc, i, total_op_count, *curr_ops_cnt;
895
    MPIDI_RMA_Op_t *curr_ptr;
896
    MPIDI_RMA_Ops_list_t *ops_list;
897
898
899
    MPID_Comm *comm_ptr;
    MPI_Win source_win_handle, target_win_handle;
    MPID_Progress_state progress_state;
900
    int errflag = FALSE;
901
    MPIU_CHKLMEM_DECL(3);
902
903
904
905
    MPIDI_STATE_DECL(MPID_STATE_MPIDI_WIN_FENCE);

    MPIDI_RMA_FUNC_ENTER(MPID_STATE_MPIDI_WIN_FENCE);

James Dinan's avatar
James Dinan committed
906
907
908
909
    MPIU_ERR_CHKANDJUMP(win_ptr->epoch_state != MPIDI_EPOCH_NONE &&
                        win_ptr->epoch_state != MPIDI_EPOCH_FENCE,
                        mpi_errno, MPI_ERR_RMA_SYNC, "**rmasync");

910
911
912
913
914
915
916
917
918
    /* 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)
    {
919
	MPIR_T_PVAR_TIMER_START(RMA, rma_winfence_clearlock);
920
921
922
923
924
925
	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-- */
926
	    if (mpi_errno != MPI_SUCCESS) {
927
		MPID_Progress_end(&progress_state);
928
		MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER,"**winnoprogress");
929
930
	    }
	    /* --END ERROR HANDLING-- */
931
	    MPIR_T_PVAR_COUNTER_INC(RMA, rma_winfence_clearlock_aux, 1);
932
933
	}
	MPID_Progress_end(&progress_state);
934
	MPIR_T_PVAR_TIMER_END(RMA, rma_winfence_clearlock);
935
    }
936
937
938

    /* Note that the NOPRECEDE and NOSUCCEED must be specified by all processes
       in the window's group if any specify it */
939
940
    if (assert & MPI_MODE_NOPRECEDE)
    {
941
942
943
944
        /* 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");

945
	win_ptr->fence_issued = (assert & MPI_MODE_NOSUCCEED) ? 0 : 1;
946
947
948
	goto fn_exit;
    }
    
949
    if (win_ptr->fence_issued == 0)
950
    {
951
	/* win_ptr->fence_issued == 0 means either this is the very first
952
953
	   call to fence or the preceding fence had the
	   MPI_MODE_NOSUCCEED assert. 
954
955
956
957

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

958
	if (!(assert & MPI_MODE_NOSUCCEED)) win_ptr->fence_issued = 1;
959
960
961
    }
    else
    {
962
963
	int nRequest = 0;
	int nRequestNew = 0;
964
965

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

970
	MPIR_T_PVAR_TIMER_START(RMA, rma_winfence_rs);
971
	/* This is the second or later fence. Do all the preceding RMA ops. */
972
	comm_ptr = win_ptr->comm_ptr;
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
	/* 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;

989
990
991
        /* Note, active target uses the following ops list, and passive
           target uses win_ptr->targets[..] */
        ops_list = &win_ptr->at_rma_ops_list;
992

993
994
995
	/* set rma_target_proc[i] to 1 if rank i is a target of RMA
	   ops from this process */
	total_op_count = 0;
996
        curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
997
998
	while (curr_ptr != NULL)
	{
Xin Zhao's avatar
Xin Zhao committed
999
1000
1001
	    total_op_count++;
	    rma_target_proc[curr_ptr->target_rank] = 1;
	    nops_to_proc[curr_ptr->target_rank]++;
1002
1003
1004
1005
1006
1007
	    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;
1008
1009
	/* do a reduce_scatter_block (with MPI_SUM) on rma_target_proc. 
	   As a result,
1010
1011
1012
1013
1014
1015
	   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;
            
1016
	mpi_errno = MPIR_Reduce_scatter_block_impl(MPI_IN_PLACE, rma_target_proc, 1,
1017
                                                   MPI_INT, MPI_SUM, comm_ptr, &errflag);
1018
	MPIR_T_PVAR_TIMER_END(RMA, rma_winfence_rs);
1019
1020
	/* result is stored in rma_target_proc[0] */
	if (mpi_errno) { MPIU_ERR_POP(mpi_errno); }
1021
        MPIU_ERR_CHKANDJUMP(errflag, mpi_errno, MPI_ERR_OTHER, "**coll_fail");
1022
1023
1024
1025
1026
1027

	/* 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];  
1028

1029
1030
1031
1032
1033
1034
1035
1036
    MPIR_T_PVAR_TIMER_START(RMA, rma_winfence_issue);
    MPIR_T_PVAR_COUNTER_INC(RMA, rma_winfence_issue_aux, total_op_count);
    MPIR_T_PVAR_ULONG2_HIGHWATERMARK_UPDATE(RMA, rma_winfence_issue_aux, total_op_count);

    MPIR_T_PVAR_ULONG2_HIGHWATERMARK_UPDATE(RMA, rma_winfence_reqs, MPIR_T_PVAR_LEVEL_GET(rma_winfence_reqs));
    MPIR_T_PVAR_LEVEL_SET(RMA, rma_winfence_reqs, 0); /* reset the level */
    MPIR_T_PVAR_COUNTER_INC(RMA, rma_winfence_reqs, 1);

1037
	i = 0;
1038
        curr_ptr = MPIDI_CH3I_RMA_Ops_head(ops_list);
1039
1040
	while (curr_ptr != NULL)
	{
1041
1042
            MPIDI_CH3_Pkt_flags_t flags = MPIDI_CH3_PKT_FLAG_NONE;

1043
	    /* The completion counter at the target is decremented only on 
1044
	       the last RMA operation. */
1045
	    if (curr_ops_cnt[curr_ptr->target_rank] ==
1046
1047
1048
1049
1050
                nops_to_proc[curr_ptr->target_rank] - 1) {
                flags = MPIDI_CH3_PKT_FLAG_RMA_AT_COMPLETE;
            }

            source_win_handle = win_ptr->handle;
1051
	    target_win_handle = win_ptr->all_win_handles[curr_ptr->target_rank];
1052

1053
#define MPIDI_CH3I_TRACK_RMA_WRITE(op_ptr_, win_ptr_) /* Not used by active mode */
1054
            MPIDI_CH3I_ISSUE_RMA_OP(curr_ptr, win_ptr, flags,
1055
                                    source_win_handle, target_win_handle, mpi_errno);
1056
#undef MPIDI_CH3I_TRACK_RMA_WRITE
1057

1058
1059
	    i++;
	    curr_ops_cnt[curr_ptr->target_rank]++;
1060
1061
	    /* If the request is null, we can remove it immediately */
	    if (!curr_ptr->request) {
1062
                MPIDI_CH3I_RMA_Ops_free_and_next(ops_list, &curr_ptr);
1063
1064
	    }
	    else  {
1065
		nRequest++;
1066
		MPIR_T_PVAR_LEVEL_INC(RMA, rma_winfence_reqs, 1);
1067
		curr_ptr    = curr_ptr->next;
1068
1069
1070
1071
1072
		/* 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. */
1073
1074
		if (nRequest > MPIR_CVAR_CH3_RMA_NREQUEST_THRESHOLD &&
		    nRequest - nRequestNew > MPIR_CVAR_CH3_RMA_NREQUEST_NEW_THRESHOLD) {
1075
		    int nDone = 0;
1076
1077
                    mpi_errno = poke_progress_engine();
                    if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
1078
1079
1080
            MPIR_T_PVAR_STMT(RMA, list_complete_timer=MPIR_T_PVAR_TIMER_ADDR(rma_winfence_complete));
            MPIR_T_PVAR_STMT(RMA, list_complete_counter=MPIR_T_PVAR_COUNTER_ADDR(rma_winfence_complete_aux));

1081
1082
                    mpi_errno = rma_list_gc(win_ptr, ops_list, curr_ptr, &nDone);
                    if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
1083
1084
1085
1086
		    /* if (nDone > 0) printf( "nDone = %d\n", nDone ); */
		    nRequest -= nDone;
		    nRequestNew = nRequest;
		}
1087
	    }
1088
	}
1089
	MPIR_T_PVAR_TIMER_END(RMA, rma_winfence_issue);
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101

	/* 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.
	*/
1102
1103
	if (total_op_count)
	{ 
1104
1105
1106
        MPIR_T_PVAR_STMT(RMA, list_complete_timer=MPIR_T_PVAR_TIMER_ADDR(rma_winfence_complete));
        MPIR_T_PVAR_STMT(RMA, list_complete_counter=MPIR_T_PVAR_COUNTER_ADDR(rma_winfence_complete_aux));
        MPIR_T_PVAR_STMT(RMA, list_block_timer=MPIR_T_PVAR_TIMER_ADDR(rma_winfence_block));
1107
1108
            mpi_errno = rma_list_complete(win_ptr, ops_list);
            if (mpi_errno != MPI_SUCCESS) MPIU_ERR_POP(mpi_errno);
1109
	}
1110

1111
        MPIU_Assert(MPIDI_CH3I_RMA_Ops_isempty(ops_list));
1112
1113
1114
1115
	
	/* wait for all operations from other processes to finish */
	if (win_ptr->my_counter)
	{
1116
	    MPIR_T_PVAR_TIMER_START(RMA, rma_winfence_wait);
1117
1118
1119
1120
1121
	    MPID_Progress_start(&progress_state);
	    while (win_ptr->my_counter)
	    {
		mpi_errno = MPID_Progress_wait(&progress_state);
		/* --BEGIN ERROR HANDLING-- */
1122
		if (mpi_errno != MPI_SUCCESS) {
1123
		    MPID_Progress_end(&progress_state);
1124
		    MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER,"**winnoprogress");
1125
1126
		}
		/* --END ERROR HANDLING-- */
1127
		MPIR_T_PVAR_COUNTER_INC(RMA, rma_winfence_wait_aux, 1);
1128
1129
	    }
	    MPID_Progress_end(&progress_state);
1130
	    MPIR_T_PVAR_TIMER_END(RMA, rma_winfence_wait);
1131
1132
1133
1134
	} 
	
	if (assert & MPI_MODE_NOSUCCEED)
	{
1135
	    win_ptr->fence_issued = 0;
1136
1137
	}

James Dinan's avatar
James Dinan committed
1138
1139
1140
        win_ptr->epoch_state = MPIDI_EPOCH_NONE;
    }

1141
 fn_exit:
1142
1143
1144
1145
1146
1147
1148
1149
1150
    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-- */
}

1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
/* 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);
1183
    MPIU_Assign_trunc(blocklens[1], dataloop_sz, int);
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
    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);
1195
1196
    if (mpi_errno) MPIU_ERR_POP(mpi_errno);
   
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
    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;
}

1232
1233

#undef FUNCNAME
Xin Zhao's avatar
Xin Zhao committed
1234
#define FUNCNAME send_rma_msg
1235
1236
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
Xin Zhao's avatar
Xin Zhao committed
1237
static int send_rma_msg(MPIDI_RMA_Op_t *rma_op, MPID_Win *win_ptr,
1238
                                   MPIDI_CH3_Pkt_flags_t flags,
1239
1240
1241
1242
1243
1244
1245
1246
1247
				   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];
1248
    int mpi_errno=MPI_SUCCESS;
1249
1250
    int origin_dt_derived, target_dt_derived, iovcnt;
    MPI_Aint origin_type_size;
1251
1252
1253
    MPIDI_VC_t * vc;
    MPID_Comm *comm_ptr;
    MPID_Datatype *target_dtp=NULL, *origin_dtp=NULL;
1254
    MPID_Request *resp_req=NULL;
1255
    MPIU_CHKPMEM_DECL(1);
Xin Zhao's avatar
Xin Zhao committed
1256
    MPIDI_STATE_DECL(MPID_STATE_SEND_RMA_MSG);
1257
1258
    MPIDI_STATE_DECL(MPID_STATE_MEMCPY);

Xin Zhao's avatar
Xin Zhao committed
1259
    MPIDI_RMA_FUNC_ENTER(MPID_STATE_SEND_RMA_MSG);
1260

1261
1262
    *request = NULL;

1263
1264
1265
1266
1267
    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;
1268
        put_pkt->flags = flags;
1269
1270
1271
1272
1273
        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;
1274
        
1275
1276
1277
        iov[0].MPID_IOV_BUF = (MPID_IOV_BUF_CAST) put_pkt;
        iov[0].MPID_IOV_LEN = sizeof(*put_pkt);
    }
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
    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;

1294
        if (!MPIR_DATATYPE_IS_PREDEFINED(resp_req->dev.datatype)) {
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
            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;
1306
        accum_pkt->flags = flags;
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
        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);
    }
1318
1319
1320
1321
1322
    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;
1323
        accum_pkt->flags = flags;
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
        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);
    }

1335
1336
1337
1338
    /*    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);
    */
1339

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

1343
    if (!MPIR_DATATYPE_IS_PREDEFINED(rma_op->origin_datatype))
1344
1345
1346
1347
1348
1349
1350
1351
1352
    {
        origin_dt_derived = 1;
        MPID_Datatype_get_ptr(rma_op->origin_datatype, origin_dtp);
    }
    else
    {
        origin_dt_derived = 0;
    }

1353
    if (!MPIR_DATATYPE_IS_PREDEFINED(rma_op->target_datatype))
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
    {
        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;
1367
        dtype_info->max_contig_blocks = target_dtp->max_contig_blocks;
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
        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);
1385
        MPIU_Memcpy(*dataloop, target_dtp->dataloop, target_dtp->dataloop_size);
1386
	MPIDI_FUNC_EXIT(MPID_STATE_MEMCPY);
1387
1388
        /* 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 */
1389
        MPL_VG_MAKE_MEM_DEFINED(*dataloop, target_dtp->dataloop_size);
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402

        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);

1403
    if (!target_dt_derived)
1404
    {
1405
1406
1407
1408
        /* basic datatype on target */
        if (!origin_dt_derived)
        {
            /* basic datatype on origin */
1409
1410
1411
            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;
1412
	    MPIU_THREAD_CS_ENTER(CH3COMM,vc);
1413
            mpi_errno = MPIDI_CH3_iStartMsgv(vc, iov, iovcnt, request);
1414
	    MPIU_THREAD_CS_EXIT(CH3COMM,vc);
1415
            MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
1416
1417
        }
        else
1418
1419
1420
        {
            /* derived datatype on origin */
            *request = MPID_Request_create();
1421
            MPIU_ERR_CHKANDJUMP(*request == NULL,mpi_errno,MPI_ERR_OTHER,"**nomemreq");
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
            
            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;

1441
	    MPIU_THREAD_CS_ENTER(CH3COMM,vc);
1442
            mpi_errno = vc->sendNoncontig_fn(vc, *request, iov[0].MPID_IOV_BUF, iov[0].MPID_IOV_LEN);
1443
	    MPIU_THREAD_CS_EXIT(CH3COMM,vc);
1444
            MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
1445
1446
1447
1448
        }
    }
    else
    {
1449
        /* derived datatype on target */
1450
        MPID_Datatype *combined_dtp = NULL;
1451
1452
1453

        *request = MPID_Request_create();
        if (*request == NULL) {
1454
	    MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER,"**nomemreq");
1455
1456
1457
1458
1459
1460
        }

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

	(*request)->dev.segment_ptr = MPID_Segment_alloc( );
1461
1462
1463
1464
        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 */

1465
1466
        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);
1467
        if (mpi_errno) MPIU_ERR_POP(mpi_errno);
1468
1469
1470
1471
1472

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

        MPID_Segment_init(MPI_BOTTOM, 1, combined_dtp->handle,
1473
1474
                          (*request)->dev.segment_ptr, 0);
        (*request)->dev.segment_first = 0;
1475
1476
1477
1478
1479
        (*request)->dev.segment_size = combined_dtp->size;

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

1480
	MPIU_THREAD_CS_ENTER(CH3COMM,vc);
1481
        mpi_errno = vc->sendNoncontig_fn(vc, *request, iov[0].MPID_IOV_BUF, iov[0].MPID_IOV_LEN);
1482
	MPIU_THREAD_CS_EXIT(CH3COMM,vc);
1483
        MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");
1484

1485
1486
1487
        /* we're done with the datatypes */
        if (origin_dt_derived)
            MPID_Datatype_release(origin_dtp);
1488
        MPID_Datatype_release(target_dtp);
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
    }

    /* 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.
             */

1510
            MPID_Request_release(*request);
1511
1512
1513
1514
1515
1516
1517
1518
1519
            *request = resp_req;

        } else {
            *request = resp_req;
        }

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

 fn_exit:
1522
    MPIU_CHKPMEM_COMMIT();
Xin Zhao's avatar
Xin Zhao committed
1523
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_SEND_RMA_MSG);
1524
1525
1526
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
 fn_fail:
1527
    if (resp_req) {
1528
        MPID_Request_release(resp_req);
1529
    }
1530
1531
1532
    if (*request)
    {
        MPIU_CHKPMEM_REAP();
1533
1534
        if ((*request)->dev.datatype_ptr)
            MPID_Datatype_release((*request)->dev.datatype_ptr);
1535
        MPID_Request_release(*request);
1536
1537
    }
    *request = NULL;
1538
1539
1540
1541
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}

1542
1543
1544
1545
/*
 * Use this for contiguous accumulate operations
 */
#undef FUNCNAME
Xin Zhao's avatar
Xin Zhao committed
1546
#define FUNCNAME send_contig_acc_msg
1547
1548
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
Xin Zhao's avatar
Xin Zhao committed
1549
static int send_contig_acc_msg(MPIDI_RMA_Op_t *rma_op,
1550
					  MPID_Win *win_ptr,
1551
                                          MPIDI_CH3_Pkt_flags_t flags,
1552
1553
1554
1555
1556
1557
1558
1559
					  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;
1560
1561
    int iovcnt;
    MPI_Aint origin_type_size;
1562
1563
    MPIDI_VC_t * vc;
    MPID_Comm *comm_ptr;
1564
    size_t len;
Xin Zhao's avatar
Xin Zhao committed
1565
    MPIDI_STATE_DECL(MPID_STATE_SEND_CONTIG_ACC_MSG);
1566

Xin Zhao's avatar
Xin Zhao committed
1567
    MPIDI_RMA_FUNC_ENTER(MPID_STATE_SEND_CONTIG_ACC_MSG);
1568
1569
1570
1571
1572

    *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 */
1573
    MPIU_Assign_trunc(len, rma_op->origin_count * origin_type_size, size_t);
1574
    if (MPIR_CVAR_CH3_RMA_ACC_IMMED && len <= MPIDI_RMA_IMMED_INTS*sizeof(int)) {
1575
1576
1577
1578
1579
1580
	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;
1581
        accumi_pkt->flags = flags;
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
	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);
1599
	mpi_errno = MPIDI_CH3_iStartMsg(vc, accumi_pkt, sizeof(*accumi_pkt), request);
1600
1601
1602
1603
1604
1605
1606
1607
	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;
1608
    accum_pkt->flags = flags;
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
    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);
1638
    mpi_errno = MPIDI_CH3_iStartMsgv(vc, iov, iovcnt, request);
1639
1640
1641
1642
    MPIU_THREAD_CS_EXIT(CH3COMM,vc);
    MPIU_ERR_CHKANDJUMP(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**ch3|rmamsg");

 fn_exit:
Xin Zhao's avatar
Xin Zhao committed
1643
    MPIDI_RMA_FUNC_EXIT(MPID_STATE_SEND_CONTIG_ACC_MSG);
1644
1645
1646
1647
1648
    return mpi_errno;
    /* --BEGIN ERROR HANDLING-- */
 fn_fail:
    if (*request)
    {
1649
        MPID_Request_release(*request);
1650
1651
1652
1653
1654
1655
    }
    *request = NULL;
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}

1656

1657
1658
1659
1660
/*
 * Initiate an immediate RMW accumulate operation
 */
#undef FUNCNAME
Xin Zhao's avatar
Xin Zhao committed
1661
#define FUNCNAME send_immed_rmw_msg
1662
1663
#undef FCNAME
#define FCNAME MPIDI_QUOTE(FUNCNAME)
Xin Zhao's avatar
Xin Zhao committed
1664
static int send_immed_rmw_msg(MPIDI_RMA_Op_t *rma_op,
1665
                                         MPID_Win *win_ptr,
1666
                                         MPIDI_CH3_Pkt_flags_t flags,
1667
1668
1669
1670
1671
                                         MPI_Win source_win_handle, 
                                         MPI_Win target_win_handle, 
                                         MPID_Request **request) 
{
    int mpi_errno = MPI_SUCCESS;
1672
    MPID_Request *rmw_req = NULL, *resp_req = NULL;