simons blog

Mutual Refinement and Composition

Introduction

Composition is a fundamental operation on Layouts. An example one encounters for example "in the wild" is composition of data with Thread Value Layout which than gives the user possibility to assign appropriate portion of data to each thread as indicated in this picture which I took from the CuTe docs:

TV_Layout

The categorical treatment of Layouts that Colfax established equips us with a step by step procedure to calculate Layout composition using the graphical representation of morphisms. In this blogpost I want to recap Chapter 4 of the Colfax Paper and conclude therewith my series of blogposts on this Paper.

Mutual Refinement and Composition

Consider A=(6,6):(6,1), B=(12,3,6):(1,72,12).

Let's find corresponding Morphism representation. Let f be morphism which encodes A and g be morphism which encodes B.

f:

This can be depicted visually:

Composition Intro

Note that we attempt to compute the Layout composition BA that corresponds to gf.

We see that is problematic, because we can't "input" the arrows coming from f to the start of g.

To be clear, what we want would be to have a picture like this, where arrows flow from f into the domain of g and from there into the codomain of g. We see below that loosely speaking that is archived by splitting up the 6 into a 2 and a 3 in the codomain of f and by factoring the 12 in the domain of g into 6 and 2.

Composition Into 2

Here we see the concept visually.

Composition Intro 3

Let's recap the concept of Refinement. ((2,(2,2))) refines ((2,4)) refines (8) but not in the other direction. That is because in our framework Refinement can be thought of such that S refines S if S may be obtained from S by replacing each entry in S with a some nested tuples of equal size. Let us think a moment about what this geometrically means using a simple example:

(2,4) can be geometrically thought of as a rectangle. (2,(2,2)) also describes shape of a rectangle. However one dimension of a rectangle can be again described as a rectangle. This is useful and important concept in CuTe because it allows us to implement highly non trivial patterns in an intuitive way as you can convince yourself of by following examples from my blogpost about hierarchical Layouts.

A mutual refinement can be thought of in terms of the following diagram

Mutual refinement

In other words we want to refine the codomain of one function and the domain of another function such that we all possible values of the codomain of one function lie in the domain of the other function. That in turn will yield that we can compose the two functions we obtain by mutually refining.

Algo

This is an algorithm to find mutual refinement between two nested tuples.

Let's apply it to codomain(f)=T=(6,6) and domain(g)=U=(12,3,6).

Initially we have X=T,Y=U,X=(),Y=(),Xm=(),Ym=(). We will now compute the mutual refinement T,U.

Note that len(X)=2 and len(Y)=3.

First step:

Fill Y' with remaining entries from Y Y=((6,2),3,6)

So we obtained T=(6,(2,3)) and U=((6,2),3,6)

Diagram

The diagram on the left show

T'   U'
T    U

We know that the domain of the second morphism needs to contain the codomain of the first morphism (after all, that is the whole reason we perform mutual refinement) which is depicted by the fact that for the upper 6 we only have an outgoing arrow. For all the other entries we have ingoing and outgoing arrow.

Branching 1

Note that we take all "branching arrows" at the second stage and instead branch at first stage and than have an "identity arrow" pointing from ii.

Branching 2

Here we simply make the replacement that for every incoming branch we replace it with a simple identity onto itself ii.

Let's take a look again at the above diagram and show how to resolve the diagram:

Diagram Compose 1

The left side is obtained by the process provided above.

  1. Find mutual refinement.
  2. Bring each part separately into the desired form by using above process Gives us the left side.

From here we can drop the "mutual refinement" layer from the middle because by construction it is clear that ii from left and right side. This gives us the right diagram from where we can read of gf=((2,3),6)(6,2,6,3) over α=(2,4,1).

This in return yields the Layout:

We have a useful theorem that connect the composition C=Lgf to BA.

Formula

So to obtain the composition we just take the size(A) complement of C.

That means we have a clear procedure to compute composition:

Let's use the most complex example given in the Paper and analyse it step by step:

Step_1

Step_2

Step_3

Step_4

Step_5

Step_6

Step_7

Conclusion

This concludes the blogpost about the Colfax paper. Please consider starring the repository they released along the paper. The codebase delivered there offers also ability to produce lots of the figures shown here and of course can be used to let computer perform the calculations. However it is helpful if we know how things supposed to look like so we can sanity check the computer output and especially understand how to interpret them.