Online Book Reader

Home Category

High Performance Computing - Charles Severance [64]

By Root 1341 0
the loops moves the damage around, but doesn’t make it go away.

The loop to perform a matrix transpose represents a simple example of this dilemma:

DO I=1,N DO 20 J=1,M

DO J=1,M DO 10 I=1,N

A(J,I) = B(I,J) A(J,I) = B(I,J)

ENDDO ENDDO

ENDDO ENDDO

Whichever way you interchange them, you will break the memory access pattern for either A or B. Even more interesting, you have to make a choice between strided loads vs. strided stores: which will it be?[48] We really need a general method for improving the memory access patterns for both A and B, not one or the other. We’ll show you such a method in The Section Called “Blocking To Ease Memory Access Patterns”.


Blocking to Ease Memory Access Patterns*

Blocking is another kind of memory reference optimization. As with loop interchange, the challenge is to retrieve as much data as possible with as few cache misses as possible. We’d like to rearrange the loop nest so that it works on data in little neighborhoods, rather than striding through memory like a man on stilts. Given the following vector sum, how can we rearrange the loop?

DO I=1,N

DO J=1,N

A(J,I) = A(J,I) + B(I,J)

ENDDO

ENDDO

This loop involves two vectors. One is referenced with unit stride, the other with a stride of N. We can interchange the loops, but one way or another we still have N-strided array references on either A or B, either of which is undesirable. The trick is to block references so that you grab a few elements of A, and then a few of B, and then a few of A, and so on — in neighborhoods. We make this happen by combining inner and outer loop unrolling:

DO I=1,N,2

DO J=1,N,2

A(J,I) = A(J,I) + B(I,J)

A(J+1,I) = A(J+1,I) + B(I,J+1)

A(J,I+1) = A(J,I+1) + B(I+1,J)

A(J+1,I+1) = A(J+1,I+1) + B(I+1,J+1)

ENDDO

ENDDO

Use your imagination so we can show why this helps. Usually, when we think of a two-dimensional array, we think of a rectangle or a square (see Figure 2.9). Remember, to make programming easier, the compiler provides the illusion that two-dimensional arrays A and B are rectangular plots of memory as in Figure 2.9. Actually, memory is sequential storage. In FORTRAN, a two-dimensional array is constructed in memory by logically lining memory “strips” up against each other, like the pickets of a cedar fence. (It’s the other way around in C: rows are stacked on top of one another.) Array storage starts at the upper left, proceeds down to the bottom, and then starts over at the top of the next column. Stepping through the array with unit stride traces out the shape of a backwards “N,” repeated over and over, moving to the right.


Figure 2.9. Arrays A and B

Figure 2.10. How array elements are stored


Imagine that the thin horizontal lines of Figure 2.10 cut memory storage into pieces the size of individual cache entries. Picture how the loop will traverse them. Because of their index expressions, references to A go from top to bottom (in the backwards “N” shape), consuming every bit of each cache line, but references to B dash off to the right, using one piece of each cache entry and discarding the rest (see Figure 2.11, top). This low usage of cache entries will result in a high number of cache misses.

If we could somehow rearrange the loop so that it consumed the arrays in small rectangles, rather than strips, we could conserve some of the cache entries that are being discarded. This is exactly what we accomplished by unrolling both the inner and outer loops, as in the following example. Array A is referenced in several strips side by side, from top to bottom, while B is referenced in several strips side by side, from left to right (see Figure 2.11, bottom). This improves cache performance and lowers runtime.

For really big problems, more than cache entries are at stake. On virtual memory machines, memory references have to be translated through a TLB. If you are dealing with large arrays, TLB misses, in addition to cache misses, are going to add to your runtime.


Figure 2.11. 2×2 squares


Here’s something that may surprise you. In the code below,

Return Main Page Previous Page Next Page

®Online Book Reader