问题
I am learning Chapel and have worked with blockdist but I can't figure out how can I distribute a 2-dimension array in row wise fashion among locales.
回答1:
The key is to pass a reshaped Locales
array as the targetLocales
argument to Block
. This is explained further below.
Here's a simple example of distributing a 2D array in a row-wise fashion:
use BlockDist;
// Using a 2D targetLocales rather than the default 1D Locales argument
var targetLocales = reshape(Locales, {0..#numLocales, 0..0});
const Space = {1..4, 1..4};
const D: domain(2) dmapped Block(boundingBox=Space, targetLocales=targetLocales) = Space;
var A: [D] int;
forall a in A do
a = a.locale.id;
writeln(A);
Sample outputs:
./row-wise -nl 4
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
./row-wise -nl 2
0 0 0 0
0 0 0 0
1 1 1 1
1 1 1 1
By default, distributions will use the built-in Locales array as the targetLocales
argument, which specifies how to paritition the elements of an array across the locales within a particular domain map, e.g. Block.
Since Locales
is a 1D array, and you're distributing a 2D array, the Block
distribution wraps Locales
like so:
1D targetLocales:
0 1 2 3 -> 0 1
2 3
So an array of shape (4,4)
would get mapped to 4 locales as:
0 0 1 1
0 0 1 1
2 2 3 3
2 2 3 3
By providing a 2D targetLocales
argument, we can tell Block
explicitly how we'd like the elements to be mapped to locales, rather than rely on the wrapping. Passing a targetLocales
array of locales with a shape of (4,1)
, will result in the desired row-wise distribution:
2D targetLocales:
0
1
2
3
So an array of shape (4,4)
would get mapped to 4 locales as:
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
This concept applies to other distributions as well.
来源:https://stackoverflow.com/questions/53369244/distribute-2d-array-row-wise-among-locales-in-chapel