-
Notifications
You must be signed in to change notification settings - Fork 411
/
ChapelDistribution.chpl
1366 lines (1122 loc) · 41.1 KB
/
ChapelDistribution.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 2020-2024 Hewlett Packard Enterprise Development LP
* 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.
*/
module ChapelDistribution {
use ChapelArray, ChapelRange;
use ChapelLocks;
use ChapelHashtable;
//
// Distribution wrapper record
//
pragma "distribution"
pragma "ignore noinit"
@chpldoc.nodoc
record _distribution : writeSerializable, readDeserializable {
var _pid:int; // only used when privatized
pragma "owned"
var _instance; // generic, but an instance of a subclass of BaseDist
var _unowned:bool; // 'true' for the result of 'getDistribution',
// in which case, the record destructor should
// not attempt to delete the _instance.
proc init(_pid : int, _instance, _unowned : bool) {
this._pid = _pid;
this._instance = _instance;
this._unowned = _unowned;
}
proc init(value) {
this._pid = if _isPrivatized(value) then _newPrivatizedClass(value) else nullPid;
this._instance = _to_unmanaged(value);
}
// Note: This does not handle the case where the desired type of 'this'
// does not match the type of 'other'. That case is handled by the compiler
// via coercions.
proc init=(const ref other : _distribution) {
var value = other._value.dsiClone();
this.init(value);
}
inline proc _value {
if _isPrivatized(_instance) {
return chpl_getPrivatizedCopy(_instance.type, _pid);
} else {
return _instance;
}
}
forwarding _value except targetLocales;
inline proc _do_destroy() {
if ! _unowned && ! _instance.singleton() {
on _instance {
// Count the number of domains that refer to this distribution.
// and mark the distribution to be freed when that number reaches 0.
// If the number is 0, .remove() returns the distribution
// that should be freed.
var distToFree = _instance.remove();
if distToFree != nil {
_delete_dist(distToFree!, _isPrivatized(_instance));
}
}
}
}
proc deinit() {
_do_destroy();
}
proc clone() {
return new _distribution(_value.dsiClone());
}
proc newRectangularDom(param rank: int, type idxType,
param strides: strideKind,
ranges: rank*range(idxType, boundKind.both, strides),
definedConst: bool = false) {
var x = _value.dsiNewRectangularDom(rank, idxType, strides, ranges);
x.definedConst = definedConst;
if x.linksDistribution() {
_value.add_dom(x);
}
return x;
}
proc newRectangularDom(param rank: int, type idxType,
param strides: strideKind,
definedConst: bool = false) {
var ranges: rank*range(idxType, boundKind.both, strides);
return newRectangularDom(rank, idxType, strides, ranges, definedConst);
}
proc newAssociativeDom(type idxType, param parSafe: bool=true) {
var x = _value.dsiNewAssociativeDom(idxType, parSafe);
if x.linksDistribution() {
_value.add_dom(x);
}
return x;
}
proc newSparseDom(param rank: int, type idxType, dom: domain) {
var x = _value.dsiNewSparseDom(rank, idxType, dom);
if x.linksDistribution() {
_value.add_dom(x);
}
return x;
}
proc idxToLocale(ind) do return _value.dsiIndexToLocale(ind);
@chpldoc.nodoc
proc ref deserialize(reader, ref deserializer) throws {
reader.read(_value);
}
// TODO: Can't this be an initializer?
@chpldoc.nodoc
proc type deserializeFrom(reader, ref deserializer) throws {
var ret : this;
ret.deserialize(reader, deserializer);
return ret;
}
@chpldoc.nodoc
proc serialize(writer, ref serializer) throws {
writer.write(_value);
}
proc displayRepresentation() { _value.dsiDisplayRepresentation(); }
/*
Return an array of locales over which this distribution was declared.
*/
proc targetLocales() const ref {
return _value.dsiTargetLocales();
}
} // record _distribution
//
// Abstract distribution class
//
pragma "base dist"
class BaseDist {
var _doms_containing_dist: int; // number of domains using this distribution
var _domsLock: chpl_LocalSpinlock; // lock for concurrent access
var _free_when_no_doms: bool; // true when original _distribution is destroyed
var pid:int = nullPid; // privatized ID, if privatization is supported
proc deinit() {
}
// Returns a distribution that should be freed or nil.
pragma "dont disable remote value forwarding"
proc remove(): unmanaged BaseDist? {
var free_dist = false;
if dsiTrackDomains() {
on this {
var dom_count = -1;
local {
_domsLock.lock();
// Set a flag to indicate it should be freed when _doms
// becomes empty
_free_when_no_doms = true;
dom_count = _doms_containing_dist;
_domsLock.unlock();
}
if dom_count == 0 then
free_dist = true;
}
} else {
// if we're not tracking domains, always free the
// distribution when it goes out of scope.
free_dist = true;
}
if free_dist then
return _to_unmanaged(this);
else
return nil;
}
// Returns true if the distribution should be removed.
inline proc remove_dom(x:unmanaged BaseDom): bool {
var count = -1;
on this {
var cnt = -1;
local {
_domsLock.lock();
_doms_containing_dist -= 1;
cnt = _doms_containing_dist;
// add one for the main distribution instance
if !_free_when_no_doms then
cnt += 1;
_domsLock.unlock();
}
count = cnt;
}
return (count==0);
}
//
// TODO: There may be some opportunities to optimize out the
// locking here, as in the add_arr() case. For example, the
// construction of the distribution and domain used for rank change
// slicing could use an unlocked version because that operation
// creates a new distribution followed immediately by a domain
// over the distribution. It's unclear how important this
// optimization is, though, because rank change slices are
// arguably less common (and already more expensive in most cases
// due to the creation of distribution and domain objects) than
// rank-preserving slicing.
//
inline proc add_dom(x:unmanaged BaseDom) {
on this {
_domsLock.lock();
_doms_containing_dist += 1 ;
_domsLock.unlock();
}
}
proc dsiDisplayRepresentation() {
writeln("<no way to display representation>");
}
pragma "last resort" @chpldoc.nodoc
proc dsiNewRectangularDom(param rank: int, type idxType,
param strides: strideKind, inds) {
compilerError("rectangular domains not supported by this distribution");
}
pragma "last resort" @chpldoc.nodoc
proc dsiNewAssociativeDom(type idxType, param parSafe: bool) {
compilerError("associative domains not supported by this distribution");
}
pragma "last resort" @chpldoc.nodoc
proc dsiNewSparseDom(param rank: int, type idxType, dom: domain) {
compilerError("sparse domains not supported by this distribution");
}
proc dsiSupportsPrivatization() param do return false;
proc dsiRequiresPrivatization() param do return false;
proc dsiDestroyDist() { }
// Does the distribution keep a list of domains? Can the domains
// keep the distribution alive longer? false for DefaultDist.
proc trackDomains() param do return true;
// dynamically-dispatched counterpart of linksDistribution
proc dsiTrackDomains() do return true;
// indicates if a distribution is a singleton. If so, we make no
// effort to free it. DefaultDist is a singleton.
proc singleton() param do return false;
// We could add dsiSingleton as a dynamically-dispatched counterpart
// indicates if this distribution is a layout. This helps
// with certain warnings.
proc dsiIsLayout() param do return false;
}
//
// Abstract domain classes
//
pragma "base domain"
class BaseDom {
// Head pointer to doubly-linked-list of arrays declared over this domain.
// Only used when trackArrays() is true. Instead of having an external list
// data structure, we just store the prev/next pointers directly in BaseArr
var _arrs_head: unmanaged BaseArr?;
var _arrs_containing_dom: int; // number of arrays using this domain
// as var A: [D] [1..2] real
// is using {1..2}
var _arrsLock: chpl_LocalSpinlock; // lock for concurrent access
var _free_when_no_arrs: bool;
var pid:int = nullPid; // privatized ID, if privatization is supported
var definedConst: bool;
proc init() {
}
proc deinit() {
}
iter _arrs: unmanaged BaseArr {
var tmp = _arrs_head;
while tmp != nil {
yield tmp!;
tmp = tmp!.next;
}
}
proc dsiMyDist(): unmanaged BaseDist {
halt("internal error: dsiMyDist is not implemented");
}
// default overloads to provide clear compile-time error messages
proc dnsError(param op: string) {
compilerError("this domain type does not support '", op, "'");
}
pragma "last resort" @chpldoc.nodoc
proc dsiLow { dnsError("low"); }
pragma "last resort" @chpldoc.nodoc
proc dsiHigh { dnsError("high"); }
pragma "last resort" @chpldoc.nodoc
proc dsiAlignedLow { dnsError("alignedLow"); }
pragma "last resort" @chpldoc.nodoc
proc dsiAlignedHigh { dnsError("alignedHigh"); }
pragma "last resort" @chpldoc.nodoc
proc dsiFirst { dnsError("first"); }
pragma "last resort" @chpldoc.nodoc
proc dsiLast { dnsError("last"); }
pragma "last resort" @chpldoc.nodoc
proc dsiStride { dnsError("stride"); }
pragma "last resort" @chpldoc.nodoc
proc dsiAlignment { dnsError("alignment"); }
pragma "last resort" @chpldoc.nodoc
proc dsiIndexOrder(i) { dnsError("indexOrder"); }
pragma "last resort" @chpldoc.nodoc
proc dsiCreateIndexBuffer(size) { dnsError("createIndexBuffer"); }
// end of default overloads to provide clear compile-time error messages
inline proc trackArrays() {
return disableConstDomainOpt || !this.definedConst;
}
// Returns (dom, dist).
// if this domain should be deleted, dom=this; otherwise it is nil.
// dist is nil or a distribution that should be removed.
pragma "dont disable remote value forwarding"
proc remove() : (unmanaged BaseDom?, unmanaged BaseDist?) {
if boundsChecking {
// TODO -- remove dsiLinksDistribution
assert( dsiMyDist().dsiTrackDomains() == dsiLinksDistribution() );
}
var ret_dom:unmanaged BaseDom? = nil;
var ret_dist:unmanaged BaseDist? = nil;
var dist = dsiMyDist();
var free_dom = false;
var remove_dist = false;
on this {
var arr_count = 0;
// Count the number of arrays using this domain
// and mark this domain to free itself when that number reaches 0.
local {
_arrsLock.lock();
arr_count = _arrs_containing_dom;
_free_when_no_arrs = true;
_arrsLock.unlock();
}
if arr_count == 0 {
free_dom = true;
if dsiLinksDistribution() {
// Remove the domain from the distribution
// and find out if the distribution should be removed.
remove_dist = dist.remove_dom(_to_unmanaged(this));
}
}
}
if free_dom then
ret_dom = _to_unmanaged(this); // caller will delete this
if remove_dist then
ret_dist = dist; // caller will remove dist
return (ret_dom, ret_dist);
}
// returns true if the domain should be removed
// 'rmFromList' indicates whether this is an array that we've been
// storing in the _arrs linked list or not (just counting it).
// Currently, only slices using existing domains avoid the list.
inline proc remove_arr(x:unmanaged BaseArr, param rmFromList=true): bool {
var count = -1;
on this {
var cnt = -1;
_arrsLock.lock();
_arrs_containing_dom -=1;
if rmFromList && trackArrays() {
if _arrs_head == x then _arrs_head = x.next;
if const xnext = x.next then xnext.prev = x.prev;
if const xprev = x.prev then xprev.next = x.next;
}
cnt = _arrs_containing_dom;
// add one for the main domain record
if !_free_when_no_arrs then
cnt += 1;
_arrsLock.unlock();
count = cnt;
}
return (count==0);
}
// addToList indicates whether this array should be added to the
// '_arrs' linked list, or just counted. At present, slice views
// are not added to the linked list because they don't need to be
// resized when their domain is re-assigned).
inline proc add_arr(x:unmanaged BaseArr, param locking=true,
param addToList = true) {
on this {
if locking then
_arrsLock.lock();
_arrs_containing_dom += 1;
if addToList && trackArrays() {
assert (x.prev == nil && x.next == nil);
if const ahead = _arrs_head {
x.next = ahead;
ahead.prev = x;
}
_arrs_head = x;
}
if locking then
_arrsLock.unlock();
}
}
// returns true if the domain should be removed
inline proc remove_containing_arr(x:unmanaged BaseArr) {
var count = -1;
on this {
var cnt = -1;
_arrsLock.lock();
_arrs_containing_dom -= 1;
cnt = _arrs_containing_dom;
// add one for the main domain record
if !_free_when_no_arrs then
cnt += 1;
_arrsLock.unlock();
count = cnt;
}
return (count==0);
}
inline proc add_containing_arr(x:unmanaged BaseArr) {
on this {
_arrsLock.lock();
_arrs_containing_dom += 1;
_arrsLock.unlock();
}
}
proc dsiSupportsPrivatization() param do return false;
proc dsiRequiresPrivatization() param do return false;
// Does the distribution keep a list of domains? Can the
// domains keep the distribution alive longer?
proc linksDistribution() param do return true;
// dynamically-dispatched counterpart of linksDistribution
proc dsiLinksDistribution() do return true;
// Overload to to customize domain destruction
proc dsiDestroyDom() { }
proc dsiDisplayRepresentation() { writeln("<no way to display representation>"); }
proc dsiSupportsAutoLocalAccess() param {
return false;
}
proc dsiIteratorYieldsLocalElements() param {
return false;
}
proc isRectangular() param do return false;
proc isAssociative() param do return false;
proc isSparse() param do return false;
proc type isDefaultRectangular() param do return false;
proc isDefaultRectangular() param do return false;
proc isSliceDomainView() param do return false; // likely unnecessary?
proc isRankChangeDomainView() param do return false;
proc isReindexDomainView() param do return false;
// proc dsiAssignDomain is a required overload to implement domain
// assignment. It is not declared here because we do not wish
// to use virtual dispatch to versions for all domain arguments.
//
// It should be declared like so:
// proc dsiAssignDomain(rhs: domain, lhsPrivate:bool)
//
// if lhsPrivate=true, the receiver is a private variable
// that:
// hasn't yet been shared with other tasks
// doesn't yet have any arrays declared over it
//
// Note that assigning to a domain typically causes arrays
// declared over that domain to be reallocated.
//
// Rectangular domains can implement this function with:
// proc dsiAssignDomain(rhs: domain, lhsPrivate:bool) {
// chpl_assignDomainWithGetSetIndices(this, rhs);
// }
//
// Irregular domains can implement this function with:
// proc dsiAssignDomain(rhs: domain, lhsPrivate:bool) {
// chpl_assignDomainWithIndsIterSafeForRemoving(this, rhs);
// }
}
class BaseRectangularDom : BaseDom {
param rank : int;
type idxType;
param strides: strideKind;
@chpldoc.nodoc proc hasUnitStride() param do return strides.isOne();
@chpldoc.nodoc proc hasPosNegUnitStride() param do return strides.isPosNegOne();
override proc isRectangular() param do return true;
proc getBaseArrType() type {
var tmp = new unmanaged BaseArrOverRectangularDom(rank=rank, idxType=idxType, strides=strides);
return tmp.type;
}
proc deinit() {
// this is a bug workaround
}
proc dsiAdd(in x) {
compilerError("Cannot add indices to a rectangular domain");
}
proc dsiRemove(x) {
compilerError("Cannot remove indices from a rectangular domain");
}
}
class BaseSparseDomImpl : BaseSparseDom(?) {
var nnzDom = {1..0};
proc deinit() {
// this is a bug workaround
}
override proc dsiBulkAdd(inds: [] index(rank, idxType),
dataSorted=false, isUnique=false, addOn=nilLocale){
var inds_ = inds;
return bulkAdd_help(inds_, dataSorted, isUnique, addOn);
}
override proc dsiBulkAddNoPreserveInds(ref inds: [] index(rank, idxType),
dataSorted=false, isUnique=false, addOn=nilLocale){
return bulkAdd_help(inds, dataSorted, isUnique, addOn);
}
proc bulkAdd_help(ref inds: [?indsDom] index(rank, idxType),
dataSorted=false, isUnique=false, addOn=nilLocale): int {
halt("Helper function called on the BaseSparseDomImpl");
}
// TODO: Would ChapelArray.resizeAllocRange() be too expensive?
// - would have to put the conditional outside of the call
/* Grow domain if necessary */
inline proc _grow(size: int, factor=arrayAsVecGrowthFactor) {
const oldNNZDomSize = nnzDom.size;
if (size > oldNNZDomSize) {
const _newNNZDomSize = if (oldNNZDomSize) then ceil(factor*oldNNZDomSize):int else 1;
nnzDom = {0..#_newNNZDomSize};
}
}
// TODO: Would ChapelArray.resizeAllocRange() be too expensive?
// - would have to put the conditional outside of the call
/* Shrink domain if necessary */
inline proc _shrink(size: int, factor=arrayAsVecGrowthFactor) {
if size == 0 {
nnzDom = {1..0};
} else {
// TODO: floor() ?
const shrinkThreshold = (nnzDom.size / (factor**2)): int;
if (size < shrinkThreshold) {
const _newNNZDomSize = (nnzDom.size / factor): int;
nnzDom = {0..#_newNNZDomSize};
}
}
}
// This method assumes nnz is updated according to the size
// requested. So, a bulk addition into a sparse domain should: (1)
// calculate new nnz and update it, (2) call this method, (3) add
// indices
inline proc _bulkGrow() {
use Math;
const nnz = getNNZ();
if (nnz > nnzDom.size) {
const _newNNZDomSize = (exp2(log2(nnz)+1.0)):int;
nnzDom = {0..#_newNNZDomSize};
}
}
inline proc _countDuplicates(arr) where isArray(arr) {
var dupCount = -1;
var prev = arr[arr.domain.lowBound];
for a in arr {
if a == prev then
dupCount += 1;
else
prev = a;;
}
return dupCount;
}
// (1) sorts indices if !dataSorted
// (2) verifies the flags are set correctly if boundsChecking
// (3) checks OOB if boundsChecking
proc bulkAdd_prepareInds(ref inds, dataSorted, isUnique, cmp) {
use Sort;
if !dataSorted then sort(inds, comparator=cmp);
//verify sorted and no duplicates if not --fast
if boundsChecking {
if dataSorted && !isSorted(inds, comparator=cmp) then
halt("bulkAdd: Data not sorted, call the function with \
dataSorted=false");
//check duplicates assuming sorted
if isUnique {
const indsStart = inds.domain.lowBound;
const indsEnd = inds.domain.highBound;
var lastInd = inds[indsStart];
for i in indsStart+1..indsEnd {
if inds[i] == lastInd then
halt("bulkAdd: There are duplicates, call the function \
with isUnique=false");
else lastInd = inds[i];
}
}
//check OOB
for i in inds do boundsCheck(i);
}
}
// this is a helper function for bulkAdd functions in sparse subdomains, which
// store the nonzeros in order based on their major and minor index
// NOTE:it assumes that nnz array of the sparse domain has non-negative
// indices. If, for some reason it changes, this function and bulkAdds have to
// be refactored. (I think it is a safe assumption at this point and keeps the
// function a bit cleaner than some other approach. -Engin)
proc __getActualInsertPts(d, inds, isUnique) {
//find individual insert points
//and eliminate duplicates between inds and dom
var indivInsertPts: [inds.domain] int;
var actualInsertPts: [inds.domain] int; //where to put in newdom
//eliminate duplicates --assumes sorted
if !isUnique {
//make sure lastInd != inds[inds.domain.lowBound]
var lastInd = inds[inds.domain.lowBound] + 1;
for (i, p) in zip(inds, indivInsertPts) {
if i == lastInd then p = -1;
else lastInd = i;
}
}
forall (i,p) in zip(inds, indivInsertPts) {
if isUnique || p != -1 { //don't do anything if it's duplicate
const (found, insertPt) = d.find(i);
p = if found then -1 else insertPt; //mark as duplicate
}
}
//shift insert points for bulk addition
//previous indexes that are added will cause a shift in the next indexes
var actualAddCnt = 0;
//NOTE: this can also be done with scan
for (ip, ap) in zip(indivInsertPts, actualInsertPts) {
if ip != -1 {
ap = ip + actualAddCnt;
actualAddCnt += 1;
}
else ap = ip;
}
return (actualInsertPts, actualAddCnt);
}
proc dsiClear(){
halt("not implemented");
}
}
record SparseIndexBuffer {
param rank: int;
var obj: borrowed BaseSparseDom(?);
type idxType = if rank==1 then int else rank*int;
var bufDom = domain(1);
var buf: [bufDom] idxType;
var cur = 0;
proc init(size, param rank: int, obj) {
this.rank = rank;
this.obj = obj;
bufDom = {0..#size};
}
proc ref deinit() {
commit();
}
proc ref add(idx: idxType) {
buf[cur] = idx;
cur += 1;
if cur == buf.size then
commit();
}
proc ref commit() {
if cur >= 1 then
obj.dsiBulkAdd(buf[..cur-1]);
cur = 0;
}
}
class BaseSparseDom : BaseDom {
// rank and idxType will be moved to BaseDom
param rank: int;
type idxType;
var parentDom;
// We currently cannot have dist here. It is due to a compiler bug due to
// inheritance of generic var fields.
// var dist;
/*var nnz = 0; //: int;*/
override proc isSparse() param do return true;
proc getNNZ(): int {
halt("nnz queried on base class");
}
proc deinit() {
// this is a bug workaround
}
proc dsiClear() {
halt("clear not implemented for this distribution - BaseSparseDom");
}
proc dsiBulkAdd(inds: [] index(rank, idxType),
dataSorted=false, isUnique=false,
addOn=nilLocale): int {
halt("Bulk addition is not supported by this sparse domain");
}
proc dsiBulkAddNoPreserveInds(ref inds: [] index(rank, idxType),
dataSorted=false, isUnique=false,
addOn=nilLocale): int {
halt("Bulk addition is not supported by this sparse domain");
}
proc boundsCheck(ind: index(rank, idxType)):void {
if boundsChecking then
if !(parentDom.contains(ind)) then
halt("Sparse domain/array index out of bounds: ", ind,
" (expected to be within ", parentDom, ")");
}
proc canDoDirectAssignment(rhs: domain) {
if this.parentDom.isRectangular() &&
rhs.parentDom.isRectangular() {
if this.dsiNumIndices == 0 {
if this.parentDom.contains(rhs.parentDom) {
return true;
}
}
}
return false;
}
//basic DSI functions
proc dsiDim(d: int) { return parentDom.dim(d); }
proc dsiDims() { return parentDom.dims(); }
proc dsiNumIndices { return getNNZ(); }
proc dsiSize { return getNNZ(); }
override proc dsiLow { return parentDom.lowBound; }
override proc dsiHigh { return parentDom.highBound; }
override proc dsiStride { return parentDom.stride; }
override proc dsiAlignment { return parentDom.alignment; }
override proc dsiFirst: rank*idxType {
halt("dsiFirst is not implemented");
}
override proc dsiLast: rank*idxType {
halt("dsiLast not implemented");
}
override proc dsiAlignedLow { return parentDom.low; }
override proc dsiAlignedHigh { return parentDom.high; }
override proc dsiCreateIndexBuffer(size) {
return new SparseIndexBuffer(rank=this.rank, obj=this, size=size);
}
} // end BaseSparseDom
class BaseAssociativeDom : BaseDom {
override proc isAssociative() param do return true;
proc deinit() {
// this is a bug workaround
}
proc dsiClear() {
halt("clear not implemented for this domain");
}
proc dsiAdd(in idx) {
compilerError("Index addition is not supported by this domain");
}
proc rank param {
return 1;
}
}
//
// Abstract array class
//
pragma "base array"
class BaseArr {
var prev: unmanaged BaseArr?;
var next: unmanaged BaseArr?;
var pid: int = nullPid; // privatized ID, if privatization is supported
var _decEltRefCounts : bool = false;
var _resizePolicy = chpl_ddataResizePolicy.normalInit;
proc chpl__rvfMe() param {
return false;
}
proc isSliceArrayView() param {
return false;
}
proc isRankChangeArrayView() param {
return false;
}
proc isReindexArrayView() param {
return false;
}
proc deinit() {
}
proc dsiStaticFastFollowCheck(type leadType) param do return false;
proc dsiGetBaseDom(): unmanaged BaseDom {
halt("internal error: dsiGetBaseDom is not implemented");
}
// takes 'rmFromList' which indicates whether the array should
// be removed from the domain's list or just decremented from
// its count of other arrays.
//
// returns (arr, dom)
// arr is this if it should be deleted, or nil.
// dom is a domain that should be removed, or nil.
pragma "dont disable remote value forwarding"
proc remove(param rmFromList: bool) {
var ret_arr = this; // this array is always deleted
var ret_dom:unmanaged BaseDom? = nil;
var rm_dom = false;
var dom = dsiGetBaseDom();
// Remove the array from the domain
// and find out if the domain should be removed.
rm_dom = dom.remove_arr(_to_unmanaged(this), rmFromList);
if rm_dom then
ret_dom = dom;
return (ret_arr, ret_dom);
}
proc chpl_setResizePolicy(policy: chpl_ddataResizePolicy) {
_resizePolicy = policy;
}
proc chpl_isElementTypeDefaultInitializable(): bool {
halt("chpl_isElementTypeDefaultInitializable must be defined");
}
proc chpl_isElementTypeNonNilableClass(): bool {
halt("chpl_isElementTypeNonNilableClass must be defined");
}
proc chpl_unsafeAssignIsClassElementNil(manager, idx): bool {
halt("chpl_unsafeAssignIsClassElementNil must be defined");
}
proc chpl_unsafeAssignHaltUninitializedElement(idx) {
halt("chpl_haltUnsafeAssignmentUninitializedElement must be defined");
}
proc dsiElementInitializationComplete() {
halt("dsiElementInitializationComplete must be defined");
}
proc dsiElementDeinitializationComplete() {
halt("dsiElementDeinitializationComplete must be defined");
}
proc dsiDestroyArr(deinitElts:bool) {
halt("dsiDestroyArr must be defined");
}
proc dsiReallocate(d: domain) {
halt("reallocating not supported for this array type");
}
proc dsiPostReallocate() {
}
// This method is unsatisfactory -- see bradc's commit entries of
// 01/02/08 around 14:30 for details
proc _purge( ind: int) {
halt("purging not supported for this array type");
}
proc _resize( length: int, old_map) {
halt("resizing not supported for this array type");
}
//
// Ultimately, these routines should not appear here; instead, we'd
// like to do a dynamic cast in the sparse array class(es) that call
// these routines in order to call them directly and avoid the
// dynamic dispatch and leaking of this name to the class. In order
// to do this we'd need to hoist eltType to the base class, which
// would require better subclassing of generic classes. A good
// summer project for Jonathan?
//
proc sparseShiftArray(shiftrange, initrange) {
halt("sparseGrowDomain not supported for non-sparse arrays");
}
proc sparseShiftArrayBack(shiftrange) {
halt("sparseShiftArrayBack not supported for non-sparse arrays");
}
proc sparseBulkShiftArray(shiftMap, oldnnz) {
halt("sparseBulkShiftArray not supported for non-sparse arrays");
}