developers:fortran_2003
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
developers:fortran_2003 [2014/10/19 12:24] – [Use of interoperability with C] Matteo Giantomassi | developers:fortran_2003 [2024/09/02 16:21] (current) – Maryam Azizi | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <WRAP important> | ||
+ | |||
+ | ====== New FORTRAN 2003 features for ABINIT 8 ====== | ||
+ | \\ \\ | ||
+ | During the 6< | ||
+ | |||
+ | To choose this set of features, we tried to respect the following rules: | ||
+ | |||
+ | * The F2003 features must be supported by a large majority of recent compilers. An exhaustive list of compilers and their F2003 features can be found at:\\ [[http:// | ||
+ | |||
+ | * We have focused on the features that are of significant importance for high-performance computing and on the language extensions that help improve the readability and the portability of the code | ||
+ | |||
+ | This is a first attempt; other F2003 extensions are not admitted for the moment, but their inclusion could be taken into consideration in the next versions. | ||
+ | Important: introduction of the new F2003 extensions implies that, from now, not all compilers can be supported by ABINIT (see first section of the document). | ||
+ | |||
+ | //M. Torrent (CEA-Bruyères-le-Châtel)// | ||
+ | //M. Giantomassi (UC Louvain)// | ||
+ | \\ \\ | ||
+ | |||
+ | < | ||
+ | ===== I. Notes on Fortran compilers and ABINIT ===== | ||
+ | < | ||
+ | |||
+ | * All the new features proposed hereafter have already been tested on the ABINIT test farm. If someone wants ABINIT to be compatible with another computing environment, | ||
+ | |||
+ | * Some of these extensions are **mandatory** in the sense that it won’t be possible to compile the code if the compiler does not support these F2003 features (e.g. '' | ||
+ | |||
+ | * Other extensions are **optional** since they can be made available in a portable way via CPP macros and/or wrapper functions so that code semantics is preserved (e.g. '' | ||
+ | |||
+ | We propose to divide the compilers in the following four categories: | ||
+ | |||
+ | - **Officially supported**\\ All tests are OK, the most important libraries and plugins are enabled and tested.\\ \\ | ||
+ | - **Deprecated**\\ Still officially supported but users are strongly encouraged to upgrade to a more recent version or to use one of the officially supported compilers since future releases of ABINIT may drop support for this version of the compiler.\\ Example: //g95// (no more maintained)\\ \\ | ||
+ | - **Unstable**\\ We cannot ensure that all the features of ABINIT and the most important plugins and libraries work as expected with this version of the compiler.\\ Example: //PGI v9+//\\ \\ | ||
+ | - **Unsupported\\ **Due to the introduction of the F2003 extensions, some old compilers will be excluded //de facto//.\\ Example: // | ||
+ | \\ | ||
+ | < | ||
+ | ===== II. Fortran 2003 features allowed in ABINIT ===== | ||
+ | //Mandatory extensions, implemented in (almost) all recent compilers// | ||
+ | < | ||
+ | |||
+ | ==== ALLOCATABLE arrays inside user-defined datatypes and as routine arguments ==== | ||
+ | |||
+ | This was not possible in F95 but it is perfectly legit in F2003.\\ | ||
+ | Note that '' | ||
+ | - An allocatable array is always continuous in memory and the compiler can perform important optimizations that are not possible when pointers are involved. Pointers, indeed, may point to non-contiguous memory locations, and the compiler MUST take this possibility into account thus leading to suboptimal code. | ||
+ | - The initial status of a pointer is '' | ||
+ | - Runtime errors are much easier to detect and debug. | ||
+ | |||
+ | Example of a user-defined data type with an allocatable entity: | ||
+ | <code fortran> | ||
+ | type (pawtab_type) | ||
+ | | ||
+ | end type pawtab_type | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | if (allocated(pawtab%phi)) deallocate(pawtab%phi) | ||
+ | </ | ||
+ | |||
+ | //As a rule of thumb, avoid pointers as much a possible. Use allocatable arrays if you need dynamic memory in a datatype or if you want to allocate memory inside a procedure and return it to the caller.// | ||
+ | |||
+ | Example of a procedure that allocates memory and returns it to the caller: | ||
+ | <code fortran> | ||
+ | | ||
+ | |||
+ | integer, | ||
+ | |||
+ | !Compute the size of out_array | ||
+ | n = ... | ||
+ | |||
+ | !Allocate contiguous memory | ||
+ | allocate(out_array(n) | ||
+ | out_array = ... | ||
+ | |||
+ | end subroutine foo | ||
+ | </ | ||
+ | \\ | ||
+ | ==== INTENT attribute for pointers ==== | ||
+ | |||
+ | Following the ABINIT coding rules, each argument of routine should have an '' | ||
+ | |||
+ | Example: | ||
+ | <code fortran> | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Note that the intent refers to the __association status of the pointer__ without any reference to the target. If you are not sure about which intent should be used, use '' | ||
+ | \\ \\ | ||
+ | ==== Access to computing environment ==== | ||
+ | |||
+ | F2003 gives access to a lot of information about the computing environment. | ||
+ | |||
+ | Example: | ||
+ | <code fortran> | ||
+ | CALL GET_ENVIRONMENT_VARIABLE(…) ! to get an environment variable | ||
+ | CALL GET_COMMAND(..) | ||
+ | CALL COMMAND_ARGUMENT_COUNT(..) | ||
+ | CALL GET_COMMAND_ARGUMENT(, | ||
+ | </ | ||
+ | |||
+ | These features can be used to improve the user interface (e.g. command line options can be passed to ABINIT), or to modify the behavior at run-time depending on the value of the environment variables. | ||
+ | \\ \\ | ||
+ | ==== Array constructor with [ ] syntax ==== | ||
+ | |||
+ | This simple extension makes the array constructor syntax more readable. Developers are encouraged to use the new syntax whenever possible in order to improve the readability of the code. | ||
+ | |||
+ | Example : | ||
+ | <code fortran> | ||
+ | | ||
+ | </ | ||
+ | \\ | ||
+ | ==== Named constants in complex constant declaration ==== | ||
+ | |||
+ | A very simple – but useful – feature. | ||
+ | |||
+ | Example: | ||
+ | <code fortran> | ||
+ | | ||
+ | </ | ||
+ | \\ | ||
+ | ==== Use of interoperability with C ==== | ||
+ | |||
+ | The F2003 intrinsic module '' | ||
+ | * Call C procedures from Fortran (e.g. FFTW3), | ||
+ | * Pass complex arguments to Fortran functions having an explicit interface with reals without having to perform a detrimental copy-in, copy out. | ||
+ | For this, the availability of the '' | ||
+ | |||
+ | At present, ABINIT requires a '' | ||
+ | * C types: '' | ||
+ | * C pointer '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | These features are already used in important parts such as '' | ||
+ | |||
+ | Example (how to call a C function): | ||
+ | <code fortran> | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Example: | ||
+ | How to avoid a copy while passing real arrays to a procedure that expects complex arguments and has an explicit interface\\ | ||
+ | Copy //real// array '' | ||
+ | <code fortran> | ||
+ | | ||
+ | | ||
+ | |||
+ | call C_F_POINTER(C_LOC(data1_real), | ||
+ | call C_F_POINTER(C_LOC(data2_real), | ||
+ | |||
+ | !If zcopy has an explicit interface, the compiler won’t | ||
+ | | ||
+ | !an argument of type complex(dpc) | ||
+ | |||
+ | call zcopy(nn, | ||
+ | </ | ||
+ | |||
+ | We propose to hide all this stuff in a «helper function» that will return a complex Fortran pointer from a real Fortran array. | ||
+ | \\ \\ | ||
+ | ==== IMPORT statement ==== | ||
+ | |||
+ | Used in interfaces, it allows the access to the variables and the types defined in modules. | ||
+ | |||
+ | Example: | ||
+ | <code fortran> | ||
+ | use m_pawtab, only : pawtab_type | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | end interface | ||
+ | </ | ||
+ | \\ \\ | ||
+ | |||
+ | < | ||
+ | ===== III. Fortran 2003 features allowed in ABINIT only by the use of specific functions ===== | ||
+ | //Optional extensions, implemented in the majority of compilers, but not all// | ||
+ | < | ||
+ | |||
+ | ==== Flush statement ==== | ||
+ | |||
+ | The execution of a '' | ||
+ | |||
+ | One should flush Fortran files though the '' | ||
+ | The reason for such limitation is that not all the compilers provide a standard Fortran '' | ||
+ | |||
+ | Example: | ||
+ | <code fortran> | ||
+ | | ||
+ | | ||
+ | call flush_unit(unt) | ||
+ | |||
+ | !For flushing ab_out or std_out, use | ||
+ | call wrtout(std_out, | ||
+ | </ | ||
+ | |||
+ | Note that the '' | ||
+ | |||
+ | Example: | ||
+ | <code fortran> | ||
+ | | ||
+ | |||
+ | if (me==master) then | ||
+ | | ||
+ | | ||
+ | !that will be read by the other nodes | ||
+ | call flush_unit(unt) | ||
+ | end if | ||
+ | |||
+ | call xmpi_barrier(comm) | ||
+ | if (me /= master) then | ||
+ | !Can safely read the string because master called flush_unit | ||
+ | | ||
+ | end if | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | \\ \\ | ||
+ | ==== IOMSG and NEWUNIT specifiers in a OPEN statement ==== | ||
+ | |||
+ | **'' | ||
+ | |||
+ | **'' | ||
+ | |||
+ | Developers should take advantage of these new features through the '' | ||
+ | |||
+ | Example: | ||
+ | <code fortran> | ||
+ | use m_io_tools, only : open_file | ||
+ | |||
+ | ii = open_file(filename, | ||
+ | & | ||
+ | |||
+ | !If IOError, print a message describing the error and stop the code | ||
+ | if (ii/=0) MSG_ERROR(iomsg) | ||
+ | </ | ||
+ | \\ | ||
+ | ==== Use of IEEE Arithmetic ==== | ||
+ | |||
+ | This extension allows one to trap or to signal possible floating point exceptions at run time without compiling the code in debug mode. Developers can use the procedures defined in the '' | ||
+ | |||
+ | Production code should not contain debugging sections with calls to the procedures defined in '' | ||
+ | \\ \\ | ||
+ | ==== Fortran extensions supported via CPP macros ==== | ||
+ | |||
+ | This section discusses a set of features belonging to F2003/F2008 that are vey useful for optimizing CPU-critical parts or for constructing reliable software. The features described in this section are made available in a portable way via CPP macros defined in '' | ||
+ | |||
+ | The most important attributes supported at the time of writing are: | ||
+ | * **'' | ||
+ | * **'' | ||
+ | * **'' | ||
+ | * **'' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | Example: | ||
+ | <code fortran> | ||
+ | | ||
+ | </ | ||
developers/fortran_2003.txt · Last modified: 2024/09/02 16:21 by Maryam Azizi