Modern Fortran Style and Usage
Total Page:16
File Type:pdf, Size:1020Kb
Modern Fortran Style and Usage Fortran is one of the oldest high-level languages and remains the premier language for writing code for science and engineering applications. Modern Fortran: Style and Usage is a book for anyone who uses Fortran, from the novice learner to the advanced expert. It describes best practices for programmers, scientists, engineers, computer scientists, and researchers who want to apply good style and incorporate rigorous usage in their own Fortran code or to establish guidelines for a team project. The presentation concentrates primarily on the characteristics of Fortran 2003, while also describing methods in Fortran 90/95 and valuable new features in Fortran 2008. The authors draw on more than a half century of experience writing produc- tion Fortran code to present clear succinct guidelines on formatting, naming, documenting, programming, and packaging conventions and various pro- gramming paradigms such as parallel processing (including OpenMP, MPI, and coarrays), OOP, generic programming, and C language interoperability. Programmers working with legacy code will especially appreciate the section on updating old programs. Norman S. Clerman is currently a private consultant. He was formerly the Chief Computer Scientist at Opcon Design Associates, LLC, a small company engaged in lens design. Walter Spector has been employed by Silicon Graphics International’s Profes- sional Services organization (formerly Cray Research Inc.) since 1984. http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 Reproduced with permission of IBM Corporate Archives http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 Modern Fortran Style and Usage Norman S. Clerman Walter Spector Silicon Graphics, Inc., Fremont, California http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 cambridge university press Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, Sao˜ Paulo, Delhi, Tokyo, Mexico City Cambridge University Press 32 Avenue of the Americas, New York, NY 10013-2473, USA www.cambridge.org Information on this title: www.cambridge.org/9780521730525 c Norman S. Clerman and Walter Spector 2012 This publication is in copyright. Subject to statutory exception and to the provisions of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published 2012 Printed in the United States of America A catalog record for this publication is available from the British Library. Library of Congress Cataloging in Publication data Clerman, Norman S., 1946– Modern Fortran : style and usage / Norman S. Clerman, Walter Spector. p. cm. Includes bibliographical references and index. ISBN 978-0-521-51453-8 (hardback) 1. FORTRAN (Computer program language) I. Spector, Walter, 1956– II. Title. QA76.73.F25C56 2012 005.133 – dc23 2011026625 ISBN 978-0-521-51453-8 Hardback ISBN 978-0-521-73052-5 Paperback Cambridge University Press has no responsibility for the persistence or accuracy of URLs for external or third-party Internet Web sites referred to in this publication and does not guarantee that any content on such Web sites is, or will remain, accurate or appropriate. http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 To Miriam for all her love and endless encouragement. Norman S. Clerman To Irene, Nyssa, Simon, and Sammy with love. Walter Spector http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 Contents SourceCodeListings..............................................page xi Preface.............................................................xiii Audience........................................................xiii GeneralConsiderations............................................xiii Organization.....................................................xiv 1. Introduction .................................................. 1 1.1 Acknowledgments............................................1 1.2 TypographicalConventions....................................1 1.3 SourceCodeListings..........................................2 2. General Principles........................................... 3 3. Formatting Conventions .................................... 9 3.1 SourceForm.................................................9 3.2 Case.......................................................10 3.3 Indentation.................................................13 3.4 WhiteSpace................................................17 3.5 StatementsandExpressions...................................20 3.6 SymbolsandNumbers.......................................22 4. Naming Conventions ...................................... 24 4.1 Modules,Procedures,andTypes...............................24 4.2 SupplementalNotation.......................................27 4.3 DataObjects................................................30 4.4 ConstructBlocks............................................34 5. Documentation Conventions ............................. 38 6. Programming Principles ................................... 44 6.1 StandardsConformance......................................44 6.2 ModuleDesign..............................................44 6.3 ProcedureArguments........................................50 6.4 ArgumentVerification........................................82 6.5 RecursiveProcedureDesign...................................86 6.6 State,SideEffects,andThreadSafety...........................90 vii http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 viii CONTENTS 7. Programming Conventions................................ 98 7.1 DeclarationandInitializationofDataObjects...................98 7.2 AllocatableandPointerAttributes............................102 7.3 DynamicDataAttributes....................................128 7.4 ControlFlow..............................................133 7.5 CharacterStringExpressions.................................138 8. Input and Output.......................................... 140 8.1 GeneralI/OOperations.....................................140 8.2 Formatted,Unformatted,andStreamI/O......................155 8.3 Messages..................................................163 9. Packaging Conventions.................................. 165 9.1 Files......................................................165 9.2 ExternalProcedures.........................................165 9.3 Submodules...............................................167 10. Generic Programming .................................... 171 10.1 GenericCodeTechniques..................................172 10.2 PreprocessorReplication....................................179 11. Object Orientation ........................................ 183 11.1 TypeandClassDesign.....................................184 11.2 Creation, Construction, Initialization, and Assignment .........189 11.3 Type-Bound Procedures and Procedure Pointer Components . 197 11.4 Inheritance...............................................208 11.5 Polymorphism............................................212 12. Parallel Processing ....................................... 216 12.1 PreliminaryConsiderations.................................217 12.2 ParallelProgramPatterns...................................220 12.3 Fortran 2008 Parallel Processing . .........................242 13. Numerical Types, Kinds, and Operations ............... 247 13.1 TheConceptofKIND.....................................247 13.2 Floating-PointOperations..................................249 13.3 Floating-PointExceptions..................................253 13.4 BitManipulation..........................................258 14. C Interoperability ......................................... 262 15. Updating Old Programs .................................. 271 15.1 Statements................................................271 15.2 Techniques...............................................282 15.3 NonstandardFeatures......................................284 http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 CONTENTS ix A. Source Code ........................................... 289 A.1 Type bound demo.........................................289 A.2 Unlimited demo...........................................295 B. Rule List ................................................. 308 Bibliography . .......................................................321 Index..............................................................327 http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 http://ebooks.cambridge.org/ebook.jsf?bid=CBO9781139027687 Cambridge Books Online © Cambridge University Press, 2012 Source Code Listings 6.1 Program Intent exp imp ........................................50 6.2 Program Intent out test .......................................57 6.3 Program Point intent in out ...................................67 6.4 Program Arg par test ..........................................83 7.1 Program Return string ........................................111