<< 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

Three charges q_{1}=+3\mu C, q_{2}=+6\mu C and q_{3}=+8\mu C are located at (2,0)m (0,0)m and (0,3) coordinates respectively. Find the magnitude and direction acted upon q_{2} by the two other charges.Draw the correct graphical illustration of the problem above showing the direction of all forces.
Kate Reply
To solve this problem, we need to first find the net force acting on charge q_{2}. The magnitude of the force exerted by q_{1} on q_{2} is given by F=\frac{kq_{1}q_{2}}{r^{2}} where k is the Coulomb constant, q_{1} and q_{2} are the charges of the particles, and r is the distance between them.
Muhammed
What is the direction and net electric force on q_{1}= 5µC located at (0,4)r due to charges q_{2}=7mu located at (0,0)m and q_{3}=3\mu C located at (4,0)m?
Kate Reply
what is the change in momentum of a body?
Eunice Reply
what is a capacitor?
Raymond Reply
Capacitor is a separation of opposite charges using an insulator of very small dimension between them. Capacitor is used for allowing an AC (alternating current) to pass while a DC (direct current) is blocked.
Gautam
A motor travelling at 72km/m on sighting a stop sign applying the breaks such that under constant deaccelerate in the meters of 50 metres what is the magnitude of the accelerate
Maria Reply
please solve
Sharon
8m/s²
Aishat
What is Thermodynamics
Muordit
velocity can be 72 km/h in question. 72 km/h=20 m/s, v^2=2.a.x , 20^2=2.a.50, a=4 m/s^2.
Mehmet
A boat travels due east at a speed of 40meter per seconds across a river flowing due south at 30meter per seconds. what is the resultant speed of the boat
Saheed Reply
50 m/s due south east
Someone
which has a higher temperature, 1cup of boiling water or 1teapot of boiling water which can transfer more heat 1cup of boiling water or 1 teapot of boiling water explain your . answer
Ramon Reply
I believe temperature being an intensive property does not change for any amount of boiling water whereas heat being an extensive property changes with amount/size of the system.
Someone
Scratch that
Someone
temperature for any amount of water to boil at ntp is 100⁰C (it is a state function and and intensive property) and it depends both will give same amount of heat because the surface available for heat transfer is greater in case of the kettle as well as the heat stored in it but if you talk.....
Someone
about the amount of heat stored in the system then in that case since the mass of water in the kettle is greater so more energy is required to raise the temperature b/c more molecules of water are present in the kettle
Someone
definitely of physics
Haryormhidey Reply
how many start and codon
Esrael Reply
what is field
Felix Reply
physics, biology and chemistry this is my Field
ALIYU
field is a region of space under the influence of some physical properties
Collete
what is ogarnic chemistry
WISDOM Reply
determine the slope giving that 3y+ 2x-14=0
WISDOM
Another formula for Acceleration
Belty Reply
a=v/t. a=f/m a
IHUMA
innocent
Adah
pratica A on solution of hydro chloric acid,B is a solution containing 0.5000 mole ofsodium chlorid per dm³,put A in the burret and titrate 20.00 or 25.00cm³ portion of B using melting orange as the indicator. record the deside of your burret tabulate the burret reading and calculate the average volume of acid used?
Nassze Reply
how do lnternal energy measures
Esrael
Two bodies attract each other electrically. Do they both have to be charged? Answer the same question if the bodies repel one another.
JALLAH Reply
No. According to Isac Newtons law. this two bodies maybe you and the wall beside you. Attracting depends on the mass och each body and distance between them.
Dlovan
Are you really asking if two bodies have to be charged to be influenced by Coulombs Law?
Robert
like charges repel while unlike charges atttact
Raymond
What is specific heat capacity
Destiny Reply
Specific heat capacity is a measure of the amount of energy required to raise the temperature of a substance by one degree Celsius (or Kelvin). It is measured in Joules per kilogram per degree Celsius (J/kg°C).
AI-Robot
specific heat capacity is the amount of energy needed to raise the temperature of a substance by one degree Celsius or kelvin
ROKEEB
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