Professional Documents
Culture Documents
(Download PDF) Modern Fortran Building Efficient Parallel Applications 1St Edition Milan Curcic Online Ebook All Chapter PDF
(Download PDF) Modern Fortran Building Efficient Parallel Applications 1St Edition Milan Curcic Online Ebook All Chapter PDF
https://textbookfull.com/product/modern-fortran-building-
efficient-parallel-applications-1st-edition-milan-curcic-2/
https://textbookfull.com/product/fortran-2018-with-parallel-
programming-1st-edition-subrata-ray-author/
https://textbookfull.com/product/modern-fortran-explained-
incorporating-fortran-2018-michael-metcalf/
https://textbookfull.com/product/modern-fortran-explained-
michael-metcalf/
Laser Additive Manufacturing. Materials, Design,
Technologies, and Applications 1st Edition Milan Brandt
https://textbookfull.com/product/laser-additive-manufacturing-
materials-design-technologies-and-applications-1st-edition-milan-
brandt/
https://textbookfull.com/product/pro-tbb-c-parallel-programming-
with-threading-building-blocks-1st-edition-michael-voss/
https://textbookfull.com/product/postgresql-query-optimization-
the-ultimate-guide-to-building-efficient-queries-1st-edition-
henrietta-dombrovskaya/
https://textbookfull.com/product/building-serverless-
architectures-learn-the-intricacies-and-nuances-of-building-
efficient-serverless-architectures-first-published-edition-
gurturk/
https://textbookfull.com/product/parallel-programming-for-modern-
high-performance-computing-systems-czarnul/
Building efficient parallel applications
Milan Curcic
Foreword by Damian Rouson
MANNING
program foo
implicit none
The main program is the integer :: a, b, n The main program can declare
fundamental Fortran unit. real :: x data, have executable code,
It is the only unit that can as well as invoke other units,
n = 1, 10
be invoked as an executable such as functions and
do n = 1, 10
from the operating system. call add(a, n)
subroutines.
end do
end program foo
Like programs, functions function sum(a, b) Functions can take any number
can also declare data and of input arguments, but always
have executable code. integer, intent(in) :: a, b return only one result.
Unlike programs, they integer :: sum
can only be invoked in
expressions; for example, sum = a + b Use functions for side
total = 2 * sum(3, 5). effect-free calculations.
end function sum
MILAN CURCIC
FOREWORD BY DAMIAN ROUSON
MANNING
SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning Publications
was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
ISBN: 9781617295287
Printed in the United States of America
contents
foreword xi
preface xiii
acknowledgments xiv
about this book xvi
about the author xxii
about the cover illustration xxiii
1 Introducing Fortran 3
1.1 What is Fortran? 4
1.2 Fortran features 6
1.3 Why learn Fortran? 8
1.4 Advantages and disadvantages 10
Side-by-side comparison with Python 10
1.5 Parallel Fortran, illustrated 12
1.6 What will you learn in this book? 13
1.7 Think parallel! 14
Copying an array from one processor to another 17
iii
iv CONTENTS
all together 47
2.4 Going forward with the tsunami simulator 51
2.5 Answer key 52
Exercise: Cold front propagation 52
2.6 New Fortran elements, at a glance 52
2.7 Further reading 52
Using portable
data types 89
4.2 Creating your first module 91
The structure of a custom module 92 Defining a module 93
■
simulator 98
4.3 Toward realistic wave simulations 99
A brief look at the physics 101 Updating the finite difference
■
code 157
6.3 Writing to files on disk: A minimal note-taking app 157
Opening a file and writing to it 158 Opening a file 159
■
a file exists 164 Error handling and closing the file 167
■
image 186
7.4 Coarrays and synchronization, explained 187
Declaring coarrays 188 Allocating dynamic coarrays 188
■
constructor 215 Custom type constructor for the Field type 218
■
exercise 242
9.2 Type systems and generic procedures 242
Static versus strong typing 242
9.3 Writing your first generic procedure 243
The problem with strong typing 243 Writing the specific
■
operators 258
9.5 Generic procedures and operators in the tsunami
simulator 259
Writing user-defined operators for the Field type 259
9.6 Answer key 260
Exercise 1: Specific average function for a derived type 260
Exercise 2: Defining a new string concatenation operator 262
9.7 New Fortran elements, at a glance 263
assignment 286
10.5 Answer key 288
Exercise 1: Validating user input 288 Exercise 2: Leap year in
■
xi
xii FOREWORD
—DAMIAN ROUSON,
PhD, P.E. President, Sourcery Institute, Oakland, California, USA
preface
When Mike Stephens from Manning first reached out to me in the summer of 2017,
he wrote, “We saw some of your forum posts and GitHub repositories; would you con-
sider writing a Fortran book with Manning?” Writing a book had never crossed my
mind, nor did I believe I was cut out for the job. I closed my eyes and took a leap of
faith. “Of course, I’d love to! Where do I send a proposal?” By the end of the summer,
we had a contract and a tentative table of contents in place. Two development editors,
two technical editors, four peer reviews, three chapter rewrites, two hurricanes, and
almost three years later, we have the finished book.
Welcome to Modern Fortran: Building Efficient Parallel Applications! If you’re holding
this book, chances are you either want to learn Fortran programming for school or
work, or you’re an experienced Fortran programmer looking to brush up on the latest
developments in the language. Either way, you’ve come to the right place. If you’re
just starting to learn, my goal with this book is to give you a straightforward, hands-on,
practical approach to Fortran programming. If you have prior experience with the
language, I want this to be a handy survival guide in the Fortran world. Forgot how to
write functions that operate on both scalars and arrays? Wondering how to write your
program for parallel execution? Practical projects and exercises with solutions are
here to show you how.
I’m happy to have the opportunity to share with you what I’ve learned over the
past 14 years. Thank you in advance for trusting me with your time and money.
Modern Fortran is my way of giving back to dozens of you who taught me and helped
me along the way. I hope you use this book to teach the next generation of Fortran
programmers.
xiii
acknowledgments
It takes a village to make a great book. Mike Stephens was the acquisitions editor—he
brought me on board and helped work out the table of contents, as well as getting clear
on who this book is for. My development editors, Kristen Watterson and Lesley Trites,
guided me along the way and diligently pushed me forward. Kristen worked with me on
the initial drafts of nine of the chapters; then Lesley took over for the remainder and
putting it all together. Technical editors Michiel Trimpe and Alain Couniot made sure
to point out any mistakes in the code and confusing paragraphs that didn’t make sense.
Bert Bates chimed in on occasion to help me pull out the concrete from the abstract.
Maurizio Tomasi was the technical proofreader and made sure that all the code in the
book works as advertised. Melody Dolab was the final proofreader and Lori Weidert was
the production editor. Also, the rest of the Manning staff who worked with me: Candace
Gillhoolley, Ana Romac, Rejhana Markanovic, Aleksandar Dragosavljević, Matko Hrvatin,
and others. Thank you all—I’ve learned a lot from you.
I also want to thank all of the reviewers: Anders Johansson, Anton Menshov,
Bridger Howell, David Celements, Davide Cadamuro, Fredric Ragnar, Jan Pieter
Herweijer, Jose San Leandro, Joseph Ian Walker, Kanak Kshetri, Ken W. Alger, Konrad
Hinsen, Kyle Mandli, Leonardo Costa Prauchner, Lottie Greenwood, Luis Moux-
Domínguez, Marcio Nicolau, Martin Beer, Matthew Emmett, Maurizio Tomasi,
Michael Jensen, Michal Konrad Owsiak, Mikkel Arentoft, Ondřej Čertík, Patrick
Seewald, Richard Fieldsend, Ryan B. Harvey, Srdjan Santic, Stefano Borini, Tiziano
Müller, Tom Gueth, Valmiky Arquissandas, and Vincent Zaballa. Your suggestions
helped make this a better book.
xiv
ACKNOWLEDGMENTS xv
Arjen Markus provided thorough reviews and suggestions on every chapter as they
became available in the Manning Early Access Program. Izaak Beekman, Jacob Williams,
Marcus Van Lier-Walqui, and Steve Lionel provided helpful comments on early drafts
of the book. Damian Rouson and his own books were an inspiration, and he encour-
aged me further along the way. Michael Hirsch helped with continuous integration of
some of the GitHub repositories associated with the book. Finally, all my readers who
trusted me and bought the book while still in the works—you helped me to keep at it
and finish the job.
Last but not least, to my wife, family, and friends who supported me and were
proud of me—I couldn’t have done it without your love and help.
Introducing Fortran
This is a book about Fortran, one of the first high-level programming languages
in history. It will teach you the language by guiding you step-by-step through
the development of a fully featured, parallel physics simulation app. Notice the
emphasis on parallel. Parallel programming allows you to break your problem down
into pieces and let multiple processors each work on only part of the problem,
thus reaching the solution in less time. By the end, you’ll be able to recognize
problems that can be parallelized, and use modern Fortran techniques to solve
them.
This book is not a comprehensive reference manual for every Fortran feature—
I’ve omitted significant parts of the language on purpose. Instead, I focus on the
most practical features that you’d use to build a real-world Fortran application. As
we work on our app chapter by chapter, we’ll apply modern Fortran features and
software design techniques to make our app robust, portable, and easy to use and
3
Another random document with
no related content on Scribd:
GEORGIA: Mr. Allan Sweat, Treasurer, Savannah, Ga.
HAWAII: Mrs. W. W. Hall, Secretary, Honolulu, Hawaii.
ILLINOIS: Mr. Chas. H. Ravell, Secretary, 135 Adams Street
Chicago, Ill.
INDIANA: Mr. Rowland Evans, Secretary, Indianapolis, Ind.
IOWA: Mr. Charles Hutchinson, Secretary, 916 Fleming Building,
Des Moines, Iowa.
KANSAS: Mrs. B. B. Smyth, Secretary, Room 8, 4th floor, State
House, Topeka, Kan.
MAINE: Capt. F. J. Morrow, U. S. A., 478½ Congress Street,
Portland, Me.
MARYLAND: Mr. George Norbury Mackenzie, Secretary, 1243
Calvert Building, Baltimore, Md.
MASSACHUSETTS: Miss Katharine P. Loring, Secretary, Prides
Crossing, Mass.
MICHIGAN: Mr. Ralph M. Dyar, Secretary, 818 Penobscot Building,
Detroit, Mich.
MINNESOTA: Mr. Edward C. Stringer, Secretary, St. Paul, Minn.
MISSOURI: Mr. Leighton Shields, Secretary, 1200 Third National
Bank Building, St Louis, Mo.
NEW HAMPSHIRE: Address of Branch, Mr. Wm. F. Thayer, First
National Bank, Concord, N. H.
NEW JERSEY: Mr. W. E. Speakman, Secretary, Woodbury, N. J.
NEW YORK: Mrs. William K. Draper, Secretary, 500 Fifth Avenue,
New York City.
NORTH CAROLINA: Mrs. Theodore F. Davidson, Secretary,
Asheville, N. C.
OHIO: Mr. R. Grosvenor Hutchins, Secretary, Columbus, O.
OKLAHOMA: Dr. Fred. S. Clinton, Secretary, Tulsa, Okla.
PENNSYLVANIA: Mr. Joseph Allison Steinmetz, Secretary,
Independence Hall Building, Philadelphia, Pa.
PHILIPPINE ISLANDS: Mrs. Victorino Mapa, Secretary, Manila, P. I.
PORTO RICO: Miss Josefina Noble, Secretary, No. 9 Tetuan Street,
San Juan, P. R.
RHODE ISLAND: Professor George Grafton Wilson, Secretary, care
Brown University, Providence, R. I.
SOUTH CAROLINA: Mr. A. W. Litschgi, Secretary, 187 King Street,
Charleston, S. C.
TEXAS: Mr. Raymond D. Allen, Secretary, 483 Bryan Street, Dallas,
Texas.
VERMONT: Mr. Chas. S. Forbes, Secretary, St. Albans, Vt.
WASHINGTON: Rev. M. A. Matthews, Seattle, Wash.
WEST VIRGINIA: Miss Elizabeth Van Rensselaer, Secretary,
Berkeley Springs, W. Va.
WYOMING: Mr. Chas. F. Mallin, Secretary, Cheyenne, Wyo.
Chinosol
(Pronounced Kinnosol)
THE ONLY
NON-POISONOUS, NON-IRRITATING, NON-ALCOHOLIC
ANTISEPTIC
WHICH IS
“MUCH STRONGER THAN CARBOLIC ACID AND AT LEAST THE
EQUAL OF BICHLORIDE OF MERCURY.”
(Extract from Bacteriological Report from the Lederle Laboratories.)
A THOROUGHLY RELIABLE DISINFECTANT
SAFE EVEN IN THE HANDS OF CHILDREN.
There is no longer any excuse for running the risk of poisoning by
carbolic acid or corrosive sublimate.
PARTIAL LIST OF AUTHORITIES.
Chinosol
DOES NOT INJURE MEMBRANES.
FREE FROM DISAGREEABLE ODOR.
Every physician approves of the prompt application of a proper
antiseptic to a bruise, cut, wound or burn, thus insuring surgical
cleanliness until he can reach the patient.
CHINOSOL IS PRESENTED IN TABLET FORM. ONE TABLET TO
ONE QUART OF WATER PRODUCES SOLUTION OF PROPER
STRENGTH.
WE ARE INTRODUCING CHINOSOL THROUGH THE DRUG
TRADE OF AMERICA. IF YOU CANNOT OBTAIN IT, REMIT TO US,
IN POSTAGE, AND WE WILL SEND YOU SUFFICIENT CHINOSOL
TO MAKE
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must, at
no additional cost, fee or expense to the user, provide a copy, a
means of exporting a copy, or a means of obtaining a copy upon
request, of the work in its original “Plain Vanilla ASCII” or other
form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using
the method you already use to calculate your applicable
taxes. The fee is owed to the owner of the Project
Gutenberg™ trademark, but he has agreed to donate
royalties under this paragraph to the Project Gutenberg
Literary Archive Foundation. Royalty payments must be
paid within 60 days following each date on which you
prepare (or are legally required to prepare) your periodic tax
returns. Royalty payments should be clearly marked as
such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4,
“Information about donations to the Project Gutenberg
Literary Archive Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.