1\input texinfo @c -*-texinfo-*- 2@c %**start of header 3@setfilename gfortran.info 4@set copyrights-gfortran 1999-2022 5 6@include gcc-common.texi 7 8@settitle The GNU Fortran Compiler 9 10@c Create a separate index for command line options 11@defcodeindex op 12@c Merge the standard indexes into a single one. 13@syncodeindex fn cp 14@syncodeindex vr cp 15@syncodeindex ky cp 16@syncodeindex pg cp 17@syncodeindex tp cp 18 19@c TODO: The following "Part" definitions are included here temporarily 20@c until they are incorporated into the official Texinfo distribution. 21@c They borrow heavily from Texinfo's \unnchapentry definitions. 22 23@tex 24\gdef\part#1#2{% 25 \pchapsepmacro 26 \gdef\thischapter{} 27 \begingroup 28 \vglue\titlepagetopglue 29 \titlefonts \rm 30 \leftline{Part #1:@* #2} 31 \vskip4pt \hrule height 4pt width \hsize \vskip4pt 32 \endgroup 33 \writetocentry{part}{#2}{#1} 34} 35\gdef\blankpart{% 36 \writetocentry{blankpart}{}{} 37} 38% Part TOC-entry definition for summary contents. 39\gdef\dosmallpartentry#1#2#3#4{% 40 \vskip .5\baselineskip plus.2\baselineskip 41 \begingroup 42 \let\rm=\bf \rm 43 \tocentry{Part #2: #1}{\doshortpageno\bgroup#4\egroup} 44 \endgroup 45} 46\gdef\dosmallblankpartentry#1#2#3#4{% 47 \vskip .5\baselineskip plus.2\baselineskip 48} 49% Part TOC-entry definition for regular contents. This has to be 50% equated to an existing entry to not cause problems when the PDF 51% outline is created. 52\gdef\dopartentry#1#2#3#4{% 53 \unnchapentry{Part #2: #1}{}{#3}{#4} 54} 55\gdef\doblankpartentry#1#2#3#4{} 56@end tex 57 58@c %**end of header 59 60@c Use with @@smallbook. 61 62@c %** start of document 63 64@c Cause even numbered pages to be printed on the left hand side of 65@c the page and odd numbered pages to be printed on the right hand 66@c side of the page. Using this, you can print on both sides of a 67@c sheet of paper and have the text on the same part of the sheet. 68 69@c The text on right hand pages is pushed towards the right hand 70@c margin and the text on left hand pages is pushed toward the left 71@c hand margin. 72@c (To provide the reverse effect, set bindingoffset to -0.75in.) 73 74@c @tex 75@c \global\bindingoffset=0.75in 76@c \global\normaloffset =0.75in 77@c @end tex 78 79@copying 80Copyright @copyright{} @value{copyrights-gfortran} Free Software Foundation, Inc. 81 82Permission is granted to copy, distribute and/or modify this document 83under the terms of the GNU Free Documentation License, Version 1.3 or 84any later version published by the Free Software Foundation; with the 85Invariant Sections being ``Funding Free Software'', the Front-Cover 86Texts being (a) (see below), and with the Back-Cover Texts being (b) 87(see below). A copy of the license is included in the section entitled 88``GNU Free Documentation License''. 89 90(a) The FSF's Front-Cover Text is: 91 92 A GNU Manual 93 94(b) The FSF's Back-Cover Text is: 95 96 You have freedom to copy and modify this GNU Manual, like GNU 97 software. Copies published by the Free Software Foundation raise 98 funds for GNU development. 99@end copying 100 101@ifinfo 102@dircategory Software development 103@direntry 104* gfortran: (gfortran). The GNU Fortran Compiler. 105@end direntry 106This file documents the use and the internals of 107the GNU Fortran compiler, (@command{gfortran}). 108 109Published by the Free Software Foundation 11051 Franklin Street, Fifth Floor 111Boston, MA 02110-1301 USA 112 113@insertcopying 114@end ifinfo 115 116 117@setchapternewpage odd 118@titlepage 119@title Using GNU Fortran 120@versionsubtitle 121@author The @t{gfortran} team 122@page 123@vskip 0pt plus 1filll 124Published by the Free Software Foundation@* 12551 Franklin Street, Fifth Floor@* 126Boston, MA 02110-1301, USA@* 127@c Last printed ??ber, 19??.@* 128@c Printed copies are available for $? each.@* 129@c ISBN ??? 130@sp 1 131@insertcopying 132@end titlepage 133 134@c TODO: The following "Part" definitions are included here temporarily 135@c until they are incorporated into the official Texinfo distribution. 136 137@tex 138\global\let\partentry=\dosmallpartentry 139\global\let\blankpartentry=\dosmallblankpartentry 140@end tex 141@summarycontents 142 143@tex 144\global\let\partentry=\dopartentry 145\global\let\blankpartentry=\doblankpartentry 146@end tex 147@contents 148 149@page 150 151@c --------------------------------------------------------------------- 152@c TexInfo table of contents. 153@c --------------------------------------------------------------------- 154 155@ifnottex 156@node Top 157@top Introduction 158@cindex Introduction 159 160This manual documents the use of @command{gfortran}, 161the GNU Fortran compiler. You can find in this manual how to invoke 162@command{gfortran}, as well as its features and incompatibilities. 163 164@ifset DEVELOPMENT 165@emph{Warning:} This document, and the compiler it describes, are still 166under development. While efforts are made to keep it up-to-date, it might 167not accurately reflect the status of the most recent GNU Fortran compiler. 168@end ifset 169 170@comment 171@comment When you add a new menu item, please keep the right hand 172@comment aligned to the same column. Do not use tabs. This provides 173@comment better formatting. 174@comment 175@menu 176* Introduction:: 177 178Part I: Invoking GNU Fortran 179* Invoking GNU Fortran:: Command options supported by @command{gfortran}. 180* Runtime:: Influencing runtime behavior with environment variables. 181 182Part II: Language Reference 183* Compiler Characteristics:: User-visible implementation details. 184* Extensions:: Language extensions implemented by GNU Fortran. 185* Mixed-Language Programming:: Interoperability with C 186* Coarray Programming:: 187* Intrinsic Procedures:: Intrinsic procedures supported by GNU Fortran. 188* Intrinsic Modules:: Intrinsic modules supported by GNU Fortran. 189 190* Contributing:: How you can help. 191* Copying:: GNU General Public License says 192 how you can copy and share GNU Fortran. 193* GNU Free Documentation License:: 194 How you can copy and share this manual. 195* Funding:: How to help assure continued work for free software. 196* Option Index:: Index of command line options 197* Keyword Index:: Index of concepts 198@end menu 199@end ifnottex 200 201@c --------------------------------------------------------------------- 202@c Introduction 203@c --------------------------------------------------------------------- 204 205@node Introduction 206@chapter Introduction 207 208@c The following duplicates the text on the TexInfo table of contents. 209@iftex 210This manual documents the use of @command{gfortran}, the GNU Fortran 211compiler. You can find in this manual how to invoke @command{gfortran}, 212as well as its features and incompatibilities. 213 214@ifset DEVELOPMENT 215@emph{Warning:} This document, and the compiler it describes, are still 216under development. While efforts are made to keep it up-to-date, it 217might not accurately reflect the status of the most recent GNU Fortran 218compiler. 219@end ifset 220@end iftex 221 222@menu 223* About GNU Fortran:: What you should know about the GNU Fortran compiler. 224* GNU Fortran and GCC:: You can compile Fortran, C, or other programs. 225* Standards:: Standards supported by GNU Fortran. 226@end menu 227 228 229@c --------------------------------------------------------------------- 230@c About GNU Fortran 231@c --------------------------------------------------------------------- 232 233@node About GNU Fortran 234@section About GNU Fortran 235 236The GNU Fortran compiler is the successor to @command{g77}, the 237Fortran 77 front end included in GCC prior to version 4 (released in 2382005). While it is backward-compatible with most @command{g77} 239extensions and command-line options, @command{gfortran} is a completely new 240implemention designed to support more modern dialects of Fortran. 241GNU Fortran implements the Fortran 77, 90 and 95 standards 242completely, most of the Fortran 2003 and 2008 standards, and some 243features from the 2018 standard. It also implements several extensions 244including OpenMP and OpenACC support for parallel programming. 245 246The GNU Fortran compiler passes the 247@uref{http://www.fortran-2000.com/ArnaudRecipes/fcvs21_f95.html, 248NIST Fortran 77 Test Suite}, and produces acceptable results on the 249@uref{https://www.netlib.org/lapack/faq.html#1.21, LAPACK Test Suite}. 250It also provides respectable performance on 251the @uref{https://polyhedron.com/?page_id=175, 252Polyhedron Fortran compiler benchmarks} and the 253@uref{https://www.netlib.org/benchmark/livermore, 254Livermore Fortran Kernels test}. It has been used to compile a number of 255large real-world programs, including 256@uref{http://hirlam.org/, the HARMONIE and HIRLAM weather forecasting code} and 257@uref{https://github.com/dylan-jayatilaka/tonto, 258the Tonto quantum chemistry package}; see 259@url{https://gcc.gnu.org/@/wiki/@/GfortranApps} for an extended list. 260 261GNU Fortran provides the following functionality: 262 263@itemize @bullet 264@item 265Read a program, stored in a file and containing @dfn{source code} 266instructions written in Fortran 77. 267 268@item 269Translate the program into instructions a computer 270can carry out more quickly than it takes to translate the 271original Fortran instructions. 272The result after compilation of a program is 273@dfn{machine code}, 274which is efficiently translated and processed 275by a machine such as your computer. 276Humans usually are not as good writing machine code 277as they are at writing Fortran (or C++, Ada, or Java), 278because it is easy to make tiny mistakes writing machine code. 279 280@item 281Provide information about the reasons why 282the compiler may be unable to create a binary from the source code, 283for example if the source code is flawed. 284The Fortran language standards require that the compiler can point out 285mistakes in your code. 286An incorrect usage of the language causes an @dfn{error message}. 287 288The compiler also attempts to diagnose cases where your 289program contains a correct usage of the language, 290but instructs the computer to do something questionable. 291This kind of diagnostic message is called a @dfn{warning message}. 292 293@item 294Provide optional information about the translation passes 295from the source code to machine code. 296This can help you to find the cause of 297certain bugs which may not be obvious in the source code, 298but may be more easily found at a lower level compiler output. 299It also helps developers to find bugs in the compiler itself. 300 301@item 302Provide information in the generated machine code that can 303make it easier to find bugs in the program (using a debugging tool, 304called a @dfn{debugger}, such as the GNU Debugger @command{gdb}). 305 306@item 307Locate and gather machine code already generated to 308perform actions requested by statements in the program. 309This machine code is organized into @dfn{modules} and is located 310and @dfn{linked} to the user program. 311@end itemize 312 313The GNU Fortran compiler consists of several components: 314 315@itemize @bullet 316@item 317A version of the @command{gcc} command 318(which also might be installed as the system's @command{cc} command) 319that also understands and accepts Fortran source code. 320The @command{gcc} command is the @dfn{driver} program for 321all the languages in the GNU Compiler Collection (GCC); 322With @command{gcc}, 323you can compile the source code of any language for 324which a front end is available in GCC. 325 326@item 327The @command{gfortran} command itself, 328which also might be installed as the 329system's @command{f95} command. 330@command{gfortran} is just another driver program, 331but specifically for the Fortran compiler only. 332The primary difference between the @command{gcc} and @command{gfortran} 333commands is that the latter automatically links the correct libraries 334to your program. 335 336@item 337A collection of run-time libraries. 338These libraries contain the machine code needed to support 339capabilities of the Fortran language that are not directly 340provided by the machine code generated by the 341@command{gfortran} compilation phase, 342such as intrinsic functions and subroutines, 343and routines for interaction with files and the operating system. 344@c and mechanisms to spawn, 345@c unleash and pause threads in parallelized code. 346 347@item 348The Fortran compiler itself, (@command{f951}). 349This is the GNU Fortran parser and code generator, 350linked to and interfaced with the GCC backend library. 351@command{f951} ``translates'' the source code to 352assembler code. You would typically not use this 353program directly; 354instead, the @command{gcc} or @command{gfortran} driver 355programs call it for you. 356@end itemize 357 358 359@c --------------------------------------------------------------------- 360@c GNU Fortran and GCC 361@c --------------------------------------------------------------------- 362 363@node GNU Fortran and GCC 364@section GNU Fortran and GCC 365@cindex GNU Compiler Collection 366@cindex GCC 367 368GNU Fortran is a part of GCC, the @dfn{GNU Compiler Collection}. GCC 369consists of a collection of front ends for various languages, which 370translate the source code into a language-independent form called 371@dfn{GENERIC}. This is then processed by a common middle end which 372provides optimization, and then passed to one of a collection of back 373ends which generate code for different computer architectures and 374operating systems. 375 376Functionally, this is implemented with a driver program (@command{gcc}) 377which provides the command-line interface for the compiler. It calls 378the relevant compiler front-end program (e.g., @command{f951} for 379Fortran) for each file in the source code, and then calls the assembler 380and linker as appropriate to produce the compiled output. In a copy of 381GCC that has been compiled with Fortran language support enabled, 382@command{gcc} recognizes files with @file{.f}, @file{.for}, @file{.ftn}, 383@file{.f90}, @file{.f95}, @file{.f03} and @file{.f08} extensions as 384Fortran source code, and compiles it accordingly. A @command{gfortran} 385driver program is also provided, which is identical to @command{gcc} 386except that it automatically links the Fortran runtime libraries into the 387compiled program. 388 389Source files with @file{.f}, @file{.for}, @file{.fpp}, @file{.ftn}, @file{.F}, 390@file{.FOR}, @file{.FPP}, and @file{.FTN} extensions are treated as fixed form. 391Source files with @file{.f90}, @file{.f95}, @file{.f03}, @file{.f08}, 392@file{.F90}, @file{.F95}, @file{.F03} and @file{.F08} extensions are 393treated as free form. The capitalized versions of either form are run 394through preprocessing. Source files with the lower case @file{.fpp} 395extension are also run through preprocessing. 396 397This manual specifically documents the Fortran front end, which handles 398the programming language's syntax and semantics. The aspects of GCC 399that relate to the optimization passes and the back-end code generation 400are documented in the GCC manual; see 401@ref{Top,,Introduction,gcc,Using the GNU Compiler Collection (GCC)}. 402The two manuals together provide a complete reference for the GNU 403Fortran compiler. 404 405@c --------------------------------------------------------------------- 406@c Standards 407@c --------------------------------------------------------------------- 408 409@node Standards 410@section Standards 411@cindex Standards 412 413@menu 414* Fortran 95 status:: 415* Fortran 2003 status:: 416* Fortran 2008 status:: 417* Fortran 2018 status:: 418@end menu 419 420Fortran is developed by the Working Group 5 of Sub-Committee 22 of the 421Joint Technical Committee 1 of the International Organization for 422Standardization and the International Electrotechnical Commission (IEC). 423This group is known as @uref{http://www.nag.co.uk/sc22wg5/, WG5}. 424Official Fortran standard documents are available for purchase 425from ISO; a collection of free documents (typically final drafts) are 426also available on the @uref{https://gcc.gnu.org/wiki/GFortranStandards, wiki}. 427 428The GNU Fortran compiler implements ISO/IEC 1539:1997 (Fortran 95). 429As such, it can also compile essentially all standard-compliant 430Fortran 90 and Fortran 77 programs. It also supports the ISO/IEC 431TR-15581 enhancements to allocatable arrays. 432 433GNU Fortran also supports almost all of ISO/IEC 1539-1:2004 434(Fortran 2003) and ISO/IEC 1539-1:2010 (Fortran 2008). 435It has partial support for features introduced in ISO/IEC 4361539:2018 (Fortran 2018), the most recent version of the Fortran 437language standard, including full support for the Technical Specification 438@code{Further Interoperability of Fortran with C} (ISO/IEC TS 29113:2012). 439More details on support for these standards can be 440found in the following sections of the documentation. 441 442Additionally, the GNU Fortran compilers supports the OpenMP specification 443(version 4.5 and partial support of the features of the 5.0 version, 444@url{https://openmp.org/@/openmp-specifications/}). 445There also is support for the OpenACC specification (targeting 446version 2.6, @uref{https://www.openacc.org/}). See 447@uref{https://gcc.gnu.org/wiki/OpenACC} for more information. 448 449@node Fortran 95 status 450@subsection Fortran 95 status 451@cindex Varying length strings 452@cindex strings, varying length 453@cindex conditional compilation 454 455The Fortran 95 standard specifies in Part 2 (ISO/IEC 1539-2:2000) 456varying length character strings. While GNU Fortran currently does not 457support such strings directly, there exist two Fortran implementations 458for them, which work with GNU Fortran. They can be found at 459@uref{https://www.fortran.com/@/iso_varying_string.f95} and at 460@uref{ftp://ftp.nag.co.uk/@/sc22wg5/@/ISO_VARYING_STRING/}. 461 462Deferred-length character strings of Fortran 2003 supports part of 463the features of @code{ISO_VARYING_STRING} and should be considered as 464replacement. (Namely, allocatable or pointers of the type 465@code{character(len=:)}.) 466 467Part 3 of the Fortran 95 standard (ISO/IEC 1539-3:1998) defines 468Conditional Compilation, which is not widely used and not directly 469supported by the GNU Fortran compiler. You can use the program coco 470to preprocess such files (@uref{http://www.daniellnagle.com/coco.html}). 471 472@node Fortran 2003 status 473@subsection Fortran 2003 status 474 475GNU Fortran implements the Fortran 2003 (ISO/IEC 1539-1:2004) standard 476except for finalization support, which is incomplete. 477See the 478@uref{https://gcc.gnu.org/wiki/Fortran2003, wiki page} for a full list 479of new features introduced by Fortran 2003 and their implementation status. 480 481@node Fortran 2008 status 482@subsection Fortran 2008 status 483 484The GNU Fortran compiler supports almost all features of Fortran 2008; 485the @uref{https://gcc.gnu.org/wiki/Fortran2008Status, wiki} 486has some information about the current implementation status. 487In particular, the following are not yet supported: 488 489@itemize @bullet 490@item 491@code{DO CONCURRENT} and @code{FORALL} do not recognize a 492type-spec in the loop header. 493 494@item 495The change to permit any constant expression in subscripts and 496nested implied-do limits in a @code{DATA} statement has not been implemented. 497@end itemize 498 499 500@node Fortran 2018 status 501@subsection Fortran 2018 status 502 503Fortran 2018 (ISO/IEC 1539:2018) is the most recent version 504of the Fortran language standard. GNU Fortran implements some of the 505new features of this standard: 506 507@itemize @bullet 508@item 509All Fortran 2018 features derived from ISO/IEC TS 29113:2012, 510``Further Interoperability of Fortran with C'', are supported by GNU Fortran. 511This includes assumed-type and assumed-rank objects and 512the @code{SELECT RANK} construct as well as the parts relating to 513@code{BIND(C)} functions. 514See also @ref{Further Interoperability of Fortran with C}. 515 516@item 517GNU Fortran supports a subset of features derived from ISO/IEC TS 18508:2015, 518``Additional Parallel Features in Fortran'': 519 520@itemize @bullet 521@item 522The new atomic ADD, CAS, FETCH and ADD/OR/XOR, OR and XOR intrinsics. 523 524@item 525The @code{CO_MIN} and @code{CO_MAX} and @code{SUM} reduction intrinsics, 526and the @code{CO_BROADCAST} and @code{CO_REDUCE} intrinsic, except that those 527do not support polymorphic types or types with allocatable, pointer or 528polymorphic components. 529 530@item 531Events (@code{EVENT POST}, @code{EVENT WAIT}, @code{EVENT_QUERY}). 532 533@item 534Failed images (@code{FAIL IMAGE}, @code{IMAGE_STATUS}, 535@code{FAILED_IMAGES}, @code{STOPPED_IMAGES}). 536 537@end itemize 538 539@item 540An @code{ERROR STOP} statement is permitted in a @code{PURE} 541procedure. 542 543@item 544GNU Fortran supports the @code{IMPLICIT NONE} statement with an 545@code{implicit-none-spec-list}. 546 547@item 548The behavior of the @code{INQUIRE} statement with the @code{RECL=} 549specifier now conforms to Fortran 2018. 550 551@end itemize 552 553 554@c ===================================================================== 555@c PART I: INVOCATION REFERENCE 556@c ===================================================================== 557 558@tex 559\part{I}{Invoking GNU Fortran} 560@end tex 561 562@c --------------------------------------------------------------------- 563@c Compiler Options 564@c --------------------------------------------------------------------- 565 566@include invoke.texi 567 568 569@c --------------------------------------------------------------------- 570@c Runtime 571@c --------------------------------------------------------------------- 572 573@node Runtime 574@chapter Runtime: Influencing runtime behavior with environment variables 575@cindex environment variable 576 577The behavior of the @command{gfortran} can be influenced by 578environment variables. 579 580Malformed environment variables are silently ignored. 581 582@menu 583* TMPDIR:: Directory for scratch files 584* GFORTRAN_STDIN_UNIT:: Unit number for standard input 585* GFORTRAN_STDOUT_UNIT:: Unit number for standard output 586* GFORTRAN_STDERR_UNIT:: Unit number for standard error 587* GFORTRAN_UNBUFFERED_ALL:: Do not buffer I/O for all units 588* GFORTRAN_UNBUFFERED_PRECONNECTED:: Do not buffer I/O for preconnected units. 589* GFORTRAN_SHOW_LOCUS:: Show location for runtime errors 590* GFORTRAN_OPTIONAL_PLUS:: Print leading + where permitted 591* GFORTRAN_LIST_SEPARATOR:: Separator for list output 592* GFORTRAN_CONVERT_UNIT:: Set conversion for unformatted I/O 593* GFORTRAN_ERROR_BACKTRACE:: Show backtrace on run-time errors 594* GFORTRAN_FORMATTED_BUFFER_SIZE:: Buffer size for formatted files 595* GFORTRAN_UNFORMATTED_BUFFER_SIZE:: Buffer size for unformatted files 596@end menu 597 598@node TMPDIR 599@section @env{TMPDIR}---Directory for scratch files 600 601When opening a file with @code{STATUS='SCRATCH'}, GNU Fortran tries to 602create the file in one of the potential directories by testing each 603directory in the order below. 604 605@enumerate 606@item 607The environment variable @env{TMPDIR}, if it exists. 608 609@item 610On the MinGW target, the directory returned by the @code{GetTempPath} 611function. Alternatively, on the Cygwin target, the @env{TMP} and 612@env{TEMP} environment variables, if they exist, in that order. 613 614@item 615The @code{P_tmpdir} macro if it is defined, otherwise the directory 616@file{/tmp}. 617@end enumerate 618 619@node GFORTRAN_STDIN_UNIT 620@section @env{GFORTRAN_STDIN_UNIT}---Unit number for standard input 621 622This environment variable can be used to select the unit number 623preconnected to standard input. This must be a positive integer. 624The default value is 5. 625 626@node GFORTRAN_STDOUT_UNIT 627@section @env{GFORTRAN_STDOUT_UNIT}---Unit number for standard output 628 629This environment variable can be used to select the unit number 630preconnected to standard output. This must be a positive integer. 631The default value is 6. 632 633@node GFORTRAN_STDERR_UNIT 634@section @env{GFORTRAN_STDERR_UNIT}---Unit number for standard error 635 636This environment variable can be used to select the unit number 637preconnected to standard error. This must be a positive integer. 638The default value is 0. 639 640@node GFORTRAN_UNBUFFERED_ALL 641@section @env{GFORTRAN_UNBUFFERED_ALL}---Do not buffer I/O on all units 642 643This environment variable controls whether all I/O is unbuffered. If 644the first letter is @samp{y}, @samp{Y} or @samp{1}, all I/O is 645unbuffered. This will slow down small sequential reads and writes. If 646the first letter is @samp{n}, @samp{N} or @samp{0}, I/O is buffered. 647This is the default. 648 649@node GFORTRAN_UNBUFFERED_PRECONNECTED 650@section @env{GFORTRAN_UNBUFFERED_PRECONNECTED}---Do not buffer I/O on preconnected units 651 652The environment variable named @env{GFORTRAN_UNBUFFERED_PRECONNECTED} controls 653whether I/O on a preconnected unit (i.e.@: STDOUT or STDERR) is unbuffered. If 654the first letter is @samp{y}, @samp{Y} or @samp{1}, I/O is unbuffered. This 655will slow down small sequential reads and writes. If the first letter 656is @samp{n}, @samp{N} or @samp{0}, I/O is buffered. This is the default. 657 658@node GFORTRAN_SHOW_LOCUS 659@section @env{GFORTRAN_SHOW_LOCUS}---Show location for runtime errors 660 661If the first letter is @samp{y}, @samp{Y} or @samp{1}, filename and 662line numbers for runtime errors are printed. If the first letter is 663@samp{n}, @samp{N} or @samp{0}, do not print filename and line numbers 664for runtime errors. The default is to print the location. 665 666@node GFORTRAN_OPTIONAL_PLUS 667@section @env{GFORTRAN_OPTIONAL_PLUS}---Print leading + where permitted 668 669If the first letter is @samp{y}, @samp{Y} or @samp{1}, 670a plus sign is printed 671where permitted by the Fortran standard. If the first letter 672is @samp{n}, @samp{N} or @samp{0}, a plus sign is not printed 673in most cases. Default is not to print plus signs. 674 675@node GFORTRAN_LIST_SEPARATOR 676@section @env{GFORTRAN_LIST_SEPARATOR}---Separator for list output 677 678This environment variable specifies the separator when writing 679list-directed output. It may contain any number of spaces and 680at most one comma. If you specify this on the command line, 681be sure to quote spaces, as in 682@smallexample 683$ GFORTRAN_LIST_SEPARATOR=' , ' ./a.out 684@end smallexample 685when @command{a.out} is the compiled Fortran program that you want to run. 686Default is a single space. 687 688@node GFORTRAN_CONVERT_UNIT 689@section @env{GFORTRAN_CONVERT_UNIT}---Set conversion for unformatted I/O 690 691By setting the @env{GFORTRAN_CONVERT_UNIT} variable, it is possible 692to change the representation of data for unformatted files. 693The syntax for the @env{GFORTRAN_CONVERT_UNIT} variable for 694most systems is: 695@smallexample 696GFORTRAN_CONVERT_UNIT: mode | mode ';' exception | exception ; 697mode: 'native' | 'swap' | 'big_endian' | 'little_endian' ; 698exception: mode ':' unit_list | unit_list ; 699unit_list: unit_spec | unit_list unit_spec ; 700unit_spec: INTEGER | INTEGER '-' INTEGER ; 701@end smallexample 702The variable consists of an optional default mode, followed by 703a list of optional exceptions, which are separated by semicolons 704from the preceding default and each other. Each exception consists 705of a format and a comma-separated list of units. Valid values for 706the modes are the same as for the @code{CONVERT} specifier: 707 708@itemize @w{} 709@item @code{NATIVE} Use the native format. This is the default. 710@item @code{SWAP} Swap between little- and big-endian. 711@item @code{LITTLE_ENDIAN} Use the little-endian format 712for unformatted files. 713@item @code{BIG_ENDIAN} Use the big-endian format for unformatted files. 714@end itemize 715For POWER systems which support @option{-mabi=ieeelongdouble}, 716there are additional options, which can be combined with the 717others with commas. Those are 718@itemize @w{} 719@item @code{R16_IEEE} Use IEEE 128-bit format for @code{REAL(KIND=16)}. 720@item @code{R16_IBM} Use IBM @code{long double} format for 721@code{REAL(KIND=16)}. 722@end itemize 723A missing mode for an exception is taken to mean @code{BIG_ENDIAN}. 724Examples of values for @env{GFORTRAN_CONVERT_UNIT} are: 725@itemize @w{} 726@item @code{'big_endian'} Do all unformatted I/O in big_endian mode. 727@item @code{'little_endian;native:10-20,25'} Do all unformatted I/O 728in little_endian mode, except for units 10 to 20 and 25, which are in 729native format. 730@item @code{'10-20'} Units 10 to 20 are big-endian, the rest is native. 731@item @code{'big_endian,r16_ibm'} Do all unformatted I/O in big-endian 732mode and use IBM long double for output of @code{REAL(KIND=16)} values. 733@end itemize 734 735Setting the environment variables should be done on the command 736line or via the @command{export} 737command for @command{sh}-compatible shells and via @command{setenv} 738for @command{csh}-compatible shells. 739 740Example for @command{sh}: 741@smallexample 742$ gfortran foo.f90 743$ GFORTRAN_CONVERT_UNIT='big_endian;native:10-20' ./a.out 744@end smallexample 745 746Example code for @command{csh}: 747@smallexample 748% gfortran foo.f90 749% setenv GFORTRAN_CONVERT_UNIT 'big_endian;native:10-20' 750% ./a.out 751@end smallexample 752 753Using anything but the native representation for unformatted data 754carries a significant speed overhead. If speed in this area matters 755to you, it is best if you use this only for data that needs to be 756portable. 757 758@xref{CONVERT specifier}, for an alternative way to specify the 759data representation for unformatted files. @xref{Runtime Options}, for 760setting a default data representation for the whole program. The 761@code{CONVERT} specifier overrides the @option{-fconvert} compile options. 762 763@emph{Note that the values specified via the GFORTRAN_CONVERT_UNIT 764environment variable will override the CONVERT specifier in the 765open statement}. This is to give control over data formats to 766users who do not have the source code of their program available. 767 768@node GFORTRAN_ERROR_BACKTRACE 769@section @env{GFORTRAN_ERROR_BACKTRACE}---Show backtrace on run-time errors 770 771If the @env{GFORTRAN_ERROR_BACKTRACE} variable is set to @samp{y}, 772@samp{Y} or @samp{1} (only the first letter is relevant) then a 773backtrace is printed when a serious run-time error occurs. To disable 774the backtracing, set the variable to @samp{n}, @samp{N}, @samp{0}. 775Default is to print a backtrace unless the @option{-fno-backtrace} 776compile option was used. 777 778@node GFORTRAN_FORMATTED_BUFFER_SIZE 779@section @env{GFORTRAN_FORMATTED_BUFFER_SIZE}---Set buffer size for formatted I/O 780 781The @env{GFORTRAN_FORMATTED_BUFFER_SIZE} environment variable 782specifies buffer size in bytes to be used for formatted output. 783The default value is 8192. 784 785@node GFORTRAN_UNFORMATTED_BUFFER_SIZE 786@section @env{GFORTRAN_UNFORMATTED_BUFFER_SIZE}---Set buffer size for unformatted I/O 787 788The @env{GFORTRAN_UNFORMATTED_BUFFER_SIZE} environment variable 789specifies buffer size in bytes to be used for unformatted output. 790The default value is 131072. 791 792@c ===================================================================== 793@c PART II: LANGUAGE REFERENCE 794@c ===================================================================== 795 796@tex 797\part{II}{Language Reference} 798@end tex 799 800 801 802@c --------------------------------------------------------------------- 803@c Compiler Characteristics 804@c --------------------------------------------------------------------- 805 806@node Compiler Characteristics 807@chapter Compiler Characteristics 808 809This chapter describes certain characteristics of the GNU Fortran 810compiler, that are not specified by the Fortran standard, but which 811might in some way or another become visible to the programmer. 812 813@menu 814* KIND Type Parameters:: 815* Internal representation of LOGICAL variables:: 816* Evaluation of logical expressions:: 817* MAX and MIN intrinsics with REAL NaN arguments:: 818* Thread-safety of the runtime library:: 819* Data consistency and durability:: 820* Files opened without an explicit ACTION= specifier:: 821* File operations on symbolic links:: 822* File format of unformatted sequential files:: 823* Asynchronous I/O:: 824@end menu 825 826 827@node KIND Type Parameters 828@section KIND Type Parameters 829@cindex kind 830 831The @code{KIND} type parameters supported by GNU Fortran for the primitive 832data types are: 833 834@table @code 835 836@item INTEGER 8371, 2, 4, 8*, 16*, default: 4** 838 839@item LOGICAL 8401, 2, 4, 8*, 16*, default: 4** 841 842@item REAL 8434, 8, 10*, 16*, default: 4*** 844 845@item COMPLEX 8464, 8, 10*, 16*, default: 4*** 847 848@item DOUBLE PRECISION 8494, 8, 10*, 16*, default: 8*** 850 851@item CHARACTER 8521, 4, default: 1 853 854@end table 855 856@noindent 857* not available on all systems @* 858** unless @option{-fdefault-integer-8} is used @* 859*** unless @option{-fdefault-real-8} is used (see @ref{Fortran Dialect Options}) 860 861@noindent 862The @code{KIND} value matches the storage size in bytes, except for 863@code{COMPLEX} where the storage size is twice as much (or both real and 864imaginary part are a real value of the given size). It is recommended to use 865the @ref{SELECTED_CHAR_KIND}, @ref{SELECTED_INT_KIND} and 866@ref{SELECTED_REAL_KIND} intrinsics or the @code{INT8}, @code{INT16}, 867@code{INT32}, @code{INT64}, @code{REAL32}, @code{REAL64}, and @code{REAL128} 868parameters of the @code{ISO_FORTRAN_ENV} module instead of the concrete values. 869The available kind parameters can be found in the constant arrays 870@code{CHARACTER_KINDS}, @code{INTEGER_KINDS}, @code{LOGICAL_KINDS} and 871@code{REAL_KINDS} in the @ref{ISO_FORTRAN_ENV} module. For C interoperability, 872the kind parameters of the @ref{ISO_C_BINDING} module should be used. 873 874 875@node Internal representation of LOGICAL variables 876@section Internal representation of LOGICAL variables 877@cindex logical, variable representation 878 879The Fortran standard does not specify how variables of @code{LOGICAL} 880type are represented, beyond requiring that @code{LOGICAL} variables 881of default kind have the same storage size as default @code{INTEGER} 882and @code{REAL} variables. The GNU Fortran internal representation is 883as follows. 884 885A @code{LOGICAL(KIND=N)} variable is represented as an 886@code{INTEGER(KIND=N)} variable, however, with only two permissible 887values: @code{1} for @code{.TRUE.} and @code{0} for 888@code{.FALSE.}. Any other integer value results in undefined behavior. 889 890See also @ref{Argument passing conventions} and @ref{Interoperability with C}. 891 892 893@node Evaluation of logical expressions 894@section Evaluation of logical expressions 895 896The Fortran standard does not require the compiler to evaluate all parts of an 897expression, if they do not contribute to the final result. For logical 898expressions with @code{.AND.} or @code{.OR.} operators, in particular, GNU 899Fortran will optimize out function calls (even to impure functions) if the 900result of the expression can be established without them. However, since not 901all compilers do that, and such an optimization can potentially modify the 902program flow and subsequent results, GNU Fortran throws warnings for such 903situations with the @option{-Wfunction-elimination} flag. 904 905 906@node MAX and MIN intrinsics with REAL NaN arguments 907@section MAX and MIN intrinsics with REAL NaN arguments 908@cindex MAX, MIN, NaN 909 910The Fortran standard does not specify what the result of the 911@code{MAX} and @code{MIN} intrinsics are if one of the arguments is a 912@code{NaN}. Accordingly, the GNU Fortran compiler does not specify 913that either, as this allows for faster and more compact code to be 914generated. If the programmer wishes to take some specific action in 915case one of the arguments is a @code{NaN}, it is necessary to 916explicitly test the arguments before calling @code{MAX} or @code{MIN}, 917e.g. with the @code{IEEE_IS_NAN} function from the intrinsic module 918@code{IEEE_ARITHMETIC}. 919 920 921@node Thread-safety of the runtime library 922@section Thread-safety of the runtime library 923@cindex thread-safety, threads 924 925GNU Fortran can be used in programs with multiple threads, e.g.@: by 926using OpenMP, by calling OS thread handling functions via the 927@code{ISO_C_BINDING} facility, or by GNU Fortran compiled library code 928being called from a multi-threaded program. 929 930The GNU Fortran runtime library, (@code{libgfortran}), supports being 931called concurrently from multiple threads with the following 932exceptions. 933 934During library initialization, the C @code{getenv} function is used, 935which need not be thread-safe. Similarly, the @code{getenv} 936function is used to implement the @code{GET_ENVIRONMENT_VARIABLE} and 937@code{GETENV} intrinsics. It is the responsibility of the user to 938ensure that the environment is not being updated concurrently when any 939of these actions are taking place. 940 941The @code{EXECUTE_COMMAND_LINE} and @code{SYSTEM} intrinsics are 942implemented with the @code{system} function, which need not be 943thread-safe. It is the responsibility of the user to ensure that 944@code{system} is not called concurrently. 945 946For platforms not supporting thread-safe POSIX functions, further 947functionality might not be thread-safe. For details, please consult 948the documentation for your operating system. 949 950The GNU Fortran runtime library uses various C library functions that 951depend on the locale, such as @code{strtod} and @code{snprintf}. In 952order to work correctly in locale-aware programs that set the locale 953using @code{setlocale}, the locale is reset to the default ``C'' 954locale while executing a formatted @code{READ} or @code{WRITE} 955statement. On targets supporting the POSIX 2008 per-thread locale 956functions (e.g. @code{newlocale}, @code{uselocale}, 957@code{freelocale}), these are used and thus the global locale set 958using @code{setlocale} or the per-thread locales in other threads are 959not affected. However, on targets lacking this functionality, the 960global LC_NUMERIC locale is set to ``C'' during the formatted I/O. 961Thus, on such targets it's not safe to call @code{setlocale} 962concurrently from another thread while a Fortran formatted I/O 963operation is in progress. Also, other threads doing something 964dependent on the LC_NUMERIC locale might not work correctly if a 965formatted I/O operation is in progress in another thread. 966 967@node Data consistency and durability 968@section Data consistency and durability 969@cindex consistency, durability 970 971This section contains a brief overview of data and metadata 972consistency and durability issues when doing I/O. 973 974With respect to durability, GNU Fortran makes no effort to ensure that 975data is committed to stable storage. If this is required, the GNU 976Fortran programmer can use the intrinsic @code{FNUM} to retrieve the 977low level file descriptor corresponding to an open Fortran unit. Then, 978using e.g. the @code{ISO_C_BINDING} feature, one can call the 979underlying system call to flush dirty data to stable storage, such as 980@code{fsync} on POSIX, @code{_commit} on MingW, or @code{fcntl(fd, 981F_FULLSYNC, 0)} on Mac OS X. The following example shows how to call 982fsync: 983 984@smallexample 985 ! Declare the interface for POSIX fsync function 986 interface 987 function fsync (fd) bind(c,name="fsync") 988 use iso_c_binding, only: c_int 989 integer(c_int), value :: fd 990 integer(c_int) :: fsync 991 end function fsync 992 end interface 993 994 ! Variable declaration 995 integer :: ret 996 997 ! Opening unit 10 998 open (10,file="foo") 999 1000 ! ... 1001 ! Perform I/O on unit 10 1002 ! ... 1003 1004 ! Flush and sync 1005 flush(10) 1006 ret = fsync(fnum(10)) 1007 1008 ! Handle possible error 1009 if (ret /= 0) stop "Error calling FSYNC" 1010@end smallexample 1011 1012With respect to consistency, for regular files GNU Fortran uses 1013buffered I/O in order to improve performance. This buffer is flushed 1014automatically when full and in some other situations, e.g. when 1015closing a unit. It can also be explicitly flushed with the 1016@code{FLUSH} statement. Also, the buffering can be turned off with the 1017@code{GFORTRAN_UNBUFFERED_ALL} and 1018@code{GFORTRAN_UNBUFFERED_PRECONNECTED} environment variables. Special 1019files, such as terminals and pipes, are always unbuffered. Sometimes, 1020however, further things may need to be done in order to allow other 1021processes to see data that GNU Fortran has written, as follows. 1022 1023The Windows platform supports a relaxed metadata consistency model, 1024where file metadata is written to the directory lazily. This means 1025that, for instance, the @code{dir} command can show a stale size for a 1026file. One can force a directory metadata update by closing the unit, 1027or by calling @code{_commit} on the file descriptor. Note, though, 1028that @code{_commit} will force all dirty data to stable storage, which 1029is often a very slow operation. 1030 1031The Network File System (NFS) implements a relaxed consistency model 1032called open-to-close consistency. Closing a file forces dirty data and 1033metadata to be flushed to the server, and opening a file forces the 1034client to contact the server in order to revalidate cached 1035data. @code{fsync} will also force a flush of dirty data and metadata 1036to the server. Similar to @code{open} and @code{close}, acquiring and 1037releasing @code{fcntl} file locks, if the server supports them, will 1038also force cache validation and flushing dirty data and metadata. 1039 1040 1041@node Files opened without an explicit ACTION= specifier 1042@section Files opened without an explicit ACTION= specifier 1043@cindex open, action 1044 1045The Fortran standard says that if an @code{OPEN} statement is executed 1046without an explicit @code{ACTION=} specifier, the default value is 1047processor dependent. GNU Fortran behaves as follows: 1048 1049@enumerate 1050@item Attempt to open the file with @code{ACTION='READWRITE'} 1051@item If that fails, try to open with @code{ACTION='READ'} 1052@item If that fails, try to open with @code{ACTION='WRITE'} 1053@item If that fails, generate an error 1054@end enumerate 1055 1056 1057@node File operations on symbolic links 1058@section File operations on symbolic links 1059@cindex file, symbolic link 1060 1061This section documents the behavior of GNU Fortran for file operations on 1062symbolic links, on systems that support them. 1063 1064@itemize 1065 1066@item Results of INQUIRE statements of the ``inquire by file'' form will 1067relate to the target of the symbolic link. For example, 1068@code{INQUIRE(FILE="foo",EXIST=ex)} will set @var{ex} to @var{.true.} if 1069@var{foo} is a symbolic link pointing to an existing file, and @var{.false.} 1070if @var{foo} points to an non-existing file (``dangling'' symbolic link). 1071 1072@item Using the @code{OPEN} statement with a @code{STATUS="NEW"} specifier 1073on a symbolic link will result in an error condition, whether the symbolic 1074link points to an existing target or is dangling. 1075 1076@item If a symbolic link was connected, using the @code{CLOSE} statement 1077with a @code{STATUS="DELETE"} specifier will cause the symbolic link itself 1078to be deleted, not its target. 1079 1080@end itemize 1081 1082@node File format of unformatted sequential files 1083@section File format of unformatted sequential files 1084@cindex file, unformatted sequential 1085@cindex unformatted sequential 1086@cindex sequential, unformatted 1087@cindex record marker 1088@cindex subrecord 1089 1090Unformatted sequential files are stored as logical records using 1091record markers. Each logical record consists of one of more 1092subrecords. 1093 1094Each subrecord consists of a leading record marker, the data written 1095by the user program, and a trailing record marker. The record markers 1096are four-byte integers by default, and eight-byte integers if the 1097@option{-fmax-subrecord-length=8} option (which exists for backwards 1098compability only) is in effect. 1099 1100The representation of the record markers is that of unformatted files 1101given with the @option{-fconvert} option, the @ref{CONVERT specifier} 1102in an open statement or the @ref{GFORTRAN_CONVERT_UNIT} environment 1103variable. 1104 1105The maximum number of bytes of user data in a subrecord is 2147483639 1106(2 GiB - 9) for a four-byte record marker. This limit can be lowered 1107with the @option{-fmax-subrecord-length} option, although this is 1108rarely useful. If the length of a logical record exceeds this limit, 1109the data is distributed among several subrecords. 1110 1111The absolute of the number stored in the record markers is the number 1112of bytes of user data in the corresponding subrecord. If the leading 1113record marker of a subrecord contains a negative number, another 1114subrecord follows the current one. If the trailing record marker 1115contains a negative number, then there is a preceding subrecord. 1116 1117In the most simple case, with only one subrecord per logical record, 1118both record markers contain the number of bytes of user data in the 1119record. 1120 1121The format for unformatted sequential data can be duplicated using 1122unformatted stream, as shown in the example program for an unformatted 1123record containing a single subrecord: 1124 1125@smallexample 1126program main 1127 use iso_fortran_env, only: int32 1128 implicit none 1129 integer(int32) :: i 1130 real, dimension(10) :: a, b 1131 call random_number(a) 1132 open (10,file='test.dat',form='unformatted',access='stream') 1133 inquire (iolength=i) a 1134 write (10) i, a, i 1135 close (10) 1136 open (10,file='test.dat',form='unformatted') 1137 read (10) b 1138 if (all (a == b)) print *,'success!' 1139end program main 1140@end smallexample 1141 1142@node Asynchronous I/O 1143@section Asynchronous I/O 1144@cindex input/output, asynchronous 1145@cindex asynchronous I/O 1146 1147Asynchronous I/O is supported if the program is linked against the 1148POSIX thread library. If that is not the case, all I/O is performed 1149as synchronous. On systems which do not support pthread condition 1150variables, such as AIX, I/O is also performed as synchronous. 1151 1152On some systems, such as Darwin or Solaris, the POSIX thread library 1153is always linked in, so asynchronous I/O is always performed. On other 1154sytems, such as Linux, it is necessary to specify @option{-pthread}, 1155@option{-lpthread} or @option{-fopenmp} during the linking step. 1156 1157@c --------------------------------------------------------------------- 1158@c Extensions 1159@c --------------------------------------------------------------------- 1160 1161@c Maybe this chapter should be merged with the 'Standards' section, 1162@c whenever that is written :-) 1163 1164@node Extensions 1165@chapter Extensions 1166@cindex extensions 1167 1168The two sections below detail the extensions to standard Fortran that are 1169implemented in GNU Fortran, as well as some of the popular or 1170historically important extensions that are not (or not yet) implemented. 1171For the latter case, we explain the alternatives available to GNU Fortran 1172users, including replacement by standard-conforming code or GNU 1173extensions. 1174 1175@menu 1176* Extensions implemented in GNU Fortran:: 1177* Extensions not implemented in GNU Fortran:: 1178@end menu 1179 1180 1181@node Extensions implemented in GNU Fortran 1182@section Extensions implemented in GNU Fortran 1183@cindex extensions, implemented 1184 1185GNU Fortran implements a number of extensions over standard Fortran. 1186This chapter contains information on their syntax and meaning. There 1187are currently two categories of GNU Fortran extensions, those that 1188provide functionality beyond that provided by any standard, and those 1189that are supported by GNU Fortran purely for backward compatibility 1190with legacy compilers. By default, @option{-std=gnu} allows the 1191compiler to accept both types of extensions, but to warn about the use 1192of the latter. Specifying either @option{-std=f95}, 1193@option{-std=f2003}, @option{-std=f2008}, or @option{-std=f2018} 1194disables both types of extensions, and @option{-std=legacy} allows 1195both without warning. The special compile flag @option{-fdec} enables 1196additional compatibility extensions along with those enabled by 1197@option{-std=legacy}. 1198 1199@menu 1200* Old-style kind specifications:: 1201* Old-style variable initialization:: 1202* Extensions to namelist:: 1203* X format descriptor without count field:: 1204* Commas in FORMAT specifications:: 1205* Missing period in FORMAT specifications:: 1206* Default widths for F@comma{} G and I format descriptors:: 1207* I/O item lists:: 1208* @code{Q} exponent-letter:: 1209* BOZ literal constants:: 1210* Real array indices:: 1211* Unary operators:: 1212* Implicitly convert LOGICAL and INTEGER values:: 1213* Hollerith constants support:: 1214* Character conversion:: 1215* Cray pointers:: 1216* CONVERT specifier:: 1217* OpenMP:: 1218* OpenACC:: 1219* Argument list functions:: 1220* Read/Write after EOF marker:: 1221* STRUCTURE and RECORD:: 1222* UNION and MAP:: 1223* Type variants for integer intrinsics:: 1224* AUTOMATIC and STATIC attributes:: 1225* Extended math intrinsics:: 1226* Form feed as whitespace:: 1227* TYPE as an alias for PRINT:: 1228* %LOC as an rvalue:: 1229* .XOR. operator:: 1230* Bitwise logical operators:: 1231* Extended I/O specifiers:: 1232* Legacy PARAMETER statements:: 1233* Default exponents:: 1234@end menu 1235 1236@node Old-style kind specifications 1237@subsection Old-style kind specifications 1238@cindex kind, old-style 1239 1240GNU Fortran allows old-style kind specifications in declarations. These 1241look like: 1242@smallexample 1243 TYPESPEC*size x,y,z 1244@end smallexample 1245@noindent 1246where @code{TYPESPEC} is a basic type (@code{INTEGER}, @code{REAL}, 1247etc.), and where @code{size} is a byte count corresponding to the 1248storage size of a valid kind for that type. (For @code{COMPLEX} 1249variables, @code{size} is the total size of the real and imaginary 1250parts.) The statement then declares @code{x}, @code{y} and @code{z} to 1251be of type @code{TYPESPEC} with the appropriate kind. This is 1252equivalent to the standard-conforming declaration 1253@smallexample 1254 TYPESPEC(k) x,y,z 1255@end smallexample 1256@noindent 1257where @code{k} is the kind parameter suitable for the intended precision. As 1258kind parameters are implementation-dependent, use the @code{KIND}, 1259@code{SELECTED_INT_KIND} and @code{SELECTED_REAL_KIND} intrinsics to retrieve 1260the correct value, for instance @code{REAL*8 x} can be replaced by: 1261@smallexample 1262INTEGER, PARAMETER :: dbl = KIND(1.0d0) 1263REAL(KIND=dbl) :: x 1264@end smallexample 1265 1266@node Old-style variable initialization 1267@subsection Old-style variable initialization 1268 1269GNU Fortran allows old-style initialization of variables of the 1270form: 1271@smallexample 1272 INTEGER i/1/,j/2/ 1273 REAL x(2,2) /3*0.,1./ 1274@end smallexample 1275The syntax for the initializers is as for the @code{DATA} statement, but 1276unlike in a @code{DATA} statement, an initializer only applies to the 1277variable immediately preceding the initialization. In other words, 1278something like @code{INTEGER I,J/2,3/} is not valid. This style of 1279initialization is only allowed in declarations without double colons 1280(@code{::}); the double colons were introduced in Fortran 90, which also 1281introduced a standard syntax for initializing variables in type 1282declarations. 1283 1284Examples of standard-conforming code equivalent to the above example 1285are: 1286@smallexample 1287! Fortran 90 1288 INTEGER :: i = 1, j = 2 1289 REAL :: x(2,2) = RESHAPE((/0.,0.,0.,1./),SHAPE(x)) 1290! Fortran 77 1291 INTEGER i, j 1292 REAL x(2,2) 1293 DATA i/1/, j/2/, x/3*0.,1./ 1294@end smallexample 1295 1296Note that variables which are explicitly initialized in declarations 1297or in @code{DATA} statements automatically acquire the @code{SAVE} 1298attribute. 1299 1300@node Extensions to namelist 1301@subsection Extensions to namelist 1302@cindex Namelist 1303 1304GNU Fortran fully supports the Fortran 95 standard for namelist I/O 1305including array qualifiers, substrings and fully qualified derived types. 1306The output from a namelist write is compatible with namelist read. The 1307output has all names in upper case and indentation to column 1 after the 1308namelist name. Two extensions are permitted: 1309 1310Old-style use of @samp{$} instead of @samp{&} 1311@smallexample 1312$MYNML 1313 X(:)%Y(2) = 1.0 2.0 3.0 1314 CH(1:4) = "abcd" 1315$END 1316@end smallexample 1317 1318It should be noted that the default terminator is @samp{/} rather than 1319@samp{&END}. 1320 1321Querying of the namelist when inputting from stdin. After at least 1322one space, entering @samp{?} sends to stdout the namelist name and the names of 1323the variables in the namelist: 1324@smallexample 1325 ? 1326 1327&mynml 1328 x 1329 x%y 1330 ch 1331&end 1332@end smallexample 1333 1334Entering @samp{=?} outputs the namelist to stdout, as if 1335@code{WRITE(*,NML = mynml)} had been called: 1336@smallexample 1337=? 1338 1339&MYNML 1340 X(1)%Y= 0.000000 , 1.000000 , 0.000000 , 1341 X(2)%Y= 0.000000 , 2.000000 , 0.000000 , 1342 X(3)%Y= 0.000000 , 3.000000 , 0.000000 , 1343 CH=abcd, / 1344@end smallexample 1345 1346To aid this dialog, when input is from stdin, errors send their 1347messages to stderr and execution continues, even if @code{IOSTAT} is set. 1348 1349@code{PRINT} namelist is permitted. This causes an error if 1350@option{-std=f95} is used. 1351@smallexample 1352PROGRAM test_print 1353 REAL, dimension (4) :: x = (/1.0, 2.0, 3.0, 4.0/) 1354 NAMELIST /mynml/ x 1355 PRINT mynml 1356END PROGRAM test_print 1357@end smallexample 1358 1359Expanded namelist reads are permitted. This causes an error if 1360@option{-std=f95} is used. In the following example, the first element 1361of the array will be given the value 0.00 and the two succeeding 1362elements will be given the values 1.00 and 2.00. 1363@smallexample 1364&MYNML 1365 X(1,1) = 0.00 , 1.00 , 2.00 1366/ 1367@end smallexample 1368 1369When writing a namelist, if no @code{DELIM=} is specified, by default a 1370double quote is used to delimit character strings. If -std=F95, F2003, 1371or F2008, etc, the delim status is set to 'none'. Defaulting to 1372quotes ensures that namelists with character strings can be subsequently 1373read back in accurately. 1374 1375@node X format descriptor without count field 1376@subsection @code{X} format descriptor without count field 1377 1378To support legacy codes, GNU Fortran permits the count field of the 1379@code{X} edit descriptor in @code{FORMAT} statements to be omitted. 1380When omitted, the count is implicitly assumed to be one. 1381 1382@smallexample 1383 PRINT 10, 2, 3 138410 FORMAT (I1, X, I1) 1385@end smallexample 1386 1387@node Commas in FORMAT specifications 1388@subsection Commas in @code{FORMAT} specifications 1389 1390To support legacy codes, GNU Fortran allows the comma separator 1391to be omitted immediately before and after character string edit 1392descriptors in @code{FORMAT} statements. A comma with no following format 1393decriptor is permited if the @option{-fdec-blank-format-item} is given on 1394the command line. This is considered non-conforming code and is 1395discouraged. 1396 1397@smallexample 1398 PRINT 10, 2, 3 139910 FORMAT ('FOO='I1' BAR='I2) 1400 print 20, 5, 6 140120 FORMAT (I3, I3,) 1402@end smallexample 1403 1404 1405@node Missing period in FORMAT specifications 1406@subsection Missing period in @code{FORMAT} specifications 1407 1408To support legacy codes, GNU Fortran allows missing periods in format 1409specifications if and only if @option{-std=legacy} is given on the 1410command line. This is considered non-conforming code and is 1411discouraged. 1412 1413@smallexample 1414 REAL :: value 1415 READ(*,10) value 141610 FORMAT ('F4') 1417@end smallexample 1418 1419@node Default widths for F@comma{} G and I format descriptors 1420@subsection Default widths for @code{F}, @code{G} and @code{I} format descriptors 1421 1422To support legacy codes, GNU Fortran allows width to be omitted from format 1423specifications if and only if @option{-fdec-format-defaults} is given on the 1424command line. Default widths will be used. This is considered non-conforming 1425code and is discouraged. 1426 1427@smallexample 1428 REAL :: value1 1429 INTEGER :: value2 1430 WRITE(*,10) value1, value1, value2 143110 FORMAT ('F, G, I') 1432@end smallexample 1433 1434 1435@node I/O item lists 1436@subsection I/O item lists 1437@cindex I/O item lists 1438 1439To support legacy codes, GNU Fortran allows the input item list 1440of the @code{READ} statement, and the output item lists of the 1441@code{WRITE} and @code{PRINT} statements, to start with a comma. 1442 1443@node @code{Q} exponent-letter 1444@subsection @code{Q} exponent-letter 1445@cindex @code{Q} exponent-letter 1446 1447GNU Fortran accepts real literal constants with an exponent-letter 1448of @code{Q}, for example, @code{1.23Q45}. The constant is interpreted 1449as a @code{REAL(16)} entity on targets that support this type. If 1450the target does not support @code{REAL(16)} but has a @code{REAL(10)} 1451type, then the real-literal-constant will be interpreted as a 1452@code{REAL(10)} entity. In the absence of @code{REAL(16)} and 1453@code{REAL(10)}, an error will occur. 1454 1455@node BOZ literal constants 1456@subsection BOZ literal constants 1457@cindex BOZ literal constants 1458 1459Besides decimal constants, Fortran also supports binary (@code{b}), 1460octal (@code{o}) and hexadecimal (@code{z}) integer constants. The 1461syntax is: @samp{prefix quote digits quote}, where the prefix is 1462either @code{b}, @code{o} or @code{z}, quote is either @code{'} or 1463@code{"} and the digits are @code{0} or @code{1} for binary, 1464between @code{0} and @code{7} for octal, and between @code{0} and 1465@code{F} for hexadecimal. (Example: @code{b'01011101'}.) 1466 1467Up to Fortran 95, BOZ literal constants were only allowed to initialize 1468integer variables in DATA statements. Since Fortran 2003 BOZ literal 1469constants are also allowed as actual arguments to the @code{REAL}, 1470@code{DBLE}, @code{INT} and @code{CMPLX} intrinsic functions. 1471The BOZ literal constant is simply a string of bits, which is padded 1472or truncated as needed, during conversion to a numeric type. The 1473Fortran standard states that the treatment of the sign bit is processor 1474dependent. Gfortran interprets the sign bit as a user would expect. 1475 1476As a deprecated extension, GNU Fortran allows hexadecimal BOZ literal 1477constants to be specified using the @code{X} prefix. That the BOZ literal 1478constant can also be specified by adding a suffix to the string, for 1479example, @code{Z'ABC'} and @code{'ABC'X} are equivalent. Additionally, 1480as extension, BOZ literals are permitted in some contexts outside of 1481@code{DATA} and the intrinsic functions listed in the Fortran standard. 1482Use @option{-fallow-invalid-boz} to enable the extension. 1483 1484@node Real array indices 1485@subsection Real array indices 1486@cindex array, indices of type real 1487 1488As an extension, GNU Fortran allows the use of @code{REAL} expressions 1489or variables as array indices. 1490 1491@node Unary operators 1492@subsection Unary operators 1493@cindex operators, unary 1494 1495As an extension, GNU Fortran allows unary plus and unary minus operators 1496to appear as the second operand of binary arithmetic operators without 1497the need for parenthesis. 1498 1499@smallexample 1500 X = Y * -Z 1501@end smallexample 1502 1503@node Implicitly convert LOGICAL and INTEGER values 1504@subsection Implicitly convert @code{LOGICAL} and @code{INTEGER} values 1505@cindex conversion, to integer 1506@cindex conversion, to logical 1507 1508As an extension for backwards compatibility with other compilers, GNU 1509Fortran allows the implicit conversion of @code{LOGICAL} values to 1510@code{INTEGER} values and vice versa. When converting from a 1511@code{LOGICAL} to an @code{INTEGER}, @code{.FALSE.} is interpreted as 1512zero, and @code{.TRUE.} is interpreted as one. When converting from 1513@code{INTEGER} to @code{LOGICAL}, the value zero is interpreted as 1514@code{.FALSE.} and any nonzero value is interpreted as @code{.TRUE.}. 1515 1516@smallexample 1517 LOGICAL :: l 1518 l = 1 1519@end smallexample 1520@smallexample 1521 INTEGER :: i 1522 i = .TRUE. 1523@end smallexample 1524 1525However, there is no implicit conversion of @code{INTEGER} values in 1526@code{if}-statements, nor of @code{LOGICAL} or @code{INTEGER} values 1527in I/O operations. 1528 1529@node Hollerith constants support 1530@subsection Hollerith constants support 1531@cindex Hollerith constants 1532 1533GNU Fortran supports Hollerith constants in assignments, @code{DATA} 1534statements, function and subroutine arguments. A Hollerith constant is 1535written as a string of characters preceded by an integer constant 1536indicating the character count, and the letter @code{H} or 1537@code{h}, and stored in bytewise fashion in a numeric (@code{INTEGER}, 1538@code{REAL}, or @code{COMPLEX}), @code{LOGICAL} or @code{CHARACTER} variable. 1539The constant will be padded with spaces or truncated to fit the size of 1540the variable in which it is stored. 1541 1542Examples of valid uses of Hollerith constants: 1543@smallexample 1544 complex*16 x(2) 1545 data x /16Habcdefghijklmnop, 16Hqrstuvwxyz012345/ 1546 x(1) = 16HABCDEFGHIJKLMNOP 1547 call foo (4h abc) 1548@end smallexample 1549 1550Examples of Hollerith constants: 1551@smallexample 1552 integer*4 a 1553 a = 0H ! Invalid, at least one character is needed. 1554 a = 4HAB12 ! Valid 1555 a = 8H12345678 ! Valid, but the Hollerith constant will be truncated. 1556 a = 3Hxyz ! Valid, but the Hollerith constant will be padded. 1557@end smallexample 1558 1559In general, Hollerith constants were used to provide a rudimentary 1560facility for handling character strings in early Fortran compilers, 1561prior to the introduction of @code{CHARACTER} variables in Fortran 77; 1562in those cases, the standard-compliant equivalent is to convert the 1563program to use proper character strings. On occasion, there may be a 1564case where the intent is specifically to initialize a numeric variable 1565with a given byte sequence. In these cases, the same result can be 1566obtained by using the @code{TRANSFER} statement, as in this example. 1567@smallexample 1568 integer(kind=4) :: a 1569 a = transfer ("abcd", a) ! equivalent to: a = 4Habcd 1570@end smallexample 1571 1572The use of the @option{-fdec} option extends support of Hollerith constants 1573to comparisons: 1574@smallexample 1575 integer*4 a 1576 a = 4hABCD 1577 if (a .ne. 4habcd) then 1578 write(*,*) "no match" 1579 end if 1580@end smallexample 1581 1582Supported types are numeric (@code{INTEGER}, @code{REAL}, or @code{COMPLEX}), 1583and @code{CHARACTER}. 1584 1585@node Character conversion 1586@subsection Character conversion 1587@cindex conversion, to character 1588 1589Allowing character literals to be used in a similar way to Hollerith constants 1590is a non-standard extension. This feature is enabled using 1591-fdec-char-conversions and only applies to character literals of @code{kind=1}. 1592 1593Character literals can be used in @code{DATA} statements and assignments with 1594numeric (@code{INTEGER}, @code{REAL}, or @code{COMPLEX}) or @code{LOGICAL} 1595variables. Like Hollerith constants they are copied byte-wise fashion. The 1596constant will be padded with spaces or truncated to fit the size of the 1597variable in which it is stored. 1598 1599Examples: 1600@smallexample 1601 integer*4 x 1602 data x / 'abcd' / 1603 1604 x = 'A' ! Will be padded. 1605 x = 'ab1234' ! Will be truncated. 1606@end smallexample 1607 1608 1609@node Cray pointers 1610@subsection Cray pointers 1611@cindex pointer, Cray 1612 1613Cray pointers are part of a non-standard extension that provides a 1614C-like pointer in Fortran. This is accomplished through a pair of 1615variables: an integer "pointer" that holds a memory address, and a 1616"pointee" that is used to dereference the pointer. 1617 1618Pointer/pointee pairs are declared in statements of the form: 1619@smallexample 1620 pointer ( <pointer> , <pointee> ) 1621@end smallexample 1622or, 1623@smallexample 1624 pointer ( <pointer1> , <pointee1> ), ( <pointer2> , <pointee2> ), ... 1625@end smallexample 1626The pointer is an integer that is intended to hold a memory address. 1627The pointee may be an array or scalar. 1628If an assumed-size array is permitted within the scoping unit, a 1629pointee can be an assumed-size array. 1630That is, the last dimension may be left unspecified by using a @code{*} 1631in place of a value. A pointee cannot be an assumed shape array. 1632No space is allocated for the pointee. 1633 1634The pointee may have its type declared before or after the pointer 1635statement, and its array specification (if any) may be declared 1636before, during, or after the pointer statement. The pointer may be 1637declared as an integer prior to the pointer statement. However, some 1638machines have default integer sizes that are different than the size 1639of a pointer, and so the following code is not portable: 1640@smallexample 1641 integer ipt 1642 pointer (ipt, iarr) 1643@end smallexample 1644If a pointer is declared with a kind that is too small, the compiler 1645will issue a warning; the resulting binary will probably not work 1646correctly, because the memory addresses stored in the pointers may be 1647truncated. It is safer to omit the first line of the above example; 1648if explicit declaration of ipt's type is omitted, then the compiler 1649will ensure that ipt is an integer variable large enough to hold a 1650pointer. 1651 1652Pointer arithmetic is valid with Cray pointers, but it is not the same 1653as C pointer arithmetic. Cray pointers are just ordinary integers, so 1654the user is responsible for determining how many bytes to add to a 1655pointer in order to increment it. Consider the following example: 1656@smallexample 1657 real target(10) 1658 real pointee(10) 1659 pointer (ipt, pointee) 1660 ipt = loc (target) 1661 ipt = ipt + 1 1662@end smallexample 1663The last statement does not set @code{ipt} to the address of 1664@code{target(1)}, as it would in C pointer arithmetic. Adding @code{1} 1665to @code{ipt} just adds one byte to the address stored in @code{ipt}. 1666 1667Any expression involving the pointee will be translated to use the 1668value stored in the pointer as the base address. 1669 1670To get the address of elements, this extension provides an intrinsic 1671function @code{LOC()}. The @code{LOC()} function is equivalent to the 1672@code{&} operator in C, except the address is cast to an integer type: 1673@smallexample 1674 real ar(10) 1675 pointer(ipt, arpte(10)) 1676 real arpte 1677 ipt = loc(ar) ! Makes arpte is an alias for ar 1678 arpte(1) = 1.0 ! Sets ar(1) to 1.0 1679@end smallexample 1680The pointer can also be set by a call to the @code{MALLOC} intrinsic 1681(see @ref{MALLOC}). 1682 1683Cray pointees often are used to alias an existing variable. For 1684example: 1685@smallexample 1686 integer target(10) 1687 integer iarr(10) 1688 pointer (ipt, iarr) 1689 ipt = loc(target) 1690@end smallexample 1691As long as @code{ipt} remains unchanged, @code{iarr} is now an alias for 1692@code{target}. The optimizer, however, will not detect this aliasing, so 1693it is unsafe to use @code{iarr} and @code{target} simultaneously. Using 1694a pointee in any way that violates the Fortran aliasing rules or 1695assumptions is illegal. It is the user's responsibility to avoid doing 1696this; the compiler works under the assumption that no such aliasing 1697occurs. 1698 1699Cray pointers will work correctly when there is no aliasing (i.e., when 1700they are used to access a dynamically allocated block of memory), and 1701also in any routine where a pointee is used, but any variable with which 1702it shares storage is not used. Code that violates these rules may not 1703run as the user intends. This is not a bug in the optimizer; any code 1704that violates the aliasing rules is illegal. (Note that this is not 1705unique to GNU Fortran; any Fortran compiler that supports Cray pointers 1706will ``incorrectly'' optimize code with illegal aliasing.) 1707 1708There are a number of restrictions on the attributes that can be applied 1709to Cray pointers and pointees. Pointees may not have the 1710@code{ALLOCATABLE}, @code{INTENT}, @code{OPTIONAL}, @code{DUMMY}, 1711@code{TARGET}, @code{INTRINSIC}, or @code{POINTER} attributes. Pointers 1712may not have the @code{DIMENSION}, @code{POINTER}, @code{TARGET}, 1713@code{ALLOCATABLE}, @code{EXTERNAL}, or @code{INTRINSIC} attributes, nor 1714may they be function results. Pointees may not occur in more than one 1715pointer statement. A pointee cannot be a pointer. Pointees cannot occur 1716in equivalence, common, or data statements. 1717 1718A Cray pointer may also point to a function or a subroutine. For 1719example, the following excerpt is valid: 1720@smallexample 1721 implicit none 1722 external sub 1723 pointer (subptr,subpte) 1724 external subpte 1725 subptr = loc(sub) 1726 call subpte() 1727 [...] 1728 subroutine sub 1729 [...] 1730 end subroutine sub 1731@end smallexample 1732 1733A pointer may be modified during the course of a program, and this 1734will change the location to which the pointee refers. However, when 1735pointees are passed as arguments, they are treated as ordinary 1736variables in the invoked function. Subsequent changes to the pointer 1737will not change the base address of the array that was passed. 1738 1739@node CONVERT specifier 1740@subsection @code{CONVERT} specifier 1741@cindex @code{CONVERT} specifier 1742 1743GNU Fortran allows the conversion of unformatted data between little- 1744and big-endian representation to facilitate moving of data 1745between different systems. The conversion can be indicated with 1746the @code{CONVERT} specifier on the @code{OPEN} statement. 1747@xref{GFORTRAN_CONVERT_UNIT}, for an alternative way of specifying 1748the data format via an environment variable. 1749 1750Valid values for @code{CONVERT} on most systems are: 1751@itemize @w{} 1752@item @code{CONVERT='NATIVE'} Use the native format. This is the default. 1753@item @code{CONVERT='SWAP'} Swap between little- and big-endian. 1754@item @code{CONVERT='LITTLE_ENDIAN'} Use the little-endian representation 1755for unformatted files. 1756@item @code{CONVERT='BIG_ENDIAN'} Use the big-endian representation for 1757unformatted files. 1758@end itemize 1759On POWER systems which support @option{-mabi=ieeelongdouble}, 1760there are additional options, which can be combined with the others 1761with commas. Those are 1762@itemize @w{} 1763@item @code{CONVERT='R16_IEEE'} Use IEEE 128-bit format for 1764@code{REAL(KIND=16)}. 1765@item @code{CONVERT='R16_IBM'} Use IBM @code{long double} format for 1766real@code{REAL(KIND=16)}. 1767@end itemize 1768 1769Using the option could look like this: 1770@smallexample 1771 open(file='big.dat',form='unformatted',access='sequential', & 1772 convert='big_endian') 1773@end smallexample 1774 1775The value of the conversion can be queried by using 1776@code{INQUIRE(CONVERT=ch)}. The values returned are 1777@code{'BIG_ENDIAN'} and @code{'LITTLE_ENDIAN'}. 1778 1779@code{CONVERT} works between big- and little-endian for 1780@code{INTEGER} values of all supported kinds and for @code{REAL} 1781on IEEE systems of kinds 4 and 8. Conversion between different 1782``extended double'' types on different architectures such as 1783m68k and x86_64, which GNU Fortran 1784supports as @code{REAL(KIND=10)} and @code{REAL(KIND=16)}, will 1785probably not work. 1786 1787@emph{Note that the values specified via the GFORTRAN_CONVERT_UNIT 1788environment variable will override the CONVERT specifier in the 1789open statement}. This is to give control over data formats to 1790users who do not have the source code of their program available. 1791 1792Using anything but the native representation for unformatted data 1793carries a significant speed overhead. If speed in this area matters 1794to you, it is best if you use this only for data that needs to be 1795portable. 1796 1797@node OpenMP 1798@subsection OpenMP 1799@cindex OpenMP 1800 1801OpenMP (Open Multi-Processing) is an application programming 1802interface (API) that supports multi-platform shared memory 1803multiprocessing programming in C/C++ and Fortran on many 1804architectures, including Unix and Microsoft Windows platforms. 1805It consists of a set of compiler directives, library routines, 1806and environment variables that influence run-time behavior. 1807 1808GNU Fortran strives to be compatible to the 1809@uref{https://openmp.org/wp/openmp-specifications/, 1810OpenMP Application Program Interface v4.5}. 1811 1812To enable the processing of the OpenMP directive @code{!$omp} in 1813free-form source code; the @code{c$omp}, @code{*$omp} and @code{!$omp} 1814directives in fixed form; the @code{!$} conditional compilation sentinels 1815in free form; and the @code{c$}, @code{*$} and @code{!$} sentinels 1816in fixed form, @command{gfortran} needs to be invoked with the 1817@option{-fopenmp}. This also arranges for automatic linking of the 1818GNU Offloading and Multi Processing Runtime Library 1819@ref{Top,,libgomp,libgomp,GNU Offloading and Multi Processing Runtime 1820Library}. 1821 1822The OpenMP Fortran runtime library routines are provided both in a 1823form of a Fortran 90 module named @code{omp_lib} and in a form of 1824a Fortran @code{include} file named @file{omp_lib.h}. 1825 1826An example of a parallelized loop taken from Appendix A.1 of 1827the OpenMP Application Program Interface v2.5: 1828@smallexample 1829SUBROUTINE A1(N, A, B) 1830 INTEGER I, N 1831 REAL B(N), A(N) 1832!$OMP PARALLEL DO !I is private by default 1833 DO I=2,N 1834 B(I) = (A(I) + A(I-1)) / 2.0 1835 ENDDO 1836!$OMP END PARALLEL DO 1837END SUBROUTINE A1 1838@end smallexample 1839 1840Please note: 1841@itemize 1842@item 1843@option{-fopenmp} implies @option{-frecursive}, i.e., all local arrays 1844will be allocated on the stack. When porting existing code to OpenMP, 1845this may lead to surprising results, especially to segmentation faults 1846if the stacksize is limited. 1847 1848@item 1849On glibc-based systems, OpenMP enabled applications cannot be statically 1850linked due to limitations of the underlying pthreads-implementation. It 1851might be possible to get a working solution if 1852@command{-Wl,--whole-archive -lpthread -Wl,--no-whole-archive} is added 1853to the command line. However, this is not supported by @command{gcc} and 1854thus not recommended. 1855@end itemize 1856 1857@node OpenACC 1858@subsection OpenACC 1859@cindex OpenACC 1860 1861OpenACC is an application programming interface (API) that supports 1862offloading of code to accelerator devices. It consists of a set of 1863compiler directives, library routines, and environment variables that 1864influence run-time behavior. 1865 1866GNU Fortran strives to be compatible to the 1867@uref{https://www.openacc.org/, OpenACC Application Programming 1868Interface v2.6}. 1869 1870To enable the processing of the OpenACC directive @code{!$acc} in 1871free-form source code; the @code{c$acc}, @code{*$acc} and @code{!$acc} 1872directives in fixed form; the @code{!$} conditional compilation 1873sentinels in free form; and the @code{c$}, @code{*$} and @code{!$} 1874sentinels in fixed form, @command{gfortran} needs to be invoked with 1875the @option{-fopenacc}. This also arranges for automatic linking of 1876the GNU Offloading and Multi Processing Runtime Library 1877@ref{Top,,libgomp,libgomp,GNU Offloading and Multi Processing Runtime 1878Library}. 1879 1880The OpenACC Fortran runtime library routines are provided both in a 1881form of a Fortran 90 module named @code{openacc} and in a form of a 1882Fortran @code{include} file named @file{openacc_lib.h}. 1883 1884@node Argument list functions 1885@subsection Argument list functions @code{%VAL}, @code{%REF} and @code{%LOC} 1886@cindex argument list functions 1887@cindex @code{%VAL} 1888@cindex @code{%REF} 1889@cindex @code{%LOC} 1890 1891GNU Fortran supports argument list functions @code{%VAL}, @code{%REF} 1892and @code{%LOC} statements, for backward compatibility with g77. 1893It is recommended that these should be used only for code that is 1894accessing facilities outside of GNU Fortran, such as operating system 1895or windowing facilities. It is best to constrain such uses to isolated 1896portions of a program--portions that deal specifically and exclusively 1897with low-level, system-dependent facilities. Such portions might well 1898provide a portable interface for use by the program as a whole, but are 1899themselves not portable, and should be thoroughly tested each time they 1900are rebuilt using a new compiler or version of a compiler. 1901 1902@code{%VAL} passes a scalar argument by value, @code{%REF} passes it by 1903reference and @code{%LOC} passes its memory location. Since gfortran 1904already passes scalar arguments by reference, @code{%REF} is in effect 1905a do-nothing. @code{%LOC} has the same effect as a Fortran pointer. 1906 1907An example of passing an argument by value to a C subroutine foo.: 1908@smallexample 1909C 1910C prototype void foo_ (float x); 1911C 1912 external foo 1913 real*4 x 1914 x = 3.14159 1915 call foo (%VAL (x)) 1916 end 1917@end smallexample 1918 1919For details refer to the g77 manual 1920@uref{https://gcc.gnu.org/@/onlinedocs/@/gcc-3.4.6/@/g77/@/index.html#Top}. 1921 1922Also, @code{c_by_val.f} and its partner @code{c_by_val.c} of the 1923GNU Fortran testsuite are worth a look. 1924 1925@node Read/Write after EOF marker 1926@subsection Read/Write after EOF marker 1927@cindex @code{EOF} 1928@cindex @code{BACKSPACE} 1929@cindex @code{REWIND} 1930 1931Some legacy codes rely on allowing @code{READ} or @code{WRITE} after the 1932EOF file marker in order to find the end of a file. GNU Fortran normally 1933rejects these codes with a run-time error message and suggests the user 1934consider @code{BACKSPACE} or @code{REWIND} to properly position 1935the file before the EOF marker. As an extension, the run-time error may 1936be disabled using -std=legacy. 1937 1938 1939@node STRUCTURE and RECORD 1940@subsection @code{STRUCTURE} and @code{RECORD} 1941@cindex @code{STRUCTURE} 1942@cindex @code{RECORD} 1943 1944Record structures are a pre-Fortran-90 vendor extension to create 1945user-defined aggregate data types. Support for record structures in GNU 1946Fortran can be enabled with the @option{-fdec-structure} compile flag. 1947If you have a choice, you should instead use Fortran 90's ``derived types'', 1948which have a different syntax. 1949 1950In many cases, record structures can easily be converted to derived types. 1951To convert, replace @code{STRUCTURE /}@var{structure-name}@code{/} 1952by @code{TYPE} @var{type-name}. Additionally, replace 1953@code{RECORD /}@var{structure-name}@code{/} by 1954@code{TYPE(}@var{type-name}@code{)}. Finally, in the component access, 1955replace the period (@code{.}) by the percent sign (@code{%}). 1956 1957Here is an example of code using the non portable record structure syntax: 1958 1959@example 1960! Declaring a structure named ``item'' and containing three fields: 1961! an integer ID, an description string and a floating-point price. 1962STRUCTURE /item/ 1963 INTEGER id 1964 CHARACTER(LEN=200) description 1965 REAL price 1966END STRUCTURE 1967 1968! Define two variables, an single record of type ``item'' 1969! named ``pear'', and an array of items named ``store_catalog'' 1970RECORD /item/ pear, store_catalog(100) 1971 1972! We can directly access the fields of both variables 1973pear.id = 92316 1974pear.description = "juicy D'Anjou pear" 1975pear.price = 0.15 1976store_catalog(7).id = 7831 1977store_catalog(7).description = "milk bottle" 1978store_catalog(7).price = 1.2 1979 1980! We can also manipulate the whole structure 1981store_catalog(12) = pear 1982print *, store_catalog(12) 1983@end example 1984 1985@noindent 1986This code can easily be rewritten in the Fortran 90 syntax as following: 1987 1988@example 1989! ``STRUCTURE /name/ ... END STRUCTURE'' becomes 1990! ``TYPE name ... END TYPE'' 1991TYPE item 1992 INTEGER id 1993 CHARACTER(LEN=200) description 1994 REAL price 1995END TYPE 1996 1997! ``RECORD /name/ variable'' becomes ``TYPE(name) variable'' 1998TYPE(item) pear, store_catalog(100) 1999 2000! Instead of using a dot (.) to access fields of a record, the 2001! standard syntax uses a percent sign (%) 2002pear%id = 92316 2003pear%description = "juicy D'Anjou pear" 2004pear%price = 0.15 2005store_catalog(7)%id = 7831 2006store_catalog(7)%description = "milk bottle" 2007store_catalog(7)%price = 1.2 2008 2009! Assignments of a whole variable do not change 2010store_catalog(12) = pear 2011print *, store_catalog(12) 2012@end example 2013 2014@noindent 2015GNU Fortran implements STRUCTURES like derived types with the following 2016rules and exceptions: 2017 2018@itemize @bullet 2019@item Structures act like derived types with the @code{SEQUENCE} attribute. 2020Otherwise they may contain no specifiers. 2021 2022@item Structures may contain a special field with the name @code{%FILL}. 2023This will create an anonymous component which cannot be accessed but occupies 2024space just as if a component of the same type was declared in its place, useful 2025for alignment purposes. As an example, the following structure will consist 2026of at least sixteen bytes: 2027 2028@smallexample 2029structure /padded/ 2030 character(4) start 2031 character(8) %FILL 2032 character(4) end 2033end structure 2034@end smallexample 2035 2036@item Structures may share names with other symbols. For example, the following 2037is invalid for derived types, but valid for structures: 2038 2039@smallexample 2040structure /header/ 2041 ! ... 2042end structure 2043record /header/ header 2044@end smallexample 2045 2046@item Structure types may be declared nested within another parent structure. 2047The syntax is: 2048@smallexample 2049structure /type-name/ 2050 ... 2051 structure [/<type-name>/] <field-list> 2052... 2053@end smallexample 2054 2055The type name may be ommitted, in which case the structure type itself is 2056anonymous, and other structures of the same type cannot be instantiated. The 2057following shows some examples: 2058 2059@example 2060structure /appointment/ 2061 ! nested structure definition: app_time is an array of two 'time' 2062 structure /time/ app_time (2) 2063 integer(1) hour, minute 2064 end structure 2065 character(10) memo 2066end structure 2067 2068! The 'time' structure is still usable 2069record /time/ now 2070now = time(5, 30) 2071 2072... 2073 2074structure /appointment/ 2075 ! anonymous nested structure definition 2076 structure start, end 2077 integer(1) hour, minute 2078 end structure 2079 character(10) memo 2080end structure 2081@end example 2082 2083@item Structures may contain @code{UNION} blocks. For more detail see the 2084section on @ref{UNION and MAP}. 2085 2086@item Structures support old-style initialization of components, like 2087those described in @ref{Old-style variable initialization}. For array 2088initializers, an initializer may contain a repeat specification of the form 2089@code{<literal-integer> * <constant-initializer>}. The value of the integer 2090indicates the number of times to repeat the constant initializer when expanding 2091the initializer list. 2092@end itemize 2093 2094@node UNION and MAP 2095@subsection @code{UNION} and @code{MAP} 2096@cindex @code{UNION} 2097@cindex @code{MAP} 2098 2099Unions are an old vendor extension which were commonly used with the 2100non-standard @ref{STRUCTURE and RECORD} extensions. Use of @code{UNION} and 2101@code{MAP} is automatically enabled with @option{-fdec-structure}. 2102 2103A @code{UNION} declaration occurs within a structure; within the definition of 2104each union is a number of @code{MAP} blocks. Each @code{MAP} shares storage 2105with its sibling maps (in the same union), and the size of the union is the 2106size of the largest map within it, just as with unions in C. The major 2107difference is that component references do not indicate which union or map the 2108component is in (the compiler gets to figure that out). 2109 2110Here is a small example: 2111@smallexample 2112structure /myunion/ 2113union 2114 map 2115 character(2) w0, w1, w2 2116 end map 2117 map 2118 character(6) long 2119 end map 2120end union 2121end structure 2122 2123record /myunion/ rec 2124! After this assignment... 2125rec.long = 'hello!' 2126 2127! The following is true: 2128! rec.w0 === 'he' 2129! rec.w1 === 'll' 2130! rec.w2 === 'o!' 2131@end smallexample 2132 2133The two maps share memory, and the size of the union is ultimately six bytes: 2134 2135@example 21360 1 2 3 4 5 6 Byte offset 2137------------------------------- 2138| | | | | | | 2139------------------------------- 2140 2141^ W0 ^ W1 ^ W2 ^ 2142 \-------/ \-------/ \-------/ 2143 2144^ LONG ^ 2145 \---------------------------/ 2146@end example 2147 2148Following is an example mirroring the layout of an Intel x86_64 register: 2149 2150@example 2151structure /reg/ 2152 union ! U0 ! rax 2153 map 2154 character(16) rx 2155 end map 2156 map 2157 character(8) rh ! rah 2158 union ! U1 2159 map 2160 character(8) rl ! ral 2161 end map 2162 map 2163 character(8) ex ! eax 2164 end map 2165 map 2166 character(4) eh ! eah 2167 union ! U2 2168 map 2169 character(4) el ! eal 2170 end map 2171 map 2172 character(4) x ! ax 2173 end map 2174 map 2175 character(2) h ! ah 2176 character(2) l ! al 2177 end map 2178 end union 2179 end map 2180 end union 2181 end map 2182 end union 2183end structure 2184record /reg/ a 2185 2186! After this assignment... 2187a.rx = 'AAAAAAAA.BBB.C.D' 2188 2189! The following is true: 2190a.rx === 'AAAAAAAA.BBB.C.D' 2191a.rh === 'AAAAAAAA' 2192a.rl === '.BBB.C.D' 2193a.ex === '.BBB.C.D' 2194a.eh === '.BBB' 2195a.el === '.C.D' 2196a.x === '.C.D' 2197a.h === '.C' 2198a.l === '.D' 2199@end example 2200 2201@node Type variants for integer intrinsics 2202@subsection Type variants for integer intrinsics 2203@cindex intrinsics, integer 2204 2205Similar to the D/C prefixes to real functions to specify the input/output 2206types, GNU Fortran offers B/I/J/K prefixes to integer functions for 2207compatibility with DEC programs. The types implied by each are: 2208 2209@example 2210@code{B} - @code{INTEGER(kind=1)} 2211@code{I} - @code{INTEGER(kind=2)} 2212@code{J} - @code{INTEGER(kind=4)} 2213@code{K} - @code{INTEGER(kind=8)} 2214@end example 2215 2216GNU Fortran supports these with the flag @option{-fdec-intrinsic-ints}. 2217Intrinsics for which prefixed versions are available and in what form are noted 2218in @ref{Intrinsic Procedures}. The complete list of supported intrinsics is 2219here: 2220 2221@multitable @columnfractions .2 .2 .2 .2 .2 2222 2223@headitem Intrinsic @tab B @tab I @tab J @tab K 2224 2225@item @code{@ref{ABS}} 2226 @tab @code{BABS} @tab @code{IIABS} @tab @code{JIABS} @tab @code{KIABS} 2227@item @code{@ref{BTEST}} 2228 @tab @code{BBTEST} @tab @code{BITEST} @tab @code{BJTEST} @tab @code{BKTEST} 2229@item @code{@ref{IAND}} 2230 @tab @code{BIAND} @tab @code{IIAND} @tab @code{JIAND} @tab @code{KIAND} 2231@item @code{@ref{IBCLR}} 2232 @tab @code{BBCLR} @tab @code{IIBCLR} @tab @code{JIBCLR} @tab @code{KIBCLR} 2233@item @code{@ref{IBITS}} 2234 @tab @code{BBITS} @tab @code{IIBITS} @tab @code{JIBITS} @tab @code{KIBITS} 2235@item @code{@ref{IBSET}} 2236 @tab @code{BBSET} @tab @code{IIBSET} @tab @code{JIBSET} @tab @code{KIBSET} 2237@item @code{@ref{IEOR}} 2238 @tab @code{BIEOR} @tab @code{IIEOR} @tab @code{JIEOR} @tab @code{KIEOR} 2239@item @code{@ref{IOR}} 2240 @tab @code{BIOR} @tab @code{IIOR} @tab @code{JIOR} @tab @code{KIOR} 2241@item @code{@ref{ISHFT}} 2242 @tab @code{BSHFT} @tab @code{IISHFT} @tab @code{JISHFT} @tab @code{KISHFT} 2243@item @code{@ref{ISHFTC}} 2244 @tab @code{BSHFTC} @tab @code{IISHFTC} @tab @code{JISHFTC} @tab @code{KISHFTC} 2245@item @code{@ref{MOD}} 2246 @tab @code{BMOD} @tab @code{IMOD} @tab @code{JMOD} @tab @code{KMOD} 2247@item @code{@ref{NOT}} 2248 @tab @code{BNOT} @tab @code{INOT} @tab @code{JNOT} @tab @code{KNOT} 2249@item @code{@ref{REAL}} 2250 @tab @code{--} @tab @code{FLOATI} @tab @code{FLOATJ} @tab @code{FLOATK} 2251@end multitable 2252 2253@node AUTOMATIC and STATIC attributes 2254@subsection @code{AUTOMATIC} and @code{STATIC} attributes 2255@cindex variable attributes 2256@cindex @code{AUTOMATIC} 2257@cindex @code{STATIC} 2258 2259With @option{-fdec-static} GNU Fortran supports the DEC extended attributes 2260@code{STATIC} and @code{AUTOMATIC} to provide explicit specification of entity 2261storage. These follow the syntax of the Fortran standard @code{SAVE} attribute. 2262 2263@code{STATIC} is exactly equivalent to @code{SAVE}, and specifies that 2264an entity should be allocated in static memory. As an example, @code{STATIC} 2265local variables will retain their values across multiple calls to a function. 2266 2267Entities marked @code{AUTOMATIC} will be stack automatic whenever possible. 2268@code{AUTOMATIC} is the default for local variables smaller than 2269@option{-fmax-stack-var-size}, unless @option{-fno-automatic} is given. This 2270attribute overrides @option{-fno-automatic}, @option{-fmax-stack-var-size}, and 2271blanket @code{SAVE} statements. 2272 2273 2274Examples: 2275 2276@example 2277subroutine f 2278 integer, automatic :: i ! automatic variable 2279 integer x, y ! static variables 2280 save 2281 ... 2282endsubroutine 2283@end example 2284@example 2285subroutine f 2286 integer a, b, c, x, y, z 2287 static :: x 2288 save y 2289 automatic z, c 2290 ! a, b, c, and z are automatic 2291 ! x and y are static 2292endsubroutine 2293@end example 2294@example 2295! Compiled with -fno-automatic 2296subroutine f 2297 integer a, b, c, d 2298 automatic :: a 2299 ! a is automatic; b, c, and d are static 2300endsubroutine 2301@end example 2302 2303@node Extended math intrinsics 2304@subsection Extended math intrinsics 2305@cindex intrinsics, math 2306@cindex intrinsics, trigonometric functions 2307 2308GNU Fortran supports an extended list of mathematical intrinsics with the 2309compile flag @option{-fdec-math} for compatability with legacy code. 2310These intrinsics are described fully in @ref{Intrinsic Procedures} where it is 2311noted that they are extensions and should be avoided whenever possible. 2312 2313Specifically, @option{-fdec-math} enables the @ref{COTAN} intrinsic, and 2314trigonometric intrinsics which accept or produce values in degrees instead of 2315radians. Here is a summary of the new intrinsics: 2316 2317@multitable @columnfractions .5 .5 2318@headitem Radians @tab Degrees 2319@item @code{@ref{ACOS}} @tab @code{@ref{ACOSD}}* 2320@item @code{@ref{ASIN}} @tab @code{@ref{ASIND}}* 2321@item @code{@ref{ATAN}} @tab @code{@ref{ATAND}}* 2322@item @code{@ref{ATAN2}} @tab @code{@ref{ATAN2D}}* 2323@item @code{@ref{COS}} @tab @code{@ref{COSD}}* 2324@item @code{@ref{COTAN}}* @tab @code{@ref{COTAND}}* 2325@item @code{@ref{SIN}} @tab @code{@ref{SIND}}* 2326@item @code{@ref{TAN}} @tab @code{@ref{TAND}}* 2327@end multitable 2328 2329* Enabled with @option{-fdec-math}. 2330 2331For advanced users, it may be important to know the implementation of these 2332functions. They are simply wrappers around the standard radian functions, which 2333have more accurate builtin versions. These functions convert their arguments 2334(or results) to degrees (or radians) by taking the value modulus 360 (or 2*pi) 2335and then multiplying it by a constant radian-to-degree (or degree-to-radian) 2336factor, as appropriate. The factor is computed at compile-time as 180/pi (or 2337pi/180). 2338 2339@node Form feed as whitespace 2340@subsection Form feed as whitespace 2341@cindex form feed whitespace 2342 2343Historically, legacy compilers allowed insertion of form feed characters ('\f', 2344ASCII 0xC) at the beginning of lines for formatted output to line printers, 2345though the Fortran standard does not mention this. GNU Fortran supports the 2346interpretation of form feed characters in source as whitespace for 2347compatibility. 2348 2349@node TYPE as an alias for PRINT 2350@subsection TYPE as an alias for PRINT 2351@cindex type alias print 2352For compatibility, GNU Fortran will interpret @code{TYPE} statements as 2353@code{PRINT} statements with the flag @option{-fdec}. With this flag asserted, 2354the following two examples are equivalent: 2355 2356@smallexample 2357TYPE *, 'hello world' 2358@end smallexample 2359 2360@smallexample 2361PRINT *, 'hello world' 2362@end smallexample 2363 2364@node %LOC as an rvalue 2365@subsection %LOC as an rvalue 2366@cindex LOC 2367Normally @code{%LOC} is allowed only in parameter lists. However the intrinsic 2368function @code{LOC} does the same thing, and is usable as the right-hand-side of 2369assignments. For compatibility, GNU Fortran supports the use of @code{%LOC} as 2370an alias for the builtin @code{LOC} with @option{-std=legacy}. With this 2371feature enabled the following two examples are equivalent: 2372 2373@smallexample 2374integer :: i, l 2375l = %loc(i) 2376call sub(l) 2377@end smallexample 2378 2379@smallexample 2380integer :: i 2381call sub(%loc(i)) 2382@end smallexample 2383 2384@node .XOR. operator 2385@subsection .XOR. operator 2386@cindex operators, xor 2387 2388GNU Fortran supports @code{.XOR.} as a logical operator with @code{-std=legacy} 2389for compatibility with legacy code. @code{.XOR.} is equivalent to 2390@code{.NEQV.}. That is, the output is true if and only if the inputs differ. 2391 2392@node Bitwise logical operators 2393@subsection Bitwise logical operators 2394@cindex logical, bitwise 2395 2396With @option{-fdec}, GNU Fortran relaxes the type constraints on 2397logical operators to allow integer operands, and performs the corresponding 2398bitwise operation instead. This flag is for compatibility only, and should be 2399avoided in new code. Consider: 2400 2401@smallexample 2402 INTEGER :: i, j 2403 i = z'33' 2404 j = z'cc' 2405 print *, i .AND. j 2406@end smallexample 2407 2408In this example, compiled with @option{-fdec}, GNU Fortran will 2409replace the @code{.AND.} operation with a call to the intrinsic 2410@code{@ref{IAND}} function, yielding the bitwise-and of @code{i} and @code{j}. 2411 2412Note that this conversion will occur if at least one operand is of integral 2413type. As a result, a logical operand will be converted to an integer when the 2414other operand is an integer in a logical operation. In this case, 2415@code{.TRUE.} is converted to @code{1} and @code{.FALSE.} to @code{0}. 2416 2417Here is the mapping of logical operator to bitwise intrinsic used with 2418@option{-fdec}: 2419 2420@multitable @columnfractions .25 .25 .5 2421@headitem Operator @tab Intrinsic @tab Bitwise operation 2422@item @code{.NOT.} @tab @code{@ref{NOT}} @tab complement 2423@item @code{.AND.} @tab @code{@ref{IAND}} @tab intersection 2424@item @code{.OR.} @tab @code{@ref{IOR}} @tab union 2425@item @code{.NEQV.} @tab @code{@ref{IEOR}} @tab exclusive or 2426@item @code{.EQV.} @tab @code{@ref{NOT}(@ref{IEOR})} @tab complement of exclusive or 2427@end multitable 2428 2429@node Extended I/O specifiers 2430@subsection Extended I/O specifiers 2431@cindex @code{CARRIAGECONTROL} 2432@cindex @code{READONLY} 2433@cindex @code{SHARE} 2434@cindex @code{SHARED} 2435@cindex @code{NOSHARED} 2436@cindex I/O specifiers 2437 2438GNU Fortran supports the additional legacy I/O specifiers 2439@code{CARRIAGECONTROL}, @code{READONLY}, and @code{SHARE} with the 2440compile flag @option{-fdec}, for compatibility. 2441 2442@table @code 2443@item CARRIAGECONTROL 2444The @code{CARRIAGECONTROL} specifier allows a user to control line 2445termination settings between output records for an I/O unit. The specifier has 2446no meaning for readonly files. When @code{CARRAIGECONTROL} is specified upon 2447opening a unit for formatted writing, the exact @code{CARRIAGECONTROL} setting 2448determines what characters to write between output records. The syntax is: 2449 2450@smallexample 2451OPEN(..., CARRIAGECONTROL=cc) 2452@end smallexample 2453 2454Where @emph{cc} is a character expression that evaluates to one of the 2455following values: 2456 2457@multitable @columnfractions .2 .8 2458@item @code{'LIST'} @tab One line feed between records (default) 2459@item @code{'FORTRAN'} @tab Legacy interpretation of the first character (see below) 2460@item @code{'NONE'} @tab No separator between records 2461@end multitable 2462 2463With @code{CARRIAGECONTROL='FORTRAN'}, when a record is written, the first 2464character of the input record is not written, and instead determines the output 2465record separator as follows: 2466 2467@multitable @columnfractions .3 .3 .4 2468@headitem Leading character @tab Meaning @tab Output separating character(s) 2469@item @code{'+'} @tab Overprinting @tab Carriage return only 2470@item @code{'-'} @tab New line @tab Line feed and carriage return 2471@item @code{'0'} @tab Skip line @tab Two line feeds and carriage return 2472@item @code{'1'} @tab New page @tab Form feed and carriage return 2473@item @code{'$'} @tab Prompting @tab Line feed (no carriage return) 2474@item @code{CHAR(0)} @tab Overprinting (no advance) @tab None 2475@end multitable 2476 2477@item READONLY 2478The @code{READONLY} specifier may be given upon opening a unit, and is 2479equivalent to specifying @code{ACTION='READ'}, except that the file may not be 2480deleted on close (i.e. @code{CLOSE} with @code{STATUS="DELETE"}). The syntax 2481is: 2482 2483@smallexample 2484@code{OPEN(..., READONLY)} 2485@end smallexample 2486 2487@item SHARE 2488The @code{SHARE} specifier allows system-level locking on a unit upon opening 2489it for controlled access from multiple processes/threads. The @code{SHARE} 2490specifier has several forms: 2491 2492@smallexample 2493OPEN(..., SHARE=sh) 2494OPEN(..., SHARED) 2495OPEN(..., NOSHARED) 2496@end smallexample 2497 2498Where @emph{sh} in the first form is a character expression that evaluates to 2499a value as seen in the table below. The latter two forms are aliases 2500for particular values of @emph{sh}: 2501 2502@multitable @columnfractions .3 .3 .4 2503@headitem Explicit form @tab Short form @tab Meaning 2504@item @code{SHARE='DENYRW'} @tab @code{NOSHARED} @tab Exclusive (write) lock 2505@item @code{SHARE='DENYNONE'} @tab @code{SHARED} @tab Shared (read) lock 2506@end multitable 2507 2508In general only one process may hold an exclusive (write) lock for a given file 2509at a time, whereas many processes may hold shared (read) locks for the same 2510file. 2511 2512The behavior of locking may vary with your operating system. On POSIX systems, 2513locking is implemented with @code{fcntl}. Consult your corresponding operating 2514system's manual pages for further details. Locking via @code{SHARE=} is not 2515supported on other systems. 2516 2517@end table 2518 2519@node Legacy PARAMETER statements 2520@subsection Legacy PARAMETER statements 2521@cindex PARAMETER 2522 2523For compatibility, GNU Fortran supports legacy PARAMETER statements without 2524parentheses with @option{-std=legacy}. A warning is emitted if used with 2525@option{-std=gnu}, and an error is acknowledged with a real Fortran standard 2526flag (@option{-std=f95}, etc...). These statements take the following form: 2527 2528@smallexample 2529implicit real (E) 2530parameter e = 2.718282 2531real c 2532parameter c = 3.0e8 2533@end smallexample 2534 2535@node Default exponents 2536@subsection Default exponents 2537@cindex exponent 2538 2539For compatibility, GNU Fortran supports a default exponent of zero in real 2540constants with @option{-fdec}. For example, @code{9e} would be 2541interpreted as @code{9e0}, rather than an error. 2542 2543 2544@node Extensions not implemented in GNU Fortran 2545@section Extensions not implemented in GNU Fortran 2546@cindex extensions, not implemented 2547 2548The long history of the Fortran language, its wide use and broad 2549userbase, the large number of different compiler vendors and the lack of 2550some features crucial to users in the first standards have lead to the 2551existence of a number of important extensions to the language. While 2552some of the most useful or popular extensions are supported by the GNU 2553Fortran compiler, not all existing extensions are supported. This section 2554aims at listing these extensions and offering advice on how best make 2555code that uses them running with the GNU Fortran compiler. 2556 2557@c More can be found here: 2558@c -- https://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Missing-Features.html 2559@c -- the list of Fortran and libgfortran bugs closed as WONTFIX: 2560@c http://tinyurl.com/2u4h5y 2561 2562@menu 2563* ENCODE and DECODE statements:: 2564* Variable FORMAT expressions:: 2565@c * TYPE and ACCEPT I/O Statements:: 2566@c * DEFAULTFILE, DISPOSE and RECORDTYPE I/O specifiers:: 2567@c * Omitted arguments in procedure call:: 2568* Alternate complex function syntax:: 2569* Volatile COMMON blocks:: 2570* OPEN( ... NAME=):: 2571* Q edit descriptor:: 2572@end menu 2573 2574@node ENCODE and DECODE statements 2575@subsection @code{ENCODE} and @code{DECODE} statements 2576@cindex @code{ENCODE} 2577@cindex @code{DECODE} 2578 2579GNU Fortran does not support the @code{ENCODE} and @code{DECODE} 2580statements. These statements are best replaced by @code{READ} and 2581@code{WRITE} statements involving internal files (@code{CHARACTER} 2582variables and arrays), which have been part of the Fortran standard since 2583Fortran 77. For example, replace a code fragment like 2584 2585@smallexample 2586 INTEGER*1 LINE(80) 2587 REAL A, B, C 2588c ... Code that sets LINE 2589 DECODE (80, 9000, LINE) A, B, C 2590 9000 FORMAT (1X, 3(F10.5)) 2591@end smallexample 2592 2593@noindent 2594with the following: 2595 2596@smallexample 2597 CHARACTER(LEN=80) LINE 2598 REAL A, B, C 2599c ... Code that sets LINE 2600 READ (UNIT=LINE, FMT=9000) A, B, C 2601 9000 FORMAT (1X, 3(F10.5)) 2602@end smallexample 2603 2604Similarly, replace a code fragment like 2605 2606@smallexample 2607 INTEGER*1 LINE(80) 2608 REAL A, B, C 2609c ... Code that sets A, B and C 2610 ENCODE (80, 9000, LINE) A, B, C 2611 9000 FORMAT (1X, 'OUTPUT IS ', 3(F10.5)) 2612@end smallexample 2613 2614@noindent 2615with the following: 2616 2617@smallexample 2618 CHARACTER(LEN=80) LINE 2619 REAL A, B, C 2620c ... Code that sets A, B and C 2621 WRITE (UNIT=LINE, FMT=9000) A, B, C 2622 9000 FORMAT (1X, 'OUTPUT IS ', 3(F10.5)) 2623@end smallexample 2624 2625 2626@node Variable FORMAT expressions 2627@subsection Variable @code{FORMAT} expressions 2628@cindex @code{FORMAT} 2629 2630A variable @code{FORMAT} expression is format statement which includes 2631angle brackets enclosing a Fortran expression: @code{FORMAT(I<N>)}. GNU 2632Fortran does not support this legacy extension. The effect of variable 2633format expressions can be reproduced by using the more powerful (and 2634standard) combination of internal output and string formats. For example, 2635replace a code fragment like this: 2636 2637@smallexample 2638 WRITE(6,20) INT1 2639 20 FORMAT(I<N+1>) 2640@end smallexample 2641 2642@noindent 2643with the following: 2644 2645@smallexample 2646c Variable declaration 2647 CHARACTER(LEN=20) FMT 2648c 2649c Other code here... 2650c 2651 WRITE(FMT,'("(I", I0, ")")') N+1 2652 WRITE(6,FMT) INT1 2653@end smallexample 2654 2655@noindent 2656or with: 2657 2658@smallexample 2659c Variable declaration 2660 CHARACTER(LEN=20) FMT 2661c 2662c Other code here... 2663c 2664 WRITE(FMT,*) N+1 2665 WRITE(6,"(I" // ADJUSTL(FMT) // ")") INT1 2666@end smallexample 2667 2668 2669@node Alternate complex function syntax 2670@subsection Alternate complex function syntax 2671@cindex Complex function 2672 2673Some Fortran compilers, including @command{g77}, let the user declare 2674complex functions with the syntax @code{COMPLEX FUNCTION name*16()}, as 2675well as @code{COMPLEX*16 FUNCTION name()}. Both are non-standard, legacy 2676extensions. @command{gfortran} accepts the latter form, which is more 2677common, but not the former. 2678 2679 2680@node Volatile COMMON blocks 2681@subsection Volatile @code{COMMON} blocks 2682@cindex @code{VOLATILE} 2683@cindex @code{COMMON} 2684 2685Some Fortran compilers, including @command{g77}, let the user declare 2686@code{COMMON} with the @code{VOLATILE} attribute. This is 2687invalid standard Fortran syntax and is not supported by 2688@command{gfortran}. Note that @command{gfortran} accepts 2689@code{VOLATILE} variables in @code{COMMON} blocks since revision 4.3. 2690 2691 2692@node OPEN( ... NAME=) 2693@subsection @code{OPEN( ... NAME=)} 2694@cindex @code{NAME} 2695 2696Some Fortran compilers, including @command{g77}, let the user declare 2697@code{OPEN( ... NAME=)}. This is 2698invalid standard Fortran syntax and is not supported by 2699@command{gfortran}. @code{OPEN( ... NAME=)} should be replaced 2700with @code{OPEN( ... FILE=)}. 2701 2702@node Q edit descriptor 2703@subsection @code{Q} edit descriptor 2704@cindex @code{Q} edit descriptor 2705 2706Some Fortran compilers provide the @code{Q} edit descriptor, which 2707transfers the number of characters left within an input record into an 2708integer variable. 2709 2710A direct replacement of the @code{Q} edit descriptor is not available 2711in @command{gfortran}. How to replicate its functionality using 2712standard-conforming code depends on what the intent of the original 2713code is. 2714 2715Options to replace @code{Q} may be to read the whole line into a 2716character variable and then counting the number of non-blank 2717characters left using @code{LEN_TRIM}. Another method may be to use 2718formatted stream, read the data up to the position where the @code{Q} 2719descriptor occurred, use @code{INQUIRE} to get the file position, 2720count the characters up to the next @code{NEW_LINE} and then start 2721reading from the position marked previously. 2722 2723 2724@c --------------------------------------------------------------------- 2725@c --------------------------------------------------------------------- 2726@c Mixed-Language Programming 2727@c --------------------------------------------------------------------- 2728 2729@node Mixed-Language Programming 2730@chapter Mixed-Language Programming 2731@cindex Interoperability 2732@cindex Mixed-language programming 2733 2734@menu 2735* Interoperability with C:: 2736* GNU Fortran Compiler Directives:: 2737* Non-Fortran Main Program:: 2738* Naming and argument-passing conventions:: 2739@end menu 2740 2741This chapter is about mixed-language interoperability, but also 2742applies if you link Fortran code compiled by different compilers. In 2743most cases, use of the C Binding features of the Fortran 2003 and 2744later standards is sufficient. 2745 2746For example, it is possible to mix Fortran code with C++ code as well 2747as C, if you declare the interface functions as @code{extern "C"} on 2748the C++ side and @code{BIND(C)} on the Fortran side, and follow the 2749rules for interoperability with C. Note that you cannot manipulate 2750C++ class objects in Fortran or vice versa except as opaque pointers. 2751 2752You can use the @command{gfortran} command to link both Fortran and 2753non-Fortran code into the same program, or you can use @command{gcc} 2754or @command{g++} if you also add an explicit @option{-lgfortran} option 2755to link with the Fortran library. If your main program is written in 2756C or some other language instead of Fortran, see 2757@ref{Non-Fortran Main Program}, below. 2758 2759@node Interoperability with C 2760@section Interoperability with C 2761@cindex interoperability with C 2762@cindex C interoperability 2763 2764@menu 2765* Intrinsic Types:: 2766* Derived Types and struct:: 2767* Interoperable Global Variables:: 2768* Interoperable Subroutines and Functions:: 2769* Working with C Pointers:: 2770* Further Interoperability of Fortran with C:: 2771@end menu 2772 2773Since Fortran 2003 (ISO/IEC 1539-1:2004(E)) there is a 2774standardized way to generate procedure and derived-type 2775declarations and global variables that are interoperable with C 2776(ISO/IEC 9899:1999). The @code{BIND(C)} attribute has been added 2777to inform the compiler that a symbol shall be interoperable with C; 2778also, some constraints are added. Note, however, that not 2779all C features have a Fortran equivalent or vice versa. For instance, 2780neither C's unsigned integers nor C's functions with variable number 2781of arguments have an equivalent in Fortran. 2782 2783Note that array dimensions are reversely ordered in C and that arrays in 2784C always start with index 0 while in Fortran they start by default with 27851. Thus, an array declaration @code{A(n,m)} in Fortran matches 2786@code{A[m][n]} in C and accessing the element @code{A(i,j)} matches 2787@code{A[j-1][i-1]}. The element following @code{A(i,j)} (C: @code{A[j-1][i-1]}; 2788assuming @math{i < n}) in memory is @code{A(i+1,j)} (C: @code{A[j-1][i]}). 2789 2790@node Intrinsic Types 2791@subsection Intrinsic Types 2792@cindex C intrinsic type interoperability 2793@cindex intrinsic type interoperability with C 2794@cindex interoperability, intrinsic type 2795 2796In order to ensure that exactly the same variable type and kind is used 2797in C and Fortran, you should use the named constants for kind parameters 2798that are defined in the @code{ISO_C_BINDING} intrinsic module. 2799That module contains named constants of character type representing 2800the escaped special characters in C, such as newline. 2801For a list of the constants, see @ref{ISO_C_BINDING}. 2802 2803For logical types, please note that the Fortran standard only guarantees 2804interoperability between C99's @code{_Bool} and Fortran's @code{C_Bool}-kind 2805logicals and C99 defines that @code{true} has the value 1 and @code{false} 2806the value 0. Using any other integer value with GNU Fortran's @code{LOGICAL} 2807(with any kind parameter) gives an undefined result. (Passing other integer 2808values than 0 and 1 to GCC's @code{_Bool} is also undefined, unless the 2809integer is explicitly or implicitly casted to @code{_Bool}.) 2810 2811@node Derived Types and struct 2812@subsection Derived Types and struct 2813@cindex C derived type and struct interoperability 2814@cindex derived type interoperability with C 2815@cindex interoperability, derived type and struct 2816 2817For compatibility of derived types with @code{struct}, use 2818the @code{BIND(C)} attribute in the type declaration. For instance, the 2819following type declaration 2820 2821@smallexample 2822 USE ISO_C_BINDING 2823 TYPE, BIND(C) :: myType 2824 INTEGER(C_INT) :: i1, i2 2825 INTEGER(C_SIGNED_CHAR) :: i3 2826 REAL(C_DOUBLE) :: d1 2827 COMPLEX(C_FLOAT_COMPLEX) :: c1 2828 CHARACTER(KIND=C_CHAR) :: str(5) 2829 END TYPE 2830@end smallexample 2831 2832@noindent 2833matches the following @code{struct} declaration in C 2834 2835@smallexample 2836 struct @{ 2837 int i1, i2; 2838 /* Note: "char" might be signed or unsigned. */ 2839 signed char i3; 2840 double d1; 2841 float _Complex c1; 2842 char str[5]; 2843 @} myType; 2844@end smallexample 2845 2846Derived types with the C binding attribute shall not have the @code{sequence} 2847attribute, type parameters, the @code{extends} attribute, nor type-bound 2848procedures. Every component must be of interoperable type and kind and may not 2849have the @code{pointer} or @code{allocatable} attribute. The names of the 2850components are irrelevant for interoperability. 2851 2852As there exist no direct Fortran equivalents, neither unions nor structs 2853with bit field or variable-length array members are interoperable. 2854 2855@node Interoperable Global Variables 2856@subsection Interoperable Global Variables 2857@cindex C variable interoperability 2858@cindex variable interoperability with C 2859@cindex interoperability, variable 2860 2861Variables can be made accessible from C using the C binding attribute, 2862optionally together with specifying a binding name. Those variables 2863have to be declared in the declaration part of a @code{MODULE}, 2864be of interoperable type, and have neither the @code{pointer} nor 2865the @code{allocatable} attribute. 2866 2867@smallexample 2868 MODULE m 2869 USE myType_module 2870 USE ISO_C_BINDING 2871 integer(C_INT), bind(C, name="_MyProject_flags") :: global_flag 2872 type(myType), bind(C) :: tp 2873 END MODULE 2874@end smallexample 2875 2876Here, @code{_MyProject_flags} is the case-sensitive name of the variable 2877as seen from C programs while @code{global_flag} is the case-insensitive 2878name as seen from Fortran. If no binding name is specified, as for 2879@var{tp}, the C binding name is the (lowercase) Fortran binding name. 2880If a binding name is specified, only a single variable may be after the 2881double colon. Note of warning: You cannot use a global variable to 2882access @var{errno} of the C library as the C standard allows it to be 2883a macro. Use the @code{IERRNO} intrinsic (GNU extension) instead. 2884 2885@node Interoperable Subroutines and Functions 2886@subsection Interoperable Subroutines and Functions 2887@cindex C procedure interoperability 2888@cindex procedure interoperability with C 2889@cindex function interoperability with C 2890@cindex subroutine interoperability with C 2891@cindex interoperability, subroutine and function 2892 2893Subroutines and functions have to have the @code{BIND(C)} attribute to 2894be compatible with C. The dummy argument declaration is relatively 2895straightforward. However, one needs to be careful because C uses 2896call-by-value by default while Fortran behaves usually similar to 2897call-by-reference. Furthermore, strings and pointers are handled 2898differently. 2899 2900To pass a variable by value, use the @code{VALUE} attribute. 2901Thus, the following C prototype 2902 2903@smallexample 2904@code{int func(int i, int *j)} 2905@end smallexample 2906 2907@noindent 2908matches the Fortran declaration 2909 2910@smallexample 2911 integer(c_int) function func(i,j) 2912 use iso_c_binding, only: c_int 2913 integer(c_int), VALUE :: i 2914 integer(c_int) :: j 2915@end smallexample 2916 2917Note that pointer arguments also frequently need the @code{VALUE} attribute, 2918see @ref{Working with C Pointers}. 2919 2920Strings are handled quite differently in C and Fortran. In C a string 2921is a @code{NUL}-terminated array of characters while in Fortran each string 2922has a length associated with it and is thus not terminated (by e.g. 2923@code{NUL}). For example, if you want to use the following C function, 2924 2925@smallexample 2926 #include <stdio.h> 2927 void print_C(char *string) /* equivalent: char string[] */ 2928 @{ 2929 printf("%s\n", string); 2930 @} 2931@end smallexample 2932 2933@noindent 2934to print ``Hello World'' from Fortran, you can call it using 2935 2936@smallexample 2937 use iso_c_binding, only: C_CHAR, C_NULL_CHAR 2938 interface 2939 subroutine print_c(string) bind(C, name="print_C") 2940 use iso_c_binding, only: c_char 2941 character(kind=c_char) :: string(*) 2942 end subroutine print_c 2943 end interface 2944 call print_c(C_CHAR_"Hello World"//C_NULL_CHAR) 2945@end smallexample 2946 2947As the example shows, you need to ensure that the 2948string is @code{NUL} terminated. Additionally, the dummy argument 2949@var{string} of @code{print_C} is a length-one assumed-size 2950array; using @code{character(len=*)} is not allowed. The example 2951above uses @code{c_char_"Hello World"} to ensure the string 2952literal has the right type; typically the default character 2953kind and @code{c_char} are the same and thus @code{"Hello World"} 2954is equivalent. However, the standard does not guarantee this. 2955 2956The use of strings is now further illustrated using the C library 2957function @code{strncpy}, whose prototype is 2958 2959@smallexample 2960 char *strncpy(char *restrict s1, const char *restrict s2, size_t n); 2961@end smallexample 2962 2963@noindent 2964The function @code{strncpy} copies at most @var{n} characters from 2965string @var{s2} to @var{s1} and returns @var{s1}. In the following 2966example, we ignore the return value: 2967 2968@smallexample 2969 use iso_c_binding 2970 implicit none 2971 character(len=30) :: str,str2 2972 interface 2973 ! Ignore the return value of strncpy -> subroutine 2974 ! "restrict" is always assumed if we do not pass a pointer 2975 subroutine strncpy(dest, src, n) bind(C) 2976 import 2977 character(kind=c_char), intent(out) :: dest(*) 2978 character(kind=c_char), intent(in) :: src(*) 2979 integer(c_size_t), value, intent(in) :: n 2980 end subroutine strncpy 2981 end interface 2982 str = repeat('X',30) ! Initialize whole string with 'X' 2983 call strncpy(str, c_char_"Hello World"//C_NULL_CHAR, & 2984 len(c_char_"Hello World",kind=c_size_t)) 2985 print '(a)', str ! prints: "Hello WorldXXXXXXXXXXXXXXXXXXX" 2986 end 2987@end smallexample 2988 2989The intrinsic procedures are described in @ref{Intrinsic Procedures}. 2990 2991@node Working with C Pointers 2992@subsection Working with C Pointers 2993@cindex C pointers 2994@cindex pointers, C 2995 2996C pointers are represented in Fortran via the special opaque derived 2997type @code{type(c_ptr)} (with private components). C pointers are distinct 2998from Fortran objects with the @code{POINTER} attribute. Thus one needs to 2999use intrinsic conversion procedures to convert from or to C pointers. 3000For some applications, using an assumed type (@code{TYPE(*)}) can be 3001an alternative to a C pointer, and you can also use library routines 3002to access Fortran pointers from C. See @ref{Further Interoperability 3003of Fortran with C}. 3004 3005Here is an example of using C pointers in Fortran: 3006 3007@smallexample 3008 use iso_c_binding 3009 type(c_ptr) :: cptr1, cptr2 3010 integer, target :: array(7), scalar 3011 integer, pointer :: pa(:), ps 3012 cptr1 = c_loc(array(1)) ! The programmer needs to ensure that the 3013 ! array is contiguous if required by the C 3014 ! procedure 3015 cptr2 = c_loc(scalar) 3016 call c_f_pointer(cptr2, ps) 3017 call c_f_pointer(cptr2, pa, shape=[7]) 3018@end smallexample 3019 3020When converting C to Fortran arrays, the one-dimensional @code{SHAPE} argument 3021has to be passed. 3022 3023If a pointer is a dummy argument of an interoperable procedure, it usually 3024has to be declared using the @code{VALUE} attribute. @code{void*} 3025matches @code{TYPE(C_PTR), VALUE}, while @code{TYPE(C_PTR)} alone 3026matches @code{void**}. 3027 3028Procedure pointers are handled analogously to pointers; the C type is 3029@code{TYPE(C_FUNPTR)} and the intrinsic conversion procedures are 3030@code{C_F_PROCPOINTER} and @code{C_FUNLOC}. 3031 3032Let us consider two examples of actually passing a procedure pointer from 3033C to Fortran and vice versa. Note that these examples are also very 3034similar to passing ordinary pointers between both languages. First, 3035consider this code in C: 3036 3037@smallexample 3038/* Procedure implemented in Fortran. */ 3039void get_values (void (*)(double)); 3040 3041/* Call-back routine we want called from Fortran. */ 3042void 3043print_it (double x) 3044@{ 3045 printf ("Number is %f.\n", x); 3046@} 3047 3048/* Call Fortran routine and pass call-back to it. */ 3049void 3050foobar () 3051@{ 3052 get_values (&print_it); 3053@} 3054@end smallexample 3055 3056A matching implementation for @code{get_values} in Fortran, that correctly 3057receives the procedure pointer from C and is able to call it, is given 3058in the following @code{MODULE}: 3059 3060@smallexample 3061MODULE m 3062 IMPLICIT NONE 3063 3064 ! Define interface of call-back routine. 3065 ABSTRACT INTERFACE 3066 SUBROUTINE callback (x) 3067 USE, INTRINSIC :: ISO_C_BINDING 3068 REAL(KIND=C_DOUBLE), INTENT(IN), VALUE :: x 3069 END SUBROUTINE callback 3070 END INTERFACE 3071 3072CONTAINS 3073 3074 ! Define C-bound procedure. 3075 SUBROUTINE get_values (cproc) BIND(C) 3076 USE, INTRINSIC :: ISO_C_BINDING 3077 TYPE(C_FUNPTR), INTENT(IN), VALUE :: cproc 3078 3079 PROCEDURE(callback), POINTER :: proc 3080 3081 ! Convert C to Fortran procedure pointer. 3082 CALL C_F_PROCPOINTER (cproc, proc) 3083 3084 ! Call it. 3085 CALL proc (1.0_C_DOUBLE) 3086 CALL proc (-42.0_C_DOUBLE) 3087 CALL proc (18.12_C_DOUBLE) 3088 END SUBROUTINE get_values 3089 3090END MODULE m 3091@end smallexample 3092 3093Next, we want to call a C routine that expects a procedure pointer argument 3094and pass it a Fortran procedure (which clearly must be interoperable!). 3095Again, the C function may be: 3096 3097@smallexample 3098int 3099call_it (int (*func)(int), int arg) 3100@{ 3101 return func (arg); 3102@} 3103@end smallexample 3104 3105It can be used as in the following Fortran code: 3106 3107@smallexample 3108MODULE m 3109 USE, INTRINSIC :: ISO_C_BINDING 3110 IMPLICIT NONE 3111 3112 ! Define interface of C function. 3113 INTERFACE 3114 INTEGER(KIND=C_INT) FUNCTION call_it (func, arg) BIND(C) 3115 USE, INTRINSIC :: ISO_C_BINDING 3116 TYPE(C_FUNPTR), INTENT(IN), VALUE :: func 3117 INTEGER(KIND=C_INT), INTENT(IN), VALUE :: arg 3118 END FUNCTION call_it 3119 END INTERFACE 3120 3121CONTAINS 3122 3123 ! Define procedure passed to C function. 3124 ! It must be interoperable! 3125 INTEGER(KIND=C_INT) FUNCTION double_it (arg) BIND(C) 3126 INTEGER(KIND=C_INT), INTENT(IN), VALUE :: arg 3127 double_it = arg + arg 3128 END FUNCTION double_it 3129 3130 ! Call C function. 3131 SUBROUTINE foobar () 3132 TYPE(C_FUNPTR) :: cproc 3133 INTEGER(KIND=C_INT) :: i 3134 3135 ! Get C procedure pointer. 3136 cproc = C_FUNLOC (double_it) 3137 3138 ! Use it. 3139 DO i = 1_C_INT, 10_C_INT 3140 PRINT *, call_it (cproc, i) 3141 END DO 3142 END SUBROUTINE foobar 3143 3144END MODULE m 3145@end smallexample 3146 3147@node Further Interoperability of Fortran with C 3148@subsection Further Interoperability of Fortran with C 3149@cindex Further Interoperability of Fortran with C 3150@cindex TS 29113 3151@cindex array descriptor 3152@cindex dope vector 3153@cindex assumed-type 3154@cindex assumed-rank 3155 3156GNU Fortran implements the Technical Specification ISO/IEC TS 315729113:2012, which extends the interoperability support of Fortran 2003 3158and Fortran 2008 and is now part of the 2018 Fortran standard. 3159Besides removing some restrictions and constraints, the Technical 3160Specification adds assumed-type (@code{TYPE(*)}) and assumed-rank 3161(@code{DIMENSION(..)}) variables and allows for interoperability of 3162assumed-shape, assumed-rank, and deferred-shape arrays, as well as 3163allocatables and pointers. Objects of these types are passed to 3164@code{BIND(C)} functions as descriptors with a standard interface, 3165declared in the header file @code{<ISO_Fortran_binding.h>}. 3166 3167Note: Currently, GNU Fortran does not use internally the array descriptor 3168(dope vector) as specified in the Technical Specification, but uses 3169an array descriptor with different fields in functions without the 3170@code{BIND(C)} attribute. Arguments to functions marked @code{BIND(C)} 3171are converted to the specified form. If you need to access GNU Fortran's 3172internal array descriptor, you can use the Chasm Language Interoperability 3173Tools, @url{http://chasm-interop.sourceforge.net/}. 3174 3175@node GNU Fortran Compiler Directives 3176@section GNU Fortran Compiler Directives 3177 3178@menu 3179* ATTRIBUTES directive:: 3180* UNROLL directive:: 3181* BUILTIN directive:: 3182* IVDEP directive:: 3183* VECTOR directive:: 3184* NOVECTOR directive:: 3185@end menu 3186 3187@node ATTRIBUTES directive 3188@subsection ATTRIBUTES directive 3189 3190The Fortran standard describes how a conforming program shall 3191behave; however, the exact implementation is not standardized. In order 3192to allow the user to choose specific implementation details, compiler 3193directives can be used to set attributes of variables and procedures 3194which are not part of the standard. Whether a given attribute is 3195supported and its exact effects depend on both the operating system and 3196on the processor; see 3197@ref{Top,,C Extensions,gcc,Using the GNU Compiler Collection (GCC)} 3198for details. 3199 3200For procedures and procedure pointers, the following attributes can 3201be used to change the calling convention: 3202 3203@itemize 3204@item @code{CDECL} -- standard C calling convention 3205@item @code{STDCALL} -- convention where the called procedure pops the stack 3206@item @code{FASTCALL} -- part of the arguments are passed via registers 3207instead using the stack 3208@end itemize 3209 3210Besides changing the calling convention, the attributes also influence 3211the decoration of the symbol name, e.g., by a leading underscore or by 3212a trailing at-sign followed by the number of bytes on the stack. When 3213assigning a procedure to a procedure pointer, both should use the same 3214calling convention. 3215 3216On some systems, procedures and global variables (module variables and 3217@code{COMMON} blocks) need special handling to be accessible when they 3218are in a shared library. The following attributes are available: 3219 3220@itemize 3221@item @code{DLLEXPORT} -- provide a global pointer to a pointer in the DLL 3222@item @code{DLLIMPORT} -- reference the function or variable using a 3223global pointer 3224@end itemize 3225 3226For dummy arguments, the @code{NO_ARG_CHECK} attribute can be used; in 3227other compilers, it is also known as @code{IGNORE_TKR}. For dummy arguments 3228with this attribute actual arguments of any type and kind (similar to 3229@code{TYPE(*)}), scalars and arrays of any rank (no equivalent 3230in Fortran standard) are accepted. As with @code{TYPE(*)}, the argument 3231is unlimited polymorphic and no type information is available. 3232Additionally, the argument may only be passed to dummy arguments 3233with the @code{NO_ARG_CHECK} attribute and as argument to the 3234@code{PRESENT} intrinsic function and to @code{C_LOC} of the 3235@code{ISO_C_BINDING} module. 3236 3237Variables with @code{NO_ARG_CHECK} attribute shall be of assumed-type 3238(@code{TYPE(*)}; recommended) or of type @code{INTEGER}, @code{LOGICAL}, 3239@code{REAL} or @code{COMPLEX}. They shall not have the @code{ALLOCATE}, 3240@code{CODIMENSION}, @code{INTENT(OUT)}, @code{POINTER} or @code{VALUE} 3241attribute; furthermore, they shall be either scalar or of assumed-size 3242(@code{dimension(*)}). As @code{TYPE(*)}, the @code{NO_ARG_CHECK} attribute 3243requires an explicit interface. 3244 3245@itemize 3246@item @code{NO_ARG_CHECK} -- disable the type, kind and rank checking 3247@item @code{DEPRECATED} -- print a warning when using a such-tagged 3248deprecated procedure, variable or parameter; the warning can be suppressed 3249with @option{-Wno-deprecated-declarations}. 3250@end itemize 3251 3252 3253The attributes are specified using the syntax 3254 3255@code{!GCC$ ATTRIBUTES} @var{attribute-list} @code{::} @var{variable-list} 3256 3257where in free-form source code only whitespace is allowed before @code{!GCC$} 3258and in fixed-form source code @code{!GCC$}, @code{cGCC$} or @code{*GCC$} shall 3259start in the first column. 3260 3261For procedures, the compiler directives shall be placed into the body 3262of the procedure; for variables and procedure pointers, they shall be in 3263the same declaration part as the variable or procedure pointer. 3264 3265 3266@node UNROLL directive 3267@subsection UNROLL directive 3268 3269The syntax of the directive is 3270 3271@code{!GCC$ unroll N} 3272 3273You can use this directive to control how many times a loop should be unrolled. 3274It must be placed immediately before a @code{DO} loop and applies only to the 3275loop that follows. N is an integer constant specifying the unrolling factor. 3276The values of 0 and 1 block any unrolling of the loop. 3277 3278 3279@node BUILTIN directive 3280@subsection BUILTIN directive 3281 3282The syntax of the directive is 3283 3284@code{!GCC$ BUILTIN (B) attributes simd FLAGS IF('target')} 3285 3286You can use this directive to define which middle-end built-ins provide vector 3287implementations. @code{B} is name of the middle-end built-in. @code{FLAGS} 3288are optional and must be either "(inbranch)" or "(notinbranch)". 3289@code{IF} statement is optional and is used to filter multilib ABIs 3290for the built-in that should be vectorized. Example usage: 3291 3292@smallexample 3293!GCC$ builtin (sinf) attributes simd (notinbranch) if('x86_64') 3294@end smallexample 3295 3296The purpose of the directive is to provide an API among the GCC compiler and 3297the GNU C Library which would define vector implementations of math routines. 3298 3299 3300@node IVDEP directive 3301@subsection IVDEP directive 3302 3303The syntax of the directive is 3304 3305@code{!GCC$ ivdep} 3306 3307This directive tells the compiler to ignore vector dependencies in the 3308following loop. It must be placed immediately before a @code{DO} loop 3309and applies only to the loop that follows. 3310 3311Sometimes the compiler may not have sufficient information to decide 3312whether a particular loop is vectorizable due to potential 3313dependencies between iterations. The purpose of the directive is to 3314tell the compiler that vectorization is safe. 3315 3316This directive is intended for annotation of existing code. For new 3317code it is recommended to consider OpenMP SIMD directives as potential 3318alternative. 3319 3320 3321@node VECTOR directive 3322@subsection VECTOR directive 3323 3324The syntax of the directive is 3325 3326@code{!GCC$ vector} 3327 3328This directive tells the compiler to vectorize the following loop. It 3329must be placed immediately before a @code{DO} loop and applies only to 3330the loop that follows. 3331 3332 3333@node NOVECTOR directive 3334@subsection NOVECTOR directive 3335 3336The syntax of the directive is 3337 3338@code{!GCC$ novector} 3339 3340This directive tells the compiler to not vectorize the following loop. 3341It must be placed immediately before a @code{DO} loop and applies only 3342to the loop that follows. 3343 3344 3345@node Non-Fortran Main Program 3346@section Non-Fortran Main Program 3347 3348@menu 3349* _gfortran_set_args:: Save command-line arguments 3350* _gfortran_set_options:: Set library option flags 3351* _gfortran_set_convert:: Set endian conversion 3352* _gfortran_set_record_marker:: Set length of record markers 3353* _gfortran_set_fpe:: Set when a Floating Point Exception should be raised 3354* _gfortran_set_max_subrecord_length:: Set subrecord length 3355@end menu 3356 3357Even if you are doing mixed-language programming, it is very 3358likely that you do not need to know or use the information in this 3359section. Since it is about the internal structure of GNU Fortran, 3360it may also change in GCC minor releases. 3361 3362When you compile a @code{PROGRAM} with GNU Fortran, a function 3363with the name @code{main} (in the symbol table of the object file) 3364is generated, which initializes the libgfortran library and then 3365calls the actual program which uses the name @code{MAIN__}, for 3366historic reasons. If you link GNU Fortran compiled procedures 3367to, e.g., a C or C++ program or to a Fortran program compiled by 3368a different compiler, the libgfortran library is not initialized 3369and thus a few intrinsic procedures do not work properly, e.g. 3370those for obtaining the command-line arguments. 3371 3372Therefore, if your @code{PROGRAM} is not compiled with 3373GNU Fortran and the GNU Fortran compiled procedures require 3374intrinsics relying on the library initialization, you need to 3375initialize the library yourself. Using the default options, 3376gfortran calls @code{_gfortran_set_args} and 3377@code{_gfortran_set_options}. The initialization of the former 3378is needed if the called procedures access the command line 3379(and for backtracing); the latter sets some flags based on the 3380standard chosen or to enable backtracing. In typical programs, 3381it is not necessary to call any initialization function. 3382 3383If your @code{PROGRAM} is compiled with GNU Fortran, you shall 3384not call any of the following functions. The libgfortran 3385initialization functions are shown in C syntax but using C 3386bindings they are also accessible from Fortran. 3387 3388 3389@node _gfortran_set_args 3390@subsection @code{_gfortran_set_args} --- Save command-line arguments 3391@fnindex _gfortran_set_args 3392@cindex libgfortran initialization, set_args 3393 3394@table @asis 3395@item @emph{Description}: 3396@code{_gfortran_set_args} saves the command-line arguments; this 3397initialization is required if any of the command-line intrinsics 3398is called. Additionally, it shall be called if backtracing is 3399enabled (see @code{_gfortran_set_options}). 3400 3401@item @emph{Syntax}: 3402@code{void _gfortran_set_args (int argc, char *argv[])} 3403 3404@item @emph{Arguments}: 3405@multitable @columnfractions .15 .70 3406@item @var{argc} @tab number of command line argument strings 3407@item @var{argv} @tab the command-line argument strings; argv[0] 3408is the pathname of the executable itself. 3409@end multitable 3410 3411@item @emph{Example}: 3412@smallexample 3413int main (int argc, char *argv[]) 3414@{ 3415 /* Initialize libgfortran. */ 3416 _gfortran_set_args (argc, argv); 3417 return 0; 3418@} 3419@end smallexample 3420@end table 3421 3422 3423@node _gfortran_set_options 3424@subsection @code{_gfortran_set_options} --- Set library option flags 3425@fnindex _gfortran_set_options 3426@cindex libgfortran initialization, set_options 3427 3428@table @asis 3429@item @emph{Description}: 3430@code{_gfortran_set_options} sets several flags related to the Fortran 3431standard to be used, whether backtracing should be enabled 3432and whether range checks should be performed. The syntax allows for 3433upward compatibility since the number of passed flags is specified; for 3434non-passed flags, the default value is used. See also 3435@pxref{Code Gen Options}. Please note that not all flags are actually 3436used. 3437 3438@item @emph{Syntax}: 3439@code{void _gfortran_set_options (int num, int options[])} 3440 3441@item @emph{Arguments}: 3442@multitable @columnfractions .15 .70 3443@item @var{num} @tab number of options passed 3444@item @var{argv} @tab The list of flag values 3445@end multitable 3446 3447@item @emph{option flag list}: 3448@multitable @columnfractions .15 .70 3449@item @var{option}[0] @tab Allowed standard; can give run-time errors 3450if e.g. an input-output edit descriptor is invalid in a given 3451standard. Possible values are (bitwise or-ed) @code{GFC_STD_F77} (1), 3452@code{GFC_STD_F95_OBS} (2), @code{GFC_STD_F95_DEL} (4), 3453@code{GFC_STD_F95} (8), @code{GFC_STD_F2003} (16), @code{GFC_STD_GNU} 3454(32), @code{GFC_STD_LEGACY} (64), @code{GFC_STD_F2008} (128), 3455@code{GFC_STD_F2008_OBS} (256), @code{GFC_STD_F2008_TS} (512), 3456@code{GFC_STD_F2018} (1024), @code{GFC_STD_F2018_OBS} (2048), and 3457@code{GFC_STD=F2018_DEL} (4096). Default: @code{GFC_STD_F95_OBS | 3458GFC_STD_F95_DEL | GFC_STD_F95 | GFC_STD_F2003 | GFC_STD_F2008 | 3459GFC_STD_F2008_TS | GFC_STD_F2008_OBS | GFC_STD_F77 | GFC_STD_F2018 | 3460GFC_STD_F2018_OBS | GFC_STD_F2018_DEL | GFC_STD_GNU | GFC_STD_LEGACY}. 3461@item @var{option}[1] @tab Standard-warning flag; prints a warning to 3462standard error. Default: @code{GFC_STD_F95_DEL | GFC_STD_LEGACY}. 3463@item @var{option}[2] @tab If non zero, enable pedantic checking. 3464Default: off. 3465@item @var{option}[3] @tab Unused. 3466@item @var{option}[4] @tab If non zero, enable backtracing on run-time 3467errors. Default: off. (Default in the compiler: on.) 3468Note: Installs a signal handler and requires command-line 3469initialization using @code{_gfortran_set_args}. 3470@item @var{option}[5] @tab If non zero, supports signed zeros. 3471Default: enabled. 3472@item @var{option}[6] @tab Enables run-time checking. Possible values 3473are (bitwise or-ed): GFC_RTCHECK_BOUNDS (1), GFC_RTCHECK_ARRAY_TEMPS (2), 3474GFC_RTCHECK_RECURSION (4), GFC_RTCHECK_DO (8), GFC_RTCHECK_POINTER (16), 3475GFC_RTCHECK_MEM (32), GFC_RTCHECK_BITS (64). 3476Default: disabled. 3477@item @var{option}[7] @tab Unused. 3478@item @var{option}[8] @tab Show a warning when invoking @code{STOP} and 3479@code{ERROR STOP} if a floating-point exception occurred. Possible values 3480are (bitwise or-ed) @code{GFC_FPE_INVALID} (1), @code{GFC_FPE_DENORMAL} (2), 3481@code{GFC_FPE_ZERO} (4), @code{GFC_FPE_OVERFLOW} (8), 3482@code{GFC_FPE_UNDERFLOW} (16), @code{GFC_FPE_INEXACT} (32). Default: None (0). 3483(Default in the compiler: @code{GFC_FPE_INVALID | GFC_FPE_DENORMAL | 3484GFC_FPE_ZERO | GFC_FPE_OVERFLOW | GFC_FPE_UNDERFLOW}.) 3485@end multitable 3486 3487@item @emph{Example}: 3488@smallexample 3489 /* Use gfortran 4.9 default options. */ 3490 static int options[] = @{68, 511, 0, 0, 1, 1, 0, 0, 31@}; 3491 _gfortran_set_options (9, &options); 3492@end smallexample 3493@end table 3494 3495 3496@node _gfortran_set_convert 3497@subsection @code{_gfortran_set_convert} --- Set endian conversion 3498@fnindex _gfortran_set_convert 3499@cindex libgfortran initialization, set_convert 3500 3501@table @asis 3502@item @emph{Description}: 3503@code{_gfortran_set_convert} set the representation of data for 3504unformatted files. 3505 3506@item @emph{Syntax}: 3507@code{void _gfortran_set_convert (int conv)} 3508 3509@item @emph{Arguments}: 3510@multitable @columnfractions .15 .70 3511@item @var{conv} @tab Endian conversion, possible values: 3512GFC_CONVERT_NATIVE (0, default), GFC_CONVERT_SWAP (1), 3513GFC_CONVERT_BIG (2), GFC_CONVERT_LITTLE (3). 3514@end multitable 3515 3516@item @emph{Example}: 3517@smallexample 3518int main (int argc, char *argv[]) 3519@{ 3520 /* Initialize libgfortran. */ 3521 _gfortran_set_args (argc, argv); 3522 _gfortran_set_convert (1); 3523 return 0; 3524@} 3525@end smallexample 3526@end table 3527 3528 3529@node _gfortran_set_record_marker 3530@subsection @code{_gfortran_set_record_marker} --- Set length of record markers 3531@fnindex _gfortran_set_record_marker 3532@cindex libgfortran initialization, set_record_marker 3533 3534@table @asis 3535@item @emph{Description}: 3536@code{_gfortran_set_record_marker} sets the length of record markers 3537for unformatted files. 3538 3539@item @emph{Syntax}: 3540@code{void _gfortran_set_record_marker (int val)} 3541 3542@item @emph{Arguments}: 3543@multitable @columnfractions .15 .70 3544@item @var{val} @tab Length of the record marker; valid values 3545are 4 and 8. Default is 4. 3546@end multitable 3547 3548@item @emph{Example}: 3549@smallexample 3550int main (int argc, char *argv[]) 3551@{ 3552 /* Initialize libgfortran. */ 3553 _gfortran_set_args (argc, argv); 3554 _gfortran_set_record_marker (8); 3555 return 0; 3556@} 3557@end smallexample 3558@end table 3559 3560 3561@node _gfortran_set_fpe 3562@subsection @code{_gfortran_set_fpe} --- Enable floating point exception traps 3563@fnindex _gfortran_set_fpe 3564@cindex libgfortran initialization, set_fpe 3565 3566@table @asis 3567@item @emph{Description}: 3568@code{_gfortran_set_fpe} enables floating point exception traps for 3569the specified exceptions. On most systems, this will result in a 3570SIGFPE signal being sent and the program being aborted. 3571 3572@item @emph{Syntax}: 3573@code{void _gfortran_set_fpe (int val)} 3574 3575@item @emph{Arguments}: 3576@multitable @columnfractions .15 .70 3577@item @var{option}[0] @tab IEEE exceptions. Possible values are 3578(bitwise or-ed) zero (0, default) no trapping, 3579@code{GFC_FPE_INVALID} (1), @code{GFC_FPE_DENORMAL} (2), 3580@code{GFC_FPE_ZERO} (4), @code{GFC_FPE_OVERFLOW} (8), 3581@code{GFC_FPE_UNDERFLOW} (16), and @code{GFC_FPE_INEXACT} (32). 3582@end multitable 3583 3584@item @emph{Example}: 3585@smallexample 3586int main (int argc, char *argv[]) 3587@{ 3588 /* Initialize libgfortran. */ 3589 _gfortran_set_args (argc, argv); 3590 /* FPE for invalid operations such as SQRT(-1.0). */ 3591 _gfortran_set_fpe (1); 3592 return 0; 3593@} 3594@end smallexample 3595@end table 3596 3597 3598@node _gfortran_set_max_subrecord_length 3599@subsection @code{_gfortran_set_max_subrecord_length} --- Set subrecord length 3600@fnindex _gfortran_set_max_subrecord_length 3601@cindex libgfortran initialization, set_max_subrecord_length 3602 3603@table @asis 3604@item @emph{Description}: 3605@code{_gfortran_set_max_subrecord_length} set the maximum length 3606for a subrecord. This option only makes sense for testing and 3607debugging of unformatted I/O. 3608 3609@item @emph{Syntax}: 3610@code{void _gfortran_set_max_subrecord_length (int val)} 3611 3612@item @emph{Arguments}: 3613@multitable @columnfractions .15 .70 3614@item @var{val} @tab the maximum length for a subrecord; 3615the maximum permitted value is 2147483639, which is also 3616the default. 3617@end multitable 3618 3619@item @emph{Example}: 3620@smallexample 3621int main (int argc, char *argv[]) 3622@{ 3623 /* Initialize libgfortran. */ 3624 _gfortran_set_args (argc, argv); 3625 _gfortran_set_max_subrecord_length (8); 3626 return 0; 3627@} 3628@end smallexample 3629@end table 3630 3631 3632@node Naming and argument-passing conventions 3633@section Naming and argument-passing conventions 3634 3635This section gives an overview about the naming convention of procedures 3636and global variables and about the argument passing conventions used by 3637GNU Fortran. If a C binding has been specified, the naming convention 3638and some of the argument-passing conventions change. If possible, 3639mixed-language and mixed-compiler projects should use the better defined 3640C binding for interoperability. See @pxref{Interoperability with C}. 3641 3642@menu 3643* Naming conventions:: 3644* Argument passing conventions:: 3645@end menu 3646 3647 3648@node Naming conventions 3649@subsection Naming conventions 3650 3651According the Fortran standard, valid Fortran names consist of a letter 3652between @code{A} to @code{Z}, @code{a} to @code{z}, digits @code{0}, 3653@code{1} to @code{9} and underscores (@code{_}) with the restriction 3654that names may only start with a letter. As vendor extension, the 3655dollar sign (@code{$}) is additionally permitted with the option 3656@option{-fdollar-ok}, but not as first character and only if the 3657target system supports it. 3658 3659By default, the procedure name is the lower-cased Fortran name with an 3660appended underscore (@code{_}); using @option{-fno-underscoring} no 3661underscore is appended while @code{-fsecond-underscore} appends two 3662underscores. Depending on the target system and the calling convention, 3663the procedure might be additionally dressed; for instance, on 32bit 3664Windows with @code{stdcall}, an at-sign @code{@@} followed by an integer 3665number is appended. For the changing the calling convention, see 3666@pxref{GNU Fortran Compiler Directives}. 3667 3668For common blocks, the same convention is used, i.e. by default an 3669underscore is appended to the lower-cased Fortran name. Blank commons 3670have the name @code{__BLNK__}. 3671 3672For procedures and variables declared in the specification space of a 3673module, the name is formed by @code{__}, followed by the lower-cased 3674module name, @code{_MOD_}, and the lower-cased Fortran name. Note that 3675no underscore is appended. 3676 3677 3678@node Argument passing conventions 3679@subsection Argument passing conventions 3680 3681Subroutines do not return a value (matching C99's @code{void}) while 3682functions either return a value as specified in the platform ABI or 3683the result variable is passed as hidden argument to the function and 3684no result is returned. A hidden result variable is used when the 3685result variable is an array or of type @code{CHARACTER}. 3686 3687Arguments are passed according to the platform ABI. In particular, 3688complex arguments might not be compatible to a struct with two real 3689components for the real and imaginary part. The argument passing 3690matches the one of C99's @code{_Complex}. Functions with scalar 3691complex result variables return their value and do not use a 3692by-reference argument. Note that with the @option{-ff2c} option, 3693the argument passing is modified and no longer completely matches 3694the platform ABI. Some other Fortran compilers use @code{f2c} 3695semantic by default; this might cause problems with 3696interoperablility. 3697 3698GNU Fortran passes most arguments by reference, i.e. by passing a 3699pointer to the data. Note that the compiler might use a temporary 3700variable into which the actual argument has been copied, if required 3701semantically (copy-in/copy-out). 3702 3703For arguments with @code{ALLOCATABLE} and @code{POINTER} 3704attribute (including procedure pointers), a pointer to the pointer 3705is passed such that the pointer address can be modified in the 3706procedure. 3707 3708For dummy arguments with the @code{VALUE} attribute: Scalar arguments 3709of the type @code{INTEGER}, @code{LOGICAL}, @code{REAL} and 3710@code{COMPLEX} are passed by value according to the platform ABI. 3711(As vendor extension and not recommended, using @code{%VAL()} in the 3712call to a procedure has the same effect.) For @code{TYPE(C_PTR)} and 3713procedure pointers, the pointer itself is passed such that it can be 3714modified without affecting the caller. 3715@c FIXME: Document how VALUE is handled for CHARACTER, TYPE, 3716@c CLASS and arrays, i.e. whether the copy-in is done in the caller 3717@c or in the callee. 3718 3719For Boolean (@code{LOGICAL}) arguments, please note that GCC expects 3720only the integer value 0 and 1. If a GNU Fortran @code{LOGICAL} 3721variable contains another integer value, the result is undefined. 3722As some other Fortran compilers use @math{-1} for @code{.TRUE.}, 3723extra care has to be taken -- such as passing the value as 3724@code{INTEGER}. (The same value restriction also applies to other 3725front ends of GCC, e.g. to GCC's C99 compiler for @code{_Bool} 3726or GCC's Ada compiler for @code{Boolean}.) 3727 3728For arguments of @code{CHARACTER} type, the character length is passed 3729as a hidden argument at the end of the argument list. For 3730deferred-length strings, the value is passed by reference, otherwise 3731by value. The character length has the C type @code{size_t} (or 3732@code{INTEGER(kind=C_SIZE_T)} in Fortran). Note that this is 3733different to older versions of the GNU Fortran compiler, where the 3734type of the hidden character length argument was a C @code{int}. In 3735order to retain compatibility with older versions, one can e.g. for 3736the following Fortran procedure 3737 3738@smallexample 3739subroutine fstrlen (s, a) 3740 character(len=*) :: s 3741 integer :: a 3742 print*, len(s) 3743end subroutine fstrlen 3744@end smallexample 3745 3746define the corresponding C prototype as follows: 3747 3748@smallexample 3749#if __GNUC__ > 7 3750typedef size_t fortran_charlen_t; 3751#else 3752typedef int fortran_charlen_t; 3753#endif 3754 3755void fstrlen_ (char*, int*, fortran_charlen_t); 3756@end smallexample 3757 3758In order to avoid such compiler-specific details, for new code it is 3759instead recommended to use the ISO_C_BINDING feature. 3760 3761Note with C binding, @code{CHARACTER(len=1)} result variables are 3762returned according to the platform ABI and no hidden length argument 3763is used for dummy arguments; with @code{VALUE}, those variables are 3764passed by value. 3765 3766For @code{OPTIONAL} dummy arguments, an absent argument is denoted 3767by a NULL pointer, except for scalar dummy arguments of type 3768@code{INTEGER}, @code{LOGICAL}, @code{REAL} and @code{COMPLEX} 3769which have the @code{VALUE} attribute. For those, a hidden Boolean 3770argument (@code{logical(kind=C_bool),value}) is used to indicate 3771whether the argument is present. 3772 3773Arguments which are assumed-shape, assumed-rank or deferred-rank 3774arrays or, with @option{-fcoarray=lib}, allocatable scalar coarrays use 3775an array descriptor. All other arrays pass the address of the 3776first element of the array. With @option{-fcoarray=lib}, the token 3777and the offset belonging to nonallocatable coarrays dummy arguments 3778are passed as hidden argument along the character length hidden 3779arguments. The token is an opaque pointer identifying the coarray 3780and the offset is a passed-by-value integer of kind @code{C_PTRDIFF_T}, 3781denoting the byte offset between the base address of the coarray and 3782the passed scalar or first element of the passed array. 3783 3784The arguments are passed in the following order 3785@itemize @bullet 3786@item Result variable, when the function result is passed by reference 3787@item Character length of the function result, if it is a of type 3788@code{CHARACTER} and no C binding is used 3789@item The arguments in the order in which they appear in the Fortran 3790declaration 3791@item The the present status for optional arguments with value attribute, 3792which are internally passed by value 3793@item The character length and/or coarray token and offset for the first 3794argument which is a @code{CHARACTER} or a nonallocatable coarray dummy 3795argument, followed by the hidden arguments of the next dummy argument 3796of such a type 3797@end itemize 3798 3799 3800@c --------------------------------------------------------------------- 3801@c Coarray Programming 3802@c --------------------------------------------------------------------- 3803 3804@node Coarray Programming 3805@chapter Coarray Programming 3806@cindex Coarrays 3807 3808@menu 3809* Type and enum ABI Documentation:: 3810* Function ABI Documentation:: 3811@end menu 3812 3813 3814@node Type and enum ABI Documentation 3815@section Type and enum ABI Documentation 3816 3817@menu 3818* caf_token_t:: 3819* caf_register_t:: 3820* caf_deregister_t:: 3821* caf_reference_t:: 3822* caf_team_t:: 3823@end menu 3824 3825@node caf_token_t 3826@subsection @code{caf_token_t} 3827 3828Typedef of type @code{void *} on the compiler side. Can be any data 3829type on the library side. 3830 3831@node caf_register_t 3832@subsection @code{caf_register_t} 3833 3834Indicates which kind of coarray variable should be registered. 3835 3836@verbatim 3837typedef enum caf_register_t { 3838 CAF_REGTYPE_COARRAY_STATIC, 3839 CAF_REGTYPE_COARRAY_ALLOC, 3840 CAF_REGTYPE_LOCK_STATIC, 3841 CAF_REGTYPE_LOCK_ALLOC, 3842 CAF_REGTYPE_CRITICAL, 3843 CAF_REGTYPE_EVENT_STATIC, 3844 CAF_REGTYPE_EVENT_ALLOC, 3845 CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY, 3846 CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY 3847} 3848caf_register_t; 3849@end verbatim 3850 3851The values @code{CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY} and 3852@code{CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY} are for allocatable components 3853in derived type coarrays only. The first one sets up the token without 3854allocating memory for allocatable component. The latter one only allocates the 3855memory for an allocatable component in a derived type coarray. The token 3856needs to be setup previously by the REGISTER_ONLY. This allows to have 3857allocatable components un-allocated on some images. The status whether an 3858allocatable component is allocated on a remote image can be queried by 3859@code{_caf_is_present} which used internally by the @code{ALLOCATED} 3860intrinsic. 3861 3862@node caf_deregister_t 3863@subsection @code{caf_deregister_t} 3864 3865@verbatim 3866typedef enum caf_deregister_t { 3867 CAF_DEREGTYPE_COARRAY_DEREGISTER, 3868 CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY 3869} 3870caf_deregister_t; 3871@end verbatim 3872 3873Allows to specifiy the type of deregistration of a coarray object. The 3874@code{CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY} flag is only allowed for 3875allocatable components in derived type coarrays. 3876 3877@node caf_reference_t 3878@subsection @code{caf_reference_t} 3879 3880The structure used for implementing arbitrary reference chains. 3881A @code{CAF_REFERENCE_T} allows to specify a component reference or any kind 3882of array reference of any rank supported by gfortran. For array references all 3883kinds as known by the compiler/Fortran standard are supported indicated by 3884a @code{MODE}. 3885 3886@verbatim 3887typedef enum caf_ref_type_t { 3888 /* Reference a component of a derived type, either regular one or an 3889 allocatable or pointer type. For regular ones idx in caf_reference_t is 3890 set to -1. */ 3891 CAF_REF_COMPONENT, 3892 /* Reference an allocatable array. */ 3893 CAF_REF_ARRAY, 3894 /* Reference a non-allocatable/non-pointer array. I.e., the coarray object 3895 has no array descriptor associated and the addressing is done 3896 completely using the ref. */ 3897 CAF_REF_STATIC_ARRAY 3898} caf_ref_type_t; 3899@end verbatim 3900 3901@verbatim 3902typedef enum caf_array_ref_t { 3903 /* No array ref. This terminates the array ref. */ 3904 CAF_ARR_REF_NONE = 0, 3905 /* Reference array elements given by a vector. Only for this mode 3906 caf_reference_t.u.a.dim[i].v is valid. */ 3907 CAF_ARR_REF_VECTOR, 3908 /* A full array ref (:). */ 3909 CAF_ARR_REF_FULL, 3910 /* Reference a range on elements given by start, end and stride. */ 3911 CAF_ARR_REF_RANGE, 3912 /* Only a single item is referenced given in the start member. */ 3913 CAF_ARR_REF_SINGLE, 3914 /* An array ref of the kind (i:), where i is an arbitrary valid index in the 3915 array. The index i is given in the start member. */ 3916 CAF_ARR_REF_OPEN_END, 3917 /* An array ref of the kind (:i), where the lower bound of the array ref 3918 is given by the remote side. The index i is given in the end member. */ 3919 CAF_ARR_REF_OPEN_START 3920} caf_array_ref_t; 3921@end verbatim 3922 3923@verbatim 3924/* References to remote components of a derived type. */ 3925typedef struct caf_reference_t { 3926 /* A pointer to the next ref or NULL. */ 3927 struct caf_reference_t *next; 3928 /* The type of the reference. */ 3929 /* caf_ref_type_t, replaced by int to allow specification in fortran FE. */ 3930 int type; 3931 /* The size of an item referenced in bytes. I.e. in an array ref this is 3932 the factor to advance the array pointer with to get to the next item. 3933 For component refs this gives just the size of the element referenced. */ 3934 size_t item_size; 3935 union { 3936 struct { 3937 /* The offset (in bytes) of the component in the derived type. 3938 Unused for allocatable or pointer components. */ 3939 ptrdiff_t offset; 3940 /* The offset (in bytes) to the caf_token associated with this 3941 component. NULL, when not allocatable/pointer ref. */ 3942 ptrdiff_t caf_token_offset; 3943 } c; 3944 struct { 3945 /* The mode of the array ref. See CAF_ARR_REF_*. */ 3946 /* caf_array_ref_t, replaced by unsigend char to allow specification in 3947 fortran FE. */ 3948 unsigned char mode[GFC_MAX_DIMENSIONS]; 3949 /* The type of a static array. Unset for array's with descriptors. */ 3950 int static_array_type; 3951 /* Subscript refs (s) or vector refs (v). */ 3952 union { 3953 struct { 3954 /* The start and end boundary of the ref and the stride. */ 3955 index_type start, end, stride; 3956 } s; 3957 struct { 3958 /* nvec entries of kind giving the elements to reference. */ 3959 void *vector; 3960 /* The number of entries in vector. */ 3961 size_t nvec; 3962 /* The integer kind used for the elements in vector. */ 3963 int kind; 3964 } v; 3965 } dim[GFC_MAX_DIMENSIONS]; 3966 } a; 3967 } u; 3968} caf_reference_t; 3969@end verbatim 3970 3971The references make up a single linked list of reference operations. The 3972@code{NEXT} member links to the next reference or NULL to indicate the end of 3973the chain. Component and array refs can be arbitrarily mixed as long as they 3974comply to the Fortran standard. 3975 3976@emph{NOTES} 3977The member @code{STATIC_ARRAY_TYPE} is used only when the @code{TYPE} is 3978@code{CAF_REF_STATIC_ARRAY}. The member gives the type of the data referenced. 3979Because no array descriptor is available for a descriptor-less array and 3980type conversion still needs to take place the type is transported here. 3981 3982At the moment @code{CAF_ARR_REF_VECTOR} is not implemented in the front end for 3983descriptor-less arrays. The library caf_single has untested support for it. 3984 3985@node caf_team_t 3986@subsection @code{caf_team_t} 3987 3988Opaque pointer to represent a team-handle. This type is a stand-in for the 3989future implementation of teams. It is about to change without further notice. 3990 3991@node Function ABI Documentation 3992@section Function ABI Documentation 3993 3994@menu 3995* _gfortran_caf_init:: Initialiation function 3996* _gfortran_caf_finish:: Finalization function 3997* _gfortran_caf_this_image:: Querying the image number 3998* _gfortran_caf_num_images:: Querying the maximal number of images 3999* _gfortran_caf_image_status :: Query the status of an image 4000* _gfortran_caf_failed_images :: Get an array of the indexes of the failed images 4001* _gfortran_caf_stopped_images :: Get an array of the indexes of the stopped images 4002* _gfortran_caf_register:: Registering coarrays 4003* _gfortran_caf_deregister:: Deregistering coarrays 4004* _gfortran_caf_is_present:: Query whether an allocatable or pointer component in a derived type coarray is allocated 4005* _gfortran_caf_send:: Sending data from a local image to a remote image 4006* _gfortran_caf_get:: Getting data from a remote image 4007* _gfortran_caf_sendget:: Sending data between remote images 4008* _gfortran_caf_send_by_ref:: Sending data from a local image to a remote image using enhanced references 4009* _gfortran_caf_get_by_ref:: Getting data from a remote image using enhanced references 4010* _gfortran_caf_sendget_by_ref:: Sending data between remote images using enhanced references 4011* _gfortran_caf_lock:: Locking a lock variable 4012* _gfortran_caf_unlock:: Unlocking a lock variable 4013* _gfortran_caf_event_post:: Post an event 4014* _gfortran_caf_event_wait:: Wait that an event occurred 4015* _gfortran_caf_event_query:: Query event count 4016* _gfortran_caf_sync_all:: All-image barrier 4017* _gfortran_caf_sync_images:: Barrier for selected images 4018* _gfortran_caf_sync_memory:: Wait for completion of segment-memory operations 4019* _gfortran_caf_error_stop:: Error termination with exit code 4020* _gfortran_caf_error_stop_str:: Error termination with string 4021* _gfortran_caf_fail_image :: Mark the image failed and end its execution 4022* _gfortran_caf_atomic_define:: Atomic variable assignment 4023* _gfortran_caf_atomic_ref:: Atomic variable reference 4024* _gfortran_caf_atomic_cas:: Atomic compare and swap 4025* _gfortran_caf_atomic_op:: Atomic operation 4026* _gfortran_caf_co_broadcast:: Sending data to all images 4027* _gfortran_caf_co_max:: Collective maximum reduction 4028* _gfortran_caf_co_min:: Collective minimum reduction 4029* _gfortran_caf_co_sum:: Collective summing reduction 4030* _gfortran_caf_co_reduce:: Generic collective reduction 4031@end menu 4032 4033 4034@node _gfortran_caf_init 4035@subsection @code{_gfortran_caf_init} --- Initialiation function 4036@cindex Coarray, _gfortran_caf_init 4037 4038@table @asis 4039@item @emph{Description}: 4040This function is called at startup of the program before the Fortran main 4041program, if the latter has been compiled with @option{-fcoarray=lib}. 4042It takes as arguments the command-line arguments of the program. It is 4043permitted to pass two @code{NULL} pointers as argument; if non-@code{NULL}, 4044the library is permitted to modify the arguments. 4045 4046@item @emph{Syntax}: 4047@code{void _gfortran_caf_init (int *argc, char ***argv)} 4048 4049@item @emph{Arguments}: 4050@multitable @columnfractions .15 .70 4051@item @var{argc} @tab intent(inout) An integer pointer with the number of 4052arguments passed to the program or @code{NULL}. 4053@item @var{argv} @tab intent(inout) A pointer to an array of strings with the 4054command-line arguments or @code{NULL}. 4055@end multitable 4056 4057@item @emph{NOTES} 4058The function is modelled after the initialization function of the Message 4059Passing Interface (MPI) specification. Due to the way coarray registration 4060works, it might not be the first call to the library. If the main program is 4061not written in Fortran and only a library uses coarrays, it can happen that 4062this function is never called. Therefore, it is recommended that the library 4063does not rely on the passed arguments and whether the call has been done. 4064@end table 4065 4066 4067@node _gfortran_caf_finish 4068@subsection @code{_gfortran_caf_finish} --- Finalization function 4069@cindex Coarray, _gfortran_caf_finish 4070 4071@table @asis 4072@item @emph{Description}: 4073This function is called at the end of the Fortran main program, if it has 4074been compiled with the @option{-fcoarray=lib} option. 4075 4076@item @emph{Syntax}: 4077@code{void _gfortran_caf_finish (void)} 4078 4079@item @emph{NOTES} 4080For non-Fortran programs, it is recommended to call the function at the end 4081of the main program. To ensure that the shutdown is also performed for 4082programs where this function is not explicitly invoked, for instance 4083non-Fortran programs or calls to the system's exit() function, the library 4084can use a destructor function. Note that programs can also be terminated 4085using the STOP and ERROR STOP statements; those use different library calls. 4086@end table 4087 4088 4089@node _gfortran_caf_this_image 4090@subsection @code{_gfortran_caf_this_image} --- Querying the image number 4091@cindex Coarray, _gfortran_caf_this_image 4092 4093@table @asis 4094@item @emph{Description}: 4095This function returns the current image number, which is a positive number. 4096 4097@item @emph{Syntax}: 4098@code{int _gfortran_caf_this_image (int distance)} 4099 4100@item @emph{Arguments}: 4101@multitable @columnfractions .15 .70 4102@item @var{distance} @tab As specified for the @code{this_image} intrinsic 4103in TS18508. Shall be a non-negative number. 4104@end multitable 4105 4106@item @emph{NOTES} 4107If the Fortran intrinsic @code{this_image} is invoked without an argument, which 4108is the only permitted form in Fortran 2008, GCC passes @code{0} as 4109first argument. 4110@end table 4111 4112 4113@node _gfortran_caf_num_images 4114@subsection @code{_gfortran_caf_num_images} --- Querying the maximal number of images 4115@cindex Coarray, _gfortran_caf_num_images 4116 4117@table @asis 4118@item @emph{Description}: 4119This function returns the number of images in the current team, if 4120@var{distance} is 0 or the number of images in the parent team at the specified 4121distance. If failed is -1, the function returns the number of all images at 4122the specified distance; if it is 0, the function returns the number of 4123nonfailed images, and if it is 1, it returns the number of failed images. 4124 4125@item @emph{Syntax}: 4126@code{int _gfortran_caf_num_images(int distance, int failed)} 4127 4128@item @emph{Arguments}: 4129@multitable @columnfractions .15 .70 4130@item @var{distance} @tab the distance from this image to the ancestor. 4131Shall be positive. 4132@item @var{failed} @tab shall be -1, 0, or 1 4133@end multitable 4134 4135@item @emph{NOTES} 4136This function follows TS18508. If the num_image intrinsic has no arguments, 4137then the compiler passes @code{distance=0} and @code{failed=-1} to the function. 4138@end table 4139 4140 4141@node _gfortran_caf_image_status 4142@subsection @code{_gfortran_caf_image_status} --- Query the status of an image 4143@cindex Coarray, _gfortran_caf_image_status 4144 4145@table @asis 4146@item @emph{Description}: 4147Get the status of the image given by the id @var{image} of the team given by 4148@var{team}. Valid results are zero, for image is ok, @code{STAT_STOPPED_IMAGE} 4149from the ISO_FORTRAN_ENV module to indicate that the image has been stopped and 4150@code{STAT_FAILED_IMAGE} also from ISO_FORTRAN_ENV to indicate that the image 4151has executed a @code{FAIL IMAGE} statement. 4152 4153@item @emph{Syntax}: 4154@code{int _gfortran_caf_image_status (int image, caf_team_t * team)} 4155 4156@item @emph{Arguments}: 4157@multitable @columnfractions .15 .70 4158@item @var{image} @tab the positive scalar id of the image in the current TEAM. 4159@item @var{team} @tab optional; team on the which the inquiry is to be 4160performed. 4161@end multitable 4162 4163@item @emph{NOTES} 4164This function follows TS18508. Because team-functionality is not yet 4165implemented a null-pointer is passed for the @var{team} argument at the moment. 4166@end table 4167 4168 4169@node _gfortran_caf_failed_images 4170@subsection @code{_gfortran_caf_failed_images} --- Get an array of the indexes of the failed images 4171@cindex Coarray, _gfortran_caf_failed_images 4172 4173@table @asis 4174@item @emph{Description}: 4175Get an array of image indexes in the current @var{team} that have failed. The 4176array is sorted ascendingly. When @var{team} is not provided the current team 4177is to be used. When @var{kind} is provided then the resulting array is of that 4178integer kind else it is of default integer kind. The returns an unallocated 4179size zero array when no images have failed. 4180 4181@item @emph{Syntax}: 4182@code{int _gfortran_caf_failed_images (caf_team_t * team, int * kind)} 4183 4184@item @emph{Arguments}: 4185@multitable @columnfractions .15 .70 4186@item @var{team} @tab optional; team on the which the inquiry is to be 4187performed. 4188@item @var{image} @tab optional; the kind of the resulting integer array. 4189@end multitable 4190 4191@item @emph{NOTES} 4192This function follows TS18508. Because team-functionality is not yet 4193implemented a null-pointer is passed for the @var{team} argument at the moment. 4194@end table 4195 4196 4197@node _gfortran_caf_stopped_images 4198@subsection @code{_gfortran_caf_stopped_images} --- Get an array of the indexes of the stopped images 4199@cindex Coarray, _gfortran_caf_stopped_images 4200 4201@table @asis 4202@item @emph{Description}: 4203Get an array of image indexes in the current @var{team} that have stopped. The 4204array is sorted ascendingly. When @var{team} is not provided the current team 4205is to be used. When @var{kind} is provided then the resulting array is of that 4206integer kind else it is of default integer kind. The returns an unallocated 4207size zero array when no images have failed. 4208 4209@item @emph{Syntax}: 4210@code{int _gfortran_caf_stopped_images (caf_team_t * team, int * kind)} 4211 4212@item @emph{Arguments}: 4213@multitable @columnfractions .15 .70 4214@item @var{team} @tab optional; team on the which the inquiry is to be 4215performed. 4216@item @var{image} @tab optional; the kind of the resulting integer array. 4217@end multitable 4218 4219@item @emph{NOTES} 4220This function follows TS18508. Because team-functionality is not yet 4221implemented a null-pointer is passed for the @var{team} argument at the moment. 4222@end table 4223 4224 4225@node _gfortran_caf_register 4226@subsection @code{_gfortran_caf_register} --- Registering coarrays 4227@cindex Coarray, _gfortran_caf_register 4228 4229@table @asis 4230@item @emph{Description}: 4231Registers memory for a coarray and creates a token to identify the coarray. The 4232routine is called for both coarrays with @code{SAVE} attribute and using an 4233explicit @code{ALLOCATE} statement. If an error occurs and @var{STAT} is a 4234@code{NULL} pointer, the function shall abort with printing an error message 4235and starting the error termination. If no error occurs and @var{STAT} is 4236present, it shall be set to zero. Otherwise, it shall be set to a positive 4237value and, if not-@code{NULL}, @var{ERRMSG} shall be set to a string describing 4238the failure. The routine shall register the memory provided in the 4239@code{DATA}-component of the array descriptor @var{DESC}, when that component 4240is non-@code{NULL}, else it shall allocate sufficient memory and provide a 4241pointer to it in the @code{DATA}-component of @var{DESC}. The array descriptor 4242has rank zero, when a scalar object is to be registered and the array 4243descriptor may be invalid after the call to @code{_gfortran_caf_register}. 4244When an array is to be allocated the descriptor persists. 4245 4246For @code{CAF_REGTYPE_COARRAY_STATIC} and @code{CAF_REGTYPE_COARRAY_ALLOC}, 4247the passed size is the byte size requested. For @code{CAF_REGTYPE_LOCK_STATIC}, 4248@code{CAF_REGTYPE_LOCK_ALLOC} and @code{CAF_REGTYPE_CRITICAL} it is the array 4249size or one for a scalar. 4250 4251When @code{CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY} is used, then only a token 4252for an allocatable or pointer component is created. The @code{SIZE} parameter 4253is not used then. On the contrary when 4254@code{CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY} is specified, then the 4255@var{token} needs to be registered by a previous call with regtype 4256@code{CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY} and either the memory specified 4257in the @var{DESC}'s data-ptr is registered or allocate when the data-ptr is 4258@code{NULL}. 4259 4260@item @emph{Syntax}: 4261@code{void caf_register (size_t size, caf_register_t type, caf_token_t *token, 4262gfc_descriptor_t *desc, int *stat, char *errmsg, size_t errmsg_len)} 4263 4264@item @emph{Arguments}: 4265@multitable @columnfractions .15 .70 4266@item @var{size} @tab For normal coarrays, the byte size of the coarray to be 4267allocated; for lock types and event types, the number of elements. 4268@item @var{type} @tab one of the caf_register_t types. 4269@item @var{token} @tab intent(out) An opaque pointer identifying the coarray. 4270@item @var{desc} @tab intent(inout) The (pseudo) array descriptor. 4271@item @var{stat} @tab intent(out) For allocatable coarrays, stores the STAT=; 4272may be @code{NULL} 4273@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 4274an error message; may be @code{NULL} 4275@item @var{errmsg_len} @tab the buffer size of errmsg. 4276@end multitable 4277 4278@item @emph{NOTES} 4279Nonallocatable coarrays have to be registered prior use from remote images. 4280In order to guarantee this, they have to be registered before the main 4281program. This can be achieved by creating constructor functions. That is what 4282GCC does such that also for nonallocatable coarrays the memory is allocated and 4283no static memory is used. The token permits to identify the coarray; to the 4284processor, the token is a nonaliasing pointer. The library can, for instance, 4285store the base address of the coarray in the token, some handle or a more 4286complicated struct. The library may also store the array descriptor 4287@var{DESC} when its rank is non-zero. 4288 4289For lock types, the value shall only be used for checking the allocation 4290status. Note that for critical blocks, the locking is only required on one 4291image; in the locking statement, the processor shall always pass an 4292image index of one for critical-block lock variables 4293(@code{CAF_REGTYPE_CRITICAL}). For lock types and critical-block variables, 4294the initial value shall be unlocked (or, respectively, not in critical 4295section) such as the value false; for event types, the initial state should 4296be no event, e.g. zero. 4297@end table 4298 4299 4300@node _gfortran_caf_deregister 4301@subsection @code{_gfortran_caf_deregister} --- Deregistering coarrays 4302@cindex Coarray, _gfortran_caf_deregister 4303 4304@table @asis 4305@item @emph{Description}: 4306Called to free or deregister the memory of a coarray; the processor calls this 4307function for automatic and explicit deallocation. In case of an error, this 4308function shall fail with an error message, unless the @var{STAT} variable is 4309not null. The library is only expected to free memory it allocated itself 4310during a call to @code{_gfortran_caf_register}. 4311 4312@item @emph{Syntax}: 4313@code{void caf_deregister (caf_token_t *token, caf_deregister_t type, 4314int *stat, char *errmsg, size_t errmsg_len)} 4315 4316@item @emph{Arguments}: 4317@multitable @columnfractions .15 .70 4318@item @var{token} @tab the token to free. 4319@item @var{type} @tab the type of action to take for the coarray. A 4320@code{CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY} is allowed only for allocatable or 4321pointer components of derived type coarrays. The action only deallocates the 4322local memory without deleting the token. 4323@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL 4324@item @var{errmsg} @tab intent(out) When an error occurs, this will be set 4325to an error message; may be NULL 4326@item @var{errmsg_len} @tab the buffer size of errmsg. 4327@end multitable 4328 4329@item @emph{NOTES} 4330For nonalloatable coarrays this function is never called. If a cleanup is 4331required, it has to be handled via the finish, stop and error stop functions, 4332and via destructors. 4333@end table 4334 4335 4336@node _gfortran_caf_is_present 4337@subsection @code{_gfortran_caf_is_present} --- Query whether an allocatable or pointer component in a derived type coarray is allocated 4338@cindex Coarray, _gfortran_caf_is_present 4339 4340@table @asis 4341@item @emph{Description}: 4342Used to query the coarray library whether an allocatable component in a derived 4343type coarray is allocated on a remote image. 4344 4345@item @emph{Syntax}: 4346@code{void _gfortran_caf_is_present (caf_token_t token, int image_index, 4347gfc_reference_t *ref)} 4348 4349@item @emph{Arguments}: 4350@multitable @columnfractions .15 .70 4351@item @var{token} @tab An opaque pointer identifying the coarray. 4352@item @var{image_index} @tab The ID of the remote image; must be a positive 4353number. 4354@item @var{ref} @tab A chain of references to address the allocatable or 4355pointer component in the derived type coarray. The object reference needs to be 4356a scalar or a full array reference, respectively. 4357@end multitable 4358 4359@end table 4360 4361@node _gfortran_caf_send 4362@subsection @code{_gfortran_caf_send} --- Sending data from a local image to a remote image 4363@cindex Coarray, _gfortran_caf_send 4364 4365@table @asis 4366@item @emph{Description}: 4367Called to send a scalar, an array section or a whole array from a local 4368to a remote image identified by the image_index. 4369 4370@item @emph{Syntax}: 4371@code{void _gfortran_caf_send (caf_token_t token, size_t offset, 4372int image_index, gfc_descriptor_t *dest, caf_vector_t *dst_vector, 4373gfc_descriptor_t *src, int dst_kind, int src_kind, bool may_require_tmp, 4374int *stat)} 4375 4376@item @emph{Arguments}: 4377@multitable @columnfractions .15 .70 4378@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4379@item @var{offset} @tab intent(in) By which amount of bytes the actual data is 4380shifted compared to the base address of the coarray. 4381@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 4382positive number. 4383@item @var{dest} @tab intent(in) Array descriptor for the remote image for the 4384bounds and the size. The @code{base_addr} shall not be accessed. 4385@item @var{dst_vector} @tab intent(in) If not NULL, it contains the vector 4386subscript of the destination array; the values are relative to the dimension 4387triplet of the dest argument. 4388@item @var{src} @tab intent(in) Array descriptor of the local array to be 4389transferred to the remote image 4390@item @var{dst_kind} @tab intent(in) Kind of the destination argument 4391@item @var{src_kind} @tab intent(in) Kind of the source argument 4392@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when 4393it is known at compile time that the @var{dest} and @var{src} either cannot 4394overlap or overlap (fully or partially) such that walking @var{src} and 4395@var{dest} in element wise element order (honoring the stride value) will not 4396lead to wrong results. Otherwise, the value is @code{true}. 4397@item @var{stat} @tab intent(out) when non-NULL give the result of the 4398operation, i.e., zero on success and non-zero on error. When NULL and an error 4399occurs, then an error message is printed and the program is terminated. 4400@end multitable 4401 4402@item @emph{NOTES} 4403It is permitted to have @var{image_index} equal the current image; the memory 4404of the send-to and the send-from might (partially) overlap in that case. The 4405implementation has to take care that it handles this case, e.g. using 4406@code{memmove} which handles (partially) overlapping memory. If 4407@var{may_require_tmp} is true, the library might additionally create a 4408temporary variable, unless additional checks show that this is not required 4409(e.g. because walking backward is possible or because both arrays are 4410contiguous and @code{memmove} takes care of overlap issues). 4411 4412Note that the assignment of a scalar to an array is permitted. In addition, 4413the library has to handle numeric-type conversion and for strings, padding 4414and different character kinds. 4415@end table 4416 4417 4418@node _gfortran_caf_get 4419@subsection @code{_gfortran_caf_get} --- Getting data from a remote image 4420@cindex Coarray, _gfortran_caf_get 4421 4422@table @asis 4423@item @emph{Description}: 4424Called to get an array section or a whole array from a remote, 4425image identified by the image_index. 4426 4427@item @emph{Syntax}: 4428@code{void _gfortran_caf_get (caf_token_t token, size_t offset, 4429int image_index, gfc_descriptor_t *src, caf_vector_t *src_vector, 4430gfc_descriptor_t *dest, int src_kind, int dst_kind, bool may_require_tmp, 4431int *stat)} 4432 4433@item @emph{Arguments}: 4434@multitable @columnfractions .15 .70 4435@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4436@item @var{offset} @tab intent(in) By which amount of bytes the actual data is 4437shifted compared to the base address of the coarray. 4438@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 4439positive number. 4440@item @var{dest} @tab intent(out) Array descriptor of the local array to store 4441the data retrieved from the remote image 4442@item @var{src} @tab intent(in) Array descriptor for the remote image for the 4443bounds and the size. The @code{base_addr} shall not be accessed. 4444@item @var{src_vector} @tab intent(in) If not NULL, it contains the vector 4445subscript of the source array; the values are relative to the dimension 4446triplet of the @var{src} argument. 4447@item @var{dst_kind} @tab intent(in) Kind of the destination argument 4448@item @var{src_kind} @tab intent(in) Kind of the source argument 4449@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when 4450it is known at compile time that the @var{dest} and @var{src} either cannot 4451overlap or overlap (fully or partially) such that walking @var{src} and 4452@var{dest} in element wise element order (honoring the stride value) will not 4453lead to wrong results. Otherwise, the value is @code{true}. 4454@item @var{stat} @tab intent(out) When non-NULL give the result of the 4455operation, i.e., zero on success and non-zero on error. When NULL and an error 4456occurs, then an error message is printed and the program is terminated. 4457@end multitable 4458 4459@item @emph{NOTES} 4460It is permitted to have @var{image_index} equal the current image; the memory of 4461the send-to and the send-from might (partially) overlap in that case. The 4462implementation has to take care that it handles this case, e.g. using 4463@code{memmove} which handles (partially) overlapping memory. If 4464@var{may_require_tmp} is true, the library might additionally create a 4465temporary variable, unless additional checks show that this is not required 4466(e.g. because walking backward is possible or because both arrays are 4467contiguous and @code{memmove} takes care of overlap issues). 4468 4469Note that the library has to handle numeric-type conversion and for strings, 4470padding and different character kinds. 4471@end table 4472 4473 4474@node _gfortran_caf_sendget 4475@subsection @code{_gfortran_caf_sendget} --- Sending data between remote images 4476@cindex Coarray, _gfortran_caf_sendget 4477 4478@table @asis 4479@item @emph{Description}: 4480Called to send a scalar, an array section or a whole array from a remote image 4481identified by the @var{src_image_index} to a remote image identified by the 4482@var{dst_image_index}. 4483 4484@item @emph{Syntax}: 4485@code{void _gfortran_caf_sendget (caf_token_t dst_token, size_t dst_offset, 4486int dst_image_index, gfc_descriptor_t *dest, caf_vector_t *dst_vector, 4487caf_token_t src_token, size_t src_offset, int src_image_index, 4488gfc_descriptor_t *src, caf_vector_t *src_vector, int dst_kind, int src_kind, 4489bool may_require_tmp, int *stat)} 4490 4491@item @emph{Arguments}: 4492@multitable @columnfractions .15 .70 4493@item @var{dst_token} @tab intent(in) An opaque pointer identifying the 4494destination coarray. 4495@item @var{dst_offset} @tab intent(in) By which amount of bytes the actual data 4496is shifted compared to the base address of the destination coarray. 4497@item @var{dst_image_index} @tab intent(in) The ID of the destination remote 4498image; must be a positive number. 4499@item @var{dest} @tab intent(in) Array descriptor for the destination 4500remote image for the bounds and the size. The @code{base_addr} shall not be 4501accessed. 4502@item @var{dst_vector} @tab intent(int) If not NULL, it contains the vector 4503subscript of the destination array; the values are relative to the dimension 4504triplet of the @var{dest} argument. 4505@item @var{src_token} @tab intent(in) An opaque pointer identifying the source 4506coarray. 4507@item @var{src_offset} @tab intent(in) By which amount of bytes the actual data 4508is shifted compared to the base address of the source coarray. 4509@item @var{src_image_index} @tab intent(in) The ID of the source remote image; 4510must be a positive number. 4511@item @var{src} @tab intent(in) Array descriptor of the local array to be 4512transferred to the remote image. 4513@item @var{src_vector} @tab intent(in) Array descriptor of the local array to 4514be transferred to the remote image 4515@item @var{dst_kind} @tab intent(in) Kind of the destination argument 4516@item @var{src_kind} @tab intent(in) Kind of the source argument 4517@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when 4518it is known at compile time that the @var{dest} and @var{src} either cannot 4519overlap or overlap (fully or partially) such that walking @var{src} and 4520@var{dest} in element wise element order (honoring the stride value) will not 4521lead to wrong results. Otherwise, the value is @code{true}. 4522@item @var{stat} @tab intent(out) when non-NULL give the result of the 4523operation, i.e., zero on success and non-zero on error. When NULL and an error 4524occurs, then an error message is printed and the program is terminated. 4525@end multitable 4526 4527@item @emph{NOTES} 4528It is permitted to have the same image index for both @var{src_image_index} and 4529@var{dst_image_index}; the memory of the send-to and the send-from might 4530(partially) overlap in that case. The implementation has to take care that it 4531handles this case, e.g. using @code{memmove} which handles (partially) 4532overlapping memory. If @var{may_require_tmp} is true, the library 4533might additionally create a temporary variable, unless additional checks show 4534that this is not required (e.g. because walking backward is possible or because 4535both arrays are contiguous and @code{memmove} takes care of overlap issues). 4536 4537Note that the assignment of a scalar to an array is permitted. In addition, 4538the library has to handle numeric-type conversion and for strings, padding and 4539different character kinds. 4540@end table 4541 4542@node _gfortran_caf_send_by_ref 4543@subsection @code{_gfortran_caf_send_by_ref} --- Sending data from a local image to a remote image with enhanced referencing options 4544@cindex Coarray, _gfortran_caf_send_by_ref 4545 4546@table @asis 4547@item @emph{Description}: 4548Called to send a scalar, an array section or a whole array from a local to a 4549remote image identified by the @var{image_index}. 4550 4551@item @emph{Syntax}: 4552@code{void _gfortran_caf_send_by_ref (caf_token_t token, int image_index, 4553gfc_descriptor_t *src, caf_reference_t *refs, int dst_kind, int src_kind, 4554bool may_require_tmp, bool dst_reallocatable, int *stat, int dst_type)} 4555 4556@item @emph{Arguments}: 4557@multitable @columnfractions .15 .70 4558@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4559@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 4560positive number. 4561@item @var{src} @tab intent(in) Array descriptor of the local array to be 4562transferred to the remote image 4563@item @var{refs} @tab intent(in) The references on the remote array to store 4564the data given by src. Guaranteed to have at least one entry. 4565@item @var{dst_kind} @tab intent(in) Kind of the destination argument 4566@item @var{src_kind} @tab intent(in) Kind of the source argument 4567@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when 4568it is known at compile time that the @var{dest} and @var{src} either cannot 4569overlap or overlap (fully or partially) such that walking @var{src} and 4570@var{dest} in element wise element order (honoring the stride value) will not 4571lead to wrong results. Otherwise, the value is @code{true}. 4572@item @var{dst_reallocatable} @tab intent(in) Set when the destination is of 4573allocatable or pointer type and the refs will allow reallocation, i.e., the ref 4574is a full array or component ref. 4575@item @var{stat} @tab intent(out) When non-@code{NULL} give the result of the 4576operation, i.e., zero on success and non-zero on error. When @code{NULL} and 4577an error occurs, then an error message is printed and the program is terminated. 4578@item @var{dst_type} @tab intent(in) Give the type of the destination. When 4579the destination is not an array, than the precise type, e.g. of a component in 4580a derived type, is not known, but provided here. 4581@end multitable 4582 4583@item @emph{NOTES} 4584It is permitted to have @var{image_index} equal the current image; the memory of 4585the send-to and the send-from might (partially) overlap in that case. The 4586implementation has to take care that it handles this case, e.g. using 4587@code{memmove} which handles (partially) overlapping memory. If 4588@var{may_require_tmp} is true, the library might additionally create a 4589temporary variable, unless additional checks show that this is not required 4590(e.g. because walking backward is possible or because both arrays are 4591contiguous and @code{memmove} takes care of overlap issues). 4592 4593Note that the assignment of a scalar to an array is permitted. In addition, 4594the library has to handle numeric-type conversion and for strings, padding 4595and different character kinds. 4596 4597Because of the more complicated references possible some operations may be 4598unsupported by certain libraries. The library is expected to issue a precise 4599error message why the operation is not permitted. 4600@end table 4601 4602 4603@node _gfortran_caf_get_by_ref 4604@subsection @code{_gfortran_caf_get_by_ref} --- Getting data from a remote image using enhanced references 4605@cindex Coarray, _gfortran_caf_get_by_ref 4606 4607@table @asis 4608@item @emph{Description}: 4609Called to get a scalar, an array section or a whole array from a remote image 4610identified by the @var{image_index}. 4611 4612@item @emph{Syntax}: 4613@code{void _gfortran_caf_get_by_ref (caf_token_t token, int image_index, 4614caf_reference_t *refs, gfc_descriptor_t *dst, int dst_kind, int src_kind, 4615bool may_require_tmp, bool dst_reallocatable, int *stat, int src_type)} 4616 4617@item @emph{Arguments}: 4618@multitable @columnfractions .15 .70 4619@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4620@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 4621positive number. 4622@item @var{refs} @tab intent(in) The references to apply to the remote structure 4623to get the data. 4624@item @var{dst} @tab intent(in) Array descriptor of the local array to store 4625the data transferred from the remote image. May be reallocated where needed 4626and when @var{DST_REALLOCATABLE} allows it. 4627@item @var{dst_kind} @tab intent(in) Kind of the destination argument 4628@item @var{src_kind} @tab intent(in) Kind of the source argument 4629@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when 4630it is known at compile time that the @var{dest} and @var{src} either cannot 4631overlap or overlap (fully or partially) such that walking @var{src} and 4632@var{dest} in element wise element order (honoring the stride value) will not 4633lead to wrong results. Otherwise, the value is @code{true}. 4634@item @var{dst_reallocatable} @tab intent(in) Set when @var{DST} is of 4635allocatable or pointer type and its refs allow reallocation, i.e., the full 4636array or a component is referenced. 4637@item @var{stat} @tab intent(out) When non-@code{NULL} give the result of the 4638operation, i.e., zero on success and non-zero on error. When @code{NULL} and an 4639error occurs, then an error message is printed and the program is terminated. 4640@item @var{src_type} @tab intent(in) Give the type of the source. When the 4641source is not an array, than the precise type, e.g. of a component in a 4642derived type, is not known, but provided here. 4643@end multitable 4644 4645@item @emph{NOTES} 4646It is permitted to have @code{image_index} equal the current image; the memory 4647of the send-to and the send-from might (partially) overlap in that case. The 4648implementation has to take care that it handles this case, e.g. using 4649@code{memmove} which handles (partially) overlapping memory. If 4650@var{may_require_tmp} is true, the library might additionally create a 4651temporary variable, unless additional checks show that this is not required 4652(e.g. because walking backward is possible or because both arrays are 4653contiguous and @code{memmove} takes care of overlap issues). 4654 4655Note that the library has to handle numeric-type conversion and for strings, 4656padding and different character kinds. 4657 4658Because of the more complicated references possible some operations may be 4659unsupported by certain libraries. The library is expected to issue a precise 4660error message why the operation is not permitted. 4661@end table 4662 4663 4664@node _gfortran_caf_sendget_by_ref 4665@subsection @code{_gfortran_caf_sendget_by_ref} --- Sending data between remote images using enhanced references on both sides 4666@cindex Coarray, _gfortran_caf_sendget_by_ref 4667 4668@table @asis 4669@item @emph{Description}: 4670Called to send a scalar, an array section or a whole array from a remote image 4671identified by the @var{src_image_index} to a remote image identified by the 4672@var{dst_image_index}. 4673 4674@item @emph{Syntax}: 4675@code{void _gfortran_caf_sendget_by_ref (caf_token_t dst_token, 4676int dst_image_index, caf_reference_t *dst_refs, 4677caf_token_t src_token, int src_image_index, caf_reference_t *src_refs, 4678int dst_kind, int src_kind, bool may_require_tmp, int *dst_stat, 4679int *src_stat, int dst_type, int src_type)} 4680 4681@item @emph{Arguments}: 4682@multitable @columnfractions .15 .70 4683@item @var{dst_token} @tab intent(in) An opaque pointer identifying the 4684destination coarray. 4685@item @var{dst_image_index} @tab intent(in) The ID of the destination remote 4686image; must be a positive number. 4687@item @var{dst_refs} @tab intent(in) The references on the remote array to store 4688the data given by the source. Guaranteed to have at least one entry. 4689@item @var{src_token} @tab intent(in) An opaque pointer identifying the source 4690coarray. 4691@item @var{src_image_index} @tab intent(in) The ID of the source remote image; 4692must be a positive number. 4693@item @var{src_refs} @tab intent(in) The references to apply to the remote 4694structure to get the data. 4695@item @var{dst_kind} @tab intent(in) Kind of the destination argument 4696@item @var{src_kind} @tab intent(in) Kind of the source argument 4697@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when 4698it is known at compile time that the @var{dest} and @var{src} either cannot 4699overlap or overlap (fully or partially) such that walking @var{src} and 4700@var{dest} in element wise element order (honoring the stride value) will not 4701lead to wrong results. Otherwise, the value is @code{true}. 4702@item @var{dst_stat} @tab intent(out) when non-@code{NULL} give the result of 4703the send-operation, i.e., zero on success and non-zero on error. When 4704@code{NULL} and an error occurs, then an error message is printed and the 4705program is terminated. 4706@item @var{src_stat} @tab intent(out) When non-@code{NULL} give the result of 4707the get-operation, i.e., zero on success and non-zero on error. When 4708@code{NULL} and an error occurs, then an error message is printed and the 4709program is terminated. 4710@item @var{dst_type} @tab intent(in) Give the type of the destination. When 4711the destination is not an array, than the precise type, e.g. of a component in 4712a derived type, is not known, but provided here. 4713@item @var{src_type} @tab intent(in) Give the type of the source. When the 4714source is not an array, than the precise type, e.g. of a component in a 4715derived type, is not known, but provided here. 4716@end multitable 4717 4718@item @emph{NOTES} 4719It is permitted to have the same image index for both @var{src_image_index} and 4720@var{dst_image_index}; the memory of the send-to and the send-from might 4721(partially) overlap in that case. The implementation has to take care that it 4722handles this case, e.g. using @code{memmove} which handles (partially) 4723overlapping memory. If @var{may_require_tmp} is true, the library 4724might additionally create a temporary variable, unless additional checks show 4725that this is not required (e.g. because walking backward is possible or because 4726both arrays are contiguous and @code{memmove} takes care of overlap issues). 4727 4728Note that the assignment of a scalar to an array is permitted. In addition, 4729the library has to handle numeric-type conversion and for strings, padding and 4730different character kinds. 4731 4732Because of the more complicated references possible some operations may be 4733unsupported by certain libraries. The library is expected to issue a precise 4734error message why the operation is not permitted. 4735@end table 4736 4737 4738@node _gfortran_caf_lock 4739@subsection @code{_gfortran_caf_lock} --- Locking a lock variable 4740@cindex Coarray, _gfortran_caf_lock 4741 4742@table @asis 4743@item @emph{Description}: 4744Acquire a lock on the given image on a scalar locking variable or for the 4745given array element for an array-valued variable. If the @var{acquired_lock} 4746is @code{NULL}, the function returns after having obtained the lock. If it is 4747non-@code{NULL}, then @var{acquired_lock} is assigned the value true (one) when 4748the lock could be obtained and false (zero) otherwise. Locking a lock variable 4749which has already been locked by the same image is an error. 4750 4751@item @emph{Syntax}: 4752@code{void _gfortran_caf_lock (caf_token_t token, size_t index, int image_index, 4753int *acquired_lock, int *stat, char *errmsg, size_t errmsg_len)} 4754 4755@item @emph{Arguments}: 4756@multitable @columnfractions .15 .70 4757@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4758@item @var{index} @tab intent(in) Array index; first array index is 0. For 4759scalars, it is always 0. 4760@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 4761positive number. 4762@item @var{acquired_lock} @tab intent(out) If not NULL, it returns whether lock 4763could be obtained. 4764@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL. 4765@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 4766an error message; may be NULL. 4767@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 4768@end multitable 4769 4770@item @emph{NOTES} 4771This function is also called for critical blocks; for those, the array index 4772is always zero and the image index is one. Libraries are permitted to use other 4773images for critical-block locking variables. 4774@end table 4775 4776@node _gfortran_caf_unlock 4777@subsection @code{_gfortran_caf_lock} --- Unlocking a lock variable 4778@cindex Coarray, _gfortran_caf_unlock 4779 4780@table @asis 4781@item @emph{Description}: 4782Release a lock on the given image on a scalar locking variable or for the 4783given array element for an array-valued variable. Unlocking a lock variable 4784which is unlocked or has been locked by a different image is an error. 4785 4786@item @emph{Syntax}: 4787@code{void _gfortran_caf_unlock (caf_token_t token, size_t index, int image_index, 4788int *stat, char *errmsg, size_t errmsg_len)} 4789 4790@item @emph{Arguments}: 4791@multitable @columnfractions .15 .70 4792@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4793@item @var{index} @tab intent(in) Array index; first array index is 0. For 4794scalars, it is always 0. 4795@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 4796positive number. 4797@item @var{stat} @tab intent(out) For allocatable coarrays, stores the STAT=; 4798may be NULL. 4799@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 4800an error message; may be NULL. 4801@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 4802@end multitable 4803 4804@item @emph{NOTES} 4805This function is also called for critical block; for those, the array index 4806is always zero and the image index is one. Libraries are permitted to use other 4807images for critical-block locking variables. 4808@end table 4809 4810@node _gfortran_caf_event_post 4811@subsection @code{_gfortran_caf_event_post} --- Post an event 4812@cindex Coarray, _gfortran_caf_event_post 4813 4814@table @asis 4815@item @emph{Description}: 4816Increment the event count of the specified event variable. 4817 4818@item @emph{Syntax}: 4819@code{void _gfortran_caf_event_post (caf_token_t token, size_t index, 4820int image_index, int *stat, char *errmsg, size_t errmsg_len)} 4821 4822@item @emph{Arguments}: 4823@multitable @columnfractions .15 .70 4824@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4825@item @var{index} @tab intent(in) Array index; first array index is 0. For 4826scalars, it is always 0. 4827@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 4828positive number; zero indicates the current image, when accessed noncoindexed. 4829@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL. 4830@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 4831an error message; may be NULL. 4832@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 4833@end multitable 4834 4835@item @emph{NOTES} 4836This acts like an atomic add of one to the remote image's event variable. 4837The statement is an image-control statement but does not imply sync memory. 4838Still, all preceeding push communications of this image to the specified 4839remote image have to be completed before @code{event_wait} on the remote 4840image returns. 4841@end table 4842 4843 4844 4845@node _gfortran_caf_event_wait 4846@subsection @code{_gfortran_caf_event_wait} --- Wait that an event occurred 4847@cindex Coarray, _gfortran_caf_event_wait 4848 4849@table @asis 4850@item @emph{Description}: 4851Wait until the event count has reached at least the specified 4852@var{until_count}; if so, atomically decrement the event variable by this 4853amount and return. 4854 4855@item @emph{Syntax}: 4856@code{void _gfortran_caf_event_wait (caf_token_t token, size_t index, 4857int until_count, int *stat, char *errmsg, size_t errmsg_len)} 4858 4859@item @emph{Arguments}: 4860@multitable @columnfractions .15 .70 4861@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4862@item @var{index} @tab intent(in) Array index; first array index is 0. For 4863scalars, it is always 0. 4864@item @var{until_count} @tab intent(in) The number of events which have to be 4865available before the function returns. 4866@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL. 4867@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 4868an error message; may be NULL. 4869@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 4870@end multitable 4871 4872@item @emph{NOTES} 4873This function only operates on a local coarray. It acts like a loop checking 4874atomically the value of the event variable, breaking if the value is greater 4875or equal the requested number of counts. Before the function returns, the 4876event variable has to be decremented by the requested @var{until_count} value. 4877A possible implementation would be a busy loop for a certain number of spins 4878(possibly depending on the number of threads relative to the number of available 4879cores) followed by another waiting strategy such as a sleeping wait (possibly 4880with an increasing number of sleep time) or, if possible, a futex wait. 4881 4882The statement is an image-control statement but does not imply sync memory. 4883Still, all preceeding push communications of this image to the specified 4884remote image have to be completed before @code{event_wait} on the remote 4885image returns. 4886@end table 4887 4888 4889 4890@node _gfortran_caf_event_query 4891@subsection @code{_gfortran_caf_event_query} --- Query event count 4892@cindex Coarray, _gfortran_caf_event_query 4893 4894@table @asis 4895@item @emph{Description}: 4896Return the event count of the specified event variable. 4897 4898@item @emph{Syntax}: 4899@code{void _gfortran_caf_event_query (caf_token_t token, size_t index, 4900int image_index, int *count, int *stat)} 4901 4902@item @emph{Arguments}: 4903@multitable @columnfractions .15 .70 4904@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 4905@item @var{index} @tab intent(in) Array index; first array index is 0. For 4906scalars, it is always 0. 4907@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 4908positive number; zero indicates the current image when accessed noncoindexed. 4909@item @var{count} @tab intent(out) The number of events currently posted to 4910the event variable. 4911@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL. 4912@end multitable 4913 4914@item @emph{NOTES} 4915The typical use is to check the local event variable to only call 4916@code{event_wait} when the data is available. However, a coindexed variable 4917is permitted; there is no ordering or synchronization implied. It acts like 4918an atomic fetch of the value of the event variable. 4919@end table 4920 4921 4922 4923@node _gfortran_caf_sync_all 4924@subsection @code{_gfortran_caf_sync_all} --- All-image barrier 4925@cindex Coarray, _gfortran_caf_sync_all 4926 4927@table @asis 4928@item @emph{Description}: 4929Synchronization of all images in the current team; the program only continues 4930on a given image after this function has been called on all images of the 4931current team. Additionally, it ensures that all pending data transfers of 4932previous segment have completed. 4933 4934@item @emph{Syntax}: 4935@code{void _gfortran_caf_sync_all (int *stat, char *errmsg, size_t errmsg_len)} 4936 4937@item @emph{Arguments}: 4938@multitable @columnfractions .15 .70 4939@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 4940@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 4941an error message; may be NULL. 4942@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 4943@end multitable 4944@end table 4945 4946 4947 4948@node _gfortran_caf_sync_images 4949@subsection @code{_gfortran_caf_sync_images} --- Barrier for selected images 4950@cindex Coarray, _gfortran_caf_sync_images 4951 4952@table @asis 4953@item @emph{Description}: 4954Synchronization between the specified images; the program only continues on a 4955given image after this function has been called on all images specified for 4956that image. Note that one image can wait for all other images in the current 4957team (e.g. via @code{sync images(*)}) while those only wait for that specific 4958image. Additionally, @code{sync images} ensures that all pending data 4959transfers of previous segments have completed. 4960 4961@item @emph{Syntax}: 4962@code{void _gfortran_caf_sync_images (int count, int images[], int *stat, 4963char *errmsg, size_t errmsg_len)} 4964 4965@item @emph{Arguments}: 4966@multitable @columnfractions .15 .70 4967@item @var{count} @tab intent(in) The number of images which are provided in 4968the next argument. For a zero-sized array, the value is zero. For 4969@code{sync images (*)}, the value is @math{-1}. 4970@item @var{images} @tab intent(in) An array with the images provided by the 4971user. If @var{count} is zero, a NULL pointer is passed. 4972@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 4973@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 4974an error message; may be NULL. 4975@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 4976@end multitable 4977@end table 4978 4979 4980 4981@node _gfortran_caf_sync_memory 4982@subsection @code{_gfortran_caf_sync_memory} --- Wait for completion of segment-memory operations 4983@cindex Coarray, _gfortran_caf_sync_memory 4984 4985@table @asis 4986@item @emph{Description}: 4987Acts as optimization barrier between different segments. It also ensures that 4988all pending memory operations of this image have been completed. 4989 4990@item @emph{Syntax}: 4991@code{void _gfortran_caf_sync_memory (int *stat, char *errmsg, size_t errmsg_len)} 4992 4993@item @emph{Arguments}: 4994@multitable @columnfractions .15 .70 4995@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 4996@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 4997an error message; may be NULL. 4998@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 4999@end multitable 5000 5001@item @emph{NOTE} A simple implementation could be 5002@code{__asm__ __volatile__ ("":::"memory")} to prevent code movements. 5003@end table 5004 5005 5006 5007@node _gfortran_caf_error_stop 5008@subsection @code{_gfortran_caf_error_stop} --- Error termination with exit code 5009@cindex Coarray, _gfortran_caf_error_stop 5010 5011@table @asis 5012@item @emph{Description}: 5013Invoked for an @code{ERROR STOP} statement which has an integer argument. The 5014function should terminate the program with the specified exit code. 5015 5016 5017@item @emph{Syntax}: 5018@code{void _gfortran_caf_error_stop (int error)} 5019 5020@item @emph{Arguments}: 5021@multitable @columnfractions .15 .70 5022@item @var{error} @tab intent(in) The exit status to be used. 5023@end multitable 5024@end table 5025 5026 5027 5028@node _gfortran_caf_error_stop_str 5029@subsection @code{_gfortran_caf_error_stop_str} --- Error termination with string 5030@cindex Coarray, _gfortran_caf_error_stop_str 5031 5032@table @asis 5033@item @emph{Description}: 5034Invoked for an @code{ERROR STOP} statement which has a string as argument. The 5035function should terminate the program with a nonzero-exit code. 5036 5037@item @emph{Syntax}: 5038@code{void _gfortran_caf_error_stop (const char *string, size_t len)} 5039 5040@item @emph{Arguments}: 5041@multitable @columnfractions .15 .70 5042@item @var{string} @tab intent(in) the error message (not zero terminated) 5043@item @var{len} @tab intent(in) the length of the string 5044@end multitable 5045@end table 5046 5047 5048 5049@node _gfortran_caf_fail_image 5050@subsection @code{_gfortran_caf_fail_image} --- Mark the image failed and end its execution 5051@cindex Coarray, _gfortran_caf_fail_image 5052 5053@table @asis 5054@item @emph{Description}: 5055Invoked for an @code{FAIL IMAGE} statement. The function should terminate the 5056current image. 5057 5058@item @emph{Syntax}: 5059@code{void _gfortran_caf_fail_image ()} 5060 5061@item @emph{NOTES} 5062This function follows TS18508. 5063@end table 5064 5065 5066 5067@node _gfortran_caf_atomic_define 5068@subsection @code{_gfortran_caf_atomic_define} --- Atomic variable assignment 5069@cindex Coarray, _gfortran_caf_atomic_define 5070 5071@table @asis 5072@item @emph{Description}: 5073Assign atomically a value to an integer or logical variable. 5074 5075@item @emph{Syntax}: 5076@code{void _gfortran_caf_atomic_define (caf_token_t token, size_t offset, 5077int image_index, void *value, int *stat, int type, int kind)} 5078 5079@item @emph{Arguments}: 5080@multitable @columnfractions .15 .70 5081@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 5082@item @var{offset} @tab intent(in) By which amount of bytes the actual data is 5083shifted compared to the base address of the coarray. 5084@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 5085positive number; zero indicates the current image when used noncoindexed. 5086@item @var{value} @tab intent(in) the value to be assigned, passed by reference 5087@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5088@item @var{type} @tab intent(in) The data type, i.e. @code{BT_INTEGER} (1) or 5089@code{BT_LOGICAL} (2). 5090@item @var{kind} @tab intent(in) The kind value (only 4; always @code{int}) 5091@end multitable 5092@end table 5093 5094 5095 5096@node _gfortran_caf_atomic_ref 5097@subsection @code{_gfortran_caf_atomic_ref} --- Atomic variable reference 5098@cindex Coarray, _gfortran_caf_atomic_ref 5099 5100@table @asis 5101@item @emph{Description}: 5102Reference atomically a value of a kind-4 integer or logical variable. 5103 5104@item @emph{Syntax}: 5105@code{void _gfortran_caf_atomic_ref (caf_token_t token, size_t offset, 5106int image_index, void *value, int *stat, int type, int kind)} 5107 5108@item @emph{Arguments}: 5109@multitable @columnfractions .15 .70 5110@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 5111@item @var{offset} @tab intent(in) By which amount of bytes the actual data is 5112shifted compared to the base address of the coarray. 5113@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 5114positive number; zero indicates the current image when used noncoindexed. 5115@item @var{value} @tab intent(out) The variable assigned the atomically 5116referenced variable. 5117@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5118@item @var{type} @tab the data type, i.e. @code{BT_INTEGER} (1) or 5119@code{BT_LOGICAL} (2). 5120@item @var{kind} @tab The kind value (only 4; always @code{int}) 5121@end multitable 5122@end table 5123 5124 5125 5126@node _gfortran_caf_atomic_cas 5127@subsection @code{_gfortran_caf_atomic_cas} --- Atomic compare and swap 5128@cindex Coarray, _gfortran_caf_atomic_cas 5129 5130@table @asis 5131@item @emph{Description}: 5132Atomic compare and swap of a kind-4 integer or logical variable. Assigns 5133atomically the specified value to the atomic variable, if the latter has 5134the value specified by the passed condition value. 5135 5136@item @emph{Syntax}: 5137@code{void _gfortran_caf_atomic_cas (caf_token_t token, size_t offset, 5138int image_index, void *old, void *compare, void *new_val, int *stat, 5139int type, int kind)} 5140 5141@item @emph{Arguments}: 5142@multitable @columnfractions .15 .70 5143@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 5144@item @var{offset} @tab intent(in) By which amount of bytes the actual data is 5145shifted compared to the base address of the coarray. 5146@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 5147positive number; zero indicates the current image when used noncoindexed. 5148@item @var{old} @tab intent(out) The value which the atomic variable had 5149just before the cas operation. 5150@item @var{compare} @tab intent(in) The value used for comparision. 5151@item @var{new_val} @tab intent(in) The new value for the atomic variable, 5152assigned to the atomic variable, if @code{compare} equals the value of the 5153atomic variable. 5154@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5155@item @var{type} @tab intent(in) the data type, i.e. @code{BT_INTEGER} (1) or 5156@code{BT_LOGICAL} (2). 5157@item @var{kind} @tab intent(in) The kind value (only 4; always @code{int}) 5158@end multitable 5159@end table 5160 5161 5162 5163@node _gfortran_caf_atomic_op 5164@subsection @code{_gfortran_caf_atomic_op} --- Atomic operation 5165@cindex Coarray, _gfortran_caf_atomic_op 5166 5167@table @asis 5168@item @emph{Description}: 5169Apply an operation atomically to an atomic integer or logical variable. 5170After the operation, @var{old} contains the value just before the operation, 5171which, respectively, adds (GFC_CAF_ATOMIC_ADD) atomically the @code{value} to 5172the atomic integer variable or does a bitwise AND, OR or exclusive OR 5173between the atomic variable and @var{value}; the result is then stored in the 5174atomic variable. 5175 5176@item @emph{Syntax}: 5177@code{void _gfortran_caf_atomic_op (int op, caf_token_t token, size_t offset, 5178int image_index, void *value, void *old, int *stat, int type, int kind)} 5179 5180@item @emph{Arguments}: 5181@multitable @columnfractions .15 .70 5182@item @var{op} @tab intent(in) the operation to be performed; possible values 5183@code{GFC_CAF_ATOMIC_ADD} (1), @code{GFC_CAF_ATOMIC_AND} (2), 5184@code{GFC_CAF_ATOMIC_OR} (3), @code{GFC_CAF_ATOMIC_XOR} (4). 5185@item @var{token} @tab intent(in) An opaque pointer identifying the coarray. 5186@item @var{offset} @tab intent(in) By which amount of bytes the actual data is 5187shifted compared to the base address of the coarray. 5188@item @var{image_index} @tab intent(in) The ID of the remote image; must be a 5189positive number; zero indicates the current image when used noncoindexed. 5190@item @var{old} @tab intent(out) The value which the atomic variable had 5191just before the atomic operation. 5192@item @var{val} @tab intent(in) The new value for the atomic variable, 5193assigned to the atomic variable, if @code{compare} equals the value of the 5194atomic variable. 5195@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5196@item @var{type} @tab intent(in) the data type, i.e. @code{BT_INTEGER} (1) or 5197@code{BT_LOGICAL} (2) 5198@item @var{kind} @tab intent(in) the kind value (only 4; always @code{int}) 5199@end multitable 5200@end table 5201 5202 5203 5204 5205@node _gfortran_caf_co_broadcast 5206@subsection @code{_gfortran_caf_co_broadcast} --- Sending data to all images 5207@cindex Coarray, _gfortran_caf_co_broadcast 5208 5209@table @asis 5210@item @emph{Description}: 5211Distribute a value from a given image to all other images in the team. Has to 5212be called collectively. 5213 5214@item @emph{Syntax}: 5215@code{void _gfortran_caf_co_broadcast (gfc_descriptor_t *a, 5216int source_image, int *stat, char *errmsg, size_t errmsg_len)} 5217 5218@item @emph{Arguments}: 5219@multitable @columnfractions .15 .70 5220@item @var{a} @tab intent(inout) An array descriptor with the data to be 5221broadcasted (on @var{source_image}) or to be received (other images). 5222@item @var{source_image} @tab intent(in) The ID of the image from which the 5223data should be broadcasted. 5224@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5225@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 5226an error message; may be NULL. 5227@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg. 5228@end multitable 5229@end table 5230 5231 5232 5233@node _gfortran_caf_co_max 5234@subsection @code{_gfortran_caf_co_max} --- Collective maximum reduction 5235@cindex Coarray, _gfortran_caf_co_max 5236 5237@table @asis 5238@item @emph{Description}: 5239Calculates for each array element of the variable @var{a} the maximum 5240value for that element in the current team; if @var{result_image} has the 5241value 0, the result shall be stored on all images, otherwise, only on the 5242specified image. This function operates on numeric values and character 5243strings. 5244 5245@item @emph{Syntax}: 5246@code{void _gfortran_caf_co_max (gfc_descriptor_t *a, int result_image, 5247int *stat, char *errmsg, int a_len, size_t errmsg_len)} 5248 5249@item @emph{Arguments}: 5250@multitable @columnfractions .15 .70 5251@item @var{a} @tab intent(inout) An array descriptor for the data to be 5252processed. On the destination image(s) the result overwrites the old content. 5253@item @var{result_image} @tab intent(in) The ID of the image to which the 5254reduced value should be copied to; if zero, it has to be copied to all images. 5255@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5256@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 5257an error message; may be NULL. 5258@item @var{a_len} @tab intent(in) the string length of argument @var{a} 5259@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 5260@end multitable 5261 5262@item @emph{NOTES} 5263If @var{result_image} is nonzero, the data in the array descriptor @var{a} on 5264all images except of the specified one become undefined; hence, the library may 5265make use of this. 5266@end table 5267 5268 5269 5270@node _gfortran_caf_co_min 5271@subsection @code{_gfortran_caf_co_min} --- Collective minimum reduction 5272@cindex Coarray, _gfortran_caf_co_min 5273 5274@table @asis 5275@item @emph{Description}: 5276Calculates for each array element of the variable @var{a} the minimum 5277value for that element in the current team; if @var{result_image} has the 5278value 0, the result shall be stored on all images, otherwise, only on the 5279specified image. This function operates on numeric values and character 5280strings. 5281 5282@item @emph{Syntax}: 5283@code{void _gfortran_caf_co_min (gfc_descriptor_t *a, int result_image, 5284int *stat, char *errmsg, int a_len, size_t errmsg_len)} 5285 5286@item @emph{Arguments}: 5287@multitable @columnfractions .15 .70 5288@item @var{a} @tab intent(inout) An array descriptor for the data to be 5289processed. On the destination image(s) the result overwrites the old content. 5290@item @var{result_image} @tab intent(in) The ID of the image to which the 5291reduced value should be copied to; if zero, it has to be copied to all images. 5292@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5293@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 5294an error message; may be NULL. 5295@item @var{a_len} @tab intent(in) the string length of argument @var{a} 5296@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 5297@end multitable 5298 5299@item @emph{NOTES} 5300If @var{result_image} is nonzero, the data in the array descriptor @var{a} on 5301all images except of the specified one become undefined; hence, the library may 5302make use of this. 5303@end table 5304 5305 5306 5307@node _gfortran_caf_co_sum 5308@subsection @code{_gfortran_caf_co_sum} --- Collective summing reduction 5309@cindex Coarray, _gfortran_caf_co_sum 5310 5311@table @asis 5312@item @emph{Description}: 5313Calculates for each array element of the variable @var{a} the sum of all 5314values for that element in the current team; if @var{result_image} has the 5315value 0, the result shall be stored on all images, otherwise, only on the 5316specified image. This function operates on numeric values only. 5317 5318@item @emph{Syntax}: 5319@code{void _gfortran_caf_co_sum (gfc_descriptor_t *a, int result_image, 5320int *stat, char *errmsg, size_t errmsg_len)} 5321 5322@item @emph{Arguments}: 5323@multitable @columnfractions .15 .70 5324@item @var{a} @tab intent(inout) An array descriptor with the data to be 5325processed. On the destination image(s) the result overwrites the old content. 5326@item @var{result_image} @tab intent(in) The ID of the image to which the 5327reduced value should be copied to; if zero, it has to be copied to all images. 5328@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5329@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 5330an error message; may be NULL. 5331@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 5332@end multitable 5333 5334@item @emph{NOTES} 5335If @var{result_image} is nonzero, the data in the array descriptor @var{a} on 5336all images except of the specified one become undefined; hence, the library may 5337make use of this. 5338@end table 5339 5340 5341 5342@node _gfortran_caf_co_reduce 5343@subsection @code{_gfortran_caf_co_reduce} --- Generic collective reduction 5344@cindex Coarray, _gfortran_caf_co_reduce 5345 5346@table @asis 5347@item @emph{Description}: 5348Calculates for each array element of the variable @var{a} the reduction 5349value for that element in the current team; if @var{result_image} has the 5350value 0, the result shall be stored on all images, otherwise, only on the 5351specified image. The @var{opr} is a pure function doing a mathematically 5352commutative and associative operation. 5353 5354The @var{opr_flags} denote the following; the values are bitwise ored. 5355@code{GFC_CAF_BYREF} (1) if the result should be returned 5356by reference; @code{GFC_CAF_HIDDENLEN} (2) whether the result and argument 5357string lengths shall be specified as hidden arguments; 5358@code{GFC_CAF_ARG_VALUE} (4) whether the arguments shall be passed by value, 5359@code{GFC_CAF_ARG_DESC} (8) whether the arguments shall be passed by descriptor. 5360 5361 5362@item @emph{Syntax}: 5363@code{void _gfortran_caf_co_reduce (gfc_descriptor_t *a, 5364void * (*opr) (void *, void *), int opr_flags, int result_image, 5365int *stat, char *errmsg, int a_len, size_t errmsg_len)} 5366 5367@item @emph{Arguments}: 5368@multitable @columnfractions .15 .70 5369@item @var{a} @tab intent(inout) An array descriptor with the data to be 5370processed. On the destination image(s) the result overwrites the old content. 5371@item @var{opr} @tab intent(in) Function pointer to the reduction function 5372@item @var{opr_flags} @tab intent(in) Flags regarding the reduction function 5373@item @var{result_image} @tab intent(in) The ID of the image to which the 5374reduced value should be copied to; if zero, it has to be copied to all images. 5375@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL. 5376@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to 5377an error message; may be NULL. 5378@item @var{a_len} @tab intent(in) the string length of argument @var{a} 5379@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg 5380@end multitable 5381 5382@item @emph{NOTES} 5383If @var{result_image} is nonzero, the data in the array descriptor @var{a} on 5384all images except of the specified one become undefined; hence, the library may 5385make use of this. 5386 5387For character arguments, the result is passed as first argument, followed 5388by the result string length, next come the two string arguments, followed 5389by the two hidden string length arguments. With C binding, there are no hidden 5390arguments and by-reference passing and either only a single character is passed 5391or an array descriptor. 5392@end table 5393 5394 5395@c Intrinsic Procedures 5396@c --------------------------------------------------------------------- 5397 5398@include intrinsic.texi 5399 5400 5401@tex 5402\blankpart 5403@end tex 5404 5405@c --------------------------------------------------------------------- 5406@c Contributing 5407@c --------------------------------------------------------------------- 5408 5409@node Contributing 5410@unnumbered Contributing 5411@cindex Contributing 5412 5413Free software is only possible if people contribute to efforts 5414to create it. 5415We're always in need of more people helping out with ideas 5416and comments, writing documentation and contributing code. 5417 5418If you want to contribute to GNU Fortran, 5419have a look at the long lists of projects you can take on. 5420Some of these projects are small, 5421some of them are large; 5422some are completely orthogonal to the rest of what is 5423happening on GNU Fortran, 5424but others are ``mainstream'' projects in need of enthusiastic hackers. 5425All of these projects are important! 5426We will eventually get around to the things here, 5427but they are also things doable by someone who is willing and able. 5428 5429@menu 5430* Contributors:: 5431* Projects:: 5432@end menu 5433 5434 5435@node Contributors 5436@section Contributors to GNU Fortran 5437@cindex Contributors 5438@cindex Credits 5439@cindex Authors 5440 5441Most of the parser was hand-crafted by @emph{Andy Vaught}, who is 5442also the initiator of the whole project. Thanks Andy! 5443Most of the interface with GCC was written by @emph{Paul Brook}. 5444 5445The following individuals have contributed code and/or 5446ideas and significant help to the GNU Fortran project 5447(in alphabetical order): 5448 5449@itemize @minus 5450@item Janne Blomqvist 5451@item Steven Bosscher 5452@item Paul Brook 5453@item Tobias Burnus 5454@item Fran@,{c}ois-Xavier Coudert 5455@item Bud Davis 5456@item Jerry DeLisle 5457@item Erik Edelmann 5458@item Bernhard Fischer 5459@item Daniel Franke 5460@item Richard Guenther 5461@item Richard Henderson 5462@item Katherine Holcomb 5463@item Jakub Jelinek 5464@item Niels Kristian Bech Jensen 5465@item Steven Johnson 5466@item Steven G. Kargl 5467@item Thomas Koenig 5468@item Asher Langton 5469@item H. J. Lu 5470@item Toon Moene 5471@item Brooks Moses 5472@item Andrew Pinski 5473@item Tim Prince 5474@item Christopher D. Rickett 5475@item Richard Sandiford 5476@item Tobias Schl@"uter 5477@item Roger Sayle 5478@item Paul Thomas 5479@item Andy Vaught 5480@item Feng Wang 5481@item Janus Weil 5482@item Daniel Kraft 5483@end itemize 5484 5485The following people have contributed bug reports, 5486smaller or larger patches, 5487and much needed feedback and encouragement for the 5488GNU Fortran project: 5489 5490@itemize @minus 5491@item Bill Clodius 5492@item Dominique d'Humi@`eres 5493@item Kate Hedstrom 5494@item Erik Schnetter 5495@item Gerhard Steinmetz 5496@item Joost VandeVondele 5497@end itemize 5498 5499Many other individuals have helped debug, 5500test and improve the GNU Fortran compiler over the past few years, 5501and we welcome you to do the same! 5502If you already have done so, 5503and you would like to see your name listed in the 5504list above, please contact us. 5505 5506 5507@node Projects 5508@section Projects 5509 5510@table @emph 5511 5512@item Help build the test suite 5513Solicit more code for donation to the test suite: the more extensive the 5514testsuite, the smaller the risk of breaking things in the future! We can 5515keep code private on request. 5516 5517@item Bug hunting/squishing 5518Find bugs and write more test cases! Test cases are especially very 5519welcome, because it allows us to concentrate on fixing bugs instead of 5520isolating them. Going through the bugzilla database at 5521@url{https://gcc.gnu.org/@/bugzilla/} to reduce testcases posted there and 5522add more information (for example, for which version does the testcase 5523work, for which versions does it fail?) is also very helpful. 5524 5525@item Missing features 5526For a larger project, consider working on the missing features required for 5527Fortran language standards compliance (@pxref{Standards}), or contributing 5528to the implementation of extensions such as OpenMP (@pxref{OpenMP}) or 5529OpenACC (@pxref{OpenACC}) that are under active development. Again, 5530contributing test cases for these features is useful too! 5531 5532@end table 5533 5534 5535@c --------------------------------------------------------------------- 5536@c GNU General Public License 5537@c --------------------------------------------------------------------- 5538 5539@include gpl_v3.texi 5540 5541 5542 5543@c --------------------------------------------------------------------- 5544@c GNU Free Documentation License 5545@c --------------------------------------------------------------------- 5546 5547@include fdl.texi 5548 5549 5550 5551@c --------------------------------------------------------------------- 5552@c Funding Free Software 5553@c --------------------------------------------------------------------- 5554 5555@include funding.texi 5556 5557@c --------------------------------------------------------------------- 5558@c Indices 5559@c --------------------------------------------------------------------- 5560 5561@node Option Index 5562@unnumbered Option Index 5563@command{gfortran}'s command line options are indexed here without any 5564initial @samp{-} or @samp{--}. Where an option has both positive and 5565negative forms (such as -foption and -fno-option), relevant entries in 5566the manual are indexed under the most appropriate form; it may sometimes 5567be useful to look up both forms. 5568@printindex op 5569 5570@node Keyword Index 5571@unnumbered Keyword Index 5572@printindex cp 5573 5574@bye 5575