User Tools

Site Tools


dev:codingconventions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
dev:codingconventions [2015/02/06 11:59]
asamuel
dev:codingconventions [2018/12/19 12:19]
tmueller [Use existing infrastructure]
Line 1: Line 1:
 ====== Coding Conventions ====== ====== Coding Conventions ======
 +
 ===== Stick to the standard ===== ===== Stick to the standard =====
-  ​* Code enabled by default should be standard ​Fortran2003 ​[-std=f2003] + 
-  * Avoid using new OOP aspects, because compilers do not yet support them well enough. +  ​* Code enabled by default should be standard ​Fortran 2008 [-std=f2008
-  * OpenMP code should follow ​the version 3.X of the standard +  * OpenMP code should follow version 3.X of the standard 
-  * MPI should should follow ​the version ​2.X of the standard +  * MPI should should follow version ​of the standard 
-  * Extended functionality should match [[wp>​POSIX]] ​[[wp>​Linux_Standard_Base|LSB]].+  * Extended functionality should match [[wp>​POSIX]] ​and [[wp>​Linux_Standard_Base|LSB]].
  
 ===== Write explicit code ===== ===== Write explicit code =====
Line 15: Line 16:
     * INTEGER i=4.9999, i.e. i=INT(4.9999) implies i==4. Use NINT(4.9999) as appropriate.     * INTEGER i=4.9999, i.e. i=INT(4.9999) implies i==4. Use NINT(4.9999) as appropriate.
     * natom*natom,​ nrow_global*ncol_global overflows INT(KIND=4) for large simulations.     * natom*natom,​ nrow_global*ncol_global overflows INT(KIND=4) for large simulations.
 +    * always use REAL() with KIND parameter, i.e. r = REAL(i, KIND=dp).
 +    * avoid FLOAT() in favour of REAL(, KIND=dp).
     * the global number of grid points (pw_grid_type%ngpts,​ngpts_cut) overflows INT(KIND=4) for large simulations.     * the global number of grid points (pw_grid_type%ngpts,​ngpts_cut) overflows INT(KIND=4) for large simulations.
  
 ===== Don't use poorly designed language features ===== ===== Don't use poorly designed language features =====
   * Do not use the ''​GOTO''​-statement. See also [[http://​xkcd.com/​292/​]] and [[doi>​10.1145/​362929.362947]]   * Do not use the ''​GOTO''​-statement. See also [[http://​xkcd.com/​292/​]] and [[doi>​10.1145/​362929.362947]]
-  * Do not use left-hand-side (lhs) reallocations of allocatables [-Wrealloc-lhs-all].  +  * Do not use left-hand-side (lhs) reallocations of allocatables [-Wrealloc-lhs-all]. ​[[http://​www.tddft.org/​pipermail/​octopus-devel/​2012-February/​005510.html | Why? ]]  
-  * Do not use ''​FORALL''​ constructs. [[https://​gcc.gnu.org/​ml/​fortran/​2012-04/​msg00026.html | Why? ]]+  * Do not use ''​FORALL''​ constructs. [[https://​gcc.gnu.org/​ml/​fortran/​2012-04/​msg00025.html | Why? ]]
   * Do not use ''​OMP THREADPRIVATE''​ variables.   * Do not use ''​OMP THREADPRIVATE''​ variables.
 +  * Do not query the ''​STAT''​ from a ''​DEALLOCATE'',​ the Fortran runtime takes care.
 +  * Do not use ''​RANDOM_NUMBER()'',​ it's not consistent across different compilers.
  
 ===== Fight spaghetti code ===== ===== Fight spaghetti code =====
Line 34: Line 39:
  
 ===== Use existing infrastructure ===== ===== Use existing infrastructure =====
-For many common ​operation ​there exist wrappers in CP2K to prevent usage errors and to allow for central redirections.+ 
 +Always prefer [[https://​gcc.gnu.org/​onlinedocs/​gcc-8.2.0/​gfortran/​Intrinsic-Procedures.html|built-in (intrinsic) functions]] instead of hand-coded routines since they usually include extra numerical checks to avoid intermediate under- or overflow while still performing optimally. Example: 
 + 
 +  * ''​NORM2''​ instead of ''​%%SQRT(x(1)**2 + x(2)**2 + x(3)**2)%%''​ 
 + 
 + 
 + 
 +For many common ​operations ​there exist wrappers in CP2K to prevent usage errors and to allow for central redirections, i.e. avoid to use direct calls to external libraries in CP2K 
   * Use the routines from ''​cp_files.F''​ instead of calling ''​OPEN''​ and ''​CLOSE''​ directly.   * Use the routines from ''​cp_files.F''​ instead of calling ''​OPEN''​ and ''​CLOSE''​ directly.
   * Use the routines from the full-matrix ''​fm''​-package instead of calling BLAS or Scalapack directly.   * Use the routines from the full-matrix ''​fm''​-package instead of calling BLAS or Scalapack directly.
   * Use the routines from ''​message_passing.F''​ instead of calling MPI directly.   * Use the routines from ''​message_passing.F''​ instead of calling MPI directly.
 +  * Use the routines from ''​fft_lib.F''​ instead of calling FFT (any library) directly.
   * Use the routines from ''​machine.F''​ to access architecture depended things like e.g. the working directory.   * Use the routines from ''​machine.F''​ to access architecture depended things like e.g. the working directory.
 +  * Don't use ''​UNIT=*''​ in ''​WRITE''​ or ''​PRINT''​ statements. Instead request a unit from the logger: ''​iw=cp_logger_get_default_unit_nr()''​ and write only if you actually received a unit: ''​IF(iw>​0) WRITE (UNIT=iw, ,,,​)''​.
 +  * Use [[error_handling|CP2K'​s error-handling]],​ instead of the ''​STOP''​ statement.
  
 ===== Remove dead code ===== ===== Remove dead code =====
-Every line of code has to be compiled and maintained. Hence, unused variables and code poses an unnecessary burden and should be removed. ​However, sometimes ​it is beneficial to keep debugging code around. Such code should be put into a ''​IF(debug_this_module)''​-block,​ with a parameter set to ''​.FALSE.''​. This way the code will stay up-to-date and easily callable.+  * Every line of code has to be compiled and maintained. Hence, unused variables and code poses an unnecessary burden and should be removed ​[-Wunused-variable -Wunused-but-set-variable -Wunused-dummy-argument]. 
 +  * Sometimes ​it is beneficial to keep debugging code around ​nevertheless. Such code should be put into a ''​IF(debug_this_module)''​-block,​ with a parameter set to ''​.FALSE.''​. This way the code will stay up-to-date and easily callable.
  
 ===== Format and document code ===== ===== Format and document code =====
Line 48: Line 65:
   * Each module and routine should be annotated with [[dev:​codingconventions#​Doxygen| Doxygen documentation]].   * Each module and routine should be annotated with [[dev:​codingconventions#​Doxygen| Doxygen documentation]].
   * Each preprocessor flag should start with two underscores and be documented in the ''​INSTALL''​-file and added to the cp2k_flags function (cp2k_info.F).   * Each preprocessor flag should start with two underscores and be documented in the ''​INSTALL''​-file and added to the cp2k_flags function (cp2k_info.F).
-  * The code should be formatted with the prettify-tool by running ''​make -j pretty''​. +  * The code should be formatted with the [[dev:​formattingconventions| ​prettify tool]] by running ''​make -j pretty''​.
 ===== Write tests ===== ===== Write tests =====
   * Every feature should be tested, with the goal of complete [[ http://​www.cp2k.org/​static/​coverage/​ | code coverage by regtesting ]].   * Every feature should be tested, with the goal of complete [[ http://​www.cp2k.org/​static/​coverage/​ | code coverage by regtesting ]].
Line 58: Line 74:
   * The following keywords are required: ''​\brief'',​ ''​\param''​ (for each parameter), ''​\retval''​ (for functions)   * The following keywords are required: ''​\brief'',​ ''​\param''​ (for each parameter), ''​\retval''​ (for functions)
   * The following keywords are optional: ''​\note'',​ ''​\par'',​ ''​\date'',​ ''​\author''​   * The following keywords are optional: ''​\note'',​ ''​\par'',​ ''​\date'',​ ''​\author''​
-  * Please run ''​make doxify''​ to format your doxygen comments, or the generate templates where none exist+  * Please run ''​make doxify''​ to format your doxygen comments, or generate templates where none exist
   * See our [[ http://​doxygen.cp2k.org/​files.html | doxygen pages ]] for the result   * See our [[ http://​doxygen.cp2k.org/​files.html | doxygen pages ]] for the result
- 
dev/codingconventions.txt · Last modified: 2018/12/21 08:41 by tmueller