Skip to content

Commit

Permalink
Convert Hashed class to a record, hashedDist (chapel-lang#23404)
Browse files Browse the repository at this point in the history
[not reviewed, will get a post-merge review tomorrow]

The last of the distributions to be converted!  And a fairly easy one...
  • Loading branch information
bradcray authored Sep 15, 2023
2 parents 23d8b85 + 9b97eb4 commit a330d2c
Show file tree
Hide file tree
Showing 22 changed files with 153 additions and 28 deletions.
8 changes: 8 additions & 0 deletions modules/dists/DSIUtil.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,14 @@ record chpl_PrivatizedDistHelper : writeSerializable {
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() {
Expand Down
117 changes: 109 additions & 8 deletions modules/dists/HashedDist.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
@unstable("HashedDist is unstable and may change in the future")
prototype module HashedDist {

use DSIUtil;

config param debugUserMapAssoc = false;


Expand Down Expand Up @@ -115,7 +117,99 @@ The `Hashed` domain map initializer is defined as follows:
targetLocales: [] locale = Locales)
*/
class Hashed : BaseDist, writeSerializable {
record hashedDist : writeSerializable {
type idxType;
type mapperT;

forwarding const chpl_distHelp: chpl_PrivatizedDistHelper(unmanaged HashedImpl(idxType, _to_unmanaged(mapperT)));

proc init(type idxType,
mapper:?t = new DefaultMapper(),
targetLocales: [] locale = Locales) {
const value = new unmanaged HashedImpl(idxType, mapper, targetLocales);
this.idxType = idxType;
this.mapperT = _to_unmanaged(t);
this.chpl_distHelp = new chpl_PrivatizedDistHelper(
if _isPrivatized(value)
then _newPrivatizedClass(value)
else nullPid,
value);
}

proc init(_pid : int, _instance, _unowned : bool) {
this.idxType = _instance.idxType;
this.mapperT = _to_unmanaged(_instance.mapper.type);
this.chpl_distHelp = new chpl_PrivatizedDistHelper(_pid,
_instance,
_unowned);
}

proc init(value) {
this.idxType = value.idxType;
this.mapperT = _to_unmanaged(value.mapper.type);
this.chpl_distHelp = new chpl_PrivatizedDistHelper(
if _isPrivatized(value)
then _newPrivatizedClass(value)
else nullPid,
_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 : hashedDist(?)) {
this.init(other._value.dsiClone());
}

proc clone() {
return new hashedDist(this._value.dsiClone());
}

@chpldoc.nodoc
inline operator ==(d1: hashedDist(?), d2: hashedDist(?)) {
if (d1._value == d2._value) then
return true;
return d1._value.dsiEqualDMaps(d2._value);
}

@chpldoc.nodoc
inline operator !=(d1: hashedDist(?), d2: hashedDist(?)) {
return !(d1 == d2);
}

proc writeThis(x) {
chpl_distHelp.writeThis(x);
}

proc serialize(writer, ref serializer) throws {
writeThis(writer);
}
}


@chpldoc.nodoc
@unstable(category="experimental", reason="assignment between distributions is currently unstable due to lack of testing")
operator =(ref a: hashedDist(?), b: hashedDist(?)) {
if a._value == nil {
__primitive("move", a, chpl__autoCopy(b.clone(), definedConst=false));
} else {
if a._value.type != b._value.type then
compilerError("type mismatch in distribution assignment");
if a._value == b._value {
// do nothing
} else
a._value.dsiAssign(b._value);
if _isPrivatized(a._instance) then
_reprivatize(a._value);
}
}


@deprecated("'Hashed' is deprecated, please use 'hashedDist' instead")
type Hashed = hashedDist;


class HashedImpl : BaseDist, writeSerializable {

// GENERICS:

Expand Down Expand Up @@ -175,7 +269,7 @@ class Hashed : BaseDist, writeSerializable {
//
proc init(type idxType,
mapper,
other: unmanaged Hashed(idxType, mapper.type)) {
other: unmanaged HashedImpl(idxType, mapper.type)) {
this.idxType = idxType;
this.mapper = mapper; // normally == other.mapper;
targetLocDom = other.targetLocDom;
Expand All @@ -189,7 +283,7 @@ class Hashed : BaseDist, writeSerializable {
proc dsiGetPrivatizeData() do return this.mapper;

proc dsiPrivatize(privatizeData) {
return new unmanaged Hashed(idxType, privatizeData, _to_unmanaged(this));
return new unmanaged HashedImpl(idxType, privatizeData, _to_unmanaged(this));
}
proc dsiGetReprivatizeData() do return 0;

Expand All @@ -198,7 +292,7 @@ class Hashed : BaseDist, writeSerializable {
}

proc dsiClone() {
return new unmanaged Hashed(idxType, mapper, targetLocales);
return new unmanaged HashedImpl(idxType, mapper, targetLocales);
}

// DISTRIBUTION INTERFACE:
Expand Down Expand Up @@ -238,8 +332,8 @@ class Hashed : BaseDist, writeSerializable {
// print out the distribution
//
proc writeThis(x) throws {
x.writeln("Hashed");
x.writeln("-------");
x.writeln("hashedDist");
x.writeln("----------");
x.writeln("distributed using: ", mapper);
x.writeln("across locales: ", targetLocales);
x.writeln("indexed via: ", targetLocDom);
Expand Down Expand Up @@ -304,7 +398,7 @@ class UserMapAssocDom: BaseAssociativeDom {
//
// LEFT: a pointer to the parent distribution
//
const dist: unmanaged Hashed(idxType, mapperType);
const dist: unmanaged HashedImpl(idxType, mapperType);

//
// DOWN: an array of local domain class descriptors -- set up in
Expand Down Expand Up @@ -519,7 +613,7 @@ class UserMapAssocDom: BaseAssociativeDom {
//
// INTERNAL INTERFACE
//
override proc dsiMyDist(): unmanaged Hashed(idxType, mapperType) {
override proc dsiMyDist(): unmanaged HashedImpl(idxType, mapperType) {
return dist;
}

Expand Down Expand Up @@ -566,6 +660,13 @@ class UserMapAssocDom: BaseAssociativeDom {
}

proc rank param { return 1; }

proc dsiGetDist() {
if _isPrivatized(dist) then
return new hashedDist(dist.pid, dist, _unowned=true);
else
return new hashedDist(nullPid, dist, _unowned=true);
}
}


Expand Down
2 changes: 1 addition & 1 deletion test/arrays/userAPI/assocDistributedHashed.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ proc main() {
output.writeln([1,2,3]);

var myMapper = new MyMapper();
var D: domain(string) dmapped Hashed(idxType=string, mapper=myMapper);
var D: domain(string) dmapped hashedDist(idxType=string, mapper=myMapper);
var A:[D] real;

D += "zero";
Expand Down
9 changes: 9 additions & 0 deletions test/deprecated/dmapType.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ config var n = 10, leaksAndBreaks=false;
writeln(Dist2.type:string);
}

{
use HashedDist;
var Dom: domain(int) dmapped new dmap(new Hashed(idxType=int));;
Dom += 1;
var A: [Dom] real;
writeln(A);
const Dist2: dmap(Hashed(int, DefaultMapper)) = new Hashed(int);
writeln(Dist2.type:string);
}
7 changes: 7 additions & 0 deletions test/deprecated/dmapType.good
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ dmapType.chpl:72: warning: 'BlockCyclic' is deprecated, please use 'blockCycDist
dmapType.chpl:84: warning: 'DimensionalDist2D' is deprecated, please use 'dimensionalDist2D' instead
dmapType.chpl:85: warning: 'DimensionalDist2D' is deprecated, please use 'dimensionalDist2D' instead
dmapType.chpl:93: warning: 'DimensionalDist2D' is deprecated, please use 'dimensionalDist2D' instead
dmapType.chpl:99: warning: 'Hashed' is deprecated, please use 'hashedDist' instead
dmapType.chpl:103: warning: 'Hashed' is deprecated, please use 'hashedDist' instead
dmapType.chpl:103: warning: 'Hashed' is deprecated, please use 'hashedDist' instead
dmapType.chpl:92: warning: 'DimensionalDist2D' is deprecated, please use 'dimensionalDist2D' instead
dmapType.chpl:4: warning: The use of 'dmap' is deprecated for this distribution; please replace 'new dmap(new <DistName>(<args>))' with 'new <DistName>(<args>)'
dmapType.chpl:8: warning: The use of 'dmap' is deprecated for this distribution; please replace 'dmap(<DistName>(<args>))' with '<DistName>(<args>)'
Expand All @@ -35,6 +38,8 @@ dmapType.chpl:68: warning: The use of 'dmap' is deprecated for this distribution
dmapType.chpl:72: warning: The use of 'dmap' is deprecated for this distribution; please replace 'dmap(<DistName>(<args>))' with '<DistName>(<args>)'
dmapType.chpl:84: warning: The use of 'dmap' is deprecated for this distribution; please replace 'new dmap(new <DistName>(<args>))' with 'new <DistName>(<args>)'
dmapType.chpl:92: warning: The use of 'dmap' is deprecated for this distribution; please replace 'dmap(<DistName>(<args>))' with '<DistName>(<args>)'
dmapType.chpl:99: warning: The use of 'dmap' is deprecated for this distribution; please replace 'new dmap(new <DistName>(<args>))' with 'new <DistName>(<args>)'
dmapType.chpl:103: warning: The use of 'dmap' is deprecated for this distribution; please replace 'dmap(<DistName>(<args>))' with '<DistName>(<args>)'
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
blockDist(1,int(64),unmanaged DefaultDist)
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Expand All @@ -57,3 +62,5 @@ blockCycDist(1,int(64))
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
dimensionalDist2D([domain(2,int(64),one)] locale,BlockDim(int(64)),BlockDim(int(64)),int(64))
0.0
hashedDist(int(64),DefaultMapper)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ record MyMapper {
}

var myMapper = new MyMapper();
var newDist = new dmap(new unmanaged Hashed(idxType=real, mapper=myMapper));
var newDist = new hashedDist(idxType=real, mapper=myMapper);

var D: domain(real) dmapped newDist;

Expand Down
2 changes: 1 addition & 1 deletion test/distributions/bradc/assoc/userAssoc-array-noelt.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ record MyMapper {
}
}

var newDist = new dmap(new unmanaged Hashed(idxType=real, mapper=new MyMapper()));
var newDist = new hashedDist(idxType=real, mapper=new MyMapper());

var D: domain(real) dmapped newDist;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ record MyMapper {
}

proc doit() {
var newDist = new dmap(new unmanaged Hashed(idxType=real, mapper=new MyMapper()));
var newDist = new hashedDist(idxType=real, mapper=new MyMapper());

var D: domain(real) dmapped newDist;

Expand Down
2 changes: 1 addition & 1 deletion test/distributions/bradc/assoc/userAssoc-basic-array.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ record MyMapper {
}
}

var newDist = new dmap(new unmanaged Hashed(idxType=real, mapper=new MyMapper()));
var newDist = new hashedDist(idxType=real, mapper=new MyMapper());

var D: domain(real) dmapped newDist;

Expand Down
2 changes: 1 addition & 1 deletion test/distributions/bradc/assoc/userAssoc-basic-domain.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ record MyMapper {
}
}

var newDist = new dmap(new unmanaged Hashed(idxType=real, mapper=new MyMapper()));
var newDist = new hashedDist(idxType=real, mapper=new MyMapper());

var D: domain(real) dmapped newDist;

Expand Down
2 changes: 1 addition & 1 deletion test/distributions/bradc/assoc/userAssoc-class.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var myMapper = proc(ind:real, targetLocs: Locales.type) {
return indAsInt % numlocs;
};

var D: domain(real) dmapped Hashed(idxType=real, mapper=new MyMapper());
var D: domain(real) dmapped hashedDist(idxType=real, mapper=new MyMapper());

D += 1.3;
D += 22.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use HashedDist;

config const verbose = false;

var newDist = new dmap(new Hashed(idxType=real));
var newDist = new hashedDist(idxType=real);

var D: domain(real) dmapped newDist;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ record MyMapper {
}

proc doit() {
var newDist = new dmap(new unmanaged Hashed(idxType=real, mapper=new MyMapper()));
var newDist = new hashedDist(idxType=real, mapper=new MyMapper());

var D: domain(real) dmapped newDist;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Random;
config const requestCapacity = true;
config const updatesPerLocale = 10000;

var D: domain(int) dmapped Hashed(idxType=int);
var D: domain(int) dmapped hashedDist(idxType=int);

if requestCapacity then
D.requestCapacity(updatesPerLocale*numLocales);
Expand Down
4 changes: 2 additions & 2 deletions test/distributions/bradc/assoc/userAssoc-fcf.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ record MyMapper {
}
}

var newDist = new dmap(new unmanaged Hashed(idxType=real, mapper=new MyMapper()));
var newDist = new hashedDist(idxType=real, mapper=new MyMapper());

var myMapper = proc(ind:real, targetLocs: Locales.type) {
const numlocs = targetLocs.domain.size;
const indAsInt = ind: int;
return indAsInt % numlocs;
};

var D: domain(real) dmapped Hashed(idxType=real, mapper=myMapper);
var D: domain(real) dmapped hashedDist(idxType=real, mapper=myMapper);

D += 1.3;
D += 22.0;
Expand Down
2 changes: 1 addition & 1 deletion test/distributions/ferguson/dist-assoc-assign.chpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use HashedDist;

var D: domain(real) dmapped Hashed(idxType=real);
var D: domain(real) dmapped hashedDist(idxType=real);
D += 1.3;

var localD: domain(real);
Expand Down
2 changes: 1 addition & 1 deletion test/distributions/ferguson/dist-assoc-bulkadd.chpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use HashedDist;

var D: domain(real) dmapped Hashed(idxType=real);
var D: domain(real) dmapped hashedDist(idxType=real);

var inds = [2.1, ];

Expand Down
2 changes: 1 addition & 1 deletion test/distributions/ferguson/dist-assoc-writeln.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ record MyMapper {
}
}

var D: domain(int) dmapped Hashed(idxType=int, mapper=new MyMapper());
var D: domain(int) dmapped hashedDist(idxType=int, mapper=new MyMapper());
D += 0;
D += 1;

Expand Down
2 changes: 1 addition & 1 deletion test/functions/generic/poi/hashed-dist-with-mapper.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ specifically:
}
proc main() {
var myMapper = new MyMapper();
var D: domain(string) dmapped Hashed(idxType=string, mapper=myMapper);
var D: domain(string) dmapped hashedDist(idxType=string, mapper=myMapper);
D += "zero";
}
*/
Expand Down
4 changes: 2 additions & 2 deletions test/optimizations/autoLocalAccess/common.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ proc createDom(space) {
return space dmapped stencilDist(space, fluff=(1,1));
}
}
else if distType == Hashed {
var D: domain(int) dmapped Hashed(idxType=int);
else if distType == hashedDist {
var D: domain(int) dmapped hashedDist(idxType=int);
for i in space {
D += i;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
-sdistType=blockCycDist --report-auto-local-access -M$CHPL_HOME/test/optimizations/autoLocalAccess/
-sdistType=cyclicDist --report-auto-local-access -M$CHPL_HOME/test/optimizations/autoLocalAccess/
-sdistType=stencilDist --report-auto-local-access -M$CHPL_HOME/test/optimizations/autoLocalAccess/
-sdistType=Hashed --report-auto-local-access -M$CHPL_HOME/test/optimizations/autoLocalAccess/
-sdistType=hashedDist --report-auto-local-access -M$CHPL_HOME/test/optimizations/autoLocalAccess/
Loading

0 comments on commit a330d2c

Please sign in to comment.