<< Chapter < Page Chapter >> Page >

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,NA(J,I) = A(J,I) + B(I,J) ENDDOENDDO

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,2A(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 [link] ). Remember, to make programming easier, the compiler provides the illusion that two-dimensional arrays A and B are rectangular plots of memory as in [link] . 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.

Arrays a and b

This figure shows two boxes. The first, labeled, Array A, contains a large grey letter, A, and a dashed grid pattern covering the box. The second, labeled Array B, contains a large grey letter, B, of the same size as letter A, with an identical dashed grid pattern inside the box.

How array elements are stored

This figure contains two objects. The first is an N-shaped zig-zag arrow, first going downward, then moving diagonally up and to the right, then moving downward again. The first downward section is labeled, column 1, and the second is labeled, column 2. The second object is a box filled with a pattern of similar zig-zag arrows, titled array. Inside the box is a dashed grid pattern overlaid on the arrows. A horizontal section of the grid is labled, denotes cache line boundary.

Imagine that the thin horizontal lines of [link] 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 [link] , 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 [link] , 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.

2×2 squares

This figure contains four objects. The first is a box labeled Array A containing an N-shaped zig-zag arrow, first going downward, then moving diagonally up and to the right, then moving downward again. Inside the box is a dashed grid pattern. The arrows are on the far left side of the box, and their length takes up the entire height of the box. The second is labeled, Array B, and contains a similar zig-zag of arrows, this time pointing to the right and in the topmost portion of the box. Array A and B are positioned next to each other. Below them is a horizontal line dividing the figure into two parts. Below the line are two more boxes, also labeled Array A and Array B, but with different arrows inside. The arrows in Array A are pictured as follows. Two small arrows in the top-left portion of the box point downward, and out of them is a larger arrow pointing down that stretches the entire height of the box. coming from the right and at a diagonal is a thick, faded line that meets the arrow at the bottom-left portion of the object. Array B contains the same shape, except with the arrows pointing to the right, from the uppermost portion of the object.

Here’s something that may surprise you. In the code below, we rewrite this loop yet again, this time blocking references at two different levels: in 2×2 squares to save cache entries, and by cutting the original loop in two parts to save TLB entries:


DO I=1,N,2 DO J=1,N/2,2A(J,I) = A(J,I) + B(I,J) A(J+1,I) = A(J+1,I) + B(I+1,J)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 ENDDODO I=1,N,2 DO J=N/2+1,N,2A(J,I) = A(J,I) + B(I,J) A(J+1,I) = A(J+1,I) + B(I+1,J)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

You might guess that adding more loops would be the wrong thing to do. But if you work with a reasonably large value of N , say 512, you will see a significant increase in performance. This is because the two arrays A and B are each 256 KB × 8 bytes = 2 MB when N is equal to 512 — larger than can be handled by the TLBs and caches of most processors.

The two boxes in [link] illustrate how the first few references to A and B look superimposed upon one another in the blocked and unblocked cases. Unblocked references to B zing off through memory, eating through cache and TLB entries. Blocked references are more sparing with the memory system.

Picture of unblocked versus blocked references

This figure contains two objects. The first is a box, labled, Blocked, with sets of arrows in a grid pointing in different directions. The for viewing purposes, the grid can be thought of as having four quadrants. In the upper-left quadrant, four parallel evenly-spaced arrows of equal length point to the right, and four identical arrows point down, crossing at sixteen points. In the upper-right quadrant, four parallel evenly-spaced arrows of equal length point down. And in the lower-left quadrant, four parallel evenly-spaced arrows of equal length point to the right. Below this object is a large, wide arrow pointing to the right, labeled, Strided Memory References. The second objects, labeled Unblocked, is similar in nature with arrows inside a box. There are eight arrows in this box. The arrows are parallel, spaced evenly, and of equal length. Four arrows come from the top-left of the object and point down. The other four arrows come from the top-left and point to the right. Below this object is a wider arrow pointed to the right, labeled strided memory references. An asterisk at the bottom of the figure, related to the titles of the two objects, is labeled, Arrays A and B are superimposed.

You can take blocking even further for larger problems. This code shows another method that limits the size of the inner loop and visits it repeatedly:


II = MOD (N,16) JJ = MOD (N,4)DO I=1,NDO J=1,JJ A(J,I) = A(J,I) + B(J,I)ENDDO ENDDODO I=1,IIDO J=JJ+1,N A(J,I) = A(J,I) + B(J,I)A(J,I) = A(J,I) + 1.0D0 ENDDOENDDODO I=II+1,N,16 DO J=JJ+1,N,4DO K=I,I+15 A(J,K) = A(J,K) + B(K,J)A(J+1,K) = A(J+1,K) + B(K,J+1) A(J+2,K) = A(J+2,K) + B(K,J+2)A(J+3,K) = A(J+3,K) + B(K,J+3) ENDDOENDDO ENDDO

Where the inner I loop used to execute N iterations at a time, the new K loop executes only 16 iterations. This divides and conquers a large memory address space by cutting it into little pieces.

While these blocking techniques begin to have diminishing returns on single-processor systems, on large multiprocessor systems with nonuniform memory access (NUMA), there can be significant benefit in carefully arranging memory accesses to maximize reuse of both cache lines and main memory pages.

Again, the combined unrolling and blocking techniques we just showed you are for loops with mixed stride expressions. They work very well for loop nests like the one we have been looking at. However, if all array references are strided the same way, you will want to try loop unrolling or loop interchange first.

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, High performance computing. OpenStax CNX. Aug 25, 2010 Download for free at http://cnx.org/content/col11136/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'High performance computing' conversation and receive update notifications?

Ask