c++ in scientific application: a case study of genericity walter e. brown fermi national accelerator...
TRANSCRIPT
C++ in Scientific Application:A Case Study of Genericity
Walter E. BrownFermi National Accelerator Laboratory
f
ACAT 2000 2
The Basis of All Scientific Computation
“We must measure what is measurable, and make measurable what is not so.”
– Galileo
ACAT 2000 3
Mensuration has a Long History
“Ye shall do no unrighteousness in judgment, in measures of length, of weight, or of quantity.”
– Leviticus 19:35
ACAT 2000 4
We’ve Made Progress, e.g.
“And he made a molten sea, ten cubits from brim to brim…, and a line of thirty cubits did compass it round about.”
– I Kings 7:23
ACAT 2000 5
Standardized Units Came Later
“Throughout the kingdom there shall be standard measures of wine, ale, and corn.… Weights [also] are to be standardized similarly.”
– Magna Carta
ACAT 2000 6
Accepted First Principles
• “In carrying out any calculation, always … attach the proper units to the final result, for the result is meaningless without this…”
• “You should check the dimensions of all the equations you use.”
• “One way to spot an erroneous equation is to check the dimensions of all its terms….”
ACAT 2000 7
Parallel Principles for Software, too!
• “One way to spot an erroneous equation is to check the dimensions of all its terms.”
– Halliday & Resnick, 1970
• “One way to spot an erroneous program is to check the data types of all its objects !”
– W. E. Brown, 1998
ACAT 2000 8
Type-Checking is Not New
“There is no transfer into another kind, like the transfer from length to area and from area to solid.”
– Aristotle
ACAT 2000 9
Such Checking is Still Needed
“[A] famous bug in this shop is
2*pi+r*w
as the area of a strip of a cylinder, which escaped notice for years….”
– Name withheld by request
ACAT 2000 10
Unit Errors are Costly…
“NASA's Mars Climate Orbiter was lost… because engineers failed to [convert] from English units to metric, an embarrassing lapse that sent the $125 million craft fatally close to the Martian surface….”
– The Washington Post, 1 Oct. ‘99
ACAT 2000 11
… and Very Hard to Find!
“[T]he error had affected the orbiter mission from its launching almost 10 months and 416 million miles before its… failure. And yet the problem was never caught and corrected ….”
– The Washington Post, 1 Oct. ‘99
ACAT 2000 12
Static Type-Checking Provides …
… succinct, intelligible, consistent (!) program documentation.
… information that a compiler could exploit to produce more efficient code.
… important programmer feedback via early (compile-time) error detection.
ACAT 2000 13
Numeric Programming TodayExtensive inspection of code samples shows:
heavy use of native numeric types (e.g., double)
occasional use of synonymous types (e.g., CLHEP’s HepDouble).
But these are inadequate! Avogadro’s number + speed of light ???? Distance + Energy ? Mass + Momentum ? Alas, arbitrary combinations will compile!
ACAT 2000 14
Past Limitations
• Inadequate expressiveness in early lang’s:– Data types were limited to those in hardware.– General utility seemed to suffice.– We just didn’t know what we were missing!
• Programmer-defined data types came later:– Validated data abstraction methodology– Lacked enough power for generic programming
ACAT 2000 15
We Can Now Do Better!
• Modern C++ gives us expressive power via:– classes and namespaces– templates (“parametric polymorphism”)
• Data abstraction has evolved:– Object-oriented design and programming– Generic programming technology
• Static type-checking is the norm
ACAT 2000 16
SIunits Project Goals
Application of contemporary technology to computation involving physical concepts
Convenient (near-trivial) expression General utility based on existing standards Nomenclature from our problem domain No run-time performance penalties!
ACAT 2000 17
SIunits Highlights 1
• Based on le Système internationale d’unités:– Specifies base and derived quantities – Specifies corresponding units of measure
• Features compile-time static type-checking:– Permits only meaningful combinations– Forbids all incommensurate expressions!– Generates implied dimensions automatically
ACAT 2000 18
SIunits Highlights 2
• Attaches no run-time overhead:– No extra memory space per object– No extra execution time for user functions– Small initialization costs for I/O, etc.
• Includes a spectrum of five models:– Ranges from standard to natural (c=k=ħ=G=1).– Models are upwardly compatible.
ACAT 2000 19
SIunits Programming
• Dimensions are types:– Base 7: Length, Mass, Time, Current, …– Derived: Energy, Force, Entropy, …
• Units behave as right-hand constants:– Base 7: meter, second, ampere, mole, …– Composite 21: joule, watt, hertz, …– Also (sub)multiples: mega_, nano_, ...
ACAT 2000 20
Examples 1: Instantiation
using namespace si;
typedef Length<float> Len;
Len d1; // implicitly 0.0F
Len d2( 2.5*meter );
Len d3( 1.2*centimeter );
Len d4( 5*d3 );
Len d5( 1.23*pico_*meter);
ACAT 2000 21
Examples 2: Bad Instantiations
Len d6( d2*d3 ); // no!
Len d7( 3.5 ); // no!
Len d8 = 3.5; // no!
Len d9; // ok, but …
d9 = 3.5; // no!
// but:
Len d10( d2+d3 ); // ok
ACAT 2000 22
Type-Checking is Still Needed
“[T]hese are really insidious bugs, very hard to find by proofreading ... because your brain knows what the expression is supposed to be [so] you tend to see the intent and not the error.”
– Name withheld by request
ACAT 2000 23
A User Viewpoint
“ [SIunits] is the first really good reason I’ve seen to switch from Fortran to C++.”
(Thank you!)
ACAT 2000 24
Personal Viewpoints
• SIunits could not have been carried out absent C++ metaprogramming
• C++ vendor support is rapidly improving
• Alas, the concepts underlying C++ seem well understood and well applied by only a small percentage of the scientific community
C++ in Scientific Application:A Case Study of Genericity
Walter E. BrownFermi National Accelerator Laboratory
f