Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Table of Contents | ||||||
---|---|---|---|---|---|---|
|
Code writing recommendations
Expand | |||||
---|---|---|---|---|---|
Use uppercase for all Fortran instructions, variable names Use lowercase for pre-compiler directives Comments need to be written in English only (uppercase & lowercase accepted) Tabulations are forbidden (to be replaced by spaces at the beginning of line) Use meaningful names for variables, in English, keep the same name throughout the code Systematically indent your blocks ( In case of long
|
Routine & file organisation
Expand |
---|
By default, one file contains only one subroutine or function, except when the understanding of the code is facilitated by grouping few procedures The name of the subroutine (function) and the name of the file need to match. A “ The same 2 rules apply for module: one module per file, same name For module several subroutines can be defined under the |
Header definition
Expand | |||||||
---|---|---|---|---|---|---|---|
Each procedure needs to have a conforming header, containing by order of appearance:
Fortran routine generic example:
|
Comments
Expand | |||||
---|---|---|---|---|---|
It is important to comment important algorithms, especially when non straightforward coding is used Comments are written in English Comments respect Fortran90 standard. The use of " Except for precompiler directives, the following characters Example:
|
Modules
Expand | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Module FormatGeneric format of a Fortran90 module is as follows:
Naming ConventionModule name is defined as follows: With module file name: modulename_mod.F Module file is placed at the same location as other files used by the option Module Usage3 types of usage:
Good practice is to split type declaration from variable declaration into 2 different modules. This way it is possible to pass variables defined in modules at upper level (resol) into calling tree at lower levels Then, such derived data type variables passed as argument of procedures can be defined using the module which defines them, allowing traceability of such variables throughout the code The procedure that uses such variables passed by argument needs to include the module that defines derived data types Example of derived data types (comments compliant with Doxygen):
Restart VariablesAll the variables communicated between Starter and Engine are declared in module Interface definitionFortran90 interface allows the compiler to do additional checks like coherency between argument types, attributes and number between calling and callee routines. It is required in some cases like when a procedure has a dummy argument with attribute In practice, it was introduced in few places of the code for routines which were called at several different places Such coherency is automatically tested by QA static analysis tools (Forcheck). And for pointer, the good practice is to use derived data types instead of pointer directly Therefore, the remaining use of interface is regarding routine with optional arguments This feature should be spread in the code instead of adding additional “dummy” arguments Interface ExampleExample of an interface for a routine called at several places in the code. The routine is put in a module to guarantee automatic update of interfaces and recompilations of all routines using this routine in case of change (dependence automatically found by compiler)
|
Memory Allocation
Expand | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Dynamic memory allocation mechanismDynamic memory allocation is directly done at the Fortran90 level using This macro allows automatic error checks encapsulating call to Fortran90 Previously, allocation error check was done by hand by the:
In practice, error checking was missing in many places. Therefore, the idea to use a macro to automatically control allocation, handle error message and execution stop in case of failure was implemented. Here is the macro detail:
The previous code becomes:
Developers are required to check the success of the allocation The message printed by this macro in case of allocation failure is rather generic. For large arrays it is preferred to print a specific message, with advice for the user, or at least the option concerned by this failure Global MemoryMemory allocation of global data structures, arrays and derived data types, should be done at the highest level, in It is advised to use derived data type with structure of arrays. This way it is possible to declare the variable at the upper level, gather the allocation of array members in a dedicated subroutine, then use the variable in procedures called at lower level without losing traceability Local MemoryIn a procedure, local variable allocation method depends on its size:
Automatic Allocated arrays go into Stack ALLOCATED ARRAYS go into Heap One should take care to reduce Stacksize usage to a reasonable size. Stacksize is hardcoded under Windows It is allowed to use In case of multiple calls to Local Memory Example:
Shared Memory Programming (SMP) and memory allocationFor Radioss Engine, OpenMP programming model is used for second level parallelization By default any memory allocation done outside of a parallel section is shared between threads Most of the parallel sections are started from The same way, any variable defined in a common or module is shared by default For pointer, notice that a single thread needs to allocate and deallocate it. The programmer has to manage synchronization in order to insure such a variable is allocated before being used by any other thread and no longer used before it is deallocated The !$OMP THREADPRIVATE directive overrides default behavior by creating thread local storage variables |
Array Aliasing
Expand | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
DescriptionHere we discuss different arguments of a procedure referencing the same memory locations. The compiler won’t be able to detect in the procedure that different argument variables reference one or more identical memory locations. Such a situation is particularly dangerous because of compiler optimization. Even if compilers are not forbidden it, if both variables are modified inside the procedure this could lead to unpredictable results. Potential conflicts or dependencies won’t be detected Code Example:
Tested on SGI O200 IRIX 6.4: output:
|