Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distribute 2D array row wise among Locales in Chapel

Tags:

chapel

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.

like image 948
Robin Sharma Avatar asked Nov 19 '18 06:11

Robin Sharma


1 Answers

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.

like image 101
ben-albrecht Avatar answered Jan 03 '23 22:01

ben-albrecht