Scientific Software Design The Object Oriented Way PDF
Scientific Software Design The Object Oriented Way PDF
Jim Xia
IBM Canada Lab in Markham
Xiaofeng Xu
General Motors Corp
CAMBRIDGE U NI VE RS I T Y PRE S S
For Zendo in exchange for the many books you have written for
me. For Leilee as a tangible product of every moment of support.
DAMIAN ROUSON
To my wife, Lucy, and two lovely children, Ginny and Alan, for
their encouragement and support.
JIM XIA
Contents
List of Figures
List of Tables
Preface
Acknowledgments
Disclaimer
page xi
xv
xvii
xxi
xxii
1.1
1.2
1.3
1.4
1.5
1.6
1.7
Introduction
Conventional Scientic Programming Costs
Conventional Programming Complexity
Alternative Programming Paradigms
How Performance Informs Design
How Design Informs Performance
Elements of Style
Nomenclature
Object-Oriented Analysis and Design
Encapsulation and Information Hiding
Wrapping Legacy Software
Composition, Aggregation, and Inheritance
Static and Dynamic Polymorphism
OOP Complexity
More on Style
3 Scientic OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1 Abstract Data Type Calculus
3.2 Analysis-Driven Design
3.2.1 Design Metrics
3.2.2 Complexity Theory
3.2.3 Information Theory
3
7
11
19
24
25
27
31
31
33
36
40
48
53
54
55
57
57
66
69
71
73
vii
viii
77
79
83
85
4.1 Essentials
4.2 Foundations
4.2.1 Building Architecture
4.2.2 Software Architecture
4.2.3 Scientic Software Architecture
4.3 Canonical Contexts
4.3.1 The Lorenz Equations: A Chaotic Dynamical System
4.3.2 Quantum Vortex Dynamics in a Superuid
4.3.3 Burgers Equation: Shock Formation and Dissipation
85
86
87
93
98
99
100
102
104
107
108
110
116
122
127
127
129
130
131
137
140
141
143
144
146
155
164
164
167
169
170
185
191
ix
199
201
202
203
205
216
226
227
229
Why Be Formal?
Problem Statement
Side Effects in Abstract Calculus
Formal Specication
10.4.1 Modeling Arrays with OCL
10.4.2 Hermeticity
10.4.3 Economy
10.5 The Shell Pattern
10.6 An Assertion Utility for Fortran
10.7 Case Study: A Fluid Turbulence Solver
231
233
237
238
238
239
242
244
245
247
251
254
259
261
269
273
278
281
285
286
292
295
297
309
317
318
320
325
326
330
335
335
337
337
342
343
343
344
345
345
346
347
347
350
357
359
359
362
366
367
368
368
370
370
371
371
371
373
379
Figures
1.1
1.2
1.3
1.4
1.5
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.10
2.11
2.12
2.13
2.14
2.15
3.1
3.2
3.3
3.4
3.5
3.6
4.1
page 4
8
14
17
18
33
34
35
37
40
42
43
44
46
47
48
49
50
51
52
61
64
66
67
68
72
95
xi
xii
4.2
4.3
4.4
Abstract class
Phase-space trajectory of the Lorenz equation system solution
Quantum vortex lament discretization and adaptive
remeshing
4.5 Quantum vortex laments before and after reconnection
4.6 Solutions of the Burgers equation with initial condition
u = 10 sin x and periodic boundary conditions
5.1 Memory leak example adapted from Stewart (2003)
5.2 OBJECT implementation: universal memory
management parent
5.3 Quantum vortex linked list with hermetic memory
management
5.4 Vortex creation and destruction
5.5 The concrete Vortex ADT implements the
Hermetic interface
5.6 Reference counting in C++
5.7 Output tools
5.8 Quantum vortex class denition
6.1 Lorenz system integration
6.2 Fortran implementation of ABSTRACT CALCULUS applied to a
Lorenz system
6.3 C++ implementation of an abstract calculus for the Lorenz
system
7.1 Fortran class model for applying the STRATEGY and
SURROGATE patterns to extend the Lorenz model
7.2 Fortran strategy pattern implementation for Lorenz
and timed Lorenz systems
7.3 C++ strategy implementation for Lorenz and timed Lorenz
systems
8.1 Associations in the Mediator pattern
8.2 Aggregation associations in the Puppeteer pattern
8.3 A UML sequence diagram for the Puppeteers Jacobian
contribution
8.4 Fortran implementation of an atmosphere puppeteer
8.5 Fortran 2003 implementation of puppets for an atmosphere
system
8.6 Fortran 2003 implementation of puppeteer pattern applied to
an atmosphere system
8.7 A mat_t class in C++ that stimulates the functionality of 2D
allocatable real arrays in Fortran
8.8 Atmosphere class denitions in C++
8.9 Puppet class denitions in C++
8.10 C++ implementation of puppeteer pattern applied to an
atmosphere system following the same design as that by
Fortran 2003
9.1 UML class diagram of an ABSTRACT FACTORY implementation
9.2 Fortran demonstration of an abstract factory
98
102
103
104
106
113
114
115
116
116
119
124
126
131
135
140
145
154
163
168
170
171
176
184
187
191
194
199
199
205
215
xiii
9.3
10.1
10.2
10.3
10.4
10.5
10.6
10.7
10.8
10.9
10.10
11.1
11.2
11.3
11.4
11.5
11.6
11.7
226
233
235
236
236
239
241
243
247
SURROGATE
274
249
250
253
255
260
266
266
272
277
278
284
290
295
297
298
309
311
316
317
318
320
321
323
323
xiv
324
324
326
327
328
329
330
332
336
339
341
342
343
344
348
353
358
360
362
364
365
365
366
367
369
370
372
Tables
2.1
3.1
4.1
10.1
11.1
11.2
OOP nomenclature
page 32
Translating blackboard abstractions into software abstractions 59
Design variables supported by patterns
96
Procedural run-time distribution
250
Corresponding C++ and Fortran 95 types
256
Fortran 2003 kind parameters and the corresponding
interoperable C types
263
11.3 Dummy array declaration interoperability
268
12.1 Runtime prole for central difference Burgers solver
procedures on an IBM smp system
290
B.1 Types of actions that are commonly used in an interaction
368
xv
Preface
This book is about software design. We use design here in the sense that it applies
to machines, electrical circuits, chemical plants, and buildings. At least two differences between designing scientic software and designing these other systems seem
apparent:
1. The extent to which one communicates a systems structure by representing
schematically its component parts and their interrelationships,
2. The extent to which such schematics can be analyzed to evaluate suitability and
prevent failures.
Schematic representations greatly simplify communications between developers.
Analyses of these schematics can potentially settle long-standing debates about which
systems will wear well over time as user requirements evolve and usage varies.
This book does not chiey concern high-performance computing. While most
current discussions of scientic programming focus on scalable performance, we
unabashedly set out along the nearly orthogonal axis of scalable design. We analyze
how the structure of a package determines its developmental complexity according
to such measures as bug search times and documentation information content. We
also present arguments for why these issues impact solution cost and time more than
does scalable performance.
We rmly believe that science is not a horse race. The greatest scientic impact
does not necessarily go to the swiftest code. Groundbreaking results often occur at the
intersection of multiple disciplines, where the basic science is so poorly understood
that important insights can be gleaned from creative simulations of modest size. In
multidisciplinary regimes, scaling up to hundreds of program units (e.g., procedures,
modules, components, or classes) matters at least as much as scaling up to hundreds
of execution units (e.g., processes, processors, threads, or cores). Put another way,
distributed development matters as much as distributed computing.
Nonetheless, recent trends suggest that exploiting even the most modest hardware including the laptops on which much of this book was written requires
parallelizing code across multiple execution units. After our journey down the path
toward scalable design, the nal chapter turns to the question of scalable execution,
or how to go from running on a single execution unit to running on many. In the
xvii
xviii
Preface
Preface
xix
Acknowledgments
This text is an outgrowth of research the authors have conducted with graduate
students, postdoctoral researchers, and various collaborators past and present. In
particular, Dr. Helgi Adalsteinsson contributed greatly to many of the C++ examples.
Drs. Karla Moris, Yi Xiong, Irene Moulitsas, Hari Radhakrishnan, Ioannis Sarris,
and Evangelos Akylas contributed to the research codes that inspired many of this
books Fortran examples.
Many examples draw from simulations we have performed in collaboration with
several domain experts in elds ranging from quantum physics to magnetohydrodynamics, re protection engineering, and atmospheric boundary layers. These
collaborators include Profs. Joel Koplik of the City University of New York, Andre
Marshall of the University of Maryland, Stavros Kassinos of the University of Cyprus,
and Robert Handler of the Texas A&M University.
Much of the work presented in this book was funded by the Ofce of Naval
Research (ONR) Automation in Ship Systems program under the direction of program manager Anthony Seman. The authors are also deeply indebted to Dr. Sameer
Shende of the University of Oregon for providing the development platform on
which the bulk of the examples in this text were written, and to Dr. Andrew McIlroy
of Sandia National Laboratories for approving the purchase of the platform on
which the remainder of the examples were developed. We thank Daniel Strong,
also of Sandia, for his artistic expression of this books theme in the cover art.
Sandia National Laboratories is a multi-program laboratory managed and operated
by Sandia Corporation, a wholly owned subsidiary of Lockheed Martin Corporation, for the U.S. Department of Energys National Nuclear Security Administration
under contract DE-AC04-94AL85000.
xxi
Disclaimer
The opinions expressed in this book are those of the authors and do not necessarily
represent those of International Business Machines Corporation or any of its
afliates.
xxii
PA R T I
THE TAO OF S C I EN T I F I C O O P
1.1 Introduction
The past several decades have witnessed impressive successes in the ability of
scientists and engineers to accurately simulate physical phenomena on computers. In
engineering, it would now be unimaginable to design complex devices such as aircraft
engines or skyscrapers without detailed numerical modeling playing an integral role.
In science, computation is now recognized as a third mode of inquiry, complementing theory and experiment. As the steady march of progress in individual spheres of
interest continues, the focus naturally turns toward leveraging efforts in previously
separate domains to advance ones own domain or in combining old disciplines into
new ones. Such work falls under the umbrella of multiphysics modeling.
Overcoming the physical, mathematical, and computational challenges of multiphysics modeling comprises one of the central challenges of 21st-century science
and engineering. In one of its three major ndings, the National Science Foundation
Blue Ribbon Panel on Simulation-Based Engineering Science (SBES) cited open
problems associated with multiscale and multi-physics modeling among a group of
formidable challenges [that] stand in the way of progress in SBES research. As
the juxtaposition of multiphysics and multiscale in the panels report implies,
multi-physics problems often involve dynamics across a broad range of lengths and
times.
At the level of the physics and mathematics, integrating the disparate dynamics
of multiple elds poses signicant challenges in simulation accuracy, consistency,
and stability. Modeling thermal radiation, combustion, and turbulence in compartment res, for example, requires capturing phenomena with length scales separated
by several orders of magnitude. The room-scale dynamics determine the issue of
paramount importance: safe paths for human egress. Determining such gross parameters while resolving neither the small length scales that control the chemical
kinetics nor the short time scales on which light propagates forms an active area of
research.
3
Research &
Development
Construction
Testing &
Tuning
At the level of the computation, the sheer size of multiscale, multiphysics simulations poses signicant challenges in resource utilization. Whereas the largest
length and time scales determine the spatial and temporal extent of the problem
domain, the shortest determine the required resolution. The ratio of the domain size
to the smallest resolvable features determines the memory requirements, whereas
the ratio of the time window to the shortest resolvable interval determines the computing time. In modeling-controlled nuclear fusion, for example, the time scales
that must be considered span more orders of magnitude than can be tracked simultaneously on any platform for the foreseeable future. Hence, much of the effort in
writing related programs goes into squeezing every bit of performance out of existing
hardware.
In between the physical/mathematical model development and the computation
lies the software development process. One can imagine the software progressing
through a life cycle much like any other product of human effort: starting with
research and development and ending with the testing and ne-tuning of the nal
product (see Figure 1.1). However, as soon as one searches for evidence of such
a life cycle in scientic computing, gaping holes appear. Research on the scientic software development process is rare. Numerous journals are devoted to novel
numerical models and the scientic insights obtained from such models, but only
a few journals focus on the software itself. Notable examples include the refereed
journals Scientic Programming and ACM Transactions on Mathematical Software.
Further along in the life cycle, the terrain becomes even more barren. Discussions
of scientic software design rarely extend beyond two categories: (1) explanations of
a particular programming paradigm, which fall into the implementation stage of the
life cycle, and (2) strategies for improving run-time efciency, which arguably impacts
all stages of the life cycle but comprises an isolated activity only once a prototype has
been constructed in the testing and tuning phase. Articles and texts on the rst topic
include presentations of structured and object-oriented programming, whereas those
on the second include discussions of parallel programming and high-performance
computing.
Implementation-independent design issues such as the chosen modular decomposition and its developmental complexity have received scant attention in the
scientic computing community. Not surprisingly then, quantitative analyses of
software designs have been limited to run-time metrics such as speed and parallel efciency. In presenting attempts to analyze static source code organization, the
author often encounters a perception that program structure is purely stylistic of
1.1 Introduction
the You say toe-may-toe; I say toe-mah-toe variety. Even to the extent it is recognized that program structure matters, no consensus is emerging on what structural
precepts prove best.
That scientic program design warrants greater attention has been noted at
high levels. The 1999 Presidential Information Technology Advisory Committee
(PITAC) summarized the situation for developers of multidisciplinary scientic
software:
Today it is altogether too difcult to develop computational science software and applications. Environments and toolkits are inadequate to meet the needs of software developers
in addressing increasingly complex interdisciplinary problems... In addition, since there
is no consistency in software engineering best practices, many of the new applications are
not robust and cannot easily be ported to new hardware.
Fortunately, the situation with regards to environments and toolkits has improved
since the PITAC assessments. Using tools such as Common Component Architecture
(CCA), for example, one can now link applications written by different developers in
different languages using different programming paradigms into a seamless, scalable
package without incurring the language interoperability and communication latency
issues associated with non-scientic toolkits that facilitate similar linkages. Several
groups have constructed development frameworks on top of CCA to facilitate distributed computing (Zhang et al. 2004), metacomputing (Malawski et al. 2005), and
other computing models.
However, the situation with regard to software engineering best practices has
seen far less progress. Most authors addressing scientic programming offer brief
opinions to aid robustness before retreating to the comfortable territory of run-time
efciency or numerical accuracy. Without a healthy debate in the literature, it is
unsurprising that no consensus has emerged. By contrast, in the broader software
engineering community, consensus has been building for over a decade on the best
practices at least within one development paradigm: object-oriented design (OOD).
In this context, the best practices have been codied as design patterns, which are
cataloged solutions to problems that recur across many projects.
Experience indicates many scientic programmers nd the term design pattern
vague or awkward on rst hearing. However, its application to software development conforms with the rst four denitions of pattern in the Merriam Webster
dictionary:
1.
2.
3.
4.
Having proved useful in the past, design patterns are offered as models to be followed
in future work. The models have artistic value in the sense of being elegant and in
the sense that software engineering, like other engineering elds, is part science and
part art. And much like mechanical design patterns, some software design patterns
mimic patterns that have evolved by chance in nature.
Gamma et al. (1995) rst collected and articulated object-oriented software
design patterns in 1995 in their seminal text Design Patterns: Elements of Reusable
Object-Oriented Software. They drew inspiration from the 1977 text, A Pattern Language, by architects Alexander et al., who in turn found inspiration in the beauty
medieval architects achieved by conforming to local regulations that required specic building features without mandating specic implementations of those features.
This freedom to compose variations on a theme facilitated individual expression
within the connes of proven forms.
Gamma et al. did not present any domain-specic patterns, although their books
introduction suggests it would be worthwhile for someone to catalog such patterns.
Into this gap we thrust the current text. Whereas Part I of this text lays the objectoriented foundation for discussing design patterns and Part III discusses several
related advanced topics, Part II aims to:
1. Catalog general and domain-specic patterns for multiphysics modeling,
2. Quantify how these patterns reduce complexity,
3. Present Fortran 2003 and C++ implementations of each pattern discussed.
Each of these objectives makes a unique contribution to the scientic computing eld. The authors know of only a handful of publications on
object-oriented software design patterns for scientic software (Blilie 2002;
Decyk and Gardner 2006; Decyk and Gardner 2007; Gardner and Manduchi 2007;
Markus 2006; Rouson et al. 2010) and even fewer quantitative analyses of how patterns reduce scientic programming complexity (Allan et al. 2008; Rouson 2008).
Finally, published design pattern examples that take full advantage of the new objectoriented constructs of Fortran 2003 have only just begun to appear (Markus 2008;
Rouson et al. 2010).
That Fortran implementations of object-oriented design patterns have lagged
those in other languages so greatly is both ironic and understandable. It is ironic
because object-oriented programming started its life nearly four decades ago with
Simula 67, a language designed for physical system simulation, which is the primary
use of Fortran. It is understandable because the Fortran standards committee, under
heavy inuence by compiler vendors, moved at glacial speeds to provide explicit support for object-orientation. (See Metcalf et al. 2004 for some history on this decision.)
In the absence of object-oriented language constructs, a small cadre of researchers
began developing techniques for emulating object-orientation in Fortran 90/95 in the
1990s (Decyk et al. 1997a, 1997b, 1998; Machiels and Deville 1997), culminating in
the publication of the rst text on OOP in Fortran 90/95 by Akin (2003).
The contemporaneous publication of the Fortran 2003 standard, which provides
object-oriented language constructs, makes the time ripe for moving beyond the
basic mechanics to higher-level discussions of objected-oriented design patterns for
scientic computing. Because the availability of Fortran 2003 motivates this book,
Fortran serves as the primary language for Part I of this text. Because most C++
programmers already know OOP, there is less need to provide C++ in this part
of the text. Some of the complexity arguments in Part I, however, are languageindependent, so we provide C++ translations to make this section accessible to C++
programmers. In Part II, greater effort is made to write C++ examples that stand on
their own as opposed to being translations of Fortran.
Reality
Physical
Model
Accuracy
Mathematical
Model
(continuous)
Numerical
Model
(semidiscrete)
Numerical
Model
(fully discrete)
Code
(discrete space-time
physics)
Visualization
Statistical
Analysis
Perception
Figure 1.2. Sequence of models in the conventional scientic code development process.
implementation phase differs considerably from the rest of the curriculum, wherein
students are encouraged to think abstractly about systems and to quantitatively evaluate competing models for describing those systems. In the automatic controls courses
taken by many engineering students, for example, model elements are delineated
precisely and their roles and relationships are specied mathematically, so the system
behavior can be analyzed rigorously.
Figure 1.2 situates programming within the conventional modeling process. One
starts off wishing to model physical reality. Based on observations of that reality,
one constructs a physical model, which for present purposes comprises a conceptual abstraction of reality. Both model and abstraction here connote useful
simplications that retain only those elements necessary for a given context. For
example, one might abstract a cooling n on a microprocessor chip as a thin solid
with constant properties transporting thermal energy in the direction orthogonal to
the chip. One might further assume the energy transfer occurs across a distance that
is large relative to the materials intermolecular spacing, so it can be considered a
continuum.
Although physical models are typically stated informally, Collins (2004) recently
proposed the development of a physics markup language (PML) based on the extensible markup language (XML). Formal specications in such a markup language
would complement the programming techniques presented in this book. Specically,
automatic code generation from a markup document might produce software counterparts of algebraic and differential operators specied in PML. Chapter 3 discusses
the implementation of such operators.
The rst formal step in conventional scientic work involves expressing the physical model in a mathematical model with continuous variation of the dependent
variables in space and time. For the n problem, this leads to the one-dimensional
(1D), unsteady heat equation:
T
2T
= 2 , T = (0, Ln ) (0, tnal )
t
x
T(0, t) = Tchip
(1.1)
T(Ln , t) = Tair
T(x, 0) = Tair
where T(x, t) is the temperature at time t a distance x from the chip, is the ns
thermal diffusivity, T is the space-time domain, Ln is the n length, tnal is the
nal time of the simulation, and where Tchip and Tair are boundary conditions.
Solving the mathematical model on a computer requires rendering
equations (1.1) discrete. Most numerical schemes employ the semidiscrete method,
which involves discretizing space rst and time second. Most spatial discretizations
require laying a grid over the spatial domain. Given a uniformly spaced grid overlaid
on , applying the central difference formula to the right-hand side of (1.1) leads to
a set of coupled ordinary differential equations for the nodal temperatures:
1
dT
dt
x2
1
2
..
.
Tchip
T +
.
2
x
..
2
Tair
1
..
.
1
(1.2)
(1.3)
10
expression of this algorithm in software could force major code revisions if one later
decides to change the discretization scheme, for example, the basis functions or the
PDE itself. The next section explains why the conventional approach to separating
these issues scales poorly as the code size grows.
A side effect of conating logically separate modeling steps is that it becomes
difcult to assign responsibility for erroneous results. For example, instabilities that
exist in the fully discrete numerical model could conceivably have existed in continuous mathematical model. Rouson et al. (2008b) provided a more subtle example in
which information that appeared to have been lost during the discretization process
was actually missing from the original PDE.
As previously mentioned, the code writing is the rst step with no notion of
abstraction. Except for ow charts which, experience indicates, very few programmers construct in practice most discussions of scientic programs offer no
formal description of program architecture other than the code itself. Not surprisingly then, scientic program architecture is largely ad hoc and individualistic. Were
the problems being solved equally individualistic, no difculty would arise. However, signicant commonalities exist in the physical, continuous mathematical, and
discrete numerical models employed across a broad range of applications. Without
a language for expressing common architectural design patterns, there can be little
dialogue on their merits. Chapter 2 employs just such a language: the Unied Modeling Language (UML). Part II presents design patterns that exploit commonalities
among a broad range of problems to generate exible, reusable program modules.
Lacking such capabilities, conventional development involves the reinvention of
architectural forms from scratch.
How does redevelopment inuence costs? Lets think rst in the time domain:
Total solution time = development time + computing time
(1.4)
Until computers develop the clairvoyance to run code before it is written, programming will always precede execution, irrespective of the fact that earlier versions of
the program might be running while newer versions are under development. Since
the development time often exceeds the computing time, the fraction of the total
solution time that can be reduced by tuning the code is limited. This is a special case
of Amdahls law, which we revisit in Section 1.5 and Chapter 12.
Time is money, so equation (1.4) can be readily transformed into monetary
form by assuming the development and computing costs are proportional to the
development and computing times as follows:
$solution = $development + $computing
= Ndev pavg tdev +
$computer trun
Nusers tuseful
(1.5)
where the $ values are costs; Ndev , pavg , and tdev are the number of developers, their
average pay rate, and the development time, respectively; and Nusers , trun , and tuseful
are the number of computer users, the computing time, and the computers useful
life, respectively. In the last term of equation (1.5), the rst factor estimates the
fraction of the computers resources available for a given users project. The second
factor estimates the fraction of the computers useful life dedicated to that users
runs.
There are of course many ways equation (1.5) could be rened, but it sufces
for current purposes. For a conservative estimate, consider the case of a graduate
student researcher receiving a total compensation including tuition, fees, stipend,
and benets of $50,000/yr for a one-year project. Even if this student is the sole
developer of her code and has access to a $150,000 computer cluster with a useful
life of three years, she typically shares the computer with, say, four other users.
Therefore, even if her simulations run for half a year on the wall clock, the solution
cost breakdown is
$solution = (1 developer)($50, 000/year)(1 year)
(1.6)
(1.7)
(1.8)
so the development costs greatly exceed the computing costs. The fraction of costs
attributable to computing decreases even further if the developer receives higher
pay or works for a longer period likewise if the computer costs less or has a longer
useful life. By contrast, if the number of users decreases, the run-time on the wall
clock decreases proportionately, so the cost breakdown is relatively insensitive to
the resource allocation. Similar, the breakdown is insensitive to adding developers
if the development time is inversely proportional to the team size (up to some point
of diminishing returns).
For commercial codes, the situation is a bit different but the bottom line is
arguably the same. The use and development of commercial codes occur in parallel, so the process speedup argument breaks down. Nonetheless, development time
savings lead to the faster release of new features, providing customer benet and a
competitive advantage for the company.
This forms the central thesis on which this book rests:
Your Time Is Worth More Than Your Computers Time
Total solution time and cost can be reduced in greater proportion by reducing
development time and costs than by reducing computing time and costs.
11
12
Ln
N
3
(1.10)
Furthermore, the maximum number of points is set by the available memory, M, and
the chosen precision, p, according to:
Nmax =
M
p
(1.11)
where typical units for M and p would be bytes and bytes per nodal value, respectively. Substituting (1.11) into (1.10) leads to the lowest possible upper bound on the
error:
pLn 3
(1.12)
sup e C
M
Whether a given program actually achieves this level of accuracy is determined by
its dynamic memory utilization. Consider two ways of computing the right-hand side
(RHS) of equation (1.3):
n+1 = T
n + t AT
n+b
T
(1.13)
n+b
= (I + tA) T
(1.14)
where I is the identity matrix. Working outward from inside the parenthesis, evalun ,
n intact while computing AT
ating the RHS in equation (1.13) requires keeping T
n
n
so a time exists when three vectors are stored in memory (T , AT , and b). Adding
this storage requirement to the additional xed storage of A, t, and other values reduces the achievable N below the maximum allowed by equation (1.11) and thereby
increases the error in (1.12).
n with the result
By contrast, evaluating equation (1.14) allows for overwriting T
n
of the product (I + tA) T , so at most two vectors need be stored at any given time.
This facilitates increasing N closer to Nmax , thereby decreasing the error. Thus,
to the extent complexity arguments lead to a strategy where memory management
plays a critical role, programming complexity plays a critical and quantiable role
in the accuracy of the results. Two programs that are algorithmically equivalent in
terms of the numerical methods employed might have different levels of error if one
program requires signicantly more dynamic memory. Chapter 12 presents much
more severe examples of this principle in which the solutions are three-dimensional
(3D) and therefore place much greater demands on the available memory.
The other way that complexity affects accuracy is by increasing the likelihood
of bugs. A statistical uncertainty argument could be constructed to quantify the inuence of bugs on accuracy, but it seems more useful to return to the theme of the
previous section by asking, How can we quantify complexity in ways that reduce
development time? Several approaches of varying sophistication are employed
throughout this book. For present purposes, lets break the development time into
the four stages:
1.
2.
3.
4.
Because this book focuses on the design stage, one wonders how program design
inuences the debugging process. Such considerations could easily lead us down a
path toward analyzing the psychology and sociology of programmers and groups of
programmers. These paths have been trod by other authors, but we instead turn to
simple truisms about which we can reason quantitatively.
In their text Design Patterns Explained, Shalloway and Trott (2002) write:
The overwhelming amount of time spent in maintenance and debugging is on nding bugs
and taking the time to avoid unwanted side effects. The actual x is relatively short!
Likewise, Oliveira and Stewart (2006), in their text Writing Scientic Software: A
Guide to Style, write: The hardest part of debugging is nding where the bug is.
Accepting these arguments, one expects debugging time to increase monotonically
with the number of program lines, , one must read to nd a given bug:
tdebugging = f (), f (1 ) > f (2 ) if 1 > 2
(1.15)
in which case reducing the number of lines one must read reduces debugging time.
This relationship argues not so much for reducing the absolute number of program
lines but for reducing the dependencies between lines. Bugs typically present themselves as erroneous output, whether from the program itself or the operating system.
Exterminating bugs requires tracing backward from the line where the symptom
occurs through the lines on which the offending line depends.
To understand how dependencies arise, it helps to examine real code. Since this
book focuses on design, we rst construct the only design document one typically
encounters for conventional scientic code. Figure 1.3 depicts a ow chart that uses
the cooling n numerical model from section 1.2 to estimate the time required for
the n to reach steady state. At long times, one expects the solution to reach a steady
state characterized by T/t = 0 = 2 T/x2 , so the temperature varies linearly in
space but ceases changing in time. The algorithm in Figure 1.3 senses this condition
by checking whether the second derivative of the n temperature adjacent to the
chip drops below some tolerance.
Additional tertiary dependencies might be generated if any procedures listed
previously call other procedures and so forth. Dissecting the temperature calculation
according to the values it uses makes clear that shared data mediates the interactions
between program lines.
Figure 1.4 provides a sample Fortran implementation of the algorithm described
in Figure 1.3. Section 1.7 discusses several stylistic points regarding the sample code.
In the current section, the emphasis remains on dependencies between lines. For
discussion purposes, imagine that during the course of solution, program line 29 in
main (Figure 1.4(c)) prints temperatures outside the interval (Tair , Tchip ), which is
13
14
Start
Yes
t < tstable?
No
t = tstable
Calculate 2T/x2
T=T + (t)2T/x2
Figure 1.3. Flow chart for a 1D, transient heat transfer analysis of a cooling n.
known to be physically impossible. Dissecting line 28 in main from left to right, one
nds the temperature depends directly on the following:
1. The time step read in read_numerics() at line 18 in main,
2. The second derivative T_xx calculated by differentiate() at line 27 of main,
and
3. The diffusivity initialized on line 16 of main and possibly overwritten on line 24
of main.
Furthermore, dissecting line 27 of main from left to right generates the following
secondary dependencies of T on:
4. The laplacian differencing matrix created at line 22 of main,
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Figure 1.4(a)
module kind_parameters
! Type kind parameter
implicit none
private
public :: rkind,ikind,ckind
integer ,parameter :: digits=8 ! num. digits of kind
integer ,parameter :: decades=9 ! num. representable decades
integer ,parameter :: rkind = selected_real_kind(digits)
integer ,parameter :: ikind = selected_int_kind(decades)
integer ,parameter :: ckind = selected_char_kind(default)
end module
Figure 1.4(b)
module conduction_module
use kind_parameters ,only : rkind,ikind
implicit none
! Tri-diagonal array positions:
integer(ikind) ,parameter::diagonals=3,low_diag=1,diag=2,up_diag=3
contains
pure logical function found(namelist_io)
integer(ikind) ,intent(in) ::namelist_io
if (namelist_io==0) then
found=.true.
else
found=.false.
end if
end function
15
16
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1
2
3
4
5
6
end function
end module
+finiteDiff(nodes,
diag)*T(nodes
+finiteDiff(nodes, up_diag)*Tlast
) &
Figure 1.4(c)
program main
use iso_fortran_env ,only : input_unit
use kind_parameters ,only : rkind,ikind
use conduction_module
implicit none
real(rkind)
,parameter :: tolerance=1.0E-06
integer(ikind) ,parameter :: chip_end=1
integer(ikind)
:: elements=4,intervals
real(rkind) :: air_temp=1.,chip_temp=1. & !boundary temperatures
,diffusivity=1.,fin_length=1.,time=0. & !physical parameters
,dt=0.01,dx
!time step, grid spacing
real(rkind),dimension(:) ,allocatable:: T,T_xx !temperature,2nd deriv
real(rkind),dimension(:,:),allocatable:: laplacian !differential op
dx=fin_length/elements
!default element size
if (.not. &
read_physics(input_unit,diffusivity,fin_length,chip_temp,air_temp))&
print *,In main: Using default physics specification.
if (.not. read_numerics(input_unit,dt,elements,dx,fin_length)) &
print *,In main: Using default numerical specification.
allocate(T(elements),laplacian(elements,diagonals),T_xx(elements))
T
= air_temp
laplacian = differencing_stencil(dx,elements)
print *,time,temperature=,time,T
dt = min(dt,stable_dt(dx,diffusivity))
do
time = time + dt
T_xx = differentiate(laplacian,T,chip_temp,air_temp)
T = T + dt*diffusivity*T_xx
print *,time,temperature=,time,T
if (T_xx(chip_end)<tolerance) exit
end do
print *,steady state reached at time ,time
end program
Figure 1.4(d)
&physics
alpha=1.,L_fin=1.,T_chip=1.,T_air=1.
/
&numerics
dt=0.01,nodes=9
/
Figure 1.4. Fin heat conduction program: (a) global constants, (b) solver, (c) driver, and (d)
input le.
17
18
T(0),T(1),...,T(10)
Legend
Read
Write
Figure 1.5. A subset of the data dependencies for a temperature calculation without argument
intent. Without intent, the entire differentiate() procedure must be reviewed to determine
whether the procedure writes to T.
the program lines executed before the symptom occurs. Assume redundant occurrences are removed while ensuring that all lines that execute before the symptom
occurs are listed prior to listing the symptomatic line. Some of the lines preceding
the symptomatic line in such a listing might appear after the symptomatic line in the
source code. For example, if the symptom occurs early in the second pass through a
loop, all lines executed during the rst pass through the loop would be listed prior
to the symptomatic line.
The aforementioned assumptions are intended to avoid inating above the
total number of lines in the code and to ensure that causality connes the bug to the
lines preceding the symptom on the list. Now if a given symptom is equally likely to
occur anywhere on the list, then its expected location is /2 lines from the top, so
the bug must be in the preceding /2 1 lines.
If the bug is equally likely to be found on any of these lines, the expected value
of the number of lines that must be searched backward to nd it is (/2 1)/2. For
code with r bugs, the search algorithm just described has an expected completion
time of:
r
(1.16)
1 tline
tnd =
2 2
where tline is the average time to review one line. We dene review to include
reading it, testing it, and thinking about it over coffee. Since we do not include
the time to revise it, which might also include revising other lines, equation (1.16)
provides a lower bound on the debugging time, which provides a lower bound on the
development time. Development time thus grows at least quadratically with code size.
To estimate r, consider that Hatton (1997) found an average of 8 statically
detectable faults per 1,000 lines among millions of lines of commercially released
scientic C code and 12 per 1,000 in Fortran 77 code. Separately, he apparently
estimated that approximately two to three times as many errors occur in C++1 . He
1 Van Snyder reported Hattons C++ results in a smartly and humorously written response to a petition
dened a fault as a misuse of the language which will very likely fail in some context. Examples include interface inconsistencies and using un-initialized variables.
Hattons average fault rate yields the estimate r 0.01.
Equation (1.16) provides a polynomial time estimate for completing the debugging process. Even though polynomial time estimates fall under the purview of computational complexity theory, one must be careful to distinguish the complexity of the
debugging process from the complexity of the code itself. Attempts to quantify code
complexity in useful ways particularly to predict fault rates have a checkered past.
Exacerbating the problem is the reliance of most programs on externally dened
libraries with which data must be shared. Truly pernicious bugs (which the vendors
might call features) result when code not written by the programmer modies
its arguments unexpectedly. Although such dependencies may seem inevitable,
Chapter 6 explains how to construct a library that never gets its grubby hands on
the data on which it operates.
As individual programs merge into complete software packages, Figure 1.5
becomes a plate of spaghetti, providing one interpretation of the infamous term
spaghetti code. The next section explores several philosophies for disentangling
the knots in spaghetti code. The most popular modern alternative, OOP, reduces
data dependencies by constructing modules with local data and allowing access to
that data only to procedures inside the associated module. Chapter 2 renes the
estimate in equation (1.16) by analyzing a more representative fault localization
algorithm and demonstrates how OOP reduces the search time. Chapter 3 demonstrates how an OOP strategy that is broadly useful in scientic programming further
limits search times.
The benets of the above modularization strategy are manifold, but the
strategy does have limits. Although strong evidence suggests defect rates are
roughly independent of module size as assumed implicitly in equation (1.16)
(Fenton and Ohlsson 2000), the same study provided evidence that defect rates
sometimes spike upward for the smallest modules. As module granularity increases,
one must explore additional avenues for reducing dependencies. The next section
surveys a range of possibilities.
Ravioli Tastes Better Than Spaghetti Code
Spaghetti code results partly from spaghetti dependencies. One way to reduce the
dependencies between program lines is to reorganize the program by partitioning
the data into private, local pockets. To the extent that managing the memory
in these pockets determines the maximum resolution, a programs organization
determines its accuracy.
19
20
capture a few choice snapshots, to take in the sights of most interest, to discern the
basic building blocks, to overhear a bit of the mindset expressed in conversation, and
to return home changed forever. Although the following paragraphs read at rst like
a timeline, most of these communities remain vibrant in certain corners, with good
reason and bad.
In the beginning, there was unstructured programming. Early machine language
programming employed individual lines as the basic unit of decomposition. Although
programmers were free to think in terms of higher levels of organization, no language support for such basic forms as loops existed. With the advent of Fortran as
the rst high-level programming came looping constructs (do loops) and conditional
execution blocks (if constructs).
At this level of program organization, the only commonly employed program
abstraction is the ow chart. With the resultant emphasis on program ow rather than
data, the rst few evolutionary steps beyond unstructured code involved new ways to
express and decompose algorithms. The 1958 version of Fortran, for example, added
the capability to dene a subroutine or function. The procedures constructed
therewith became the basic units of decomposition in procedural programming.
Whereas some paradigm shifts expand the developers toolset, others prune
away its rougher edges. Those aspects of structured programming and functional
programming considered here involve pruning. Given the natural emphasis on a
programs logical ow that stems from staying within the procedural paradigm, most
structured programming methods emphasize the avoidance of arbitrarily employing
discontinuous jumps from one region of code to another. Because the unconditional
goto statement offers the most severe and abrupt such jump, the goto has been
frequently maligned by structured programming adherents. One of the founders of
the structured programming movement, Dijkstra (1968), summarized some of the
basic reasons in an article entitled GOTO statement considered harmful.
Knuth (1974) pointed out that the most harmful uses of goto were already beginning to be subsumed by newer, more specic constructs, a prime example being
error handling, where it is desired to exit some branch in a programs call tree to dispatch a distant portion of code intended to deal gracefully with an aberrant situation
such as reaching the end of an input le. Knuth attempted to redirect the structured
programming dialogue toward more fundamental questions of composing programs
from collections of entities about which formal correctness proofs can be made. He
also provided a simple rule for avoiding complicated ow: It should be possible to
draw all forward jumps in a ow chart on one side and all backward jumps on the
other side with no jumps crossing each other. Most references to spaghetti code are
to programs that violate these principles. Nonetheless, as late as 1987, the debate on
the goto statement raged on, with Rubin (1987) writing an article entitled GOTO
considered harmful considered harmful that garnered multiple responding letters
to the editor, including one from a disappointed Disktra.
In addition to eschewing goto statements in this books source code, a recent
incarnation of structured programming is adopted to facilitate splitting tasks across a
number of hardware units. The Open Multi-Processing (OpenMP2 ) toolsets for parallelizing code operate on so-called structured blocks of code. The OpenMP standard
2 http://www.openmp.org
denes a structured block as a contiguous set of statements with one point of entry
and one point of exit.
Lest we pat ourselves on the back for advancing so far from the Stone Age, a
resurgence of unstructured programming came with the advent of interactive mathematical packages. This trend has many benets. For relatively simple tasks and even
for some more complicated tasks with regularly spaced data and modest operation
counts, the benets of packages such as Matlab and Mathcad far outweigh their disadvantages. The ability to leverage hundreds of programmer-years with the press of
a button, to integrate visualization into calculations, and especially to work with units
that are automatically converted when necessary in Mathcad outweighs the difculty
of life without subroutines and functions for small code segments of a 1020 lines.
Of the approaches discussed in this section, functional programming represents the
rst that deals with the data dependencies discussed in the previous section. Functional programming emphasizes the specication of relationships between entities,
rather than specifying steps that must be done. Mathematica is the most commonly
employed functional programming language/environment with which most scientic
programmers would be familiar.
The subset of functional programming that most inuences this book is purely
functional programming, in which side effects, including modifying procedure
arguments, are completely ruled out. While we nd it impractical to rule out side
effects completely, partly for reasons explained in Chapter 3, minimizing side effects
amounts to limiting data access. Limiting data access reduces the linkages in diagrams
like Figure 1.5, which has an important impact on the debugging time estimate of
equation (1.16). We discuss this impact in Chapter 3.
Object-based programming further limits data access by encouraging encapsulation and information hiding. Although Chapter 3 provides more complete
explanations, here we dene encapsulation as the act of partitioning data and procedures for operating on that data into modules. The data referenced here is inside
a derived type of the form
type fluid
real :: viscosity
real, dimension(:,:,:), allocatable :: velocity
character, allocatable :: chemical_formula
end type
With the object-based approach, one puts procedures into a given module only if
they operate on instances (objects) of the type dened in that module or provide
services to a procedure that does. Furthermore, one generally lets only those module
procedures, typically termed methods in OOP discussions, operate on instances of the
modules data type. (Chapter 3 details the construction of methods via Fortran 2003
type-bound procedures.) The latter relationship represents data privacy, which is one
form of information hiding. Another form involves keeping some of the procedures
in the module inaccessible to code outside the module. Each of these strategies limits
data access and thereby helps reduce the debugging search time. Encapsulation and
information hiding are so closely linked that they are often considered one concept.
That concept is the rst of the three main pillars of OOP.
21
22
The second pillar, polymorphism, has Greek roots meaning many faces. One
category of polymorphism takes the form of a one-to-many mapping from procedure
names to procedure implementations. Here we dene an interface as the information
summarized in a Fortran interface body: a procedures name, its argument types,
and its return type, if any. Although polymorphic variables did not enter the language until Fortran 2003, Fortran programmers had already encountered one form
of polymorphism when using intrinsic functions. For example, in writing the code:
real :: x=1.,sin_x
real ,dimension(10,10) :: a=0.,sin_a
sin_x = sin(x)
sin_a = sin(a)
where the function sin can accept a scalar argument or an array arguments and
correspondingly return either a scalar or an array. The procedure that responds to
each of the above calls must be somewhat different even though the call retains the
same form. Chapter 3 explains other forms of polymorphism and how the programmer can construct her own polymorphic procedures. Chapter 3 also analyzes the
inuence polymorphism has on programming complexity.
One form of polymorphism is so important that it is typically given its own
name: operator overloading. Again it is closely related to something Fortran 90
programmers have already encountered in writing expressions like:
real
:: x=1.,y=2.,z
real ,dimension(10,10) :: a=0.,b=1.,c
z = x + y
c = a + b
where the + operator apparently has different meanings in different contexts: the
rst being to add two scalars and the second being to perform element-wise addition
on two matrices. Operator overloading is when the programmer species how such
operators can be applied to derived types created by the programmer. Chapter 3
explains how operator overloading plays such a critical role in reducing programming
complexity that it could be considered the fourth pillar of scientic OOP (SOOP).
Object-oriented programming is object-based programming plus inheritance, the
third pillar of OOP.3 Inheritance increases code reuse by facilitating subtypes, to be
dened based on existing types. In Fortran 2003, subtypes are termed extended types
and take the form:
type ,extends(fluid) :: magnetofluid
real ,dimension(:,:,:) ,allocatable :: magnetic_field
end type magnetofluid
which species that a magnetofluid is a fluid with an associated magnetic eld.
Every instance of a magnetofluid thus inherits a viscosity, velocity, and a chemical
3 Perhaps the best denition is that object-based programming is OOP minus whatever one considers
to be the most critical feature of OOP. For example, another interpretation is that object-based
programming is OOP minus polymorphism (Satir and Brown 1995).
formula from the fluid type that magnetofluid extends. Equally important, languages that support OOP also provide facilities for the module implementing the
magnetouid to inherit various methods from the Fluid module. By analogy with
biological inheritance, the extended type is often referred to as the child, whereas
the type it extends is termed the parent.
Component-based software engineering (CBSE) can be viewed as the next evolutionary step beyond OOP. Where OOP informs how we construct and connect
individual objects, CBSE works with components that can be comprised of collections of objects along with code not necessarily written in an object-oriented fashion.
Most of the CBSE toolsets developed for mainstream software engineering have
severe inefciencies when applied to scientic problems. Where distributing applications across the Internet is increasingly the norm in nonscientic work, parallelizing
applications across a set of closely linked computational nodes has become the norm
in scientic work. The communication patterns and needs differ considerably in these
two domains.
Fortunately, for high-performance scientic programming, the CCA facilitates
the management of interprocedural communication at run-time among programs
that might have been written in disparate languages employing equally disparate programming paradigms (Bernholdt et al. 2006). A concept closely linked with CBSE
is design by contract, which expresses the notion that software clients write specications with which software designers must comply. In fact, one denition of a
component is an object with a specication. Chapter 11 discusses a toolset for writing specications and how the specication process inuences programming in ways
that increase code robustness.
The generic programming paradigm enhances programmers ability to write
reusable algorithms. It is often referred to as programming with concepts, wherein
procedures or algorithms are expressed with a high level of abstraction in the applicable data types. When sorting a set of objects, for example, whether they are of
numeric type or the previously dened fluid type, the well-established quick-sort
algorithm can be applied, providing a well-dened comparison method between any
two objects. Appropriate use of this paradigm leads to highly reusable components,
thus rendering simpler programs.
A number of modern programming languages explicitly support generic programming. Templates in C++, such as the container classes in the Standard Template
Library (STL), are among the most commonly used generic constructs. Templates
allow one to write code in which the type being manipulated can remain unspecied
in the expression of the algorithm. Even though Fortran lacks templates, one can
emulate the simpler template capabilities in Fortran. Part II discusses some templateemulation techniques. Furthermore, Fortran supports its own version of generic programming via parameterized derived types that allow type parameters to be generic
in source. Type parameters specify attributes used in dening the derived type as in:
type vector(precision,length)
real(precision) ,dimension(length) :: v
end type
where precision and length are type parameters that can be used in a subsequent
declaration to specify the kind and dimension of v.
23
24
(1.17)
where the numerator and denominator are respectively the original completion time
and the minimum time based on completely eliminating 80% of the process. This
calculation suggests that when modest speedup is acceptable, optimization efforts
should be focused on the code that occupies most of the run time. The search for
those parts leads naturally to the following rule of thumb:
Pareto Principle: For many systems in which participants share resources, roughly 20%
of the participants occupy 80% of the resources.
Specically, experience indicates that for many codes, approximately 20% of the lines
occupy 80% of the execution time. Though this situation might seem purely fortuitous, some form of Pareto rule always holds. In any system where participants (lines)
share resources (execution time), there will always be a number k such that k% of
the participates occupy 1 k% of the resources so long as the number of participants
is sufciently large to admit a fraction equal to the chosen percentage. Two limiting
cases are equal sharing (k = 50%) and monopoly by one line (k 100%). Hence,
the Pareto Principle suggests most systems are skewed slightly towards monopoly.
This very likely stems from the fact that, considering all possible resource allocations
for N participants, there is only one that corresponds to an equitable distribution,
whereas there are N possible monopolies.
Considering Amdahls law vis--vis Paretos rule leads to the design principle that
guides this entire book: structure 80% of the code to reduce development time rather
than run time. Focus run-time reduction efforts on the 20% of the code that occupies
the largest share of the run time. Ultimately, this means design must be as analytical
and empirical as the numerics and physics. Development time estimates must guide
the design. Solution time estimates must guide the algorithm choice. Proles of the
run-time share occupied by each procedure must guide the optimization efforts.
Rouson et al. (2008b) discussed a common case that is even far more extreme than
Paretos Principle: 79% of the run time is occupied by less than 1% of the code.
The reason is that this short code segment calls an external library that does the
heavy lifting. With increasing development of high-performance scientic libraries,
this situation can be expected to become the norm.
The percentage of the code that must be highly optimized increases with the
desired amount of speedup. When the optimization focuses on parallelization, the
process of scaling up from running on a single execution unit to running on many
thousands eventually takes the search for optimization into the darkest crevices of the
code. If one starts from code that is already parallel, however, the Pareto-inspired
rule-of-thumb holds: Targeting a vefold increase in speed is as reasonable when
going from 1 to 5 execution units as when going from 10,000 to 50,000.
May the Source Be With You
Write 80% of your source code with yourself and your fellow programmers in
mind, that is, to make your life easier, not your computers life easier. The CPU
will never read your source code without an interpreter or compiler anyway.
Arcane or quirky constructs and names written to impress on the CPU the urgency
or seriousness of your task might well get lost in the translation.
tively.
25
26
opportunities for parallel execution. Since current trends suggest that data distribution costs will dominate computing costs at the exascale, the chosen constructs
must minimize the data distribution needs. A signicant driver for data distribution is the need to synchronize memory when one execution unit modies data that
another unit needs. Over the past decade, the computer science community has
issued many calls for functional programming as a solution that limits such communication: Side-effect-free procedures do not modify state that other procedures
need.
Any strategy that promises exaops must allow for a rapidly evolving landscape
of hardware, programming languages, and parallel programming models. Hardware
is likely to become heterogeneous with exotic combinations of processor types. Old
languages will evolve while new languages are under development. The current parallel programming toolsets will themselves evolve whereas many of their ideas will
ultimately be embedded into new and established languages. One way to mitigate
the impact of this evolution on application programmers relies on establishing highlevel, software abstractions that can remain reasonably static and self-consistent
over time. Many of the most impactful application domains, for example, energy
and climate sciences, will remain as high a priority a decade hence as a decade
prior. In these physics-based elds, the tensor form of the governing equations
will not change. Thus, tensor calculus abstractions hold great promise for achieving
abstraction invariance.
Finally, candidate abstractions must be language-agnostic. OOD patterns thus
provide a natural lexicon for expressing candidate abstractions. Developers can implement OOD patterns in any language that supports OOP, as Part II of the current
text demonstrates in Fortran 2003 and C++.
All of the reasoning in this section suggests architecting scientic software by
combining OOD patterns built around the concept of a parallel, tensor calculus
based on side-effect-free, composable functions. This forms the central theme and
ultimate aim of the current book, namely that the design aims of a book focused
on expressiveness over performance ultimately align with performance points to a
deep truth. Consider that the calculus abstractions employed throughout much of
this book are modeled after the analytical expressions used in deriving closed-form,
analytical solutions to PDEs. In writing analytical solutions, one often arrives all at
once at the solution for the whole domain or some unidirectional factor in the
solution with methods such as the separation of variables or some globally varying
term in the solution in the case of a series expansion. Thus, analytical solutions
are intrinsically parallel. One expects the same to be true of software constructs
patterned after continuous forms.
air = constructGas(1.52E-05);
27
28
The constant denitions in the previous examples serve dual roles as declarations
and effectively as comments.
Rule 1.3. Make constants constant.
The only downside to deciding to declare and initialize the viscosity is that
every time we declare a new data structure, we increase the possible data dependencies identied in section 1.2 as a key cause of increased programming complexity.
Declaring the viscosity constant via the parameter or const keywords in Fortran
and C++, respectively, gives the code read-only access to it, thereby reducing data
dependencies.
Rule 1.4. Minimize global data.
Rule 1.5. Make global data constant.
Rule 1.6. Provide global type parameters, including precision speciers.
Although global data sharing is to be discouraged, dening a minimal set of
values with read-only access (i.e., constants) causes no harm and, in the case of
type parameters, makes the code considerably more exible. Other globally useful
values include mathematical and physical constants. The kind_parameters module
in Figure 1.4 illustrates. For example, if every module uses this module and every
real type declaration takes the form:
real(rkind) :: x
then changing the entire software package from single- to double-precision proves
trivial from the software perspective numerical considerations aside.
Values that require coordination across multiple les also require global specication in order to avoid conicts. These might include le names and unit
numbers.
Rule 1.7. Provide global conditional debugging.
As proposed by Akin (2003), another useful global constant in Fortran is one
that facilitates toggling between production mode and debugging mode as follows:
logical ,parameter :: debugging=.true.
One can use such a parameter to perform useful debugging tasks conditionally. One
such task might be to print a message at the beginning and end of each routine.
Making debugging a global constant allows most compilers to remove any code that
depends on it when a high optimization level is employed. Lines such as:
if (debugging) print constructGas: starting
...
if (debugging) print constructGas: finished
would be removed during the dead-code removal phase of most optimizing compilers
if debugging is .false. Hence, there is no run-time penalty during production runs.
This practice proves most important in Chapter 2 and beyond, where we adopt a
programming strategy that leads to calling sequences that can only be determined at
runtime.
Rule 1.8. Declare intent for all procedure arguments.
29
30
By contrast, we have never found a good reason to violate data privacy in production code, although it is sometimes useful temporarily while debugging. The point
is to habituate oneself to writing robust code by default, rather than to impose an
unnatural level of uniformity. As Ralph Waldo Emerson said, A foolish consistency
is the hobgoblin of small minds.
Love Thy Neighbor As Thy Source
Structure your code so that it comments itself and reads like prose. Missives
that would never have impressed the CPU might win you the adoration of programmers who have to read your code. Those programmers include your future
self.
EXERCISES
1. In some systems, cooling fans stay on after the rest of the system shuts down. To
determine how long it takes to cool the ns of Section 1.2 down, consider the n
to be a lumped mass with a spatially uniform temperature, T, that obeys:
mc
dT
out
= Q
dt
(1.18)
Believe those who are seeking the truth. Doubt those who nd it.
Andre Gide
2.1 Nomenclature
Chapter 1 introduced the main pillars of object-orientation: encapsulation, information hiding, polymorphism, and inheritance. The current chapter provides more
detailed denitions and demonstrations of these concepts in Fortran 2003 along with
a complexity analysis. As noted in the preface, we primarily target three audiences:
Fortran 95 programmers unfamiliar with OOP, Fortran 95 programmers who emulate OOP, and C++ programmers familiar with OOP. In reading this chapter, the
rst audience will learn the basic OOP concepts in a familiar syntax. The second
audience will nd that many of the emulation techniques that have been suggested
in the literature can be converted quite easily to employ the intrinsic OOP constructs
of Fortran 2003. The third audience will benet from the exposure to OOP in a language other than their native tongue. All will hopefully benet from the complexity
analysis at the end of the chapter.
We hope that using the newest features of Fortran gives this book lasting value.
Operating at the bleeding edge, however, presents some short-term limitations. As
of January 2011, only two compilers implemented the full Fortran 2003 standard:
The IBM XL Fortran compiler,
The Cray Compiler Environment.
However, it appears that the Numerical Algorithms Group (NAG), Gnu Fortran
(gfortran), and Intel compilers are advancing rapidly enough that they are likely
to have the features required to compile the code in this book by the time of
publication (Chivers and Sleightholme 2010).
Since Fortran is the primary language for Part I of this text, we default to the
Fortran nomenclature in this chapter and the next one. Upon rst usage of a particular term, we also include a parenthetical note containing the italicized name of
the closest corresponding C++ construct. The relationship between the Fortran and
C++ constructs, however, is rarely one of exact equality. Often, both could be used in
31
32
Fortran
C++
General
Derived type
Component
Class
select type
Class
Data member
Dynamic Polymorphism
(emulated via
dynamic_cast)
Virtual Member function
Base class
Subclass
Namespace
Function overloading
Destructor
Overloaded operator
Overloaded assignment
operator
Pure virtual member
function
Function prototype
Primitive type/procedure
Type-bound procedure
Parent type
Extended type
Module
Generic interface
Final procedure
Dened operator
Dened assignment
Deferred procedure binding
Procedure interface
Intrinsic type/procedure
Booch
Method, operation
Parent class
Child class
Package
Static polymorphism
Abstract method
Procedure signature
Built-in type/procedure
et al. (1999)
additional ways that do not correspond with the common usage of the other. For example, we suggest below that Fortran 2003 derived types correspond to C++ classes,
but Fortran derived types can be parameterized in the sense that parameters declared in the type denition can be used to set things like precision and array bounds
directly in a type declaration without calling a constructor1 . No analogous feature
exists in C++. Likewise, C++ classes can be templatized, that is, types can be generic
in the source and not resolved to actual types until compile-time. No analogous feature exists in Fortran, although some of the simpler features of templates can be
emulated in Fortran as explained by Gray et al. (1999) and Akin (2003).
We focus on a capability subset common to Fortran and C++. When we draw
comparisons between Fortran and C++ constructs, we imply only that the two largely
correspond when used in the manner described herein. Table 2.1 summarizes the
approximate correspondences along with some language-neutral terms generally
employed for the same concepts. Most of the latter come from the Unied Modeling Language (UML), a schematic toolset for describing object-oriented software
independently from any chosen implementation language (Booch et al. 1999). As
demonstrated in the remainder of Parts I and II, and Appendix B using UML during
the software analysis and design phase facilitates focusing on the high-level modeling
concepts rather than specic implementations thereof.
1 Section 2.2 denes and discusses the special type of procedure referred to in OOP terminology as a
constructor.
radio transmitter
greet
astronaut
33
34
Astronaut
greeting : String
constructor
+ astronaut(String) : Astronaut
+ greet() : String
Figure 2.2. Astronaut class diagram.
A more complete OOA might incorporate additional actors, such as a navigator, along with additional use cases such as request landing location. OOA also
considers various usage scenarios; however, one scenario sufces for the current,
simple example. Given the simplicity of the current objective, we instead proceed to
the OOD phase. OOD involves decomposing a problem domain into a set of useful
classes, each of which is also termed an abstract data type (ADT). An ADT describes a collection of objects with a common state representation and functionality.
Typically, an ADT embodies an abstraction, or abstract representation of whatever
system it models. The term abstract implies the ADT retains only the features and
behaviors required to represent the actual system in a particular context. One often
regards an object as an anthropomorphic, or at least animated, entity that receives
messages and collaboratively acts with other objects to accomplish some goal driven
by the received message.
The OOD results in a specication of the ADTs that can be used to facilitate the
given use cases. UML also provides a standard schema for diagramming a designs
structure: the UML class diagram, so named because class is a common synonym
for ADT. Due to differences in the meanings of the class keywords in Fortran and
C++, we prefer the language-neutral ADT terminology; however, we use class
interchangeably.
Figure 2.2 provides a UML class diagram for an astronaut ADT. A class diagram
represents an ADT in a three-part box. The top part provides the class name. In
Figure 2.2, the class is named Astronaut.
The middle part of a class diagram describes the attributes that is, data comprising the state of each instance of the class, each instance being termed an object.
Names can be preceded by + or , indicating whether they are public or private,
respectively. Public data can be referenced by code outside the module containing
the class implementation. Private data can only be referenced inside the module.
These can also be referred to as public or private scope. In this part of a class diagram, a datums type follows its name and is separated from the name by a colon. In
Figure 2.2, the state is represented by a private String greeting.
The bottom part of a class diagram details the operations that is, the methods.
This part provides the names, scope, and return types of procedures implemented
in the class. In OOP parlance, the procedure used to initialize, or instantiate, an
object is termed a constructor. In UML, one customarily lists constructors rst and
precedes them by the stereotype name constructor (Deitel 2008). UML encloses
stereotypes in guillemets ( and ). As indicated, the astronaut class implements
a public constructor that takes a String and returns an astronaut instance. It also
implements a public greet() method that returns a String.
Array
+
+
+
size(Array<Element>) : Integer
shape(Array<Element>) : Array<Element>
allocated(Array<Element>) : Boolean
(a)
type
Integer
type
Character
type
Real
enumeration
Boolean
false
true
(b)
String : Array<Character>
(c)
Figure 2.3. UML Array model : (a) Array template class, (b) supporting primitive classes, and
(c) sample instantiation.
Figure 2.3 shows the attribute and operation portions of a UML diagram are
optional, as is the listing of private information. The Integer, String, and Real types
suppress all attributes and operations. The Array class diagram suppresses only
attributes. The Boolean type suppresses only operations.
Although UML does not contain an intrinsic array type, any types dened by a
UML model are considered UML model types. Henceforth, we assume in all of our
UML diagrams the availability of a utility template class enabling the construction
of arrays of various types. By denition, a template class is a class used to construct
other classes. One often says such a class is generic or, in UML terminology, parameterized. In the template class of Figure 2.3(a), the parameter is Element, which
must represent another class. We have omitted the attributes of the Array class in
Figure 2.3(a) under the assumption they are private. In most cases, they would include array descriptors holding the values returned by the methods depicted in the
bottom box of in Figure 2.3(a).
We can also model intrinsic or, in UML terminology, primitive types as classes.
The primitive types we use in our UML diagrams correspond to those in Figure 2.3(b).
The Integer class name draws inspiration from the like-named Fortran type. The
Boolean class provides an example of an enumeration of values: true and false.
It corresponds to the Fortran logical type.
The details of resolving the String type to an actual programming language type
differ from language to language and even from implementation to implementation.
In the code examples later in this chapter, we use allocatable deferred-length character variables, for which Fortran provides automatic dynamic memory management.
35
36
Figure 2.3(c) depicts another option: instantiation of a String class as an array with
Character elements (which assumes prior denition of a Character type). Finally,
Figure 2.3(a) also depicts our assumption that the available array template class utility implements the functionality of Fortran allocatable arrays and can therefore be
allocated dynamically and can return information about its size, shape, and allocation
status.
Although the Array class in Figure 2.3(a) is 1D, one can readily build up multidimensional arrays from such a class. In Fortran, there is no need to do so since the
language has built-in multidimensional arrays. In C++, building a templatized multidimensional array by wrapping STL vectors proves straightforward. We demonstrate
this in Chapter 8.
In scientic work, an ADT often represents a physical entity, such as a uid
or plasma, or a mathematical entity such as a vector eld or a grid. Before turning
to more scientic examples, the next section describes the technology that supports
constructing an ADT in Fortran 2003 with references to the corresponding C++
constructs.
Codes Are People Too
Object-oriented thinking lends itself to an anthropomorphic view of objects. They
have state (attributes) and behavior (operations). They receive messages entailing
the operations invoked on them and the arguments passed to them. An operations
result conveys an objects response. A program thus consists of a set of objects
collaborating to accomplish the users goal. A UML use case diagram summarizes
the actors in this drama along with their activities. A UML class diagram details
the supporting class structure.
1
2
3
4
5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(a)
program hello_world
implicit none
character(len=12),parameter :: message=Hello world!
print *,message
end program
(b)
module astronaut_class
implicit none
private
! Hide everything by default
public :: astronaut ! Expose type & constructor
type astronaut
private
character(:), allocatable :: greeting
contains
procedure :: greet ! Public by default
end type
interface astronaut ! Map generic to actual name
procedure constructor
end interface
contains
function constructor(new_greeting) result(new_astronaut)
character(len=*), intent(in) :: new_greeting
type(astronaut) :: new_astronaut
new_astronaut%greeting = new_greeting
end function
20
21
22
23
24
25
26
27
28
29
30
31
32
33
program oo_hello_world
use astronaut_class ,only : astronaut
type(astronaut) :: pilot
pilot = astronaut(Hello, world!)
print *, pilot%greet()
end program
Figure 2.4. Hello, world! programs: (a) structured and (b) object-oriented.
of OOP, line 3 defaults all module components to private scoping, while line 4
makes public the derived type name and the generic constructor name. The derived
type component greeting is explicitly declared private. Since Fortran type-bound
procedures are public by default, greet() also has public scope.
The main program oo_hello_world declares an astronaut named pilot and
calls a like-named constructor that returns an astronaut instance. If the type components were public or had default initializations, we could have called the intrinsic
structure constructor that Fortran 2003 provides with the same name as the type
37
38
name. The result of that constructor could even be used to initialize pilot in its
declaration on line 30. The information-hiding philosophy, however, recommends
against exposing components, and Figure 2.4(b) omits any default initialization. The
gure therefore includes a user-dened constructor.
The program then calls greet(), the invocation of which shows no arguments
on the calling side. The argument received by greet() in the astronaut class implementation represents the object on which it is invoked: pilot. The Fortran 2003
standard refers to this argument as the passed-object dummy argument. In most OOP
languages, including Java and C++, the type of that argument is assumed and need
not be explicitly declared in the denition of the procedure. In these other languages,
one refers to the corresponding entity by the keyword this, so we generally choose
that to be its local name in Fortran as well. The class keyword employed in the
declaration of this facilitates inheritance, which Section 2.5 discusses, and dynamic
polymorphism, which Section 2.6 discusses.
A common model for understanding OOP views a type-bound procedure invocation as sending a message to the object on which it is invoked, that is, the passed-object
dummy argument. The message conveys a request for action. Thus, one views line
32 in Figure 2.4(b) as the main program sending the request greet to the object
pilot. That object responds by returning a character variable containing its greeting.
Figure 2.4(b) also leverages the 2003 standards augmentation of Fortrans
dynamic memory management capabilities. The greeting component of the
astronaut type is an allocatable deferred-length character string. The semantics of
allocatable entities obligates the compiler to automatically allocate sufcient space
to store whatever character string gets assigned to greeting at line 18 and likewise
for whatever string gets assigned to the greet() function result at line 24. Furthermore, the standard obligates the compiler to free any associated memory when the
corresponding entities go out of scope. The compiler must also free (or reuse) any
memory previously assigned to the left-hand-sides (LHSs) of these expressions.
This rst foray into OOP demonstrates the overhead it adds to the software
development process. Much of the additional overhead comes in the early phases
of the process: the problem analysis and design that occur before ngers reach the
keyboard. This extra effort manifests itself in discussions about the relevant actors,
their goals, and their use of the system being modeled. For sufciently small, oneoff programming tasks, the overhead associated with OOA and OOD likely prove
cost prohibitive in that the complexity of Figure 2.4(b) exceeds that of Figure 2.4(a)
by virtually any straightforward measure: the number of lines of code, procedures,
modules, language features, or programming constructs employed.
To the extent portions of Figure 2.4(b) might be more easily reused and extended, however, the additional overhead pays off many times over. In such cases,
information hiding erects a partition between code inside the astronaut class and
any outside code that relies on it. Specically, data privacy opens up the possibility
of completely changing the internal representation of the type components without affecting code external to the class. For example, suppose we wish to provide
a default initialization for the greeting component in pilot to be printed in cases
when the user forgets to instantiate objects. Because allocatable character string
declarations cannot have default initializations, we might facilitate this by switching
to the following xed-width form:
type astronaut
private
character(len=27):: &
greeting=Houston, we have a problem!
contains
procedure :: greet
end type
Making only this change would not force any changes to the main program. Specically, the astronaut() and greet() procedure invocations at lines 31 and 32 could
remain unchanged.
The general principle illustrated by the latter example can be articulated by considering the concept of a class interface, which comprises the collection of entities
(procedure names, argument types, variables, and constants) a class makes public. Regarding public procedures, an actual interface might comprise one or more
interface blocks, containing one or more interface specications (C++ function prototypes). In Fortran, such interface specications only need to be expressed in source
code when dealing with external procedures. External procedures are those not encapsulated in a module. Those without a corresponding interface specication are
said to have an implicit interface. The presence of an interface specication enables
the compiler to check for argument type/rank/bounds mismatches between the calling code and the called code. Such type safety is provided automatically for module
procedures. Module procedures are those contained in a module. Module procedures
are said to have explicit (though confusingly tacit) interfaces.
In order to facilitate argument type checking, we encapsulate all procedures
in this book inside modules. We therefore have no occasion to include interface
specications in programs except to link to C/C++ procedures in Chapter 11. We
employ the term interface to refer to information that, in practice, we never isolate
into one place separate from the executable code that implements that interface.2
Our class interface denition provides a succinct way to summarize one of the
primary benets of information hiding: Changes to privately scoped entities inside a
class, including changes to the internal representation of its data, have no impact on
code outside the class so long as the changes would require no modications to the
class interface. The power of this rule cannot be overstated. Section 2.7 provides an
analysis of the impact this rule has on code complexity and robustness.
2 In Fortran 2008, it is possible to move the implementation of the type-bound procedures to a separate
submodule, leaving only their interfaces in the module that denes the type. This allows complete
documentation of the interface needed by users of the module without exposing details of how the
services described in the interface are implemented.
39
40
Address Compliance
Replace non standardcompliant features
CBSE
Incorporate new code into a
component framework,
Integrate with larger
projects
Address
Obsolescent/Deprecated
Features
Wrap common blocks,
Prefer use over include,
Replace statement functions
...
Figure 2.5. Steps for modernizing legacy Fortran programs (adapted from Norton and Decyk
[2003])
41
42
fin analyzer
specify
problem
system
architect
design
numerical
method
numerical analyst
predict heat
conduction
thermal analyst
Figure 2.6. Use case diagram for a n heat conduction analysis system.
have the allocatable attribute to include derived types and their components. We
have found that this capability eliminates the vast majority of cases where we would
otherwise use the pointer attribute. Nonetheles, when derived types do contain
pointers, Fortran 2003 final procedures must be used to free associated memory
when the pointer name goes out of scope.
The fth step in Figure 2.5 turns to the chief subject of this book: objectorientation. To illustrate this process, we return to the structured heat n conduction
of Chapter 1 and consider how to wrap that code in an object-oriented n analyzer
framework. Figure 2.6 provides a use case for the usage of such a framework. The
actors contemplated there include a system architect, a numerical analyst, and a
thermal analyst. The architect uses the n analyzer to specify the problem, providing
the numerical analyst with a set of relevant physical parameters, including tolerances
and boundary and initial conditions. The numerical analyst uses this specication to
develop a mathematical model in the form of a discrete set of equations that can be
solved on a digital computer. The thermal analyst uses the resulting mathematical
model to predict the performance of various ns.
Having a use case diagram in hand enables us to think abstractly about the
purpose of the software without tying us to a particular design or implementation.
Starting from a given use case diagram, different software development strategies
might generate designs and implementations bearing little resemblance to each other.
Conventional practice would likely proceed directly to the structured program implementation in Figure 1.4. Another strategy might model classes after the actors as
in the astronaut design of Section 2.3. A third might model classes after the activities.
An OOD following the latter path might lead to the class interfaces of Figure 2.7,
including a problem denition class, numerical differentiation class, and a heat conduction class. Chapter 3 presents a fourth approach that leads to a very different
class decomposition.
The Problem class described in Figure 2.7(a) provides a constructor that takes
an Integer le handle as its only argument. Although the implementation details
Problem
constructor
+ problem(le_handle : Integer) : Problem
+ boundary_vals() : Array<Real>
+ diffusivity() : Real
+ nodes() : Integer
+ time_step() : Real
+ spacing() : Real
(a)
Differentiator
constructor
+ differentiator(specication : Problem) : Differentiator
+ laplacian(T :Array<Real>,Tboundaries :Array<Real>) :Array<Real>
(b)
Conductor
constructor
+ conductor(d :Differentiator,spec :Problem,T :Array<Real>) :Conductor
+ heat_for(time_step : Real)
+ temperature() : Array<Real>
+ time_derivative() : Real
(c)
Figure 2.7. Object-oriented heat n analysis package class interfaces : (a) problem specication
class, (b) numerical differentiation class, and (c) heat conduction class.
43
44
1
2
3
4
5
6
7
(a)
program fin_test
use iso_fortran_env ,only : input_unit
use kind_parameters ,only : rkind
use problem_class
,only : problem
use differentiator_class ,only : differentiator
use conductor_class
,only : conductor
implicit none
8
9
10
11
12
13
14
15
16
17
18
19
specification
= problem(input_unit)
finite_difference = differentiator(specification)
fin = conductor(finite_difference,specification)
print (a,5g9.3),initial temperature = ,fin%temperature()
call fin%heat_for(specification%time_step())
print (a,5g9.3),final temperature
= ,fin%temperature()
20
21
22
23
24
25
26
27
28
1
2
1
2
3
4
if (abs(fin%time_derivative())<tolerance) then
print (2(a,es9.3)),|dT/dt|=,fin%time_derivative(),<,tolerance
print *,In main: test passed. :)
else
print (2(a,es9.3)),|dT/dt|=,fin%time_derivative(),>,tolerance
print *,In main: test failed. :(
end if
end program
(b)
&physics alpha=1.,L_fin=1.,T_chip=1.,T_air=1. /
&numerics dt=0.01,nodes=3 /
(c)
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
Figure 2.8. Test-driven development: (a) main program, (b) input, and (c) output for adiabatic
test.
T
= 2 T = 0
t
(2.1)
Figures 2.8(a)-(b) depict a main program and an input le that tests for this condition
given a set of classes that implement the interfaces of Figure 2.7. Figure 2.8(c) shows
the resulting output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Figure 2.9
module problem_class
use kind_parameters ,only: ikind,rkind
implicit none
private
public :: problem
type problem
private ! Default values:
integer(ikind) :: num_nodes=4
real(rkind):: air_temp=1.,chip_temp=1.!boundary temperatures
real(rkind):: alpha=1.,length=1.
!physical parameters
real(rkind):: dt=0.1,dx
!numerical parameters
contains
! constructor computes default
procedure :: boundary_vals
procedure :: diffusivity
procedure :: nodes
procedure :: time_step
procedure :: spacing
end type
interface problem
procedure spec ! constructor
end interface
contains
type(problem) function spec(file)
use conduction_module &
,only : read_physics,read_numerics,stable_dt
integer(ikind) :: file
integer(ikind) :: elements_default
28
29
30
31
elements_default = spec%num_nodes+1
spec%dx = spec%length/elements_default ! default element size
45
46
if (.not. read_physics(file
&
,spec%alpha,spec%length,spec%chip_temp,spec%air_temp)) &
print *,In problem constructor: Using default physics spec.
35
36
37
38
if (.not. read_numerics(file
&
,spec%dt,spec%num_nodes,spec%dx,spec%length)) &
print *,In problem constructor: Using default numerics spec.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
spec%dt = min(spec%dt,stable_dt(spec%dx,spec%alpha))
end function
pure function boundary_vals(this)
class(problem) ,intent(in) :: this
integer(ikind) ,parameter :: end_points=2
real(rkind) ,dimension(end_points) :: boundary_vals
boundary_vals = (/this%chip_temp,this%air_temp/)
end function
pure real(rkind) function diffusivity(this)
class(problem) ,intent(in) :: this
diffusivity = this%alpha
end function
pure integer(ikind) function nodes(this)
class(problem) ,intent(in) :: this
nodes = this%num_nodes
end function
pure real(rkind) function time_step(this)
class(problem) ,intent(in) :: this
time_step = this%dt
end function
pure real(rkind) function spacing(this)
class(problem) ,intent(in) :: this
spacing = this%dx
end function
end module
Figure 2.9. Problem denition class implementation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Figure 2.10
module differentiator_class
use kind_parameters ,only: ikind,rkind
implicit none
private
! Hide everything by default.
public :: differentiator ! Expose type/constructor/type-bound procs.
type differentiator
private
real(rkind),dimension(:,:),allocatable::diff_matrix
contains
procedure :: laplacian ! return Laplacian
procedure :: lap_matrix ! return Laplacian matrix operator
end type
interface differentiator
procedure constructor
end interface
contains
type(differentiator) function constructor(spec)
use problem_class ,only : problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Figure 2.11
module conductor_class
use kind_parameters ,only : ikind,rkind
use differentiator_class, only : differentiator
implicit none
private
! Hide everything by default
public::conductor ! Expose type and type-bound procedures
integer(ikind), parameter :: end_points=2
type conductor
private
type(differentiator) :: diff
real(rkind) :: diffusivity
real(rkind) ,dimension(end_points) :: boundary
real(rkind) ,dimension(:), allocatable :: temp
contains
! internal temperatures
procedure :: heat_for
procedure :: temperature
procedure :: time_derivative
end type
interface conductor
procedure constructor
end interface
contains
type(conductor) function constructor(diff,prob,T_init)
use conduction_module ,only : read_physics
use problem_class
,only : problem
type(differentiator) ,intent(in)
:: diff
type(problem)
,intent(in)
:: prob
47
48
n:Conductor
diff
diff:Differentiator
diff_matrix = (/1.,-2.,1./)
Figure 2.12. Conductor/Differentiator composite object diagram.
49
50
Conductor
Fun_conductor
constructor
+ fun_conductor(le_handle :Integer,temp_dist :Procedure) :Fun_conductor
type
Procedure
Figure 2.13. Function-initialized conductor inheritance hierarchy.
the parent type as an component in the child type. The child type can override any of
the parents methods by implementing them itself. Otherwise, the compiler handles
any invocations of the parents methods on the child by invoking these calls on the
parent. The Fortran language standard refers to the child as an extended type.
Consider a developer who wants to construct a Conductor by passing a temperature distribution function. We designate this new class fun_conductor as a
shorthand for function-initialized conductor. Since the existing Conductor constructor in Figure 2.11 accepts an optional argument containing nodal temperature
array, we can leverage this ability in the fun_conductor() constructor by accepting an argument that designates a temperature distribution function. The constructor
could sample that function at the node locations and pass those samples to the parent
types conductor() constructor.
The open triangle in Figure 2.13 indicates that a Conductor generalizes
a Fun_conductor. One usually models inheritance as generalization in UML.
Fun_Conductor enhances the Conductor interface by dening a new constructor and
simplies the interface by hiding the Problem and Differentiator objects inside the
fun_conductor() constructor. The test program in Figure 2.15 passes a le handle
and temperature distribution function to the constructor, delegating the acquisition
of all other construction-related data to the constructor.
Figure 2.13 declares the temperature distribution function to be of primitive
type Procedure, which might represent any of several language mechanisms for
passing a procedure argument. In C++, one might pass a function pointer. In Fortran, one might pass a procedure dummy argument (as in our implementation in
Figure 2.14), a procedure pointer, or a C function pointer (see Chapter 11). An OOP
purist might encapsulate the mechanism in a class, reserving the right to change the
private implementation details.
1
2
3
4
5
6
7
module fun_conductor_class
use kind_parameters ,only : ikind,rkind
use conductor_class ,only : conductor
implicit none
private
public::fun_conductor
type ,extends(conductor) :: fun_conductor
private
real(rkind) :: dt
contains
procedure :: time_step
end type
interface fun_conductor
procedure constructor
end interface
contains
type(fun_conductor) function constructor(file,T_distribution)
use differentiator_class ,only : differentiator
use problem_class
,only : problem
integer(ikind) ,intent(in)
:: file
type(differentiator)
:: diff
type(problem)
:: prob
real(rkind), dimension(:) ,allocatable :: T
real(rkind)
:: dx
integer(ikind)
:: i
interface
pure real(rkind) function T_distribution(x)
use kind_parameters ,only : rkind
real(rkind) ,intent(in) :: x
end function
end interface
prob = problem(file)
diff = differentiator(prob)
dx
= prob%spacing()
constructor%dt = prob%time_step()
allocate(T(prob%nodes()))
forall(i=1:prob%nodes()) T(i) = T_distribution(i*dx)
constructor%conductor = conductor(diff,prob,T)
end function
real(rkind) function time_step(this)
class(fun_conductor) :: this
time_step = this%dt
end function
end module
Figure 2.14. Function-initialized conductor implementation.
Figure 2.14 shows a sample implementation of a function-initialized conductor that inherits the type-bound procedures of the Conductor class implemented
in Figure 2.11. Since linear temperature distributions also satisfy equation 2.1 and
therefore dene a steady-state solution, we can use a test analogous to the one used
for the Conductor class. Figure 2.15 demonstrates such a test along with the input
and output les.
The convenience of inheritance lies in the automated compiler support for
applying the operations of the parent to the instances of the child. The typebound procedures temperature() and time_derivative() invoked on fin in
Figure 2.15(a) result in calls to the corresponding procedures in the Conductor
implementation of Figure 2.11, even though the passed-object dummy argument
51
52
1
2
3
4
5
6
7
8
9
10
11
(a)
module initializer
implicit none
private
public :: linear
contains
pure real(rkind) function linear(x)
use kind_parameters ,only : rkind
real(rkind) ,intent(in) :: x
linear = 1. - x
end function
end module
12
13
14
15
16
17
18
19
20
program fun_fin_test
use iso_fortran_env
,only : input_unit
use kind_parameters
,only : rkind
use fun_conductor_class ,only : fun_conductor
use initializer
,only : linear
implicit none
real(rkind) ,parameter :: tolerance=1.0E-06
type(fun_conductor)
:: fin
21
22
23
24
25
26
27
28
29
30
31
32
33
1
2
1
2
3
4
fin = fun_conductor(input_unit,linear)
print (a,5g9.2),initial temperature = ,fin%temperature()
call fin%heat_for(fin%time_step())
print (a,5g9.2),final temperature
= ,fin%temperature()
if (abs(fin%time_derivative())<tolerance) then
print (2(a,es9.3)),|dT/dt|=,fin%time_derivative(),<,tolerance
print *,In main: test passed. :)
else
print (2(a,es9.3)),|dT/dt|=,fin%time_derivative(),>,tolerance
print *,In main: test failed. :(
end if
end program
(b)
&physics alpha=1.,L_fin=1.,T_chip=1.,T_air=0. /
&numerics dt=0.01,nodes=3 /
initial temperature =
1.0
final temperature
=
1.0
|dT/dt|=0.000E+00<1.000E-06
In main: test passed. :)
(c)
0.75
0.75
0.50
0.50
0.25
0.25
0.0
0.0
Figure 2.15. Function-initialized conductor test: (a) main program, (b) input, and (c) output
for steady-state test.
this those procedures receive is of type fun_conductor rather than the declared
conductor. The next section describes this ability to pass objects of types other than
declared type.
53
54
in the Conductor class implementation of Figure 2.11, we originally planned for the
passed-object dummy argument to be of type Conductor. We compiled and ran that
code in the n test of Figure 2.8(c). Dynamic polymorphism, as designated by the
class keyword, enables the reuse of that compiled code, without recompilation, in
the subsequent writing of the Fun_Conductor class. The compiler generates object
code that can detect at runtime the actual argument passed and then dispatch code
capable of invoking the specied method on the passed object. The underlying technology is also referred to as dynamic dispatching. It proves especially useful when
the source code is unavailable, as might be the case in using a polymorphic library
constructed using the techniques in Chapter 6.
Fortran links the concepts of inheritance and dynamic polymorphism by requiring programmers to replace the usual type keyword with its class counterpart in
passed-object dummy argument declarations. The class keyword can be applied if
and only if the passed-object dummy argument is extensible. The actual argument
passed must then be of the type named in the class construct or any type that extends that type, including all generations of descendants. Such a chain of extended
types comprises a class hierarchy or inheritance hierarchy.
rc c
1 tline
2 2
(2.2)
where c is the number of lines with write access to the solution vector. In OOP, c
corresponds to the number of lines in a class (assuming data privacy with one class
per module in Fortran). Hence, signicant reductions can only be accomplished if
class size restrictions can be enforced. Although it might seem draconian to restrict
class size across a project, Chapter 3 outlines a strategy that very naturally leads to
roughly constant c across the classes.
55
56
must be weighed against the possibility that a compiler might make an unnecessary
temporary version of the object before copying it into the left-hand-side object and
destroying the temporary return instance. In many cases, optimizing compilers can
eliminate such temporaries.
EXERCISES
1. Verify that the corresponding tests fail with nonlinear initial temperature
distributions in (a) Figure 2.8(a) and (b) Figure 2.15(a).
2. In some systems, cooling fans stay on after the rest of the system shuts down.
To determine how long it takes to cool the ns of Section 1.2 down, consider
the n to be a lumped mass with a spatially uniform temperature, that obeys
mc dT
dt = Qout , where m and c are the n mass and specic heat and Qout is the
heat transfer rate.
(a) Draw a UML class diagram for a lumped mass class capable of determining
the time necessary to reach a nal cooling temperature from a given starting
temperature.
(b) Write a main program that uses the above class to output the cooling time.
List a few subclasses one might later desire to build from your lumped mass
class.
Scientic OOP
58
Scientic OOP
They get exposed to eld instances starting with scalar elds such as pressure and
temperature. These concepts get generalized to include vector elds such as electric
and magnetic vector elds. Ultimately, further generalization leads to higher-order
tensor elds such as stress elds in continuum mechanics and energy tensor elds in
relativity.
The apparent reusability of the eld concept makes it a prime candidate for encapsulation in software. A scalar eld class constructed to model thermodynamic
pressure could be reused to model absolute temperature or material density. Furthermore, one can imagine an ADT hierarchy in which scalar eld objects become
components of a vector eld class.
A similar statement can be made for the particle concept. A neutral particle class
used to model atmospheric aerosols might be reused to develop a charged particle
class for studying electric precipitators. Inheritance might be employed to add a
new charge component to a derived type, while adding a type-bound procedure that
computes electric and magentic eld forces on the particle.
Dening a set of algebraic, integral, and differential operators for application to
elds enables compact representations of most physical laws. Fortran supports userdened operators (C++ overloaded operators), the implementations of which can
evaluate approximations to the corresponding mathematical operators. The results
of expressions containing user-dened operators can be assigned to derived type instances either via a language-dened intrinsic assignment or by a dened assignment
(C++ overloaded assignment operator). We refer to the evaluation of expressions
involving the application of mathematical operators to ADT instances as abstract
data type calculus or ADT calculus.
Supporting ADT calculus has been a recurring theme across numerous scientic software projects over the past decade, including the Sophus library at the
University of Bergen1 (Grant, Haveraaen, and Webster 2000), the Overture project
at Lawrence Livermore National Laboratory2 (Henshaw 2002), and the Sundance3
and Morfeus4 projects that originated at Sandia National Laboratories (Long 2004;
Rouson et al. 2008). In many instances, these projects allow one to hide discrete
numerical algorithms behind interfaces that present continuous ones. As noted on
the Sundance home page, this lets software abstractions resemble blackboard abstractions. Users of these ADTs can write ordinary differential equations (ODEs)
and PDEs formally in a manner that closely resembles their expression informally
in blackboard discussions.
Table 3.1 depicts software abstractions that might correspond to various blackboard ones. The rst row of the software abstraction column declares a variable T,
its time derivative dT_dt, and its Laplacian laplacian_T to each be scalar elds. The
second row provides a notation that might be used to dene boundary conditions.
The third row provides a software notation analogous to the subscript notation frequently used for partial derivatives a time derivative in the case shown. The fourth
row demonstrates forward Euler time advancement. The fth row shows how the
result of the T%t() operator might be calculated. In doing so, it also depicts the full
1
2
3
4
http://www.ii.uib.no/saga/Sophus/
https://computation.llnl.gov/casc/Overture/
http://www.math.ttu.edu/klong/Sundance/html/
http://public.ca.sandia.gov/csit/research/scalable/morfeus.php
Blackboard abstraction
Software abstraction
type(field) :: T,dT_dt,laplacian_T
call T%boundary(x=0,T0)
T%t()
T = T + T%t()*dt
dT_dt = alpha*laplacian(T)
laplacian_T = T%xx() + T%yy() + T%zz()
expression of a PDE as it might appear inside an ADT that uses the field class
to represent heat conduction physics. The nal row demonstrates the evaluation of
the PDE RHS by invoking the aforementioned subscript analogy. This expression
would likely appear as the result of a function implemented inside the field class
itself.
Figures 3.13.3 refactor the n heat conduction problem using ADT calculus.
Figure 3.1 offers a selection of tests of forward Euler quadrature:
T n+1 = T n + T/t|tn t
= T n + 2 T n t
(3.1)
(3.2)
(3.3)
= T n + 2 T n+1 t
1
Tn
= 1 t 2
(3.4)
(3.5)
where lines 27 and 30 in Figure 3.1 provide the corresponding software expressions.
In the case of implicit time advancement schemes such as backward Euler, the
choice of software abstractions requires considerable thought to preserve clarity and
generality without undue overhead in development time and runtime. One question
regarding line 30 in Figure 3.1 concerns which terms to encapsulate as objects and
which to represent using intrinsic data types. A preference for objects makes the
code easier to maintain: Many details private to the employed classes can be changed
without affecting the client code.
In our implementation, the * and - operators return real arrays, whereas
the .inverseTimes. operator returns an integrable_conductor object. Practical
concerns dominated our choice: The arrays contain more data than would t in
an integrable_conductor, whereas the result of .inverseTimes. contains the right
amount of data for encapsulation in an integrable_conductor, as must be the case
because it produces the RHS that ultimately gets assigned to fin.
The implementation of the .inverseTimes. operator invoked at line 30 also
reects an important efciency concern: Rather than computing a matrix inverse and
59
60
Scientic OOP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Figure 3.1
program integrable_fin_test
use iso_fortran_env
,only : input_unit
use kind_parameters
,only : rkind,ikind
use initializer
,only : linear
use problem_class
,only : problem
use integrable_conductor_class ,only : integrable_conductor
implicit none
real(rkind) ,dimension(:,:) ,allocatable :: I ! identity matrix
real(rkind) ,parameter
:: tolerance=1.0E-06
real(rkind)
:: dt=0.1
! default time step
type(integrable_conductor):: fin
! heat equation solver
integer(ikind)
:: scheme
! quadrature choice
type(problem)
:: specs
! problem specifications
namelist /test_suite/ scheme
15
16
17
18
19
20
21
22
enum ,bind(c)
enumerator forward_euler,backward_euler
end enum
if (.not. get_scheme_from(input_unit)) scheme=forward_euler ! default
specs = problem(input_unit)
fin
= integrable_conductor(specs,linear)
print (a,5g9.2),initial temperature = ,fin%temperature()
23
24
25
26
27
28
29
30
31
32
dt = specs%time_step()
select case (scheme)
case(forward_euler)
fin = fin + fin%t()*dt
case(backward_euler)
I = identity(fin%rhs_operator_size())
fin = ( I - fin%rhs_operator()*dt ) .inverseTimes. fin
case default; stop In main: no method specified.
end select
33
34
35
36
37
38
39
40
41
42
43
44
45
integer(ikind)
:: namelist_status
read(file,nml=test_suite,iostat=namelist_status)
if (namelist_status == found) then
get_scheme_from = .true.
else
get_scheme_from = .false.
end if
rewind(file) ! future reads start from the file head
end function
55
56
57
58
59
60
61
62
63
64
The structure of line 30 reects a desire to decouple the time integration code
from the specic PDE solved. A more equation-specic form might be:
real(rkind) :: alpha
alpha= specs%diffusivity()
fin= (I - alpha*fin%laplacian_operator()*dt) .inverseTimes. fin
where we assume the existence of a suitably dened type-bound procedure that
returns a matrix-form nite difference approximation to the Laplacian operator. The above implementation would tie the time advancement code and the
integrable_conductor interface to the heat equation. For a similar reason, the main
program obtains its own copy of the time step from the problem object, keeping
the physics model free of any of the details of the quadrature scheme used for time
advancement.
Handling derived type components inside dened operators requires discipline
to perform only those operations on each component that are consistent with the
meaning of the operators. When writing a time-integration formula, this suggests
each component must satisfy a differential equation. For alpha in Figure 3.2, the
effective differential equation is d/dt = 0. For stencil in Figure 3.3, we instead
choose to make it a module variable. Giving it module scope enables its use throughout the Field class without incorporating it into the field derived type. We also
give stencil the protected attribute so only procedures inside the enclosing module can modify it. We recommend this practice for all module variables that lack
the private or parameter attributes. We designate stencil protected in case it
becomes public in a future revision.
Whereas Chapter 2 presented the use of UML diagrams in OOA and OOD,
these diagrams also nd use in reverse engineering existing codes. This need arises
61
62
Scientic OOP
when handed legacy software without design documents. It also arises with new software for which the development path appears sufciently straightforward to proceed
to OOP without going through OOA and OOD as might occur in ADT calculus, given the close correspondence between the program syntax and the high-level
mathematical expressions being modeled.
Even in the above cases, UML diagrams can illuminate subtle aspects of the software structure. Figure 3.4 depicts two new types of relationships in a class diagram
reverse engineered from the code in Figures 3.23.3. Whereas the composition and
inheritance relationships in Chapter 2 could be discerned simply by looking at the
source-code denitions of the corresponding derived type denitions, the relationships in Figure 3.4 require deeper inspection of the code. The dashed arrows drawn
to the Problem class denote dependencies that result from argument passing. The
solid line drawn between the Differentiator and Field classes indicates the use of
an object that is neither aggregated explicitly nor inherited implicitly: It has module
scope.
UML diagrams can also explain software behavior. Important behavioral aspects
of ADT calculus relate to the depth of the resulting call trees and the uidity with
which the trees change with the addition or rearrangement of terms in the generating
formulae. The UML sequence diagrams in Figure 3.5 depict the calling sequences
for the forward and backward Euler time-integration formulas at lines 27 and 30 of
Figure 3.1. Following the message-passing interpretation of OOP, one can read a
sequence diagram from top to bottom as a series of messages passed to the objects
in a simulation.
The evaluation of the forward Euler expression fin + fin\%t()*dt involves
rst sending the message t() to fin instructing it to differentiate itself with respect
to time. This results in fin sending the message xx()" to its field component
T_field, which in turn creates a new, temporary Field object. This new Field initializes its allocatable array component by sending the message laplacian() to the
Differentiator object stencil. Subsequently, the temporary Field invokes the division operator on itself represented as a loopback in the sequence diagram. Finally,
fin relays its multiplication and addition messages * and + to T_field to complete
formation of the RHS of the forward Euler expression.
Figure 3.5(b) diagrams the sequence of calls in the evaluation of the backward Euler expression (I-fin\%rhs_operator()) .inverseTimes. fin. Upon
receiving the message rhs_operator(), fin queries its T_field component for its
second-order nite difference matrix. T_field in turn requests the corresponding
matrix from stencil. Because the compiler handles the ensuing intrinsic array subtraction operation, the sequence diagram does not show this step. Finally fin invokes
the .inverseTimes. operator on itself by relaying the same message to its T_field
component.
The relationship between scientic research and ADT calculus is not unique:
ADT calculus could nd use outside science (in nancial mathematics for example)
and other programming paradigms nd use in science. Nonetheless, it appears to
be the one major, modern programming idiom that originated amongst scientic
programmers. Thus, when we refer to Scientic OOP (SOOP), we refer to ADT
calculus. As a novel paradigm, SOOP warrants its own analysis and scrutiny. The
next section provides such analysis and scrutiny.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Figure 3.2
module integrable_conductor_class
use kind_parameters ,only : ikind ,rkind
use field_class
,only : field, distribution
implicit none
private
public :: integrable_conductor
type integrable_conductor
private
type(field) :: T_field ! temperatures
real(rkind) :: alpha
! thermal diffusivity
contains
procedure
:: t
procedure
:: rhs_operator
procedure
:: rhs_operator_size
procedure
:: temperature
procedure
:: time_derivative
procedure ,private
:: product
generic
:: operator(*) => product
procedure ,private
:: total
generic
:: operator(+) => total
procedure ,private ,pass(rhs) :: inverseTimes
generic
:: operator(.inverseTimes.) => inverseTimes
end type
interface integrable_conductor
procedure constructor
end interface
contains
type(integrable_conductor) function constructor(spec,T_distribution)
use problem_class ,only : problem
type(problem) :: spec ! problem specification
procedure(distribution) T_distribution
constructor%T_field = field(spec,T_distribution)
constructor%alpha
= spec%diffusivity()
end function
35
36
37
38
39
40
41
42
43
44
45
function rhs_operator(this)
class(integrable_conductor) ,intent(in) :: this
real(rkind) ,dimension(:,:) ,allocatable :: rhs_operator
rhs_operator = this%T_field%xx_matrix()
end function
63
64
Scientic OOP
46
47
48
49
50
51
function temperature(this)
class(integrable_conductor) ,intent(in) :: this
real(rkind) ,dimension(:) ,allocatable :: temperature
temperature = this%T_field%nodal_values()
end function
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
1
2
3
4
5
6
7
8
9
10
Figure 3.3
module field_class
use kind_parameters
,only : ikind, rkind
use differentiator_class ,only : differentiator
implicit none
private
abstract interface
pure function distribution (x) result(ret)
import
real(rkind), intent(in) :: x
real(rkind) ret
end function
end interface
type field
private
real(rkind) ,allocatable ,dimension(:) :: node ! internal points
contains
procedure
:: nodal_values
procedure
:: field_size
procedure ,private :: product
generic
:: operator(*) => product
procedure ,private :: ratio
generic
:: operator(/) => ratio
procedure ,private :: total
generic
:: operator(+) => total
procedure ,private ,pass(rhs) :: inverseTimes
generic
:: operator(.inverseTimes.) => inverseTimes
procedure
:: xx
! 2nd-order spatial derivative
procedure
:: xx_boundary ! 2nd-order boundary derivative
procedure
:: xx_matrix
! matrix derivative operator
end type
interface field
procedure constructor
end interface
public :: field, distribution
type(differentiator) ,protected
:: stencil
integer(ikind)
,parameter
:: end_points=2
real(rkind) ,dimension(end_points) :: boundary
contains
type(field) function constructor(spec,sample)
use problem_class ,only : problem
type(problem) :: spec
procedure (distribution) sample
real(rkind)
:: dx
integer(ikind) :: i
stencil = differentiator(spec)
dx
= spec%spacing()
allocate(constructor%node(spec%nodes()))
forall(i=1:spec%nodes()) constructor%node(i) = sample(i*dx)
boundary = spec%boundary_vals()
end function
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
65
66
Scientic OOP
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function inverseTimes(lhs,rhs)
use linear_solve_module ,only : gaussian_elimination ! See Appendix A
real(rkind) ,dimension(:,:) ,allocatable ,intent(in) :: lhs
class(field)
,intent(in) :: rhs
type(field)
,allocatable :: inverseTimes
allocate(inverseTimes)
inverseTimes%node = gaussian_elimination(lhs,rhs%node)
end function
end module field_class
Figure 3.3. Field class implementation.
Field
Differentiator
<<constructor>>
field(Problem,Procedure)
Problem
67
68
Scientic OOP
fin:integrable_conductor
fin + fin%t()*dt
T_field:field
stencil:differentiator
t()
xx()
<<create>>
:field
laplacian()
operator(/)
operator(*)
operator(*)
operator(+)
operator(+)
(a)
fin:integrable_conductor
T_field:field
stencil:differentiator
(I rhs_operator()*dt)
.inverseTimes. fin
rhs_operator()
xx_matrix()
lap_matrix()
inverseTimes(Ihs,rhs)
inverseTimes(Ihs,rhs)
(b)
Figure 3.5. Sequence diagrams for the two quadrature schemes in Figure 3.1: (a) forward
Euler and (b) backward Euler.
69
70
Scientic OOP
weakest to strongest. Coincidental cohesion, the weakest form, occurs when parts of
a system have no signicant relationship to each other. Functions in classical, procedural mathematics libraries exhibit coincidental cohesion in that most procedure
pairs have in common only that they evaluate mathematical functions. Functional
cohesion, the strongest form, occurs when each part of a system contributes to a
single task.
In the discrete time-advancement schemes, each of the operators and type-bound
procedures contributes to time advancement. This guarantees functional cohesion
at least in a minimal ADT that implements only the requisite calculus.
The term coupling describes the extent to which different ADTs depend
on each other. As with cohesion, one can rank types of coupling from loosely
coupled to tightly coupled. Authors typically rank data coupling as the loosest
form (Pressman 2001; Schach 2002). Data coupling occurs when one part of a system depends on data from another part. Control coupling, a moderately tighter form,
arises when the logical ow of execution in one part depends on ags passed in from
another part. Content coupling, which typically ranks as the tightest form of coupling (Pressman; Schach), occurs when one violates an abstraction by providing one
ADT with direct access to the state or control information inside another ADT. An
especially egregious form of content coupling can occur when it is possible to branch
directly into the middle of a procedure inside one ADT from a line inside another.
Because Fortran requires dened operators to be free of side effects (all arguments must have the intent(in) property), a developer can write the time
advancement expressions in Figure 3.1 without any information about the state or
ow of control inside the objects and operators employed. ADT calculus thus allows
for strict adherence to data privacy, which precludes content coupling.
Furthermore, depending on the design of the ADT calculus, one can write discrete time advancement expressions in which nearly all the procedure arguments are
instances of the class being advanced. This is the case for the forward Euler algorithm in Figure 3.1. The arguments arise implicitly when the compiler resolves the
statement
fin = fin + fin%t()*dt
into a procedure call of the form
call assign(fin,total(fin,product(t(fin),dt)))
where total(), product() and t() are the dened addition, multiplication, and
time differentiation procedures, respectively, in Figures 3.23.3 and where assign()
could be a suitably dened assignment procedure.6 Thus, in the absence of any ags
internal to the derived type itself, no control coupling arises when employing an
ADT calculus. This leaves only the loosest form of coupling: data coupling. At least
in terms of the top-level semantics, even data coupling is kept to a minimum given
that all operators are either unary or binary, so at most two arguments can be passed.
6 We explicitly reference the dened assignment here for clarity. In practice, Fortrans intrinsic as-
signment sufces unless the derived type contains a pointer component, in which case one needs to
dene an assignment if one desires a deep copy of all the RHS data into the LHS object. Otherwise, the intrinsic assignment executes a shallow copy in which only a pointer assignment occurs,
resulting in the LHS and RHS pointer components sharing a target.
Ce
Ce + Ca
(3.6)
vanishes when modications to other ADTs do not impact the ADT in question (a
completely stable situation) and approaches unity when changes to any other ADT
can potentially affect the one in question (a completely unstable situation). In an
exercise at the end of this chapter, we ask the reader to calculate the instability of
the packages in our various n models. Chapter 8 discusses the instability metric in
the much richer context of multiphysics modeling.
Building up the instability metric from the afferent and efferent couplings represents a subtle shift from measuring properties of software to measuring properties
of the software development process. Although one could argue that the couplings
themselves have little importance in static code, it would be hard to downplay their
potential impact on the code revision process. The instability metric captures that potential impact. The next two subsections also focus on development-time processes:
bug searches and developer communications.
71
72
Scientic OOP
True
True
2
1
False
False
The corresponding condition for the bisection bug search is the presence of
exactly one place where the constraint statements change from truth to falsehood.
A sufciently tight bracketing of the bug in a chronological listing of executed lines
would likely sufce. The number of lines n bracketed at the nth iteration of a bisection search provides as a measure of the maximum distance between the lines
being tested (the end points of the nth segment) and the bug. Because the bisection
methods cuts the size of the interval in half at each iteration, the maximum distance
from the bug at iteration n is
n = 0 /2n
(3.7)
(3.8)
iterations. At the beginning of the bisection search (n = 0), two lines are checked,
those being the rst and last lines of the suspect code. At each subsequent iteration,
one new line is checked, that being the midpoint line. Thus, the search for all r bugs
terminates after
searched = r(2 + log2 0 ) r(2 + log2 )
(3.9)
that generate the call tree and due to these software expressions close resemblance
to the blackboard expressions from which they derive. Hence
searched rc(2 + log2 c)
(3.10)
(3.11)
and to the assertion that the maximum number of lines searched remains very nearly
constant independent of the size of the overall project.
The above analysis focuses on testing for the presence of a bug during program
execution. This would appear to neglect the possibility of errors in the input data.
However, sufciently restrictive validation constraints should detect such errors during the two initial tests that start the bisection search namely the one on the rst
executed line.
73
74
Scientic OOP
type along with the aforementioned mathematical operators, time differentiator, and
dened assignment.
Shannon reasoned as follows about the set of all possible messages that can be
transmitted between two locations (two developers in the present case):
If the number of messages in the set is nite then this number or any monotonic function
of this number can be regarded as a measure of the information produced when one
message is chosen from the set, all choices being equally likely.
The underlying notion here is that communication hinges on novelty. If only one
message is possible, the transmission always contains that message and no new information arrives. Imagine an analog electrical transmission line on which the voltage
remains xed. Such a line cannot be used to transmit information. It follows that the
degree to which the voltage can vary, and likewise the number of possible states into
which the line can be placed, the more novel is the choice of a particular voltage, the
more information can be conveyed.
Shannon went on to choose the logarithm as the monotonic function because it
satises several conditions that match our intuitive understanding of information. For
example, consider a digital transmission line that can transmit one of two messages,
say 0 or 1. Adding two identical new lines would cube the number of states available
to the collective system: There would be 23 possible messages. Intuitively, however,
one expects the amount of information to triple in going from one line to three. The
logarithm rescales the number of states to match our intuition: log2 23 = 3 log2 2 = 31,
where the base of the logarithm determines the size of one unit of information. In
digital systems, the natural unit is the binary digit, or bit.
More generally, Shannon dened information entropy, a measure of information
content, as
N
H
pi log pi
(3.12)
i=1
where pi is the frequency of occurrence of the ith token (e.g., a character or keyword)
in some large sample representative of the domain of interest, and where N is the
number of unique tokens. In Shannons entropy, log pi represents the amount of information communicated in each occurrence of the ith token. In probability theory,
summing the product of a quantity and its probability over the entire space of probabilities provides its expected value or mean. Taking all choices (tokens) as equally
likely leads to pi = 1/N and therefore
H = log N
(3.13)
which returns us to the simpler denition: the logarithm of the number of possible
messages.
To relate these concepts to source code, consider that the information received
over time on a transmission line can also be spread over space in a program source
le. Thus one draws an analogy between the probability of occurrence of a token
in a signal and its probability of occurrence in a program. One can measure the
probability by calculating the tokens frequency of occurrence in a signal (or program
le) sufciently long to provide accurate statistics.
= (Tu ) + 2
t
x
x
(3.14)
where u u/(c) is a constant ow speed divided by the material density and specic
heat capacity. To enable forward and backward Euler time advancement, we might
build the new type as follows:
module integrable_fluid_ADT
implicit none
private
public :: integrable_fluid
real(rkind) ,parameter :: u_star =1.0_rkind
type ,extends(integrable_conductor) :: integrable_fluid
private
contains
procedure :: t
procedure :: rhs_operator
procedure ,private ,pass(rhs) ::
inverseTimes
generic :: operator(.inverseTimes.) => inverseTimes
end type
interface integrable_fluid
procedure constructor
end interface
contains
! implementation omitted
end module
where we have overridden three of the parent types type-bound procedures: t,
rhs_operator, and inverseTimes. Two compute the new time derivative and the
RHS operator matrix. A third solves the linear system, allowing for a more sophisticated Gaussian elimination process, for example, one with pivoting, due to potential
changes in the matrix properties. The new type inherits the parent types remaining
type-bound procedures.
The character/keyword patterns that recur in the integrable_conductor and
integrable_fluid types would occur in any ADT designed for forward and backward Euler time advancement. Considering each character as a token yields high
pi and low H when adding integrable_fluid type to the package. This slow
information growth reects in very succinct human communications between programmers. The test writer simply informs the developers of the required operators.
The developers provide interfaces supporting those operators.
75
76
Scientic OOP
Taking larger groups of characters as the tokens yields even lower H. At one
extreme, the entire listing of integrable_conductor type-bound procedures from
the contains to the end type could be one token. This token would recur across
all ADTs that support the desired calculus without inheriting any procedure bindings. The resulting low H values would indicate the possibility of very high le
compression, the signicance of which we consider next.
The dependence on the token set demonstrates the nonabsolute nature of information entropy H. Kolmogorov complexity K provides a more general quantication
of information in that it does not depend on a probability model (which depends on
the choice of tokens). Although the cost of this generality lies in the noncomputability
of K, estimating K by compressing les links K to H, which is computable. Shannons
relative entropy provides the link:
The ratio of the entropy of a source to the maximum value it could have while still
restricted to the same symbols will be called its relative entropy. This is the maximum
compression possible when we encode into the same alphabet.
Thus, the best available compression software yields the tightest estimate to the
Kolmogorov complexity as well as the best estimate of the relative entropy.7
Equation (3.12) is formally analogous to the thermodynamic entropy of statistical
mechanics, wherein it describes the information one obtains by determining the
microscopic state of a collection of matter (as determined by the quantum states of
its constituent parts) when it is in a particular macroscopic state. The thermodynamic
limit treats all microscopic states as equally likely so H = k ln N, where the Boltzmann
constant k facilitates converting the logarithm to the natural base and thereby sets
the unit of information.
Kirk and Jenkins (2004) took the thermodynamic analogy further. They estimated the entropy of Java byte code. They also showed that other properties of
the compressed code behave like other thermodynamic variables such as volume,
temperature, and pressure. They plotted these variables against each other for an
ensemble of student projects. We will not pursue an empirical study of source code
entropy in the current text, but an exercise at the end of Chapter 8 presents a very
simple, quantiable entropy measure.
The Audacity of Hope
Pessimism abounds regarding the utility, and even the possibility, of reasoning
quantitatively about software properties. Yet heuristic arguments with varying
degrees of sophistication suggest properties of the software development process
are amenable to analysis. OOD metrics suggest ADT calculus makes packages stable under revision. Complexity theory suggests it renders bug search times nearly
independent of project size. Information theory suggests it reduces the required
developer communications by limiting the growth in the interface information
content incurred when adding new abstractions.
7 Encapsulation and information hiding also reduce interface content in any OOP project due to the
shorter argument lists they facilitate. SOOP augments these strategy by limiting most argument lists
to one or two for the unary and binary operators, respectively, of an ADT calculus.
77
78
Scientic OOP
by empirical proles of the relative runtime share of various processes in the simulation. On many problems of modest size, the gains will not justify the loss in semantic
elegance and the attendant sacricing of interface clarity.
Optimizing compilers are very likely to be efcient with intrinsic assignment of
RHS objects to the LHS ones. Whereas naive compiler implementations might form
the RHS results array component before copying it into the LHS, more sophisticated compilers often recognize the opportunity to overwrite the LHS in the process
of constructing the RHS result. Doing so, however, might require separating the
multiplication and addition steps, because overwriting the fin component during
the multiplication phase (or likewise during the combined addition/multiplication
phases) generates the wrong result. This subtle interplay between memory management and operation count underscores one of the reasons such optimizations are
best left to the compiler wherever possible.
The issue of creating a temporary object to hold the RHS result represents one
example of the larger issue of how to handle temporaries created as the result of all
of the operators and type-bound procedures in an expression. The compiler knows it
will overwrite the LHS of an assignment, but one cannot safely expect similar insight
regarding the fate of other type-bound procedure results. One strategy involves
writing all ADT calculus procedures such that they return intrinsic types. In the
forward Euler expression, having fin\%t, operator(*), and operator(+) all return
real arrays, exposes a great deal of information to the compiler that can be used
for the kinds of optimizations that might otherwise require combined operators.
This has the drawback, however, of limiting the programmer to the intrinsic data
structures. There would be no way to write a procedure that works only with the
nonzero elements of sparse arrays, for example. Chapter 10 outlines a strategy that
enables users to eliminate most temporaries while retaining control over the data
abstractions.
A common theme in object-oriented software design (the theme of Part II of the
current text) is a preference for aggregation over inheritance. Both technologies involve an instance of one object serving as a component of another. With aggregation,
the programmer explicitly imposes this structure. With inheritance, the compiler creates this structure automatically. In many instances, however, employing inheritance
resembles using a machete to slice butter. Much of the power is wasted if the two
types do not share a signicant portion of their interfaces and the writer of the child
type must override most or all of the type-bound procedures. An even worse case
occurs when some of the procedures in the parent types interface have no reasonable
meaning in the child types interface.
This dilemma exposes the ambiguities of the has a and is a metaphors for
aggregation and inheritance relationships, respectively. These metaphors are interchangeable in theory. Nonetheless, aggregation often feels more natural in practice.
To wit, saying an integrable_conductor is a field that satises the heat equation
is essentially equivalent to saying an integrable_conductor has a field that satises the heat equation. Yet several of the procedures bound to the field type
would need different interfaces for clarity in the integrable_conductor. It makes
little sense to talk about a conductors second derivative. Furthermore, some of
the procedures exposed in the field interface would prove completely superuous in the integrable_conductor interface. For example, the ADT calculus the
For many hundreds of years, the essence of scientic discourse could be thought of
metaphorically as a collective discussion at the blackboard or on a handwritten page.
8 http://www.merriam-webster.com/dictionary/tao
79
80
Scientic OOP
The advent of scientic computing, however, has largely turned this open dialogue
into sorcery. Individual researchers or small research groups develop codes that are
largely indecipherable to the uninitiated. This has ramications both for the openness
of the scientic dialogue and the ability to independently verify codes and validate
their output.
Via ADT calculus, SOOP empowers researchers to build software constructs
that express the essential nature of the mathematical constructs they represent. In
many respects, the syntax of ADT calculus resembles many commercial, symbolic
computation engines such as Mathematica, Mathcad, or Maple. Although some of
these packages evolved from open versions, their modern incarnations are proprietary. In this regard, ADT calculus aims to bring the nonprofessional programmer
the person who sees scientic insights as their primary product rather than software
behind the curtain to see how their code can more closely resemble the natural syntax
that professional programmers provide them via the aforementioned packages.
EXERCISES
1. The Crank-Nicolson time advancement algorithm applied to the heat equation
takes the form
t 2 1
t 2 n
n+1
n
T +
(3.15)
= 1
T
T
2
2
Modify Figure 3.1 to include a third enumeration named crank_nicolson. Add a
case(crank_nicolson) to the select case construct in the same gure to include
a software expression of the form
fin = ( I - fin%rhs_operator()*(alpha*dt/2.) &
.inverseTimes. ( fin + fin%t()*(alpha*dt/2.)
where I is an appropriately sized identity matrix and fin%rhs_operator()
returns a nite difference matrix operator approximating a 1D Laplacian operator. Test the new scheme by dening a new, nonlinear initial condition and
advancing to steady state until the the magnitude of the value returned by
fin%time_derivative() falls below tolerance.
2. Draw a UML sequence diagram to describe the sequence of execution corresponding to the above Crank-Nicolson code.
3. Use the Fortran 2003 pass attribute in the product type-bound procedure
binding in Figure 3.2 to enable the products
fin%rhs_operator()*(dt/(alpha*2.))
and
fin%t()*(alpha*dt/2.)
to be written in the reversed order
(alpha*dt/2.)*fin%rhs_operator()
and
(alpha*dt/2.)*fin%t()
respectively.
4. Add a debugging parameter of logical type to the kind_parameters module
(or, for clarity, create a new module) and modify each module in the integrable
conductor model to give it access to this parameter via a use statement. Insert
lines of the form
if (debugging) print *,In (procedure name): start
and
if (debugging) print *,In (procedure name): end
at the beginning of each procedure throughout the model.
(a) Set debugging=.true. and print the calling sequence in the integrable
conductor model.
(b) Print the calling sequence again after reordering the terms in the forward
Euler expression, for example
fin = fin%t()*dt + fin
Try other rearrangements of this expression.
5. Calculate the afferent couplings (Ca), efferent couplings (Ce), and instability (I)
of all classes in the n heat conduction models in Chapters 23. Which approach
appears to be the most stable in terms of the average ADT stability across the
design?
81
PA R T II
SOOP T O N U T S A N D B OL TS
4.1 Essentials
Whereas code reuse played an important role in Part I of this text, design reuse
plays an equally important role in Part II. The effort put into thinking abstractly
about software structure and behavior pays off in high-level designs that prove useful
independent of the application and implementation language. Patterns comprise
reusable elements of successful designs.
The software community typically uses the terms design patterns and objectoriented design patterns interchangeably. This stems from the expressiveness of
OOP languages in describing the relationships and interactions between ADTs. Patterns can improve a codes structure and readability and reduce its development costs
by encouraging reuse.
Software design patterns comprise four elements (Gamma et al. 1995):
1. The pattern name: a handle that describes a design problem, its solution, and
consequences in a word or two.
2. The problem: a description of when to apply the pattern and within what context.
3. The solution: the elements that constitute the design, the relationships between
these elements, their responsibilities, and their collaborations.
4. The consequences: the results and trade-offs of applying the pattern.
Although there have been suggestions to include additional information in identifying a pattern, for example, sample code and known uses to validate the pattern
as a proven solution, authors generally agree that elements 2-4 enumerate the three
essential factors in each pattern. Alexander et al. (1977) referred to a slightly different three-part rule as a relation between a certain context, a problem, and a
solution.
The current text takes a soup-to-nuts approach, providing the three essential
elements followed by complete, compilable Fortran and C++ examples. We choose
simplicity and clarity over speed and semantic power. Sophisticated programmers
85
86
might exploit more advanced language features to enhance our codes generality,
speed it up, or reduce its resource utilization. We encourage readers to revise and
extend our examples. Publishing versions that employ more advanced programming
techniques, for example, generic programming, or that address the performance
needs of larger scale applications, might launch a much needed dialogue about
scientic software design.
Demonstrating patterns in code completes a cycle. All patterns start out as working designs that developers reused across well-engineered projects without conscious
recognition of their universality. The code came rst. Gamma et al. (1995) distilled
recurring themes out of the resulting source code after observing those designs
positive impact on projects. Thus, going from ubiquitous truths to simple examples
brings us full circle: Software begets patterns beget software. An additional, unique
step the current text adds to the cycle involves theoretical analysis along the lines of
Section 3.2.
Pattern recognition requires perspective. A frequently used program construction in one language might be an intrinsic facility in another. For instance, as pointed
out by Gamma et al. (1995) and demonstrated by Decyk et al. (1997b1998), one
might construct patterns for inheritance, encapsulation, or polymorphism in a procedural language, whereas these exist in object-oriented languages. Thus, the patterns
one sees at a particular level of abstraction reect the programming capabilities
supported at that level.
At the source code level, intrinsic language constructs represent patterns supported by the creators of the language. Thus, whereas patterns enable us to see the
forest in the trees, such a view treats trees as an intrinsic construct. Were one to attempt to construct trees from scratch using designer genes, the tree concept itself
would represent a biological design pattern.
Patterns comprise tried-and-true constructions well worn by the time of their
inclusion in a pattern canon, or pattern language. The subsequent chapters of Part
II present a pattern menagerie for which we make none of the comprehensiveness
claims that might justify calling it a pattern language. Rather, Part II whets the
readers appetite for patterns with a few we have found broadly useful.
Its Easy Being Green
OOD patterns emerge like the independent recurrence of a solution to a common biological problem in multiple evolutionary tree branches. In this sense, they
resemble organic growth in nature. Moreover, as the construction of natural beings, software bears consideration as a product of nature. Copying time-honored
designs that naturally arise in one environment eases the creation of designs that
feel natural in another.
4.2 Foundations
This section sketches the evolution of patterns research from its origins in building
architecture through the identication of patterns in software architecture to their
recent penetration into scientic software architecture.
4.2 Foundations
the rst Gold Medal for Research by the American Institute of Architects and is now Professor
Emeritus of Architecture at the University of California, Berkeley.
2 Oral tradition attributes this koan to Hakuin Ekaku, 16861769.
87
88
one of the hallmarks of the timeless way. It is a dialogue spoken in pattern languages.
Because each person possesses a pattern language unique to the events and cycles of
their own life, facilitating mutual understanding necessitates constructing a common
pattern language.
Several aspects of Alexanders writing strike chords that resonate across many
schools of philosophical and scientic thought, Eastern and Western, ancient and
modern. In stating, To seek the timeless way, we must rst know the quality without a name, he acknowledges that fundamental truths evade precise articulation in
everyday language. A second universal theme is Alexanders useage of dialectics:
He maintains that vibrant architecture can only emerge from an interplay of ideas
between its designers and its intended inhabitants. Pattern languages give the interested parties a set of design elements along with rules for combining those elements to
extract designs from a combinatorial space of possibilities. Languages allow innite
variation in many of the ultimate details while still maintaining thematic consistency.
For instance, Timeless Way outlines a pattern language for stone houses in the South
of Italy and sketches several plans that could be instantiated from the language.
In addition to identifying the fundamental nature of patterns and their importance, Alexander postulates that the distinction between living and dead patterns
lies in the extent to which everyone shares an understanding of them. In cultures
where the patterns are alive, all people share a basic understanding of building. Such
was the case in agricultural societies. Farmers felt comfortable building barns. Home
owners felt comfortable laying out their homes. While this might seem farfetched in
modern times, Alexander points out that as recently as fty years ago in Japan, for
example, every child learned how to lay out a house.
By contrast, the specialization required in industrial societies tends to kill
patterns:
Even within any one profession, professional jealousy keeps people from sharing their
pattern languages. Architects, like chefs, jealously guard their recipes so that they can
maintain a unique style to sell.
The languages start out by being specialized, and hidden from the people; and then within
the specialties, the languages become more private still, and hidden from one another
and fragmented.
In such settings, so much wisdom fails to propagate down through the generations
that even the specialists begin to make obvious mistakes. Alexander gives examples
from his campus where two seminar rooms lack the geometry and lighting to foster vibrant, comfortable discussions. The architects who designed the room forgot
several basic rules of thumb:
Rooms need natural light from two directions.
Windows too low create silhouettes of occupants as viewed by other occupants.
A reective display surface orthogonal to an adjacent window reects the
4.2 Foundations
Timeless Way denes architectural design patterns in terms of the context within
which they occur, a conict that arises in that context, and the way in which the
pattern resolves the conict. A sample contextconictresolution triplet reads
communal roomconict between privacy and community alcove opening off
communal room. This three-part logic imparts a degree of empiricism: One can
verify whether the context exists, whether the conict arises, and whether the pattern
resolves the conict. Alexander asserts that this empiricism moves the statement
that a pattern is alive from the realm of taste to the realm of objective veriability.
Alexanders nal step in dening a pattern is naming it. Thus, the aforementioned
triplet becomes the ALCOVE pattern.
Whereas patterns share their empirical nature with physics, they share another
quality with computer science: invariance. As Chapter 10 describes, the formal methods subdiscipline of computer science deals in part with logical statements that must
be true throughout the execution of an algorithm. These are invariants of the algorithm. The search for patterns represents a search for invariant aspects of designs
that can be observed across all valid implementations of the pattern.
Timeless Way advocates precision in specifying patterns. An ENTRANCE TRANSITION pattern describes the junction at which the indoors meets the outdoors.
Examples of precision in dening this pattern include stipulations that doors be 20
feet from the street, that windows facing the transition region not be visible from the
street, that the character of surfaces differ from that in the adjoining spaces, and that
it include a glimpse of something entirely hidden from the street. These stipulations
imbue patterns with a property many consider to be among the most fundamental
in all of science: falsiability. Their precision opens them up to challenge.
The book also acknowledges the elusive nature of even some precisely dened
concepts. In one sense, an entrance transition never exists in its own right but merely
describes a relationship between two other entities: indoors and outdoors. Specically, it describes their intersection. In fact, the transition described might even occur
fully indoors. This points up a paradox Alexander identies in the goal of making designs whole. Wholeness implies limits and self-containment, and yet patterns might
exist solely as relationships or as characteristics of a particular part of a design, say,
the part of the indoors closest to the outdoors.
Lest patterns appear overly formal and sterile, Timeless Way connects them intimately with the realm of social science. The ultimate test of a pattern comes in the
way it makes occupants feel. This exposes one of the reasons patterns vary among
cultures. Nonetheless, within the appropriate culture or subculture, Alexander reports a remarkably high degree of unanimity about the feelings patterns evoke in
people.
Not every concept from Timeless Way maps neatly or obviously to software. This
seems particularly true in its discussion of construction. Nonetheless, contemplating
the relationship between the two elds generates useful insights. Consider Alexanders comparison of the emergence of a design to the growth of an organism from
an undifferentiated clump of cells. Rather than stitching together preformed parts,
the organism comprises a single, whole entity throughout its maturation. Different
organs arise in constant contact with each other. Though innitely more complex,
this process seems reminiscent of a technique the current authors employ in program
construction: We replicate a skeleton ADT multiple times before differentiating it
89
90
for the specic role it will ultimately play. In Fortran, such a skeleton might take the
form:
module skeleton_ADT
! Manage entity scope/lifetime
use
,only :
! Surgical use of module entities
implicit none
! Prevent implicit typing
private
! Hide everything by default
public :: skeleton
! Expose type/constructor/methods
type skeleton
private
! Hide data
! derived type components
contains
procedure ::
! Public methods
final :: destructor ! Free pointer-allocated memory
end type
interface skeleton
! Generic constructor name
procedure constructor
end interface
contains
type(skeleton) function constructor()
! return new instance
end function
elemental subroutine destructor(this)
type(skeleton) ,intent(inout) :: this
! deallocate any allocated pointer components
end subroutine
end module skeleton_ADT
Such a skeleton is not viable in the sense that it cannot be compiled. It simply
provides a scaffolding containing the minimum number of elements required for
a well-designed ADT. These include the enforcement of explicit typing, default
privacy, modular scoping, and memory management. What it does not contain is
the equivalent of DNA molecules driving a process of self-organization. Only the
programmer can drive that process.
The skeleton ADT contains this books rst elemental procedure. Fortran
requires rank and parameter matches for automatic invocation of nal subroutines.
If a program instantiates an array of skeleton objects, destructor will only be
called if a nal subroutine explicitly accepts an array argument of matching rank or
if destructor is elemental and therefore supports all ranks.
Timeless Way concludes by providing the kernel of the way. True to form, it
does so by pointing out a paradox: By enforcing a discipline based on reconnecting
your design to its living purposes, a pattern language frees you from its very own
strictures. Once you have mastered it, you no longer need it. You can create beautiful
designs by relying on your innate skills now that you have learned to trust them.
Alexander refers to this state of mind as egoless.
Moving from Timeless Way to the second volume, A Pattern Language, one nds
fewer specics of direct utility in software. Yet the structure and manner in which one
uses the book carries over. In A Pattern Language, Alexander et al. (1977) catalog
4.2 Foundations
253 patterns and order them from the largest to the smallest. Each intermediate
pattern helps complete larger patterns that come before it. Each is itself completed
by smaller patterns that come after it. The progression of patterns goes from regions and towns to clusters of buildings, buildings themselves, building subunits, and
construction details.
Each pattern comprises, in order:
91
92
The third volume, The Oregon Experiment, illustrates the application of the
theory expounded by Timeless Way and the structural template laid out in A Pattern
Language to a specic architectural project at the University of Oregon. It would at
rst seem that the more concrete nature of building design and construction would
lend itself to fewer direct comparisons with software design and construction. Yet the
chief force driving the adoption of patterns in one setting mirrors that in the other:
managing growth. The Oregon Experiment arose from the crisis brought about by
rapid growth over a single decade from a century-long size of a few thousand students
to a student body of 15,000. Recognizing that computer programs experienced a
contemporaneous period of rapid increase in scale, leading to what is often termed
the software crisis, it starts to seem less farfetched that the work of Alexander and
colleagues ultimately inuenced software design.
The Oregon Experiment comprises six chapters, each expounding a single principle the authors believe will probably have to be followed in all communities where
people seek comparably human and organic results. The six principles comprise organic order, participation, piecemeal growth, patterns, diagnosis, and coordination.
The authors set the principle of organic order up in opposition to the totalitarian rule
imposed by traditional master plans. Such plans attempt to map out the organization
of a site envisioned for the distant future. Organic order relies instead on a pattern
language adopted by thousands of local projects meeting immediate needs. A critical
driver of organic order is a planning board small enough to keep meetings focused
and balanced enough between users and decision makers such that real, informed
decision making happens in the room.
With the case for user participation already detailed in the rst two volumes, The
Oregon Experiment focuses on a specic example of user participation in designing a
set of new buildings for the School of Music. The embodiment of user participation
takes the form of a user design group. This group drafts pattern-inspired design
schematics before architects enter the project.
Piecemeal growth harbors two important implications. First, it acknowledges the
need for constant repair of existing structures as part of the growth process. Second,
it suggests new pieces be added slowly, in small increments over time. The authors
suggest enforcing piecemeal growth by distributing a capital budget evenly across
budget categories. With the same amount of funds allocated for small projects in
aggregate as for large projects in aggregate, there will be many more small projects
than large ones.
Of the 250 patterns in A Pattern Language, The Oregon Experiment cites 200
relevant to the university. Of these, they nd 160 of appropriate scope for individual user groups to employ those dealing with building rooms and gardens, for
example while 37 are of sufciently large scope they require university-wide, multiproject agreement to complete. These include BIKE PATHS AND RACKS and PROMENADE
patterns, for example. They also identify 18 domain-specic patterns of special interest to universities, for example, CLASSROOM DISTRIBUTION and REAL LEARNING IN
CAFES patterns. In keeping with the empirical philosophy of Timeless Way, the authors suggest planning boards adopt new patterns or revise old patterns only when
the patterns have experimental or observational support.
Diagnosis refers to an annual process of reevaluating which spaces are alive
and which are dead. As with the timeless nature of patterns, diagnosis has deep
4.2 Foundations
As they point out, object-oriented software designers follow patterns as well. Much
like common literary themes, different patterns arise in different genres or domains.
The GoF explained that their book only captures a fraction of the likely knowledge
of expert designers. They made clear they were not attempting to present patterns for
concurrency or distributed programming, for example; nor did they cover patterns
specic to particular application domains such as user interfaces, device drivers, or
databases.
93
94
Inspired by Alexander, the GoF draw analogies between software architecture and building architecture, comparing objects and interfaces to walls and doors.
Expanding on the three-part rule, the GoF include several additional features of the
patterns they present, including:
Pattern Name and Classication: the name captures the essence of the pattern
and becomes part of the design lexicon, while the classication species the
patterns role (creational, structural or behavioral) and scope (whether they
determine static, compile-time class relationships or dynamic, run-time object
relationships):
Intent: what a pattern does, its rationale, and the problem it solves.
Also Known As: pattern aliases.
Motivation: an archetypal scenario.
Applicability: situational context.
Structure: schematic descriptions of class relationships and interactions.
Participants: classes or objects involved.
Collaborations: the roles of the participating classes.
Consequences: design trade-offs.
Implementation: issues that arise in constructing the solution.
Sample Code: illustrative code fragments.
Known Uses: examples from real systems.
Related Patterns: differences from, and relationships to, other patterns.
While we do not adopt the GoFs complete structure for describing patterns, we
include various elements of this structure in our pattern descriptions.
The GoF identied the key drivers that complicate OOD: encapsulation, granularity, dependency, exibility, performance, evolution, and reusability. They aptly
pointed out that these drivers often oppose each other, sometimes in complicated
multidimensional ways. The desire to reduce dependencies between abstractions
complicates encapsulation. Performance demands often discourage high levels of
granularity in scientic applications where efciency necessitates long loops over
large data sets. Frequent reuse of a given class early in the evolution of a package
might inhibit the packages exibility in later generations.
Two guiding principles reverberate throughout the GoF patterns:
1. Favor object composition over class inheritance.
2. Program to an interface, not an implementation.
Section 3.3 discusses the rst principle. Illustrating the second principle requires introducing a new concept: an abstract class. An abstract class denes the interface that
its child classes must implement. No instances of an abstract class can be instantiated.
In its purest form, an abstract class contains no data and defers the implementation
of all its methods to its child classes. Any class that is nonabstract is termed concrete.
In Fortran and C++, the language mechanisms typically employed to relate abstract classes to concrete ones match those employed to relate parent classes to their
children: type extension in Fortran and inheritance in C++. Whereas a concrete child
class inherits from its parent class any state and behavior that it does not specically
override, no such state and behavior exist if the parent class encapsulates no data and
provides no default procedure denitions. This is the case for pure abstract classes
4.2 Foundations
Abstract_speaker
Astronaut
Figure 4.1. Abstract class realization: Concrete Astronaut realizes, or implements, an
Abstract_speaker interface.
(C++ pure virtual classes). In such a case, one more naturally refers to the child class
as implementing or realizing the interface dened by the abstract parent. UML models a realization relationship with a dashed line adorned by an open triangle at the
interface end, as demonstrated in Figure 4.1. Furthermore, UML denotes abstract
class names in bold italics.
Fortran abstract types facilitate creating abstract classes. Figure 4.2 demonstrates
the denition of an abstract type containing no components, and specifying a deferred
binding speak, leaving the implementation of the corresponding method to extended
types. It constrains those implementations to conform to the abstract interface talk.
Figure 4.2 further depicts the extension of the abstract_speaker type to create an
astronaut with the same purpose as the like-named type in Figure 2.4.
The construction of an abstract type as a base for extension in Figure 4.2(a)
does not enhance the concrete type implemented in Figure 4.2(b). It enhances the
say_something() client code in Figure 4.2(c), enabling it to be written using the
abstract type with knowledge of neither what concrete type actually gets passed
at runtime nor what procedure actually gets invoked on that type. At runtime, any
concrete type that extends abstract_speaker can be passed to say_something. This
exibility is a hallmark of design patterns and explains why abstract class construction
plays a recurring role across many patterns.
Neither Fortran nor C++ formally denes class interfaces in a way that decouples
this concept from a classs implementation. In both languages, however, one can
approximate the class interface concept with abstract classes. The GoF advocate
that all extensions of an abstract class merely dene or redene the abstract methods
of their parent abstract class without dening any new methods. All child classes
so dened share an identical interface. This enables the writing of client code that
depends only on the interface, not on the implementation.
As the GoF pointed out, one must instantiate concrete classes eventually. Creational patterns do so by associating an interface with its implementation during
instantiation, thereby abstracting the object creation. We describe a creational pattern, FACTORY, in Chapter 9. Other GoF patterns we have found useful in our research
include the behavioral patterns STRATEGY and TEMPLATE METHOD, and the structural
pattern FACADE. Each of these is the subject of, or inspiration for, a section in Part
II of the current text. FACTORY METHOD encapsulates object construction for a family
of subclasses. STRATEGY represents a family of interchangeable algorithms. FACADE
presents a unied interface to a complicated subsystem. TEMPLATE METHOD species an algorithm in skeleton form, leaving child classes to implement many of the
algorithms steps.
95
96
Variable
Factory Method
Proxy
Composite
Facade
Iterator
Mediator
Strategy
Template Method
1
2
3
4
module speaker_class
implicit none
private
public :: say_something
Figure 4.2(a)
5
6
7
4.2 Foundations
8
9
10
11
12
13
14
15
16
17
abstract interface
function talk(this) result(message)
import :: speaker
class(speaker) ,intent(in) :: this
character(:) ,allocatable :: message
end function
end interface
18
19
contains
20
21
22
23
24
subroutine say_something(somebody)
class(speaker) :: somebody
print *,somebody%speak()
end subroutine
25
26
1
2
3
4
5
end module
Figure 4.2(b)
module astronaut_class
use speaker_class ,only : speaker
implicit none
private
public :: astronaut
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
97
98
99
100
a design pattern viewpoint, the mathematical problem statement itself is simply the
context within which the software design problem occurs. Dening a set of canonical
contexts here facilitates focusing the rest of Part II more tightly on the software
problems, their solutions, and their consequences.
A desirable canon would contain contexts that are simple enough to avoid distracting from the software issues yet complex enough to serve as a proxy for the
contexts commonly encountered in scientic research. The chosen ones involve solving coupled ordinary differential equations, coupled integro-differential equations,
and a partial differential equation. All exhibit nonlinearity. In contexts involving temporal dependence, the time integration algorithms applied in these contexts range
from straightforward explicit Runge-Kutta schemes to multistep, semi-implicit, and
fully implicit schemes. In contexts involving spatial dependence, the nite difference methods employed likewise range from the explicit and straightforward to the
implicit and more complicated. Three important complications missing from our
canonical contexts are addressed in Part III of this text: complicated geometry and
boundary conditions and runtime scalability.
Section 4.3.1 provides the context for Chapters 68. Section 4.3.2 provides the
context for Chapter 5. Section 4.3.3 provides the context for Chapter 9.
(4.1)
{U1 , U2 , ..., Un }T is the problem state vector; x and t are coordinates in the
where U
space-time domain (0, T]; and F {F1 , F2 , ..., Fn }T is a vector-valued operator
that couples the state vector components via a set of governing ordinary-, partial-,
or integro-differential equations. Closing the equation set requires specifying
appropriate boundary and initial conditions:
U)
= C(
x, t), x
B(
(4.2)
x, 0) = U0 (x), x
U(
(4.3)
U)
typically represents linear or nonlinear combinations of U
where bounds , B(
species the values of those combinations on .
and its derivatives, and where C
A common step in solving equation (4.1) involves rendering its right-hand side
discrete by projecting the solution onto a nite set of trial-basis functions or by
replacing all spatial differential operators in F by nite difference operators and all
spatial integral operators in F by numerical quadratures. Often, one also integrates
equation (4.1) against a nite set of test functions. Either process can render the
spatial variation of the solution discrete while retaining its continuous dependence
on time. One commonly refers to such schema as semidiscrete approaches. The
resulting equations take the form:
d
V(t))
V(t) = R(
t (0, T]
dt
(4.4)
(v2 v1 )
v
d 1
=
v1 ( v3 ) v2 .
v2
dt
v3
v1 v2 v3
(4.5)
101
102
40
30
20
10
0
20
10
0
10
20
20
(x,y,z)
from linearity. The parameter characterizes the relative magnitudes of the diffusion of momentum to that of thermal energy, whereas determines the stability of
buoyancy-driven motions and relates to the wavelength of the Fourier modes retained in the discretization. When > +1, solutions are unstable at sufciently high
values (> 24.74). Such behavior indicates turbulence in the original ow problem.
Explicit Euler time advancement of the Lorenz equations takes the form:
n+1 = V
n + t R
V
n
V
(4.6)
n+1 are the solution vectors at times tn and tn+1 tn +t, respectively,
n and V
where V
and where R is the RHS of equation (4.5). Figure 4.3 depicts the behavior of an
explicit Euler time integration of the Lorenz system in phase space that is, in v1 -v2 v3 space. The buttery-shaped geometrical object toward which the initial solution
trajectory evolves is a strange attractor (so called because it possesses a fractional, or
fractal, spatial dimension) characteristic of a variety of forced, nonlinear, dissipative
systems. In the current case, the forcing stems from the energy input required to
maintain the overall thermal gradient.
(4.7)
v=
4
S r3
where S = S( , t) traces the vortex lament in 3D space as a function of arc-length
and time t, denotes a vector product, and the integration is performed along all
vortex laments in the simulation. The superuid velocity is thought to arise from
the motion of atoms at the ground energy level. The remaining excited atoms move
with a macroscopic velocity u that can be accurately described by classical mechanics
(see section 12.2.1).
In the absence of any background superuid motion imposed by boundary or
initial conditions, the equation of motion for a point S on a lament is:
dS
= v S (u v) S [S (u v)]
dt
(4.8)
where S dS/d and where and are temperature-dependent constants. The rst
term on the RHS is the total velocity induced by other points on the same lament
and other laments. At absolute-zero temperature, with all the atoms in the ground
state, this would be the only term on the RHS. The rst RHS term causes a circular
loop, such as that of Figure 4.4, to propagate in a direction orthogonal to the plane
of the loop. The second and third RHS terms model the effect of mutual friction
To be inserted
To be deleted
S(,t )
S'
103
104
Figure 4.5. Quantum vortex lament before (left) and after (right) reconnection. Curved
arrows indicate direction of circulation.
between the superuid component and the excited atoms, which comprise a normal
uid occupying the same volume. For a circular loop, the second term causes the
vortex to shrink as it propagates. For the same loop, the third term, which is typically
the smallest in magnitude, imposes a drag that opposes the induced velocity.
One renders the continuous vortex core discrete by placing a series of mesh
points along each lament as in Figure 4.4. Writing equations (4.7)(4.8) for each
mesh point yields a coupled set of nonlinear, integro-differential equations. The
motions of these mesh points determines the lament motion and shape.
Two processes change the connectivity of the points. First, as the distance
between mesh points changes, new points must be inserted and existing points
removed to balance resolution requirements against computational cost and complexity (Morris 2008). Second, when two laments approach one another, their
interaction tends to draw them closer, resulting in antiparallel vortices (Schwarz
1985, 1988). Koplik and Levine (1993) showed that the laments join and reconnect
whenever two antiparallel vortex laments approach within a few core diameters of
each other, as demonstrated in Figure 4.5.
Broadly speaking, numerical solution of the discretized vortex equation system
breaks down into three pieces. One is interpolating the normal uid velocity u at each
superuid vortex point S. A second is evaluating the Biot-Savart integral. A third is
evaluating the parametric spatial derivative S . Morris (2008) tackled these problems
in a dissertation on superuid turbulence. Appendix A details those aspects of her
numerical methods that appear in the Exercises at the end of Chapter 5.
(4.9)
which we write in the subscript notation of Table 3.1. Although Burgers (1948) introduced this equation as a simplied model for uid turbulence, in which setting
u plays a role analogous to velocity and to kinematic viscosity, the equation also
crops up in settings ranging from superconductivity to cosmology (see Canuto et al.
2006).
Despite its apparent simplicity, solving the Burgers equation presents sufciently
vexing challenges to warrant exibility: A numerical method that works in one regime
across a given range of the parameter nu for a given set of initial and boundary conditions might not work well in other regimes. Great value accrues from ensuring the
ability to swap numerical methods within a single solver framework. Furthermore,
the dynamics can change sufciently even within a single solution time interval to
warrant dynamic adaptivity. In particular, the nonlinear second term on the LHS
of equation (4.9) causes solutions to develop steep gradients characteristic of the
nearly discontinuous shock waves that occur in settings as widely disparate as supernovae and rocket plumes. These might necessitate locally adaptive, shock-capturing
schemes in the vicinity of the steep gradients (Berger and Colella 1989), while less
demanding discretizations could be used elsewhere in space and time as the RHS
term dissipates these gradients.
The perils of approximating the Burgers equation arise partly from oscillating
instabilities related to bifurcations in the behavior of the discretized versions of the
equation. For small , solutions near these bifurcations display a rich variety of
behaviors. These include limit cycles (asymptotic approaches periodicity in time)
and strange attractors (Maario et al. 2007).
An additional attraction to including the Burgers equation in our canon stems
from its unusual status as one of the few nonlinear PDEs with known exact solutions.
Hopf (1950) and Cole (1951) demonstrated that transforming the Burgers equation
according to:
u = 2x /
(4.10)
converts it into the linear heat equation:
t = xx .
(4.11)
4 n=
e(x2 n)
2 /(4t)
(4.12)
in the case of periodic boundary conditions. Figure 4.6 plots an initial condition
u(x, 0) = 10 sin(x) along with exact and approximate periodic solutions on the interval
[0, 2). The numerical solution integrates the conservative form:
ut = uxx (u2 /2)x
(4.13)
so called because discretizations of the RHS conserve the integral of u2 /2 over the
domain when = 0. One solution in the gure employs second-order, Runge-Kutta
time advancement and sixth-order accurate Pad scheme for spatial differencing (Moin 2001). Appendix A summarizes these two algorithms, the advantage of
using the high order nite difference scheme is to achieve spectral-like accuracy in
space. Another solution plotted in the same gure, obtained with a second-order
central difference scheme for spatial discretization, is also ploted in Figure 4.6 for
105
8
6
4
2
u
106
0
2
4
6
8
10
3
x
Figure 4.6. Solutions of the Burgers equation with initial condition u = 10 sin x and periodic
boundary conditions.
comparison. With only 16 grid points, the sixth-order Pad scheme leads to more
accurate results than the second-order central difference scheme.
Yes We Canon
Simple pattern demonstrations run the risk of missing important complications
found in actual scientic codes. Complicated ones run the risk of losing the forest
in the trees. We have constructed a small canon of relatively simple problems
that exhibit some of the chief complexities found in more complicated scientic
simulations.
EXERCISES
1. Write an abstract vortex_filament derived type in Fortran with a deferred
remesh binding that implements an abstract interface.
2. Write a pure virtual VortexFilament class derived type in C++ with a pure virtual
remesh member function.
3. List recurring themes you see in buildings you like. Are there analogies you can
make to software architecture?
Memory is a crazy woman [who] hoards colored rags and throws away food.
Austin OMalley
108
classes. Several reasons motivate implementing OBJECT as a unidimensional, hierarchical collection of classes rather than a single class. First, when the desired common
functionality involves unrelated methods, collecting them into one class results in
the coincidental cohesion, the weakest and least desirable form (see Section 3.2.1).
A more understandable and maintainable design results from separating concerns.
Second, any deferred bindings in the parent type (base class) hierarchy must
be implemented in all concrete classes in the project. The OBJECT pattern should
therefore be extremely stable. Only deferred bindings expected to survive for the life
of the project should be included. Nonetheless, if some deferred bindings seem likely
to be removed later, placing these deeper in the ancestral hierarchy inoculates the
child classes from subsequent deletion of the relevant ancestor, because the classes
only reference the ultimate descendant that is, ultimate from viewpoint of the
OBJECT hierarchy. Declarations of instances of this ultimate descendant elsewhere in
the package need not be changed when more distant ancestral relatives are removed.
Since efcient use of HPC platforms usually requires a sophisticated understanding of the underlying architecture and the chosen parallel programming paradigm,
we defer further discussion of HPC to Part III. There we exploit the C interoperability features of Fortran 2003 to construct a mixed Fortran/C++, object-oriented,
scalable ADT calculus. That calculus circumvents many of the details required for
more traditional approaches ensuring runtime scalability.
Here we focus on an example involving the bane of every programmer whose
project requires considerable use of dynamic memory allocation: memory leaks. By
introducing allocatable entities, Fortran relieves the programmer from thinking
about leaks in a large fraction of the cases where they could occur in C++. C++ has a
long-standing reputation of difcult memory management relating to exposing lowlevel entities directly to the programmer specically, exposing machine addresses
via pointers. Many partial solutions exist (standard container classes, management
via lifetime of stack allocated objects, reference counting, garbage collectors, etc.),
but the current language standard does not provide any general, portable, or efcient
solution to the problem of managing object lifetimes. Section 5.2.2 presents an invasive reference-counting scheme, but in the absence of a language-provided solution,
this approach will almost certainly prevent a compiler from eliminating temporaries
and a host of other optimizations.
Another approach puts each developer in charge of managing memory but gives
them tools that assist them in doing so. Although the use of allocatable entities
in Fortran obviates the need for developers to explicitly free memory in the vast
majority of circumstances, at least three scenarios warrant giving programmers this
responsibility. First, as recently 2006, three of ve compilers tested exhibited memory leaks resulting from not freeing memory in the manner in which the Fortran
2003 standard obligates them (Rouson et al. 2006). Until widespread availability of
leak-free compilers becomes the norm, developers will often need to adopt defensive postures. Second, as recently as December 2009, only two of seven compilers
surveyed supported the nal subroutines the standard provides for programmers to
instruct the compiler as to how to free memory automatically when a pointer component goes out of scope (Chivers and Sleightholme 2010). Third, programmers might
want to explicitly free memory before the associated entity goes out of scope.
109
110
The exibility and power of allocatable entities for automatic dynamic memory management makes the need for pointers (and hence nal subroutines) quite rare
in Fortran. The quantum vortex lament discretization of Chapter 4, however, maps
quite nicely onto a linked list data structure with pointer components. Figure 4.4 naturally evokes notions of translating each mesh point into a node in the list and each
line segment into a pointer or bidirectional pair of pointers. We use this example in
the remainder of this section.
111
112
The Fortran standard obligates compilers to nalize an object whenever a programmer deallocates it as line 48 does.2 This makes finalize inherently recursive
as indicated by the recursive attribute on line 43. This artice neatly provides for
automatic traversal and destruction of the entire linked list in the three executable
lines 4648.
The language semantics harbor equally subtle and elegant implications for
manual_finalizer. When one invokes this procedure to free memory in advance
of when a compiler might otherwise do so automatically via the nal subroutine, it
is critical to ensure that the two procedures do not interact in harmful ways. This
issue vanishes with compilers that do not support nal subroutines. For these, the
programmer must remove the final binding at line 15 manually or automatically,
for example via Fortrans conditional compilation facility or Cs preprocessor. In
such cases, manual_finalizer breaks the chain in the same manner as finalizer
and traverses the vortex, deallocating each node until it returns to the broken link.
When the compiler supports nal subroutines, the deallocate command at
line 58 implicitly invokes finalizer, after which the destruction process continues
recursively and automatically, making only one pass through the do while block
that begins at line 56. The automatically invoked nal subroutine and the explicitly
invoked manual destruction procedure peacefully coexist.
Figure 5.4 demonstrates vortex construction, output, and destruction. Exercises
at the end of this chapter guide the reader in writing code for point insertion, removal,
and time advancement via ADT calculus. Line 5 in Figure 5.4 marks the rst Fortran pointer assignment in this text. Were the constructor result produced by the
RHS of that line assigned to a LHS object lacking the pointer attribute, an intrinsic
assignment would copy the RHS into the LHS and then deallocate the RHS. This
would start the aforementioned nalization cascade and destroy the entire ring! The
pointer assignment prevents this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Figure 5.1(a)
module Rmat_module
implicit none
type Rmat
real ,dimension(:,:) ,pointer :: matrix=>null()
contains
procedure :: total
procedure :: assign
generic
:: operator(+) => total
generic
:: assignment(=) => assign
end type
contains
type(Rmat) function total(lhs,rhs)
class(Rmat) ,intent(in) :: lhs,rhs
allocate(total%matrix(size(lhs%matrix,1),size(lhs%matrix,2)))
total%matrix = lhs%matrix + rhs%matrix
2 Although the standard does not specify whether nalization or deallocation comes rst, it seems
reasonable to assume compilers will nalize rst to avoid breaking nal subroutines that assume the
object remains allocated upon invocation, and to prevent memory leaks that would result if there
remained no handle for a pointer component still associated with a target.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
end function
subroutine assign(lhs,rhs)
class(Rmat) ,intent(inout) :: lhs
type(Rmat) ,intent(in) :: rhs
lhs%matrix = rhs%matrix
end subroutine
end module
Figure 5.1(b)
program main
use Rmat_module
implicit none
integer ,parameter :: rows=2,cols=2
type(Rmat) :: a,b,c
allocate(a%matrix(rows,cols))
allocate(b%matrix(rows,cols))
allocate(c%matrix(rows,cols))
a%matrix=1.
b%matrix=2.
c = a + b ! memory leak
call foo(a+b,c)
contains
subroutine foo(x,y)
type(Rmat) ,intent(in) :: x
type(Rmat) ,intent(inout) :: y
y=x
! memory leak
y=x+y
! memory leak
end subroutine
end program
Figure 5.1. Memory leak example adapted from Stewart (2003).
1
2
3
4
Figure 5.2
module hermetic_module
implicit none
private
public :: hermetic ! Expose type and type-bound procedures
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
abstract interface
subroutine final_interface(this)
import :: hermetic
class(hermetic) ,intent(inout) :: this
end subroutine
end interface
113
114
contains
subroutine SetTemp(this)
class(hermetic) ,intent(inout) :: this
if (.not. associated(this%temporary)) allocate(this%temporary)
this%temporary = 1
end subroutine
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
subroutine CleanTemp(this)
class(hermetic) :: this
if (associated(this%temporary)) then
if (this%temporary > 1) this%temporary = this%temporary - 1
if (this%temporary == 1) then
call this%force_finalization()
deallocate(this%temporary)
end if
end if
end subroutine
end module
Figure 5.2.
1
2
3
4
5
OBJECT
Figure 5.3
module vortex_module
use hermetic_module ,only : hermetic
implicit none
private
public :: vortex
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
interface vortex
procedure ring
end interface
21
22
23
24
25
26
27
contains
function ring(radius,num_points)
real ,intent(in) :: radius
integer ,intent(in) :: num_points
type(vortex) ,pointer :: ring,current
integer :: i
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
subroutine manual_finalizer(this)
class(vortex) ,intent(inout) :: this
type(vortex) ,pointer :: current,next
current => this%next
this%next=>null() ! break the chain so loop terminates
do while(associated(current%next))
next => current%next ! start after break in chain
deallocate(current)
current=>next
end do
end subroutine
62
63
64
65
66
67
68
69
70
71
72
73
subroutine output(this)
class(vortex) ,intent(in) ,target :: this
type(vortex) ,pointer :: current
current => this
do
print *,current%point
current => current%next
if (associated(current,this)) exit
end do
end subroutine
end module
Figure 5.3. Quantum vortex linked list with hermetic memory management.
1
2
3
4
5
program main
use vortex_module ,only : vortex
implicit none
type(vortex) ,pointer :: helium
helium => vortex(radius=1.,num_points=8)
115
116
call helium%output()
call helium%force_finalization()
end program
Figure 5.4. Vortex creation and destruction.
Hermetic
Vortex
Figure 5.5. The concrete Vortex ADT implements the Hermetic interface.
employ reference counting to obtain functionality similar to garbage collection. Reference counting refers to the strategy of keeping track of the number of references
or pointers that refer to the memory associated with an object, so the memory can be
freed when it is no longer referenced. In practice, reference counting helps prevent
the aforementioned memory leaks in applications that rely frequently on pointers.
The signicant risk of memory leaks associated with raw pointers combined with
the ubiquitous need for pointers in C++ makes a reference-counted pointer (RCP)
class an ideal candidate for incorporation into an OBJECT pattern. Figure 5.6 contains
a RCP implementation based on C++ template classes. Figure 5.6(a) shows a basic
RCP template class pointer<> denition. It wraps raw C++ pointers and thereby
facilitates treating objects as if one were using a C++ pointer directly.
1
2
#ifndef POINTER_H_
#define POINTER_H_
3
4
5
6
/**
* A simple wrapper for raw pointer.
*/
7
8
9
#include <iostream>
#include <unistd.h>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
117
118
41
42
43
44
45
46
47
1
2
private:
T *ptr_;
};
#endif
#ifndef REF_H_
#define REF_H_
3
4
5
6
/**
* A very simple invasive reference counted pointer.
*/
7
8
#include "pointer.h"
9
10
11
12
13
14
15
Ref() {}
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Ref() {
if(ptr() != NULL) ptr()->release();
}
37
38
39
40
41
42
43
44
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
1
2
#ifndef REFBASE_H_
#define REFBASE_H_
3
4
5
6
/**
* Base for reference counted objects.
*/
7
8
#include "Ref.h"
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class RefBase {
public:
RefBase() : cnt_(0) {}
RefBase(const RefBase &) : cnt_(0) {}
virtual RefBase() {}
void grab() throw() {
cnt_++;
}
void release() throw() {
cnt_--;
if(! cnt_) delete this;
}
private:
int cnt_;
};
#endif /* ! REFBASE_H_ */
Figure 5.6. Reference counting in C++: (a) a template pointer base class for the referencecounted class Ref, (b) a template class for the reference-counted pointer Ref, and (c) a
reference-counted object RefBase base class .
We use the RCP class from Figure 5.6(b) and the reference-counted object class of Figure 5.6(c) for the C++ examples throughout this book. The
current implementation grew out of C++ sample code that rst appeared in
Rouson, Adalsteinsson, and Xia (2010). The presentation here provides more
119
120
A pointer<T> object can be constructed a few different ways: It can be initialized as a NULL by default or it can be initialized by a pointer. We rely on the
default copy constructor generated by the compiler. Were we to dene a copy
constructor, it would behave identically to the compiler-generated one.
pointer() : ptr_(NULL){}
// default constructor
pointer(T *other) : ptr_(other){} // takes a raw pointer
Assignment operators.
The assignment operators for pointer<> are overloaded to have a few different
forms:
pointer& operator=(T * other);
// RHS is a T*
const pointer& operator=(const T&); // RHS is const T&
pointer& operator=(T & other);
// RHS is T&
As with the constructors, we provide no copy assignment operator and rely on
the compiler to generate the correct behavior.
Member access
The member access operators are dened so that any pointer<T> object is
treated just as a regular pointer. Consider the following declarations of the operators -> and *. We can illustrate their usage with the following example: Given
a declaration pointer<T> x;, one can use pointer syntax such as x->foo() or
(*x).foo(), assuming type T has a member function named foo():
operator ->:
T* operator->();
const T* operator->() const;
operator *:
T& operator*();
const T& operator*() const;
Pointer comparison.
Since the pointer<> class emulates actual pointer behavior, we also need to
overload the comparison operators == and != so that they are actually comparing pointer addresses. Continuing with the previous example of object x, the
expression x == NULL tests if x contains a NULL pointer. Similarly, x != y returns true if (the address of) the memory associated with x is not the same as
that associated with y:
bool operator==(const T *other) const;
bool operator!=(const T *other) const;
Pointer access.
The destructor, dened as Ref<>, decreases the reference count when a RCP
goes out of scope. As shown in Figure 5.6(b), the destructor invokes the method
release() to decrement the reference count by one. If the reference count
reaches zero, then all pointers have reached the end of their lifetimes and the
memory to which the pointer points is freed. The RefBase class denes the
grab() and release()implementations as discussed after the next bullet:
Ref();
Assignment operators.
The assignment operators manage the reference counts for both the LHS and
RHS. As shown below, these operators take an approach that differs slightly
from the copy constructors. One can envisage the process of assigning a Ref<>
pointer comprising disassociating the LHS pointer followed by the reassociating
it with the RHS pointer. The implementations of the assignment operators reect
this process: release the LHS pointer rst then associate (the address of) the LHS
121
122
pointer to the RHS pointer, and nally grab() the LHS pointer to update the
reference count on the pointer:
// assignment operators:
Self_t& operator=(const Self_t&); // RHS is a Ref<T>
Self_t& operator=(T *other);
// RHS is T*
A helper function named cast() is also dened for Ref<>. As implied by its
name, this function typecasts between two Ref<> pointers with different parameter
types using C++s dynamic_cast. The function is used in Chapter 68 when type cast
is actually needed.
The class denition of RefBase, as shown in Figure 5.6(c), provides a base class
for any object that needs reference counting. This class implements the method
grab() and release(). The actual number of references to this object is stored by
the attribute cnt_. A careful reader might have noticed the absence of keyword
virtual in denitions of grab() and release(). This is intentional. It prevents
subclasses of RefBase from overriding these methods and potentially breaking the
reference counting scheme.
The combination of Ref<> and its parameter class RefBase closely corresponds to
the hermetic type dened in Fortran. We also use Ref<> and RefBase in Chapters 68
to emulate Fortrans rank-one allocatable arrays.
5.2.2.2 Output Format for Floating Point Quantities
The I/O formatting differences between C++ and Fortran warrant a format conversion tool to facilitate direct comparison between the two languages output. Since all
of our Fortran examples use list-directed output for oating point arrays, we developed a helper class to handle the C++ oat output format. Figure 5.7(a) denes this
class fmt. It stores two parameters, w_ and p_, for controlling the output width and
precision (the number of signicant digits in the output eld). Users can choose a
desired width and precision when constructing a fmt object. By default, the output
width is 12 characters wide and with 8 signicant digits, matching roughly that in
Fortran for a single oat4 .
The internal member v_, which is of C++ STLs vecotr<float> type, stores the
actual value of the output item. Figure 5.7(b) gives the detailed type denitions of
real_t and crd_t. To simulate the list-directed output format for a Fortran real
(C++ float) array, such as that produced by print *, X, the C++ operator <<
is overloaded, as shown from line 18 to 29 in fmt.h. Hence, the C++ analogue to
the aforementioned Fortran print statement is std::cout << fmt(X);.
directed output formatting is controlled by compilers, which choose appropriate width and precisions
depending on the data type of the output item list.
ring, the vortex class requires reference counting. This is readily seen by the fact
that class vortex extends RefBase. Similar to the Fortran denition, the C++ vortex
stores the vortex coordinates using an array of oats. Member variable next_, dened
as type Ref<vortex>, replaces a raw C++ pointer in the linked list node structure.
Since next_ does the reference counting on its own, it is not necessary to dene any
functionality for the destructor as is evident in the code at lines 89 in Figure 5.8(b)5 .
In the class denition of vortex, we include a typedef iterator to enable
node traversal for insertion, deletion, update, or query. Since an iterator normally
requires no reference counting when iterating through the vortex ring, we use type
pointer<Ref<vortext>>. The constructor:
vortex(real_t radius, int num_points);
creates a vortex ring with the input radius and number of points, providing the same
functionality as ring() in Figure 5.3.
The method destroy() initiates the vortex ring destruction. It may seem odd
that vortex has an empty destructor while an additional function call is needed to
initiate its destruction. Its necessity derives from the rings circular structure: Its tail
points to its head. Upon creation, there are two references to the head: one held
by the external user and another that is the vortex rings tail. Hence, the number of
references to the head does not reach zero when the external user no longer needs
the ring.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
123
124
1
2
}
// Restore original format flags.
os.flags(flags);
return os;
}
#endif //! _H_FMT_
#ifndef _H_GLOBALS_
#define _H_GLOBALS_
3
4
5
6
#include "Ref.h"
#include "RefBase.h"
#include <vector>
7
8
9
10
11
#endif //!_H_GLOBALS
Figure 5.7. Output tools: (a) C++ emulation of Fortrans list-directed output and (b) global
header le dening real_t and crd_t.
1
2
#ifndef _VORTEX_H_
#define _VORTEX_H_
3
4
5
6
#include "RefBase.h"
#include "globals.h"
#include <vector>
7
8
9
10
11
12
13
14
15
virtual vortex();
16
17
void destroy();
virtual void output() const;
18
19
20
21
22
23
24
};
private:
crd_t
ptr_t
25
26
#endif
point_;
next_;
1
2
3
#include <cmath>
#include "vortex.h"
#include "fmt.h"
4
5
6
vortex::vortex()
{}
7
8
9
vortex::vortex()
{}
10
11
12
13
14
15
16
void vortex::destroy()
{
// the following line start destruction of the vortex ring
// breaks the ring and triggers the chain action
this->next_ = NULL;
}
17
18
19
20
21
22
23
24
delta = 2.0f*pi*radius/real_t(num_points);
25
26
27
28
this->point_.push_back(radius);
this->point_.push_back(0.0f);
this->point_.push_back(0.0f);
29
30
31
32
current = this->next_;
33
34
35
36
37
38
39
40
temp_point.push_back(radius*cos(theta));
temp_point.push_back(radius*sin(theta));
temp_point.push_back(0.0);
41
42
43
44
(*current)->point_ = temp_point;
45
46
47
48
49
50
current = (*current)->next_;
51
52
53
54
125
126
56
57
58
59
60
61
tmp = this->point_;
62
63
64
65
current = this->next_;
66
67
do
{
68
69
70
71
tmp = (*current)->point_;
std::cout << fmt(tmp) << std::endl;
72
73
74
75
1
2
current = (*current)->next_;
} while (*current != this);
#include <iostream>
#include "vortex.h"
3
4
5
6
int main ()
{
typedef vortex::ptr_t ptr_t;
8
9
helium->output();
helium->destroy();
10
11
12
13
14
return 0;
15
127
128
articial beginning and ending points by breaking a link as Section 5.2 demonstrates
in the context of object destruction, doing so merely to traverse the list would be
overkill at best and catastrophic at worst, given the possibility of starting the destruction cascade. A second approach might involve storing references to arbitrarily
chosen beginning and ending objects. We opted to avoid these approaches in favor
of simplicity.
EXERCISES
1. Draw a UML sequence diagram for a three-point ring undergoing the object
destruction cascade described in Section 5.2.
2. Write a remeshable_vortex implementation that extends the vortex ADT,
adding an insert point insertion procedure and a remove point removal
procedure.
3. Add the requisite operators in the vortex type to facilitate time advancement
via ADT calculus based on the local induction approximation.
130
(a)
Integrand
+ add(Integrand,Integrand) : Integrand
+ multiply(Integrand,real) : Integrand
+ t(Integrand) : Integrand
(b)
Integrand
Lorenz
(c)
Figure 6.1. Lorenz system integration: (a) integration package (Integrator), (b) abstract
Integrand interface, and (c) class diagram.
class and the Integration procedure. Figure 6.1(b) depicts the Integrand class interface. It contains the addition, multiplication, and time differentiation operations
required to implement Runge-Kutta marching schemes, in which context authors
often refer to the time differentiation operation as function evaluation because it
in equation (4.4).
involves evaluating the RHS function in an ODE system, namely R
UML depicts abstract operation names such as add and multiply in italics and abstract
class names such as Integrand in bold italics.
131
132
integrand objects, multiplication of an integrand by a real scalar, time differentiation of an integrand, and the assignment of one integrand to another. The
integrate procedure accepts a polymorphic object along with a real, scalar time
step and uses the aforementioned operators to overwrite the object with a version
of itself one time step in the future. It arrives at the future time step instance via the
explicit Euler algorithm expressed in ADT calculus. Figure 6.2(c) denes the lorenz
type, which implements a constructor along with each of the deferred bindings and
an output method.
The Lorenz system code in Figure 6.2(c) implements an ADT calculus much like
that presented in Chapter 3. Whereas ADT calculus decouples the client code (the
code that integrates lorenz over time) from the lorenz implemenation, ABSTRACT
CALCULUS goes one step further: It decouples the client code from depending even on
the lorenz interface at least insofar as the public types and type-bound procedure
interfaces comprise a concrete interface. Instead, the integration code depends only
on an abstract interface specication to which the concrete interface conforms.
1
2
3
Figure 6.2(a)
program main
use lorenz_module ,only : lorenz,integrate
implicit none ! Prevent implicit typing
4
5
6
7
8
9
10
11
12
type(lorenz)
:: attractor
integer
:: step ! time step counter
integer ,parameter :: num_steps=2000, &
space_dimension=3 ! phase space dimension
real
,parameter :: sigma=10.,rho=28.,beta=8./3.,&
dt=0.01 ! Lorenz parameters and time step size
real
,parameter ,dimension(space_dimension) &
:: initial_condition=(/1.,1.,1./)
13
14
15
16
17
18
19
20
1
2
3
4
attractor = lorenz(initial_condition,sigma,rho,beta)
print *,attractor%output()
do step=1,num_steps
call integrate(attractor,dt)
print *,attractor%output()
end do
end program
Figure 6.2(b)
module integrand_module
implicit none
! Prevent implicit typing
private
! Hide everything by default
public :: integrate ! expose time integration procedure
5
6
7
8
9
10
11
12
::
::
::
::
t
add
assign
multiply
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
abstract interface
function time_derivative(this) result(dState_dt)
import :: integrand
class(integrand) ,intent(in) :: this
class(integrand) ,allocatable :: dState_dt
end function time_derivative
function symmetric_operator(lhs,rhs) result(operator_result)
import :: integrand
class(integrand) ,intent(in) :: lhs,rhs
class(integrand) ,allocatable :: operator_result
end function symmetric_operator
function asymmetric_operator(lhs,rhs) result(operator_result)
import :: integrand
class(integrand) ,intent(in) :: lhs
class(integrand) ,allocatable :: operator_result
real
,intent(in) :: rhs
end function asymmetric_operator
subroutine symmetric_assignment(lhs,rhs)
import :: integrand
class(integrand) ,intent(in)
:: rhs
class(integrand) ,intent(inout) :: lhs
end subroutine symmetric_assignment
end interface
41
42
43
44
45
46
47
48
1
2
3
contains
subroutine integrate(model,dt)
class(integrand) :: model
real ,intent(in) :: dt
! time step size
model = model + model%t()*dt ! Explicit Euler formula
end subroutine
end module integrand_module
Figure 6.2(c)
module lorenz_module
use integrand_module ,only : integrand,integrate
implicit none
4
5
6
7
private
! Hide everything by default
public :: integrate ! Expose time integration procedure
public :: lorenz
8
9
10
11
12
13
14
15
16
17
133
134
20
21
22
23
interface lorenz
procedure constructor
end interface
24
25
contains
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
allocate(delta)
allocate(delta%state(size(this%state)))
! 1st lorenz equation
delta%state(1)=this%sigma*( this%state(2) -this%state(1))
! 2nd lorenz equation
delta%state(2)=this%state(1)*(this%rho-this%state(3))-this%state(2)
! 3rd lorenz equation
delta%state(3)=this%state(1)*this%state(2)-this%beta*this%state(3)
! hold Lorenz parameters constant over time
delta%sigma=0.
delta%rho=0.
delta%beta=0.
call move_alloc (delta, dState_dt)
end function
63
64
65
66
67
68
69
70
71
72
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
allocate (local_product)
local_product%state = lhs%state*rhs
local_product%sigma = lhs%sigma*rhs
local_product%rho
= lhs%rho *rhs
local_product%beta = lhs%beta *rhs
call move_alloc(local_product, product)
end function
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
select type(rhs)
class is (lorenz)
lhs%state = rhs%state
lhs%sigma = rhs%sigma
lhs%rho
= rhs%rho
lhs%beta = rhs%beta
class default
stop assign_lorenz: rhs argument type not supported
end select
end subroutine
end module lorenz_module
Figure 6.2. Fortran implementation of ABSTRACT CALCULUS applied to a Lorenz system:
(a) Integration test for the Lorenz system; (b) Integrand class; (c) Lorenz class.
Figure 6.2(c) represents the rst occurrence of the select type construct and
move_alloc intrinsic procedure in this text. A conuence of circumstances likely to
occur in most abstract calculi necessitates these language facilities. First, the deferred
binding add in the abstract type integrand in Figure 6.2(b) species an abstract
interface symmetric_operator that forces procedures implementing the deferred
binding to accept a dummy argument declared as class(integrand). This restriction
cannot apply to the passed-object dummy argument lhs, which must be declared as
class(lorenz) in order for the compiler to differentiate this implementation of the
135
136
deferred binding from other derived types implementations. The restriction applies
to the other argument rhs.
Inside the select type construct, the code accesses lorenz components not
included in the integrand abstract type specication. Since this code would fail for
any extensions of integrand that do not contain these components, the language
semantics require guarding against this possibility. Although the type determination
happens at runtime, the class is(lorenz) type guard statement ensures that the
guarded code only executes when rhs is a lorenz object or an instance of a type that
extends lorenz and therefore contains the required components.
Were add_Lorenz to compute its result (sum) directly, an additional (nested)
type guard statement would be required to ensure that the result (delcared as
class(integrand)) has the requisite components. The desired result type is known,
so this level of type guarding feels somewhat superuous even though the deferred
binding requires it. We therefore eliminate the need for it by declaring and computing a local_sum of type lorenz and transferring its allocation to the actual result
sum via the move_alloc intrinsic procedure. After the move_alloc invocation at the
end of add_Lorenz, sum will be associated with the memory originally associated
with local_sum, whereas the latter will be unallocated. This move_alloc invocation
ensures that the chosen method for avoiding extra type guarding does not impose
additional dynamic memory utilization. For this reason, the move_alloc intrinsic
will also play a signicant role in memory recycling in Chapter 10.
In Fortran, the select type construct provides the only means for declared runtime type conversions because the language contains no explicit type casting facility.
Similar to a select case construct, a select type allows a programmer to specify
a list of possible blocks of code to execute based on the possible dynamic types of
the object at runtime. Each constituent block is bound with a statement, which can
be type is or class is, specifying a unique data type. A special type guard statement, class default, also provides an error-handling facility in case all specied
types fail to match the dynamic type of the object1 . The Fortran 2003 standard also
guarantees that, at most, one of the constituent blocks executes. If none of the blocks
is selected and no class default statement is provided, the program simply exits
the construct quietly and continues to subsequent lines. Although the select type
construct guards against crashes, it remains the programmers responsibility to insert
a class default case to detect and handle the scenario in which no block is selected.
In C++, the corresponding technique involves attempting a dynamic_cast to
some useful type. If the ultimate type proves incompatible with the type being
cast, however, dynamic_cast either returns NULL for casts involving a pointer or
throws an exception for casts involving a reference. Handling such exceptions necessitates a conditional test (e.g., an if statement) or a exception-handling code (i.e., a
try-catch block). This design approach suffers from fragility: It relies heavily on an
assumption that all programmers will use safe practices. Neglecting the conditional
or the exception handling can lead to catastrophic results wherein the code crashes
or an incorrect result goes undetected until some later (and potentially distant) point
in the code.
1 Although C++ programmers commonly decry the lack of exception handling in Fortran, this scenario
exemplies the general rule that exception handling is less necessary in Fortran than in C++.
Even though both language constructs rely on runtime type information (RTTI)
and both have pitfalls, a select type, in general, is safer than a dynamic_cast in
that a select type only performs a downcast along the class hierarchy tree based
upon the actual dynamic type (the runtime type) of the object or pointer. Downcasts
are the most common situations arising from need for explicit type changes (casts)
so that the programmer can access additional data members and functions of an
extended type. In C++, dynamic_cast operators can be used for downcasts as well
as upcasts. A common casting error occurs when one casts to a type on a different
branch of an inheritance hierarchy2 . The distinction between the two constructs is
also apparent from the language design point of view: C++s dynamic_cast relies
solely on programmers knowledge of the actual runtime type to which an object
is cast, whereas Fortrans select type asks programmers to supply a list of possible dynamic types with corresponding execution blocks to be selected. Therefore,
dynamic_cast is more exible to use at the cost of placing a greater number of
responsibilities on programmers.
1
2
3
#include "lorenz.h"
#include "fmt.h"
#include <iostream>
4
5
6
7
int main () {
using namespace std;
typedef lorenz::ptr_t ptr_t;
8
9
10
11
const int
num_steps=2000, space_dimension=3;
const float sigma=10, rho=28, beta=8.0/3.0, dt=0.01;
const crd_t initial_condition(space_dimension, 1.0);
12
13
14
15
ptr_t
16
17
18
try {
std::cout << fmt(output, 12, 10) << "\n";
2 An upcast followed by a downcast can end up with cross-branch casting.
137
138
1
2
3
4
#include "globals.h"
5
6
7
8
9
10
virtual integrand();
11
12
13
14
15
16
17
18
19
protected:
integrand(const integrand&);
integrand();
};
20
21
22
23
#endif
#include "integrand.h"
2
3
4
5
6
integrand::integrand() : RefBase() {
}
7
8
9
10
11
12
13
integrand::integrand() {
}
1
2
#ifndef __H_LORENZ__
#define __H_LORENZ__ 1
3
4
#include "integrand.h"
5
6
7
8
9
10
11
12
13
14
15
16
public:
integrand::ptr_t
void
integrand::ptr_t
d_dt() const;
operator+=(integrand::ptr_t other);
operator*(float val) const;
17
18
const crd_t&
output() const;
19
20
virtual lorenz();
21
22
23
24
25
private:
crd_t state_;
// solution vector.
float sigma_, rho_, beta_;
};
26
27
1
2
#endif
#include <iostream>
#include <exception>
3
4
#include "lorenz.h"
5
6
7
8
9
10
11
12
13
14
// default constructor
lorenz::lorenz ()
{}
15
16
17
18
19
20
21
139
140
return state_;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
lorenz::lorenz()
{}
Figure 6.3. C++ implementation of an abstract calculus for the Lorenz system: (a) Integration
test, (b) Integrand declaration, (c) Integrand implementation, (d) Lorenz declaration, and (e)
Lorenz implementation.
delegate their work to highly optimized versions of linear algebra libraries such as
BLAS (Blackford et al. 2002) and LAPACK (Barker et al. 2001) or scalable parallel libraries such as ScaLAPACK (Blackford et al. 1997), PETSc (Balay et al. 2007),
or Trilinos (Heroux et al. 2005). We detail the process of building atop Trilinos in
Chapter 11.
A common thread running through many design patterns is their fostering of
loose couplings between abstractions. Unlike procedural time-integration libraries
and even many object-oriented ones, integrate() never sees the data it integrates.
It therefore relies on no assumptions about the datas layout in memory, its type, or
its precision. The programmer retains the freedom to restructure the data or change
its type or precision with absolutely no impact on the time-integration code.
Sometimes, one developers benet is anothers drawback. For example, if implemented naively, one potential drawback of ABSTRACT CALCULUS lies in making it
more challenging for the compiler to optimize. Section 3.3 delineated several of the
corresponding challenges for ADT calculus and potential workarounds. Naturally,
these same potential pitfalls and solutions apply to ABSTRACT CALCULUS, because this
pattern simply creates a unied interface to an ADT calculus supported by any classes
that implement the interface.
The benets of the ABSTRACT CALCULUS over type-specic ADT calculus stem
from the uniformity the pattern imposes across the elements of a package and the
resulting ability to vary the dynamic type of a mathematical entity at execution time.
The cost paid for this is the runtime overhead involved in discovering that type on the
y and, more importantly, the seeming impossibility of performing the static analysis
of the code required for the more advanced optimization strategies particularly interprocedural optimizations. This returns again to the theme of coarse-grained data
structure design that facilitates large numbers of computations inside each operator, tied to a sound understanding of theoretical operation-count predictions and a
disciplined approach to empirical proling.
Ultimately the highest virtue to which ABSTRACT CALCULUS aspires harkens back
to Timeless Way (Section 4.2.1), which attempts to breathe life into designs by releasing ones innate understanding of the application domain. For those trained in
scientic simulation, the underlying mathematics eventually feels innate. ABSTRACT
CALCULUS takes the conversations that historically occurred at the blackboard to
the screen. This is a dialogue spoken in pattern languages. Although each person
possesses a pattern language unique to their perspective and life experiences (some
prefer vector notation, others tensor notation), providing an elementary set of common operations enables users to extend the classes so developed in ways that reect
their predilections. In doing so, it draws back the curtain and attracts software users
into a dialogue previously limited to expert developers.
141
142
as ABSTRACT CALCULUS would be known as the GoF FACADE pattern. Much as the
facade described in Section 4.2.3 presents a simplied and unied interface to two
complicated subsystems, so does ABSTRACT CALCULUS greatly simplify and unify the
top-level interface to a multiphysics solver. As formulated, this pattern supports
writing expressions on a single class of objects at a time without reference to, or a
mechanism for, combining classes.
Chapter 8 outlines how to aggregate multiple classes into one entity, a PUPPETEER,
for writing ABSTRACT CALCULUS expressions. From one viewpoint, a PUPPETEER serves
merely as the concrete side of the double-faceted FACADE structure described in
section 4.2.3. It could therefore be treated as part of an ABSTRACT CALCULUS. However,
it plays an additional role that warrants treating it as a separate pattern. Chapter 8
explains further.
The ordering of our patterns chapters draws inspiration from A Pattern Language (Alexander et al. 1975). Just as they started broadest patterns for towns and
regions and proceeded to smaller patterns that complete the larger ones, ABSTRACT
CALCULUS sets functionality requirements that propagate downward throughout the
remainder of the design. In one sense, the remainder of Part II concerns laying out
patterns necessary to complete an ABSTRACT CALCULUS in a complex, multiphysics
setting.
Were we presenting a complete pattern language in the style of
Alexander et al. (1977), we would express the relationships between patterns in a
graph with nodes representing patterns and edges drawn between the nodes to indicate which patterns complete which other patterns. Instead, we focus Part II on a
core set of patterns that would likely all be involved in completing a solver.
EXERCISES
1. Write an abstract type or pure virtual class that denes the operators and methods
required to time advance the heat conduction equation used throughout Part I of
this text.
2. Write a concrete ADT that implements the interface dened in Exercise 1 and
solves the heat conduction equations. Explore varying approaches to code reuse:
(a) aggregate an instance of one of the heat conduction ADTs from Part I and delegate calculations to that object, and (b) modify one of the earlier heat conduction
ADTs so that it extends the interface dened in Exercise 1 and directly associates
any deferred type-bound procedures or pure virtual functions with those in the
modied ADT. Discuss the benets and drawbacks of each approach in terms of
code construction costs and maintainability.
144
(7.1)
(7.2)
which has been written in a predictor/corrector form involving two substeps but can
also easily be combined into a single step.
in an object and time advancing that object via
Encapsulating the state vector V
ABSTRACT CALCULUS makes sense if the overloaded and dened operations in that
calculus have reasonable mathematical interpretations when applied to each state
vector component. More specically, the overloaded forms of equations (7.1)(7.2)
have meaningful interpretations if each component of the state vector satises a
differential equation. Time is a state vector component in the current context, so we
augment the governing equations (4.5) with a fourth equation:
d
=1
dt
(7.3)
where is the objects time stamp. The challenge is to extend the lorenz type by
adding the new component , adding its governing equation (7.3), and adding the
ability to select an integration strategy dynamically at runtime.
Obviously the driving force in this context is the need for exibility. An opposing force is a desire for simplicity. Placing the responsibility for implementing
new numerical quadrature algorithm in the hands of each integrand descendant
requires little effort on the part of the integrand designer and is thus the simplest
approach from a design standpoint. Conversely, requiring the integrand designer to
implement each new algorithm requires greater effort on the part of the integrand
designer, but still retains the simplicity of having all the algorithms in one abstraction
and under the control of that abstractions developers.
The next section discusses how STRATEGY resolves the conicting forces in the
current design context. The Fortran implementation of STRATEGY provides the context for the SURROGATE pattern. We therefore discuss the problem a SURROGATE
solves in the Fortran implementation section 7.2.1.
A STRATEGY severs the link between algorithms and data. Data objects delegate
operations to Strategy classes that apply appropriate algorithms to the data.
surrogate
strategy
integrand
lorenz
timed_lorenz
euler_integrate
runge_kutta2_integrate
Figure 7.1. Fortran class model for applying the STRATEGY and SURROGATE patterns to extend
the Lorenz model.
145
146
1
2
3
4
5
6
7
8
9
10
11
12
13
type(explicit_euler)
type(runge_kutta_2nd)
type(lorenz)
type(timed_lorenz)
integer
integer ,parameter
::
::
::
::
::
::
lorenz_integrator
!Integration strategy
timed_lorenz_integrator !Integration strategy
attractor ! Lorenz equation/state abstraction
timed_attractor ! Time-stamped abstraction
step
! Time step counter
num_steps=2000,space_dimension=3
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
1
2
3
4
module surrogate_module
implicit none
private
public :: surrogate
5
6
7
8
9
!
!
!
!
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
abstract interface
function time_derivative(this) result(dState_dt)
import :: integrand
class(integrand) ,intent(in) :: this
class(integrand) ,allocatable :: dState_dt
end function time_derivative
function symmetric_operator(lhs,rhs) result(operator_result)
import :: integrand
class(integrand) ,intent(in) :: lhs,rhs
class(integrand) ,allocatable :: operator_result
end function symmetric_operator
function asymmetric_operator(lhs,rhs) result(operator_result)
147
148
import :: integrand
class(integrand) ,intent(in) :: lhs
class(integrand) ,allocatable :: operator_result
real
,intent(in) :: rhs
end function asymmetric_operator
subroutine symmetric_assignment(lhs,rhs)
import :: integrand
class(integrand) ,intent(in)
:: rhs
class(integrand) ,intent(inout) :: lhs
end subroutine symmetric_assignment
end interface
51
52
contains
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
1
2
3
subroutine integrate(model,dt)
class(integrand) :: model
! integrand
real ,intent(in)
:: dt
! time step size
if (allocated(model%quadrature)) then
call model%quadrature%integrate(model,dt)
else
stop integrate: no integration procedure available.
end if
end subroutine
end module integrand_module
4
5
6
implicit none
private
7
8
9
10
11
12
14
15
16
17
18
19
20
21
22
1
2
3
4
5
abstract interface
subroutine integrator_interface(this,dt)
import :: surrogate
class(surrogate) ,intent(inout) :: this ! integrand
real
,intent(in)
:: dt
! time step size
end subroutine
end interface
end module
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
interface lorenz
procedure constructor
end interface
25
26
contains
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
149
150
class(lorenz)
,intent(in) :: this
class(integrand) ,allocatable :: dState_dt
type(lorenz)
,allocatable :: local_dState_dt
allocate(local_dState_dt)
call local_dState_dt%set_quadrature(this%get_quadrature())
allocate(local_dState_dt%state(size(this%state)))
! 1st Lorenz equation
local_dState_dt%state(1) = this%sigma*(this%state(2)-this%state(1))
! 2nd Lorenz equation
local_dState_dt%state(2) = this%state(1)*(this%rho-this%state(3)) &
-this%state(2)
! 3rd Lorenz equation
local_dState_dt%state(3) = this%state(1)*this%state(2)
&
-this%beta*this%state(3)
local_dState_dt%sigma
= 0.
local_dState_dt%rho
= 0.
local_dState_dt%beta
= 0.
! transfer the allocation from local_dState_dt to dState_dt
call move_alloc(local_dState_dt,dState_dt)
end function
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
interface timed_lorenz
procedure constructor
end interface
151
152
contains
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
allocate(local_dState_dt)
local_dState_dt%time
= 1.
local_dState_dt%lorenz = this%lorenz%t()
! avoid unnecessary memory allocation
call move_alloc(local_dState_dt,dState_dt)
end function
! dt/dt = 1.
! delegate to parent
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
select type(rhs)
class is (timed_lorenz)
allocate(local_sum)
local_sum%time
= lhs%time
+ rhs%time
local_sum%lorenz = lhs%lorenz + rhs%lorenz
class default
stop add_timed_lorenz: type not supported
end select
65
66
67
68
69
70
71
72
73
74
75
76
77
allocate(local_product)
local_product%time
= lhs%time * rhs
local_product%lorenz = lhs%lorenz* rhs
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
! return state
function output(this) result(coordinates)
class(timed_lorenz) ,intent(in) :: this
real ,dimension(:) ,allocatable :: coordinates
102
103
104
105
106
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
contains
16
17
18
19
20
21
22
23
153
154
1
2
3
4
5
6
7
implicit none
private
8
9
10
11
12
13
14
15
contains
16
17
18
19
20
21
22
! Time integrator
subroutine integrate(this,dt)
class(surrogate) ,intent(inout) :: this
! integrand
real
,intent(in)
:: dt
! time step size
class(integrand) ,allocatable
:: this_half ! function evaluation
! at interval t+dt/2.
23
24
25
26
27
28
29
30
31
32
33
! predictor step
! corrector step
Figure 7.2. Fortran strategy pattern implementation for Lorenz and timed Lorenz systems:
(a) integration test of Lorenz and time-stamped Lorenz systems, (b) abstract surrogate,
(d) abstract strategy, (c) abstract integrand, (e) Lorenz ADT, (f) time-stamped Lorenz ADT,
(g) explicit Euler strategy, and (h) RK2 strategy.
One construct appearing for the rst time in this text is the non_overridable
attribute applied to the integrate procedure binding at line 11 in Figure 7.2(c). This
attribute ensures that types that extend integrand retain its implementation of that
binding. This ensures consistency in the sense that extended types will not be able to
overload the integrate name. Each will instead have access to the same collection
of integration methods implemented in a separate inheritance hierarchy.
Another construct making its rst appearance is the nopass attribute appearing
on line 12 in Figures 7.2(d), (g), and (h). It instructs the compiler to not pass as an
argument the object on which the type-bound procedure is invoked. Instead, the
user must pass the argument (i.e., the integrand) explicitly. This simply forces the
procedure invocation syntax, say, integrate(x,dt), to mirror the corresponding
mathematical syntax xdt. Although one must prepend x% to the procedure invocation, this could be avoided by dening a module procedure that in turn invokes the
type-bound procedure.
The previously used intrinsic procedure allocate appears in a new form:
sourced allocation. This form appears rst at line 60 in Figure 7.2(c):
allocate (this%quadrature, source=s)
where the new source argument ensures that this%quadrature takes on the value
and dynamic type of s, which must be of a concrete strategy type. Another sourced
allocation occurs at line 26 in Figure 7.2(h):
allocate(this_half,source=this)
after which the immediately subsequent line overwrites this_half and thereby
negates the utility of copying the value of this. Fortran 2008 provides a new molded
allocation of the form:
allocate(this_half,mold=this)
which copies only the dynamic type of this, not its value.
Beginning with this chapter, the software problems being solved exhibit sufcient complexity that the code length exceeds the length of the related text. This
increases the expectation that the codes will be intelligible in a self-contained way.
We therefore increase the frequency with which we document our intent in comments. We also continue to attempt to write code that comments itself in the manner
described in Chapter 1. Also, the complexity of the examples in this chapter begin
to show the real power and benet of UML, as Figure 7.1 succinctly summarizes the
structural relationships between the different parts of the code in Figure 7.2.
Our strategies for dealing with code length evolve as the trend toward longer
codes continues unabated in subsequent chapters. In Chapter 8, we shift the code
listings to the ends of the relevant sections to preserve continuity in the text. In
Chapters 11 and 12, where we discuss multipackage, open-source projects with code
listings in the tens of thousands of lines, we shift to an even greater reliance on UML
supplemented by illustrative code snippets.
155
156
1
2
3
4
5
#include
#include
#include
#include
#include
6
7
8
9
int main ()
{
typedef lorenz::ptr_t ptr_t;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Ref<timed_lorenz> timed_attractor
= new timed_lorenz(initial_condition, sigma, rho, beta,
new runge_kutta_2nd);
std::cout << "\n timed_lorenz attractor:\n"
<< fmt(timed_attractor->get_time(), 12, 9) << " "
<< fmt(timed_attractor->coordinate(), 12, 9) << std::endl;
for (int i = 0; i < num_steps; ++i)
{
timed_attractor->integrate(dt);
std::cout << fmt(timed_attractor->get_time(), 12, 9) << " "
<< fmt(timed_attractor->coordinate(), 12, 9) << std::endl;
}
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1
2
return 0;
#ifndef _H_STRATEGY_
#define _H_STRATEGY_
3
4
5
#include "globals.h"
#include "RefBase.h"
6
7
8
9
class integrand;
class strategy : public RefBase {
public:
12
13
14
15
virtual strategy() {}
virtual void integrate (model_t this_obj, real_t dt) const = 0;
};
16
17
1
2
#endif //!_H_STRATEGY_
#ifndef _H_INTEGRAND_
#define _H_INTEGRAND_
3
4
5
6
#include "strategy.h"
#include "RefBase.h"
#include "globals.h"
7
8
9
10
11
12
13
14
15
16
integrand(strategy_t);
integrand(const integrand&);
virtual integrand();
17
18
19
20
21
22
23
24
25
virtual
virtual
virtual
virtual
ptr_t
ptr_t
ptr_t
ptr_t
clone() const = 0;
d_dt() const = 0;
operator+=(ptr_t) = 0;
operator*=(real_t) = 0;
26
27
28
29
private:
strategy_t quadrature_;
};
30
31
#include "model_ops.h"
32
33
1
2
#endif //!_H_integrand_
#ifndef _H_MODEL_OPS_
#define _H_MODEL_OPS_
3
4
5
6
7
8
9
10
inline integrand::ptr_t
operator+(integrand::ptr_t a, integrand::ptr_t b)
{
integrand::ptr_t tmp = a->clone();
*tmp += b;
return tmp;
}
157
158
19
20
1
2
#include "integrand.h"
#include <exception>
3
4
5
6
7
8
9
10
11
12
13
14
integrand::integrand(strategy_t quad) :
RefBase(), quadrature_(quad)
{}
15
16
17
18
19
20
21
integrand::integrand()
{}
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
1
2
#ifndef _H_LORENZ_
#define _H_LORENZ_
3
4
5
#include "integrand.h"
11
12
13
14
15
16
17
18
19
virtual
virtual
virtual
virtual
ptr_t
ptr_t
ptr_t
ptr_t
clone() const;
d_dt() const;
operator+=(ptr_t);
operator*=(real_t);
20
21
22
23
24
25
26
27
28
29
30
private:
crd_t state_;
real_t sigma_, rho_, beta_;
};
31
32
1
2
3
#endif // !_H_LORENZ_
#include "lorenz.h"
#include "fmt.h"
#include <exception>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
lorenz::lorenz()
{}
21
22
23
24
25
26
159
160
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
1
2
return beta_;
3
4
5
#include "strategy.h"
#include "lorenz.h"
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void
set_time (real_t);
real_t get_time() const;
private:
real_t time_;
};
26
27
1
2
#endif //!_H_TIMED_LORENZ_
Figure 7.3(i): timed_lorenz.cpp
#include "timed_lorenz.h"
#include <exception>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
timed_lorenz::timed_lorenz()
{}
19
20
21
22
161
162
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1
2
3
4
5
#include "strategy.h"
#include "integrand.h"
6
7
8
9
10
11
12
13
14
#endif //!_H_EXPLICIT_EULER_
1
2
3
4
5
6
explicit_euler::explicit_euler()
{}
7
8
9
10
1
2
3
4
5
#include "strategy.h"
#include "integrand.h"
6
7
8
9
10
11
12
13
1
2
#endif
#include <iostream>
#include <exception>
3
4
5
#include "integrand.h"
#include "runge_kutta_2nd.h"
6
7
8
9
10
runge_kutta_2nd::runge_kutta_2nd()
{}
11
12
13
14
15
16
163
164
EXERCISES
1. Runge-Kutta-Fehlberg (RKF) algorithms provide one context for dynamically
varying a time integration method. By alternating between two schemes with
different orders of accuracy, RKF methods facilitate time step size control based
on error estimation. For example, a 4th-order-accurate Runge-Kutta-Fehlberg
method (RKF45) uses a 5th-order-accurate step for error estimation. In advancing
a differential equation dy/dt = f (t, y) from a time ti over a time step hi to a time
ti+1 ti + hi , RKF45 constructs both approximations from one set of function
evaluations (Morris 2008):
K1 = hf (ti , yi )
1
1
K2 = hf ti + h, yi + K1
4
4
3
3
9
K3 = hf ti + h, yi + K1 + K2
8
32
32
12
1932
7200
7296
K4 = hf ti + h, yi +
K1
K2 +
K3
13
2197
2197
2197
439
3680
845
K5 = hf ti + h, yi +
K1 8K2 +
K3
K4
216
513
4104
1
8
3544
1849
11
K6 = hf ti + h, yi K1 + 2K2
K3
K4 K 5
2
27
2565
4104
40
which yield the 4th - and 5th -order-accurate schemes
25
1408
2197
1
K1 +
K3 +
K4 K5
216
2565
4101
5
16
6656
28561
9
2
yRK5
K1 +
K3 +
K4 K5 + K5
i+1 = yi +
135
12825
56430
50
55
yRK4
i+1 = yi +
RK5
Estimating the error as e yRK4
i+1 yi+1 , one adjusts the time step size just before
each step according to:
hi =
0.20
Shi1 etol
e e 0.25
tol
Shi1 e
if e etol
if e > etol
where S is a safety factor just below unity and etol is the error tolerance.
Design a STRATEGY for solving the Lorenz system (4.5) using the RKF45 algorithm.
Express your design in a UML class diagram. Given S = and etol =, write an
implementation of your design.
165
166
2. In many situations, nite difference approximations perform best when the stencil
employed to approximate derivatives at a given point is skewed toward the direction from which information is propagating. Labeling a given direction in one
dimension forward and the opposite direction backward, it is desirable to be
able to switch between the forward and backward differencing schemes described
in Section A.5.1. Design a STRATEGY for switching between these two schemes at
runtime. Express your design in a UML class diagram. Write an implementation
of your design.
Never be afraid to try something new. Remember, amateurs built the ark.
Professionals built the Titanic.
Miss Piggy
168
Air
Cloud
Mediator
Ground
(8.1)
contains
n+1 on the RHS makes iteration necessary when R
where the presence of V
nonlinearities. One generally poses the problem in terms of nding the roots of a
residual vector f formed by gathering the terms in equation (8.1):
V
n+1
n+1 V
n + t R
n+1 ) V
V
n +R
(8.2)
f(V
2
n is known from a previous time step.
where V
The difculty arises in nding the roots of f using Jacobian-based iteration methods. Consider Newtons method. Dening ym as the mth iterative approximation to
n+1 , Newtons method can be expressed as:
V
Jym f(ym )
(8.3)
n+1
V
fi
fi
t Ri
=
= ij
n+1
yj y=ym Vj
2
n+1 =ym
Vj n+1
V
(8.4)
n+1 =ym
V
(8.5)
where J is the Jacobian, Ri is the RHS of the ith governing equation, and ij is the
Kronecker delta. Equation (8.3) represents a linear algebraic system. Equation (8.4)
represents vector addition.
are partitioned such that different elements are
Now imagine that f, y, and R
hidden behind interfaces to different abstractions. In particular, let us partition R
T
a c g , where the partitions separate the elements of R
corresuch that R
sponding to the air, cloud, and ground ADTs, respectively. For present purposes,
each partition can be thought of as a 1D vector containing the RHS of one component equation from the Lorenz system (4.5). With this notation, one can rewrite the
Jacobian in equation (8.3) as:1
J I
t (a, c, g )
2 (
, ,
)
(8.6)
1 In equation (8.4) and elsewhere, we abbreviate a common notation for Jacobians in which the list of
f components appears in the numerator and the list of y components appears in the denominator.
We do not mean for the arrows to connote vectors that transform as rst-order tensors.
where , , and are the air, cloud, and ground state vectors, respectively, so
T
y , and where I is the identity matrix.
The second dilemma presents itself in the need to calculate cross terms such
as a/ . The question is, How does the air ADT differentiate its components
with respect to the state vector partition when that partition lies hidden
of R
inside the cloud abstraction? Even if one solves this puzzle, a more perplexing one
remains: Where does J live? Since the precise form of equations (8.5)(8.6) varies
with the choice of time integration algorithms, the natural place to construct J is
inside the time integration procedure. In an ABSTRACT CALCULUS implementation,
however, that procedure maintains a blissful ignorance of the governing equations
inside the dynamical system it integrates as exemplied by the integrate procedures
in Chapter 6.
It appears the conicting forces driving the software problems in this book climax
in the current chapters context, potentially forcing the violation of abstractions.
The desire to maintain a modular architecture with local, private data confronts
the need to handle intermodule dependencies. These dependencies force rigidly
specied communication and complicate the calculation of inherently global entities.
A Showdown at the OOP Corral
The ABSTRACT CALCULUS edice threatens to collapse under the weight of the
problems posed here. Modularity and data privacy present signicant impediments to coupling independently developed abstractions without exposing their
implementation details. Of prime concern are how to facilitate interabstraction
communication and how to assemble inherently global quantities.
169
170
Puppeteer
Ground
Air
Cloud
developer fullls these requests by calling accessors made public by the other dynamical systems in the simulation. As depicted in Figure 8.2, the PUPPETEER uses object
aggregation to reduce the aforementioned number of interabstraction associations
from 2N = 6 to N = 3: the PUPPETEER knows a datums sender and recipient, but they
need not know the PUPPETEER.
The dialogue the PUPPETEER mediates in the atmospheric boundary layer model
can be paraphrased as follows:
Puppeteer to Air: Please pass me a vector containing the partial derivatives of each
of your governing equations RHS with respect to each element in your state
vector.
Air to Puppeteer: Here is the requested vector ( a/ ). You can tell the dimension
of my state by the size of this vector.
Puppeteer to Air: I also know that your governing equations depend on the Cloud
state vector because your interface requests information that I retrieved from a
Cloud. Please pass me a vector analogous to the previous one but differentiated
with respect to the Cloud state information I passed to you.
Puppeteer note to self : Since I did not pass any information to the Air object from
the Ground object, I will set the cross-terms corresponding to a/ g to zero.
Ill determine the number of zero elements by multiplying the size of a/ by
the size of g / after I receive the latter from my Ground puppet.
The PUPPETEER then holds analogous dialogues with its Cloud and Ground pup V
to integrate. The
pets, after which the PUPPETEER passes an array containing R/
latter procedure uses the passed array to form J, which it then passes to a PUPPETEER
for use in inverting the matrix system (equation 8.3). Most importantly, integrate
does so without violating the PUPPETEERs data privacy, and the PUPPETEER responds
without violating the privacy of its puppets. The PUPPETEERs construction is based
solely on information from the public interfaces of each puppet. Figure 8.3 details
V
in a UML sequence diagram. Note the coordinate()
the construction of R/
method calls return solution variables. The next three calls return diagonal blocks
V.
The subsequent six calls return off-diagonal blocks. The nal two steps
of R/
V.
allocate and ll R/
8.2.1 Fortran Implementation
Figure 8.4 provides the denition of the atmosphere type, which acts as a PUPPETEER in our prototype atmospheric boundary-layer simulation involving the Lorenz
this:atmosphere
air_puppet:air
cloud_puppet:cloud
ground_puppet:ground
coordinate()
air's state vector
coordinate()
cloud's state vector
coordinate()
ground's state vector
d_dAir(cloud_coord)
dAir_dAir
d_dCloud(air_coord, ground_coord)
dCloud_dCloud
d_dGround(air_coord, cloud_coord)
dGround_dGround
d_dy(cloud_coord)
dAir_dCloud
dAir_dGround
d_dx (air_coord, ground_coord)
dCloud dAir
d_dz (air_coord, gound_coord)
d_Cloud_dGround
d_dx (air_coord, cloud_coord)
dGround_dAir
d_dy (air_coord, cloud_coord)
dGround_dCloud
allocate
dRHS_dState
:real
assembly result
Figure 8.3. A UML sequence diagram for the Puppeteers Jacobian contribution.
equations partitioned in the manner described in the previous section. Here the
atmosphere contains references to all its puppets, which are of type air, cloud, and
ground. The puppets data can be constructed independently after the atmosphere
object is created.
Lines 1618 in Figure 8.4 show the composition of the atmosphere type. The
atmosphere object relays all information using Fortran allocatable scalar data.
171
172
Although the puppets are instantiated before the puppeteer, they are destroyed
when the puppeteer is destroyed, for example when its name goes out of scope. This
happens automatically at the hands for the compiler.
The function dRHS_dV (from line 72 to line 153) illustrates the construction of a
V,
corresponding to the UML sequence diagram given
Jacobian contribution, R/
in Figure 8.3. Figure 8.5 details the denitions for all the puppets in the atmosphere
system. Puppet air, cloud, and ground track the rst, second, and third state variables
in the Lorenz system, respectively.
1
2
3
4
5
6
7
8
9
10
11
12
!implicit none
! Prevent implicit typing
private
! Hide everything by default
public :: atmosphere
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
interface atmosphere
procedure constructor
end interface
38
39
contains
40
41
42
43
44
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
real ,dimension(:)
,allocatable ::
&
air_coordinate, cloud_coordinate, ground_coordinate
85
86
87
88
89
90
91
92
93
94
95
96
97
air_coordinate = this%air_puppet%coordinate()
cloud_coordinate= this%cloud_puppet%coordinate()
ground_coordinate=this%ground_puppet%coordinate()
98
99
173
174
dAir_dAir
dCloud_dCloud
= this%air_puppet%d_dAir(cloud_coordinate)
= this%cloud_puppet%d_dCloud(air_coordinate,&
ground_coordinate)
dGround_dGround = this%ground_puppet%d_dGround(air_coordinate,&
cloud_coordinate)
air_eqs
= size(dAir_dAir,1)
! submatrix rows
air_vars
= size(dAir_dAir,2)
! submatrix columns
cloud_eqs
= size(dCloud_dCloud,1)
! submatrix rows
cloud_vars = size(dCloud_dCloud,2)
! submatrix columns
ground_eqs = size(dGround_dGround,1) ! submatrix rows
ground_vars = size(dGround_dGround,2) ! submatrix columns
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
&
&
&
&
&
&
&
&
&
&
&
&
&
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
175
176
real :: factor
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
n=size(lhs,1)
b = rhs%state_vector()
if (n /= size(lhs,2) .or. n /= size(b)) &
stop atmosphere.f03: ill-posed matrix problem in inverseTimes()
allocate(x(n))
A = lhs
do p=1,n-1
! Forward elimination
if (abs(A(p,p))<pivot_tolerance) &
stop invert: use an algorithm with pivoting
do row=p+1,n
factor=A(row,p)/A(p,p)
forall(col=p:n)
A(row,col) = A(row,col) - A(p,col)*factor
end forall
b(row) = b(row) - b(p)*factor
end do
end do
x(n) = b(n)/A(n,n) ! Back substitution
do row=n-1,1,-1
x(row) = (b(row) - sum(A(row,row+1:n)*x(row+1:n)))/A(row,row)
end do
allocate(product,source=rhs)
product%air_puppet = air(x(1),x(2))
call product%cloud_puppet%set_coordinate(x(3))
call product%ground_puppet%set_coordinate(x(4))
end function
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
!
!
!
!
!
!
!
!
!
This type tracks the evolution of the first state variable in the
Lorenz system according to the first Lorenz equation. It also
tracks the corresponding paramater (sigma) according to the
differential equation d(sigma)/dt=0. For illustrative purposes, this
implementation exposes the number of state variables (2) to the
puppeteer without providing direct access to them or exposing
anything about their layout, storage location or identifiers (x and
sigma). Their existence is apparent in the rank (2) of the matrix
d_dAir() returns as its diagonal Jacobian element contribution.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
type air
private
real :: x,sigma !1st Lorenz equation solution variable and parameter
contains
procedure :: coordinate! accessor: return phase-space coordinates
procedure :: t ! time derivative ( evaluate RHS of Lorenz equation)
procedure :: d_dAir ! contribution to diagonal Jacobian element
procedure :: d_dy
! contribution to off-diagonal Jacobian element
procedure ,private :: add ! add two instances
procedure ,private :: subtract ! subtract one instance from another
procedure ,private :: multiply ! multiply instance by a real scalar
! map defined operators to corresponding procedures
generic
:: operator(+) => add
generic
:: operator(-) => subtract
generic
:: operator(*) => multiply
end type
37
38
39
40
interface air
procedure constructor
end interface
41
42
43
44
45
46
47
48
49
50
51
contains
! constructor: allocate and initialize components
type(air) function constructor(x_initial,s)
real
,intent(in) :: x_initial
real
,intent(in) :: s
if (debugging) print *,
air%construct: start
constructor%x = x_initial
constructor%sigma = s
if (debugging) print *,
air%construct: end
end function
52
53
54
177
178
class(air)
,intent(in) :: this
real ,dimension(:) ,allocatable :: return_x
return_x = [ this%x ,this%sigma ]
end function
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
if (debugging) print *,
air%d_dy:
allocate(dRHS_dy(num_eqs,size(y)))
!dRHS_dy = [ d{sigma*(y(1)-x(1))}/dy(1)
!
[ d{0}/dy(1)
dRHS_dy = 0.
dRHS_dy(1,1) = this%sigma
if (debugging) print *,
air%d_dy:
end function
start
0
0
... 0]
... 0]
end
100
101
102
103
104
105
106
107
108
109
if (debugging) print *,
air%add: start
sum%x
= lhs%x
+ rhs%x
sum%sigma = lhs%sigma + rhs%sigma
if (debugging) print *,
air%add: end
end function
&
120
121
122
123
124
125
126
127
128
129
130
131
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
!
!
!
!
!
!
!
!
!
!
This type tracks the evolution of the second state variable in the
Lorenz system according to the second Lorenz equation. It also
tracks the corresponding paramater (rho) according to the
differential equation d(rho)/dt=0. For illustrative purposes, this
implementation does not expose the number of state variables (2) to
the puppeteer because no iteration is required and the need for
arithmetic operations on rho is therefore an internal concern. The
rank of the matrix d_dCloud() returns is thus 1 to reflect the only
variable on which the puppeteer needs to iterate when handling
nonlinear couplings in implicit solvers.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
type cloud
private
real :: y,rho ! 2nd Lorenz equation solution variable and parameter
contains
procedure :: set_coordinate ! accessor: set phase-space coordinate
procedure :: coordinate! accessor: return phase-space coordinate
procedure :: t
! time derivative
procedure :: d_dCloud ! contribution to diagonal Jacobian element
procedure :: d_dx ! contribution to off-diagonal Jacobian element
179
180
41
42
43
44
interface cloud
procedure constructor
end interface
45
46
47
48
49
50
51
52
53
54
55
contains
! constructor: allocate and initialize components
type(cloud) function constructor(y_initial,r)
real ,intent(in) :: y_initial
real ,intent(in) :: r
if (debugging) print *,
cloud: start
constructor%y = y_initial
constructor%rho = r
if (debugging) print *,
cloud: end
end function
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
181
182
1
2
real
,intent(in) :: rhs
type(cloud)
:: product
if (debugging) print *,
cloud%multiply: start
product%y
= lhs%y* rhs
product%rho = lhs%rho* rhs
if (debugging) print *,
cloud%multiply: end
end function
end module cloud_module
Figure 8.5(c): ground.f03
module ground_module
use global_parameters_module ,only :debugging !print call tree if true
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
!
!
!
!
!
!
!
!
!
!
This type tracks the evolution of the third state variable in the
Lorenz system according to the third Lorenz equation. It also
tracks the corresponding paramater (beta) according to the
differential equation d(beta)/dt=0. For illustrative purposes,
this implementation does not expose the number of state variables(2)
to the puppeteer because no iteration is required and the need for
arithmetic operations on beta is therefore an internal concern. The
rank of the matrix d_dGround() returns is thus 1 to reflect the
only variable on which the puppeteer needs to iterate when handling
nonlinear couplings in implicit solvers.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
type ground
private
real :: z,beta ! 3rd Lorenz equation solution variable and parameter
contains
procedure :: set_coordinate ! accessor set phase-space coordinate
procedure :: coordinate ! accessor: return phase-space coordinate
procedure :: t
! time derivative
procedure :: d_dGround ! contribution to diagonal Jacobian element
procedure :: d_dx
! contribution to off-diagonal Jacobian element
procedure :: d_dy
! contribution to off-diagonal Jacobian element
procedure ,private :: add ! add two instances
procedure ,private :: subtract ! subtract one instance from another
procedure ,private :: multiply ! multiply instance by a real scalar
! map defined operators to corresponding procedures
generic ,public :: operator(+) => add
generic ,public :: operator(-) => subtract
generic ,public :: operator(*) => multiply
end type
41
42
43
44
45
interface ground
procedure constructor
end interface
contains
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
183
184
allocate(dRHS_dx(num_eqs,size(x)))
!dRHS_dz = [ d{x(1)*y(1) - beta*z(1)}/dx(1)
0 ...
dRHS_dx=0.
dRHS_dx(1,1) = y(1)
if (debugging) print *,
ground%d_dx: end
end function
0 ]
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
1
2
3
4
5
6
7
8
9
10
11
12
13
!
!
!
!
This code integrates the Lorenz equations over time using separate
abstractions for equation and hiding the coupling of those
abstractions inside an abstraction that follows the Puppeteer design
pattern of Rouson, Adalsteinsson and Xia (ACM TOMS 37:1, 2010).
14
15
16
17
18
19
type(air)
,allocatable
type(cloud)
,allocatable
type(ground) ,allocatable
type(atmosphere)
integer
::
::
::
::
::
sky
!puppet for
puff !puppet for
earth !puppet for
boundary_layer !
step
!
2 In class denition for mat_t, we only provide a minimum set of denitions of functions and operators
that are actually used throughout the example. It is by no means to be used as a realistic class
denition for two-dimensional allocatable array. Readers are encouraged to extend this class so it
becomes more complete.
185
186
integer ,parameter
real
,parameter
real
23
24
real
,parameter
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
t=0.
write(*,(f10.4),advance=no) t
print *,boundary_layer%state_vector()
do step=1,num_steps
call integrate(boundary_layer,dt)
t = t + dt
write(*,(f10.4),advance=no) t
print *,boundary_layer%state_vector()
end do
if (debugging) print *,main: end
46
47
48
49
50
51
52
53
54
55
contains
! abstract Trapezoidal rule integration
subroutine integrate(integrand,dt)
type(atmosphere) ,intent(inout) :: integrand
real
,intent(in)
:: dt
type(atmosphere) ,allocatable
:: integrand_estimate,residual
integer ,parameter :: num_iterations=5
integer :: newton_iteration,num_equations,num_statevars,i,j
real ,dimension(:,:) ,allocatable :: dRHS_dState,jacobian,identity
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
1
2
3
4
5
end do
integrand = integrand_estimate
if (debugging) print *, integrate: end
end subroutine
end program main
Figure 8.6(b): global_parameters.f03
module global_parameters_module
! parameter that is used to control the debugging. The call tree
! information is printed if value of debugging is true
logical ,parameter :: debugging=.false.
end module
Figure 8.6. Fortran 2003 implementation of puppeteer pattern applied to an atmosphere
system. (a) Time-integration test of Lorenz system using puppeteer pattern. (b) A global
debugging ag.
x(3,3)
! Fortran declaration
In Fortran, x(3,1) refers to the third element of x, and x(1,3) refers to seventh element
of x. The corresponding C++ expressions are x[0][2] and x[2][0], respectively.
In designing mat_ class, we choose zero-based notation because it is the most
natural way to express array subscripts to C++ programmers. We also adhere to Fortrans convention in element ordering. The computation of element order is shown
in the denitions of operator() in gure 8.7(b) as follows:
mat_t::value_type mat_t::operator()(int r, int c) const {
// error checking for invalid subscript range; code omitted
return data_.at(c*r_ + r);
}
Thus for the aforementioned 3 3 matrix, mat_(2, 0) corresponds to Fortrans x(3,
1), and mat_(0, 2) corresponds to x(1, 3).
1
2
#ifndef _H_MAT_
#define _H_MAT_
3
4
5
6
#include "globals.h"
#include <iostream>
#include <iomanip>
187
188
class mat_t {
public:
typedef crd_t::value_type value_type;
typedef crd_t::reference reference;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
mat_t();
mat_t(int rows, int cols);
void clear();
void resize(int rows, int cols);
void clear_resize(int rows, int cols, value_type value = 0);
void identity(int rows);
int rows() const;
int cols() const;
value_type operator()(int r, int c) const;
reference operator()(int r, int c);
void set_submat(int r, int c, const mat_t &other);
mat_t& operator-=(const mat_t&);
mat_t& operator*=(real_t);
26
27
28
29
30
private:
int r_, c_;
crd_t data_;
};
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
struct dim_t {
const int eqs;
const int vars;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
}
os.flags(flags);
return os;
66
67
1
2
3
#endif // !_H_MAT_
#include "mat.h"
#include <exception>
#include <iostream>
4
5
6
7
8
9
10
11
mat_t::mat_t() :
r_(0), c_(0)
{}
12
13
14
15
16
17
18
19
20
void mat_t::clear() {
r_ = c_ = 0;
data_.clear();
}
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
189
190
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
Figure 8.7. A mat_t class in C++ that simulates the functionality of 2D allocatable real arrays in
Fortran. (a) Class denition for mat_t, which simulates Fortrans two-dimensional allocatable
array of type real. (b) Implementation of class mat_t.
1
2
#ifndef _H_ATMOSPHERE_
#define _H_ATMOSPHERE_
3
4
5
6
7
#include
#include
#include
#include
"integrand.h"
"air.h"
"cloud.h"
"ground.h"
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
private:
air air_;
cloud cloud_;
ground ground_;
};
34
35
#endif //!_H_ATMOSPHERE_
191
192
1
2
3
#include "atmosphere.h"
#include <exception>
#include <cmath>
4
5
6
7
8
9
10
11
12
13
14
15
16
atmosphere::atmosphere() {
}
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
ground_.d_dx(air_.coordinate(),cloud_.coordinate()));
// dGround/dCloud
result.set_submat(adim.eqs+cdim.eqs, adim.vars,
ground_.d_dy(air_.coordinate(),cloud_.coordinate()));
// dGround/dGround
result.set_submat(adim.eqs+cdim.eqs, adim.vars+cdim.vars,
ground_.d_dGround(air_.coordinate(),cloud_.coordinate()));
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
193
194
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
1
2
#ifndef _H_AIR_
#define _H_AIR_
3
4
#include "mat.h"
5
6
7
8
class air {
public:
air(real_t x, real_t sigma);
9
10
11
12
13
14
17
18
19
20
21
22
23
private:
static const int dim_;
crd_t x_; // sigma is stored at x_[1]
};
24
25
#endif //!_H_AIR_
#include "air.h"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
195
196
1
2
#ifndef _H_CLOUD_
#define _H_CLOUD_
3
4
#include "mat.h"
5
6
7
8
class cloud {
public:
cloud(real_t y, real_t rho);
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private:
static const int dim_;
crd_t y_;
real_t rho_;
};
27
28
#endif //!_H_CLOUD_
#include "cloud.h"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1
2
#ifndef _H_GROUND_
#define _H_GROUND_
3
4
#include "mat.h"
5
6
7
8
class ground {
public:
ground(real_t y, real_t rho);
9
10
11
12
13
14
15
16
17
197
198
ground& operator*=(real_t);
19
20
21
22
23
24
25
26
private:
static const int dim_;
crd_t z_;
real_t beta_;
};
27
28
#endif //!_H_GROUND_
#include "ground.h"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
44
45
46
47
48
49
50
51
52
53
54
55
1
2
#include "atmosphere.h"
#include "fmt.h"
3
4
5
6
7
8
int main() {
const int num_steps=1000;
const real_t x=1., y=1., z=1., sigma=10., rho=28, beta=8./3., dt=0.02;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
real_t t = 0.;
std::cout << fmt(t,5,2) << " "
<< fmt(boundary_layer->state_vector()) << "\n";
for(int step = 1; step <= num_steps; ++step) {
integrate(boundary_layer, dt);
t += dt;
std::cout << fmt(t,5,2) << " "
<< fmt(boundary_layer->state_vector()) << "\n";
}
199
200
build a Cloud abstraction that predicts acid rain by modeling chemical species adsorption at droplet surfaces. The PUPPETEER would rst request droplet locations
from the Cloud instance, then request species concentrations at the droplet location from the Air instance, and nally pass these concentrations to the Cloud in the
and R/
V.
process of constructing R
A second important consequence derives from the aforementioned containment
relationships. Implemented using Fortrans allocatable components, the PUPPETEER
can be viewed as holding only references to its puppets (similar to the assignment of a
smart pointer in C++, Fortrans move_alloc transfers the Air, Cloud, and Ground to
the corresponding puppets during the constructor call in 8.4). The actual construction
of puppets data and their lifetime, therefore, can be independent from the existence
of the PUPPETEER (see main in 8.6). This opens the possibility of varying the physical
models dynamically mid-simulation. In the atmospheric boundary layer model, the
cloud model would not have to be included until the atmospheric conditions became
ripe for cloud formation. Of course, when an absent object would otherwise supply
information required by another object, the PUPPETEER must substitute default values.
Whether this substitution happens inside the PUPPETEER or inside each ADT (via
optional arguments) is implementation-dependent.
The cost of separating concerns and varying the physics at runtime lies in the
conceptual work of discerning who does what, where they do it, and in which format.
Consider the following lines from the Fortran implementation of the trapezoidal time
integration algorithm. This algorithm is implemented in the integrate() procedure
of 8.6(a):
dRHS_dState
= integrand_estimate%dRHS_dV()
...
jacobian = identity - 0.5*dt*dRHS_dState
residual = integrand_estimate - &
( integrand + (integrand%t() + integrand_estimate%t())*(0.5*dt))
integrand_estimate = integrand_estimate - &
(jacobian .inverseTimes. residual)
where the ellipses indicate deleted lines.3 The rst line represents an abstract cal V.
All calculations, including the assignment, happen inside the
culation of R/
PUPPETEER. This design decision stems from the fact that the details of the PUPPETEER
so the information-hiding philosophy of
(and the objects it contains) determine R,
OOP precludes exposing these to integrate(). By contrast, the formula for calculating the Jacobian in the next line depends on the chosen time-integration algorithm,
so while dt*dRHS_dState represents an overloaded operation implemented inside
the PUPPETEER, that operation returns a Fortran array of intrinsic type (C++ primitive type), and the remainder of the line represents arithmetic on intrinsic entities.
Thus, the identity matrix and the Jacobian are simple oating-point arrays. Finally,
the subsequent two lines would be the same for any algorithm that employs implicit
3 The name inverseTimes is intended to be suggestive of the ultimate result. The sample code in 8.4
employs Gaussian elimination rather than computing and premultiplying the inverse Jacobian.
EXERCISES
1. Develop your own PUPPET pattern that abstracts the notion of an individual subsystem to be manipulated by a PUPPETEER. Shield the PUPPETEER from knowledge
of the concrete PUPPETS by having the PUPPETEER manipulate a collection of an
abstract puppet objects, each one having a private state vector along with public
methods for accepting and providing coupling terms. Write an atmosphere PUPPETEER with an three-element puppet array and pass it puppets corresponding to
each of the Lorenz equations (4.5). Advance your PUPPETEER by an explicit time
advancement algorithm such as 2nd-order Runge Kutta.
2. Consider the number of possible interpretations of the time integration line (46)
in Figure 6.2(b) with each interpretation corresponding to a unique dynamic type
that extends integrand type. Calculate the growth in information entropy as
each new single-physics abstraction is added to a multiphysics package.
201
Factory Patterns
where un and un+1 are the solution at tn and tn+1 , respectively. Given an interface,
field, to a family of 1D scalar eld implementations, we desire to write Fortran
client code statements on a field reference u of the form:
u = u + dt*(nu*u%xx() - u*u%x())
or equivalent C++ client statements of the form:
u += dt*(nu*u.xx() - u*u.x());
where the Fortran and C++ field classes are both abstract, and u can dynamically
(at runtime) take on any type that implements the field interface.
The quandary surrounding the where of object instantiation poses related questions about the when and the how of instantiation. Eliminating the clients control
over the instantiations spatial position in the code offers the potential to also eliminate the clients control over its temporal position and its path to completion.
Flexibility in the timing matters most when an objects construction occupies signicant resources such as memory or execution time. In such cases, it might be desirable
for a constructor to return a lightweight reference or an identifying tag but to delay
the actual resource allocation until the time of the objects rst use. Flexibility in the
steps taken to complete the instantiation matters most when the process involves
constructing multiple parts that must t coherently into a complex whole.
The solutions discussed in Section 9.2 focus primarily on the where question,
moving the object constructor invocation outside the client code and into a class
hierarchy parallel to the one in question. Section 9.4 briey discusses some of the
options for addressing the when and how questions.
Give Me Liberty and Give Me Birth!
The freedom associated with keeping clients references to an object abstract can
be realized throughout the objects lifetime if these references are abstract from
moment of the objects creation. This precludes letting a client invoke the constructors dened in the objects concrete implementation and creates a quandary
regarding how, where, and when the objects instantiation occurs.
203
204
Factory Patterns
abstract, it served as both the ABSTRACT FACTORY and the concrete factory. The
GoF dene two concrete MazeFactory subclasses: EnhantedMaze and BombedMaze.
These overload the aforementioned virtual member functions to produce spellbound
doors and boobytrapped walls, respectively.
In most designs, an ABSTRACT FACTORY contains one or more FACTORY METHOD
implementations, the intent of which the GoF dene as follows:
Dene an interface for creating an object, but let subclasses decide which class to
instantiate. Factory method lets a class defer instantiation to subclasses.
In MazeFactory, the MakeMaze, MakeWall, MakeRoom, and MakeDoor productmanufacturing methods are FACTORY METHODS. Whereas the MazeFactory base class
denes default implementations for these procedures, subclasses can override that
behavior to provide more specic implementations.
The GoF present an alternative based on parameterizing the FACTORY METHOD.
In this case, one denes a single FACTORY METHOD for the entire product family.
The value of a parameter passed to the FACTORY METHOD determines which family
member it constructs and returns.
By analogy with the MazeFactory, an ABSTRACT FACTORY that could support the
Burgers equation client code of Section 9.1 might construct a product family in which
each member abstracts one aspect of a 1D scalar eld. These might include Boundary, Differentiator, and Field classes. The Boundary abstraction could store values
associated with boundary conditions of various types. The Differentiator could provide methods for computing derivatives based on various numerical approximation
schemes. The Field class could store values internal to the problem domain and aggregate a Boundary object and a Differentiator object. Alternatively, all of these
classes could be abstract with default implementations of their methods or with
no method implementations, leaving to subclasses the responsibility for providing
implementations.
By analogy with the EnchantedFactory and BombedFactory, one might dene
additional product families by subclassing each of the classes described in the previous paragraph. For example, a Periodic6thOrder subclass of Field might aggregate
a PeriodicBoundary object and a PeriodicPade6th object. The PeriodicBoundary
subclass of Boundary might or might not store data, considering that the boundary
values are redundant in the periodic case. The PeriodicPade6th subclass of Differentiator could provide algorithms for a sixth-order-accurate Pad nite difference
scheme as described in Appendix A.
Figure 9.1 depicts a minimal class model for supporting the Burgers equation
client code from Section 9.1. The ABSTRACT FACTORY FieldFactory in that gure
publishes a FACTORY METHOD create(). The concrete Periodic6thFactory implements
the FieldFactory interface and therefore provides a concrete create() method. That
method constructs a Periodic6thOrder object, but returns a Field reference, freeing
the client code to manipulate only this reference without any knowledge of the class
of the actual object returned. The greatest benet of this freedom accrues from
the ability to switch between Periodic6thOrder and another Field implementation
without changing the client code. An exercise at the end of this chapter explores this
possibility.
+create()
returns
Periodic6thFactory
Periodic6thOrder
constructs
+create()
Periodic6thOrder constructs a Field product family that has only one member, Periodic6thOrder, under the assumption that any additional dependencies on
other products are either eliminated by subsuming the necessary logic into Periodic6thOrder or delegated to Periodic6thOrder for satisfying internally. The code
examples in Sections 9.2.19.2.2 take the former approach. An example at the end
of this chapter asks reader to design and implement the second approach.
Figure 9.1 debuts the UML diagrammatic technique for adorning relationships.
Adornments indicate the nature of adjacent relationships. The arrows indicate the
implied order for reading the adornment: a Periodic6thFactory object constructs a
Periodic6thOrder object and returns a Field reference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
program main
use field_module ,only : field,initial_field
use field_factory_module ,only : field_factory
use periodic_6th_factory_module ,only : periodic_6th_factory
use kind_parameters ,only : rkind, ikind
205
206
Factory Patterns
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
abstract interface
real(rkind) pure function initial_field(x)
import :: rkind
real(rkind) ,intent(in) :: x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
end function
function field_op_field(lhs,rhs)
import :: field
class(field) ,intent(in) :: lhs,rhs
class(field) ,allocatable :: field_op_field
end function
function field_op_real(lhs,rhs)
import :: field,rkind
class(field) ,intent(in) :: lhs
real(rkind) ,intent(in) :: rhs
class(field) ,allocatable :: field_op_real
end function
real(rkind) function real_to_real(this,nu,grid_resolution)
import :: field,rkind,ikind
class(field) ,intent(in) :: this
real(rkind) ,intent(in) :: nu
integer(ikind),intent(in) :: grid_resolution
end function
function derivative(this)
import :: field
class(field) ,intent(in) :: this
class(field) ,allocatable :: derivative
end function
subroutine field_eq_field(lhs,rhs)
import :: field
class(field) ,intent(inout) :: lhs
class(field) ,intent(in) :: rhs
end subroutine
subroutine output_interface(this)
import :: field
class(field) ,intent(in) :: this
end subroutine
end interface
end module
Figure 9.2(c): periodic_6th_order.f03
module periodic_6th_order_module
use field_module ,only : field,initial_field
use kind_parameters ,only : rkind,ikind
use matrix_module
,only : matrix, new_matrix
implicit none
private
public :: periodic_6th_order
type ,extends(field) :: periodic_6th_order
private
real(rkind) ,dimension(:) ,allocatable :: f
contains
procedure :: add => total
procedure :: assign => copy
procedure :: subtract => difference
procedure :: multiply_field => product
procedure :: multiply_real => multiple
procedure :: runge_kutta_2nd_step => rk2_dt
procedure :: x => df_dx
! 1st derivative w.r.t. x
procedure :: xx => d2f_dx2 ! 2nd derivative w.r.t. x
207
208
Factory Patterns
20
21
procedure :: output
end type
22
23
24
25
26
27
interface periodic_6th_order
procedure constructor
end interface
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
contains
function constructor(initial,grid_resolution)
type(periodic_6th_order) ,pointer :: constructor
procedure(initial_field) ,pointer :: initial
integer(ikind) ,intent(in) :: grid_resolution
integer :: i
allocate(constructor)
allocate(constructor%f(grid_resolution))
if (.not. allocated(x_node)) x_node = grid()
forall (i=1:size(x_node)) constructor%f(i)=initial(x_node(i))
contains
pure function grid()
integer(ikind) :: i
real(rkind) ,dimension(:) ,allocatable :: grid
allocate(grid(grid_resolution))
forall(i=1:grid_resolution) &
grid(i) = 2.*pi*real(i-1,rkind)/real(grid_resolution,rkind)
end function
end function
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function total(lhs,rhs)
class(periodic_6th_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: total
type(periodic_6th_order) ,allocatable :: local_total
select type(rhs)
class is (periodic_6th_order)
allocate(local_total)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function difference(lhs,rhs)
class(periodic_6th_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: difference
type(periodic_6th_order) ,allocatable :: local_difference
select type(rhs)
class is (periodic_6th_order)
allocate(local_difference)
local_difference%f = lhs%f - rhs%f
call move_alloc(local_difference,difference)
class default
stop periodic_6th_order%difference: unsupported rhs class.
end select
end function
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function product(lhs,rhs)
class(periodic_6th_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: product
type(periodic_6th_order) ,allocatable :: local_product
select type(rhs)
class is (periodic_6th_order)
allocate(local_product)
local_product%f = lhs%f * rhs%f
call move_alloc(local_product,product)
class default
stop periodic_6th_order%product: unsupported rhs class.
end select
end function
111
112
113
114
115
116
117
118
119
120
function multiple(lhs,rhs)
class(periodic_6th_order) ,intent(in) :: lhs
real(rkind) ,intent(in) :: rhs
class(field) ,allocatable :: multiple
type(periodic_6th_order) ,allocatable :: local_multiple
allocate(local_multiple)
local_multiple%f = lhs%f * rhs
call move_alloc(local_multiple,multiple)
end function
121
122
123
124
125
126
127
128
129
subroutine copy(lhs,rhs)
class(field) ,intent(in) :: rhs
class(periodic_6th_order) ,intent(inout) :: lhs
select type(rhs)
class is (periodic_6th_order)
lhs%f = rhs%f
class default
stop periodic_6th_order%copy: unsupported copy class.
209
210
Factory Patterns
130
131
end select
end subroutine
132
133
134
135
136
137
138
139
140
141
142
143
function df_dx(this)
class(periodic_6th_order) ,intent(in) :: this
class(field) ,allocatable :: df_dx
class(matrix),allocatable,save :: lu_matrix
integer(ikind) :: i,nx, x_east, x_west
integer(ikind) :: x_east_plus1,x_east_plus2
integer(ikind) :: x_west_minus1,x_west_minus2
real(rkind) ,dimension(:,:) ,allocatable :: A
real(rkind) ,dimension(:)
,allocatable :: b,coeff
real(rkind) :: dx
class(periodic_6th_order) ,allocatable :: df_dx_local
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
nx=size(x_node)
dx=2.*pi/real(nx,rkind)
coeff = first_coeff_6th
if (.NOT. allocated(lu_matrix)) allocate(lu_matrix)
if (.NOT. lu_matrix%is_built()) then
allocate(A(nx,nx))
!__________Initialize coeffecient matrix A _____
A=0.0_rkind
do i=1, nx
x_east = mod(i,nx)+1
x_west = nx-mod(nx+1-i,nx)
if (i==2) then
x_east_plus1=x_east+1; x_west_minus1=nx
x_east_plus2=x_east+2; x_west_minus2=nx-1
else if (i==3) then
x_east_plus1=x_east+1; x_west_minus1=1
x_east_plus2=x_east+2; x_west_minus2=nx
else if (i==nx-1) then
x_east_plus1=1; x_west_minus1=x_west-1
x_east_plus2=2; x_west_minus2=x_west-2
else if (i==nx-2) then
x_east_plus1=nx; x_west_minus1=x_west-1
x_east_plus2=1; x_west_minus2=x_west-2
else
x_east_plus1=x_east+1; x_west_minus1=x_west-1
x_east_plus2=x_east+2; x_west_minus2=x_west-2
end if
A(i,x_west_minus1) =coeff(2)
A(i,x_west)
=coeff(1)
A(i,i)
=1.0_rkind
A(i,x_east)
=coeff(1)
A(i,x_east_plus1) =coeff(2)
end do
lu_matrix=new_matrix(A)
deallocate(A)
end if
allocate(b(nx))
b=0.0
do i=1,nx
x_east = mod(i,nx)+1
x_west = nx-mod(nx+1-i,nx)
if (i==2) then
x_east_plus1=x_east+1; x_west_minus1=nx
x_east_plus2=x_east+2; x_west_minus2=nx-1
else if (i==3) then
x_east_plus1=x_east+1; x_west_minus1=1
x_east_plus2=x_east+2; x_west_minus2=nx
else if (i==nx-1) then
x_east_plus1=1; x_west_minus1=x_west-1
x_east_plus2=2; x_west_minus2=x_west-2
else if (i==nx-2) then
x_east_plus1=nx; x_west_minus1=x_west-1
x_east_plus2=1; x_west_minus2=x_west-2
else
x_east_plus1=x_east+1; x_west_minus1=x_west-1
x_east_plus2=x_east+2; x_west_minus2=x_west-2
end if
202
203
204
205
206
207
208
209
210
b(i)=(0.25*coeff(4)*(this%f(x_east_plus1)-this%f(x_west_minus1))+&
0.5*coeff(3)*(this%f(x_east)-this%f(x_west))+ &
coeff(5)/6.0*(this%f(x_east_plus2)-this%f(x_west_minus2)))/dx
end do
allocate(df_dx_local)
df_dx_local%f=lu_matrix .inverseTimes. b
call move_alloc(df_dx_local, df_dx)
end function
211
212
213
214
215
216
217
218
219
220
221
222
function d2f_dx2(this)
class(periodic_6th_order) ,intent(in)
:: this
class(field) ,allocatable :: d2f_dx2
class(matrix),allocatable, save
:: lu_matrix
integer(ikind) :: i,nx,x_east,x_west
integer(ikind) :: x_east_plus1,x_east_plus2
integer(ikind) :: x_west_minus1,x_west_minus2
real(rkind) ,dimension(:,:) ,allocatable :: A
real(rkind) ,dimension(:)
,allocatable :: coeff,b
real(rkind)
:: dx
class(periodic_6th_order) ,allocatable :: d2f_dx2_local
223
224
225
226
227
228
229
nx=size(this%f)
dx=2.*pi/real(nx,rkind)
coeff = second_coeff_6th
if (.NOT. allocated(lu_matrix)) allocate(lu_matrix)
if (.NOT. lu_matrix%is_built()) then
allocate(A(nx,nx))
230
231
232
233
234
235
236
237
238
239
211
212
Factory Patterns
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
x_east_plus1=x_east+1; x_west_minus1=1
x_east_plus2=x_east+2; x_west_minus2=nx
else if (i==nx-1) then
x_east_plus1=1; x_west_minus1=x_west-1
x_east_plus2=2; x_west_minus2=x_west-2
else if (i==nx-2) then
x_east_plus1=nx; x_west_minus1=x_west-1
x_east_plus2=1; x_west_minus2=x_west-2
else
x_east_plus1=x_east+1; x_west_minus1=x_west-1
x_east_plus2=x_east+2; x_west_minus2=x_west-2
end if
A(i,x_west_minus1) =coeff(2)
A(i,x_west)
=coeff(1)
A(i,i)
=1.0_rkind
A(i,x_east)
=coeff(1)
A(i,x_east_plus1) =coeff(2)
end do
lu_matrix=new_matrix(A)
deallocate(A)
end if
allocate(b(nx))
do i=1, nx
x_east = mod(i,nx)+1
x_west = nx-mod(nx+1-i,nx)
if (i==2) then
x_east_plus1=x_east+1; x_west_minus1=nx
x_east_plus2=x_east+2; x_west_minus2=nx-1
else if (i==3) then
x_east_plus1=x_east+1; x_west_minus1=1
x_east_plus2=x_east+2; x_west_minus2=nx
else if (i==nx-1) then
x_east_plus1=1; x_west_minus1=x_west-1
x_east_plus2=2; x_west_minus2=x_west-2
else if (i==nx-2) then
x_east_plus1=nx; x_west_minus1=x_west-1
x_east_plus2=1; x_west_minus2=x_west-2
else
x_east_plus1=x_east+1; x_west_minus1=x_west-1
x_east_plus2=x_east+2; x_west_minus2=x_west-2
end if
b(i)=(0.25*coeff(4)* &
(this%f(x_east_plus1)-2.0*this%f(i)+this%f(x_west_minus1))+ &
coeff(3)*(this%f(x_east)-2.0*this%f(i)+this%f(x_west))+ &
coeff(5)/9.0* &
(this%f(x_east_plus2)-this%f(i)+this%f(x_west_minus2)))/dx**2
end do
allocate(d2f_dx2_local)
d2f_dx2_local%f=lu_matrix .inverseTimes. b
call move_alloc(d2f_dx2_local, d2f_dx2)
end function
291
292
293
294
subroutine output(this)
class(periodic_6th_order) ,intent(in) :: this
integer(ikind) :: i
do i=1,size(x_node)
print *, x_node(i), this%f(i)
end do
end subroutine
299
300
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
end module
Figure 9.2(d): field_factory.f03
module field_factory_module
use field_module ,only : field,initial_field
implicit none
private
public :: field_factory
type, abstract :: field_factory
contains
procedure(create_interface), deferred :: create
end type
abstract interface
function create_interface(this,initial,grid_resolution)
use kind_parameters ,only : ikind
import :: field, field_factory ,initial_field
class(field_factory), intent(in) :: this
class(field) ,pointer :: create_interface
procedure(initial_field) ,pointer :: initial
integer(ikind) ,intent(in) :: grid_resolution
end function
end interface
end module
Figure 9.2(e): periodic_6th_factory.f03
module periodic_6th_factory_module
use field_factory_module! , only : field_factory
use field_module ! ,only : field,initial_field
implicit none
private
public :: periodic_6th_factory
type, extends(field_factory) :: periodic_6th_factory
contains
procedure :: create=>new_periodic_6th_order
end type
contains
function new_periodic_6th_order(this,initial,grid_resolution)
use periodic_6th_order_module ,only : periodic_6th_order
use kind_parameters ,only : ikind
class(periodic_6th_factory), intent(in) :: this
class(field) ,pointer :: new_periodic_6th_order
procedure(initial_field) ,pointer :: initial
integer(ikind) ,intent(in) :: grid_resolution
new_periodic_6th_order=> periodic_6th_order(initial,grid_resolution)
end function
end module
Figure 9.2(f): matrix.f03
module matrix_module
use kind_parameters ,only : rkind, ikind
213
214
Factory Patterns
3
4
5
6
7
8
9
10
11
12
13
14
15
16
implicit none
private
public :: matrix
public :: new_matrix
type :: matrix
integer(ikind), allocatable :: pivot(:)
real(rkind), allocatable :: lu(:,:)
contains
procedure :: back_substitute
procedure :: is_built
procedure :: matrix_eq_matrix
generic
:: operator(.inverseTimes.) => back_substitute
generic
:: assignment(=) => matrix_eq_matrix
end type
17
18
19
20
interface new_matrix
procedure constructor
end interface
21
22
23
24
25
26
contains
logical function is_built(this)
class(matrix),intent(in)
:: this
is_built = allocated (this%pivot)
end function
27
28
29
30
31
32
33
34
35
36
37
38
n=size(A,1)
allocate(new_matrix)
allocate(new_matrix%pivot(n), new_matrix%lu(n,n))
new_matrix%lu=A
call dgetrf(n,n,new_matrix%lu,n,new_matrix%pivot,info)
end function
39
40
41
42
43
44
45
subroutine matrix_eq_matrix(lhs,rhs)
class(matrix) ,intent(in) :: rhs
class(matrix) ,intent(out) :: lhs
lhs%pivot = rhs%pivot
lhs%lu
= rhs%lu
end subroutine
46
47
48
49
50
51
52
53
54
55
56
57
n=size(this%lu,1)
allocate(lower(n,n), upper(n,n))
:: this
:: b
:: x, temp_x
:: lower, upper
:: local_b
:: n,i,j
:: temp
lower=0.0_rkind
upper=0.0_rkind
do i=1,n
do j=i,n
upper(i,j)=this%lu(i,j)
end do
end do
do i=1,n
lower(i,i)=1.0_rkind
end do
do i=2,n
do j=1,i-1
lower(i,j)=this%lu(i,j)
end do
end do
allocate(local_b(n))
local_b=b
do i=1,n
if (this%pivot(i)/=i) then
temp=local_b(i)
local_b(i)=local_b(this%pivot(i))
local_b(this%pivot(i))=temp
end if
end do
allocate(temp_x(n))
temp_x(1)=local_b(1)
do i=2,n
temp_x(i)=local_b(i)-sum(lower(i,1:i-1)*temp_x(1:i-1))
end do
allocate(x(n))
x(n)=temp_x(n)/upper(n,n)
do i=n-1,1,-1
x(i)=(temp_x(i)-sum(upper(i,i+1:n)*x(i+1:n)))/upper(i,i)
end do
do i=n,1,-1
if (this%pivot(i)/=i) then
temp=x(i)
x(i)=x(this%pivot(i))
x(this%pivot(i))=temp
end if
end do
end function
end module
Figure 9.2. Fortran demonstration of an abstract factory: (a) main client, (b) abstract eld
class, (c) periodic 6th-order Pad eld, (d) abstract eld factory, (e) periodic 6th-order Pad
factory, (f) matrix module.
Fortran constructs appearing for the rst time in Figure 9.2 include typed allocation and procedure pointers. Line 28 in the main program of Figure 9.2(a)
demonstrates how to allocate the dynamic type of a field_factory instance to
be its child class periodic_6th_factory. That allocation yields a concrete object to
which results can be assigned and on which type-bound procedures can be invoked.
215
216
Factory Patterns
1
2
3
4
5
#include
#include
#include
#include
#include
6
7
8
9
10
int main ()
{
typedef field::ptr_t field_ptr_t;
typedef field_factory::ptr_t field_factory_ptr;
11
12
13
14
15
const
const
const
const
16
17
real_t t = 0.0;
18
19
20
field_factory_ptr field_creator =
field_factory_ptr (new periodic_6th_factory());
21
22
field_ptr_t u
23
= field_creator->create(u_initial,
grid_resolution);
24
25
26
27
28
real_t dt;
30
31
32
33
// first substep
u_half = u + (u->xx()*nu - (u*u*half)->x())*dt*half;
34
35
36
// second substep
u += (u_half->xx()*nu - (u_half*u_half*half)->x())*dt;
37
38
39
40
41
t += dt;
42
43
44
45
46
47
1
2
return 0;
#ifndef _FIELD_H_
#define _FIELD_H_ 1
3
4
5
#include "RefBase.h"
#include "globals.h"
6
7
8
9
10
11
12
virtual
virtual
virtual
virtual
13
14
15
16
ptr_t
ptr_t
ptr_t
ptr_t
operator+=(ptr_t)
operator-(ptr_t)
operator*(real_t)
operator*(ptr_t)
= 0;
const = 0;
const = 0;
const = 0;
17
18
19
20
21
22
23
24
25
26
27
};
const = 0;
const = 0;
28
29
30
31
32
33
34
//
// define operators to be used for field ptr_t
//
inline field::ptr_t operator+= (field::ptr_t a, field::ptr_t b) {
*a += b;
217
218
Factory Patterns
35
36
return a;
37
38
39
40
41
42
43
*tmp += b;
return tmp;
44
45
46
47
48
49
50
tmp = *tmp - b;
return tmp;
51
52
53
54
55
56
57
58
tmp = *tmp * b;
return tmp;
59
60
61
62
63
64
65
tmp = *tmp * b;
return tmp;
66
67
1
2
#endif
Figure 9.3(c): field_factory.h
#ifndef _FIELD_FACTORY_H_
#define _FIELD_FACTORY_H_ 1
3
4
5
#include "RefBase.h"
#include "field.h"
6
7
8
9
10
11
12
13
14
15
16
};
17
18
#endif
1
2
3
4
5
#include "field.h"
#include "field_factory.h"
6
7
8
9
10
11
12
13
14
15
1
2
};
#endif
3
4
5
periodic_6th_factory::periodic_6th_factory ()
{ }
6
7
8
periodic_6th_factory::periodic_6th_factory ()
{ }
9
10
11
12
13
14
15
16
1
2
3
4
5
6
#include <vector>
#include "field.h"
#include "globals.h"
7
8
9
10
11
12
13
14
15
16
17
18
19
219
220
Factory Patterns
virtual ptr_t operator*(real_t) const;
virtual ptr_t operator*(ptr_t) const;
20
21
22
23
24
25
26
const;
const;
const;
27
28
29
const;
const;
30
31
32
33
34
35
36
37
38
};
private:
crd_t f_;
static crd_t x_node_;
39
40
1
2
3
4
5
6
#endif
#include
#include
#include
#include
#include
#include
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
f_ = get_grid();
50
51
52
53
54
55
56
57
58
59
60
61
62
63
periodic_6th_order::periodic_6th_order() {
}
64
65
66
67
68
69
70
71
72
73
74
75
throw periodic_6th_order_error();
76
77
78
79
80
81
82
return ptr_t(this);
83
84
85
86
87
221
222
Factory Patterns
if ((other == NULL) || (f_.size() != other->f_.size())) {
std::cerr << "periodic_6th_order::operator- " <<
"rhs is invalid" << std::endl;
88
89
90
91
92
93
throw periodic_6th_order_error();
94
Ref<periodic_6th_order> result =
Ref<periodic_6th_order>(new periodic_6th_order(*this));
95
96
97
98
99
100
101
102
103
return result;
104
105
106
107
108
109
110
111
112
113
114
115
return result;
116
117
118
119
120
121
122
123
124
125
126
throw periodic_6th_order_error();
127
Ref<periodic_6th_order> result =
Ref<periodic_6th_order>(new periodic_6th_order(*this));
128
129
130
131
132
133
134
135
136
return result;
137
138
139
140
141
142
150
151
152
153
154
155
156
157
158
159
160
161
crd_t coeff;
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
if (i == 1) {
x_east_plus1=x_east+1;
x_east_plus2=x_east+2;
x_west_minus1=nx-1;
x_west_minus2=nx-2;
}
else if (i == 2) {
x_east_plus1=x_east+1;
x_east_plus2=x_east+2;
x_west_minus1=0;
x_west_minus2=nx-1;
}
else if (i == nx-2) {
x_east_plus1=0;
x_east_plus2=1;
x_west_minus1=x_west-1;
x_west_minus2=x_west-2;
}
else if (i == nx-3) {
x_east_plus1=nx-1;
x_east_plus2=0;
x_west_minus1=x_west-1;
x_west_minus2=x_west-2;
}
else {
223
224
Factory Patterns
198
199
200
201
202
x_east_plus1=x_east+1;
x_east_plus2=x_east+2;
x_west_minus1=x_west-1;
x_west_minus2=x_west-2;
203
A(i,x_west_minus1)
A(i,x_west)
A(i,i)
A(i,x_east)
A(i,x_east_plus1)
204
205
206
207
208
=coeff.at(1);
=coeff.at(0);
=1.0;
=coeff.at(0);
=coeff.at(1);
209
210
211
212
213
214
215
b.at(i) = (0.25*coeff.at(3)*
(f_.at(x_east_plus1)-f_.at(x_west_minus1))+
0.5*coeff.at(2)*(f_.at(x_east) - f_.at(x_west)) +
coeff.at(4)/6.0 * (f_.at(x_east_plus2)f_.at(x_west_minus2)))/dx;
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
crd_t coeff;
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
if (i == 1) {
x_east_plus1=x_east+1;
x_east_plus2=x_east+2;
x_west_minus1=nx-1;
x_west_minus2=nx-2;
}
else if (i == 2) {
x_east_plus1=x_east+1;
x_east_plus2=x_east+2;
x_west_minus1=0;
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
A(i,x_west_minus1)
A(i,x_west)
A(i,i)
A(i,x_east)
A(i,x_east_plus1)
274
275
276
277
278
=coeff.at(1);
=coeff.at(0);
=1.0;
=coeff.at(0);
=coeff.at(1);
279
280
281
282
283
284
285
b.at(i) = (0.25*coeff.at(3)*
(f_.at(x_east_plus1)-2.0*f_.at(i)+f_.at(x_west_minus1))+
coeff.at(2)*(f_.at(x_east)-2.0*f_.at(i)+f_.at(x_west)) +
coeff.at(4)/9.0 * (f_.at(x_east_plus2)f_.at(i)+f_.at(x_west_minus2)))/(dx*dx);
286
287
288
289
290
291
292
293
294
dx=2.0*pi/grid_resolution;
295
296
k_max=grid_resolution*0.5;
297
298
CFL=2.0/(24.0*(1-cos(k_max*dx))/11.0/(1.0+4.0/11.0*cos(k_max*dx))+
3.0*(1.0-cos(2.0*k_max*dx))/22.0/(1.0+4.0/11.0*cos(k_max*dx)));
return (CFL*dx*dx/nu);
299
300
301
302
303
1
2
#ifndef INITIALIZER_H_
#define INITIALIZER_H_
225
226
Factory Patterns
3
4
#include "globals.h"
5
6
7
8
9
#endif
2
3
4
5
6
7
8
9
10
11
12
13
sister class. It then benets from the same exibility a FACTORY METHOD affords
clients.
Another consequence of this chapters two patterns lies in the resulting proliferation of classes. Were the Burgers equation solver architect willing to expose
Periodic6thOrder to the client, she could eliminate the other three classes. Obviously,
the exibility afforded by not exposing it must be balanced against the complexity
associated with hiding it.
The latter paragraph strikes a theme that recurs throughout this text. Flexibility
often comes at the cost of increased complexity. Engendering exibility in an application requires increasing the sophistication of the infrastructure that supports it.
This proved true in Part I of the text, wherein we argued that the sophistication of
OOP matters most as a software application grows larger. A similar statement holds
for the design patterns in Part II. They prove more worthwhile with increasing scale
in OOP projects. A successful designer allows for growth while consciously avoiding
slicing butter with a machete.
Up to this point, we have emphasized the role of factory patterns in determining
where object construction occurs: inside factories. In the context of private data,
where also determines who. To the extent factories hide concrete classes from clients,
they assume sole responsibility for object construction.
Other creational patterns also manage the how of object construction. The GoF
BUILDER pattern, for example, adds a Director abstraction between the client a
Builder interface. Clients direct the Director to construct a product. The concrete
Builder steps through the construction of the parts required to piece together a
complex object. This isolates the client from even knowing the construction steps,
in contrast to the MazeFactory example in which the client individually invokes
methods to manufacture the mazes constituent parts.
Lastly, the GoF SINGLETON pattern address one aspect of when construction
happens: upon the rst request and only then. A SINGLETON instance ensures that
only one instance of a class gets instantiated. This provides an OOD alternative to
global data, ensuring all clients access the same instance.
227
228
Factory Patterns
EXERCISES
1. Augment the UML class diagram of Figure 9.1 by including interfaces and corresponding implementations of Boundary and Differentiator abstractions. Modify
the Fortran code in Figure 9.2 or the C++ code in Figure 9.3 so that the Periodic6thOrder constructor instantiates and stores Boundary and Differentiator
abstractions.
2. Refactor the code from Exercise 2 so that the Periodic6thOrder stores a pointer
to a DifferentationStrategy using the STRATEGY pattern of Chapter 7. Supply an
additional DifferentiatorStrategy8th eighth-order-accurate Pad nite difference
scheme as dened in Appendix A.
PA R T III
G UMBO S OOP
10 Formal Constraints
232
Formal Constraints
that will very likely fail in some context. Examples include interface inconsistencies
and using uninitialized variables. Hatton suggested that formal methods reduce defect density by a factor of three (cited in J3 Fortran Standards Technical Committee
1998). This chapter shows how code inspired by formal constraints can also detect
compiler faults.
Pace (2004) expressed considerable pessimism about the prospects for adopting formal methods in scientic simulation because of the requisite mathematical
training, which often includes set theory and a predicate calculus. A candidate for
adoption must balance such rigor against ease of use. The Object Constraint Language (OCL) strikes such a balance, facilitating expressing of formal statements
about software models without using mathematical symbols unfamiliar to nonspecialists. OCLs designers intended for it to be understood by people who are not
mathematicians or computer scientists (Warmer and Kleppe 2003).
To attract scientic programmers, any software development strategy must address performance. Fortunately, program specication and verication are part of
the software design rather than the implementation. Thus, they need not impact
run-time performance. However, run-time checking of assertions, a third pillar of formal methods, is part of the implementation (Clarke and Rosenblum 2006; Dahlgren
2007). Assertions are tool-based or language-based mechanisms for gracefully terminating execution whenever programmer-inserted Boolean expressions fail. When
the Boolean expressions are sufciently simple, they occupy a negligible fraction of
the run time compared to the long loops over oating point calculations typical of
scientic software.
A nal factor inuencing adoption of formal methods is the lack of a common approach for describing the structure of traditional scientic codes beyond ow charts.
OCLs incorporation into the UML standard (Warmer and Kleppe 2003) suggests
that newcomers must simultaneously leap two hurdles: learning OCL and learning UML. Fortunately, increasing interest in object-oriented scientic programming
has led to more frequent scientic program structural descriptions resembling UML
class diagrams (Akin 2003; Barton and Nackman 1994), and several scientic programming projects use automated documentation tools that produce class diagrams
(Heroux et al. 2005).
This chapter details how exposure to formal methods inspired systematic runtime assertion checking in a production research code, and how these assertions
enabled the detection of an inscrutable compiler bug. The chapter also explains how
applying OCL constraints to a UML software model forced us to think abstractly
about an otherwise language-specic construct, pointers, and how this abstraction
process inspired a workaround to a dilemma at the intersection of language design
and existing compiler technology.
The remaining sections apply constraints on memory use in the Burgers
equation solver of Chapter 9. Section 10.2 denes the problem that the constraints address. Section 10.3 describes an aspect of Fortran that poses a relevant
dilemma. Section 10.4 species a useful set of constraints on a UML class diagram.
Section 10.5 describes a useful design pattern that facilitates applying the constraints.
Section 10.7 details performance results from a case study culled from a research
application.
Surrogate
Hermetic
Integrand
IntegrationStrategy
uses
Burgers
RK2
FieldFactory
Field
ret
urn
HermeticField
HermeticFieldFactory
constructs
233
234
Formal Constraints
and construct a Burgers object and pass it repeatedly to integrate for advancement
one time step per invocation. Advancing from an initial time t to a nal time t_final
over time increments dt might take the form:
type(burgers) :: cosmos
cosmos = burgers()
do while (t<t_final)
call integrate(cosmos,dt)
t = t + dt
end do
where we have assumed the existence of default component initialization in the
overloaded structure constructor burgers.
In Figure 10.1, the Burgers class aggregates a reference to a Field component u.
Since Field is abstract, this reference must be a pointer or allocatable component
in Fortran. The structure constructor would use this component to dynamically allocate a Field instance. The closed diamond on the Burgers end of the Burgers/Field
relationship indicates that this instances lifetime coincides with that of the Burgers
object. It gets constructed when the Burgers object is constructed and nalized when
the Burgers object gets nalized.
Because this chapter focuses on situations where one needs to monitor compilers compliance with the memory management stipulations in the Fortran 2003
standard, the Field class in Figure 10.1 extends the Hermetic class described in
Chapter 5. This gives HermeticField utilities to assist in avoiding memory leaks
and dangling pointers. It also forces the HermeticField developer to dene a
force_finalization procedure intended to facilitate manual invocation of the nal
subroutine.
The Burgers class of Figure 10.1 makes use of an ABSTRACT FACTORY and
its concrete implementations FACTORY METHOD. The Burgers constructor uses a
HeremticFieldFactory to construct a HermeticField. It returns a pointer to an
abstract Field.
The Integrand interface uses STRATEGY and SURROGATE pattern implementations
to advance the Burgers object. Advancement happens via the RK2 algorithm in
the design shown. As demonstrated in Figure 9.1, each RK2 substep involves a
invoking the time differentiation method t() to evaluate the RHS of the Burgers
equation. This evaluation might take the form of the code in Figure 10.2, where
burgers extends the integrand of Figure 6.2(b) and aggregates a pointer to a Field
from Figure 9.2(b) and where we have written Burgers equation at line 22 in the
nonconservative form of equation 4.9 for simplicity.
The associate construct debuts in this text in burgers_module at line 23. It allows the creation of local aliases. In the case shown, v and dv_dt become a shorthand
for this%u and d_dt%u, respectively.
The procedures a compiler invokes to evaluate the RHS of line 24 include the
type-bound operators operator(*) and operator(-) and the type-bound functions
xx() and x(). Figure 10.3 shows the corresponding call tree.
The object dv_dt in Figure 10.3 ultimately becomes the result of the t()
type-bound procedure call in a time integration procedure such as integrate in
module burgers_module
use integrand_module ,only : integrand
implicit none
private
public :: burgers
type ,extends(integrand) :: burgers
class(field) ,allocatable :: u
contains
procedure :: t
procedure :: add => total
procedure :: assign
procedure :: multiply => product
generic :: operator(*) => product
generic :: assignment(=) => assign
end type
contains
function t(this) result(d_dt)
class(burgers) :: this
type(burgers) :: d_dt
class(field) ,pointer :: v, dv_dt
if (.not. allocated(this%u)) stop invalid argument inside t()
allocate(d_dt%u,source=this%u)
associate( v => this%u , dv_dt =>d_dt%u)
dv_dt = v%xx()*nu - v*v%x()
end associate
end function
! (additional type-bound procedures omitted)
end module burgers_module
Figure 10.2. Partial implementation of a Burgers solver class.
235
236
Formal Constraints
v%xx()
operator(*)
v%x()
operator(*)
operator(-)
dv_dt
assignment(=)
(b)
Figure 10.3. Burgers equation: (a) abstract calculus and (b) the resultant call tree.
0.5*dt
operator(*)
this_half
this
operator(+)
assignment(=)
(b)
Figure 10.4. RK2 predictor step: (a) abstract calculus and (b) the resultant call tree.
Stewart (2003), which handles a broader set of cases and allows us to leverage the
OBJECT pattern implementation of Chapter 5.
For the remainder of Chapter 10, we refer to all functions called in ABSTRACT
CALCULUS expressions as operators. In practice, some of these would be generic
type-bound operators and dened assignments, for example, operator(*) and
assignment(=) in Figure 10.3(b). Others would be functions that mimic integral
or differential mathematical operators such as xx(). With this nomenclature, we
dene intermediate results as temporary according to the following:
Denition 10.1. Any object is dened as temporary if it can be safely nalized at the
termination of the rst operator to which it is passed as an argument.
Corollary 10.1. All objects that are not temporary are persistent.
In the OBJECT implementation of Figure 5.2, any object that extends hermetic
starts life with a temporary component pointer (in its hermetic parent) initialized
to null(). This defaults all objects to being persistent. With this denition and its
corollary in hand, one can summarize the Stewart memory management scheme in
ve rules:
Rule 10.1. Mark all operator results as temporary.
Rule 10.2. Mark all left-hand arguments to dened assignments as persistent.
Rule 10.3. Guard all temporary operator arguments upon receipt.
Rule 10.4. Finalize all temporary operator arguments that were received unguarded.
Rule 10.5. Finalize persistent objects inside the procedure that instantiated them.
These rules constrain code behavior. The challenge for this chapter lies in expressing these constraints in OCL and incorporating the resulting formalisms into
the UML class model.
It Beats Flipping Burgers
The continual turnover of Burgers object allocations illustrates the necessity for
disciplined memory management by the compiler or the developer. Sorting out
when to free an objects memory can be difcult or impossible in the absence of
rules that distinguish temporary from persistent objects.
237
238
Formal Constraints
approach has proved problematic with recent versions of several compilers. That
section also explains that the pointer-component approach remains infeasible for
most compilers. Even when either approach works, developers might desire to control the memory utilization more closely in order to recycle allocated space or to free
it sooner than the compiler might.
Fortrans requirement that type-bound operators and the procedures they call be
free of side effects greatly complicates programmers ability to free memory allocated
for operator arguments. The intent(in) attribute enforces the requirement but
also poses a dilemma. In nested invocations of operators, an argument passed to
one operator might be a result calculated by another operator of higher precedence.
That higher-precedence operator is likely to have allocated memory for its result
just as the lower-precedence operator might allocate memory for its result. The
easiest and most efcient place for the programmer to release memory that was
dynamically allocated inside the result of one operator is inside the operator to which
this result is passed. However, the operator receiving the result cannot modify it. A
similar dilemma relates to dened assignments such as the assignment(=) binding
in Figure 10.2, wherein there frequently arises a need to free memory associated
with allocatable components of the RHS argument if it is the result of an expression
evaluation.
Even compilers that do the proper deallocations might not do so economically.
For example, the rst author has analyzed intermediate code received from one vendor and found that this vendors compiler carries along all the memory allocated at
intermediate steps in the call tree, performing deallocations only after the nal assignment at the top of the tree (Rouson et al. 2006). In complicated expressions written
on large objects, that vendors approach could make ABSTRACT CALCULUS infeasible.
Hence, in addition to correcting for compiler nonconformance with the Fortran standards memory management stipulations, the rules and resulting constraints in this
chapter economize memory usage in ways the standard does not require.
Warning: Calculus May Cause Memory Loss, Bloating, and Insomnia
Fortrans prohibition against side effects in type-bound operators complicates the
process of explicitly deallocating objects before they go out of scope and can lead
to unrestrained growth in memory usage. Even compilers that correctly automate
deallocation or nalization might not do so economically, resulting in developers
losing sleep strategizing to free memory sooner than would the compiler.
Because OCL is a subset of UML, however, types dened by a UML model are considered OCL model types. We can therefore incorporate the Array type in Figure 2.3
into models for use in OCL. We do so in the remainder of this chapter for the 1D
case and leave the multidimensional case as a modeling exercise for the reader.
10.4.2 Hermeticity
We can now specify the constraints that preclude a category of memory leaks in
composite operator calls. We refer to leak-free execution as hermetic memory
management, or simply hermeticity. The memory of interest is associated with the
allocatable array components inside data structures passed into, through, and out
of call trees of the form of Figures 10.310.4. We assume no other large memory
allocations inside operators. This assumption is discussed further vis--vis economy
in Section 10.4.3.
Figure 10.5 provides a partial class diagram, including several ABSTRACT CALCULUS operators: the unary operator x(), binary operator add, and a dened assignment
assign. Our primary task is to constrain the behavior of such operators using the
memory management rules listed in Section 10.2. We model Denition 10.1 and
Corollary 10.1 simply by including in the HermeticField class a temporary attribute,
modeled as an instance of the Boolean model type described in Figure 2.3. The value
of this attribute classies the object in the set of objects dened by the Denition or
in the complementary set dened by the Corollary. The use of the Boolean temporary attribute demonstrates several differences between a software model and a
software implementation. For example, an implementation of HermeticField would
likely extend Hermetic as in Figure 10.1, thereby inheriting the temporary and
depth attributes as well as the force_nalization() method. That level of detail
is unnecessary for purposes of writing the intended constraints. Along this vein, to
consider the hermetic derived type component temporary in Figure 5.2 to be an implementation of the HermeticField temporary attribute, one has to map the integer
value 0 for the component to the value True for the Boolean type in Figure 2.3, and
map all nonzero values to False. Finally, even the last artice misses the detail that
the temporary component in the hermetic implementation Figure 5.2 is a pointer.
UML does not contain a pointer type or attribute, but the modeling of pointers in
UML plays a central role in Section 10.4.3, so we defer further discussion to that
section.
+
+
+
+
HermeticField
values : Array<Real>
temporary : Boolean
depth : Integer
x() : HermeticField
add(HermeticField) : HermeticField
assign(HermeticField)
force_nalization()
239
240
Formal Constraints
In formal methods, constraints take three forms: preconditions that must be true
before a procedure commences; postconditions that must be true after it terminates;
and invariants that must be true throughout its execution. OCL preconditions bear
the prex pre:, whereas post: precedes postconditions and inv: precedes
invariants. The contexts for OCL pre- and postconditions are always operations
or methods. When writing OCL constraints as standalone statements, a software
designer writes the constraint context above the constraint. Hence, Rule 10.1 applied
to the Hermetic method SetTemp() in Figure 10.6 could be written:
context : Hermetic :: SetTemp()
post: temporary = true
Alternatively, the designer indicates the context by linking the constraint to the
corresponding operations visually in a UML diagram. The visual link might involve
writing a constraint just after the method it constrains in a class diagram or by drawing
connectors between the constraint and the method as in Figure 10.6. When using
connectors, one commonly encloses the constraint in a UML annotation symbol as
Figure 10.6 depicts.
The argument and return value declarations in Figure 10.6 reect OCL conventions along with a desire for succinct presentation. For both of these reasons,
Figure 10.6 omits the implicit passing of the object on which the method is invoked.
In C++ and in many of the Fortran examples in this book, that object is referenced
by the name this. In OCL, that object is named self and can be omitted in the
interest of brevity. The postconditions applied to the Hermetic in Figure 10.6 omit
self because it is the only object in play. Several postconditions applied to HermeticField, however, include it to explicitly distinguish its attributes from those of the
other the objects received or returned by the methods.
We formally specify Rule 10.1 through postconditions on the arithmetic and
differential operators x() and add(), respectively, and Rule 10.2 through a postcondition on the dened assignment assign(). Specically, two postconditions in
Figure 10.6 specify that the results of x() and add() must be temporary. Another
postcondition species that the LHS argument (self) passed implicitly to assign
must be persistent.
Rule 10.3 governs the treatment of the subset of temporary objects that must
be guarded against premature nalization. According to the memory management
scheme of Stewart (2003), the GuardTemp() procedure guards a temporary object by
incrementing its depth count. The postcondition applied to the like-named method
in Figure 10.6 stipulates this.
Formalization of the guarded state dened by rule 10.3 requires making a
statement about the state of a class of objects throughout a segment of the program
execution. One naturally expresses such concepts via invariants. In OCL invariants, the context is always a class, an interface, or a type. For modeling purposes,
Figure 10.6 therefore denes a GuardedField class that exists only to provide a welldened subset of HermeticField specically for purposes of specifying the invariant
that subset satises: depth>1.
As the GoF demonstrated, class diagrams also provide an effective medium
through which to describe conforming implementations. The note linked to the
Hermetic
temporary : Boolean
depth : Integer
{post: temporary=True implies depth = depth + 1}
+ SetTemp()
+ GuardTemp()
+ CleanTemp()
...
if (depth==1)call force_finalization()
+ force_finalization()
HermeticField
values : Array<Real>
+ x() d_dx : HermeticField
+ add(rhs : HermeticField) total : HermeticField
+ assign(rhs : HermeticField)
GuardedField
...
type (GuardeField),pointer :: guarded_self, guarded_rhs
call precondition(self,rhs)
call self.GuardTemp(); call rhs.GuardTem()
guarded_self => GuardeField(self)
guarded_rhs => GuardedField(rhs)
call do_add(guarded_self,guarded_rhs)
call self.CleanTemp(); call rhs.CleanTemp()
Call postcondition(self,rhs)
...
241
242
Formal Constraints
Next the do_add() procedure would then sum its GuardedField arguments, the
type denition for which would be sufciently minimal and intimate in its connection to HermeticField that it would make sense for it to have access to the
private data inside its parent HermeticField. In Fortran, this would take place
most naturally by dening both types in one module. The CleanTemp() procedure would unguard temporary objects by decrementing their depth count and
would call force_finalization if the depth count drops to unity, as demonstrated
in the sample code linked to that method in Figure 10.6. Finally, the procedure
postcondition() would check assertions inspired formal postconditions.
Rule 10.4 governs the nalization of temporary objects. It applies to all operators
and dened assignments. As applied to x(), the corresponding formal constraint
takes the form of the second postcondition linked to that method in Figure 10.6.
Predicate calculus stipulates that this postcondition evaluates to true if the expression
after the implies operator is true whenever the expression before the implies is true.
The constraint also evaluates to true whenever the expression before the implies is
false.
Rule 10.5 governs the nalization of persistent objects. Only dened assignments
create persistent objects. Since all HermeticField operators have access to private
HermeticField components, they can make direct assignments to those components.
HermeticField operators therefore do not call the dened assignment procedure, so
most persistent HermeticField objects are created in client code outside the HermeticField class. For this reason, no postcondition corresponding to Rule 10.5 appears
in Figure 10.6.
As valid OCL expressions, the constraints in Figure 10.6 are backed
by a formal grammar dened in Extended Backus-Naur Form (EBNF)
(Rouson and Xiong 2004). An EBNF grammar species the semantic structure of
allowable statements in a formal language. The statements meanings can therefore
be communicated unambiguously as part of the UML design document. An additional advantage of formal constraints is their ability to express statements that could
not be written in an executable language. One example is the relationship between
the Boolean expressions in the implied statement in Figure 10.6. Another is the fact
that these constraints must be satised on the set of all instances of the class. Programs that run on nite-state machines cannot express conditions on sets that must
in theory be unbounded. A third example is any statement about what must be true
at all times, such as the invariant constraint on the GuardedField class in Figure 10.6.
In general, an additional benet of formal methods accrues from the potential to
use set theory and predicate logic to prove additional desirable properties from the
specied constraints. However, such proofs are most naturally developed by translating the constraints into a mathematical notation that is opaque to those who are
not formal methods specialists. Since OCL targets non-specialists, proving additional
properties mathematically would detract from the main purpose of simply clarifying
design intent and software behavior.
10.4.3 Economy
Rening Rules 10.110.5 can lead to more economical memory usage. Although
the memory reductions are modest in some instances, we hope to demonstrate
an important tangential benet from the way OCL forces designers to think more
abstractly about otherwise language-specic constructs. For this purpose, consider
again the function add(), which in the model of Figure 10.6 takes the implicit argument self and the explicit argument rhs and returns total. We can ensure
economical memory usage by specifying that temporary memory be recycled. To facilitate this, the scalars self, rhs, and total must have the pointer or allocatable
attribute. In many respects, the allocatable approach is more robust: It obligates
the compiler to free the memory when it goes out of scope and the move_alloc()
intrinsic facilates moving the memory allocation from one object to another without costly deallocation and reallocation (see Section 6.2.1). However, because
allocatable scalars are a Fortran 2003 construct without universal compiler support,
the pointer approach is desirable from the standpoint of portability. We consider
that approach for the remainder of this chapter.
To pass pointers in Fortran, one writes the add() function signature as:
function add(self,rhs) result(total)
class(HermeticField), pointer, intent(in) :: self
type(HermeticField), pointer, intent(in) :: rhs
type(HermeticField), pointer :: total
Since OCL does not have a pointer type, however, we model pointers as a UML association between two classes. Specically, we model self, rhs, and total as instances
of a HermeticFieldPointer class that exists solely for its association with the HermeticField class. We further assume HermeticFieldPointer implements the services of
Fortran pointers, including an associated() method that returns a Boolean value
specifying whether the object on which it is invoked is associated with the object
passed explicitly as an argument.
Figure 10.7 diagrams the HermeticFieldPointer/HermeticField relationship. A
diagrammatic technique appearing for the rst time in that gure is the adornment
of either end of the relationship with multiplicities (see Section 2.5) describing the
allowable number of instances of the adjacent classes. The adornments in Figure 10.7
indicate that one or more HermeticFieldPointer instances can be associated with any
one HermeticField instance.
In Figure 10.7, we apply the label target to the HermeticField end of the association. From a HermeticFieldPointer object, this association can be navigated
through the OCL component selection operator ., so an economical postcondition might take the form shown in the annotation linked to HermeticFieldPointer,
wherein the implied conditions stipulate that total must be associated with one of
HermeticFieldPointer
1*
+ Boolean : associated(HermeticField)
+ add(rhs : HermeticFieldFieldPointer)
HermeticField
target
243
244
Formal Constraints
along with a corresponding constructor and various dened operators such as:
function add(self,rhs) result(total)
class(HermeticFieldPointer) ,intent(in) :: self
type(HermeticFieldPointer) ,intent(in) :: rhs
type(HermeticFieldPointer)
:: total
end function
facilitates satisfying the Fortrans intent(in) requirement and restricts us from
changing with what target object the target_field is associated while allowing us
to deallocate any allocatable entities inside the target with compilers that fail to do so.
Since HermeticFieldPointer exists only to wrap a HermeticField instance, one
might refer to the former as a shell and the latter as a kernel. Together they comprise
a SHELL pattern. Chapter 11 describes similar instances in which a lightweight class
serves as a wrapper or identication tag for another class. An exercise at the end
of the current chapter asks the reader to write a SHELL pattern description using the
three-part rule of Section 4.1.
245
246
Formal Constraints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module assertion_utility
use iso_fortran_env ,only : error_unit
implicit none
private
public :: error_message,assert,assert_identical
type error_message
character(:) ,allocatable :: string
end type
contains
subroutine assert(assertion,text)
logical ,dimension(:) ,intent(in) :: assertion
type(error_message) ,dimension(:) ,intent(in) :: text
integer :: i
logical :: any_failures
call assert_identical( [size(assertion),size(text)] )
any_failures=.false.
do i=1,size(assertion)
if (.not. assertion(i)) then
any_failures=.true.
write(error_unit,*) Assertion failed with message:
if (allocated(text(i)%string)) then
write(error_unit,*) text(i)%string
else
write(error_unit,*) (no message provided).
end if
end if
end do
if (any_failures) stop Execution halted on failed assertion(s)!
end subroutine
subroutine assert_identical(integers)
integer ,dimension(:) ,intent(in) :: integers
integer :: i
logical :: any_mismatches
any_mismatches = .false.
do i=2,size(integers)
type(field) :: a, b, c
a = field()
b = field()
call do_something(a + b, c)
247
248
Formal Constraints
where we assume the field type contains one or more allocatable components and
a logical component that identies it as temporary or persistent. We further assume
the field() constructor allocates memory for allocatable components inside a and
b and gives those components default values. In this scenario, the addition operator
invoked inside the rst argument slot at line 4 would mark its result temporary based
on Rule 10.6.
Next consider the following denition of the above do_something procedure:
1
2
3
4
5
6
subroutine do_something(x, y)
type(field) ,intent(in) :: x
type(field) ,intent(inout) :: y
y =x
y = x + y
end subroutine
where the temporary object now referenced by x inside do_something() would have
its allocatable components deallocated by the dened assignment at line 4 in the
latter code in accordance with Rule 10.6. That deallocation would in turn preclude
proper execution of the addition operator in the subsequent line. Nonetheless, the
current case study involved no such scenarios and the simplied Rules 10.610.9
sufced.
Figure 10.9 presents precondition() and postcondition() subroutines
adapted from Rouson et al. (2006) using the assertion utility of Figure 10.8. In
the subject turbulence code, each field instance contains two allocatable array
components: One stores a physical-space representation and another stores the corresponding Fourier coefcients. Since one can always compute Fourier coefcients
from the physical-space samples that generated those coefcients, and vice versa,
having both allocated is either redundant or inconsistent. It is therefore never valid
for both to be allocated except momentarily in the routine that transforms one to
the other. This condition is checked in the Figure 10.9.
The aforementioned turbulence simulations employed analogous routines. The
code called a precondition() procedure at the beginning of each field method. It
called a postcondition() procedure at the end of each method.
Besides monitoring the source code behavior, these assertions proved eminently
useful in checking for compiler bugs. Using these routines, Rouson et al. (2006) discovered that one vendors compiler was not always allocating arrays as requested.
Despite repeated attempts to reproduce this problem in a simple code to submit in
a bug report, the simplest demonstration of the error was a 4,500-line package in
which the error only occurred after several time steps inside a deeply nested call
tree. The related saga cost several weeks of effort plus a wait of more than a year
for a compiler x. The disciplined approach to checking assertions at the beginning
and end of each procedure, as inspired by the OCL pre- and postconditions, paid its
greatest dividend in motivating a switch to a different compiler.
Figure 10.10 presents a procedure that veries economical memory recycling
based on a SHELL class named field_pointer. Rouson et al. (2006) used analogous
code to check whether memory allocated for temporary arguments had been recycled
by associating that memory with the function result.
subroutine precondition(this,constructor)
use assertion_utility ,only : assert
implicit none
type(field) ,intent(in) :: this
logical ,intent(in) :: constructor
logical :: both_allocated ,at_least_one_allocated
both_allocated= allocated(this%fourier) .and. allocated(this%physical)
call assert( [.not. both_allocated] &
,[error_message(redundant or inconsistent argument)])
at_least_one_allocated = &
allocated(this%fourier) .or. allocated(this%physical)
if (constructor) then
call assert( [.not. at_least_one_allocated] &
,[error_message(constructor argument pre-allocated)])
else; call assert( [at_least_one_allocated] &
,[error_message(argument data missing)])
end if
end subroutine
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
subroutine postcondition(this,public_operator,deletable)
use assertion_utility ,only : assert
implicit none
type(field) ,intent(in) :: this
logical ,intent(in) :: public_operator,deletable
logical :: both_allocated,at_least_one_allocated
both_allocated= allocated(this%fourier) .and. allocated(this%physical)
call assert( [.not. both_allocated] &
,[error_message(redundant or inconsistent result)])
at_least_one_allocated = &
allocated(this%fourier) .or. allocated(this%physical)
if (public_operator .and. deletable) then
if (this%temporary) then
call assert([.not. at_least_one_allocated] &
,[error_message(temporary argument persisting]))
else; call assert([at_least_one_allocated] &
,[error_message(persistent argument deleted)])
end if
else
call assert([at_least_one_allocated] &
,[error_message(invalid result]))
end if
end subroutine
Figure 10.9. Memory allocation assertions in a Fourier-spectral turbulence code.
Since our pre- and postcondition subroutines contain simple Boolean expressions, they require insignicant amounts of execution time. Table 10.1 shows that the
most costly procedure for the case studied by Rouson et al. (2006) the transform(),
which contained 3D FFT calls. That procedure accounted for 34 percent of the
processor time. By contrast, precondition() and postcondition() occupied immeasurably low percentages of execution time even though the number of calls to
these routines exceeds the calls to transform() by roughly an order of magnitude.
249
250
Formal Constraints
1
2
3
4
5
6
7
8
9
10
11
12
13
subroutine economical_postcondition(lhs,rhs,result)
use assertions ,only : assert
implicit none
type(field_pointer) ,intent(in) :: lhs,rhs,result
if (lhs%target%temporary) then
call assert( [associated(result%target,lhs%target)] &
,[error_message(lhs not recycled]))
else if (right%target%temorary) then
call assert( &
[associated(result%target,rhs%target)] &
,[error_message(rhs not recycled]))
end if
end subroutine
Figure 10.10. Memory economy assertions in a Fourier-spectral turbulence code.
Function Name
transform
assign
xx
dealias
add
times
...
precondition
postcondition
Number of Calls
% of Total Runtime
108
167
156
36
75
75
...
1477
885
34.42
19.67
16.18
7.70
7.43
5.03
...
0
0
Thus, the capability to turn assertions off appears to be nonessential in the chosen application. This observation is likely to hold for the majority of demanding,
leading-edge scientic applications.
EXERCISES
1. Write a set of preconditions prescribing desirable memory management behavior
for the methods in Figure 10.6.
2. Using the three-part rule of Section 4.1, write a more complete description of the
SHELL pattern.
3. Modify the assertion utility of Figure 10.8 so that the error_message derived
type has private data along with a error_message generic procedure that overloads the intrinsic structure constructor and supports the constructor call syntax
in Figure 10.9.
11 Mixed-Language Programming
252
Mixed-Language Programming
ambitious class of tools eliminates the quadratic growth in ordered pairings by inserting an intermediary through which an object declared in one language communicates
with objects in all other languages without either side knowing the identity of the
other side. The intermediary plays a role analogous to the MEDIATOR mentioned in
Sections 4.2.3 and 8.1. Such tools generally work with a common interface representation: an Interface Denition Language (IDL). The remainder of this section
discusses tools that follow both approaches.
The Simplied Wrapper and Interface Generator (SWIG) automates the interface construction process. SWIG interfaces C and C++ code with codes written in
several other languages, including Python, Perl, Ruby, PHP, Java, and C#1 . To access
C functions, for example, from another language, one writes a SWIG interface le.
This le species the function prototypes to be exported to other languages. From an
interface le, SWIG generates a C wrapper le that, when linked with the rest of the
project code, can be used to build a dynamically loadable extension that is callable
from the desired language. Often, the developer need not write the interface le line
by line and can instead incorporate a C/C++ header le in a SWIG interface le via
a single #include directive.
SWIG does not provide direct support for interfacing to Fortran. Since SWIG
interfaces to Python, one workaround involves using the PyFort tool2 that enables
the creation of Python language extensions from Fortran routines as suggested by
Pierrehumbert3 . PyFort supports Fortran 77 with plans for supporting Fortran 90,
but Pierrehumbert found it easier to manually construct C wrappers for Fortran
routines.
The Chasm language interoperability toolkit targets scientic programmers.
Chasm enables bidirectional invocations between statically typed languages that is
languages in which type information can be discovered in the compiled code prior to
runtime including Fortran, C, C++, and Java. (Chasm also enables unidirectional
invocations from dynamically typed languages such as Ruby and Python to statically
typed languages but not the reverse.) Because many scientic applications are written in Fortran and many libraries in C++, the most oft-cited discussion of Chasm
focuses on Fortran/C++ pairings (Rasmussen et al. 2006). In this setting, Chasm facilitates invoking methods on C++ objects from Fortran 95 and vice versa. Chasm
aims for the interoperability holy grail articulated by Barrett (1998):
Informally, a polylingual system is a collection of software components, written in diverse languages, that communicate with one another transparently. More specically, in
a polylingual system it is not possible to tell, by examining the source code of a polylingual software component, that it is accessing or being accessed by components of other
languages. All method calls, for example, appear to be within a components language,
even if some are interlanguage calls. In addition, no IDL or other intermediate, foreign
type model is visible to developers, who may create types in their native languages and
need not translate them into a foreign type system.
Chasm therefore eschews the use of a specially tailored IDL. Chasm instead
automatically generates an Extensible Markup Language (XML) representation of
1 http://www.swig.org
2 http://sourceforge.net/projects/pyfortran
3 http://geosci.uchicago.edu/ rtp1/itr/Python.html
Language A
Caller
Language A
Stub
Language B
Skeleton
Language B
Callee
Figure 11.1. Chasm stub and skeleton interfaces for language A procedure invoking language
B procedure.
4 http://www.gnu.org
5 http://www.cmake.org
253
254
Mixed-Language Programming
Real object
Flat
interface
Language A
Shadow object
Language B
2003. The strategy builds on and updates the approach Gray et al. (1999) outlined for
interfacing object-based Fortran 95 with object-oriented C++.Gray and colleagues
aimed to make user-dened types in Fortran available in C++ and vice versa. In their
approach, a real object in one language exports its behavior to the other language
via a at interface consisting of intrinsic types and 1D arrays thereof. The original
object exports its identity and state to the second language via a shadow object
that presents a logical interface. Figure 11.2 illustrates this architecture. Through
use of the logical interface, the shadow object may appear as a native object to a user
in language B.
Gray and colleagues distinguish an objects logical interface from its physical
interface. In their schema, constructing a physical interface requires three steps:
1. Unmangling procedure names,
2. Flattening interfaces with interoperable built-in data types,
3. Ensuring code initialization.
We explain these steps next.
Unmangling reverses the name-mangling practices compilers use to map global
identiers in source code to unique identiers in object code. The root cause for
name mangling stems from the C++ and Fortran languages allowing certain global
identiers to have the same name. For example, C++ allows two procedures to have
the same name while being distinguishable by only their signatures, and Fortran
allows different modules to have the same module procedure names. Compilers
generate these global names and pass them to linkers as entries in a symbol table.
Linkers generally reject duplicate symbols in producing the object or executable
code. Thus, a mangling scheme enables successful linking by eliminating symbol
duplication.
C++ compilers mangle all procedure names by default. In Fortran, the most
common global identiers are procedure names (external procedures or module
procedures)6 . However, Fortran restricts the name conicts on global identiers
used in a program so that only module procedures require name mangling.
Name-mangling rules are not standardized, which results in different namemangling schemes used in different compilers. Therefore, object codes produced
by different compilers are not linkable even on the same platform. C++ provides
6 Internal procedure names in Fortran are not global identiers. Also generic names in Fortran are
255
256
Mixed-Language Programming
Table 11.1. Corresponding C++ and Fortran 95 types: Which
primitive C++ types maps to the given intrinsic Fortran type
depending on the platform
Type
C++
Fortran 95
Integer
Real
Character
long/int
double/oat
char
integer
real
character
the extern "C" construct to unmangle the procedure name, assuming there is no
function overloading in the program, thus enabling interfacing to C, which disallows
function overloading. In Fortran, one can use external procedures to avoid name
mangling. Since Fortran is case-insensitive, it further requires C++ to use lower
case in procedure names. Gray and colleagues took this approach to unmangle the
procedure names: Their physical interfaces consisted of a set of C++ extern "C"
procedures in lower case and Fortran 95 external procedures. Since the Fortran
2003 standard introduced features to enhance interoperability with C, we can update the Gray and colleagues unmangling scheme by requiring C bindings on Fortran
procedures in the physical interface. Section 11.3.1 describes the mechanics of this
process.
Flattening (interfaces), the second step in the Gray et al. strategy, is required for
physical interfacing through nonmember functions in C++ or external procedures in
Fortran 95. Flattening requires passing only intrinsic data types that are common
to C++ and Fortran 95. Gray et al. suggest that the rst types listed in Table 11.1
match Institute of Electrical and Electronics Engineers (IEEE) standards on most
platforms, even though the C++ and Fortran 95 standards do not guarantee matching
representations. The second types listed in the C++ column, however, might match
the corresponding type in the Fortran column on other platforms. Section 11.3.1
eliminates this ambiguity by leveraging the Fortran 2003 standards interoperable
data types.
In addition, Gray et al. limit array arguments to be contiguous, 1D arrays in
the attened physical interface because of the semantic disparity in treating arrays
between the two languages. In Fortran 95, a multidimensional array has its rigid shape
and its elements can only be accessed via array subscripts. In C++, a multidimensional
array is treated the same as an 1D array, and the elements can be accessed via a
pointer using pointer arithmetic. Furthermore, pointer arrays and assumed-shape
arrays in Fortran can represent noncontiguous portions of a data array. This concept
is not naturally supported in C++.7 Thus it is reasonable to the restrict the array
arguments to the contiguous, 1D arrays.
Since Fortran 95 passes all arguments by reference, Gray et al. further require
all C++ arguments to be passed as pointers or references. With this approach, all
parameters passed by value in the C++ logical interface must be passed by reference
in the physical interface. One benet of this calling convention is that it can be
extended to support code that conforms to earlier Fortran standards, for example
7 Currently Fortran language committee, J3, is working on a TR to provide utilities for C programmers
Fortran 77. Nevertheless, one can eliminate the need for this convention in Fortran
2003 by leveraging that standards provision for passing by value. Section 11.3.1
explains further.
In applications, interface attening may expose safety and portability issues. One
issue Gray et al. have to address is the this pointer passed implicitly to all nonstatic
member functions. By the rules of interface attening, the this pointer has to be
passed using a reference to a long by C++ and an integer by Fortran 95 in the
physical interface. This raises concerns as to its safety and portability. To address
these issues, Gray et al. chose to use an opaque pointer in encapsulating the details
of casting between the this pointer and a long or integer formal parameter in
the calls. In addition, Gray et al. strongly recommended automating the conversion
process from the logical interface seen by end-users to its corresponding physical
interface, which is normally hidden from the same users.
Initialization of data, the last step in the Gray et al. strategy, relates to the underlying operating systems (OS) and has been a long-standing portability issue for
programs written in C, C++, and Fortran. The issue almost exclusively concerns the
extern const and static objects in C/C++, and objects with the save attribute
in Fortran. In C++ extern const and static objects can be explicitly initialized.
Similarly, a save object in Fortran can be explicitly initialized either by its declaration statement or by a separate data statement. These initialized data objects are
stored in a .data segment and are all initialized before main program starts executing. However neither C++ nor Fortran stipulate the explicit initializations for these
objects, that is, extern const, static, or save objects can be declared without being initialized in their native languages. These uninitialized objects are inserted into
a .bss segment, whose initialization is dependent on the OS. These .bss objects
may cause porting issues. For example, a programmer who solely develops on an
ELF-based OS may comfortably leave all the static variables uninitialized because
the operating system mandates data in .bss be lled up with zeros at startup time.
However, on other systems, such as XCOFF-based AIX, these objects are left with
uninitialized states.
In their strategy, Gray et al. built code using shared libraries on ELF-based systems. They reported success with this technique on Linux, Sun, and SGI platforms
but not on IBMs AIX machines.
In the Gray et al. approach, the logical interface uses the the physical interfacing techniques to enable the shadow-object relationship of Figure 11.2. The logical
interface consists of C++ class denitions or Fortran 95-derived type denitions in
a module that a programmer would see naturally in her native language. We illustrate this approach by using the astronaut example from Section 2.2. Lets assume
the astronaut class in Figure 2.4(b) has to be implemented in Fortran 95. Thus the
logical interfaces in Fortran 95 for astronaut class would be the following:
module astronaut_class
implicit none
public :: astronaut
type astronaut
private
character(maxLen), pointer :: greeting
257
258
Mixed-Language Programming
end type
interface astronaut
module procedure constructor
end interface
interface destruct
module procedure free_greeting
end interface
contains
function constructor(new_greeting) result(new_astronaut)
type(astronaut) :: new_astronaut
character(len=*), intent(in) :: new_greeting
! ...
end function
subroutine greet(this)
type(astronaut), intent(in) :: this
! ...
end subroutine
subroutine free_greeting (this)
type(astronaut), intent(inout) :: this
! ...
end subroutine
end module
To enable accessing the astronaut class in C++, one might construct the following
logical interface in C++:
class astronaut
{
public:
astronaut (const string &);
void greet() const;
astronaut();
private:
string greeting;
};
The physical interface consists of a set of procedures with at interfaces seen by
compilers but not necessarily by application programmers. The Fortran 95 physical
interface corresponding to the C++ logical interface comprises the following external
procedures:
subroutine astronaut_construct(this, new_greeting)
integer :: this
! this pointer
259
260
Mixed-Language Programming
Heroux et al. (2005) described the goals, philosophy, and practices of the
Trilinos project:
The Trilinos Project is an effort to facilitate the design, development, integration, and
ongoing support of mathematical software libraries within an object-oriented framework
for the solution of large-scale, complex multiphysics engineering and scientic problems.
Trilinos addresses two fundamental issues of developing software for these problems:
(i) providing a streamlined process and set of tools for development of new algorithmic implementations and (ii) promoting interoperability of independently developed
software.
Trilinos uses a two-level software structure designed around collections of packages.
A Trilinos package is an integral unit usually developed by a small team of experts in
a particular algorithms area such as algebraic preconditioners, nonlinear solvers, etc.
Packages exist underneath the Trilinos top level, which provides a common look-and-feel,
including conguration, documentation, licensing, and bug-tracking.
With more than 50 packages in its most recent release, Trilinos might best be described as a meta-project aimed at encouraging consistently professional software
engineering practices in scalable, numerical algorithm development projects. Object
orientation plays a central role in the Trilinos design philosophy. Hence, when requests came in for Fortran interfaces to Trilinos, which is mostly written in C++, the
Trilinos developers assigned a high value to writing object-oriented interfaces while
using idioms that feel natural to Fortran programmers.
Figure 11.3 shows the global architecture of a ForTrilinos-enabled application.
The top layer comprises application code a user writes by instantiating Fortranderived type instances and invoking type-bound procedures on those instances.
These instances are lightweight in that they hold only identifying information about
the underlying Trilinos C++ objects.
Application
Object-Oriented Interface
ForTrilinos
Interface Bodies
Procedural
bindings
C Header Files
CTrilinos
External Linkage Wrappers
Trilinos
The next layer down, the ForTrilinos layer, supports a users application code
by dening public, extensible derived types arranged in hierarchies that mirror
the hierarchies in the underlying Trilinos packages. As such, the Fortran layer
reintroduces properties like polymorphism, inheritance, function overloading, and
operator overloading, each of which gets sacriced internally in the CTrilinos layer
due to the C languages lack of support for OOP. That internal sacrice ensures
portability by exploiting the C interoperability constructs in the Fortran 2003 standard to guarantee type compatibility, procedural name consistency, and compatible
argument passing conventions between Fortran and C.
Each ForTrilinos method wraps one or more CTrilinos functions and invokes
those functions via procedural bindings consisting of Fortran interface bodies and
corresponding C header les. CTrilinos produces the ForTrilinos procedural bindings automatically via scripts that parse the underlying C++ and simultaneously
generate the CTrilinos header les and the ForTrilinos interface bodies. Although
the CTrilinos scripts output C function prototypes for the header les, the actual implementation of each function is written in C++. Embedding the C++ in a
extern "C"{} constructs provides external linkage that is, the ability to link with
any C procedures that do not use function overloading.
In the bottom layer of Figure 11.3 lie the C++ class implementations dened by
various Trilinos packages. This layered design approach relieves the developers of
Trilinos numerical packages from any burden of designing for interoperability. In
this sense, the ForTrilinos/CTrilinos effort is noninvasive.
Sections 11.3.111.3.4 adapt material from Morris et al. (2010), presenting code
snippets and class diagrams from ForTrilinos and CTrilinos to demonstrate the C
interoperability features of Fortran 2003 in 11.3.1, method invocation in 11.3.2, object
construction and destruction in 11.3.3, and mirroring C++ inheritance hierarchies in
Fortran in 11.3.4.
11.3.1 C Interoperability in Fortran 2003
The C interoperability constructs of Fortran 2003 ensure the interoperability of types,
variables, and procedures. These constructs provide for referencing global data and
procedures for access by a companion processor. That companion processor need
not be a C compiler and might even be a Fortran compiler. In the case of a procedure,
for example, the Fortran standard merely constrains the procedure to be describable
by a C function prototype.
A ForTrilinos/CTrilinos procedural binding example provides a broad sampling
of the Fortran 2003 C interoperability features. For that purpose, we consider here
one of the Trilinos Petra (Greek for foundation) packages: Epetra (essential
petra). Epetra provides the objects and functionality needed for the development of
linear and nonlinear solvers in a parallel or serial computational environment. All
Trilinos packages build atop one of the Petra packages, most commonly Epetra, so
enabling Fortran developers to build Epetra objects is an essential rst step toward
enabling their use of other Trilinos packages.
The Epetra MultiVector class facilitates constructing and using dense multivectors, vectors, and matrices on parallel, distributed-memory platforms. The
261
262
Mixed-Language Programming
original C++ prototype for an Epetra_MultiVector method that lls the multi-vector
with random elements has the following prototype:
class Epetra_MultiVector {
int Random();
}
which CTrilinos scripts parse and automatically export the corresponding C wrapper
prototype:
int Epetra_MultiVector_Random (CT_Epetra_MultiVector_ID_t selfID);
as well as the corresponding ForTrilinos interface block:
1
2
3
4
5
6
7
interface
integer(c_int) function Epetra_MultiVector_Random ( selfID ) &
bind(C,name=Epetra_MultiVector_Random)
import :: c_int ,FT_Epetra_MultiVector_ID_t
type(FT_Epetra_MultiVector_ID_t), intent(in), value :: selfID
end function
end interface
Fortran 2003
Kind Parameter
C type
integer
integer
real
real
character
c_int
c_long
c_double
c_oat
c_char
int
long
double
oat
char
valid_kind_parameters() result. Trilinos uses the open-source CMake9 build systems ctest facility to run tests such as that in Figure 11.5 in an automated manner,
search the results, and report successes and failures.
The statement at line 2 of the Epetra_MultiVector_Random interface body continues on line 3, where the bind attribute species that the referenced procedure
is describable by an interoperable C prototype. The name= parameter enforces Cs
case sensitivity during link-time procedure name resolution. Because it is possible
for the linked procedure to be implemented in Fortran, this also provides a mechanism for making Fortran case sensitive insofar as procedure names are concerned.
The generalize_all() procedure in Figure 11.4 provides an example of a Fortran
implementation of a bind(C) interface body dened elsewhere in ForTrilinos. It also
demonstrates a C pointer argument received as a c_ptr derived type and converted
to a Fortran pointer by the c_f_pointer() subroutine. The iso_c_binding module
provides c_ptr and c_f_pointer().
The Epetra_MultiVector_Random interface body also ensures the default C
behavior of passing of its FT_Epetra_MultiVector_ID_t argument.10 Elsewhere in
ForTrilinos appears that types denition:
type, bind(C) :: FT_Epetra_MultiVector_ID_t
private
integer(ForTrilinos_Table_ID_t) :: table
integer(c_int) :: index
integer(FT_boolean_t) :: is_const
end type
where the bind(C) attribute ensures interoperability with the CTrilinos struct:
typedef struct {
CTrilinos_Table_ID_t table; /* Table of object references */
int index;
/* Array index of the object */
boolean is_const;
/* Whether object was declared const */
} CT_Epetra_MultiVector_ID_t;
9 http://www.cmake.org
10 Although not specically mandated by any Fortran standard, the default behavior of most For-
tran compilers resembles passing arguments by reference, which provides an objects address to
the receiving procedure, thereby allowing the receiving procedure to modify the object directly.
An example of an alternative approach that simulates the same behavior is the copy-in/copy-out
technique that allows for modifying the passed entity without passing its address.
263
264
Mixed-Language Programming
1
2
3
4
5
6
7
Figure 11.4
module fortrilinos_utils
implicit none
private
public :: length
public :: generalize_all
public :: valid_kind_parameters
contains
8
9
10
11
12
13
14
15
!
!
!
!
!
!
!
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
!
!
!
!
!
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
!
!
!
!
!
!
!
!
!
!
!
This procedure checks the values of parameters required to interoperate with CTrilinos. The Fortran 2003 standard requires that these
parameters be defined in the intrinsic module iso_c_binding with
values that communicate meanings specified in the standard. This
procedures quoted interpretations of these values are largely excerpted from the standard. This procedure returns true if all of
the interoperating Fortran kind parameters required by ForTrilinos
have a corresponding C type defined by the companion C processor.
Otherwise, it returns false. (For purposes of ForTrilinos, the
Fortran standards use of the word processor is interpreted as
denoting the combination of a compiler, an operating system, and
! a hardware architecture.)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
if (present(verbose)) then
verbose_output=verbose
else
verbose_output=.false.
end if
91
92
93
94
95
96
97
98
99
100
101
102
select case(c_long)
case(-1)
write(error_unit ,fmt=(2a)) c_long error: ,no_fortran_kind
valid_kind_parameters = .false.
case(-2)
write(error_unit ,fmt=(2a)) c_long error: ,no_c_type
valid_kind_parameters = .false.
case default
if (verbose_output) write(output_unit,fmt=(2a)) &
c_long: ,interoperable
end select
103
104
105
106
107
108
109
select case(c_double)
case(-1)
write(error_unit ,fmt=(2a)) c_double error: ,imprecise
valid_kind_parameters = .false.
case(-2)
write(error_unit ,fmt=(2a)) c_double error: ,limited
265
266
Mixed-Language Programming
110
111
112
113
114
115
116
117
118
119
120
121
122
valid_kind_parameters = .false.
case(-3)
write(error_unit ,fmt=(2a)) &
c_double error: ,limited_and_imprecise
valid_kind_parameters = .false.
case(-4)
write(error_unit ,fmt=(2a)) &
c_double error: ,not_interoperable_nonspecific
valid_kind_parameters = .false.
case default
if (verbose_output) write(output_unit,fmt=(2a)) &
c_double: ,interoperable
end select
123
124
125
126
127
128
129
130
131
132
select case(c_bool)
case(-1)
write(error_unit ,fmt=(a)) c_bool error: invalid value for &
a logical kind parameter on the processor.
valid_kind_parameters = .false.
case default
if (verbose_output) write(output_unit ,fmt=(a)) c_bool: &
valid value for a logical kind parameter on the processor.
end select
133
134
135
136
137
138
139
140
141
142
143
144
select case(c_char)
case(-1)
write(error_unit ,fmt=(a)) c_char error: invalid value for &
a character kind parameter on the processor.
valid_kind_parameters = .false.
case default
if (verbose_output) write(output_unit ,fmt=(a)) c_char: &
valid value for a character kind parameter on the processor.
end select
end function
end module fortrilinos_utils
Figure 11.4. ForTrilinos utilities.
1
2
3
4
5
6
7
8
9
10
11
12
program main
use fortrilinos_utils ,only : valid_kind_parameters
use iso_fortran_env
,only : error_unit ,output_unit
implicit none
if (valid_kind_parameters(verbose=.true.)) then
write(output_unit,*)
write(output_unit,fmt=(a)) "End Result: TEST PASSED"
else
write(error_unit,*)
write(error_unit,fmt=(a)) "End Result: TEST FAILED"
end if
end program
Figure 11.5. ForTrilinos interoperability test.
where table identies which of several tables holds a reference to the underlying
Trilinos C++ object and index species which entry in the table refers to that object.
CTrilinos constructs one table for each class in the packages it wraps, and it stores
all instances of that class in the corresponding table.
In the CT_Epetra_MultiVector_ID_t struct, the table member is dened as an
enumerated type of the form:
typedef enum {
CT_Invalid_ID,
/*does not reference a valid table entry*/
/* lines omitted */
CT_Epetra_MultiVector_ID/*references Epetra_MultiVector entry*/
/* lines omitted */
} CTrilinos_Table_ID_t;
which aliases the int type and denes a list of int values ordered in unit increments
from CT_Invalid_ID=0. Likewise, ForTrilinos exploits the Fortran 2003 facility for
dening an interoperable list of enumerated integers according to:
enum ,bind(C)
enumerator ::
&
FT_Invalid_ID,
&
! lines omitted
FT_Epetra_MultiVector_ID, &
! lines omitted
end enum
which denes the list but does not alias the associated type. Since Fortran provides no mechanism aliasing intrinsic types, ForTrilinos employs the nearest Fortran
counterpart: an alias for the relevant kind parameter:
integer(kind(c_int)) ,parameter :: ForTrilinos_Table_ID_t=c_int
which takes on the same kind and value as the c_int parameter itself.
The nal FT_Epetra_MultiVector_ID_t component is_const, a Boolean
value, allows for marking structs that cannot be modied because they were declared as const. Since Cs support for a Boolean type only entered the language in
its most recent standard (C99), CTrilinos denes its own integer Boolean type for use
with earlier C compilers. ForTrilinos denes a corresponding FT_boolean_t integer
kind parameter and employs it in dening its multi-vector type.
CTrilinos reserves exclusive rights to modify its struct members even though
they have an interoperable representation in ForTrilinos. ForTrilinos honors these
rights by giving the FT_Epetra_MultiVector_ID_t components private scope within
a module that contains only type denitions and parameter initializations.
Finally, line 4 of the Epetra_MultiVector_Random interface body imports entities dened in the host module scoping unit. The Fortran standard does not make
names from a host scoping unit available within an interface body. Hence, although
each ForTrilinos procedural binding module contains use statements of the form:
use ,iso_c_binding ,only : c_int
use ,ForTrilinos_enums ,only : FT_Epetra_MultiVector_ID_t
267
268
Mixed-Language Programming
Table 11.3. Dummy array declaration interoperability
(N/A=none allowed): the interoperability restrictions apply to
the dummy (received) array, not to the actual (passed) array
Array category
Fortran declaration
Interoperable
C declaration
Explicit-shape
Assumed-size
Allocatable
Pointer
Assumed-shape
real(c_oat) :: a(10,15)
real(c_oat) :: b(10,*)
real(c_oat),allocatable:: c(:,:)
real(c_oat),pointer:: d(:,:)
real(c_oat) :: e(:,:)
oat a[15][10];
oat b[ ][10];
N/A
N/A
N/A
the entities these statements make accessible to the surrounding module are not automatically accessible within the interface bodies in that module. The import statement
brings the named entities into the interface body scoping unit. This completes the
discussion of the Epetra_MultiVector_Random binding.
An important interoperability scenario not considered up to this point involves
the interoperability of array arguments. Whereas Gray et al. (1999) mapped all array arguments to their attened, 1D equivalents, Fortran 2003 facilitates passing
multidimensional arrays. Standard-compliant compilers must copy passed data into
and out of bind(C) procedures in a manner that ensures contiguous memory on
the receiving side even when, for example, the actual argument passed is an array
pointer that references a noncontiguous subsection of a larger array. The restrictions
in doing so relate primarily to how the interface body for receiving procedure must
declare its array arguments. Only arrays declared on the receiving end as explicitshape or assumed-size are interoperable. Table 11.3 delineates the interoperable and
noninteroperable declarations.
An important special case concern character array arguments. Metcalf et al. (2004)
state:
In the case of default character type, agreement of character length is not required . . .
In Fortran 2003, the actual argument corresponding to a default character (or with kind
[c_char]) may be any scalar, not just an array element or substring thereof; this case is
treated as if the actual argument were an array of length 1 . . .
where the bracketed text corrects a typographical error in the original text. Passing
a character actual argument represents a special case of what Fortran 2003 terms
sequence association, which is an exception to the array shape rules that otherwise
apply. The standard allows sequence association only when the dummy argument
(declared in the receiving procedure interface) references contiguous memory that
is an explicit-shape or assumed-size arrays. In such cases, the actual (passed) argument need not have a rank and a shape matching those of the dummy (received)
argument. The actual argument rank and shape only need to provide at least as many
elements as required by the dummy argument.
As an extreme scenario, an actual argument can even be an array element, in
which case the dummy takes the rest of the array as input. Although originally
269
270
Mixed-Language Programming
type. Figure 11.6 illustrates the Fortran Epetra multivector construction code and
randomization method call is thus:
type(epetra_multivector) :: mv
mv = epetra_multivector(...)
call mv%Random()
which is the most natural Fortran equivalent to the original C++, and where ellipses
again mark arguments omitted from this pseudocode.
CTrilinos tabulates each struct a CTrilinos function returns. Recording the struct
in a table facilitates passing it into the appropriate destruction function, which
deletes the underlying object and its struct ID. Compile-time type checking is enforced by providing each wrapped class with its own custom (but internally identical)
struct ID.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Figure 11.6
module Epetra_MultiVector_module ! ---------- Excerpt -------------use ForTrilinos_enums ,only: FT_Epetra_MultiVector_ID_t,&
FT_Epetra_Map_ID_t,ForTrilinos_Universal_ID_t
use ForTrilinos_table_man
use ForTrilinos_universal,only:universal
use ForTrilinos_error
use FEpetra_BlockMap ,only: Epetra_BlockMap
use iso_c_binding
,only: c_int,c_double,c_char
use forepetra
implicit none
private
! Hide everything by default
public :: Epetra_MultiVector ! Expose type/constructors/methods
type ,extends(universal)
:: Epetra_MultiVector !"shell"
private
type(FT_Epetra_MultiVector_ID_t) :: MultiVector_id
contains
procedure :: cpp_delete => ctrilinos_delete_EpetraMultiVector
procedure
:: get_EpetraMultiVector_ID
procedure ,nopass :: alias_EpetraMultiVector_ID
procedure
:: generalize
procedure
:: Random
procedure
:: Norm2
procedure
:: NumVectors
end type
25
26
27
28
29
30
contains
31
32
33
34
35
36
37
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
271
272
Mixed-Language Programming
93
94
95
96
use iso_c_binding
,only : c_loc
class(Epetra_MultiVector) ,intent(in) ,target :: this
generalize = generalize_all(c_loc(this%MultiVector_id))
end function
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
subroutine Random(this,err)
class(Epetra_MultiVector) ,intent(inout) :: this
type(error)
,optional ,intent(out)
:: err
integer(c_int)
:: error_out
error_out = Epetra_MultiVector_Random (this%MultiVector_id)
if (present(err)) err=error(error_out)
end subroutine
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
subroutine ctrilinos_delete_EpetraMultiVector(this)
class(Epetra_MultiVector),intent(inout) :: this
call Epetra_MultiVector_Destroy( this%MultiVector_id )
end subroutine
142
143
end module
144
273
274
Mixed-Language Programming
Hermetic
+ cpp_delete ()
Ref_Counter
Universal
counter : Ref_Counter
+ register_self ()
+ force_finalize ()
count : Pointer<Integer>
obj : Pointer<Hermetic>
<<reference count>>
+ grab ()
{if this%count=0,
+ release ()
call obj%cpp_delete()}
1 <<assignment (=)>>
+ assign ()
{call lhs%release() ...
<< constructor >>
call lhs%grab()}
+ ref_counter ()
<< destructor >>
+ finalize_ref_counter ()
{call this%release()}
The grab() and release() methods respectively increment and decrement the
reference count. When the count reaches zero, release() calls the CTrilinos destruction procedure for the underlying C++ object. To achieve this, ref_counter
keeps a reference to the object using the obj component, which is of Hermetic type.
Hermetic provides a hook for ref_counter to use to invoke the CTrilinos destruction
functions. As shown in Figure 11.8(b), the abstract Hermetic class only denes the
interface for invoking the CTrilinos destruction procedures. Hermetic delegates to
subclasses the implementation of that interface, including the CTrilinos destruction
function invocation.
Figure 11.8(c) provides the Universal implementation. All ForTrilinos objects
must invoke their inherited register_self() method to register themselves to participate in reference counting before they can be used that is, after instantiation
of a Trilinos C++ object and its ForTrilinos shadow and assignment of the shadows
struct ID, the shadow object must call register_self(). Afterward, the shadow can
be used freely in assignments and other operations during which all new references
to this object are counted automatically by ref_counter. Universal also provides
a force_finalize() method for programmers to manually invoke the release()
method of ref_counter. This method is needed only for ForTrilinos objects created in the main program or with the save attribute, for which the language does not
mandate automatic object nalizations and the programmer must therefore manually
release the objects memory.
Fortran provides no facility for overriding its default copying behavior.11 The
aforementioned reference-counting scheme therefore requires no object copying.
Thus, ForTrilinos objects cannot be used in places, such as array constructors, where
object copying is required.
The need for reference counting in Fortran is unique to the mixed-language
programming paradox: An objects creation and deletion in Fortran is constrained
by the underlying C++ objects creation and deletion. In the vast majority of cases,
developers writing solely in Fortran avoid object creation, deletion, and nalization concerns altogether by using allocatable components instead of pointers and
shadows. This practice obligates standard-compliant compilers to automatically deallocate the memory associated with such components when the encompassing object
goes out of scope.
1
2
3
4
5
6
7
8
9
10
Figure 11.8(a):ref_count
module ref_counter_module
use hermetic_module, only : hermetic
private
public :: ref_counter
type ref_counter
private
integer, pointer :: count => null()
class(hermetic), pointer :: obj => null()
contains
procedure, non_overridable :: grab
11 Fortran 2003 provides no facility analogous to C++ copy constructors.
275
276
Mixed-Language Programming
11
12
13
14
15
16
17
18
19
interface ref_counter
module procedure constructor
end interface
20
21
contains
22
23
24
25
26
27
28
29
30
subroutine grab(this)
class(ref_counter), intent(inout) :: this
if (associated(this%count)) then
this%count = this%count + 1
else
stop Error in grab: count not associated
end if
end subroutine
31
32
33
34
35
subroutine release(this)
class (ref_counter), intent(inout) :: this
if (associated(this%count)) then
this%count = this%count - 1
36
37
38
39
40
41
42
43
44
else
if (this%count == 0) then
call this%obj%cpp_delete
deallocate (this%count, this%obj)
end if
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
1
2
3
4
5
6
7
call constructor%grab
end function
end module
Figure 11.8(b):hermetic
module hermetic_module
private
public :: hermetic
type, abstract :: hermetic
contains
procedure(free_memory), deferred :: cpp_delete
end type
8
9
10
11
12
13
14
15
1
2
3
4
abstract interface
subroutine free_memory (this)
import
class(hermetic), intent(inout) :: this
end subroutine
end interface
end module
Figure 11.8(c):universal
module universal_module
use hermetic_module ,only : hermetic
use ref_counter_module, only : ref_counter
implicit none
5
6
7
8
9
10
11
12
13
contains
procedure, non_overridable :: force_finalize
procedure, non_overridable :: register_self
end type
14
15
contains
16
17
18
19
20
21
call this%counter%release
end subroutine
22
23
24
25
26
27
28
this%counter = ref_counter(this)
end subroutine
end module
Figure 11.8. Implementation of reference-counted ForTrilinos OBJECT hierarchy.
277
278
Mixed-Language Programming
Epetra_RowMatrix
Epetra_CrsMatrix
Epetra_MsrMatrix
Epetra_VbrMatrix
Epetra_BasicRowMatrix
Epetra_FECrsMatrix
Epetra_JadMatrix
Epetra_OskiMatrix
Epetra_VbrRowMatrix
Epetra_FEVbrMatrix
279
280
Mixed-Language Programming
names. The object-construction process provides a useful guide to the use of each of
these procedures. Consider the following pseudocode:
1
2
3
4
5
6
program main
use Epetra_Vector_module, only : Epetra_Vector
implicit none
type(Epetra_Vector) :: A
A = Epetra_Vector(...) ! arguments omitted
end main
where the structure constructor invoked on the RHS of line 5 returns a new
Epetra_Vector which has a new Fortran struct ID tag for the underlying C++
object that the ForTrilinos constructor directs CTrilinos to create at lines 5557
in Figure 11.10. The new struct ID is also passed to the structure constructor of
Epetra_Vector, from_struct, at line 58 to create a FT_Epetra_MultiVector_ID_t
alias struct ID and construct the new Epetra_Vector objects Epetra_MultiVector
parent component at lines 3537 in Figure 11.10. Finally, the intrinsic assignment
assigns the new Epetra_Vector object to LHS and invokes the dened assignment
in the ref_counter parents component. This completes the construction process.
Creation of the alias struct ID proceeds in three steps. First, starting with
the innermost invocation on lines 3537 of Figure 11.10, generalize() converts
the FT_Epetra_Vector_ID_t struct to the generic ForTrilinos_Universal_ID_t.
Second, alias_EpetraMultiVector_ID() converts the returned generic ID into
the parent FT_Epetra_MultiVector_ID_t. Third, the Epetra_MultiVector() constructor uses the latter ID to construct an Epetra_MultiVector that gets assigned
to the parent component of the new Epetra_Vector at line 3537.
Lines 7089 of Figure 11.6 present alias_EpetraMultiVector_ID(). At
lines 8183, that procedure passes its generic_id argument to the CTrilinos
CT_Alias() function along with an enumerated value identifying the table of
the desired return ID. Lines 8688 copies that return ID into a newly allocated
generic alias_id, the C address of which is then passed to the module procedure degeneralize_EpetraMultiVector(), which converts alias_id into a
class-specic struct ID.
Being able to reference the same object by either its own struct ID or that of
the parent guarantees the ability to invoke the Epetra_MultiVector type-bound
procedure Norm2() on an Epetra_Vector or on an Epetra_MultiVector. Thus, the
ForTrilinos equivalent of the presented CTrilinos example follows:
type(Epetra_CrsMatrix) :: A
type(Epetra_JadMatrix) :: B
A=Epetra_CrsMatrix(...) ! arguments omitted
B=Epetra_JadMatrix(...) ! arguments omitted
call A%TwoRowMatrixOp(B)
Supporting this syntax requires that Epetra_RowMatrix_TwoRowMatrixOp() be
wrapped by TwoRowMatrixOp(). ForTrilinos uses the type-bound procedure
get_EpetraRowMatrix_ID() to pass the required arguments to the wrapped function. Consequently, users can pass any Epetra_RowMatrix subclass instances to an
Epetra_RowMatrix method.
11.3.5 Discussion
Experience with implementing the strategy outlined in this article suggests that it
provides a noninvasive approach to wrapping the packages in Trilinos. Developers
of individual packages need not concern themselves with designing for interoperability. Several packages have been successfully wrapped without modication to
the underlying package.
The strategy has also proven to be less labor intensive and more exible than
prior practices of manually constructing wrappers for individual packages for specic
end-users. The labor reduction stems from the possibility of automating portions
of the process, namely the generation of procedural bindings consisting of the C
header les and the Fortran interface bodies. The increased exibility is manifold.
First, the adherence to language-standard features, designed specically to promote interoperability, all but guarantees compatible bit representations, linking,
and argument-passing conventions. Second, the ability to construct Petra objects,
which are used by all Trilinos packages, ensures that wrapping Petra packages immediately decreases the subsequent effort required to wrap new packages. Third,
the ability for Fortran programmers to directly control the construction of Epetra objects, in particular, eliminates the need for the developers of individualized
wrappers to hardwire assumptions about parameter values into the construction
process.
Drawbacks of the above approach include the amount of new code that must
be generated and the inability to pass objects directly between Fortran and C++.
The desired portability comes at the cost of manipulating only lightweight, Fortran
shadows of the underlying C++ objects. Direct manipulation of the objects happens
only in C++, and the functionality that supports that manipulation gets exported in
C header les, which necessitates sacricing OOP in the intermediate layer. Fortunately, this sacrice is purely internal and is not exposed to the end users. Even
though there has been some discussion in the Fortran community of adding interoperability with C++ to a future Fortran standard, no formal effort is underway and
the standardization of any such effort would likely be many years away.
1
2
3
4
Figure 11.10
module Epetra_Vector_module
use ForTrilinos_enums
,only: &
FT_Epetra_MultiVector_ID_t,FT_Epetra_Vector_ID_t,&
FT_Epetra_BlockMap_ID_t,ForTrilinos_Universal_ID_t,FT_boolean_t
281
282
Mixed-Language Programming
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use ForTrilinos_table_man
use ForTrilinos_universal
use ForTrilinos_error
use FEpetra_MultiVector ,only: Epetra_MultiVector
use FEpetra_BlockMap
,only: Epetra_BlockMap
use iso_c_binding
,only: c_int
use forepetra
implicit none
private
! Hide everything by default
public :: Epetra_Vector ! Expose type/constructors/methods
type ,extends(Epetra_MultiVector)
:: Epetra_Vector !"shell"
private
type(FT_Epetra_Vector_ID_t) :: vector_id
contains
procedure
:: cpp_delete => &
ctrilinos_delete_EpetraVector
procedure
:: get_EpetraVector_ID
procedure ,nopass :: alias_EpetraVector_ID
procedure
:: generalize
end type
25
26
27
28
29
30
contains
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
subroutine ctrilinos_delete_EpetraVector(this)
class(Epetra_Vector) ,intent(inout) :: this
call Epetra_Vector_Destroy(this%vector_id)
283
284
Mixed-Language Programming
115
end subroutine
116
117
118
end module
119
EXERCISES
1. Expand the valid_kind_parameters() test of Figure 11.4 to include tests for
compiler support for the remaining interoperable types in the Fortran 2003 standard. See Metcalf et al. (2004) or the Fortran 2003 standard (a free draft version
is available on the Web).
2. Download the Trilinos solver framework from http://trilinos.sandia.gov. Write
Fortran 2003 a wrapper module for the Epetra_VbrMatrix class.
12 Multiphysics Architectures
When sorrows come, they come not single spies but in battalions.
William Shakespeare
286
Multiphysics Architectures
1
2
3
Figure 12.1
module periodic_2nd_order_module
use field_module ,only : field,initial_field
use kind_parameters ,only : rkind,ikind
implicit none
private
public :: periodic_2nd_order, constructor
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
interface periodic_2nd_order
procedure constructor
end interface
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
contains
function constructor(initial,num_grid_pts)
type(periodic_2nd_order) ,pointer :: constructor
procedure(initial_field) ,pointer :: initial
integer(ikind) ,intent(in) :: num_grid_pts
integer :: i
allocate(constructor)
allocate(constructor%f(num_grid_pts))
if (.not. allocated(x_node)) x_node = grid()
do i=1,size(x_node)
constructor%f(i)=initial(x_node(i))
end do
contains
pure function grid()
integer(ikind) :: i
real(rkind) ,dimension(:) ,allocatable :: grid
allocate(grid(num_grid_pts))
do i=1,num_grid_pts
grid(i) = 2.*pi*real(i-1,rkind)/real(num_grid_pts,rkind)
end do
end function
end function
53
54
55
56
57
58
287
288
Multiphysics Architectures
59
60
61
62
63
dx=2.0*pi/num_grid_pts
k_max=num_grid_pts/2.0_rkind
CFL=1.0/(1.0-cos(k_max*dx))
rk2_dt = CFL*dx**2/nu
end function
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function total(lhs,rhs)
class(periodic_2nd_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: total
type(periodic_2nd_order) ,allocatable :: local_total
integer
:: i
select type(rhs)
class is (periodic_2nd_order)
allocate(local_total)
allocate(local_total%f(size(lhs%f)))
do i=1, size(lhs%f)
local_total%f(i) = lhs%f(i) + rhs%f(i)
end do
call move_alloc(local_total,total)
class default
stop periodic_2nd_order%total: unsupported rhs class.
end select
end function
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
function difference(lhs,rhs)
class(periodic_2nd_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: difference
type(periodic_2nd_order) ,allocatable :: local_difference
integer
:: i
select type(rhs)
class is (periodic_2nd_order)
allocate(local_difference)
allocate(local_difference%f(size(lhs%f)))
do i=1, size(lhs%f)
local_difference%f(i) = lhs%f(i) - rhs%f(i)
end do
call move_alloc(local_difference,difference)
class default
stop periodic_2nd_order%difference: unsupported rhs class.
end select
end function
102
103
104
105
106
107
108
109
110
111
112
113
function product(lhs,rhs)
class(periodic_2nd_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: product
type(periodic_2nd_order) ,allocatable :: local_product
integer
:: i
select type(rhs)
class is (periodic_2nd_order)
allocate(local_product)
allocate(local_product%f(size(lhs%f)))
do i=1, size(lhs%f)
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function multiple(lhs,rhs)
class(periodic_2nd_order) ,intent(in) :: lhs
real(rkind) ,intent(in) :: rhs
class(field) ,allocatable :: multiple
type(periodic_2nd_order) ,allocatable :: local_multiple
integer
:: i
allocate(local_multiple)
allocate(local_multiple%f(size(lhs%f)))
do i=1, size(lhs%f)
local_multiple%f(i) = lhs%f(i) * rhs
end do
call move_alloc(local_multiple,multiple)
end function
135
136
137
138
139
140
141
142
143
144
145
subroutine copy(lhs,rhs)
class(field) ,intent(in) :: rhs
class(periodic_2nd_order) ,intent(inout) :: lhs
select type(rhs)
class is (periodic_2nd_order)
lhs%f = rhs%f
class default
stop periodic_2nd_order%copy: unsupported copy class.
end select
end subroutine
146
147
148
149
150
151
152
function df_dx(this)
class(periodic_2nd_order) ,intent(in) :: this
class(field) ,allocatable :: df_dx
integer(ikind) :: i,nx, x_east, x_west
real(rkind) :: dx
class(periodic_2nd_order) ,allocatable :: local_df_dx
153
154
155
156
157
158
159
160
161
162
163
164
nx=size(x_node)
dx=2.*pi/real(nx,rkind)
allocate(local_df_dx)
allocate(local_df_dx%f(nx))
do i=1,nx
x_east = mod(i,nx)+1
x_west = nx-mod(nx+1-i,nx)
local_df_dx%f(i)=0.5*(this%f(x_east)-this%f(x_west))/dx
end do
call move_alloc(local_df_dx, df_dx)
end function
165
166
167
168
function d2f_dx2(this)
class(periodic_2nd_order) ,intent(in)
class(field) ,allocatable :: d2f_dx2
:: this
289
290
Multiphysics Architectures
169
170
171
integer(ikind) :: i,nx,x_east,x_west
real(rkind)
class(periodic_2nd_order) ,allocatable
:: dx
:: local_d2f_dx2
172
173
174
175
176
177
178
179
180
181
182
183
184
nx=size(this%f)
dx=2.*pi/real(nx,rkind)
allocate(local_d2f_dx2)
allocate(local_d2f_dx2%f(nx))
do i=1, nx
x_east = mod(i,nx)+1
x_west = nx-mod(nx+1-i,nx)
local_d2f_dx2%f(i) = &
(this%f(x_east)-2.0*this%f(i)+this%f(x_west))/dx**2
end do
call move_alloc(local_d2f_dx2, d2f_dx2)
end function
185
186
187
188
189
190
191
192
subroutine output(this)
class(periodic_2nd_order) ,intent(in) :: this
integer(ikind) :: i
do i=1,size(x_node)
print *, x_node(i), this%f(i)
end do
end subroutine
193
194
end module
Figure 12.1. Periodic, 2nd-order central differences concrete eld class.
Procedure
Runtime Share
d2f_dx2
df_dx
multiple
copy
product
total
difference
24.5%
24.0%
18.7%
15.1%
6.2%
5.8%
5.4%
Total
99.7%
The evidence in Table 12.1 suggests the greatest impact of any optimization
effort will come through reducing the time occupied by the seven listed procedures.
These procedures collectively occupy more than 99% of the runtime. First efforts
might best be aimed at the top four procedures, which collectively occupy a runtime
share exceeding 80%, or even just the top two, which occupy a combined share
near 50%.
Amdahls Law quanties the overall speed gain for a given gain in a subset of a
process. In doing so, it also facilitates calculating the maximum gain in the limit as
the given subsets share of the overall completion time vanishes. Imagine breaking
some process into N distinct portions with the ith portion occupying Pi fraction of
the overall completion time. Then imagine ordering the portions such that the N th
portion subsumes all parts of the overall process that have xed costs and therefore
cannot be sped up. Dene the speedup of the ith portion as:
Si
toriginal
toptimized
(12.1)
where the numerator and denominator are the original and optimized completion
times, respectively, and where SN 1 by denition. Amdahls Law states that the
speedup of the overall process is:
S)
=
Sov erall (P,
PN +
1
N1 Pi
(12.2)
i=1 Si
291
292
Multiphysics Architectures
Each of these procedures occupies a share of 0.1% or lower, totaling 0.3%. Given
that the time occupied by system calls lies beyond an applications control and given
that reducing the number of calls would require signicant algorithmic redesign, it
seems reasonable to treat the system calls as a xed cost and take PN 0.003 and
Smax 333. Hence, it would be impossible to take full advantage of more than 333
execution units in parallel. With the recent advent of quad-socket, quad-core nodes,
the number of cores on even a 32-node cluster would exceed 333. At the time of this
writing, a relatively modest $150,000 research equipment grant is likely to garner
5121,024 cores, putting it out of reach for full utilization by a code with even a 0.3%
share of nonscalable execution.
This discussion neglects the overhead costs associated with parallel execution.
Such overhead might include, for example, the spawning and joining of threads
or communication between processes. Also, we considered only the ideal case of
Si i. With nite speedup on portions 1 through N 1, the Soverall might continue
to improve with increasing numbers of execution units beyond the aforementioned
limits. A code might then make effective use of greater than 250 execution units,
although Soverall Smax would still hold.
Finally, the parallel efciency denition in equation (12.3) applies to strong scaling, the case in which the problem size remains xed as the number of execution
units increases. The alternative, weak scaling describes the case where the problem
size grows proportionately to the number of execution units. Which type of scaling
matters more depends on the application domain.
Vision without Execution Is Hallucination.
Thomas Edison
Regarding code efciency, a viewpoint held without the backing of execution
data represents an unfounded belief. Proling procedures runtime share guides
developers toward the chief bottlenecks. Amdahls law determines the speedup
achievable by focusing on specic bottlenecks.
1 http://www-01.ibm.com/software/awdtools/mass/
2 http://tinyurl.com/2wmrjb7
293
294
Multiphysics Architectures
Vectorization requires that the array sections occupy contiguous memory. The
aforementioned example satises this condition because the f component, an
instance of periodic_2nd_order, contains an allocatable array and is thereby guaranteed to be contiguous. By contrast, pointer arrays may pose challenges in this
respect since they can be associated with noncontiguous array targets. In the case
of SIMD architectures, alignments can also pose challenges to compilers applying
vectorization techniques (Eichenberger et al. 2004).
The other popular auto-parallelization technique exploits symmetric multiprocessing (SMP) systems that is systems in which each processor has an identical view
of the memory. SMP computers (and SMP nodes on multinode, distributed clusters)
can run multiple threads to parallelize a loop. The advent of multicore chips bolsters the popularity of such multithreading. The technique requires a mechanism for
spawning and joining threads in shared memory as Section 12.1.3 explains. To maximize the speedup of a loop using all available cores, a compiler may automatically
parallelize the loop, that is, divide it into multiple segments and bind each segment
to an available core3 .
Considering the aforementioned loop from multiple, the concept of the autoparallelization can be explained using the following pseudo-code:
CHUNK_SIZE = size(lhs%f) / NUM_THREADS
! start parallel region
call create_thread(run_by_one_thread, 1, CHUNK_SIZE)
call create_thread(run_by_one_thread, CHUNK_SIZE+1, 2*CHUNK_SIZE)
...
call create_thread(run_by_one_thread, &
(NUM_THREADS-1)*CHUNK_SIZE+1, size(lhs%f))
! join threads & end of parallel region
... subroutine run_by_one_thread (lbound, ubound)
integer, intent( in ) :: lbound, ubound
do i=lbound, ubound
local_multiple%f(i) = lhs%f(i) * rhs
end do
end subroutine
Nothing fundamental differentiates between the automated parallelized loops executed at runtime by compiler-automated means versus the directive-based technique
we discuss next. For the simple loop used in multiple, the compiler can easily determine the loop is independent in execution order and therefore parallelizable. In
practice, however, many loops are not so amenable to simple analysis, and compilers
may have difculty removing data dependencies between loop iterations. In those
cases, the programmer usually has an edge in knowing the feasibility of parallelizing
a loop.
3 Although binding each thread to its own core avoids resource conicts and is allowable for a compiler
to do, the OpenMP multithreading technology discussed in Section 12.1.3 does not yet facilitate a
standard, portable mechanism for such bindings.
Master
Thread
Thread n
Master
Thread
Thread 3
Thread 3
Thread 2
Thread 2
Thread 1
Thread 1
n = OMP_NUM_THREADS ( )
Figure 12.2. Fork-join programming model: A typical multithreaded program forks and joins
multiple threads of execution many times throughout a given run.
4 http://www.sgi.com/products/servers/
5 http://software.intel.com/en-us/articles/cluster-openmp-for-intel-compilers/
295
296
Multiphysics Architectures
Most current compilers support OpemMP. Table 12.1 shows the runtime prole
for a serial, central-difference, Burgers-solver executable generated without automatic parallelization by the IBM XL Fortran compiler version 13.1 on an eight-core
platform running the AIX operating system. Since d2f_dx2 and df_dx combine to
occupy a 48.5% runtime share, optimizing these two procedures will have the greatest
impact on speeding up the code. The simplest approach for doing so is with OpenMP
bracing the loops in these procedures with OpenMP directives to the compiler. For
df_dx, the result is:
!$OMP parallel do private (x_east, x_west)
do i=1,nx
x_east = mod(i,nx)+1
x_west = nx-mod(nx+1-i,nx)
local_df_dx%f(i)=0.5*(this%f(x_east)-this%f(x_west))/dx
end do
!$OMP end parallel do
where the !$OMP OpenMP sentinels prompt an OpenMP-enabled compiler to interpret the trailing directives. A compiler without OpenMP capabilities ignores the
entire line, seeing the leading exclamation mark as the harbinger for a comment.
The parallel do/end parallel do pair simultaneously fork threads and delimit a
work-sharing region. At the beginning of this region, an OpenMP compiler forks
a number of threads equal to the OMP_NUM_THREADS environment variable6 . At the
end, the compiler joins all the threads into one. The !$OMP end parallel do directive implicitly synchronizes the computation, acting as a barrier that forces the
master thread to wait for all other threads to nish their work before closing the
parallel region. Finally, the private clause gives each thread its own copies of the
listed variables. These variables can be considered local to each thread.
When the number of threads exceeds the number of available execution units,
performance generally degrades. Hence, the maximum number of threads tested
on the aforementioned platform is eight. Figure 12.3 shows the performance of the
central-difference Burgers solver with loop-level OpemMP directives embedded in
df_dx and d2f_dx2 up to eight threads. The symbols labeled Amdahls law correspond to equation (12.2) with PN = 1 0.485 and Si = n, where n is the number of
threads. The speedup at n = 2 and n = 4 hugs Amdhals law closely, while the performance slips some at n = 8. Deviations from Amdahls law generally stem from costs
incurred for thread startup, synchronization, and any other overhead associated with
the OpenMP implementation.
Just as it is common to conate the shared-memory programming model with the
shared-memory hardware that often supports it, it is common to conate the forkjoin programming style with the OpenMP API that commonly supports it. In fact,
one can employ other styles with OpenMP, including the more prevalent parallel
programming style: Single Program Multiple Data (SPMD). With SPMD OpenMP,
6 The setting of environment variables is platform-dependent. In a Unix bash shell, for example, typing
50
45
40
35
30
25
20
15
Number of threads
Figure 12.3. Scalability of a central difference Burgers solver using OpenMP directives.
one forks all threads at the beginning of program execution and joins them only at
the end of execution (Wallcraft 2000; Wallcraft 2002; Krawezik et al. 2002). Multiple
copies of the same program run simultaneously on separate execution units with the
work organized via explicit references to a thread identication number rather than
being shared out automatically by the compiler as with the aforementioned loop-level
directives.
Although there has been some work on automating the transformation of more
traditional OpenMP code to a SPMD style (Liu et al. 2003), manual SPMD OpenMP
programming requires considerably greater effort and care than loop-level OpenMP.
In the process, OpenMP loses some of its luster relative to the more common SPMD
API: the Message Passing Interface (MPI). The knowledge and effort required to
write SPMD OpenMP is comparable to that required for MPI. Furthermore, SPMD
OpenMP and MPI share the same motivation: Both scale better to high numbers
of execution units. Hence, when massive scalability drives the design, one more
naturally turns to MPI. The next section discusses a MPI-based solver.
12.1.4 Library-Based Parallelization: ForTrilinos and MPI
Programmers commonly describe MPI as the assembly language of parallel programming. Characteristic of this description, moving to MPI would typically be the
zenith of programmer involvement in the parallelization process. It would also be
very difcult for raw MPI code to adhere to the style suggestions set forth in Part I of
this book. Most MPI calls involve cryptic argument names and side effects, making
it challenging to write code that comments itself or code that gives visual cues to its
structure as recommended in Section 1.7.
Embedding MPI in an object-oriented library that supports domain-specic abstractions lowers the barrier to its use. The Trilinos project does this for various
297
Multiphysics Architectures
0.8
Parallel efficiency
298
0.6
0.4
0.2
X
Strong scaling
Weak scaling
6
4
8
Number of processors
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
abstract interface
real(c_double) pure function initial_field(x)
import :: c_double
real(c_double) ,intent(in) :: x
end function
function field_op_field(lhs,rhs)
import :: field
class(field) ,intent(in) :: lhs,rhs
class(field) ,allocatable :: field_op_field
end function
function field_op_real(lhs,rhs)
import :: field,c_double
class(field) ,intent(in) :: lhs
real(c_double) ,intent(in) :: rhs
class(field) ,allocatable :: field_op_real
end function
real(c_double) function real_to_real(this,nu,grid_resolution)
import :: field,c_double,c_int
class(field) ,intent(in) :: this
real(c_double) ,intent(in) :: nu
integer(c_int),intent(in) :: grid_resolution
end function
function derivative(this)
import :: field
class(field) ,intent(in) :: this
class(field) ,allocatable :: derivative
end function
subroutine field_eq_field(lhs,rhs)
import :: field
299
300
Multiphysics Architectures
55
56
57
58
59
60
61
62
63
64
65
66
67
68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
abstract interface
function create_interface(this,initial,grid_resolution,comm)
import :: field, field_factory ,initial_field,c_int,Epetra_Comm
class(Epetra_Comm),intent(in) :: comm
class(field_factory), intent(in) :: this
class(field) ,pointer :: create_interface
procedure(initial_field) ,pointer :: initial
integer(c_int) ,intent(in) :: grid_resolution
end function
end interface
end module
Figure 12.5 (c)
module periodic_2nd_order_factory_module
use FEpetra_Comm ,only:Epetra_Comm
use field_factory_module ,only : field_factory
use field_module ,only : field,initial_field
use periodic_2nd_order_module ,only : periodic_2nd_order
implicit none
private
public :: periodic_2nd_order_factory
9
10
11
12
end type
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
contains
function new_periodic_2nd_order(this,initial,grid_resolution,comm)
use iso_c_binding, only:c_int
class(periodic_2nd_order_factory), intent(in) :: this
class(field) ,pointer :: new_periodic_2nd_order
procedure(initial_field) ,pointer :: initial
integer(c_int) ,intent(in) :: grid_resolution
class(Epetra_Comm), intent(in) :: comm
new_periodic_2nd_order=> &
periodic_2nd_order(initial,grid_resolution,comm)
end function
end module
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
real(c_double) ,parameter
:: pi=acos(-1._c_double)
real(c_double) ,dimension(:) ,allocatable :: x_node
type(Epetra_Map)
,allocatable :: map
35
36
37
38
interface periodic_2nd_order
procedure constructor
end interface
301
302
Multiphysics Architectures
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
contains
function constructor(initial,grid_resolution,comm) result(this)
type(periodic_2nd_order) ,pointer :: this
procedure(initial_field) ,pointer :: initial
integer(c_int) ,intent(in) :: grid_resolution
integer(c_int) :: i,j
class(Epetra_Comm), intent(in) :: comm
integer(c_int) :: NumGlobalElements
integer(c_int),dimension(:),allocatable :: MyGlobalElements
integer(c_int)
:: NumMyElements,IndexBases=1,status
real(c_double) ,dimension(:) ,allocatable :: f_v
type(error) :: ierr
NumGlobalElements=grid_resolution
allocate(this)
if (.not. allocated(x_node)) x_node = grid()
if (.not. allocated(map)) then
allocate(map,stat=status)
ierr=error(status,periodic_2nd_order: create map)
call ierr%check_allocation()
map = Epetra_Map(NumGlobalElements,IndexBases,comm)
end if
NumMyElements= map%NumMyElements()
allocate(MyGlobalElements(NumMyElements))
MyGlobalElements = map%MyGlobalElements()
allocate(f_v(NumMyElements))
forall(i=1:NumMyElements)f_v(i)=initial(x_node(MyGlobalElements(i)))
this%f=Epetra_Vector(map,zero_initial=.true.)
call this%f%ReplaceGlobalValues(NumMyElements,f_v,MyGlobalElements)
contains
pure function grid()
integer(c_int) :: i
real(c_double) ,dimension(:) ,allocatable :: grid
allocate(grid(grid_resolution))
forall(i=1:grid_resolution) &
grid(i)= 2.*pi*real(i-1,c_double)/real(grid_resolution,c_double)
end function
end function
76
77
78
79
80
81
82
83
84
85
86
subroutine copy(lhs,rhs)
class(field) ,intent(in) :: rhs
class(periodic_2nd_order) ,intent(inout) :: lhs
select type(rhs)
class is (periodic_2nd_order)
lhs%f = rhs%f
class default
stop periodic_2nd_order%copy: unsupported copy class.
end select
end subroutine
87
88
89
90
91
92
93
k_max=grid_resolution/2.0_c_double
CFL=1.0/(1.0-cos(k_max*dx))
rk2_dt = CFL*dx**2/nu
end function
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function total(lhs,rhs)
class(periodic_2nd_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: total
type(periodic_2nd_order) ,allocatable :: local_total
select type(rhs)
class is (periodic_2nd_order)
allocate(periodic_2nd_order::local_total)
local_total%f=Epetra_Vector(map,zero_initial=.true.)
call local_total%f%Update( &
1._c_double,lhs%f,1._c_double,rhs%f,0._c_double)
call move_alloc(local_total,total)
class default
stop periodic_2nd_order%total: unsupported rhs class.
end select
end function
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function difference(lhs,rhs)
class(periodic_2nd_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: difference
type(periodic_2nd_order) ,allocatable :: local_difference
select type(rhs)
class is (periodic_2nd_order)
allocate(periodic_2nd_order::local_difference)
local_difference%f=Epetra_Vector(map,zero_initial=.true.)
call local_difference%f%Update(&
1._c_double,lhs%f,-1._c_double,rhs%f,0._c_double)
call move_alloc(local_difference,difference)
class default
stop periodic_2nd_order%difference: unsupported rhs class.
end select
end function
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function product(lhs,rhs)
class(periodic_2nd_order) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: product
type(periodic_2nd_order) ,allocatable :: local_product
select type(rhs)
class is (periodic_2nd_order)
allocate(periodic_2nd_order::local_product)
local_product%f=Epetra_Vector(map,zero_initial=.true.)
call local_product%f%Multiply(1._c_double,lhs%f,rhs%f,0._c_double)
call move_alloc(local_product,product)
class default
stop periodic_2nd_order%product: unsupported rhs class.
end select
end function
303
304
Multiphysics Architectures
149
150
151
152
153
154
155
156
157
158
function multiple(lhs,rhs)
class(periodic_2nd_order) ,intent(in) :: lhs
real(c_double) ,intent(in) :: rhs
class(field) ,allocatable :: multiple
type(periodic_2nd_order) ,allocatable :: local_multiple
allocate(periodic_2nd_order::local_multiple)
local_multiple%f=Epetra_Vector(map,zero_initial=.true.)
call local_multiple%f%Scale(rhs,lhs%f)
call move_alloc(local_multiple,multiple)
end function
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function df_dx(this)
class(periodic_2nd_order) ,intent(in) :: this
class(field) ,allocatable :: df_dx
type(Epetra_Vector) :: x
type(Epetra_CrsMatrix) :: A
type(error) :: err
real(c_double) ,dimension(:)
,allocatable :: c
real(c_double) :: dx
integer(c_int) :: nx
type(periodic_2nd_order), allocatable :: df_dx_local
integer(c_int),dimension(:),allocatable :: MyGlobalElements
integer(c_int),dimension(:),allocatable :: MyGlobalElements_diagonal
integer(c_int),dimension(:),allocatable :: NumNz
integer(c_int) :: NumGlobalElements,NumMyElements,i
integer(c_int) :: indices(2), NumEntries
real(c_double) ::values(2)
real(c_double),parameter :: zero =0.0
integer(c_int),parameter :: diagonal=1
178
179
180
181
182
! Executable code
nx=size(x_node)
dx=2.*pi/real(nx,c_double)
NumGlobalElements = nx
183
184
185
186
187
188
! Get update list and number of local equations from given Map
NumMyElements = map%NumMyElements()
call assert_identical( [NumGlobalElements,map%NumGlobalElements()] )
allocate(MyGlobalElements(NumMyElements))
MyGlobalElements = map%MyGlobalElements()
189
190
191
192
193
! Create an integer vector NumNz that is used to build the Epetra Matrix
! NumNz(i) is the number of non-zero elements for the ith global eqn.
! on this processor
allocate(NumNz(NumMyElements))
194
195
196
197
198
199
200
! Create a Epetra_Matrix
A = Epetra_CrsMatrix(FT_Epetra_DataAccess_E_Copy,map,NumNz)
201
202
203
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
!Finish up
call A%FillComplete()
!create vector x
x=Epetra_Vector(A%RowMap())
Call A%Multiply_Vector(.false.,this%f,x)
allocate(c(NumMyElements))
c=x%ExtractCopy()
!create vector of df_dx
allocate(periodic_2nd_order::df_dx_local)
df_dx_local%f=Epetra_Vector(map,zero_initial=.true.)
call df_dx_local%f%ReplaceGlobalValues(&
NumMyElements,c,MyGlobalElements)
call move_alloc(df_dx_local, df_dx)
end function
247
248
249
250
251
252
253
254
255
256
257
258
function d2f_dx2(this)
class(periodic_2nd_order) ,intent(in) :: this
class(field) ,allocatable :: d2f_dx2
type(Epetra_Vector) :: x
type(Epetra_CrsMatrix) :: A
type(error) :: err
real(c_double) ,dimension(:)
,allocatable :: c
real(c_double) :: dx
integer(c_int) :: nx
type(periodic_2nd_order) ,allocatable :: d2f_dx2_local
integer(c_int),dimension(:),allocatable :: MyGlobalElements,NumNz
305
306
Multiphysics Architectures
259
260
261
262
263
integer(c_int),dimension(:),allocatable :: MyGlobalElements_diagonal
integer(c_int) :: NumGlobalElements,NumMyElements,i
integer(c_int) :: indices(2),NumEntries
real(c_double) :: values(2),two_dx2
integer(c_int),parameter :: diagonal=1
264
265
266
267
268
! Executable code
nx=size(x_node)
dx=2.*pi/real(nx,c_double)
NumGlobalElements = nx
269
270
271
272
273
274
! Get update list and number of local equations from given Map
NumMyElements = map%NumMyElements()
call assert_identical( [NumGlobalElements,map%NumGlobalElements()] )
allocate(MyGlobalElements(NumMyElements))
MyGlobalElements = map%MyGlobalElements()
275
276
277
278
279
! Create an integer vector NumNz that is used to build the Epetra Matrix
! NumNz(i) is the number of non-zero elements for the ith global eqn.
! on this processor
allocate(NumNz(NumMyElements))
280
281
282
283
284
285
286
! Create a Epetra_Matrix
A = Epetra_CrsMatrix(FT_Epetra_DataAccess_E_Copy,map,NumNz)
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
!Finish up
call A%FillComplete()
!create vector x
x=Epetra_Vector(A%RowMap())
Call A%Multiply_Vector(.false.,this%f,x)
allocate(c(NumMyElements))
c=x%ExtractCopy()
!create vector of df_dx
allocate(periodic_2nd_order::d2f_dx2_local)
d2f_dx2_local%f=Epetra_Vector(map,zero_initial=.true.)
call d2f_dx2_local%f%ReplaceGlobalValues( &
NumMyElements,c,MyGlobalElements)
call move_alloc(d2f_dx2_local, d2f_dx2)
end function
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
subroutine output(this,comm)
class(periodic_2nd_order) ,intent(in) :: this
class(Epetra_Comm),intent(in) ::comm
integer(c_int) :: i,NumMyElements,NumGlobalElements
integer(c_int), dimension(:), allocatable :: MyGlobalElements
real(c_double), dimension(:), allocatable :: f_v
real(c_double), dimension(:), allocatable :: f
NumGlobalElements=map%NumGlobalElements()
NumMyElements=map%NumMyElements()
allocate(MyGlobalElements(NumMyElements))
MyGlobalElements=map%MyGlobalElements()
allocate(f_v(NumMyElements))
f_v=this%f%ExtractCopy()
allocate(f(NumGlobalElements))
call comm%GatherAll(f_v,f,NumMyElements)
do i=1,NumGlobalElements
if (comm%MyPID()==0) write(20,(2(E20.12,1x))) x_node(i),f(i)
enddo
end subroutine
354
355
356
357
358
359
1
2
3
4
5
6
subroutine force_finalize(this)
class(periodic_2nd_order), intent(inout) :: this
call this%f%force_finalize
end subroutine
end module
307
308
Multiphysics Architectures
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#endif
if (comm%MyPID()==0) write(10,*) Elapsed CPU time=,t_end-t_start
call u%output(comm)
call half_uu%force_finalize
call u_half%force_finalize
call u%force_finalize
call comm%force_finalize
#ifdef HAVE_MPI
call MPI_FINALIZE(rc)
#endif
end program
Figure 12.5. ForTrilinos-based Burgers solver: (a) eld, (b) eld_factory, (c) periodic_2nd_factory, (d) periodic_2nd_order, and (3) main program.
at ftp://ftp.nag.co.uk/sc22wg5/N1801-N1850/N1830.pdf.
8 Cite Google archive of comp.lang.fortran
309
310
Multiphysics Architectures
can even exceed that of MPI on platforms where a compiler can take advantage of
special hardware support for the coarray programming model. Cray compilers and
hardware exhibit this behavior (Numrich 2005).
Coarrays thus provide a powerful duo of simplicity and platform-agnosticism.
Whereas syntax greatly enhances the scalability of development-time processes, operating at an abstraction level slightly above other parallel programming models
enhances the scalability of runtime processes. The language standard body moved to
this higher abstraction level by using the image terminology without tying the term
to a specic technology. Hence, one compiler development team might implement
its coarray support in MPI, whereas another development team might implement it
in a hardware-specic instruction set. A third could use OpenMP. And with current
trends toward increasing single-socket parallelism with multicore chips, the greatest
benet might ultimately derive from mapping coarray syntax to a mixed model that
distributes work locally via multithreading (e.g., with OpenMP) and communicating
nonlocally via message passing (e.g., with MPI).
Consider again the 2nd-order, central-difference Burgers equation solver. The
following factors arise in attempting to get a supporting ABSTRACT CALCULUS to scale
on parallel systems using coarrays:
Data distribution.
Work load distribution, or operational model.
Necessary synchronization.
9 At the time of this writing, no fully compliant Fortran 2008 compilers have been released. Most
compilers with partial compliance have not reached a sufcient maturity in terms of features and
robustness to fully test the coarray code in this section. The GNU Fortran compiler is transitioning
towards supporting the coarray syntax for single-image runs. All code in this section has been
checked for syntactical correctness by compiling it with the development version of GNU Fortran
4.6.0 20100508 (experimental) installed via macports on a Mac OS X 10.6 platform. Where the
compiler appears to be in error, we have consulted the draft Fortran 2008 standard for guidance.
{sync all}
{sync all}
Field
- f : Array<real>
<<operator>>
+ add() : Field
+ subtract() : Field
+ multiply_field() : Field
+ multiply_real() : Field
<<assignment>>
+ assign()
+ copy()
+ state() : Array<real>
Figure 12.6. Class diagram of periodic_2nd_order class using coarray grid eld. Methods
constuct() and assign() require synchronization as noted by constraints sync.
where the asterisk represents the number of images that will be set at compile-time
or runtime. The language standard precludes any more explicit specication of the
number of images to be allocated.
When allocated, global_f behaves as though it consists of many rank-one arrays,
each afliated with an image. An image can access its own global_f array using
regular array syntax. It can also access the global_f array on another image using
an image index syntax that encloses the index in square brackets. Computing the
spatial derivatives x() and xx(), for example, requires cross-image data access at
lines 141, 144, and 148 in Figure 12.7.
Considering the second of the aforementioned factors, the work load distribution highlights a critically signicant distinction between the field interface and
its implementation in periodic_2nd_order in the context of a coarray ABSTRACT
CALCULUS: Each periodic_2nd_order type-bound function, such as the operators +
and * and the differentiation methods x() and xx(), returns a field instance rather
than a periodic_2nd_order one. (Figures 12.8 denes the field class.) This circumvents Fortrans prohibition against functions returning objects that contain coarray
components.10
10 Fortrans prohibition against allocatable coarray components inside function addresses perfor-
311
312
Multiphysics Architectures
More importantly, studying the time integration expressions reveals that none
of the supporting operations involve updates on global_f until the nal assignment. Thus, none require synchronization and their results can be stored locally
in a field. This principle can be captured using the following operational model:
While periodic_2nd_order holds a reference to the global eld, all its operators are
evaluated concurrently on all images, storing temporary results locally. Nonlocal updates need happen only after all of an expressions local operations have completed,
making the results ready for assignment back to the global_f.
The third factor, synchronization, refers to barrier points in the executing code.
By denition, all images have to reach a given barrier before executing any subsequent code. In general, a synchronization is needed after a coarray is updated
on one image and before it is accessed from another image. Based on our operational model, periodic_2nd_order assignments require synchronization. The only
other place that needs synchronization is construct(), where the allocation and
initialization of the global_f occurs. The following call is the most common form of
synchronization:
sync all
Another form of synchronization occurs implicitly upon allocation of an allocatable
coarray.
Lines 4575 in Figure 12.7 show the user-dened periodic_2nd_order constructor. This procedure could be written as a subroutine due to the aforementioned
restriction on functions returning objects containing a coarray component. The
num_images() intrinsic function employed in the constructor queries the total number of images in the run. The constructor uses that value to compute the local grid
size assuming the eld is evenly distributed on all images. Function this_image()
queries the image index of the executing image and uses that value to calculate the
initial eld values.
Figure 12.7
1
2
3
4
module periodic_2nd_order_module
use kind_parameters ,only : rkind, ikind
use field_module ,only : field
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
implicit none
private
public :: periodic_2nd_order, initial_field
type periodic_2nd_order
private
real(rkind), allocatable :: global_f(:)[:]
contains
procedure :: construct
procedure :: assign
=> copy
procedure :: add
=> add_field
procedure :: multiply
=> multiply_field
procedure :: x
=> df_dx
procedure :: xx
=> d2f_dx2
procedure :: output
generic
generic
generic
end type
24
25
26
27
28
29
30
31
32
abstract interface
real(rkind) pure function initial_field(x)
import :: rkind
real(rkind) ,intent(in) :: x
end function
end interface
33
34
contains
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
this%global_f(:) = grid()
58
59
60
61
do i = 1, local_grid_size
this%global_f(i) = initial(this%global_f(i))
end do
62
63
sync all
64
65
66
67
68
69
70
71
72
73
74
contains
pure function grid()
integer(ikind) :: i
real(rkind) ,dimension(:) ,allocatable :: grid
allocate(grid(local_grid_size))
do i=1,local_grid_size
grid(i) = 2.*pi*(local_grid_size*(this_image()-1)+i-1) &
/real(num_grid_pts,rkind)
end do
end function
313
314
Multiphysics Architectures
75
end subroutine
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
allocate (add_field)
add_field = rhs%state()+this%global_f(:)
end function
106
107
108
109
110
111
112
113
allocate (multiply_field)
multiply_field = this%global_f(:)*rhs%global_f(:)
end function
114
115
116
117
118
119
120
function df_dx(this)
class(periodic_2nd_order), intent(in) :: this
class(field) ,allocatable :: df_dx
integer(ikind) :: i,nx, me, east, west
real(rkind) :: dx
real(rkind), allocatable :: tmp_field_array(:)
121
122
123
nx=size(this%global_f)
dx=2.*pi/(real(nx,rkind)*num_images())
124
125
allocate(df_dx,tmp_field_array(nx))
126
127
me = this_image()
128
129
if (me == 1) then
west = num_images()
east = 2
else if (me == num_images()) then
west = me - 1
east = 1
else
west = me - 1
east = me + 1
end if
139
140
141
tmp_field_array(1) = &
0.5*(this%global_f(2)-this%global_f(nx)[west])/dx
142
143
144
tmp_field_array(nx) = &
0.5*(this%global_f(1)[east]-this%global_f(nx-1))/dx
145
146
147
148
149
do i=2,nx-1
tmp_field_array(i)=&
0.5*(this%global_f(i+1)-this%global_f(i-1))/dx
end do
150
151
152
df_dx = tmp_field_array
end function
153
154
155
156
157
158
159
function d2f_dx2(this)
class(periodic_2nd_order), intent(in) :: this
class(field) ,allocatable :: d2f_dx2
integer(ikind) :: i,nx, me, east, west
real(rkind) :: dx
real(rkind), allocatable :: tmp_field_array(:)
160
161
162
nx=size(this%global_f)
dx=2.*pi/(real(nx,rkind)*num_images())
163
164
allocate(d2f_dx2,tmp_field_array(nx))
165
166
me = this_image()
167
168
169
170
171
172
173
174
175
176
177
if (me == 1) then
west = num_images()
east = 2
else if (me == num_images()) then
west = me - 1
east = 1
else
west = me - 1
east = me + 1
end if
178
179
180
181
tmp_field_array(1) = &
(this%global_f(2)-2.0*this%global_f(1)+this%global_f(nx)[west])&
/dx**2
182
183
184
tmp_field_array(nx) =&
(this%global_f(1)[east]-2.0*this%global_f(nx)+this%global_f(nx-1))&
315
316
Multiphysics Architectures
185
/dx**2
186
187
188
189
190
191
do i=2,nx-1
tmp_field_array(i)=&
(this%global_f(i+1)-2.0*this%global_f(i)+this%global_f(i-1))&
/dx**2
end do
192
193
194
195
d2f_dx2 = tmp_field_array
end function
end module
Figure 12.7. Denition of periodic 2nd-order central difference eld class contains a coarray
global eld. Operation results are stored locally using eld type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Figure 12.8
module field_module
use kind_parameters ,only : rkind, ikind
implicit none
private
public :: field
type :: field
real(rkind), allocatable :: f(:)
contains
procedure :: add
=> total
procedure :: subtract
=> difference
procedure :: multiply_field => product
procedure :: multiply_real => multiple
procedure :: assign
=> assign_field_f
procedure :: copy
=> copy_filed
procedure :: state
=> field_values
generic
:: operator(+)
=> add
generic
:: operator(-)
=> subtract
generic
:: operator(*)
=> multiply_real,multiply_field
generic
:: assignment(=) => assign, copy
end type
21
22
contains
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
end subroutine
41
42
43
44
45
46
47
48
function total(lhs,rhs)
class(field) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: total
allocate (total)
total%f = lhs%f + rhs%f
end function
49
50
51
52
53
54
55
56
function difference(lhs,rhs)
class(field) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: difference
allocate (difference)
difference%f = lhs%f - rhs%f
end function
57
58
59
60
61
62
63
64
function product(lhs,rhs)
class(field) ,intent(in) :: lhs
class(field) ,intent(in) :: rhs
class(field) ,allocatable :: product
allocate(product)
product%f = lhs%f * rhs%f
end function
65
66
67
68
69
70
71
72
73
function multiple(lhs,rhs)
class(field) ,intent(in) :: lhs
real(rkind) ,intent(in) :: rhs
class(field) ,allocatable :: multiple
allocate(multiple)
multiple%f = lhs%f * rhs
end function
end module
Figure 12.8. Denition of eld type that stores operation results from periodic_2nd_order
class.
317
318
Multiphysics Architectures
The following sections briey describe the physics, the software architecture, and
the simulation results. We completed most of the related work before the availability
of Fortran 2003 compilers. As such, some of the symbols in the associated UML
diagrams represent constructs or relationships we emulated without direct language
support namely the interface implementation (inheritance) relationship symbol and
the abstract class notation. The emulation techniques build upon the programming
styles outlined by Decyk et al. (1997b, 1997a, 1998) and Akin (2003) and supported
by the memory management infrastructure described by Rouson et al. (2006).
Figure 12.9. Quantum vortices and classical vortices in turbulent superuid liquid helium.
Image courtesy of Morris (2008). For a color version of this image, please visit
http://www.cambridge.org/Rouson.
and aligned with their normal-uid counterparts so that the overall two-uid mixture moves in tandem (Morris et al. 2008). Figure 12.9 demonstrates this bundling,
colocation, and alignment.
The Navier-Stokes equations govern the normal uid velocity u:
ut = u u p + 2 u + f
u = 0
(12.4)
(12.5)
where p is the hydrostatic pressure, is the kinematic viscosity, and f is the mutual
friction force between the superuid and normal uid. The total material velocity is u + v. Morris et al. (2008) solved a transformed system of two equations: one
corresponding to the Laplacian of one component of equation (12.4) and a second
corresponding to the parallel component of the curl of equation (12.4). Choosing
the second component in the Cartesian system {x1 , x2 , x3 } and dening the vorticity
u and the nonlinear product H u yields:
2
( H) + 2 H2 + 2 ( 2 u2 )
u2 =
t
x2
2 = ( H)2 + 2 2
t
(12.6)
(12.7)
where the mutual friction force was dropped under the assumption that it inuences
the normal uid motion much less than it does the massless superuid vortex lines.
The advantage of equations (12.6)(12.7) over (12.4) lies in the global conservation of
energy (when = 0) as well as in the reduction of the number of dependent variables
from four to two.11
Section 4.3.2 presented the superuid evolution equations (4.7)(4.8), which we
repeat here for convenience:
(S r) dS
v=
(12.8)
4
S r3
dS
= v S (u v) S [S (u v)]
dt
(12.9)
Section 4.3.2 denes each of the quantities in the latter two equations.
Morris et al. (2008) solved equations (12.6)(12.7) on the semiopen, cubical domain
xj [0, 2 ), j = 1, 2, 3 with periodic boundary conditions in each direction.
Figure 12.10 shows the class model emulated by the solver of Morris et al. (2008).
At the heart of this class diagram are two physics abstractions: a Fluid class
that encapsulates equations (12.6)(12.7) and a Tangle class that encapsulates
equations (4.7)(4.8). The depicted Integrand abstract class species the interface
physics abstractions must satisfy in order to be integrated over time by the marching
algorithm of Spalart et al. (1991). Appendix A describes that scheme. An exercise
at the end of the current chapter asks the reader to write ABSTRACT CALCULUS versions of the governing equations and to enhance the class diagram to show the public
methods and the likely private data that would support these expressions.
11 The differentiations required to go from equation (12.4) to equation (12.7) destroy information
that can be recovered by solving the original equations averaged over the two directions of
differentiation: x1 and x3 . See Rouson et al. (2008b) for more detail.
319
320
Multiphysics Architectures
Integrand
Superfluid
Tangle
Fluid
1
2
Field
Two patterns appear in Figure 12.10. The Superuid class embodies the PUPPETEER pattern, whereas the Integrand class embodies an ABSTRACT CALCULUS for
time integration. That gure also presents several additional opportunities for using
patterns. Whereas the Field class embodies an ADT calculus for spatial differentiation, use of the ABSTRACT CALCULUS pattern would increase the exibility of the
design by replacing the concrete Field with an abstract class. To facilitate this, one
might also add an ABSTRACT FACTORY and a FACTORY METHOD to the design. Furthermore, the time-integration process could be made more exible via a STRATEGY and
a supporting SURROGATE. Another exercise at the end of the chapter asks the reader
to add these elements to the design.
In addition to the time advancement role the Superuid PUPPETEER plays, it also
shuttles information between the Fluid and the quantum vortex Tangle. Specically,
the Superuid queries the Tangle for the locations of each vortex lament grid point.
The Superuid queries the Fluid for the local velocities at each location vortex point
and passes these velocities to the Tangle for use in equation (4.8). Likewise, in a fully
consistent simulation that accounts for the inuence of the quantum vortices on the
normal uid motion, the Superuid would query the Tangle for the mutual friction
vectors at each vortex point and pass these to the Fluid for incorporation into the
Navier-Stokes equation (12.4).
In many applications, the Superuid might also referee the competing stability
and accuracy requirements of its puppets. A likely algorithm for doing so would
involve polling the puppets for their suggested time steps based on their private evolution equations and state vectors at a given point in time. The Superuid would then
choose the smallest answer so as to satisfy the accuracy and stability requirements
of each puppet. It might be useful to design an abstract POLLSTER to conduct such
polls when passed a pointer array of poll respondents. To facilitate this, each physics
abstraction could extend a Respondent type, and a polymorphic poll() procedure
could be written to collect the responses and set the time step.
(12.10)
with the primary variables f (x, e, t) representing the particle probability distribution
functions, x describing the particles locations, e denoting the their velocities, and
containing collisional information. For incompressible ow, the collision term can
be simplied using Bhatnagar-Gross-Krook (BGK) model (Bhatnagar et al. 1954)
as (f f eq )/ , where f eq is the particle distribution function at the state of thermal
equilibrium and is the relaxation time. We restrict ourselves to the BGK model in
this section.
The LBE method borrows ideas from cellular automata uids (Frisch et al. 1986).
A regular lattice covers the physical space domain. Particles are distributed in each
lattice with probability distribution function f . This approach can also be viewed as a
nite-difference approximation to the Boltzmann equation (He and Luo 1997). Particles travel to neighbor lattices along different directions and collide with each other.
Figure 12.11 shows the lattice geometry and velocity vectors in the two-dimensional
nine-speed D2Q9 model(He and Luo 1997). A rst-order-accurate discretization in
time leads to:
1
eq
fi (x + ei t, t + t) fi (x, t) = (fi (x, t) fi (x, t))
(i = 0, 1, , 8)
(12.11)
(0, 0)
ei = c(cos(i 1)/2, sin(i 1)/2)
e2
e5
e6
e0
e1
e3
e8
e7
Figure 12.11. The lattice of D2Q9 model.
e4
i=0
i = 1, 2, 3, 4
i = 5, 6, 7, 8
(12.12)
321
322
Multiphysics Architectures
where here c = x/t is the lattice velocity. The equilibrium form of the distribution
function is approximated as:
1
1
1
eq
2
fi = i 1 + 2 ei u + 4 (ei u) 2 u u
(12.13)
cs
2cs
2cs
where cs = c/ 3 is the LBE sound speed (He and Luo 1997), the weighting functions 0 = 4/9, i = 1/9 for i = 1, 2, 3, 4, and i = 1/36 for i = 5, 6, 7, 8. The uid density
and velocity u is obtained by:
=
8
i=0
fi ,
u =
8
ei fi
(12.14)
i=0
da=5 mm
Stent
d=4 mm
40 mm
Vorticity
t=0.3T
1E-06
0
1E-06
1.2E-06
1.4E-06
1.6E-06
1.8E-06
2E-06
2.2E-06
2.4E-06
2.6E-06
2.8E-06
3E-06
No stent
t=0.3T
t=0.3T
Triangular stent
(0.1dH_0.1dL)
t=0.3T
t=0.3T
Rectangular stent
(0.1dH_0.1dL)
t=0.3T
t=0.3T
Rectangular stent
(0.1dH_0.3dL)
t=0.3T
Triangular stent
(0.2dH_0.1dL)
Rectangular stent
(0.2dH_0.1dL)
t=0.3T
t=0.3T
Rectangular stent
(0.2dH_0.3dL)
t=0.3T
Figure 12.13. Vorticity contours inside the sac of aneurysm under the effects of different shapes
of stent. For a color version of this image, please visit http://www.cambridge.org/Rouson.
(12.15)
(12.16)
The lattice information is stored in the Grid class, including the additional boundary
lattice information used to treat the complicated geometries. The information in the
Grid class is hidden from Fluid class and can only be accessed from the Field class,
which represents the particle distribution functions in nine directions.
With this code structure, the solver can be extended to three-dimensional code
by simply modifying the Field and Grid classes to 3D domains while keeping the
323
324
Multiphysics Architectures
Integrand
Fluid
1
1
Field
1
1
Grid
Integrand
Blood
1
1
Plasma
RBC
1
1
1
Plasma_Field
RBC_Field
1
1
Grid
Fluid and Integrand classes the same. One could also develop a general solver for
both 2D and 3D ows by replacing the Field and Grid classes with ABSTRACT FACTORY
class and adding FACTORY METHOD to 2D or 3D application. For the bioengineering
problems we discussed in this section, the blood is treated as a Newtonian uid due to
the large diameter of the artery is. When people are more interested in better understanding the blood characteristics, detailed numerical simulations will be needed to
study the interactions among the blood plasma, blood cells, and elastic vessels. Due
to the different properties of blood plasma and blood cells, a multicomponent solver
would be needed for such a study. Figure 12.15 shows a class diagram that extends
the single-component LBE solver to a multicomponent solver. The RBC class represents the red blood cells in the blood plasma. The PUPPETEER pattern implemented
in the Blood class facilitated the extension.
(12.17)
bi,i = 0
(12.18)
ext
t ui = P,i + Hi + ui,jj + bj bi,j + Bext
j bi,j + Bi,j bj
(12.19)
ext
t bi = uj bi,j + bj uj,i uj Bext
i,j + Bj ui,j + bii
(12.20)
where ui and bi are the components of velocity and magnetic uctuation elds,
is the magnitude of
is the kinematic viscosity, is the magnetic diffusivity, Bext
i
externally applied magnetic eld, P = p/ + 1/2ui ui is the modied pressure, is the
uid density, p is the uctuating pressure, Hi = i,j,k uj k , i,j,k is the alternating unit
symbol, and i = i,j,k uk,j is the vorticity. Assuming particles follow the drag law of
Stokes (Stokes 1851), the equations of motion for each particle are:
dr
=v
dt
dv
1
= [u(r, t) v]
dt
p
p
2p a2
9
(12.21)
(12.22)
(12.23)
where r and v are the particle position and velocity, receptively; u(r, t) is the undisturbed uid velocity in the neighborhood of the particle; and p , a and p are the
particles hydrodynamic response time, radius, and material density, respectively.
Equation (12.22) balances particle inertia (as characterized by its mass 4p a3 /3)
against the molecular momentum exchange integrated over the particle surface (as
characterized by the transport coefcient ). The undisturbed uid velocity, u(r, t), interpolated to particle location, r, estimates the appropriate freestream velocity in the
325
326
Multiphysics Architectures
Y
Z
Figure 12.16. Planar projection of inertial particles in liquid-metal MHD turbulence under
the inuence of an externally applied magnetic eld orthogonal to the viewing plane:
fastest (red), slowest (blue). For a color version of this image, please visit http://
www.cambridge.org/Rouson.
Semisolid
Cloud
Magnetofluid
Magnet
Fluid
1
2
1
3
Field
1
1
e (u eG ) + 2 u
Ro
R
(12.24)
(12.25)
327
328
Multiphysics Architectures
x2
x3
x1
Figure 12.18. Geophysical coordinate system used for deriving the governing equations: x1 ,
x2 , and x3 point to the East (into the page), the North, and the vertical directions, respectively.
and Ro G/(2) are the Reynolds and Rossby numbers; is the kinematic viscosity; is the angular velocity of the system; and p is the hydrodynamic pressure plus
appropriate potentials.
The unit vectors associated with the system rotation and the geostrophic velocity
are e and eG , respectively. In equation (12.24), eG = ex1 , and e = ex2 . These choices
generate Coriolis forces solely in the horizontal (x1 -x3 ) plane and an effective driving
uniform pressure gradient in the negative x3 direction.
Figure 12.19 depicts the physics of interest in simulating the ABL for purposes of
understanding radar return signals. Figure 12.19(a) demonstrates streamwise velocity
component contours characteristic of vertical plumes that spread horizontally away
from the ground. Figure 12.19(b) demonstrates the high mechanical energy dissipation rates that surround the depicted plumes. Dissipative eddies make up the
ow structures with the shortest length and time scales and therefore represent
the most likely candidates for scattering short-wavelength electromagnetic waves.
Morris et al. demonstrated highly non-Gaussian statistics of the dissipation and the
corresponding much greater likelihood of the presence of the smallest-scale features
than would otherwise be estimated based on average dissipation rates.
A useful next step in understanding the anomalous return signals would be to
predict variation of quantities known to cause scattering. In the scattering mechanism identied by Tatarskii (1961), this principally includes the relative humidity.
Additionally, the relevant plume dynamics would likely be inuenced by other scalar
thermodynamic variables such as the absolute temperature. A complete study would
then involve predicting the return signals of the simulated scattering elds.
Figure 12.20 diagrams a solver architecture for simulating scalar transport in the
ABL. The depicted Atmosphere PUPPETEER aggregates zero or more scalar quantities
that would presumably be simulated via a scalar advection/diffusion equation of the
form:
1
(12.26)
t = u + 2
Sc
where is the transported scalar and Sc G/ is the dimensionless parameter
characterizing the relative importance of bulk transport and molecular diffusion with
a diffusion coefcient . In cases where these scalars actively inuence the air ow
for example, temperature variations that impart momentum to the air via buoyancy
0.30
0.20
0.10
0.00
1.6
y
x
1.8
2.0
8
2.2
3.250645360-14
2.4
2.6
16
12
17.66129438678
(a)
0.40
0.30
0.20
0.10
0.00
1.6
y
x
1.8
z
2.0
0.0001
2.2
Preido-Dispation
0.001
0.01
4.98622067E-06
2.4
2.6
0.1
0.819025337696
(b)
Figure 12.19. Instantaneous contours in a vertical slice of an Ekman layer: (a) wind velocity component, and (b) pseudo-dissipation. For a color version of this image, please visit
http://www.cambridge.org/Rouson.
329
330
Multiphysics Architectures
Integrand
Atmosphere
Fluid
Scalar
Field
1
6..
Boundary
Figure 12.20. Atmospheric boundary layer class diagram.
terminal packages, that is, applications that build atop Trilinos packages but on which Trilinos does
multiphysics applications. A suggested Morfeus use case involves end-users substituting their own concrete classes for the Morfeus examples and writing applications
that access those concrete classes only through the interfaces Morfeus publishes. The
determination of what concrete implementation gets invoked would then happen at
runtime via dynamic polymorphism. Morfeus provides only the structure through
which user-dened methods get invoked on user-dened objects. Morfeus thus exemplies a framework in the manner Gamma et al. (1995) contrast with applications
and libraries: An application is any code that has a main program, whereas a library
is code that user code calls, and a framework is code that calls user code.
Early candidates for inclusion in Morfeus include a tensor eld ABSTRACT CALCULUS, including scalar, vector, and dyadic tensor eld interfaces. Each of these
will publish generic operators and corresponding deferred bindings for arithmetic
and spatial differentiation, integration, and arithmetic. These will be based on the
coordinate-free programming concepts of Grant, Haveraaen, and Webster (2000),
and will be developed in collaboration with the second of those three authors. In
Fortran syntax, the coordinate-free programming paradigm involves writing differential operations on tensors in forms such as .grad.,.div., and .curl. without
specifying a coordinate system, deferring to a lower-level abstraction the resolution
of these forms into coordinate-specic calculations based on the selection of a manifold in which the tensors are embedded. That manifold could provide a metric tensor
in the case of general curvilinear coordinates or scale factors in the special case of
orthogonal coordinates (Aris 1962). Figure 12.21 depicts several tensor eld interfaces. That gure species no relationships between the abstract classes that describe
the interfaces. An exercise at the end of the chapter asks the reader to discuss the
pros and cons of various approaches to relating those interfaces based on the design
principles discussed in this book.
The problem decomposition described in Figure 12.21 carries with it at least two
scalability benets. First, it naturally lends itself to a purely functional, side-effectfree programming style that supports asynchronous expression evaluation as noted in
Section 12.1.2. This becomes increasingly important as massively parallel hardware
platforms evolve toward communication dominating computation in the run-time
budget. Synchronization implies communication. A design patterns that lights a
path to highly asynchronous computation thus lights a path to massively parallel
computation. In this sense, scalable design leads directly to scalable execution.
Second, Numrich (2005) demonstrated a beautiful, formal analogy between array indices and contravariant tensor indices, and likewise between coarray indices
and covariant tensor indices. This analogy naturally generates a powerful notation
for expressing parallel algorithms for matrix and vector algebra. Moreover, the analogy supports very direct mapping of objects onto distributed memory via co- and
contravariant partitioning operators. All of this enables an exceptionally direct translation of parallel mathematical algorithms into compilable, efcient code that closely
mirrors the original mathematical syntax. This makes coarray Fortran 2008 an obvious candidate for inclusion into an ABSTRACT CALCULUS. We envision a scenario in
which continuum tensor calculus expressions written at the highest abstraction levels
not, in turn, depend. The rst release of the Morfeus source is expected to occur around the time
of publication of this book.
331
332
Multiphysics Architectures
Physics
+ t(Physics) : Physics
+ L(Physics) : Physics
+ N(Physics) : Physics
Field
<<operator>>
+ grad(Field) : VectorField
+ total(Field,Field) : Field
+ difference(Field,Field) : Field
+ product(Field,Field) : Field
+ laplacian(Field) : Field
{ t = L(this) + N(this) }
Manifold
metric : Dyad
grid : Array<Points>
Dyad
VectorField
<<operator>>
+ grad(VectorField) : Dyad
+ div(VectorField) : Field
+ curl(VectorField) : VectorField
+ dot(VectorField,VectorField) : Field
+ total(VectorField,VectorField) : VectorField
+ difference(VectorField,VectorField) : VectorField
get resolved into discrete, tensor algebra expressions at the lowest abstraction levels,
and that these tensor algebra calculations get translated very directly into coarray
syntax via the analogy with covariant tensor forms.
Another early candidate for inclusion in Morfeus will be a Physics abstraction
that publishes an abstract interface for an evolution equation by dening a public time
derivative method, t(), as in Table 3.1. For Runge-Kutta schemes, t() represents
the RHS function evaluation in equations of the form 4.1. For semi-implicit time
advancement, wherein it proves useful to advance implicit terms differently from
explicit ones, the default implementation for t() might simply sum the linear and
nonlinear differential operators L and N, respectively, as indicated in Figure 12.21.
Appendix A.5.2.4 provides a description of the semi-implicit algorithm used in
several of the case studies described in Section 12.2.1.
Another candidate would be a Puppeteer abstraction that denes several default
methods for polling puppets for information that requires coordination such as time
step selection, cross-coupling terms and Jacobian construction. Additional design
patterns described in Part II of the current text also provide likely candidates either
on their own or incorporated into the other patterns. For example, the STRATEGY
and SURROGATE patterns could be incorporate into an ABSTRACT CALCULUS to provide exibility in the decision which discrete approximations will be employed to
approximate the continuous forms.
Between design and execution lies development. In this context, scalable development refers to practices for managing the complexities of programming writ large:
multideveloper, multiplatform, multilanguage, multipackage, multiuser software
construction. Scalable development methodologies must address build automation,
documentation, repository management, issue tracking, user feedback, and testing.
Hosting Morfeus on the Trilinos terminal application repository brings with it professional software engineering solutions to each of these problems. These solutions
include:
Nightly automated multi-platform builds via CMake,13
Nightly automated regression testing via the CMake tool CTest,
Automated e-mail notication of test failures with links to an online test matrix
We expect that each of these technologies will contribute to an end-to-end, ubiquitous scalability that is, scalable design leading to scalable development leading
to scalable execution with an inclusive vision of domain-specic abstractions that
disruptively reduce design and development times and thereby bring more scientic
software developers into the once-protected domain of massively parallel execution.
[T]he Most Dangerous Enemy of a Better Solution Is an Existing
Codebase That Is Just Good Enough.
Eric S. Raymond
The Morfeus framework aims to increase the exibility of multiphysics software architectures by encouraging the use of patterns. In particular, it aims to
demonstrate a scalable ABSTRACT CALCULUS that enables application developers
to operate at an abstraction level characteristic of their application domain: tensor
calculus. Moreover, the promise of coarray Fortran 2008 suggests the possibility
of writing tensor-algebraic approximations to the tensor calculus in ways that
translate directly into coarray syntax via analogies with covariant tensors. Such
an approach could represent a disruptive technology if it meets the challenge of
overcoming attachments to working codes that get the job done for the small
subset of researchers with the skills and access to use them.
EXERCISES
1. The switch from 6th-order Pad differences to 2nd-order central differences can
be accomplished with minimal code revision by designing a new concrete factory periodic_2nd_factory to replace periodic_6th_factory in Figure 9.1(e).
Write this new factory and the revise the main program of Figure 9.1(a) to use
your new factory instead of periodic_6th_factory.
2. Calculate the cumulative, overall theoretical maximum speedups that could result
from progressive optimization of the procedures in Table 12.1.
13
14
15
16
17
http://www.cmake.org
http://git-scm.com/
http://www.gnu.org/software/mailman/
http://www.bugzila.org
http://www.doxygen.org
333
334
Multiphysics Architectures
3. Progressively augment the type-bound procedures in periodic_2nd_order module in Figure 12.1 with loop-level (parallel do) directives. Use the Fortran
cpu_time() and system_time() procedures to measure cumulative speedup on
1, 2, and 4 cores for progressive parallelization of the rst four procedures listed
in Table 12.1.
4. Write ABSTRACT CALCULUS versions of the governing equations (12.6)(12.9) as
they would appear inside a time derivative method on the Integrand class of
Figure 12.10. Enhance the class diagram in that gure to include the public methods that appear in your expressions. Further enhance that gure with private data
in each class that could support the calculations you specied.
5. Replace the Field class in Figure 12.10 with an ABSTRACT FACTORY and a FACTORY
METHOD capable of constructing a periodic_6th_order concrete product. Also
add a STRATEGY to the Integrand class diagram to facilitate swapping between
2nd- and 4th-order accurate Runge-Kutta algorigthms.
6. Discuss the pros and cons of choosing aggregation or composition versus
inheritance to relate the tensor eld abstractions in Figure 12.21.
APPENDIX A
Mathematical Background
This chapter provides the mathematical background necessary for the material in this
book. The level of the treatment targets seniors and beginning graduate students in
engineering and the physical sciences.
A.1 Interpolation
A.1.1 Lagrange Interpolation
Solving the quantum vortex problem described in Chapters 4 and 5 requires estimating the 3D uid velocity vector eld u(x, t) at each quantum vortex mesh point S.
Without loss of generality, we consider here interpolating a scalar eld u(x) that can
be interpreted as a single component of u at a given instant of time. We estimate the
desired values with 3D linear Lagrange polynomial interpolation. Given a collection of data points u(x0 , y0 , z0 ), u(x1 , y1 , z1 ), . . . , u(xk , yk , zk ) across a 3D space, each
Lagrange polynomial in a series expansion of such polynomials evaluates to unity
at the location of one datum and zero at the locations of all other data. In 1D, this
yields:
k
j (x) =
i=0,i=j
(A.1)
When the problem is well resolved in space, it can be preferable for both accuracy
and efciency reasons to use only the nearest-neighbor end points of the smallest
interval (in 1D) or corners of the smallest cube (in 3D) that brackets the point of
interest. Each polynomial for the 1D case then reduces to a single factor of the form:
j (x) =
(x xi )
.
(xj xi )
(A.2)
where xi is an endpoint of the interval between xi and xj . This facilitates writing the
series:
u(x) =
1
uj j (x)
(A.3)
j=0
335
336
Mathematical Background
(i +1,j +1,k +1)
(i,j +1,k+1)
(i +1,j,k +1)
(i,j,k+1)
(x,y,z)
(i,j,+1,k)
(i,j,k)
(i +1,j+1,k)
(i +1,j,k)
(A.4)
8
uj j (x, y, z)
(A.5)
j=0
where:
j (x, y, z) =
(x xi ) (y yi ) (z zi )
(xj xi ) (yj yi ) (zj zi )
(A.6)
For the uniform grid as shown in Fig. A.1, the distances between neighbor points in
x, y, and z direction are all equal to , and we have:
u(x, y, z) =
(A.7)
(A.8)
when the determinant of matrix A does not vanish. When det A = 0, the system is
ill-posed and the equations have no unique solution. The basic Gaussian elimination
procedure includes two steps. The rst step, forward elimination, transfers the system
of equations into an upper triangular form. The second step solves the triangular
system using back substitution.
Consider the following equations:
a11
a21
..
.
a12
a22
..
.
..
.
a1n
a2n
..
.
an1
an2
ann
x1
x2
..
.
xn
b1
b2
..
.
(A.9)
bn
The augmented matrix of the above linear system is the matrix [A| b]:
a11
a21
..
.
an1
a12
a22
..
.
an2
..
.
a1n
a2n
..
.
ann
b1
b2
.
..
b
n
(A.10)
This system retains the same solution under certain elementary operations, including
row interchanges, multiplication of a row by a nonzero number, and replacement of
one row with the sum of two rows. Gaussian elimination uses these operations to
transform the equation system into upper triangular form. In the rst step, one
chooses an equation with a nonzero x1 coefcient as the pivot equation and
swaps this equations coefcients and RHS with those in the rst row of the augmented matrix. Upon switching, one zeroes out the x1 coefcients listed below the
pivot equation by multiplying the rst row of the augmented matrix by ai1 /a11 and
subtracting the result from the ith row for all i = 2, 3, , n.
A similar process based on choosing an equation with a nonzero x2 coefcient
as the pivot and swapping that equation with the second equation zeros ultimately
produces zeros below the diagonal in the second column. This process continues until
337
338
Mathematical Background
a11
0
..
.
a12
a22
..
.
..
.
a1n
a2n
..
.
ann
b1
b
2
.
..
b
n
(A.11)
where primes denote values that have been modied by the Gaussian elimination
process. Upon obtaining the upper triangular form, the back substitution process
proceeds as follows: the bottom equation immediately yields:
xn = bn /ann .
(A.12)
(A.13)
and so forth, working up the equation listing until the rst equation has been solved.
The accuracy of Gaussian elimination can deteriorate by round-off errors accumulated through the many algebraic operations. Approximately n3 /3 multiplications
and n3 /3 additions are required to solve n equations using Gaussian elimination. The
accuracy also depends on the arrangement of the equation system. Rearranging the
equation in order to put the coefcients with largest magnitude on the main diagonal
tends to improve accuracy. This rearrangement process is called partial pivoting.
Figure A.2 shows corresponding pseudocode.
1
2
Figure A.2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
!eliminate all terms in the ith colume of the rows below the pivot row
for j=i+1 to n do
for k=i to n do
factor=A(j,i)/A(i,i)
A(j,k)=A(j,k)-factor*A(i,k)
end for
end for
end for
21
22
23
24
!back substitute
x(n) = b(n)/A(n,n)
for i=n-1 to 1 do
known=0.0
for j=i+1 to n do
known=known+A(i,j)*x(j)
end for
x(i) = (b(i) - known)/A(i,i)
end for
Figure A.2. Pseudocode of Gaussian elimination with partial pivoting.
Chapter 3 demonstrates the application of Gaussian elimination to a linear system that arises in solving a n heat conduction problem, wherein one obtains a vector
of temperature values T n+1 at time tn+1 from the temperature vector T n at a previous
time step tn = tn+1 t for some time step t according to:
t 2 1 n
T
T n+1 = I
(A.14)
where I is the identity matrix with unit values along the diagonal and zeros elsewhere.
Given a uniformly spaced grid overlaid on the computational domain, applying the
central difference formula of Section A.5.1 to the RHS of equation (A.14) leads to
the linear algebraic equations in the form of (A.8), where:
A=
1+
2t
(x)2
t 2
(x)
..
.
0
t
(x)2
1 + 2t 2
(x)
..
.
0
..
.
0
..
.
0
..
.
0
..
.
t
(x)2
T1n+1
T2n+1
..
.
1 + 2t 2
(x)
t 2
(x)
t 2
(x)
1 + 2t 2
(x)
(A.15)
x=
n+1
Tm1
n+1
Tm
n
T1 + t 2 Tchip
(x)
T2n
..
b=
.
Tm1
n + t T
Tm
2 air
(A.16)
(A.17)
(x)
where m is the number of discrete node points at which one seeks n temperatures and
x is the uniform space between each node. Matrix A is diagonally dominant, in the
sense that the magnitude of each main diagonal element exceeds that of the other elements in any given row. Diagonal dominance eliminates the need for partial pivoting.
Figure (A.3) provides a Fortran subroutine implementation of Gaussian elimination
without partial pivoting useful for solving the above equations. Figure (A.4) presents
an equivalent C++ code.
339
340
Mathematical Background
2t
(x)2
2
t
(x)2
1 + 2t 2
(x)
i = 2, 3, , m
(A.18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
T2
t
(x)2
1 + 2t 2
(x)
= T2n +
Tm
t
(x)2
1 + 2t 2
(x)
n
= Tm
+
T1n +
n
Tm
+
t
(x)2
Tchip
t
(x)2
(A.19)
Tair
(A.20)
Figure A.3
module linear_solve_module
use kind_parameters ,only : rkind,ikind
implicit none
contains
function gaussian_elimination(lhs,rhs) result(x)
real(rkind) ,dimension(:,:) ,allocatable ,intent(in) :: lhs
real(rkind) ,dimension(:) ,allocatable ,intent(in) :: rhs
real(rkind) ,dimension(:)
,allocatable :: x,b ! Linear system:
real(rkind) ,dimension(:,:) ,allocatable :: A
! Ax = b
real(rkind)
:: factor
real(rkind) ,parameter :: pivot_tolerance=1.0E-02
integer(ikind)
:: row,col,n,p ! p=pivot row/col
n=size(lhs,1)
b = rhs ! Copy rhs side to preserve required intent
if ( n /= size(lhs,2) .or. n /= size(b)) &
stop gaussian_elimination: ill-posed system
allocate(x(n))
A = lhs
! Copy lhs side to preserve required intent
!______ Gaussian elimination _______________________________________
do p=1,n-1
! Forward elimination
if (abs(A(p,p))<pivot_tolerance) &
stop gaussian_elimination: use pivoting
do row=p+1,n
factor=A(row,p)/A(p,p)
forall(col=p:n) A(row,col) = A(row,col) - A(p,col)*factor
b(row) = b(row) - b(p)*factor
end do
end do
x(n) = b(n)/A(n,n) ! Back substitution
do row=n-1,1,-1
x(row) = (b(row) - sum(A(row,row+1:n)*x(row+1:n)))/A(row,row)
end do
end function
end module
Figure A.3. Gaussian elimination procedure.
Ti = Tin +
where d1 = 1 +
according to:
2t
.
(x)2
t
(x)2
1 + 2t 2
(x)
n
Ti1
Tm
dm
Figure A.4(a)
#ifndef GAUSSIAN_ELIMINATION_H_
#define GAUSSIAN_ELIMINATION_H_ 1
3
4
5
#include <exception>
#include "mat.h" // 2-D array
6
7
8
9
10
11
crd_t gaussian_elimination (const mat_t & lhs, const crd_t & rhs);
12
13
1
2
#endif
Figure A.4(b)
#include "gaussian_elimination.h"
#include <cmath>
3
4
5
6
7
8
9
10
11
12
13
if ( n != lhs.cols() || n != rhs.size()) {
std::cerr << "gaussian_elimination: ill-posed system"
<< std::endl;
throw gaussian_elimination_error();
}
14
15
16
17
18
19
20
21
(A.21)
i = 3, 4, , m 1
(A.22)
341
342
Mathematical Background
if (fabs(A(p,p)) < pivot_tolerance) {
std::cerr << "gaussian_elimination: use pivoting"
<< std::endl;
throw gaussian_elimination_error();
}
for (int row = p+1; row < n; ++row) {
real_t factor = A(row,p) / A(p,p);
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
b.at(row) -= b.at(p)*factor;
37
// Back substitution
crd_t x(n);
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
}
return x;
53
Figure A.4. Gaussian elimination in C++. (a) Header le. (b) C++ code.
Tin+1
Ti +
t
T n+1
(x)2 i+1
di
i = m 1, m 2, , 1
(A.23)
A.2.2 LU Decomposition
LU decomposition functions improves upon Gaussian elimination process by factoring the coefcient matrix into a form that can be reused with multiple RHS b vectors.
The operation count for solving the resulting factored system is lower than the count
for Gaussian elimination. This approach proves useful when a linear system must be
solved at each time step of a differential equation solver and the coefcient matrix
stays constant over time. Such is the case for equations (A.57) and (A.58).
LU decomposition factors any nonsingular coefcient matrix A (that is one with
nonzero determinant) into the product of a lower triangular matrix L and an upper
triangular one U. When no pivoting is required, the factorization takes the simplest
form: A = LU. With pivoting, LU decomposition takes the form PA = LU, where P
is a permutation matrix. Permutation matrices have one unit entry in each row and
each column with zeros elsewhere. The multiplication of PA produces a new matrix
by interchanging the rows in matrix A so that PA has an LU decomposition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Figure A.5
function thomasTimes(lhs,rhs)
real(rkind) ,dimension(:,:) ,allocatable ,intent(in) :: lhs
class(field)
,intent(in) :: rhs
type(field)
,allocatable :: tomasTimes
real(rkind) ,dimension(:)
,allocatable :: x,b ! Linear system:
real(rkind) ,dimension(:,:) ,allocatable :: A
! Ax = b
real(rkind)
:: factor
integer(ikind)
:: row,n
n=size(lhs,1)
b = rhs%node
! Copy rhs side to preserve required intent
if ( n /= size(lhs,2) .or. n /= size(b)) &
stop thomasTimes: ill-posed system
allocate(x(n))
A = lhs
! Copy lhs side to preserve required intent
!______ Establish upper triangular matrix _______________________
do row=2,n
factor=A(row,row-1)/A(row-1,row-1)
A(row,row) = A(row,row) - factor*A(row-1,row)
b(row) = b(row) - factor*b(row-1)
end do
!______ Back supstitution _______________________________________
x(n) = b(n)/A(n,n) ! Back substitution
do row=n-1,1,-1
x(row) = (b(row) - A(row,row+1)*x(row+1))/A(row,row)
end do
allocate(thomasTimes)
thomasTimes%node = x
end function
Figure A.5. Thomas algorithm procedure.
343
344
Mathematical Background
f (x)
Xn
Xn+1
Figure A.6. One iteration of Newtons method (dashed line is the tangent line of f [x] at xn )
reference point xn :
f (xn + x) = f (xn ) + f (xn )x +
(A.24)
f (xn )
.
f (xn )
(A.25)
Figure A.6 illustrates this process. One nds the root of f (x) by repeating this process until |x| is sufciently small. Starting sufciently close to the desired root
of a smooth function, Newtons method converges faster than some competing
methods and it can diverge for poor initial guesses or ill-behaved functions. One
pathological case occurs when the initial guess lies at a stationary point that is, a
point with a horizontal tangent line, in which case the iteration step (A.25) requires
division by 0.
(A.26)
where the Jacobian matrix J generalizes the 1D derivative f . The ij th element of J is:
Jij =
fi (x)
i = 1, 2, , m j = 1, 2, , m.
xj
(A.27)
edV = q dS
(A.28)
t V
S
where , e, and q are the material density, thermal energy per unit mass, and surface
heat ux, respectively, and where the LHS and RHS integrals are volume and surface
integrals, respectively. The LHS of equation (A.28) represents the total thermal
energy in the volume, whereas the RHS represents the net heat transfer into the
volume at the boundary.
One can combine the two sides of equation (A.28) into a single term by applying
Gausss divergence theorem to convert the RHS of equation (A.28) to a volume
integral so that:
( [e) + q] dV = 0
(A.29)
t
V
Because there is nothing special about the volume V, equation (A.29) holds
for any arbitrary volume, including one shrunk to within an innitesimally small
neighborhood of a point. This forces the integrand to vanish at all points:
(e) + q = 0
t
(A.30)
Writing the energy, e cT, in terms of the specic heat c and temperature T, and writing Fouriers law of heat conduction q kT, where k is the thermal conductivity,
yields:
T
= 2 T
(A.31)
t
where k/(c) is the thermal diffusivity. Equation (A.31) is the heat equation.
We now present the analytical solutions for the 1D heat equation problem solved
numerically throughout Part I of this text. In equation 1.1, the heat equation is
subjected to the initial condition T(x, 0) = Tair and the boundary condition T(0, t) =
Tchip and T(Ln , t) = Tair . If T(0, t) = T(Ln , t) = 0, the heat equation can be solved
by the method of separation variables and yields the general solution:
T(x, t) =
n=
Bn sin
n=0
where:
2
Bn =
Ln
n n2 2 t/L2n
xe
Ln
Ln
Tair sin
0
n
xdx
Ln
(A.32)
(A.33)
345
346
Mathematical Background
(A.35)
The solution is T1 = A + Bx where A = Tchip and B = (Tair Tchip )/Ln . The solution
T1 does not satisfy the initial condition. In order to have T(x, 0) = T1 (x) + T2 (x, 0) =
Tair , T2 must satisfy the initial condition:
T2 (x, 0) = Tair Tchip
Tair Tchip
x
Ln
(A.36)
T2 also must satisfy the heat equation and the boundary conditions:
T2 (0, t) = 0, T2 (Ln , t) = 0
(A.37)
n=
Bn sin
n=0
where:
2
Bn =
Ln
Ln
n n2 2 t/L2n
xe
Ln
Tair Tchip
n
Tair Tchip
x sin
xdx
Ln
Ln
(A.38)
(A.39)
Thus, the analytical solution of the temperature in equation 1.1 is T(x, t) = T1 (x) +
T2 (x, t).
A.4.2 The Burgers Equation
Of much younger vintage, the Burgers equation at rst appears more daunting due
to its nonlinearity:
(A.40)
ut + uux = uxx .
Nonetheless, as mentioned in Section 4.3.3, the Burgers equation can be transformed
into the heat equation and solved exactly for the case of periodic boundary conditions
employed in the text. Section 4.3.3 provides the resulting analytical solution. Here we
briey sketch the analytical solution process. The initial condition is u(x, 0) = A sin(x)
and the boundary condition is u(x + n , t) = u(x, t) = 0 for all integers n. As described
in Section 4.3.3, the transformed Burgers equation is:
t = xx .
(A.41)
(x, 0) = e 2
A sin(x)dx
= e 2 A cos(x)
(A.42)
347
cn en t cos(nx)
2
(A.43)
n=
where:
cn =
1
2
1
2
(x, 0) cos(nx)dx
1
e 2 A cos x cos(nx)dx
(A.44)
The above integration can be expressed by Bessel function In as (Benton and Platzman 1972):
cn = (1)n In (
A
)
2
(A.45)
n=
n2 t sin(nx)
n=1 ncn e
n2 t cos(nx)
c0 + 2 n=
n=1 cn e
(A.46)
x
x
(A.47)
(x0 ,y0 )
x2
+
2
(A.48)
348
Mathematical Background
(x0, y0+y)
(x0x, y0)
(x0+x,y0)
(x0, y0)
(x0, y0y)
The nite-difference approximation to f /x derives from rearranging equation (A.48) and combining it with equation (A.47) so that:
f
2 f
f
+
=
x
x (x0 ,y0 ) x2
(x0 ,y0 )
x
+
2
(A.49)
where the second and subsequent RHS terms represent the truncation error, which is
the difference between the f /x and f /x. The truncation error behaves asymptotically like its leading term, which is usually specied with the order of (O) notation,
so the truncation error behaves like:
e
f
f
= O(x)
x (x0 ,y0 ) x
(A.50)
(x0 ,y0 )
x2
+
2
(A.51)
x (x0 ,y0 )
x
f
=
+ O(x)
x (x0 ,y0 )
(A.52)
(A.53)
The leading-order truncation error for this approximation equals that of the forward
difference formula.
Higher-order nite-difference approximations result from combining Taylor series expansions of f at different points. For example, one derives the second-order,
central difference formula by subtracting equation (A.51) from equation (A.48) and
rearranging so that:
f (x0 + x, y0 ) f (x0 x, y0 )
f
(A.54)
x (x0 ,y0 )
x
which has a leading-order error of order O[(x)2 ]. We leave the derivation of the
precise form of this error as an exercise for the reader. Other combinations of Taylor
series produce approximations to higher-order derivatives. Adding equation (A.51)
to equation (A.48) gives a second-order-accurate approximation to the second partial
derivative of f with respect to x:
2 f
f (x0 + x, y0 ) 2f (x0 , y0 ) + f (x0 x, y0 )
(A.55)
2
x
(x)2
(x0 ,y0 )
y (x0 ,y0 )
y
which has a leading-order error of order O(y), and likewise in the z direction.
Higher-order nite difference methods result from canceling additional error
terms. To avoid broadening the stencil, compact schemes can be constructed with
special care. These generally express the derivative approximation implicitly, necessitating the solution of a linear system of equations. The solution to this system yields
the derivative approximation at all points in the domain simultaneously. The classical Pad schemes take this form (Moin 2001) for the fourth-order rst derivative:
f
f
f
3
+
+4
=
(A.57)
(fx +x fxi x )
x (xi +x) x (xi x)
x xi x i
and fourth-order second derivative:
1 2 f
10 2 f
1 2 f
+
+
12 x2
12 x2
12 x2
(xi x)
xi
(xi +x)
(A.58)
349
350
Mathematical Background
(A.59)
(A.60)
x, 0) = U0 (x), x
U(
(A.61)
{U1 , U2 , ..., Un }T is the problem state vector; x and t are coordinates in the
where U
{1 , 2 , ..., n }T is a vector-valued operator
space-time domain (0, T]; and
that couples the state vector components via a set of governing ordinary-, partial-, or
U)
typically
integro-differential equations. The space domain is bounded by , B(
and its derivatives, and C
species
represents linear or nonlinear combinations of U
the values of those combinations on .
Numerical solution of (A.59) on a digital computer requires discretizing space
and time. Adopting the aforementioned semidiscrete approach and applying the
spatial nite difference methods of the previous section results in a system of ODEs
of the form:
d
V)
V = R(
(A.62)
dt
contains the discretized values of U
on the grid and the vector function
where V
R represents the discrete approximation to the RHS of (A.59) derived via nite
differences or another suitable approximation formalism.
Considering the following single, rst-order ODE simplies the presentation of
approaches to solving (A.62):
dy
= f (y)
(A.63)
dt
The numerical methods discussed next estimate the solution yn+1 at time tn+1 =
tn + t based on a know solution at tn .
t=tn
According to equation (A.63), dy/dt|tn = f (yn ). Using this information and truncating equation (A.64) after two RHS terms produces the forward Euler method:
yn+1 = yn + tf (yn ),
(A.65)
which is also known as the explicit Euler method because the unknown quantity yn+1
appears only on the LHS.
Whereas studying the truncation error addresses the asymptotic behavior of a
numerical method for innitesimal t, ensuring accurate solutions in practice requires also considering the methods behavior at nite t. In the most pathological
cases, errors that stay under control at sufciently small t might explode at larger
t. Ideally, numerical time-integration algorithms would remain stable whenever
the exact solution is stable, and unstable only when the exact solution is unstable.
One gains some understanding of a time advancement schemes stability
properties by studying its behavior for a simple, model problem:
dy
= y,
dt
(A.66)
where R + iI and where R and I are the real and imaginary parts of . Since
equation (A.66) admits an exponential solution, a sufcient condition for a stable
exact solution is R 0. This bound thus holds also for the problems of interest in
studying the stability of time advancement schemes.
Applying explicit Euler method (A.65) to the model problem (A.66) yields:
yn+1 = yn + tyn
(A.67)
(A.68)
(A.69)
Therefore, the Euler explicit method is conditionally stable in the sense that the time
step must be small enough to satisfy (A.69).
The expression just to the left of the inequality sign in equation (A.69) is a
polynomial in t. All explicit time-advancement schemes generate stability criteria
that place polynomial bounds on the time step. As such, explicit schemes suffer
from the same problem that aficts high-order polynomial interpolation schemes
as mentioned in Section A.1.1: All polynomials diverge at innity. This precludes
unconditional stability because any polynomial will violate the inequality in (A.69)
outside some nite region of the complex- plane. As we demonstrate next, implicit
methods circumvent this restriction by generating stability bounds based on rational
polynomials that is, ratios of polynomials. Some implicit algorithms even offer
unconditional stability: They are stable for all problems for which the exact solution
is stable.
The implicit Euler scheme derives from expanding y in a Taylor series about the
time tn+1 :
t 2
t 3
d3 y
dy
d2 y
yn+1 = yn
3
+
(A.70)
t + 2
dt t=tn+1
2
6
dt
dt
t=tn+1
t=tn+1
351
352
Mathematical Background
(A.71)
which is also called the backward Euler method. The additional stability afforded
by implicit schemes comes at the cost of requiring iterative solutions when f (yn+1 ) is
nonlinear. This additional complexity generally manifests in increased solution costs
relative to explicit methods.
Applying the implicit Euler scheme to the model problem (A.66) leads to:
yn+1 = yn + tyn+1 ,
(A.72)
(A.73)
where the amplication factor is the rational polynomial = 1/(1t). The method
is stable if, and only if, | | 1, which always holds when the exact solution is stable
(R 0). Therefore, the implicit Euler method is unconditionally stable.
A.5.2.2 Trapezoidal Method
An alternative strategy for developing time-advancement algorithms integrates
equation (A.63) over the interval [yn , yn+1 ] to obtain:
tn+1
yn+1 = yn +
f (y) dt
(A.74)
tn
f [y(t)]
fi i (t),
(A.75)
i=nm
yn+1 = yn +
ci
ci fi ,
(A.76)
i=nm
tn+1
tn
i (t) dt.
(A.77)
In the degenerate case of a single quadrature point, the only available interpolating
polynomial is a constant. Choosing the point tn , for example, generates the polynomial n = 1, which produces the explicit Euler formula (A.65). Choosing instead
tn+1 generates the polynomial n+1 = 1 and the implicit Euler formula (A.65). This
general machinery outputs a time-integration algorithm in the form of coefcient
values, {ci |i = n m, ..., n + 1}, for each choice of quadrature points and resulting
interpolating polynomials.
n +1
0
tn1
tn2
tn
tn+1
Figure A.8. Linear Lagrange polynomials for the case in which the f values at tn and tn+1 are
used to derive a numerical quadrature scheme.
Figure A.8 displays the case in which the two quadrature points are the previous
time step, tn , and the next one, tn+1 . With these points, the interpolating polynomials
are the two lines shown: n (t) and n+1 (t). In this simple case, one can evaluate the
integral (A.77) by graphical inspection:
yn+1 = yn +
t
[f (yn ) + f (yn+1 )],
2
(A.78)
which is called the trapezoidal rule. The trapezoidal rule provides the timeintegration mechanism in the Crank-Nicolson method, which couples trapezoidal time integration with central difference spatial approximation for the heat
equation (1.1). In the 1D case, the Crank-Nicolson method is thus:
Tjn+1
= Tjn +
t
2
n+1
n+1
2Tjn+1 + Tj1
Tj+1
(x)2
n 2T n + T n
Tj+1
j
j1
(x)2
"
(A.79)
353
354
Mathematical Background
(A.80)
where k1 = tf (yn ) and k2 = tf (yn + k1 ). The values of 1 , 2 , and determine
the methods truncation error. A common trait among Runge-Kutta methods is
that they truncate the Taylor series expansion of the solution precisely at the term
corresponding to the order of accuracy of the method. In other words, the leadingorder error term precisely equals the rst omitted term in the Taylor expansion of the
exact solution. According to the Taylor series expansion given by equation (A.64),
second-order accuracy requires retaining three terms:
(t)2 df
f (yn )
(A.81)
yn+1 = yn + tf (yn ) +
2 dy t=tn
Comparing the estimated solution in the RK2 formula (A.80) to the truncated Taylor
expansion (A.81) determines the the RK2 constants. To facilitate a direct comparison, one substitutes the Taylor series expansion of k2 about yn equation (A.80). This
leads to:
df
yn+1 = yn + (1 + 2 )tf (yn ) + 2 (t)2
f (yn )
(A.82)
dy t=tn
Comparing equation (A.82) to equation (A.81) and matching the coefcients of the
similar terms gives:
1 + 2 = 1
1
2 =
2
(A.83)
(A.84)
(A.85)
(A.86)
yn+1/2 = yn +
(A.87)
(A.88)
corresponding to = 0.5.
A.5.2.4 Semi-implicit Methods
To take advantage of the stabilizing effect of implicit schemes in the presence of
diffusive terms that impose severe time step restrictions while avoiding iterative solution of nonlinear equations, it is attractive to advance linear and nonlinear terms
(A.89)
where L contains all linear terms and N contains all nonlinear and inhomogeneous
terms, the Spalart et al. algorithm takes the form:
U = Un + tn+1 {L[1 U + 1 Un ] + 1 N(Un )}
(A.90)
(A.91)
(A.92)
where primes denote substep values between the nth and (n+1)th time steps, and
tn+1 = tn+1 tn . For N, this approach resembles an explicit Euler substep followed
by two second-order Adams-Bashforth substeps N1 . For L, this approach resembles
trapezoidal integration.
Third-order accuracy can be achieved by matching the Taylor series:
t 2 Ri
Uj
2
Uj
3
(A.93)
j=1
t 3 2 Ri
3
Uj Uk + O(t 4 )
6
Uk Uj
3
=+
(A.94)
j=1 k=1
(A.95)
(A.96)
(A.97)
{1 , 2 } {17/60, 5/12}
(A.98)
355
356
Mathematical Background
(A.99)
(A.100)
APPENDIX B
358
fin analyzer
association
specify
problem
actor
system
architect
design
numerical
method
numerical analyst
predict heat
conduction
system boundary
thermal analyst
use case
Figure B.1. Use case diagram for a n heat conduction analysis system.
3. Associations: connections between use cases and actors. An association is established when an actor interacts with the system through a use case that describes
the interaction. An association is drawn using a solid line connecting an actor and a use case. Optionally an arrow can be added to indicate the direction
of interaction, that is, who initiates the interaction. In the n analyzer system, ve associations are identied with the following pairs of actor and use
case: system architectspecify problem, numerical analystspecify problem, numerical analystdesign numerical method, thermal analystdesign
numerical method, and thermal analystpredict thermal conduction.
4. The system boundary: shown as a rectangular box to indicate the scope of the
system. All the use cases are inside the system boundary, and all actors are
outside the system boundary.
5. Other relationships: relationships between actors and between use cases. Actor
generalization and use case relationships, such as include, extend, and
generalization, are also common in practice (Booch et al. 1999).
A use case diagram can also include packages to group different modeling elements
into chunks. The UML package diagram provides a convenient way to depict very
general collections of model elements. The elements need not be object-oriented. For
example, Figure 6.1 depicts a package containing a class and a stand-alone procedure
that accepts an instance of that class as an argument. Because package diagrams see
minimal use in the current text, appearing only in Figure 6.1, we go into no greater
depth here.
In our n analyzer example, the interaction drawn between the system architect
and the n analyzer system depicts use of the system to specify the heat conduction
problem. This usage is depicted by specify problem use case. The output from this
problem specication including the production of a set of physical parameters such
as tolerances, boundary conditions, initial heat conditions, and so on becomes the
input for another actor, the numerical analyst. Note the numerical analyst has two
use case associations with the n analyer. First, she receives data from the n analyzer
through the specify problem use case. This is depicted by the numerical analyst
specify problem association. Then she develops a numeric model based on these data
from the n analyzer. The latter interaction is depicted by the numerical analyst
design numerical method association. Similarly, the thermal analyst receives the
numeric model produced by the numerical analyst and uses this model to predict the
n heat transfer performance.
Use cases diagrams produce a static view of a system. Applications of use case
diagrams in modeling typically take two forms (Booch et al. 1999):
1. Actor-centric: model the context of a system. This modeling method places an
emphasis on actors and their roles, focusing on the environment surrounding
the system. It starts with identifying actor and organizing actors according to
their roles, followed by pinpointing use cases and their associations with actors
based on the actors behaviors and requirements on the system. Once the use
case diagram is completed, all use cases are associated with one or more actors.
Our n analyzer example follows this approach.
2. System behavior-centric: model the requirements of a system. Much like specifying a contract on the system functionalities, this approach focuses on the system
behaviors. It begins with identifying actors and gathering their requirements to
the system. Use cases are developed based on these requirements. Additional
use cases may be developed based on additional system requirements, variants
to existing use cases, and so on. (Booch et al. 1999) has provided an example
of validation system for credit card transactions to illustrate this approach. A
detect card fraud use case is an addition to the use case diagram due to security requirements. Similarly, they introduced a manage network outage use
case to the diagram due to a requirement that the system provide reliable and
continuous operations.
In each case, use case diagrams aid in visualizing and documenting the system
behavior. They can also aid in documenting the business requirements. Ideally, these
diagrams enhance designers, developers, and domain experts understanding of the
system before and during construction.
359
360
visibility
Astronaut
attribute
greeting : String
<< constructor >>
stereotype
+ astronaut(String) : Astronaut
<< process >>
+ greet () : String
operations
three-compartment box. In Figure B.2, we redraw the Astronaut class from Figure 2.2
to illustrate the following elements of a class:
Name: The top compartment holds the class name, Astronaut (Figure 2.2). The
name is the only mandatory part of a class diagram. In diagrams that contain
large collections of classes (hundreds or thousands in some cases), it is common
to omit details such as attributes and operations so readers can focus on the
relationships between classes.
Attributes: As shown in Figure B.2, the middle compartment of a class is used to
show the class attributes. These are the Fortran-derived type components or the
C++ data members. The attributes are normally drawn in one of two ways:
1. By showing attribute names only.
2. By specifying attribute names followed by their data types, separating the
two by a colon. Additionally the diagram can provide default attribute
values.
Our example in Figure B.2 uses the second method. The Astronaut class contains
one attribute, greeting, of type String.
Operations: An operation is a service a class provides. Common Fortran
operations include type-bound procedures, dened operators, and dened
assignments. Common C++ operations include virtual member functions and
overloaded operators. As shown in Figure B.2, the bottom compartment of
a class box displays the operations. Similar to attributes, operations can be
specied with one of two methods:
1. By showing operation names only.
2. By specifying operation names accompanied with their function signatures,
including the procedure name and argument types followed by a colon and
the return data type, if any.
Our Astronaut example employs the latter method to reveal more information
about the operations. Two operations are shown in the Astronaut class: an overridden constructor astronaut(), which takes a string and returns a constructed
Astronaut object; and the procedure greet(), which produces String.
As with attributes, the list of operations of a class can be omitted from the
diagram if the emphasis of the design is placed on other aspects of the system
such as the interactions between classes.
Responsibilities: Optionally, one can detail a classs responsibilities in an additional compartment drawn below that of the operations. The responsibility part
of a class describes the contracts or obligations of the class (Booch et al. 1999).
In addition to these basic elements, there are some further notations commonly
employed to specify additional information on classes:
Visibility: Visibility refers to the accessibility of attributes or operations by other
1 The protected keyword in Fortran denotes a module variable that is visible but not directly
361
362
and Fortran parameterized derived types are both template classes. In UML,
a template class is drawn as an ordinary class with an additional dashed box
in the upper-right corner of the class. This additional box shows the template
parameters used to instantiate an object of this class. As shown in Figure 2.3(a),
the Array class in Chapter 2 is a template class, with Element being the sole
template parameter.
Datatypes: As one of the classiers available in UML, a datatype represents
a set of data values, including intrinsic data types and enumerations. The graphical representation of a datatype is similar to a stereotype. Figure 2.3(b) shows
a common usage of datatypes in UML modeling: dening data types used in
template classes. In the example, data types of Integer, Character, Real, and
Boolean can be used as for the template parameter Element.
B.2.2 Relationships
In UML, a relationship is abstracted as a connection between two elements. In class
diagrams, all relationships are represented lines connecting classes. The following
three kinds of relationships are identied as the most important relationships in
OOD: dependency, generalization, and association.
B.2.2.1 Dependency
A dependency is a relationship between two classes wherein one class depends on
the behaviors of the other. Most often this indicates a use relationship between two
classes: We say class A depends on class B when B is used either as a passed parameter
or as a local variable for the operations of A. One draws dependencies in UML using
a dashed line connecting two classes with an arrow pointing to the class on which the
other class depends.
Derived from Figure 3.4, Figure B.3 emphasizes the class dependencies of the
heat conduction system given in Section 3.1. We only draw the dependencies used in
Integrable_conductor
<<constructor>>
integrable_conductor(Problem, Procedure)
Field
Differentiator
<<constructor>>
field(Problem, Procedure)
Problem
the heat conduction system, omitting other relationships between classes. In the
system, both the Integrable_conductor class and Field class have a dependency
on the Problem class. The class denitions for Integrable_conductor and Field
also highlight the cause of their dependencies on Problem: The constructions of
Integrable_conductor and Field require the passing of a Problem object. From the
corresponding code in Figure 3.2 and Figure 3.3, we can further see that the Fields
construction is directly impacted by the implementation (or implementation changes)
of Problem because the Field derives the nodal values, the stencil, and the system boundary conditions from the Problem attributes, whereas the dependency on
Problem for constructing an Integrable_conductor object is pertinent to that of its
T_field component. In this sense, we can say Field has a stronger dependency on
Problem.
363
364
surrogate
integrand
lorenz
strategy
euler_integrate
generalization
timed_lorenz
runge_kutta_integrate
Figure B.4. Class generalizations and realizations in SURROGATE and STRATEGY patterns for a
timed Lorenz system.
B.2.2.3 Association
An association represents a link or connection between two classes. Graphically,
associations add visual cues to the structural relationships between classes in a class
diagram. In UML, an association is drawn as a solid line connecting two classes.
Beyond the basic form, a few adornments supply additional information when
applied to association relationships:
Name and direction: An association name (usually a verb) is used to describe
FieldFactory
re
associations
tu
rn
Periodic6thOrder
Periodic6thFactory
constructs
direction
name
ABSTRACT FACTORY
multiplicity
HermeticFieldPointer
1 *
1
HermeticField
target
Role name
365
366
Puppeteer
aggregation
Ground
Air
Cloud
Figure B.7. In the PUPPETEER pattern, puppeteer is an aggregation of air, cloud, and ground.
also responsible for the disposition (e.g., creation and destruction) of its parts
in a composition2 . In UML, a composition is represented using a lled diamond
attached to the whole.
abstraction of similar things, an object gives a concrete instance of one. Similarly to a class, an object is also depicted using a box in UML. However, an object
can be readily distinguished from a class by the fact that the text contained in
the name compartment is underlined. Also the text is usually composed of two
components: an object name followed by a class name, separated by a colon (:).
Sometimes an object can be anonymous, that is, the object name before the
colon is omitted. This usually characterizes a temporary object returned from a
function call. In rare situations, one can also render objects without associated
class abstractions. These objects are called orphans, and readers can refer to
Booch et al. (1999) for more details.
In addition to the name compartment, an object can have a compartment
showing attributes and their values. Collectively they represent the particular
2 It can be confusing to decide whether an aggregation or a composition relationship should be used
in a design. The difference between them is subtle, and the design errors caused by making a wrong
choice are normally very minor.
object name
fin:Conductor
object
diff
attribute
composition relationship
link
diff:Differentiator
diff_matrix = (/1.,2.,1./)
attribute values
Figure B.8. An object diagram that shows the object n of class Conductor is composed of an
object diff of class Differentiator.
state that the object is in. Figure B.8 contains two objects: the heat conductor n
and the differentiator diff. Their respective types are Conductor and Differentiator. Further we note that n contains a private component, named diff, whereas
n is in its initial state with its attribute diff_matrix being assigned to a specic
set of values.
Links: Analogous to an object being an instance of a class, a link is an instance
of an association. As with associations, links show relationships between objects
in an object diagram. In UML, a link is rendered using a line connecting two objects, just like an association in class diagrams. In addition, adornments used on
associations, that is, names, multiplicities, roles, and aggregations/compositions,
can also be applied to links. In Figure B.8, one can observe the fact that n is a
composition of diff.
Both the object diagrams and the class diagrams described in B.2 belong to the
structural modeling category. Both types of diagrams are commonly used to capture
the static view of a system. In the next section, we introduce sequence diagrams that
focus primarily on modeling the dynamic aspects of the system.
367
368
description
call
return
send
create
destroy
object
sequence number
message
1 : xx()
fin:integrable_conductor
T_field:field
action call
link
1:1 <<create>>
action create
1.2 : laplacian()
:field
stencil:differentiator
1:3: return
action return
Figure B.9. Interactions among objects during the computation of the 2nd-order spatial
derivative for forward Euler method.
Figure B.9 is a communication diagram, and it contains all the elements normally
found in a communication diagram. Semantically a communication diagram and a
sequence diagram are equivalent in that one can convert from one type of diagram
to the other without losing any information.
We choose to use a sequence diagram in our examples based on two factors.
First sequence diagrams are more explicit in revealing the time ordering among
different actions during an operation. In a complex operation, particularly when
many function calls are made between the same couple of objects at different times,
the sequences of calls can become cluttered in a communication diagram, but can
always be shown clearly in a sequence diagram. The second factor is that the sequence
diagram has analogues in other elds. For example, the Message Sequence Chart
(MSC) used in the telecommunication industry is nearly identical to a sequence
diagram.
We redraw the interactions of Figure B.9 using a sequence diagram in
Figure B.10. By comparing these two gures, one can observe that the vertical axis
in a sequence diagram denotes the time. The higher a message is drawn, the earlier
the event occurs.
Next, every object in the sequence diagram has an object lifeline: a dashed
vertical line showing the time period during which the object exists. Most objects
will exist during the whole operation. So they are aligned at the top of the diagram,
with their lifelines extended from the top to the bottom of the diagram. However,
objects can also be brought into life by dynamic creation create.
Last, every action is drawn with a focus of control, a tall thin rectangle, to indicate
the duration of the action. Usually the top of a rectangle is aligned with a message
that starts the action. The bottom of the rectangle indicates the completion of an
action, and may be aligned with another message, for example, a return message.
A focus of control may also provide hints as to the duration of the action: A short
focus of control usually implies a rapid completion of an action. Compared to the
369
T_field:field
stencil:differentiator
xx ()
<<create>>
time
370
:field
laplacian()
lifeline
focus of control
Figure B.10. Sequence diagram showing operations during the computation of the 2nd-order
spatial derivative for forward Euler method.
communication diagram, we can see that a sequence diagram places greater emphasis
on the time ordering of messages.
the context is denoted by the keyword context followed by the name of the element
in UML diagram. OCL expressions depend on the context of the expression and can
have many different functions; for example, the expression can be an initial value
when the context is an attribute.
371
372
Figure B.11. Field class diagram of the turbulence solver of Rouson et al. (2006).
where self refers explicitly to the contextual instance and can be omitted when the
contextual instance is obvious. In this example shown in Figure B.11, self refers to
the instance of Field class for which the operation d_dx is called. In the precondition, xor is the exclusive or operation, which evaluates to true only when exactly one
operand is true. The precondition species that the contextual instance must have
allocated values in either Fourier space or physical space but not both. The implies
operation in the postcondition indicates that the OCL expression is true when both
boolean operands before and after implies are true. If the boolean operand before implies is false, the OCL expression is always true. The postcondition states
that neither the Fourier nor physical space values of the contextual instance can be
allocated if the contextual instance is a temporary object.
Bibliography
Akin, E. (2003). Object-Oriented Programming via Fortran 90/95. Cambridge University Press,
Cambridge.
Alexander, C. (1979). The Timeless Way of Building. Oxford University Press, New York.
Alexander, C., S. Ishikawa, and M. Silverstein (1977). A Pattern Language: Towns, Buildings,
Construction, Volume 2 of Center for Environmental Structure Series. Oxford University
Press, New York.
Alexander, C., M. Silverstein, S. Angel, S. Ishikawa, and D. Abrams (1975). The Oregon
Experiment. Oxford University Press, New York.
Allan, B., R. Armstrong, D. E. Bernholdt, F. Bertrand, K. Chiu, T. L. Dahlgren, K. Damevski,
W. R. Ewasif, T. G. W. Epperly, M. Govindaraju, D. S. K. J. A. Kohl, M. Krishnan, G. Kumfert, J. W. Larson, S. Lefantzi, M. J. Lewis, A. D. Malony, L. C. McInnes, J. Nieplocha,
B. Norris, S. G. Parker, J. Ray, S. Shende, and T. L. W. andn S. Zhou (2006). A component architecture for high-performance scientic computing. Intl. J. High Perform. Comput.
Appl. 20(2), 163202.
Allan, B., B. Norris, W. R. Elwasif, R. C. Armstrong, K. S. Breuer, and R. M. Everson
(2008). Managing scientic software complexity with Bocca and CCA. Scientic Programming 16(4), 315327.
Aris, R. (1962). Vectors, Tensors, and the Basic Equations of Fluid Mechanics. Dover
Publications. Mineola, NY.
Artoli, A., A. Hoekstra, and P. Sloot (2006). Mesoscopic simulation of systolic ow in the
human abdominal aorta. Journal of Biomechanics 39, 837884.
Atlas, D., K. R. Hardy, and K. Naito (1966). Optimizing the radar detection of clean air
turnbulence, Journal of Applied Meterology 5(4), 450460.
Balay, S., K. Buschelman, V. Eijkhout, W. Gropp, D. Kaushik, M. Knepley, L. C. McInnes,
B. Smith, and H. Zhang (2007). Petsc users manual. Technical Report ANL-95/11 Rev.
2.3.3, Argonne National Laboratory.
Barker, V. A., L. S. Blackford, J. Dongarra, J. D. Croz, S. Hammarling, M. Marinova,
J. Waniewsky, and P. Yalamov (2001). LAPACK95 Users Guide. Society for Industrial
and Applied Mathematics.
Barrett, D. (1998). Polylingual systems: An approach to seamless interoperability. Ph. D. thesis,
University of Massachusetts, Amherst, MA.
Barton, J. and L. R. Nackman (1994). Scientifc and Engineering C++: An Introduction with
Advanced Techniques and Examples. Addison-Wesley, New York.
Beck, K. and W. Cunningham (1987). Using pattern languages for object-oriented programs. In OOPSLA-87 Workshop on the Specication and Design for Object-Oriented
Programming.
Benton, E. R. and G. W. Platzman (1972). A table of solutions of the one-dimensional burgers
equation. Quarterly of Applied Mathematics 30, 195212.
373
374
Bibliography
Berger, M. J. and P. Colella (1989). Local adaptive mesh renement for shock hydrodynamics.
J. Comput. Phys 82, 6484.
Bernard, P. S., J. M. Thomas, and R. A. Handler (1993). Vortex dynamics and the production
of Reynolds stress. Journal of Fluid Mechanics 253, 385419.
Bhatnagar, P., E. Gross, and M. Krook (1954). Model for collision processes in gases. Phys.
Rev. 94, 511525.
Blackford, L. S., J. Choi, A. Cleary, E. DAzevedo, J. Demmel, I. Dhillon, J. Dongarra,
S. Hammarling, G. Henry, A. Petitet, K. Stanley, D. Walker, and R. C. Whaley (1997).
ScaLAPACK Users Guide. Society for Industrial and Applied Mathematics.
Blackford, L. S., J. Demmel, J. Dongarra, I. Duff, S. Hammarling, G. Henry, M. Heroux,
L. Kaufman, A. Lumsdaine, A. Petitet, R. Pozo, K. Remington, and R. C. Whaley (2002).
An updated set of basic linear algebra subprograms (blas). ACM Trans. Math. Soft. 28(2),
135151.
Blilie, C. (2002). Patterns in scientic software. Computers in Science and Engineering 4(4),
4853.
Booch, G., J. Rumbaugh, and I. Jacobson (1999). The Unied Modeling Language User Guide.
Addison-Wesley, New York.
Boyd, J., J. Buick, J. Cosgrove, and P. Stansell (2004). Application of the lattice boltzmann
method to arterial ow simulation: Investigation of boundary conditions for complex arterial
geometries. Australasian Physical and Engineering Sciences in Medicine 27, 207212.
Bientinesi, P., Gunnels, J. A., M. E. M. E. S. Q.-O., and van de Geijn, R. A. (2005). The
science of deriving dense linear algebra algorithms. ACM Transactions on Mathematical
Software 31, 126.
Burgers, J. M. (1948). A mathematical model illustrating the theory of turbulence. Adv. Appl.
Mech. (1), 2527.
Canuto, C., M. Y. Hussaini, A. Quarteroni, and T. A. Zang (2006). Spectral Methods:
Fundamentals in Single Domains. Springer-Verlag, Berlin/Heidelberg.
Canuto, C. M. Y. Hussaini, A. Quarteroni, and T. Zang (2007). Spectral Methods. Evolution to Complex Geometries and Applications to Fluid Dynamics. Springer-Verlag,
Berlin/Heidelberg.
Chaitin, G. J. (1996). How to run algorithmic complexity theory on a computer: Studying the
limits of mathematical reasoning. Complexity 2(1), 1521.
Chapman, B., G. Jost, R. van der Pas, and D. J. Kuck (2007). Using OpenMP: Portable Shared
Memory Parallel Programming. The MIT Press, Cambridge, MA.
Chivers, I. D. and J. Sleightholme (2010). Compiler support for the Fortran 2003 and 2008
standards. ACM Fortran Forum 29(1), 2932.
Clarke, L. A. and D. S. Rosenblum (2006). A historical perspective on runtime checking in
software development. ACM SIGSOFT Software Engineering Notes 31(3), 2537.
Cole, J. D. (1951). On a quasilinear parabolic equation occuring in aerodynamics. Q. Appl.
Math. 9, 225236.
Collins, J. B. (2004). Standardizing an ontology of physics for modeling and simulation. In
Proceedings of the 2004 Fall Simulation Interoperability Workshop, Orlando, FL. Paper
04FSIW096.
Cosgrove, J., J. Buick, S. Tonge, C. Munro, C. Greated, and D. Campbell (2003). Application of the lattice boltzmann method to transition in oscillatory channel ow. Journal of
Physics 36, 26092630.
Dalhgren, T. L. (2007) Performance-Driven Interface Contract Enforcement for Scientic
Components. Ph.D. Dissertation, University of California, Davis (also Technical Report
UCRL-TH-235341, Lawrence Livermore National Laboratory).
Dahlgren, T. L., T. Epperly, G. Kumfert, and J. Leek (2009). Babel users guide. Technical
Report 230026, Lawrence Livermore National Laboratory, Livermore, CA.
Davidson, P. (2001). An Introduction to Magnetohydrodynamics. Cambridge University Press,
New York.
de Bruyn Kops, S. M. and J. J. Riley (1998). Direct numerical simulation of laboratory
experiments in isotropic turbulence. Phys. Fluids 10(9), 2527.
Bibliography
Decyk, V. K. and H. J. Gardner (2006). A factory pattern in Fortran 95, Volume 4487,
pp. 583590. Springer, Berlin/Heidelberg.
Decyk, V. K. and H. J. Gardner (2007). Object-oriented design patterns in Fortran 90/95.
Computer Physics Communications 178(8), 611620.
Decyk, V. K., C. D. Norton, and B. K. Szymanski (1997a). Expressing object-oriented concepts
in Fortran 90. ACM Fortran Forum 16(1), 1318.
Decyk, V. K., C. D. Norton, and B. K. Szymanski (1997b). How to express C++ concepts in
Fortran 90. Scientic Programming 6(4), 363390.
Decyk, V. K., C. D. Norton, and B. K. Szymanski (1998). How to support inheritance
and run-time polymorphism in Fortran 90. Computer Physics Communications 115,
917.
Deitel, P. J. (2008). C++: How to Program. Prentice Hall, Upper Saddle River, NJ.
Dijkstra, E. W. (1968). Go-to statement considered harmful. Communications of the
ACM 11(3), 147148.
Doviak, R. and D. Zrnic (1984). Doppler radar and weather observations. Academic Press,
New York.
Eichenberger, A. E., P. Wu, and K. OBrien (2004). Vectorization for simd architectures with
alignment constraints. ACM SIGPLAN Notices 39(6), 8293.
Fenton, N. E. and N. Ohlsson (2000). Quantitative analysis of faults and failures in a complex
software system. IEEE Transactions on Software Engineering 26(8), 797814.
Frisch, U., B. Hasslacher, and Y. Pomeau (1986). Lattice gas cellular automata for the navierstokes equations. Phys. Rev. Lett. 56, 15051508.
Gamma, E., R. Helm, R. Johnson, and J. Vlissides (1995). Design Patterns: Elements of
Reusable Object-Oriented Software. Addison-Wesley, New York.
Gardner, H. and G. Manduchi (2007). Design Patterns for e-Science. Springer-Verlag,
Berlin/Heidelberg.
Grant, P. W., M. Haveraaen, and M. F. Webster (2000). Coordinate free programming of
computational uid dynamics problems. Scientic Programming 8, 211230.
Gray, M. G., R. M. Roberts, and T. M. Evans (1999). Shadow-object interface between Fortran
95 and C++. Computing in Science and Engineering 1(2), 6370.
Hatton, L. (1997). The T Experiments: Errors in scientic software. IEEE Computational
Science and Engineering 4(2), 2738.
He, X. and L.-S. Luo (1997). Theory of the lattice boltzmann: from the boltzmann equation
to the lattice boltzmann equation. Phys. Rev. E 56, 68116817.
Henshaw, W. D. (2002). Overture: An object-oriented framework for overlapping grid applications. Technical Report 147889, Lawrence Livermore National Laboratory (also 32nd
AIAA Conference on Applied Aerodynamics, St. Louis, Missouri, June 2427, 2002).
Heroux, M. A., R. A. Bartlett, V. E. Howle, R. J. Hoekstra, J. J. Hu, T. G. Kolda,
R. B. Lehoucq, K. R. Long, R. P. Pawlowski, E. T. Phipps, A. G. Salinger, H. K. Thornquist,
R. S. Tuminaro, J. M. Willenbring, A. Williams, and K. S. Stanley (2005). An overview of
the trilinos project. ACM Transactions on Mathematical Software 31(3), 397423.
Hopf, E. (1950). The partial differential equation ut +uux = uxx . Commun. Pure Appl. Math.
3, 201230.
Huang, K. (1963). Statistical Mechanics, Wiley, Hoboken.
J3 Fortran Standards Technical Committee (1998). Technical report. Technical Report
ISO/IEC 15581:1998(E), International Organization for Standards/International Electrotechnical Committee, Geneva, Switzerland.
Karniadakis G. E. and Sherwin S. J. (2005). Spectral/hp Element Methods for Computational
Fluid Dynamics (Numerical Mathematics and Scientic Computation). Oxford University
Press, New York.
Kirk, S. R. and S. Jenkins (2004). Information theory-based software metrics and obfuscation.
J. Sys. Soft. 72(2), 179186.
Knuth, D. (1974). Structured programming with go to statements. Computing Surveys 6(4),
261301.
375
376
Bibliography
Koplik, J. and H. Levine (1993). Vortex reconnection in superuid helium. Physical Review
Letters 71, 13751378.
Krawezik, G., G. Allon, and F. Cappello (2002). SPMD OpenMP versus MPI on a IBM SMP
for 3 Kernels of the NAS Benchmarks, Volume 2327/2006 of Lecture Notes in Computer
Science, 515518.
Lewis, J. P. (2001). Limits to software estimation. ACM SIGSOFT Soft. Eng. Notes 26(4),
5459.
Lieber, B., A. Stancampiano, and A. Wakhloo (1997). Alteration of hemodynamics in
aneurism models by stenting: Inuence on stent porosity. Annals of Biomedical Engineering 25(3), 460469.
Linde, G. J. and M. T. Ngo (2008). WARLOC: A high-power coherent 94 GHz radar. In
Aerospace and Electronic Systems, IEEE Transactions, Milan, Italy, pp. 11021117.
Liu, Z., B. Chapman, T. Weng, and O. Hernandez (2003). Improving the Performance of
OpenMP by Array Privatization, Volume 2716/2003 of Lecture Notes in Computer Science,
244259.
Long, K. (2004). Sundance 2.0 tutorial. Technical Report 2004-4793, Sandia National
Laboratories.
Lorenz, E. N. (1963). Deterministic Nonperiodic Flow. Journal of the Atmospheric Sciences,
130141.
Lu, J., S. Wang, and S. Norville (2002). Method and apparatus for magnetically stirring a
thixotropic metal slurry. U.S. Patent No. 6,402,367 B1.
Maario, B., S. Viriato, and C. Francisco (2007). Dynamics in spectral solutions of burgers
equation. Journal of computational and applied mathematics 205(1), 296304.
Machiels, L. and M. O. Deville (1997). Fortran 90: An entry into object-oriented programming for the solution of partial differential equations. ACM Transactions on Mathematical
Software 23, 3242.
Malawski, M., K. Dawid, and V. Sunderam (2005). Mocca towards a distributed cca
framework for metacomputing. In Proceedings of the 19th IEEE International Parallel and
Distributed Processing Symposium (IPDPS05) Workshop 4 Volume 05. IEEE Computer
Society.
Manz, B., L. Gladden, and P. Warren (1999). Flow and dispersion in porous media:
Lattice-boltzmann and nmr studies. AIchE Journal of Fluid Mechanics and Transport
Phenomena 45(9), 18451854.
Markus, A. (2003). Avoiding memory leaks with derived types. ACM Fortran Forum 22(2),
16.
Markus, A. (2006). Design patterns and Fortran 90/95. ACM Fortran Forum 26(1), 1329.
Markus, A. (2008). Design patterns in Fortran 2003. ACM Fortran Forum 27(3), 215.
Martin, K. and B. Hoffman (2008). Mastering CMake 4th edition. Kitware, Inc.
Martin, R. C. (2002). Agile software development, principles, patterns and practices. Prentice
Hall, Upper Saddle River, NJ.
Mattson, T. G., B. A. Sanders, and B. L. Massingill (2005). Patterns in Parallel Programming.
Addison-Wesley, New York.
Metcalf, M., J. K. Reid, and M. Cohen (2004). fortran 95/2003 explained. Oxford University
Press, New York.
Mittal, R. (2005). Immersed boundary methods. Annual Reviews of Fluid Mechanics 37,
239261.
Moin, P. (2001). Fundamentals of Engineering Numerical Analysis. Cambridge University
Press, New York.
Morris, K. (2008). A direct numerical simulation of superuid turbulence. Ph. D. thesis, The
Graduate Center of The City University of New York.
Morris, K., R. A. Handler, and D. W. I. Rouson (2010, [in review]). Intermittency in the
turbulent ekman layer. Journal of Turbulence.
Morris, K., J. Koplik, and D. W. I. Rouson (2008). Vortex locking in direct numerical
simulations of quantum turbulence. Physical Review Letters 101, 015301.
Mulcahy, J. (1991). Pumping liquid metals. U.S. Patent No. 5011528.
Bibliography
Norton, C. D. and V. K. Decyk (2003). Modernizing Fortran 77 legacy codes. NASA Tech
Briefs 27(9), 72.
Numrich, R. W. (2005). Parallel numerical algorithms based on tensor notation and Co-Array
Frotran syntax. Parallel Computing, 31(6), 588607.
Numrich, R. W. and J. K. Reid (1998). Co-array fortran for parallel programming. ACM
Fortran Forum 17(2), 131.
Oliveira, J. N. (1997). http://www3.di.uminho.pt/ jno/html/camwfm.html.
Oliveira, S. and D. E. Stewart (2006). Writing Scientic Software: A Guide to Good Style.
Cambridge University Press, New York.
Pace, D. K. (2004). Modeling and simulation verication challenges. Technical Report 2, Johns
Hopkins APL Technical Digest.
Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery (1996). Numerical recipes
in fortran 90: The art of parallel scientic computing, volume 2. Cambridge University Press,
New York.
Press, W. H., W. T. Vetterling, B. P. Flannery, and S. A. Teukolsky (2002). Numerical Recipes
in C++: The Art of Scientic Computing, 2nd edition. Cambridge University Press, New
York.
Pressman, R. (2001). Software Engineering: A Practitioners Approach, 5th Ed. McGraw-Hill,
New York.
R. van Engelen, L. W. and G. Cats (1997). Tomorrows weather forecast: Automatic code
generation for atmospheric modeling. IEEE Computational Science and Engineering.
Rasmussen, C. E., M. J. Sottile, S. S. Shende, and A. D. Maloney (2006). Bridging the language
gap in scientic computing: the chasm approach. Concurrency and Computation: Practice
and Experience 18(2), 151162.
Reid, J. K. (2005). Co-arrays in the next fortran standard. ACM Fortran Forum 24(2), 417.
Rouson, D., K. Morris, and X. Xu (2005). Dynamic memory de-allocation in fortran 95/2003
derived type calculus. Scientic Programming 13(3), 189203.
Rouson, D., X. Xu, and K. Morris (2006). Formal constraints on memory management for
composite overloaded operations. Scientic Programming 14(1), 2740.
Rouson, D. W. I. (2008). Towards analysis-driven scientic software architecture: The case
for abstract data type calculus. Scientic Programming 16(4), 329339.
Rouson, D. W. M., H. Adalsteinsson, and J. Xia (2010). Design patterns for multiphysics
modeling in Fortran 2003 and C++. ACM Transactions on Mathematical Software 37(1),
Article 3.
Rouson, D. W. I., S. C. Kassinos, I. Moulitsas, I. Sarris, and X. Xu (2008a). Dispersed-phase
structural anisotropy in homogeneous magnetohydrodynamic turbulence at low magnetic
reynolds number. Physics of Fluids 20(025101).
Rouson, D. W. I., R. Rosenberg, X. Xu, I. Moulitsas, and S. C. Kassinos (2008b). A gridfree abstraction of the navier-stokes equation in fortran 95/2003. ACM Transactions on
Mathematical Software 34(1), Article 2.
Rouson, D. W. I. and Y. Xiong (2004). Design metrics in quantum turbulence simuluations:
How physics inuences software architecure. Scientic Programming 12(3), 185196.
Rubin, F. (1987). GOTO considered harmful considered harmful. Communications of the
ACM 30(3), 195196.
Satir, G. and D. Brown (1995). C++: The Core Language. OReilly & Associates, Inc.,
Sebastopol, CA.
Schach, S. (2002). Object-Oriented and Classical Software Engineering. Addison-Wesley, New
York.
Schwarz, K. W. (1985). Three-dimensional vortex dynamics in superuid 4 He: line-line and
line-boundary interactions. Physical Review B 31, 57825804.
Schwarz, K. W. (1988). Three dimensional vortex dynamics in superuid 4 He: homogeneous
superuid turbulence. Physical Review B 38, 23982417.
Shalloway, A. and J. R. Trott (2002). Design Patterns Explained. Addison-Wesley, New York.
Shannon, C. (1948). A mathematical theory of communication. The Bell System Technical
Journal.
377
378
Bibliography
Shepperd, M. and D. C. Ince (1994). A critique of three metrics. Journal of Systems and
Software 26, 197210.
Spalart, P. R., R. D. Moser, and M. M. Rogers (1991). Spectral methods for the navier-stokes
equations with one innite and two periodic directions. J. Comp. Phys. 96(297), 297324.
Stevens, W. P., G. J. Myers, and L. L. Constantine (1974). Structured design. IBM Systems
Journal 13(2), 115.
Stevenson, D. (1997). How goes cse? Thoughts on the IEEE cs workshop at Purdue.
Stewart, G. (2003). Memory leaks in derived types revisited. ACM Fortran Forum 22(3),
2527.
Stokes, G. (1851). On the effect of the internal friction of uids on the motion of pendulums.
Trans. Camb. Phil. Soc. 9(Part II), pp. 8106.
Strang, G. (2003). Introduction to Linear Algebra. Wellesley Cambridge Press, Wellesley,
MA.
Stucki, L. G. and G. L. Foshee (1971). New assertion concepts for self-metric software validation. In Proceedings of the International Conference on Reliable Software, pp. 5971. ACM
SIGPLAN and SIGMETRICS.
Tatarskii, V. (1961). Wave propagation in a turbulent medium. McGraw-Hill, New York.
Thomas, H. (1949). Elliptic problems in linear difference equations over a network. Technical
report, Watson Sci. Comput. Lab. Rept., Columbia University, New York.
Vandevoorde, D. and N. M. Josuttis (2003). C++ Templates, the complete guide. Addison
Wesley, New York.
Vinen, W. F. and J. J. Niemela (2002). Quantum turbulence. Journal of Low Temperature
Physics 128(56), 167231.
Wallcraft, A. J. (2000). Spmd openmp versus mpi for ocean models. Concurrency: Practice
and Experience 12, 11551164.
Wallcraft, A. J. (2002). A comparison of co-array fortran and openmp fortran for spmd
programming. The Journal of Supercomputing 22(3), 231250.
Warmer, J. and A. Kleppe (2003). The Object Constraint Language: Getting Your Models
Ready for MDA, 2nd Ed. Addison-Wesley, New York.
Won, J., S. Suh, H. Ko, K. Lee, W. Shim, B. Chang, D. Choi, S. Park, and D. Lee (2006).
Problems encountered during and after stentgraft treatment of aortic dissection. Journal of
Vascular and Interventional Radiology 17, 271281.
Xu, X. and J. Lee (2008). Application of the lattice boltzmann method to ow in aneurysm
with ring-shaped stent obstacles. Int. J. Numer. Meth. Fluids 59(6), 691710.
Zhang, K., K. Damevski, V. Venkatachalapathy, and S. G. Parker (2004). Scirun2: A cca
framework for high performance computing. In Ninth International Workshop on HighLevel Parallel Programming Models and Supportive Environments (HIPS04), pp. 7279.
Index
abstract, 34
calculus, 129, 130, 132, 137, 140, 141
class, 94
data type, 34
data type calculus, 58, 59, 62, 63, 66, 69, 70, 72,
73, 7578, 80, 99, 130, 132, 141
interface, 79
abstract calculus, 202, 236, 293, 310
abstract factory, 228, 234
abstract factory pattern, 99
abstract syntax tree, 253
actual argument, 54
adapter pattern, 99
ADT
calculus, 111
ADT calculus, 141
aggregation, 48, 57, 62, 78, 365367
allocatable, 35, 38, 41, 62
allocation
sourced, 155
ANSI C, 254
application, 96
application programming interface
(API), 295
architecture, 7
argument
actual, 41, 54, 79, 145, 202, 268, 269
dummy, 41, 50, 79, 111, 135, 268
passed-object dummy, 38, 51, 54, 135
assertion, 232, 233, 241, 242, 247
assertions, 232
assignment
dened, 55
intrinsic, 55, 58, 70, 78
association, 362
attribute, 34
auto-parallelization, 293
auto-vectorization, 293
Babel, 254
back substitution, 337, 338, 341, 343
backward difference, 349
backward Euler, 59
beginners mind, 87
binding
deferred, 95
bisection method, 71, 72
BLACS, 108
BLAS, 141
body count, 262
Boolean, 35
BOOST, 116
Builder, 227
Burgers equation, 202, 204, 310
buttery effect, 101
bzip2, 69
C++
0x standard, 99
class, 36
data members, 36
exception handling, 137
function prototype, 39
namespace, 36
Standard Template Library, 23, 36, 129
virtual member function, 36
call tree, 234
central difference, 349
chain of responsibility pattern, 99
chaos, 101
character string
deferred-length, 38
xed-width, 38
Chasm, 252254
child, 57, 78
class
interface, 95
coarray, 309
cohesion, 6971, 109
Common Component Architecture (CCA), 5, 23
compiler
Cray Fortran, 31
Gnu Fortran, 31
Intel, 31
379
380
Index
compiler (cont.)
Numerical Algorithms Group, 31
XL Fortran, 31
complexity, 67, 69, 79
algorithmic, 67, 69
computational, 69
Kolmogorov, 67, 76
theory, 71
component, 23
-based, 24
-based software engineering, 23
derived type, 29
composition, 48, 57, 365367
concrete class, 94
concurrency, 55
constructor, 32, 34, 38, 4143, 49, 50, 53, 55
generic name, 37
context, 370
copy
deep, 70
shallow, 70
coupling, 6971, 81
Cray, 31
dead-code removal, 28, 246
decorator pattern, 99
deferred binding, 79, 95
dened assignment, 55
dependency, 362
depth count, 111
derived type
components, 29
design by contract, 24
design patterns, 5
creational, 203
diagonally dominant, 339
distributed programming, 99
dynamic dispatching, 54
encapsulation, 21, 31, 36, 39, 50, 54
exception handling, 136
expression templates, 77
extended types, 22
extensibility, 45, 54, 55
Extensible Markup Language (XML), 252
facade pattern, 108
factory method, 203, 204, 234
fault, 231
nal subroutine, 109111
nite differences
central, 105
Pade, 105
Runge-Kutta, 105
attening, 255, 257
fork-join, 295
formal
methods, 231, 232
specication, 231
verication, 231
formal constraints, 24
formal methods, 24
Fortran
2008 standard, 39, 99, 155
forward difference, 348
forward elimination, 337
forward Euler, 58
forward reference, 145
framework, 96, 331
function
intrinsic, 22, 41, 53
function evaluation, 131
Gang of Four (GoF), 9396, 99, 127, 141144, 164,
202204, 226228, 240
garbage collection, 117
Gaussian elimination, 337342
generalization, 358, 362, 363
generic
constructor name, 37
interface, 41
name, 41
programming, 35
type, 32
graphical user interface, 98
GUI, 98
heat equation, 345
hierarchy
class, 54, 57
inheritance, 54
high-performance computing (HPC), 4, 107
IBM, 31
IEEE, 256
implicit Euler, 351
information
entropy, 74, 75
theory, 69, 73
information hiding, 21, 31, 36, 38, 39, 54, 57
inheritance, 22, 31, 48, 57, 62, 75, 78, 363, 364
initialization, 255, 257
inline, 253
instantiate, 34
interface, 19, 22, 32, 39, 4143, 50, 53, 57, 58, 61,
67, 73, 75, 76, 78, 79, 361, 363, 370, 371
abstract, 79, 95
blocks, 39
class, 94, 95
explicit, 39
graphical user, 98
implicit, 39
logical, 255
physical, 255
specications, 39
interface block, 262
interface body, 262
intermediate object representation (IOR), 254
Index
intrinsic assignment, 55, 58, 70, 78
intrinsic function, 22, 41, 53
intrinsic operation, 62
intrinsic type, 35
invariant, 240
iterator pattern, 127
Java, 38, 98, 99, 108, 254
Virtual Machine, 98
kernel, 245
koan, 87
LAPACK, 141
legacy
interfaces, 57
legacy Fortran, 40, 41, 45
Lorenz equations, 101
magnetohydrodynamics (MHD), 100
Mathematical Acceleration Subsystem (MASS),
293
memory leak, 41
memory management, 231
message, 34, 36, 38, 53, 62, 74
Message Passing Interface (MPI), 297
method, 21, 23, 32, 34, 35, 41, 50, 53, 54
module, 62
molecular diffusion, 100
Morfeus, 330
Morpheus, 53
multicore, 294
multiphysics, 3, 4, 6, 100, 285
multiplicity, 49, 243, 367
multithreading, 99, 293295
multphysics, 7
name mangling, 255
Newtons method, 343
object, 34
object pattern, 108, 109, 127, 237
object-based, 251, 255
object-oriented
analysis (OOA), 33, 34, 38, 40, 45
design (OOD), 5, 33, 34, 38, 40, 42, 43, 45
programming (OOP), 6, 12, 19, 2124, 26,
3134, 3638, 40, 45, 48, 50, 54, 57, 62
object-oriented analysis (OOA), 285
object-oriented design (OOD), 285
object-oriented programming (OOP), 285
observer pattern, 99
OCL (see UML), 232
opaque pointer, 257
Open Multi-Processing (OpenMP), 20, 295
operating system, 257
operation, 32, 3436, 43, 51, 53
intrinsic, 62
overloading, 256
operator, 22
operator , 22
package, 130
Pade, 226
parallel programming, 99
parent, 57, 75, 78
partial pivoting, 338, 339, 342, 343
passed-object dummy argument, 38, 51, 54, 135
pattern language, 86, 88, 141
PETSc, 141
phase space, 102
pointer
assignment, 112
polymorphism, 22, 31, 32, 45, 53, 54
dynamic, 38, 53, 54, 57
static, 53
postcondition, 240
precondition, 240
primitive type, 35
procedure
external, 39, 41
nal, 42
module, 21, 39, 53
signature, 32, 79
type-bound, 36, 38, 39, 51, 53, 55, 75
procedure pointer, 215
programming
function, 26
functional, 20, 21
generic, 23
object-based, 21, 22, 24
object-oriented, 4, 6, 22
purely functional, 21
structured, 20
unstructured, 20, 21
protocol, 54
puppeteer, 169, 202, 227
Python, 254
quantum vortices, 100
realization, 95
refactor, 40, 59
reference counting, 117
reference-counted pointer (RCP), 117, 273
reuse, 22, 38, 48, 54, 57, 58
reverse engineering, 61, 62, 69
Runge-Kutta, 131
2nd order (RK2), 144
Runge-Kutta methods, 216
scalable abstract calculus, 285
ScaLAPACK, 108, 141
scaling
strong, 292
weak, 292
381
382
Index
Scientic Interface Denition Language (SIDL),
254
scientic OOP (SOOP), 57, 62, 79, 80
scope, 62
private, 34, 39
public, 34
semidiscrete, 101, 347
sequence association, 268
shadow object, 255
shell, 245, 248, 250, 269, 270
shoshin, 87
side effects, 13, 21, 29, 238
signature, 32, 79
Simplied Wrapper and Interface Generator
(SWIG), 252
single instruction multiple data, 293
skeleton, 253
software engineering
component-based (CBSE), 23
speedup, 291
state pattern, 99
stereotype, 34
STL, 23, 36
strategy, 202
strong scaling, 292
struct ID, 269, 270, 273
structure constructor, 37, 55
structured
programming, 20, 24
stub, 253
superuidity, 100
surrogate, 202
symmetric multiprocessing (SMP), 294
Taoism, 87
template, 23, 32
class, 35, 36, 117, 121
emulation, 23
expression, 77
metaprogramming, 116
template method, 228
tensor, 331
calculus, 309
test-driven development, 43
toolkit, 96
Trilinos, 108, 141, 169
truncation error, 348
type
child, 78
derived, 21
extended, 50, 53, 54, 79
extension, 49
nalization, 111
generic, 32, 35
guard, 136
intrinsic, 35, 59, 78
parameter (UML), 35
parameterized, 32
parameterized (UML), 35
parent, 57, 75, 78
primitive, 35
selection, 135
type-bound procedure, 21
typed allocation, 215
types
extended, 22
UML, 33, 232
adorned relationships, 205
attribute, 3436
class diagram, 3436, 49, 56, 357, 359, 366
generalization, 50, 363, 364
object diagram, 49, 357, 366
OCL, 232
operation, 32, 3436, 43, 51, 53
package, 130
package diagram, 357
realization, 95
sequence diagram, 62, 66, 80, 357
use case, 33, 34, 36, 42, 357, 358
Unied Modeling Language (UML), 32
annotation, 240
weak scaling, 292
wrapping legacy Fortran, 40, 41, 45
Zen Buddhism, 87