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.

376 chapter 14slightly faster or smaller than the original version, undo your changes and go back tothe original, clear code.Use the right algorithms and data structures: Do not use an O(n 2 ) DFT algorithm to doa Fourier transform of a thousand elements when there’s an O(n log n) FFT available.Similarly, do not store a thousand items in an array that requires an O(n) search whenyou could use an O(log n) binary tree or an O(1) hash table.14.14.1 Programming for Virtual Memory (Method)While paging makes little appear big, you pay a price because your program’s runtime increases with each page fault. If your program does not fit into RAM all atonce, it will run significantly slower. If virtual memory is shared among multipleprograms that run simultaneously, they all can’t have the entire RAM at once, andso there will be memory access conflicts, in which case the performance of all theprograms will suffer. The basic rules for programming for virtual memory are asfollows.1. Do not waste your time worrying about reducing the amount of memory used(the working set size) unless your program is large. In that case, take a globalview of your entire program and optimize those parts that contain the largestarrays.2. Avoid page faults by organizing your programs to successively perform theircalculations on subsets of data, each fitting completely into RAM.3. Avoid simultaneous calculations in the same program to avoid competitionfor memory and consequent page faults. Complete each major calculationbefore starting another.4. Group data elements close together in memory blocks if they are going to beused together in calculations.14.14.2 Optimizing Programs; Java versus Fortran/CMany of the optimization techniques developed for Fortran and C are also relevantfor Java applications. Yet while Java is a good language for scientific programmingand is the most universal and portable of all languages, at present Java code runsslower than Fortran or C code and does not work well if you use MPI for parallelcomputing (see Appendix D). In part, this is a consequence of the Fortran and Ccompilers having been around longer and thereby having been better refined toget the most out of a computer’s hardware, and in part this is also a consequence ofJava being designed for portability and not speed. Since modern computers are sofast, whether a program takes 1s or 3s usually does not matter much, especially in−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 376

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

Saved successfully!

Ooh no, something went wrong!