Title: Programming Language Issues for BTeV
1Programming LanguageIssues for BTeV
- Marc PaternoCD/CPD/APS
- 1 October 2001
2Why Use C?
- Abstractions
- OO programming classes
- generic programming templates
- Robustness
- strong type safety, catch errors at compile time
- well-defined management of object lifetimes
necessary for resource allocation - Speed
- comparable to C or Fortran
3Outline
- I will assume that C is the primary language to
be used by BTeV. - this means I assume the Framework and EDM will be
written in C - Issues addressed
- C language features
- Interaction with Fortran
- Use of assembly language
- Other languages
- Java, Python, assembly language
4Abstraction
- C provides two main mechanisms for abstraction
- classes, for object-oriented programming
- templates, for generic programming
- Placing arbitrary limits on the use of either
weakens the ability to implement optimal designs - The greatest strength of C lies in a correct
combination of the two techniques
5OO terminology
- Classes need little introduction all we need
here is two definitions - Class a data type, which combines a description
of a state along with a set of functions
associated with the manipulation of that state - Object an instance of a class
- string is a class
- the string Hello is an object
6Templates
- Function templates express an algorithm,
parameterized on the type(s) of its argument(s)
sort(begin, end)
- Class templates generate a family of classes,
parameterized on some feature
vectorltintgt bitsetlt32gt
7Use the Standard Library
- The standard library provides a rich set of data
structures and algorithms - Prefer to use standard collections to third-party
versions - more portable
- more familiar
- Prefer to use standard algorithms to re-writing
them yourself - less work
- more efficiency
8Standard Collections
- Sequences
- vector a dynamic array
- list a doubly-linked list
- deque a double-ended queue
- Sorted containers
- set, multiset ordered collections
- map, multimap associative array (dictionary)
- Designed to interoperate with the standard
library algorithms
9Standard Algorithms
- searching
- find, find_first_of, search,...
- manipulation of collections
- copy, erase, random_shuffle, sort, partial_sort,
partition, ... - operations on sets
- set_union, set_difference,...
- many others
10Example of the simplest algorithm
vectorltThinggt v(...)// explicit loopfor (int i
0 i ! v.size() i) doWork(vi)//
standard algorithmstdfor_each(v.begin(),
v.end(), doWork)
- Even in this simple case, the standard algorithm
has benefits - less likely to have error in loop
initialization/ending/advancement - slight improvement in efficiency
11Robustness
- Bad data happens
- A program must not crash because of it
- A program must not be left in an inconsistent
state - Almost any function can fail
- so every function must be checked
- What options are availablefor writing robust
code?
12Option 1 Pervasive tests
- Do as was done in C and Fortran
- Every function returns a status condition
- Every call must be checked
13Example of option 1
int myfunc( ... ) int rc 0 if ((rc
f1(...) lt 0) return rc if ((rc f2(...) lt 0)
return rc // do other work, maybe resetting
rc ... return rc
- Work is obscured by the bulk of error-handling
code - Production-quality systems require still more
(logging, error stack) further obscuration
14Option 2 Exceptions
- Standard C provides a better mechanism
exceptions - An exception is thrown at the point where the
exceptional condition is detected - An exception is caught wherever that specific
kind of exception can be dealt with - C will clean up the call stack, releasing
resources, during unwinding - Beware of non-standard systems that do not
provide strict lifetime management!
15Example of option 2
- Using exceptions, the same functionality is
achieved much more cleanly - If f1( ) fails, then f2( ) is not called if f2(
) fails, the other work is not done
void myfunc( ... ) f1(...) f2(...) //
do other work ...
16Use of Fortran
- Possible to support both legacy code and new
development in Fortran - Primary constraint do not severely compromise
the infrastructure design to support Fortran - otherwise, one loses the advantages for which C
was chosen - if full freedom for use of Fortran is required,
Fortran should be the main language
17Aside What is Fortran?
- Many people have experience with F77
- procedural code
- no dynamic memory allocation
- no user-defined data structures
- Fewer have experience with
- user-defined structures
- parallel programming
- dynamic memory
- F90, F95 are less widely known
18Possible Plan for Use of Fortran
- Framework modules can be mostly Fortran, but
require some C - Fortran code will not be able to take advantage
of the full power of the C infrastructure - Fortran code will be required to follow many of
the same policies as C code
19Possible Plan ... continued
- Upper level of framework module must be C
- Extract data (objects) from the Event
- Fills in Fortran data structures (common blocks)
- Calls Fortran to do work
- reads from/writes to common blocks
- Wrap up again in C
- Reads from common blocks
- creates objects, puts then into the Event
20Possible plan ... continued
- Some limitations
- C packages communicate only through the Event
- Fortran from different trigger/reconstruction
packages communicate only through the Event - no common blocks between packages
- common blocks can be used to share data between
routines of the same package
21Assembly Language
- Assembly language drawbacks
- difficult to write
- difficult to maintain
- Assembly language benefits
- fast
- allows use of special features of chips
- Use it only when the advantages are needed
22Profile!
- Humans are notoriously poor at guessing where
bottlenecks lie - measure (profile) your code to identify the slow
parts - improve algorithms and data structures first
- implement limited assembly language where
profiling indicates the necessity
23Assembly guidelines
- Have a high-level (C, Fortran, C)
implementation first - if youve done profiling, you already have this
- use the high-level version to test correctness of
the assembly code - C includes mechanism for support of assembly
code - use your compilers support to make assembly code
easier to use and maintain
24Example of inline assembly code
typedef stdbitsetlt32gt flag_t inline flag_t
getCpuFeatures() flag_t result(0)_asm mov
eax, 1 cpuid mov result, edxreturn
result
25Use of the example
- bool hasStreamingSIMD() return
getCpuFeatures()25 -
- bool hasMMX() return getCpuFeatures()23
- Client code doesnt notice that getCpuFeatures(
) is implemented in assembly language
26Other Languages
- Java
- Can be a great advantage where a widely-used Java
library is available for some niche, e.g. JDBC
for database servers - Scripting languages
- Many are available and widely used
- Python, Ruby, Perl, Visual Basic, shells, ...
- Dont try to prevent personal use of personal
preferences - Choose an experiment standard only after needs
have been assessed.