# Presentation: 136479

## C++ in Scientific Application:A Case Study of Genericity

- Walter E. Brown
*Fermi National Accelerator Laboratory*
- f

## The Basis  of All Scientific Computation

*“**We must measure what is measurable, and make measurable what is not so.”*
*	**	**	**	** – **Galileo*

- ACAT 2000

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

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

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

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

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

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

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

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

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

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

## Numeric Programming Today

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

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

## We Can Now Do Better!

- Modern **C++** gives us expressive power via:
**class**es and **namespace**s
**template**s (“parametric polymorphism”)
- Data abstraction has evolved:
- Object-oriented design and programming
- Generic programming technology
- Static type-checking is the norm

- ACAT 2000

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

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

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

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

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

**Notes:**
0 meters by default

2.5 * meter

internally .012 * meter

same as 6.0 centimeters

1.23e-12 meters

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

**Notes:**
0 meters by default

2.5 * meter

internally .012 * meter

same as 6.0 centimeters

1.23e-12 meters

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

## A User Viewpoint

*“ **[****SIunits****] is the first really good reason I’ve seen to switch  from Fortran to ****C++****.”*
- (Thank you!)

- ACAT 2000

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

- ACAT 2000

## C++ in Scientific Application:A Case Study of Genericity

- Walter E. Brown
*Fermi National Accelerator Laboratory*
- f