-
Notifications
You must be signed in to change notification settings - Fork 426
/
Copy pathDefaultRectangular.chpl
2295 lines (1969 loc) · 77.9 KB
/
DefaultRectangular.chpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2004-2019 Cray Inc.
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// DefaultRectangular.chpl
//
module DefaultRectangular {
config const dataParTasksPerLocale = 0;
config const dataParIgnoreRunningTasks = false;
config const dataParMinGranularity: int = 1;
if dataParTasksPerLocale<0 then halt("dataParTasksPerLocale must be >= 0");
if dataParMinGranularity<=0 then halt("dataParMinGranularity must be > 0");
use DSIUtil, ChapelArray;
private use ChapelDistribution, ChapelRange, SysBasic, SysError;
private use ChapelDebugPrint, ChapelLocks, OwnedObject, IO;
private use DefaultSparse, DefaultAssociative, DefaultOpaque;
use ExternalArray;
config param debugDefaultDist = false;
config param debugDefaultDistBulkTransfer = false;
config param debugDataPar = false;
config param debugDataParNuma = false;
config param defaultDoRADOpt = true;
config param defaultDisableLazyRADOpt = false;
config param earlyShiftData = true;
config param usePollyArrayIndex = false;
enum ArrayStorageOrder { RMO, CMO }
config param defaultStorageOrder = ArrayStorageOrder.RMO;
// would like to move this into DefaultRectangularArr and
// DefaultRectangularDom so each instance can choose individually
param storageOrder = defaultStorageOrder;
// A function which help to compute the final index
// to be used for DefaultRectangularArr access. This function
// helps Polly to effectively communicate the array dimension
// sizes and the index subscripts to Polly.
pragma "lineno ok"
pragma "llvm readnone"
proc polly_array_index(arguments:int ...):int {
param rank = (arguments.size - 1) / 2;
param blkStart = 2;
param blkEnd = 2 + rank - 1;
param indStart = blkEnd + 1;
param indEnd = indStart + rank - 1;
var offset = arguments(1);
var blk:rank*int;
var ind:rank*int;
blk(rank) = 1;
for param i in 1..(rank-1) by -1 do
blk(i) = blk(i+1) * arguments(blkStart+i);
for param j in 1..rank {
ind(j) = arguments(indStart+j-1);
}
var ret:int = offset;
for param i in 1..rank {
ret += ind(i) * blk(i);
}
return ret;
}
class DefaultDist: BaseDist {
override proc dsiNewRectangularDom(param rank: int, type idxType,
param stridable: bool, inds) {
const dom = new unmanaged DefaultRectangularDom(rank, idxType, stridable,
_to_unmanaged(this));
dom.dsiSetIndices(inds);
return dom;
}
override proc dsiNewAssociativeDom(type idxType, param parSafe: bool)
return new unmanaged DefaultAssociativeDom(idxType, parSafe, _to_unmanaged(this));
override proc dsiNewOpaqueDom(type idxType, param parSafe: bool)
return new unmanaged DefaultOpaqueDom(_to_unmanaged(this), parSafe);
override proc dsiNewSparseDom(param rank: int, type idxType, dom: domain)
return new unmanaged DefaultSparseDom(rank, idxType, _to_unmanaged(this), dom);
proc dsiIndexToLocale(ind) return this.locale;
// Right now, the default distribution acts like a singleton.
// So we don't have to copy it when a clone is requested.
proc dsiClone() return _to_unmanaged(this);
proc dsiAssign(other: unmanaged this.type) { }
proc dsiEqualDMaps(d:unmanaged DefaultDist) param return true;
proc dsiEqualDMaps(d) param return false;
proc trackDomains() param return false;
override proc dsiTrackDomains() return false;
proc singleton() param return true;
proc dsiIsLayout() param return true;
}
//
// Replicated copies are set up in chpl_initOnLocales() during locale
// model initialization
//
pragma "locale private"
var defaultDist = new dmap(new unmanaged DefaultDist());
proc chpl_defaultDistInitPrivate() {
if defaultDist._value==nil {
// FIXME benharsh: Here's what we want to do:
// defaultDist = new dmap(new DefaultDist());
// The problem is that the LHS of the "proc =" for _distributions
// loses its ref intent in the removeWrapRecords pass.
//
// The code below is copied from the contents of the "proc =".
const nd = new dmap(new unmanaged DefaultDist());
__primitive("move", defaultDist, chpl__autoCopy(nd.clone()));
}
}
class DefaultRectangularDom: BaseRectangularDom {
var dist: unmanaged DefaultDist;
var ranges : rank*range(idxType,BoundedRangeType.bounded,stridable);
proc linksDistribution() param return false;
override proc dsiLinksDistribution() return false;
proc type isDefaultRectangular() param return true;
proc isDefaultRectangular() param return true;
proc init(param rank, type idxType, param stridable, dist) {
super.init(rank, idxType, stridable);
this.dist = dist;
}
proc intIdxType type {
return chpl__idxTypeToIntIdxType(idxType);
}
override proc dsiMyDist() {
return dist;
}
pragma "no doc"
record _serialized_domain {
param rank;
type idxType;
param stridable;
var dims;
param isDefaultRectangular;
}
proc chpl__serialize() {
return new _serialized_domain(rank, idxType, stridable, dsiDims(), true);
}
proc type chpl__deserialize(data) {
return defaultDist.newRectangularDom(data.rank,
data.idxType,
data.stridable,
data.dims);
}
override proc dsiDisplayRepresentation() {
writeln("ranges = ", ranges);
}
// function and iterator versions, also for setIndices
proc dsiGetIndices() return ranges;
proc dsiSetIndices(x) {
ranges = x;
}
proc dsiAssignDomain(rhs: domain, lhsPrivate:bool) {
chpl_assignDomainWithGetSetIndices(this, rhs);
}
iter these_help(param d: int) /*where storageOrder == ArrayStorageOrder.RMO*/ {
if d == rank {
for i in ranges(d) do
yield i;
} else if d == rank - 1 {
for i in ranges(d) do
for j in these_help(rank) do
yield (i, j);
} else {
for i in ranges(d) do
for j in these_help(d+1) do
yield (i, (...j));
}
}
/*
iter these_help(param d: int) where storageOrder == ArrayStorageOrder.CMO {
param rd = rank - d + 1;
if rd == 1 {
for i in ranges(rd) do
yield i;
} else if rd == 2 {
for i in ranges(rd) do
for j in these_help(rank) do
yield (j, i);
} else {
for i in ranges(rd) do
for j in these_help(d+1) do
yield ((...j), i);
}
}
*/
iter these_help(param d: int, block) /*where storageOrder == ArrayStorageOrder.RMO*/ {
if d == block.size {
for i in block(d) do
yield i;
} else if d == block.size - 1 {
for i in block(d) do
for j in these_help(block.size, block) do
yield (i, j);
} else {
for i in block(d) do
for j in these_help(d+1, block) do
yield (i, (...j));
}
}
/*
iter these_help(param d: int, block) where storageOrder == ArrayStorageOrder.CMO {
param rd = rank - d + 1;
if rd == 1 {
for i in block(rd) do
yield i;
} else if rd == 2 {
for i in block(rd) do
for j in these_help(block.size, block) do
yield (j, i);
} else {
for i in block(rd) do
for j in these_help(d+1, block) do
yield ((...j), i);
}
}
*/
iter these(tasksPerLocale = dataParTasksPerLocale,
ignoreRunning = dataParIgnoreRunningTasks,
minIndicesPerTask = dataParMinGranularity,
offset=createTuple(rank, intIdxType, 0:intIdxType)) {
if rank == 1 {
for i in ranges(1) do
yield i;
} else {
for i in these_help(1) do
yield i;
}
}
iter these(param tag: iterKind,
tasksPerLocale = dataParTasksPerLocale,
ignoreRunning = dataParIgnoreRunningTasks,
minIndicesPerTask = dataParMinGranularity,
offset=createTuple(rank, intIdxType, 0:intIdxType))
where tag == iterKind.standalone {
if chpl__testParFlag then
chpl__testPar("default rectangular domain standalone invoked on ", ranges);
if debugDefaultDist then
chpl_debug_writeln("*** In domain standalone code:");
const numTasks = if tasksPerLocale == 0 then here.maxTaskPar
else tasksPerLocale;
if debugDefaultDist {
chpl_debug_writeln(" numTasks=", numTasks, " (", ignoreRunning,
"), minIndicesPerTask=", minIndicesPerTask);
}
const (numChunks, parDim) = if __primitive("task_get_serial") then
(1, -1) else
_computeChunkStuff(numTasks,
ignoreRunning,
minIndicesPerTask,
ranges);
if debugDefaultDist {
chpl_debug_writeln(" numChunks=", numChunks, " parDim=", parDim,
" ranges(", parDim, ").length=", ranges(parDim).length);
}
if debugDataPar {
chpl_debug_writeln("### numTasksPerLoc = ", numTasks, "\n" +
"### ignoreRunning = ", ignoreRunning, "\n" +
"### minIndicesPerTask = ", minIndicesPerTask, "\n" +
"### numChunks = ", numChunks, " (parDim = ", parDim, ")\n" +
"### nranges = ", ranges);
}
if numChunks <= 1 {
for i in these_help(1) {
yield i;
}
} else {
if debugDefaultDist {
chpl_debug_writeln("*** DI: ranges = ", ranges);
}
// TODO: The following is somewhat of an abuse of what
// _computeBlock() was designed for (dense ranges only; I
// multiplied by the stride as a white lie to make it work
// reasonably. We should switch to using the RangeChunk
// library...
coforall chunk in 0..#numChunks {
var block = ranges;
const len = if (!ranges(parDim).stridable) then ranges(parDim).length
else ranges(parDim).length:uint * abs(ranges(parDim).stride):uint;
const (lo,hi) = _computeBlock(len,
numChunks, chunk,
ranges(parDim)._high,
ranges(parDim)._low,
ranges(parDim)._low);
if block(parDim).stridable then
block(parDim) = lo..hi by block(parDim).stride align chpl__idxToInt(block(parDim).alignment);
else
block(parDim) = lo..hi;
if debugDefaultDist {
chpl_debug_writeln("*** DI[", chunk, "]: block = ", block);
}
for i in these_help(1, block) {
yield i;
}
}
}
}
iter these(param tag: iterKind,
tasksPerLocale = dataParTasksPerLocale,
ignoreRunning = dataParIgnoreRunningTasks,
minIndicesPerTask = dataParMinGranularity,
offset=createTuple(rank, intIdxType, 0:intIdxType))
where tag == iterKind.leader {
const numSublocs = here.getChildCount();
if localeModelHasSublocales && numSublocs != 0 {
var dptpl = if tasksPerLocale==0 then here.maxTaskPar
else tasksPerLocale;
if !ignoreRunning {
const otherTasks = here.runningTasks() - 1; // don't include self
dptpl = if otherTasks < dptpl then (dptpl-otherTasks):int else 1;
}
// Make sure we don't use more sublocales than the numbers of
// tasksPerLocale requested
const numSublocTasks = min(numSublocs, dptpl);
// For serial tasks, we will only have a single chunk
const (numChunks, parDim) = if __primitive("task_get_serial") then
(1, -1) else
_computeChunkStuff(numSublocTasks,
ignoreRunning=true,
minIndicesPerTask,
ranges);
if debugDataParNuma {
chpl_debug_writeln("### numSublocs = ", numSublocs, "\n" +
"### numTasksPerSubloc = ", numSublocTasks, "\n" +
"### ignoreRunning = ", ignoreRunning, "\n" +
"### minIndicesPerTask = ", minIndicesPerTask, "\n" +
"### numChunks = ", numChunks, " (parDim = ", parDim, ")\n" +
"### nranges = ", ranges);
}
if numChunks == 1 {
if rank == 1 {
yield (offset(1)..#ranges(1).length,);
} else {
var block: rank*range(intIdxType);
for param i in 1..rank do
block(i) = offset(i)..#ranges(i).length;
yield block;
}
} else {
coforall chunk in 0..#numChunks { // make sure coforall on can trigger
local do on here.getChild(chunk) {
if debugDataParNuma {
if chunk!=chpl_getSubloc() then
chpl_debug_writeln("*** ERROR: ON WRONG SUBLOC (should be "+chunk+
", on "+chpl_getSubloc()+") ***");
}
// Divide the locale's tasks approximately evenly
// among the sublocales
const numSublocTasks = (if chunk < dptpl % numChunks
then dptpl / numChunks + 1
else dptpl / numChunks);
var locBlock: rank*range(intIdxType);
for param i in 1..rank do
locBlock(i) = offset(i)..#(ranges(i).length);
var followMe: rank*range(intIdxType) = locBlock;
const (lo,hi) = _computeBlock(locBlock(parDim).length,
numChunks, chunk,
locBlock(parDim)._high,
locBlock(parDim)._low,
locBlock(parDim)._low);
followMe(parDim) = lo..hi;
const (numChunks2, parDim2) = _computeChunkStuff(numSublocTasks,
ignoreRunning=true,
minIndicesPerTask,
followMe);
coforall chunk2 in 0..#numChunks2 {
var locBlock2: rank*range(intIdxType);
for param i in 1..rank do
locBlock2(i) = followMe(i).low..followMe(i).high;
var followMe2: rank*range(intIdxType) = locBlock2;
const low = locBlock2(parDim2)._low,
high = locBlock2(parDim2)._high;
const (lo,hi) = _computeBlock(locBlock2(parDim2).length,
numChunks2, chunk2,
high, low, low);
followMe2(parDim2) = lo..hi;
if debugDataParNuma {
chpl_debug_writeln("### chunk = ", chunk, " chunk2 = ", chunk2, " " +
"followMe = ", followMe, " followMe2 = ", followMe2);
}
yield followMe2;
}
}
}
}
} else {
if debugDefaultDist then
chpl_debug_writeln("*** In domain/array leader code:"); // this = ", this);
const numTasks = if tasksPerLocale==0 then here.maxTaskPar
else tasksPerLocale;
if debugDefaultDist then
chpl_debug_writeln(" numTasks=", numTasks, " (", ignoreRunning,
"), minIndicesPerTask=", minIndicesPerTask);
const (numChunks, parDim) = if __primitive("task_get_serial") then
(1, -1) else
_computeChunkStuff(numTasks,
ignoreRunning,
minIndicesPerTask,
ranges);
if debugDefaultDist then
chpl_debug_writeln(" numChunks=", numChunks, " parDim=", parDim,
" ranges(", parDim, ").length=", ranges(parDim).length);
if debugDataPar {
chpl_debug_writeln("### numTasksPerLoc = ", numTasks, "\n" +
"### ignoreRunning = ", ignoreRunning, "\n" +
"### minIndicesPerTask = ", minIndicesPerTask, "\n" +
"### numChunks = ", numChunks, " (parDim = ", parDim, ")\n" +
"### nranges = ", ranges);
}
if numChunks == 1 {
if rank == 1 {
yield (offset(1)..#ranges(1).length,);
} else {
var block: rank*range(intIdxType);
for param i in 1..rank do
block(i) = offset(i)..#ranges(i).length;
yield block;
}
} else {
var locBlock: rank*range(intIdxType);
for param i in 1..rank do
locBlock(i) = offset(i)..#(ranges(i).length);
if debugDefaultDist then
chpl_debug_writeln("*** DI: locBlock = ", locBlock);
coforall chunk in 0..#numChunks {
var followMe: rank*range(intIdxType) = locBlock;
const (lo,hi) = _computeBlock(locBlock(parDim).length,
numChunks, chunk,
locBlock(parDim)._high,
locBlock(parDim)._low,
locBlock(parDim)._low);
followMe(parDim) = lo..hi;
if debugDefaultDist then
chpl_debug_writeln("*** DI[", chunk, "]: followMe = ", followMe);
yield followMe;
}
}
}
}
iter these(param tag: iterKind, followThis,
tasksPerLocale = dataParTasksPerLocale,
ignoreRunning = dataParIgnoreRunningTasks,
minIndicesPerTask = dataParMinGranularity,
offset=createTuple(rank, intIdxType, 0:intIdxType))
where tag == iterKind.follower {
proc anyStridable(rangeTuple, param i: int = 1) param
return if i == rangeTuple.size then rangeTuple(i).stridable
else rangeTuple(i).stridable || anyStridable(rangeTuple, i+1);
if chpl__testParFlag then
chpl__testPar("default rectangular domain follower invoked on ", followThis);
if debugDefaultDist then
chpl_debug_writeln("In domain follower code: Following ", followThis);
param stridable = this.stridable || anyStridable(followThis);
var block: rank*range(idxType=intIdxType, stridable=stridable);
if stridable {
type strType = chpl__signedType(intIdxType);
for param i in 1..rank {
// See domain follower for comments about this
const rStride = ranges(i).stride;
const rSignedStride = rStride:strType,
fSignedStride = followThis(i).stride:strType;
if rStride > 0 {
const riStride = rStride:intIdxType;
const low = ranges(i).alignedLowAsInt + followThis(i).low*riStride,
high = ranges(i).alignedLowAsInt + followThis(i).high*riStride,
stride = (rSignedStride * fSignedStride):strType;
block(i) = low..high by stride;
} else {
const irStride = (-rStride):intIdxType;
const low = ranges(i).alignedHighAsInt - followThis(i).high*irStride,
high = ranges(i).alignedHighAsInt - followThis(i).low*irStride,
stride = (rSignedStride * fSignedStride):strType;
block(i) = low..high by stride;
}
}
} else {
for param i in 1..rank do
block(i) = ranges(i)._low+followThis(i).low:intIdxType..ranges(i)._low+followThis(i).high:intIdxType;
}
if rank == 1 {
for i in zip((...block)) {
yield chpl_intToIdx(i);
}
} else {
for i in these_help(1, block) {
yield chpl_intToIdx(i);
}
}
}
proc dsiMember(ind: rank*idxType) {
for param i in 1..rank do
if !ranges(i).contains(ind(i)) then
return false;
return true;
}
proc dsiIndexOrder(ind: rank*idxType) {
var totOrder: intIdxType;
var blk: intIdxType = 1;
for param d in 1..rank by -1 {
const orderD = ranges(d).indexOrder(ind(d));
// NOTE: This follows from the implementation of indexOrder()
if (orderD == (-1):intIdxType) then return orderD;
totOrder += orderD * blk;
blk *= ranges(d).length;
}
return totOrder;
}
proc dsiDims()
return ranges;
proc dsiDim(d : int)
return ranges(d);
// optional, is this necessary? probably not now that
// homogeneous tuples are implemented as C vectors.
proc dsiDim(param d : int)
return ranges(d);
proc dsiNumIndices {
var sum = 1:intIdxType;
for param i in 1..rank do
sum *= ranges(i).length;
return sum;
// WANT: return * reduce (this(1..rank).length);
}
proc dsiLow {
if rank == 1 {
return ranges(1).low;
} else {
var result: rank*idxType;
for param i in 1..rank do
result(i) = ranges(i).low;
return result;
}
}
proc dsiHigh {
if rank == 1 {
return ranges(1).high;
} else {
var result: rank*idxType;
for param i in 1..rank do
result(i) = ranges(i).high;
return result;
}
}
proc dsiAlignedLow {
if rank == 1 {
return ranges(1).alignedLow;
} else {
var result: rank*idxType;
for param i in 1..rank do
result(i) = ranges(i).alignedLow;
return result;
}
}
proc dsiAlignedHigh {
if rank == 1 {
return ranges(1).alignedHigh;
} else {
var result: rank*idxType;
for param i in 1..rank do
result(i) = ranges(i).alignedHigh;
return result;
}
}
proc dsiStride {
if rank == 1 {
return ranges(1).stride;
} else {
var result: rank*chpl__signedType(intIdxType);
for param i in 1..rank do
result(i) = ranges(i).stride;
return result;
}
}
proc dsiAlignment {
if rank == 1 {
return ranges(1).alignment;
} else {
var result: rank*idxType;
for param i in 1..rank do
result(i) = ranges(i).alignment;
return result;
}
}
proc dsiFirst {
if rank == 1 {
return ranges(1).first;
} else {
var result: rank*idxType;
for param i in 1..rank do
result(i) = ranges(i).first;
return result;
}
}
proc dsiLast {
if rank == 1 {
return ranges(1).last;
} else {
var result: rank*idxType;
for param i in 1..rank do
result(i) = ranges(i).last;
return result;
}
}
proc dsiBuildArray(type eltType) {
return new unmanaged DefaultRectangularArr(eltType=eltType, rank=rank,
idxType=idxType,
stridable=stridable,
dom=_to_unmanaged(this));
}
proc dsiBuildArrayWith(type eltType, data:_ddata(eltType), allocSize:int) {
var allocRange:range(idxType) = (ranges(1).low)..#allocSize;
return new unmanaged DefaultRectangularArr(eltType=eltType,
rank=rank,
idxType=idxType,
stridable=stridable,
dom=_to_unmanaged(this),
data=data,
dataAllocRange=allocRange);
}
proc dsiLocalSlice(ranges) {
halt("all dsiLocalSlice calls on DefaultRectangulars should be handled in ChapelArray.chpl");
}
proc dsiTargetLocales() {
return [this.locale, ];
}
proc dsiHasSingleLocalSubdomain() param return true;
proc dsiLocalSubdomain(loc: locale) {
if (this.locale == loc) {
return _getDomain(_to_unmanaged(this));
} else {
var a: domain(rank, idxType, stridable);
return a;
}
}
iter dsiLocalSubdomains(loc: locale) {
yield dsiLocalSubdomain(loc);
}
// convenience routine for turning an int (tuple) into an index (tuple)
inline proc chpl_intToIdx(i) {
return chpl__intToIdx(this.idxType, i);
}
}
// helper routines for converting tuples of integers into tuple indices
inline proc chpl__intToIdx(type idxType, i: integral, j ...) {
const first = chpl__intToIdx(idxType, i);
const rest = chpl__intToIdx(idxType, (...j));
return (first, (...rest));
}
inline proc chpl__intToIdx(type idxType, i: integral, j: integral) {
return (chpl__intToIdx(idxType, i), chpl__intToIdx(idxType, j));
}
inline proc chpl__intToIdx(type idxType, i: _tuple) {
return chpl__intToIdx(idxType, (...i));
}
// TODO: should this include the ranges that represent the domain?
record _remoteAccessData {
type eltType;
param rank : int;
type idxType;
param stridable: bool;
param blkChanged : bool = false;
var off: rank*idxType;
var blk: rank*chpl__idxTypeToIntIdxType(idxType);
var str: rank*chpl__signedType(chpl__idxTypeToIntIdxType(idxType));
var origin: chpl__idxTypeToIntIdxType(idxType);
var factoredOffs: chpl__idxTypeToIntIdxType(idxType);
var data: _ddata(eltType);
var shiftedData: _ddata(eltType);
inline proc theData ref {
if stridable {
return data;
} else {
return shiftedData;
}
}
inline proc getDataElem(i) ref {
if stridable {
return dataElem(i);
} else {
return shiftedDataElem(i);
}
}
inline proc dataElem(i) ref {
return data(i);
}
inline proc shiftedDataElem(i) ref
return shiftedData(i);
}
inline proc _remoteAccessData.getDataIndex(ind : idxType) {
return this.getDataIndex(chpl__tuplify(ind));
}
//
// Copied from DefaultRectangularArr.getDataIndex
//
inline proc _remoteAccessData.getDataIndex(ind: rank*idxType) {
if stridable {
var sum = origin;
for param i in 1..rank do
sum += (chpl__idxToInt(ind(i)) - chpl__idxToInt(off(i))) * blk(i) / abs(str(i)):chpl__idxTypeToIntIdxType(idxType);
return sum;
} else {
// optimize common case to get cleaner generated code
if (rank == 1 && earlyShiftData) {
if blkChanged {
return chpl__idxToInt(ind(1)) * blk(1);
} else {
return chpl__idxToInt(ind(1));
}
} else {
var sum = if earlyShiftData then 0:chpl__idxTypeToIntIdxType(idxType) else origin;
if blkChanged {
for param i in 1..rank {
sum += chpl__idxToInt(ind(i)) * blk(i);
}
} else {
if storageOrder == ArrayStorageOrder.RMO {
for param i in 1..rank-1 {
sum += chpl__idxToInt(ind(i)) * blk(i);
}
sum += chpl__idxToInt(ind(rank));
} else {
for param i in 2..rank {
sum += chpl__idxToInt(ind(i)) * blk(i);
}
sum += chpl__idxToInt(ind(1));
}
}
if !earlyShiftData then sum -= factoredOffs;
return sum;
}
}
}
proc _remoteAccessData.computeFactoredOffs() {
factoredOffs = 0;
for param i in 1..rank do {
factoredOffs = factoredOffs + blk(i) * chpl__idxToInt(off(i));
}
}
proc _remoteAccessData.initShiftedData() {
if earlyShiftData && !stridable {
type idxSignedType = chpl__signedType(chpl__idxTypeToIntIdxType(idxType));
const shiftDist = if isIntType(idxType) then origin - factoredOffs
else origin:idxSignedType - factoredOffs:idxSignedType;
shiftedData = _ddata_shift(eltType, data, shiftDist);
}
}
proc _remoteAccessData.strideAlignUp(lo, r)
return r.low + (lo - r.low + abs(r.stride):idxType - 1)
/ abs(r.stride):idxType * abs(r.stride):idxType;
proc _remoteAccessData.strideAlignDown(hi, r)
return hi - (hi - r.low) % abs(r.stride):idxType;
proc _remoteAccessData.initDataFrom(other : _remoteAccessData) {
this.data = other.data;
}
//
// Based on the old 'dsiSlice' method
//
proc _remoteAccessData.toSlice(newDom) {
compilerAssert(this.rank == newDom.rank);
// NB: Sets 'blkChanged' if the new domain is stridable.
var rad : _remoteAccessData(eltType, newDom.rank, newDom.idxType, newDom.stridable, newDom.stridable || this.blkChanged);
rad.initDataFrom(this);
rad.shiftedData = if newDom.stridable then this.data else this.shiftedData;
rad.origin = this.origin:newDom.idxType;
rad.off = chpl__tuplify(newDom.dsiLow);
rad.str = chpl__tuplify(newDom.dsiStride);
for param i in 1..rank {
const shift = this.blk(i) * (chpl__idxToInt(newDom.dsiDim(i).low) - chpl__idxToInt(this.off(i))) / abs(this.str(i)) : rad.idxType;
if this.str(i) > 0 {
rad.origin += shift;
} else {
rad.origin -= shift;
}
const mult = (newDom.dsiDim(i).stride / this.str(i)) : rad.idxType;
rad.blk(i) = this.blk(i) * mult;
}
rad.computeFactoredOffs();
rad.initShiftedData();
return rad;
}
//
// Based on the old 'dsiReindex' method
//
proc _remoteAccessData.toReindex(newDom) {
compilerAssert(this.rank == newDom.rank);
// NB: Only sets 'blkChanged' if underlying RADs have it set
var rad : _remoteAccessData(eltType, newDom.rank, newDom.idxType, newDom.stridable, blkChanged);
rad.initDataFrom(this);
rad.shiftedData = if newDom.stridable then this.data else this.shiftedData;
rad.origin = this.origin:newDom.intIdxType;
rad.blk = this.blk;
rad.off = chpl__tuplify(newDom.dsiLow);
rad.str = chpl__tuplify(newDom.dsiStride);
rad.factoredOffs = 0:newDom.intIdxType;
rad.computeFactoredOffs();
rad.initShiftedData();
return rad;
}
//
// Based on the old 'dsiRankChange' method
//
proc _remoteAccessData.toRankChange(newDom, cd, idx) {
compilerAssert(this.rank == idx.size && this.rank != newDom.rank);
type intIdxType = newDom.intIdxType;
type idxSignedType = chpl__signedType(intIdxType);
// Unconditionally sets 'blkChanged'
//
// TODO: If 'collapsedDims' were param, we would know if blk(rank) was 1 or not.
var rad : _remoteAccessData(eltType, newDom.rank, newDom.idxType, newDom.stridable, true);
const collapsedDims = chpl__tuplify(cd);
rad.initDataFrom(this);
rad.shiftedData = if newDom.stridable then this.data else this.shiftedData;
rad.origin = this.origin:newDom.intIdxType;
var curDim = 1;
for param j in 1..idx.size {
if !collapsedDims(j) {
rad.off(curDim) = newDom.dsiDim(curDim).low;
const off = (chpl__idxToInt(rad.off(curDim)) - chpl__idxToInt(this.off(j))):idxSignedType;
rad.origin += ((this.blk(j):idxSignedType) * off / this.str(j)):intIdxType;
rad.blk(curDim) = this.blk(j);
rad.str(curDim) = this.str(j);
curDim += 1;
} else {
const off = (chpl__idxToInt(idx(j)) - chpl__idxToInt(this.off(j))):idxSignedType;
rad.origin += (this.blk(j):idxSignedType * off / this.str(j)):intIdxType;
}
}
rad.computeFactoredOffs();
rad.initShiftedData();
return rad;
}
//
// Local cache of remote ddata access info
//
class LocRADCache {
type eltType;
param rank: int;
type idxType;
param stridable: bool;
var targetLocDom: domain(rank);
var RAD: [targetLocDom] _remoteAccessData(eltType, rank, idxType,
stridable);
var RADLocks: [targetLocDom] chpl_LocalSpinlock;
pragma "dont disable remote value forwarding"
proc init(type eltType, param rank: int, type idxType,
param stridable: bool, newTargetLocDom: domain(rank)) {
this.eltType = eltType;
this.rank = rank;
this.idxType = idxType;
this.stridable = stridable;
// This should resize the arrays
targetLocDom=newTargetLocDom;
}
inline proc lockRAD(rlocIdx) {
RADLocks[rlocIdx].lock();
}
inline proc unlockRAD(rlocIdx) {
RADLocks[rlocIdx].unlock();
}
}
class DefaultRectangularArr: BaseRectangularArr {
/*type eltType;
param rank : int;
type idxType;
param stridable: bool;*/