Distribute 2D array row wise among Locales in Chapel

我与影子孤独终老i 提交于 2020-08-27 09:02:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!