12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

378 chapter 14You see (Listing 14.1) that each iteration of the for loop requires the data andcode for all the functions as well as access to all the elements of the matrices andarrays. The working set size of this calculation is the sum of the sizes of the arraysf12(N,N), f21(N,N), and pion(N) plus the sums of the sizes of the functions force12and force21.A better way to perform the same calculation is to break it into separatecomponents (Listing 14.2):✞☎for j = 1, n; { for i = 1, n; f12 (i , j ) = force12 (pion ( i ) , pion ( j ) ) }for j = 1, n; { for i = 1, n; f21 (i , j ) = force21 (pion ( i ) , pion ( j ) ) }for✝j = 1, n; { for i = 1, n; ftot = f12(i , j ) + f21(i , j ) }Listing 14.2 GOOD program, separate loops.Here the separate calculations are independent and the working set size, that is, theamount of memory used, is reduced. However, you do pay the additional overheadcosts associated with creating extra for loops. Because the working set size of thefirst for loop is the sum of the sizes of the arrays f12(N, N) and pion(N), and ofthe function force12, we have approximately half the previous size. The size of thelast for loop is the sum of the sizes for the two arrays. The working set size of theentire program is the larger of the working set sizes for the different for loops.As an example of the need to group data elements close together in memory orcommon blocks if they are going to be used together in calculations, consider thefollowing code (Listing 14.3):✞☎Common zed , y l t ( 9 ) , part (9) , zpart1 (50000) , zpart2 (50000) , med2 ( 9 )for j = 1, n; ylt ( j ) = zed ∗ part ( j )/med2 ( 9 ) / / Discontinuous variables✝Listing 14.3 BAD Program, discontinuous memory.Here the variables zed, ylt, and part are used in the same calculations and areadjacent in memory because the programmer grouped them together in Common(global variables). Later, when the programmer realized that the array med2 wasneeded, it was tacked onto the end of Common.All the data comprising the variableszed, ylt, and part fit onto one page, but the med2 variable is on a different pagebecause the large array zpart2(50000) separates it from the other variables. Infact, the system may be forced to make the entire 4-kB page available in order tofetch the 72 B of data in med2. While it is difficult for a Fortran or C programmer toensure the placement of variables within page boundaries, you will improve yourchances by grouping data elements together (Listing 14.4):✞☎Common zed , y l t ( 9 ) , part (9) , med2 ( 9 ) , zpart1 (50000) , zpart2 (50000)for j = 1, n; ylt ( j ) = zed∗part ( j )/med2 ( J ) / / Continuous✝Listing 14.4 GOOD program, continuous memory.−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 378

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!