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

218 views

Category:

Documents

6 download

Embed Size (px)

TRANSCRIPT

  • Fortran Programming Guide

    FORTRAN 77 5.0 Fortran 902.0

    901 San Antonio RoadPalo Alto, , CA 94303-4900

    USA 650 960-1300 fax 650 969-9131

    Part No: 805-4940Revision A, February 1999

  • 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, Inc.in 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

    TM

    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

    TM

    , 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

    TM

    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

    TM

    , 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.

    PleaseRecycle

  • Contents

    Preface xi

    1. Introduction 11

    Standards Conformance 11

    Features of the Fortran Compilers 12

    2. Fortran Input/Output 21

    Accessing Files From Within Fortran Programs 21

    Accessing Named Files 21

    Opening Files Without a Name 23

    Preconnected Units 23

    Opening Files Without an OPEN Statement 23

    Passing File Names to Programs 24

    f77: VAX / VMS Logical File Names 27

    Direct I/O 28

    Internal Files 29

    Tape I/O (f77 Only) 211

    Using TOPEN Routines 211

    Fortran Formatted I/O for Tape 211

    Fortran Unformatted I/O for Tape 211

    Tape File Representation 212

    Contents iii

  • End-of-File 212

    Multifile Tapes 213

    Fortran 90 I/O Considerations 213

    3. Program Development 31

    Facilitating Program Builds With the make Utility 31

    The Makefile 32

    make Command 33

    Macros 33

    Overriding of Macro Values 34

    Suffix Rules in make 34

    Version Tracking and Control With SCCS 35

    Controlling Files With SCCS 35

    Checking Files Out and In 37

    4. Libraries 41

    Understanding Libraries 41

    Specifying Linker Debugging Options 42

    Generating a Load Map 42

    Listing Other Information 43

    Consistent Compiling and Linking 44

    Setting Library Search Paths and Order 44

    Search Order for Standard Library Paths 44

    LD_LIBRARY_PATH Environment Variable 45

    Library Search Path and OrderStatic Linking 46

    Library Search Path and OrderDynamic Linking 47

    Creating Static Libraries 48

    Tradeoffs for Static Libraries 48

    Creation of a Simple Static Library 49

    Creating Dynamic Libraries 412

    iv Fortran Programming Guide Revision A, February 1999

  • Tradeoffs for Dynamic Libraries 412

    Position-Independent Code and -pic 413

    Binding Options 413

    Naming Conventions 414

    A Simple Dynamic Library 415

    Libraries Provided with Sun Fortran Compilers 416

    VMS Library 417

    POSIX Library 417

    Shippable Libraries 418

    5. Program Analysis and Debugging 51

    Global Program Checking 51

    GPC Overview 51

    How to Invoke Global Program Checking 52

    Some Examples of -Xlist and Global Program Checking 54

    Suboptions for Global Checking Across Routines 57

    -Xlist Suboption Reference 59

    Some Examples Using Suboptions 512

    Special Compiler Options 514

    Subscript Bounds (-C) 514

    f77: Undeclared Variable Types (-u) 514

    Version Checking (-V) 515

    Interactive Debugging With dbx and Sun WorkShop 515

    f77: Viewing Compiler Listing Diagnostics 516

    6. Floating-Point Arithmetic 61

    Introduction 61

    IEEE Floating-Point Arithmetic 62

    -ftrap=mode Compiler Options 63

    Floating-Point Exceptions and Fortran 63

    Contents v

  • Handling Exceptions 64

    Trapping a Floating-Point Exception 64

    SPARC: Nonstandard Arithmetic 64

    IEEE Routines 65

    Flags and ieee_flags() 66

    IEEE Extreme Value Functions 610

    Exception Handlers and ieee_handler() 611

    Retrospective Summary 615

    Debugging IEEE Exceptions 616

    Further Numerical Adventures 617

    Avoiding Simple Underflow 618

    Continuing With the Wrong Answer 619

    SPARC: Excessive Underflow 619

    7. Porting 71

    Time and Date Functions 71

    Formats 74

    Carriage-Control 74

    Working With Files 75

    Porting From Scientific Mainframes 75

    Data Representation 76

    Hollerith Data 77

    Nonstandard Coding Practices 79

    Uninitialized Variables 79

    Aliasing Across Calls 710

    Obscure Optimizations 710

    Troubleshooting 711

    Results Are Close, but Not Close Enough 712

    Program Fails Without Warning 713

    vi Fortran Programming Guide Revision A, February 1999

  • 8. Performance Profiling 81

    The time Command 81

    Multiprocessor Interpretation of time Output 82

    The gprof Profiling Command 82

    Overhead Considerations 85

    The tcov Profiling Command 85

    Old Style tcov Coverage Analysis 85

    New Style Enhanced tcov Analysis 86

    f77 I/O Profiling 87

    9. Performance and Optimization 91

    Choice of Compiler Options 91

    Performance Option Reference 92

    Other Performance Strategies 97

    Using Optimized Libraries 97

    Eliminating Performance Inhibitors 98

    Further Reading 99

    10. SPARC: Parallelization 101

    Essential Concepts 101

    SpeedupsWhat to Expect 102

    Steps to Parallelizing a Program 103

    Data Dependency Issues 103

    Parallel Options and Directives Summary 105

    Number of Processors 106

    Stacks, Stack Sizes, and Parallelization 107

    Automatic Parallelization 108

    Loop Parallelization 108

    Arrays, Scalars, and Pure Scalars 109

    Automatic Parallelization Criteria 109

    Contents vii

  • Automatic Parallelization With Reduction Operations 1010

    Explicit Parallelization 1014

    Parallelizable Loops 1014

    Sun-Style Parallelization Directives 1016

    Cray-Style Parallelization Directives 1029

    Debugging Parallelized Programs 1032

    Debugging Without dbx 1032

    Using dbx 1034

    11. C-Fortran Interface 111

    Compatibility Issues 111

    Function or Subroutine? 112

    Data Type Compatibility 112

    Case Sensitivity 116

    Underscores in Routine Names 116

    Argument-Passing by Reference or Value 117

    Argument Order 117

    Array Indexing and Order 118

    File Descriptors and stdio 118

    File Permissions 119

    Libraries and Linking With the f77 or f90 Command 1110

    Passing Data Arguments by Reference 1110

    Simple Data Types 1110

    COMPLEX Data 1111

    Character Strings 1112

    One-Dimensional Arrays 1113

    Two-Dimensional Arrays 1114

    Structures 1115

    Pointers 1118

    viii Fortran Programming Guide Revision A, February 1999

  • Passing Data Arguments by Value 1118

    Functions That Return a Value 1121

    Returning a Simple Data Type 1121

    Returning COMPLEX Data 1122

    Returning a CHARACTER String 1123

    Labeled COMMON 1126

    Sharing I/O Between Fortran and C 1127

    Alternate Returns 1127

    Index 29

    Contents ix

  • x Fortran Programming Guide Revision A, February 1999

  • Preface

    This guide combines the essential information programmers need to develop efficientapplications using the two Sun

    TM

    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.

    Discussion of the compiler command-line options and their use can be found in thecompanion book, the Fortran Users Guide.

    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.

    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

    TM

    operating environment orUNIX in general is also assumed.

    How This Book Is OrganizedThis guide is organized into the following chapters:

    Preface xi

  • Chapter 1, "Introduction," briefly describes the features of the compilers.

    Chapter 2, "Fortran Input/Output," discusses how to use I/O efficiently.

    Chapter 3, "Program Development," demonstrates how program management toolslike SCCS, make, and Teamware can be helpful.

    Chapter 4, "Libraries," explains use and creation of software libraries.

    Chapter 5, "Program Analysis and Debugging," describes use of dbx and otheranalysis tools.

    Chapter 6, "Floating Point Arithmetic," introduces important issues regardingnumerical computation accuracy.

    Chapter 7, "Porting," considers porting programs to Sun compilers.

    Chapter 8, "Performance Profiling," describes techniques for performancemeasurement.

    Chapter 9, "Performance and Optimization," indicates ways to improve executionperformance of Fortran programs.

    Chapter 10, "Parallelization," explains the multiprocessing features of thecompilers.

    Chapter 11, "C-Fortran Interface," describes how C and Fortran routines can calleach other and pass data.

    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.

    The Sun Fortran documentation covers the release of the Fortran compilers on anumber of operating environments and hardware platforms:

    FORTRAN 77 5.0 is released for:

    Solaris 2.5.1, 2.6, and Solaris 7 environments on:

    architectures based on the SPARCTM

    microprocessor

    x86-based architectures, where x86 refers to the IntelTM implementation of oneof the following: Intel 80386

    TM

    , Intel 80486TM

    , PentiumTM

    , or the equivalent

    Fortran 90 2.0 is released for:

    Solaris 2.5.1, 2.6, and Solaris 7 environments on SPARC processors only.

    xii Fortran Programming Guide Revision A, February 1999

  • 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.

    Related BooksThe following books augment this manual and provide essential information:

    Fortran Users Guideprovides information on command line options and how touse the compilers.

    FORTRAN 77 Language Reference. Complete language reference.

    Fortran Library Reference-gives details on the language and routines.

    Sun Performance WorkShop Fortran Overview gives a high-level outline of theFortran package suite.

    Other Programming Books C Users Guidedescribes compiler options, pragmas, and more.

    Numerical Computation Guidedetails floating-point computation and numericalaccuracy issues.

    Sun WorkShop Performance Library Reference-discusses the library of subroutinesand functions to perform useful operations in computational linear algebra andFourier transforms.

    Other Sun WorkShop Books Sun WorkShop Quick Install-provides installation instructions.

    Sun WorkShop Installation Reference-provides supporting installation and licensinginformation.

    Sun Visual WorkShop C++ Overview-gives a high-level outline of the C++package suite.

    Using Sun WorkShopgives information on performing development operationsthrough Sun WorkShop.

    xiii

  • Debugging a Program With dbxprovides information on using dbx commandsto debug a program.

    Analyzing Program Performanc...

Recommended

View more >