Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to check/ensure that two arrays have the same domain and distribution?

Tags:

chapel

A nice feature in Chapel is that it distinguishes between the domain of an array and its distribution. What is the best way to check that two arrays have the same domain and distribution (which one often wants)?

The best I can see is to check D1==D2 and D1.dist==D2.dist, if D1 and D2 are both domains.

In particular, consider the following code :

const Dom = {1..5, 1..5};
const BDom = newBlockDom(Dom);
var x : [Dom] int;
var y : [BDom] int;

test(x,y);

proc test(a : [?Dom1] int, b : [Dom1] int) {
}

This compiles and runs just fine, which makes sense if the query syntax in the function declaration just tests for domain equality, but not for distribution equality (even though Dom1 also knows about how a is distributed). Is the only way to check for distribution equality in this case is to do a.domain.dist==b.domain.dist?

like image 477
Nikhil Padmanabhan Avatar asked Oct 16 '22 02:10

Nikhil Padmanabhan


1 Answers

To check whether two domains describe the same distributed index set in Chapel, you're correct that you'd use D1 == D2 and D1.dist == D2.dist. Domain equality in Chapel checks whether two domains describe the same index set, so is independent of the domain maps / distributions. Similarly, an equality check between two domain maps / distributions checks whether they distribute indices identically.

Note that in Chapel, both domains and distributions have a notion of identity, so if you created two distributed domains as follows:

var BDom1 = newBlockDom(Dom),
    BDom2 = newBlockDom(Dom);

they would pass the above equality checks, yet be distinct domain values. In some cases, it might be reasonable to wonder whether two domain expressions refer to the identical domain instance, but I believe there is no official user-facing way to do this in Chapel today. If this is of interest, it would be worth filing a feature request against on our GitHub issues page.

With respect to your code example:

const Dom = {1..5, 1..5};
const BDom = newBlockDom(Dom);
var x : [Dom] int;
var y : [BDom] int;

test(x,y);

proc test(a : [?Dom1] int, b : [Dom1] int) {
}

there is a subtlety going on here that requires some explanation. First, note that if you reverse the arguments to your test() routine, it will not compile, acting perhaps more similar to what you were expecting (TIO):

test(y,x);

The reason for this is that domains which don't have an explicit domain map are treated specially in formal array arguments. Specifically, in defining Chapel, we didn't want to have a formal argument that was declared like X here:

proc foo(X: [1..n] real) { ... }

require that the actual array argument be non-distributed / have the default domain map. In other words, we wanted the user to be able to pass in a Block- or Cyclic-distributed array indexed from 1..n so that the formal was constraining the array's index set but not its distribution. Conversely, if a formal argument's domain is defined in terms of an explicit domain map, like:

proc bar(X: [BDom] int) { ... }

(using your Block-distributed definition of BDom above), it requires the actual array argument to match that domain.

An effect of this is that in your example, since Dom1 was matched to a domain with a default domain map, b is similarly loosely constrained to have the same index set yet with any distribution. Whereas when the first actual argument is block-distributed (as in my call), Dom1 encodes that distribution and applies the constraint to b.

If your reaction to this is that it feels confusing / asymmetric, I'm inclined to agree. I believe we have discussed treating declared/named domains differently from anonymous ones in this regard (since it was the anonymity of the domain in X: [1..n] that we were focused on when adopting this rule, and its application to queried domains like Dom1 in cases like this is something of a side effect of the current implementation). Again, a GitHub issue would be completely fair game for questioning / challenging this behavior.

like image 54
Brad Avatar answered Oct 21 '22 07:10

Brad