Fortran Programming Guide FORTRAN 77 5.0 — Fortran 90 isd/_course_fortran_90/_docs/Sun_C... · and…

Download Fortran Programming Guide FORTRAN 77 5.0 — Fortran 90 isd/_course_fortran_90/_docs/Sun_C... · and…

Post on 30-Jul-2018




6 download

Embed Size (px)


<ul><li><p>Fortran Programming Guide</p><p>FORTRAN 77 5.0 Fortran 902.0</p><p>901 San Antonio RoadPalo Alto, , CA 94303-4900</p><p>USA 650 960-1300 fax 650 969-9131</p><p>Part No: 805-4940Revision A, February 1999</p></li><li><p>Copyright Copyright 1999 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303-4900 U.S.A. All rights reserved.All rights reserved. This product or document is protected by copyright and distributed under licenses restricting its use, copying,distribution, and decompilation. No part of this product or document may be reproduced in any form by any means without prior writtenauthorization of Sun and its licensors, if any.Portions of this product may be derived from the UNIX system, licensed from Novell, Inc., and from the Berkeley 4.3 BSD system,licensed from the University of California. UNIX is a registered trademark in the United States and in other countries and is exclusivelylicensed by X/Open Company Ltd. Third-party software, including font technology in this product, is protected by copyright and licensedfrom Suns suppliers. RESTRICTED RIGHTS: Use, duplication, or disclosure by the U.S. Government is subject to restrictions of FAR52.227-14(g)(2)(6/87) and FAR 52.227-19(6/87), or DFAR 252.227-7015(b)(6/95) and DFAR 227.7202-3(a).Sun, Sun Microsystems, the Sun logo, SunDocs, SunExpress, Solaris, Sun Performance Library, Sun Performance WorkShop Fortran, SunVisual WorkShop C++, Sun WorkShop, and Sun WorkShop Professional are trademarks or registered trademarks of Sun Microsystems, the United States and in other countries. All SPARC trademarks are used under license and are trademarks or registered trademarks ofSPARC International, Inc. in the United States and in other countries. Products bearing SPARC trademarks are based upon an architecturedeveloped by Sun Microsystems, Inc.The OPEN LOOK and Sun</p><p>TM</p><p>Graphical User Interfaces were developed by Sun Microsystems, Inc. for its users and licensees. Sunacknowledges the pioneering efforts of Xerox Corporation in researching and developing the concept of visual or graphical user interfacesfor the computer industry. Sun holds a nonexclusive license from Xerox to the Xerox Graphical User Interface, which license also coversSuns licensees who implement OPEN LOOK GUIs and otherwise comply with Suns written license agreements.f90 is derived from Cray CF90</p><p>TM</p><p>, a product of Silicon Graphics, Inc.THIS PUBLICATION IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING,BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, ORNON-INFRINGEMENT.Copyright 1999 Sun Microsystems, Inc., 901 San Antonio Road, Palo Alto, Californie 94303-4900 U.S.A. Tous droits rservs.Ce produit ou document est protg par un copyright et distribu avec des licences qui en restreignent lutilisation, la copie et ladcompilation. Aucune partie de ce produit ou de sa documentation associe ne peut tre reproduite sous aucune forme, par quelquemoyen que ce soit, sans lautorisation pralable et crite de Sun et de ses bailleurs de licence, sil y en a.Des parties de ce produit pourront tre derives du systme UNIX licenci par Novell, Inc. et du systme Berkeley 4.3 BSD licenci parlUniversit de Californie. UNIX est une marque enregistre aux Etats-Unis et dans dautres pays, et licencie exclusivement par X/OpenCompany Ltd. Le logiciel dtenu par des tiers, et qui comprend la technologie relative aux polices de caractres, est protg par uncopyright et licenci par des fournisseurs de Sun.Sun, Sun Microsystems, le logo Sun, SunDocs, SunExpress, Solaris, Sun Performance Library, Sun Performance WorkShop Fortran, SunVisual WorkShop C++, Sun WorkShop, et Sun WorkShop Professional sont des marques dposes ou enregistres de Sun Microsystems,Inc. aux Etats-Unis et dans dautres pays. Toutes les marques SPARC, utilises sous licence, sont des marques dposes ou enregistres deSPARC International, Inc. aux Etats-Unis et dans dautres pays. Les produits portant les marques SPARC sont bass sur une architecturedveloppe par Sun Microsystems, Inc.Les utilisateurs dinterfaces graphiques OPEN LOOK et Sun</p><p>TM</p><p>ont t dvelopps de Sun Microsystems, Inc. pour ses utilisateurs etlicencis. Sun reconnat les efforts de pionniers de Xerox Corporation pour la recherche et le dveloppement du concept des interfacesdutilisation visuelle ou graphique pour lindustrie de linformatique. Sun dtient une licence non exclusive de Xerox sur linterfacedutilisation graphique, cette licence couvrant aussi les licencis de Sun qui mettent en place les utilisateurs dinterfaces graphiques OPENLOOK et qui en outre se conforment aux licences crites de Sun.f90 est deriv de CRAY CF90</p><p>TM</p><p>, un produit de Silicon Graphics, Inc.CETTE PUBLICATION EST FOURNIE "EN LETAT" SANS GARANTIE DAUCUNE SORTE, NI EXPRESSE NI IMPLICITE, Y COMPRIS,ET SANS QUE CETTE LISTE NE SOIT LIMITATIVE, DES GARANTIES CONCERNANT LA VALEUR MARCHANDE, LAPTITUDE DESPRODUITS A REPONDRE A UNE UTILISATION PARTICULIERE OU LE FAIT QUILS NE SOIENT PAS CONTREFAISANTS DEPRODUITS DE TIERS.</p><p>PleaseRecycle</p></li><li><p>Contents</p><p>Preface xi</p><p>1. Introduction 11</p><p>Standards Conformance 11</p><p>Features of the Fortran Compilers 12</p><p>2. Fortran Input/Output 21</p><p>Accessing Files From Within Fortran Programs 21</p><p>Accessing Named Files 21</p><p>Opening Files Without a Name 23</p><p>Preconnected Units 23</p><p>Opening Files Without an OPEN Statement 23</p><p>Passing File Names to Programs 24</p><p>f77: VAX / VMS Logical File Names 27</p><p>Direct I/O 28</p><p>Internal Files 29</p><p>Tape I/O (f77 Only) 211</p><p>Using TOPEN Routines 211</p><p>Fortran Formatted I/O for Tape 211</p><p>Fortran Unformatted I/O for Tape 211</p><p>Tape File Representation 212</p><p>Contents iii</p></li><li><p>End-of-File 212</p><p>Multifile Tapes 213</p><p>Fortran 90 I/O Considerations 213</p><p>3. Program Development 31</p><p>Facilitating Program Builds With the make Utility 31</p><p>The Makefile 32</p><p>make Command 33</p><p>Macros 33</p><p>Overriding of Macro Values 34</p><p>Suffix Rules in make 34</p><p>Version Tracking and Control With SCCS 35</p><p>Controlling Files With SCCS 35</p><p>Checking Files Out and In 37</p><p>4. Libraries 41</p><p>Understanding Libraries 41</p><p>Specifying Linker Debugging Options 42</p><p>Generating a Load Map 42</p><p>Listing Other Information 43</p><p>Consistent Compiling and Linking 44</p><p>Setting Library Search Paths and Order 44</p><p>Search Order for Standard Library Paths 44</p><p>LD_LIBRARY_PATH Environment Variable 45</p><p>Library Search Path and OrderStatic Linking 46</p><p>Library Search Path and OrderDynamic Linking 47</p><p>Creating Static Libraries 48</p><p>Tradeoffs for Static Libraries 48</p><p>Creation of a Simple Static Library 49</p><p>Creating Dynamic Libraries 412</p><p>iv Fortran Programming Guide Revision A, February 1999</p></li><li><p>Tradeoffs for Dynamic Libraries 412</p><p>Position-Independent Code and -pic 413</p><p>Binding Options 413</p><p>Naming Conventions 414</p><p>A Simple Dynamic Library 415</p><p>Libraries Provided with Sun Fortran Compilers 416</p><p>VMS Library 417</p><p>POSIX Library 417</p><p>Shippable Libraries 418</p><p>5. Program Analysis and Debugging 51</p><p>Global Program Checking 51</p><p>GPC Overview 51</p><p>How to Invoke Global Program Checking 52</p><p>Some Examples of -Xlist and Global Program Checking 54</p><p>Suboptions for Global Checking Across Routines 57</p><p>-Xlist Suboption Reference 59</p><p>Some Examples Using Suboptions 512</p><p>Special Compiler Options 514</p><p>Subscript Bounds (-C) 514</p><p>f77: Undeclared Variable Types (-u) 514</p><p>Version Checking (-V) 515</p><p>Interactive Debugging With dbx and Sun WorkShop 515</p><p>f77: Viewing Compiler Listing Diagnostics 516</p><p>6. Floating-Point Arithmetic 61</p><p>Introduction 61</p><p>IEEE Floating-Point Arithmetic 62</p><p>-ftrap=mode Compiler Options 63</p><p>Floating-Point Exceptions and Fortran 63</p><p>Contents v</p></li><li><p>Handling Exceptions 64</p><p>Trapping a Floating-Point Exception 64</p><p>SPARC: Nonstandard Arithmetic 64</p><p>IEEE Routines 65</p><p>Flags and ieee_flags() 66</p><p>IEEE Extreme Value Functions 610</p><p>Exception Handlers and ieee_handler() 611</p><p>Retrospective Summary 615</p><p>Debugging IEEE Exceptions 616</p><p>Further Numerical Adventures 617</p><p>Avoiding Simple Underflow 618</p><p>Continuing With the Wrong Answer 619</p><p>SPARC: Excessive Underflow 619</p><p>7. Porting 71</p><p>Time and Date Functions 71</p><p>Formats 74</p><p>Carriage-Control 74</p><p>Working With Files 75</p><p>Porting From Scientific Mainframes 75</p><p>Data Representation 76</p><p>Hollerith Data 77</p><p>Nonstandard Coding Practices 79</p><p>Uninitialized Variables 79</p><p>Aliasing Across Calls 710</p><p>Obscure Optimizations 710</p><p>Troubleshooting 711</p><p>Results Are Close, but Not Close Enough 712</p><p>Program Fails Without Warning 713</p><p>vi Fortran Programming Guide Revision A, February 1999</p></li><li><p>8. Performance Profiling 81</p><p>The time Command 81</p><p>Multiprocessor Interpretation of time Output 82</p><p>The gprof Profiling Command 82</p><p>Overhead Considerations 85</p><p>The tcov Profiling Command 85</p><p>Old Style tcov Coverage Analysis 85</p><p>New Style Enhanced tcov Analysis 86</p><p>f77 I/O Profiling 87</p><p>9. Performance and Optimization 91</p><p>Choice of Compiler Options 91</p><p>Performance Option Reference 92</p><p>Other Performance Strategies 97</p><p>Using Optimized Libraries 97</p><p>Eliminating Performance Inhibitors 98</p><p>Further Reading 99</p><p>10. SPARC: Parallelization 101</p><p>Essential Concepts 101</p><p>SpeedupsWhat to Expect 102</p><p>Steps to Parallelizing a Program 103</p><p>Data Dependency Issues 103</p><p>Parallel Options and Directives Summary 105</p><p>Number of Processors 106</p><p>Stacks, Stack Sizes, and Parallelization 107</p><p>Automatic Parallelization 108</p><p>Loop Parallelization 108</p><p>Arrays, Scalars, and Pure Scalars 109</p><p>Automatic Parallelization Criteria 109</p><p>Contents vii</p></li><li><p>Automatic Parallelization With Reduction Operations 1010</p><p>Explicit Parallelization 1014</p><p>Parallelizable Loops 1014</p><p>Sun-Style Parallelization Directives 1016</p><p>Cray-Style Parallelization Directives 1029</p><p>Debugging Parallelized Programs 1032</p><p>Debugging Without dbx 1032</p><p>Using dbx 1034</p><p>11. C-Fortran Interface 111</p><p>Compatibility Issues 111</p><p>Function or Subroutine? 112</p><p>Data Type Compatibility 112</p><p>Case Sensitivity 116</p><p>Underscores in Routine Names 116</p><p>Argument-Passing by Reference or Value 117</p><p>Argument Order 117</p><p>Array Indexing and Order 118</p><p>File Descriptors and stdio 118</p><p>File Permissions 119</p><p>Libraries and Linking With the f77 or f90 Command 1110</p><p>Passing Data Arguments by Reference 1110</p><p>Simple Data Types 1110</p><p>COMPLEX Data 1111</p><p>Character Strings 1112</p><p>One-Dimensional Arrays 1113</p><p>Two-Dimensional Arrays 1114</p><p>Structures 1115</p><p>Pointers 1118</p><p>viii Fortran Programming Guide Revision A, February 1999</p></li><li><p>Passing Data Arguments by Value 1118</p><p>Functions That Return a Value 1121</p><p>Returning a Simple Data Type 1121</p><p>Returning COMPLEX Data 1122</p><p>Returning a CHARACTER String 1123</p><p>Labeled COMMON 1126</p><p>Sharing I/O Between Fortran and C 1127</p><p>Alternate Returns 1127</p><p>Index 29</p><p>Contents ix</p></li><li><p>x Fortran Programming Guide Revision A, February 1999</p></li><li><p>Preface</p><p>This guide combines the essential information programmers need to develop efficientapplications using the two Sun</p><p>TM</p><p>Fortran compilers, f77 (FORTRAN 77 version 5.0)and f90 (Fortran 90 version 2.0). It presents issues relating to input/output, programdevelopment, use and creation of software libraries, program analysis anddebugging, numerical accuracy, porting, performance, optimization, parallelization,and the C/Fortran interface.</p><p>Discussion of the compiler command-line options and their use can be found in thecompanion book, the Fortran Users Guide.</p><p>Note - This guide covers the Sun FORTRAN 77 and Fortran 90 compilers. The textuses "f77/f90" and Fortran to indicate information that is common to bothcompilers.</p><p>Who Should Use This BookThis guide is intended for scientists, engineers, and programmers who have aworking knowledge of the Fortran language and wish to learn how to use the SunFortran compilers effectively. Familiarity with the Solaris</p><p>TM</p><p>operating environment orUNIX in general is also assumed.</p><p>How This Book Is OrganizedThis guide is organized into the following chapters:</p><p>Preface xi</p></li><li><p> Chapter 1, "Introduction," briefly describes the features of the compilers.</p><p> Chapter 2, "Fortran Input/Output," discusses how to use I/O efficiently.</p><p> Chapter 3, "Program Development," demonstrates how program management toolslike SCCS, make, and Teamware can be helpful.</p><p> Chapter 4, "Libraries," explains use and creation of software libraries.</p><p> Chapter 5, "Program Analysis and Debugging," describes use of dbx and otheranalysis tools.</p><p> Chapter 6, "Floating Point Arithmetic," introduces important issues regardingnumerical computation accuracy.</p><p> Chapter 7, "Porting," considers porting programs to Sun compilers.</p><p> Chapter 8, "Performance Profiling," describes techniques for performancemeasurement.</p><p> Chapter 9, "Performance and Optimization," indicates ways to improve executionperformance of Fortran programs.</p><p> Chapter 10, "Parallelization," explains the multiprocessing features of thecompilers.</p><p> Chapter 11, "C-Fortran Interface," describes how C and Fortran routines can calleach other and pass data.</p><p>Multiplatform ReleaseNote - The name of the latest Solaris operating environment release is Solaris 7 butsome documentation and path or package path names may still use Solaris 2.7 orSunOS 5.7.</p><p>The Sun Fortran documentation covers the release of the Fortran compilers on anumber of operating environments and hardware platforms:</p><p>FORTRAN 77 5.0 is released for:</p><p> Solaris 2.5.1, 2.6, and Solaris 7 environments on:</p><p> architectures based on the SPARCTM</p><p>microprocessor</p><p> x86-based architectures, where x86 refers to the IntelTM implementation of oneof the following: Intel 80386</p><p>TM</p><p>, Intel 80486TM</p><p>, PentiumTM</p><p>, or the equivalent</p><p>Fortran 90 2.0 is released for:</p><p> Solaris 2.5.1, 2.6, and Solaris 7 environments on SPARC processors only.</p><p>xii Fortran Programming Guide Revision A, February 1999</p></li><li><p>Note - The term x86 refers to the Intel 8086 family of microprocessor chips,including the Pentium, Pentium Pro, and Pentium II processors and compatiblemicroprocessor chips made by AMD and Cyrix. In this document, the term x86refers to the overall platform architecture. Features described in this book that areparticular to a specific platform are differentiated by the terms SPARC andx86 in the text.</p><p>Related BooksThe following books augment this manual and provide essential information:</p><p> Fortran Users Guideprovides information on command line options and how touse the compilers.</p><p> FORTRAN 77 Language Reference. Complete language reference.</p><p> Fortran Library Reference-gives details on the language and routines.</p><p> Sun Performance WorkShop Fortran Overview gives a high-level outline of theFortran package suite.</p><p>Other Programming Books C Users Guidedescribes compiler options, pragmas, and more.</p><p> Numerical Computation Guidedetails floating-point computation and numericalaccuracy issues.</p><p> Sun WorkShop Performance Library Reference-discusses the library of subroutinesand functions to perform useful operations in computational linear algebra andFourier transforms.</p><p>Other Sun WorkShop Books Sun WorkShop Quick Install-provides installation instructions.</p><p> Sun WorkShop Installation Reference-provides supporting installation and licensinginformation.</p><p> Sun Visual WorkShop C++ Overview-gives a high-level outline of the C++package suite.</p><p> Using Sun WorkShopgives information on performing development operationsthrough Sun WorkShop.</p><p>xiii</p></li><li><p> Debugging a Program With dbxprovides information on using dbx commandsto debug a program.</p><p> Analyzing Program Performanc...</p></li></ul>