LCOV - code coverage report
Current view: top level - src/start - cp2k_runs.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:123285f) Lines: 88.5 % 392 347
Test Date: 2026-11-04 00:58:17 Functions: 80.0 % 5 4

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : MODULE cp2k_runs
      10              :    USE atom,                            ONLY: atom_code
      11              :    USE bibliography,                    ONLY: Iannuzzi2026,&
      12              :                                               cite_reference,&
      13              :                                               cp2kqs2020
      14              :    USE bsse,                            ONLY: do_bsse_calculation
      15              :    USE cell_opt,                        ONLY: cp_cell_opt
      16              :    USE cp2k_debug,                      ONLY: cp2k_debug_energy_and_forces
      17              :    USE cp2k_info,                       ONLY: compile_date,&
      18              :                                               compile_revision,&
      19              :                                               cp2k_version,&
      20              :                                               cp2k_year
      21              :    USE cp_control_types,                ONLY: dft_control_type
      22              :    USE cp_dbcsr_api,                    ONLY: dbcsr_finalize_lib,&
      23              :                                               dbcsr_print_config,&
      24              :                                               dbcsr_print_statistics
      25              :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_config
      26              :    USE cp_files,                        ONLY: close_file,&
      27              :                                               open_file
      28              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      29              :                                               cp_logger_get_default_io_unit,&
      30              :                                               cp_logger_type,&
      31              :                                               cp_logger_would_log,&
      32              :                                               cp_note_level
      33              :    USE cp_output_handling,              ONLY: cp_add_iter_level,&
      34              :                                               cp_print_key_finished_output,&
      35              :                                               cp_print_key_unit_nr,&
      36              :                                               cp_rm_iter_level
      37              :    USE cp_parser_methods,               ONLY: parser_search_string
      38              :    USE cp_parser_types,                 ONLY: cp_parser_type,&
      39              :                                               parser_create,&
      40              :                                               parser_release
      41              :    USE cp_units,                        ONLY: cp_unit_set_create,&
      42              :                                               cp_unit_set_release,&
      43              :                                               cp_unit_set_type,&
      44              :                                               export_units_as_xml
      45              :    USE dbm_api,                         ONLY: dbm_library_print_stats
      46              :    USE dbt_api,                         ONLY: dbt_library_print_stats
      47              :    USE environment,                     ONLY: cp2k_finalize,&
      48              :                                               cp2k_init,&
      49              :                                               cp2k_read,&
      50              :                                               cp2k_setup
      51              :    USE f77_interface,                   ONLY: configure_dbcsr,&
      52              :                                               create_force_env,&
      53              :                                               destroy_force_env,&
      54              :                                               f77_default_para_env => default_para_env,&
      55              :                                               f_env_add_defaults,&
      56              :                                               f_env_rm_defaults,&
      57              :                                               f_env_type
      58              :    USE farming_methods,                 ONLY: do_deadlock,&
      59              :                                               do_nothing,&
      60              :                                               do_wait,&
      61              :                                               farming_parse_input,&
      62              :                                               get_next_job
      63              :    USE farming_types,                   ONLY: deallocate_farming_env,&
      64              :                                               farming_env_type,&
      65              :                                               init_farming_env,&
      66              :                                               job_finished,&
      67              :                                               job_running
      68              :    USE force_env_methods,               ONLY: force_env_calc_energy_force
      69              :    USE force_env_types,                 ONLY: force_env_get,&
      70              :                                               force_env_type
      71              :    USE geo_opt,                         ONLY: cp_geo_opt
      72              :    USE global_types,                    ONLY: global_environment_type,&
      73              :                                               globenv_create,&
      74              :                                               globenv_release
      75              :    USE grid_api,                        ONLY: grid_library_print_stats,&
      76              :                                               grid_library_set_config
      77              :    USE input_constants,                 ONLY: &
      78              :         bsse_run, cell_opt_run, debug_run, do_atom, do_band, do_cp2k, do_embed, do_farming, &
      79              :         do_fist, do_ipi, do_mixed, do_nnp, do_opt_basis, do_optimize_input, do_qmmm, do_qs, &
      80              :         do_sirius, do_swarm, do_tamc, do_test, do_tree_mc, do_tree_mc_ana, driver_run, ehrenfest, &
      81              :         energy_force_run, energy_run, geo_opt_run, history_guess, linear_response_run, mimic_run, &
      82              :         mol_dyn_run, mon_car_run, mtlr_run, negf_run, none_run, pint_run, real_time_propagation, &
      83              :         rtp_method_bse, rtp_method_bse_linearized, tree_mc_run, vib_anal
      84              :    USE input_cp2k,                      ONLY: create_cp2k_root_section
      85              :    USE input_cp2k_check,                ONLY: check_cp2k_input
      86              :    USE input_cp2k_global,               ONLY: create_global_section
      87              :    USE input_cp2k_read,                 ONLY: read_input
      88              :    USE input_keyword_types,             ONLY: keyword_release
      89              :    USE input_parsing,                   ONLY: section_vals_parse
      90              :    USE input_section_types,             ONLY: &
      91              :         section_release, section_type, section_vals_create, section_vals_get, &
      92              :         section_vals_get_subs_vals, section_vals_release, section_vals_retain, section_vals_type, &
      93              :         section_vals_val_get, section_vals_write, write_section_xml
      94              :    USE ipi_driver,                      ONLY: run_driver
      95              :    USE kinds,                           ONLY: default_path_length,&
      96              :                                               default_string_length,&
      97              :                                               dp,&
      98              :                                               int_8
      99              :    USE library_tests,                   ONLY: lib_test
     100              :    USE machine,                         ONLY: default_output_unit,&
     101              :                                               m_chdir,&
     102              :                                               m_flush,&
     103              :                                               m_getcwd,&
     104              :                                               m_memory,&
     105              :                                               m_memory_max,&
     106              :                                               m_walltime
     107              :    USE mc_run,                          ONLY: do_mon_car
     108              :    USE md_run,                          ONLY: qs_mol_dyn
     109              :    USE message_passing,                 ONLY: mp_any_source,&
     110              :                                               mp_comm_type,&
     111              :                                               mp_para_env_release,&
     112              :                                               mp_para_env_type
     113              :    USE mimic_loop,                      ONLY: do_mimic_loop
     114              :    USE mscfg_methods,                   ONLY: do_mol_loop,&
     115              :                                               loop_over_molecules
     116              :    USE mtlr_u_j_methods,                ONLY: do_mtlr_u_j
     117              :    USE neb_methods,                     ONLY: neb
     118              :    USE negf_methods,                    ONLY: do_negf
     119              :    USE offload_api,                     ONLY: offload_mempool_stats_print
     120              :    USE optimize_basis,                  ONLY: run_optimize_basis
     121              :    USE optimize_input,                  ONLY: run_optimize_input
     122              :    USE pint_methods,                    ONLY: do_pint_run
     123              :    USE qs_environment_types,            ONLY: get_qs_env
     124              :    USE qs_linres_module,                ONLY: linres_calculation
     125              :    USE reference_manager,               ONLY: export_references_as_xml
     126              :    USE rt_bse,                          ONLY: run_propagation_bse
     127              :    USE rt_bse_linearized,               ONLY: run_propagation_linearized_bse
     128              :    USE rt_propagation,                  ONLY: rt_prop_setup
     129              :    USE swarm,                           ONLY: run_swarm
     130              :    USE tamc_run,                        ONLY: qs_tamc
     131              :    USE tmc_setup,                       ONLY: do_analyze_files,&
     132              :                                               do_tmc
     133              :    USE vibrational_analysis,            ONLY: vb_anal
     134              : #include "../base/base_uses.f90"
     135              : 
     136              :    IMPLICIT NONE
     137              : 
     138              :    PRIVATE
     139              : 
     140              :    PUBLIC :: write_xml_file, run_input
     141              : 
     142              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp2k_runs'
     143              : 
     144              : CONTAINS
     145              : 
     146              : ! **************************************************************************************************
     147              : !> \brief performs an instance of a cp2k run
     148              : !> \param input_declaration ...
     149              : !> \param input_file_name name of the file to be opened for input
     150              : !> \param output_unit unit to which output should be written
     151              : !> \param mpi_comm ...
     152              : !> \param initial_variables key-value list of initial preprocessor variables
     153              : !> \author Joost VandeVondele
     154              : !> \note
     155              : !>      para_env should be a valid communicator
     156              : !>      output_unit should be writeable by at least the lowest rank of the mpi group
     157              : !>
     158              : !>      recursive because a given run_type might need to be able to perform
     159              : !>      another cp2k_run as part of its job (e.g. farming, classical equilibration, ...)
     160              : !>
     161              : !>      the idea is that a cp2k instance should be able to run with just three
     162              : !>      arguments, i.e. a given input file, output unit, mpi communicator.
     163              : !>      giving these three to cp2k_run should produce a valid run.
     164              : !>      the only task of the PROGRAM cp2k is to create valid instances of the
     165              : !>      above arguments. Ideally, anything that is called afterwards should be
     166              : !>      able to run simultaneously / multithreaded / sequential / parallel / ...
     167              : !>      and able to fail safe
     168              : ! **************************************************************************************************
     169        11304 :    RECURSIVE SUBROUTINE cp2k_run(input_declaration, input_file_name, output_unit, mpi_comm, initial_variables)
     170              :       TYPE(section_type), POINTER                        :: input_declaration
     171              :       CHARACTER(LEN=*), INTENT(IN)                       :: input_file_name
     172              :       INTEGER, INTENT(IN)                                :: output_unit
     173              : 
     174              :       CLASS(mp_comm_type)                                 :: mpi_comm
     175              :       CHARACTER(len=default_path_length), &
     176              :          DIMENSION(:, :), INTENT(IN)                     :: initial_variables
     177              : 
     178              :       INTEGER                                            :: f_env_handle, grid_backend, ierr, &
     179              :                                                             iter_level, method_name_id, &
     180              :                                                             new_env_id, prog_name_id, run_type_id
     181              :       INTEGER(KIND=int_8)                                :: m_memory_max_mpi
     182              :       LOGICAL                                            :: echo_input, grid_apply_cutoff, &
     183              :                                                             grid_validate, has_excited_states, &
     184              :                                                             has_properties, I_was_ionode
     185              :       TYPE(cp_logger_type), POINTER                      :: logger, sublogger
     186              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     187              :       TYPE(dft_control_type), POINTER                    :: dft_control
     188              :       TYPE(f_env_type), POINTER                          :: f_env
     189              :       TYPE(force_env_type), POINTER                      :: force_env
     190              :       TYPE(global_environment_type), POINTER             :: globenv
     191              :       TYPE(section_vals_type), POINTER                   :: glob_section, input_file, root_section, &
     192              :                                                             section
     193              : 
     194        11304 :       NULLIFY (para_env, f_env, dft_control)
     195        11304 :       ALLOCATE (para_env)
     196        11304 :       para_env = mpi_comm
     197              : 
     198              :       ! The surrounding CP2K runtime owns DBCSR. Only redirect its diagnostics
     199              :       ! to this run's output; the DBCSR API leaves an active library initialized.
     200        11304 :       CALL configure_dbcsr(mpi_comm, output_unit)
     201              : 
     202        11304 :       NULLIFY (globenv, force_env)
     203              : 
     204        11304 :       CALL cite_reference(cp2kqs2020)
     205        11304 :       CALL cite_reference(Iannuzzi2026)
     206              : 
     207              :       ! Parse the input
     208              :       input_file => read_input(input_declaration, input_file_name, &
     209              :                                initial_variables=initial_variables, &
     210        11304 :                                para_env=para_env)
     211              : 
     212        11304 :       CALL para_env%sync()
     213              : 
     214        11304 :       logger => cp_get_default_logger()
     215              : 
     216        11304 :       glob_section => section_vals_get_subs_vals(input_file, "GLOBAL")
     217        11304 :       CALL section_vals_val_get(glob_section, "ECHO_INPUT", l_val=echo_input)
     218        11304 :       IF (echo_input .AND. (output_unit > 0)) THEN
     219              :          CALL section_vals_write(input_file, &
     220              :                                  unit_nr=output_unit, &
     221              :                                  hide_root=.TRUE., &
     222           16 :                                  hide_defaults=.FALSE.)
     223              :       END IF
     224              : 
     225        11304 :       CALL check_cp2k_input(input_declaration, input_file, para_env=para_env, output_unit=output_unit)
     226        11304 :       root_section => input_file
     227              :       CALL section_vals_val_get(input_file, "GLOBAL%PROGRAM_NAME", &
     228        11304 :                                 i_val=prog_name_id)
     229              :       CALL section_vals_val_get(input_file, "GLOBAL%RUN_TYPE", &
     230        11304 :                                 i_val=run_type_id)
     231        11304 :       CALL section_vals_val_get(root_section, "FORCE_EVAL%METHOD", i_val=method_name_id)
     232              : 
     233        11304 :       IF (prog_name_id /= do_cp2k) THEN
     234              :          ! initial setup (cp2k does it in the creation of the force_env)
     235          528 :          CALL globenv_create(globenv)
     236          528 :          CALL section_vals_retain(input_file)
     237          528 :          CALL cp2k_init(para_env, output_unit, globenv, input_file_name=input_file_name)
     238          528 :          CALL cp2k_read(root_section, para_env, globenv)
     239          528 :          CALL cp2k_setup(root_section, para_env, globenv)
     240              :       END IF
     241              : 
     242        11304 :       CALL cp_dbcsr_config(root_section)
     243        11304 :       IF (output_unit > 0 .AND. &
     244              :           cp_logger_would_log(logger, cp_note_level)) THEN
     245         5680 :          CALL dbcsr_print_config(unit_nr=output_unit)
     246         5680 :          WRITE (UNIT=output_unit, FMT='()')
     247              :       END IF
     248              : 
     249              :       ! Configure the grid library.
     250        11304 :       CALL section_vals_val_get(root_section, "GLOBAL%GRID%BACKEND", i_val=grid_backend)
     251        11304 :       CALL section_vals_val_get(root_section, "GLOBAL%GRID%VALIDATE", l_val=grid_validate)
     252        11304 :       CALL section_vals_val_get(root_section, "GLOBAL%GRID%APPLY_CUTOFF", l_val=grid_apply_cutoff)
     253              : 
     254              :       CALL grid_library_set_config(backend=grid_backend, &
     255              :                                    validate=grid_validate, &
     256        11304 :                                    apply_cutoff=grid_apply_cutoff)
     257              : 
     258          364 :       SELECT CASE (prog_name_id)
     259              :       CASE (do_atom)
     260          364 :          globenv%run_type_id = none_run
     261          364 :          CALL atom_code(root_section)
     262              :       CASE (do_optimize_input)
     263            6 :          CALL run_optimize_input(input_declaration, root_section, para_env)
     264              :       CASE (do_swarm)
     265            6 :          CALL run_swarm(input_declaration, root_section, para_env, globenv, input_file_name)
     266              :       CASE (do_farming)
     267              :          ! FARMING temporarily replaces the runtime communicator with disjoint
     268              :          ! job communicators. Each job owns DBCSR until its communicator is freed.
     269           24 :          CALL dbcsr_finalize_lib()
     270           24 :          CALL farming_run(input_declaration, root_section, para_env, initial_variables)
     271           24 :          CALL configure_dbcsr(mpi_comm, output_unit)
     272              :       CASE (do_opt_basis)
     273            8 :          CALL run_optimize_basis(input_declaration, root_section, para_env, globenv)
     274            8 :          globenv%run_type_id = none_run
     275              :       CASE (do_cp2k)
     276              :          CALL create_force_env(new_env_id, &
     277              :                                input_declaration=input_declaration, &
     278              :                                input_path=input_file_name, &
     279              :                                output_path="__STD_OUT__", mpi_comm=para_env, &
     280              :                                output_unit=output_unit, &
     281              :                                owns_out_unit=.FALSE., &
     282        10776 :                                input=input_file, ierr=ierr)
     283        10776 :          CPASSERT(ierr == 0)
     284        10776 :          CALL f_env_add_defaults(new_env_id, f_env, handle=f_env_handle)
     285        10776 :          force_env => f_env%force_env
     286        10776 :          CALL force_env_get(force_env, globenv=globenv)
     287              :       CASE (do_test)
     288           80 :          CALL lib_test(root_section, para_env, globenv)
     289              :       CASE (do_tree_mc) ! TMC entry point
     290           28 :          CALL do_tmc(input_declaration, root_section, para_env, globenv)
     291              :       CASE (do_tree_mc_ana)
     292           12 :          CALL do_analyze_files(input_declaration, root_section, para_env)
     293              :       CASE default
     294        22080 :          CPABORT("Unknown program")
     295              :       END SELECT
     296        11304 :       CALL section_vals_release(input_file)
     297              : 
     298        11372 :       SELECT CASE (globenv%run_type_id)
     299              :       CASE (pint_run)
     300           68 :          CALL do_pint_run(para_env, root_section, input_declaration, globenv)
     301              :       CASE (none_run, tree_mc_run)
     302              :          ! do nothing
     303              :       CASE (driver_run)
     304            0 :          CALL run_driver(force_env, globenv)
     305              :       CASE (energy_run, energy_force_run)
     306              :          IF (method_name_id /= do_qs .AND. &
     307              :              method_name_id /= do_sirius .AND. &
     308              :              method_name_id /= do_qmmm .AND. &
     309              :              method_name_id /= do_mixed .AND. &
     310              :              method_name_id /= do_nnp .AND. &
     311              :              method_name_id /= do_embed .AND. &
     312         6434 :              method_name_id /= do_fist .AND. &
     313              :              method_name_id /= do_ipi) THEN
     314            0 :             CPABORT("Energy/Force run not available for all methods ")
     315              :          END IF
     316              : 
     317         6434 :          sublogger => cp_get_default_logger()
     318         6434 :          CALL cp_add_iter_level(sublogger, "JUST_ENERGY", n_rlevel_new=iter_level)
     319              : 
     320              :          ! loop over molecules to generate a molecular guess
     321              :          ! this procedure is initiated here to avoid passing globenv deep down
     322              :          ! the subroutine stack
     323         6434 :          IF (do_mol_loop(force_env=force_env)) THEN
     324           16 :             CALL loop_over_molecules(globenv, force_env)
     325              :          END IF
     326              : 
     327              :          ! Only this driver knows that no later geometry step will be requested.
     328              :          ! Keep history for nested/repeated SCF consumers and for library/shell use.
     329         6434 :          IF (method_name_id == do_qs) THEN
     330         5734 :             CALL get_qs_env(force_env%qs_env, dft_control=dft_control)
     331         5734 :             section => section_vals_get_subs_vals(force_env%qs_env%input, "PROPERTIES")
     332         5734 :             CALL section_vals_get(section, explicit=has_properties)
     333         5734 :             section => section_vals_get_subs_vals(force_env%qs_env%input, "DFT%EXCITED_STATES")
     334         5734 :             CALL section_vals_get(section, explicit=has_excited_states)
     335              :             force_env%qs_env%skip_wf_history = .NOT. (has_properties .OR. has_excited_states .OR. &
     336              :                                                       dft_control%qs_control%cdft .OR. &
     337              :                                                       dft_control%qs_control%et_coupling_calc .OR. &
     338              :                                                       dft_control%do_xas_calculation .OR. &
     339              :                                                       dft_control%do_xas_tdp_calculation .OR. &
     340         6618 :                                                       force_env%qs_env%scf_control%density_guess == history_guess)
     341              :          END IF
     342              : 
     343        11598 :          SELECT CASE (globenv%run_type_id)
     344              :          CASE (energy_run)
     345         5164 :             CALL force_env_calc_energy_force(force_env, calc_force=.FALSE.)
     346              :          CASE (energy_force_run)
     347         1270 :             CALL force_env_calc_energy_force(force_env, calc_force=.TRUE.)
     348              :          CASE default
     349         6434 :             CPABORT("Unknown run type")
     350              :          END SELECT
     351         6434 :          IF (method_name_id == do_qs) force_env%qs_env%skip_wf_history = .FALSE.
     352         6434 :          CALL cp_rm_iter_level(sublogger, "JUST_ENERGY", n_rlevel_att=iter_level)
     353              :       CASE (mol_dyn_run)
     354         1644 :          CALL qs_mol_dyn(force_env, globenv)
     355              :       CASE (geo_opt_run)
     356          812 :          CALL cp_geo_opt(force_env, globenv)
     357              :       CASE (cell_opt_run)
     358          234 :          CALL cp_cell_opt(force_env, globenv)
     359              :       CASE (mon_car_run)
     360           20 :          CALL do_mon_car(force_env, globenv, input_declaration, input_file_name)
     361              :       CASE (do_tamc)
     362            2 :          CALL qs_tamc(force_env, globenv)
     363              :       CASE (real_time_propagation)
     364          210 :          IF (method_name_id /= do_qs) THEN
     365            0 :             CPABORT("Real time propagation needs METHOD QS. ")
     366              :          END IF
     367          210 :          CALL get_qs_env(force_env%qs_env, dft_control=dft_control)
     368          210 :          dft_control%rtp_control%fixed_ions = .TRUE.
     369          344 :          SELECT CASE (dft_control%rtp_control%rtp_method)
     370              :          CASE (rtp_method_bse_linearized)
     371              :             ! Run the linearized TD-BSE method
     372           58 :             CALL run_propagation_linearized_bse(force_env)
     373              :          CASE (rtp_method_bse)
     374              :             ! Run the TD-BSE method
     375           14 :             CALL run_propagation_bse(force_env)
     376              :          CASE default
     377              :             ! Run the TDDFT method
     378          210 :             CALL rt_prop_setup(force_env)
     379              :          END SELECT
     380              :       CASE (ehrenfest)
     381           76 :          IF (method_name_id /= do_qs) THEN
     382            0 :             CPABORT("Ehrenfest dynamics needs METHOD QS ")
     383              :          END IF
     384           76 :          CALL get_qs_env(force_env%qs_env, dft_control=dft_control)
     385           76 :          dft_control%rtp_control%fixed_ions = .FALSE.
     386           76 :          CALL qs_mol_dyn(force_env, globenv)
     387              :       CASE (bsse_run)
     388           12 :          CALL do_bsse_calculation(force_env, globenv)
     389              :       CASE (linear_response_run)
     390          178 :          IF (method_name_id /= do_qs .AND. &
     391              :              method_name_id /= do_qmmm) THEN
     392            0 :             CPABORT("Property calculations by Linear Response only within the QS or QMMM program ")
     393              :          END IF
     394              :          ! The Ground State is needed, it can be read from Restart
     395          178 :          CALL force_env_calc_energy_force(force_env, calc_force=.FALSE., linres=.TRUE.)
     396          178 :          CALL linres_calculation(force_env)
     397              :       CASE (debug_run)
     398         1030 :          SELECT CASE (method_name_id)
     399              :          CASE (do_qs, do_qmmm, do_fist)
     400          970 :             CALL cp2k_debug_energy_and_forces(force_env)
     401              :          CASE DEFAULT
     402          970 :             CPABORT("Debug run available only with QS, FIST, and QMMM program ")
     403              :          END SELECT
     404              :       CASE (vib_anal)
     405           60 :          CALL vb_anal(root_section, input_declaration, para_env, globenv)
     406              :       CASE (do_band)
     407           34 :          CALL neb(root_section, input_declaration, para_env, globenv)
     408              :       CASE (negf_run)
     409            6 :          CALL do_negf(force_env)
     410              :       CASE (mimic_run)
     411            0 :          CALL do_mimic_loop(force_env)
     412              :       CASE (mtlr_run)
     413           16 :          IF (method_name_id /= do_qs) THEN
     414            0 :             CPABORT("RUN_TYPE MTLR is available only for METHOD QS.")
     415              :          END IF
     416           16 :          CALL do_mtlr_u_j(force_env)
     417              :       CASE default
     418        17738 :          CPABORT("Unknown run type")
     419              :       END SELECT
     420              : 
     421              :       ! Sample peak memory
     422        11304 :       CALL m_memory()
     423              : 
     424        11304 :       CALL dbcsr_print_statistics()
     425        11304 :       CALL dbm_library_print_stats(mpi_comm=mpi_comm, output_unit=output_unit)
     426        11304 :       CALL dbt_library_print_stats(mpi_comm=mpi_comm, output_unit=output_unit)
     427        11304 :       CALL grid_library_print_stats(mpi_comm=mpi_comm, output_unit=output_unit)
     428        11304 :       CALL offload_mempool_stats_print(mpi_comm=mpi_comm, output_unit=output_unit)
     429              : 
     430        11304 :       m_memory_max_mpi = m_memory_max
     431        11304 :       CALL mpi_comm%max(m_memory_max_mpi)
     432        11304 :       IF (output_unit > 0) THEN
     433         5680 :          WRITE (output_unit, *)
     434              :          WRITE (output_unit, '(T2,"MEMORY| Estimated peak process memory [MiB]",T73,I8)') &
     435         5680 :             (m_memory_max_mpi + (1024*1024) - 1)/(1024*1024)
     436              :       END IF
     437              : 
     438        11304 :       IF (prog_name_id == do_cp2k) THEN
     439        10776 :          f_env%force_env => force_env ! for mc
     440        10776 :          IF (ASSOCIATED(force_env%globenv)) THEN
     441        10776 :          IF (.NOT. ASSOCIATED(force_env%globenv, globenv)) THEN
     442            0 :             CALL globenv_release(force_env%globenv) !mc
     443              :          END IF
     444              :          END IF
     445        10776 :          force_env%globenv => globenv !mc
     446              :          CALL f_env_rm_defaults(f_env, ierr=ierr, &
     447        10776 :                                 handle=f_env_handle)
     448        10776 :          CPASSERT(ierr == 0)
     449        10776 :          CALL destroy_force_env(new_env_id, ierr=ierr)
     450        10776 :          CPASSERT(ierr == 0)
     451              :       ELSE
     452              :          I_was_ionode = para_env%is_source()
     453          528 :          CALL cp2k_finalize(root_section, para_env, globenv)
     454          528 :          CPASSERT(globenv%ref_count == 1)
     455          528 :          CALL section_vals_release(root_section)
     456          528 :          CALL globenv_release(globenv)
     457              :       END IF
     458              : 
     459        11304 :       CALL mp_para_env_release(para_env)
     460              : 
     461        11304 :    END SUBROUTINE cp2k_run
     462              : 
     463              : ! **************************************************************************************************
     464              : !> \brief performs a farming run that performs several independent cp2k_runs
     465              : !> \param input_declaration ...
     466              : !> \param root_section ...
     467              : !> \param para_env ...
     468              : !> \param initial_variables ...
     469              : !> \author Joost VandeVondele
     470              : !> \note
     471              : !>      needs to be part of this module as the cp2k_run -> farming_run -> cp2k_run
     472              : !>      calling style creates a hard circular dependency
     473              : ! **************************************************************************************************
     474           24 :    RECURSIVE SUBROUTINE farming_run(input_declaration, root_section, para_env, initial_variables)
     475              :       TYPE(section_type), POINTER              :: input_declaration
     476              :       TYPE(section_vals_type), POINTER         :: root_section
     477              :       TYPE(mp_para_env_type), POINTER          :: para_env
     478              :       CHARACTER(len=default_path_length), DIMENSION(:, :), INTENT(IN) :: initial_variables
     479              : 
     480              :       CHARACTER(len=*), PARAMETER :: routineN = 'farming_run'
     481              :       INTEGER, PARAMETER                       :: minion_status_done = -3, &
     482              :                                                   minion_status_wait = -4
     483              : 
     484              :       CHARACTER(len=7)                         :: label
     485              :       CHARACTER(LEN=default_path_length)       :: output_file
     486              :       CHARACTER(LEN=default_string_length)     :: str
     487              :       INTEGER :: dest, handle, i, i_job_to_restart, ierr, ijob, ijob_current, &
     488              :                  ijob_end, ijob_start, iunit, n_jobs_to_run, new_output_unit, &
     489              :                  new_rank, ngroups, num_minions, output_unit, primus_minion, &
     490              :                  minion_rank, source, tag, todo
     491           24 :       INTEGER, DIMENSION(:), POINTER           :: group_distribution, &
     492           24 :                                                   captain_minion_partition, &
     493           24 :                                                   minion_distribution, &
     494           24 :                                                   minion_status
     495              :       LOGICAL                                  :: found, captain, minion
     496              :       REAL(KIND=dp)                            :: t1, t2
     497           24 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: waittime
     498              :       TYPE(cp_logger_type), POINTER            :: logger
     499              :       TYPE(cp_parser_type), POINTER            :: my_parser
     500              :       TYPE(cp_unit_set_type)         :: default_units
     501              :       TYPE(farming_env_type), POINTER          :: farming_env
     502              :       TYPE(section_type), POINTER              :: g_section
     503              :       TYPE(section_vals_type), POINTER         :: g_data
     504              :       TYPE(mp_comm_type)                       :: minion_group, new_group
     505              : 
     506              :       ! the primus of all minions, talks to the captain on topics concerning all minions
     507           24 :       CALL timeset(routineN, handle)
     508           24 :       NULLIFY (my_parser, g_section, g_data)
     509              : 
     510           24 :       logger => cp_get_default_logger()
     511              :       output_unit = cp_print_key_unit_nr(logger, root_section, "FARMING%PROGRAM_RUN_INFO", &
     512           24 :                                          extension=".log")
     513              : 
     514           24 :       IF (output_unit > 0) WRITE (output_unit, FMT="(T2,A)") "FARMING| Hi, welcome on this farm!"
     515              : 
     516           24 :       ALLOCATE (farming_env)
     517           24 :       CALL init_farming_env(farming_env)
     518              :       ! remember where we started
     519           24 :       CALL m_getcwd(farming_env%cwd)
     520           24 :       CALL farming_parse_input(farming_env, root_section, para_env)
     521              : 
     522              :       ! the full mpi group is first split in a minion group and a captain group, the latter being at most 1 process
     523           24 :       minion = .TRUE.
     524           24 :       captain = .FALSE.
     525           24 :       IF (farming_env%captain_minion) THEN
     526            4 :          IF (output_unit > 0) WRITE (output_unit, FMT="(T2,A)") "FARMING| Using a Captain-Minion setup"
     527              : 
     528            4 :          ALLOCATE (captain_minion_partition(0:1))
     529           12 :          captain_minion_partition = [1, para_env%num_pe - 1]
     530           12 :          ALLOCATE (group_distribution(0:para_env%num_pe - 1))
     531              : 
     532              :          CALL minion_group%from_split(para_env, ngroups, group_distribution, &
     533            4 :                                       n_subgroups=2, group_partition=captain_minion_partition)
     534            4 :          DEALLOCATE (captain_minion_partition)
     535            4 :          DEALLOCATE (group_distribution)
     536            4 :          num_minions = minion_group%num_pe
     537            4 :          minion_rank = minion_group%mepos
     538              : 
     539            4 :          IF (para_env%mepos == 0) THEN
     540            2 :             minion = .FALSE.
     541            2 :             captain = .TRUE.
     542              :             ! on the captain node, num_minions corresponds to the size of the captain group
     543            2 :             CPASSERT(num_minions == 1)
     544            2 :             num_minions = para_env%num_pe - 1
     545            2 :             minion_rank = -1
     546              :          END IF
     547            4 :          CPASSERT(num_minions == para_env%num_pe - 1)
     548              :       ELSE
     549              :          ! all processes are minions
     550           20 :          IF (output_unit > 0) WRITE (output_unit, FMT="(T2,A)") "FARMING| Using a Minion-only setup"
     551           20 :          CALL minion_group%from_dup(para_env)
     552           20 :          num_minions = minion_group%num_pe
     553           20 :          minion_rank = minion_group%mepos
     554              :       END IF
     555           24 :       IF (output_unit > 0) WRITE (output_unit, FMT="(T2,A,I0)") "FARMING| Number of Minions ", num_minions
     556              : 
     557              :       ! keep track of which para_env rank is which minion/captain
     558           72 :       ALLOCATE (minion_distribution(0:para_env%num_pe - 1))
     559           72 :       minion_distribution = 0
     560           24 :       minion_distribution(para_env%mepos) = minion_rank
     561          120 :       CALL para_env%sum(minion_distribution)
     562              :       ! we do have a primus inter pares
     563           24 :       primus_minion = 0
     564           48 :       DO i = 1, para_env%num_pe - 1
     565           48 :          IF (minion_distribution(i) == 0) primus_minion = i
     566              :       END DO
     567              : 
     568              :       ! split the current communicator for the minions
     569              :       ! in a new_group, new_size and new_rank according to the number of groups required according to the input
     570           72 :       ALLOCATE (group_distribution(0:num_minions - 1))
     571           68 :       group_distribution = -1
     572           24 :       IF (minion) THEN
     573           22 :          IF (farming_env%group_size_wish_set) THEN
     574            4 :             farming_env%group_size_wish = MIN(farming_env%group_size_wish, para_env%num_pe)
     575              :             CALL new_group%from_split(minion_group, ngroups, group_distribution, &
     576            4 :                                       subgroup_min_size=farming_env%group_size_wish, stride=farming_env%stride)
     577           18 :          ELSE IF (farming_env%ngroup_wish_set) THEN
     578           18 :             IF (ASSOCIATED(farming_env%group_partition)) THEN
     579              :                CALL new_group%from_split(minion_group, ngroups, group_distribution, &
     580              :                                          n_subgroups=farming_env%ngroup_wish, &
     581            0 :                                          group_partition=farming_env%group_partition, stride=farming_env%stride)
     582              :             ELSE
     583              :                CALL new_group%from_split(minion_group, ngroups, group_distribution, &
     584           18 :                                          n_subgroups=farming_env%ngroup_wish, stride=farming_env%stride)
     585              :             END IF
     586              :          ELSE
     587            0 :             CPABORT("must set either group_size_wish or ngroup_wish")
     588              :          END IF
     589           22 :          new_rank = new_group%mepos
     590              :       END IF
     591              : 
     592              :       ! transfer the info about the minion group distribution to the captain
     593           24 :       IF (farming_env%captain_minion) THEN
     594            4 :          IF (para_env%mepos == primus_minion) THEN
     595            2 :             tag = 1
     596            4 :             CALL para_env%send(group_distribution, 0, tag)
     597            2 :             tag = 2
     598            2 :             CALL para_env%send(ngroups, 0, tag)
     599              :          END IF
     600            4 :          IF (para_env%mepos == 0) THEN
     601            2 :             tag = 1
     602            6 :             CALL para_env%recv(group_distribution, primus_minion, tag)
     603            2 :             tag = 2
     604            2 :             CALL para_env%recv(ngroups, primus_minion, tag)
     605              :          END IF
     606              :       END IF
     607              : 
     608              :       ! write info on group distribution
     609           24 :       IF (output_unit > 0) THEN
     610           12 :          WRITE (output_unit, FMT="(T2,A,T71,I10)") "FARMING| Number of created MPI (Minion) groups:", ngroups
     611           12 :          WRITE (output_unit, FMT="(T2,A)", ADVANCE="NO") "FARMING| MPI (Minion) process to group correspondence:"
     612           34 :          DO i = 0, num_minions - 1
     613           22 :             IF (MODULO(i, 4) == 0) WRITE (output_unit, *)
     614              :             WRITE (output_unit, FMT='(A3,I6,A3,I6,A1)', ADVANCE="NO") &
     615           34 :                "  (", i, " : ", group_distribution(i), ")"
     616              :          END DO
     617           12 :          WRITE (output_unit, *)
     618           12 :          CALL m_flush(output_unit)
     619              :       END IF
     620              : 
     621              :       ! protect about too many jobs being run in single go. Not more jobs are allowed than the number in the input file
     622              :       ! and determine the future restart point
     623           24 :       IF (farming_env%cycle) THEN
     624            2 :          n_jobs_to_run = farming_env%max_steps*ngroups
     625            2 :          i_job_to_restart = MODULO(farming_env%restart_n + n_jobs_to_run - 1, farming_env%njobs) + 1
     626              :       ELSE
     627           22 :          n_jobs_to_run = MIN(farming_env%njobs, farming_env%max_steps*ngroups)
     628           22 :          n_jobs_to_run = MIN(n_jobs_to_run, farming_env%njobs - farming_env%restart_n + 1)
     629           22 :          i_job_to_restart = n_jobs_to_run + farming_env%restart_n
     630              :       END IF
     631              : 
     632              :       ! and write the restart now, that's the point where the next job starts, even if this one is running
     633              :       iunit = cp_print_key_unit_nr(logger, root_section, "FARMING%RESTART", &
     634           24 :                                    extension=".restart")
     635           24 :       IF (iunit > 0) THEN
     636           12 :          WRITE (iunit, *) i_job_to_restart
     637              :       END IF
     638           24 :       CALL cp_print_key_finished_output(iunit, logger, root_section, "FARMING%RESTART")
     639              : 
     640              :       ! this is the job range to be executed.
     641           24 :       ijob_start = farming_env%restart_n
     642           24 :       ijob_end = ijob_start + n_jobs_to_run - 1
     643           24 :       IF (output_unit > 0 .AND. ijob_end - ijob_start < 0) THEN
     644            0 :          WRITE (output_unit, FMT="(T2,A)") "FARMING| --- WARNING --- NO JOBS NEED EXECUTION ? "
     645            0 :          WRITE (output_unit, FMT="(T2,A)") "FARMING| is the cycle keyword required ?"
     646            0 :          WRITE (output_unit, FMT="(T2,A)") "FARMING| or is a stray RESTART file present ?"
     647            0 :          WRITE (output_unit, FMT="(T2,A)") "FARMING| or is the group_size requested smaller than the number of CPUs?"
     648              :       END IF
     649              : 
     650              :       ! actual executions of the jobs in two different modes
     651           24 :       IF (farming_env%captain_minion) THEN
     652            4 :          IF (minion) THEN
     653              :             ! keep on doing work until captain has decided otherwise
     654            2 :             todo = do_wait
     655              :             DO
     656           20 :                IF (new_rank == 0) THEN
     657              :                   ! the head minion tells the captain he's done or ready to start
     658              :                   ! the message tells what has been done lately
     659           20 :                   tag = 1
     660           20 :                   dest = 0
     661           20 :                   CALL para_env%send(todo, dest, tag)
     662              : 
     663              :                   ! gets the new todo item
     664           20 :                   tag = 2
     665           20 :                   source = 0
     666           20 :                   CALL para_env%recv(todo, source, tag)
     667              : 
     668              :                   ! and informs his peer minions
     669           20 :                   CALL new_group%bcast(todo, 0)
     670              :                ELSE
     671            0 :                   CALL new_group%bcast(todo, 0)
     672              :                END IF
     673              : 
     674              :                ! if the todo is do_nothing we are flagged to quit. Otherwise it is the job number
     675            0 :                SELECT CASE (todo)
     676              :                CASE (do_wait, do_deadlock)
     677              :                   ! go for a next round, but we first wait a bit
     678            0 :                   t1 = m_walltime()
     679              :                   DO
     680            0 :                      t2 = m_walltime()
     681            0 :                      IF (t2 - t1 > farming_env%wait_time) EXIT
     682              :                   END DO
     683              :                CASE (do_nothing)
     684           18 :                   EXIT
     685              :                CASE (1:)
     686           20 :                   CALL execute_job(todo)
     687              :                END SELECT
     688              :             END DO
     689              :          ELSE ! captain
     690            6 :             ALLOCATE (minion_status(0:ngroups - 1))
     691            4 :             minion_status = minion_status_wait
     692            2 :             ijob_current = ijob_start - 1
     693              : 
     694           20 :             DO
     695           24 :                IF (ALL(minion_status == minion_status_done)) EXIT
     696              : 
     697              :                ! who's the next minion waiting for work
     698           20 :                tag = 1
     699           20 :                source = mp_any_source
     700           20 :                CALL para_env%recv(todo, source, tag) ! updates source
     701           20 :                IF (todo > 0) THEN
     702           18 :                   farming_env%Job(todo)%status = job_finished
     703           18 :                   IF (output_unit > 0) THEN
     704           18 :                      WRITE (output_unit, FMT=*) "Job finished: ", todo
     705           18 :                      CALL m_flush(output_unit)
     706              :                   END IF
     707              :                END IF
     708              : 
     709              :                ! get the next job in line, this could be do_nothing, if we're finished
     710           20 :                CALL get_next_job(farming_env, ijob_start, ijob_end, ijob_current, todo)
     711           20 :                dest = source
     712           20 :                tag = 2
     713           20 :                CALL para_env%send(todo, dest, tag)
     714              : 
     715           22 :                IF (todo > 0) THEN
     716           18 :                   farming_env%Job(todo)%status = job_running
     717           18 :                   IF (output_unit > 0) THEN
     718           18 :                      WRITE (output_unit, FMT=*) "Job: ", todo, " Dir: ", TRIM(farming_env%Job(todo)%cwd), &
     719           36 :                         " assigned to group ", group_distribution(minion_distribution(dest))
     720           18 :                      CALL m_flush(output_unit)
     721              :                   END IF
     722              :                ELSE
     723            2 :                   IF (todo == do_nothing) THEN
     724            2 :                      minion_status(group_distribution(minion_distribution(dest))) = minion_status_done
     725            2 :                      IF (output_unit > 0) THEN
     726            2 :                         WRITE (output_unit, FMT=*) "group done: ", group_distribution(minion_distribution(dest))
     727            2 :                         CALL m_flush(output_unit)
     728              :                      END IF
     729              :                   END IF
     730            2 :                   IF (todo == do_deadlock) THEN
     731            0 :                      IF (output_unit > 0) THEN
     732            0 :                         WRITE (output_unit, FMT=*) ""
     733            0 :                         WRITE (output_unit, FMT=*) "FARMING JOB DEADLOCKED ... CIRCULAR DEPENDENCIES"
     734            0 :                         WRITE (output_unit, FMT=*) ""
     735            0 :                         CALL m_flush(output_unit)
     736              :                      END IF
     737            0 :                      CPASSERT(todo /= do_deadlock)
     738              :                   END IF
     739              :                END IF
     740              : 
     741              :             END DO
     742              : 
     743            2 :             DEALLOCATE (minion_status)
     744              : 
     745              :          END IF
     746              :       ELSE
     747              :          ! this is the non-captain-minion mode way of executing the jobs
     748              :          ! the i-th job in the input is always executed by the MODULO(i-1,ngroups)-th group
     749              :          ! (needed for cyclic runs, we don't want two groups working on the same job)
     750           20 :          IF (output_unit > 0) THEN
     751           10 :             IF (ijob_end - ijob_start >= 0) THEN
     752           10 :                WRITE (output_unit, FMT="(T2,A)") "FARMING| List of jobs : "
     753           81 :                DO ijob = ijob_start, ijob_end
     754           71 :                   i = MODULO(ijob - 1, farming_env%njobs) + 1
     755           71 :                   WRITE (output_unit, FMT=*) "Job: ", i, " Dir: ", TRIM(farming_env%Job(i)%cwd), " Input: ", &
     756          152 :                      TRIM(farming_env%Job(i)%input), " MPI group:", MODULO(i - 1, ngroups)
     757              :                END DO
     758              :             END IF
     759           10 :             CALL m_flush(output_unit)
     760              :          END IF
     761              : 
     762          162 :          DO ijob = ijob_start, ijob_end
     763          142 :             i = MODULO(ijob - 1, farming_env%njobs) + 1
     764              :             ! this farms out the jobs
     765          162 :             IF (MODULO(i - 1, ngroups) == group_distribution(minion_rank)) THEN
     766          104 :                IF (output_unit > 0) THEN
     767           54 :                   WRITE (output_unit, FMT="(T2,A,I5.5,A)", ADVANCE="NO") " Running Job ", i, &
     768          108 :                      " in "//TRIM(farming_env%Job(i)%cwd)//"."
     769           54 :                   CALL m_flush(output_unit)
     770              :                END IF
     771          104 :                CALL execute_job(i)
     772          104 :                IF (output_unit > 0) THEN
     773           54 :                   WRITE (output_unit, FMT="(A)") " Done, output in "//TRIM(output_file)
     774           54 :                   CALL m_flush(output_unit)
     775              :                END IF
     776              :             END IF
     777              :          END DO
     778              :       END IF
     779              : 
     780              :       ! keep information about how long each process has to wait
     781              :       ! i.e. the load imbalance
     782           24 :       t1 = m_walltime()
     783           24 :       CALL para_env%sync()
     784           24 :       t2 = m_walltime()
     785           72 :       ALLOCATE (waittime(0:para_env%num_pe - 1))
     786           24 :       waittime = 0.0_dp
     787           24 :       waittime(para_env%mepos) = t2 - t1
     788           24 :       CALL para_env%sum(waittime)
     789           24 :       IF (output_unit > 0) THEN
     790           12 :          WRITE (output_unit, '(T2,A)') "Process idle times [s] at the end of the run"
     791           36 :          DO i = 0, para_env%num_pe - 1
     792              :             WRITE (output_unit, FMT='(A2,I6,A3,F8.3,A1)', ADVANCE="NO") &
     793           24 :                " (", i, " : ", waittime(i), ")"
     794           36 :             IF (MOD(i + 1, 4) == 0) WRITE (output_unit, '(A)') ""
     795              :          END DO
     796           12 :          CALL m_flush(output_unit)
     797              :       END IF
     798           24 :       DEALLOCATE (waittime)
     799              : 
     800              :       ! give back the communicators of the split groups
     801           24 :       IF (minion) CALL new_group%free()
     802           24 :       CALL minion_group%free()
     803              : 
     804              :       ! and message passing deallocate structures
     805           24 :       DEALLOCATE (group_distribution)
     806           24 :       DEALLOCATE (minion_distribution)
     807              : 
     808              :       ! clean the farming env
     809           24 :       CALL deallocate_farming_env(farming_env)
     810              : 
     811              :       CALL cp_print_key_finished_output(output_unit, logger, root_section, &
     812           24 :                                         "FARMING%PROGRAM_RUN_INFO")
     813              : 
     814          336 :       CALL timestop(handle)
     815              : 
     816              :    CONTAINS
     817              : ! **************************************************************************************************
     818              : !> \brief ...
     819              : !> \param i ...
     820              : ! **************************************************************************************************
     821          122 :       RECURSIVE SUBROUTINE execute_job(i)
     822              :       INTEGER                                            :: i
     823              : 
     824              :          ! change to the new working directory
     825              : 
     826          122 :          CALL m_chdir(TRIM(farming_env%Job(i)%cwd), ierr)
     827          122 :          IF (ierr /= 0) THEN
     828            0 :             CPABORT("Failed to change dir to: "//TRIM(farming_env%Job(i)%cwd))
     829              :          END IF
     830              : 
     831              :          ! generate a fresh call to cp2k_run
     832          122 :          IF (new_rank == 0) THEN
     833              : 
     834           89 :             IF (farming_env%Job(i)%output == "") THEN
     835              :                ! generate the output file
     836           85 :                WRITE (output_file, '(A12,I5.5)') "FARMING_OUT_", i
     837          255 :                ALLOCATE (my_parser)
     838           85 :                CALL parser_create(my_parser, file_name=TRIM(farming_env%Job(i)%input))
     839           85 :                label = "&GLOBAL"
     840           85 :                CALL parser_search_string(my_parser, label, ignore_case=.TRUE., found=found)
     841          170 :                IF (found) THEN
     842           85 :                   CALL create_global_section(g_section)
     843           85 :                   CALL section_vals_create(g_data, g_section)
     844              :                   CALL cp_unit_set_create(default_units, "OUTPUT")
     845           85 :                   CALL section_vals_parse(g_data, my_parser, default_units)
     846           85 :                   CALL cp_unit_set_release(default_units)
     847              :                   CALL section_vals_val_get(g_data, "PROJECT", &
     848           85 :                                             c_val=str)
     849           85 :                   IF (str /= "") output_file = TRIM(str)//".out"
     850              :                   CALL section_vals_val_get(g_data, "OUTPUT_FILE_NAME", &
     851           85 :                                             c_val=str)
     852           85 :                   IF (str /= "") output_file = str
     853           85 :                   CALL section_vals_release(g_data)
     854           85 :                   CALL section_release(g_section)
     855              :                END IF
     856           85 :                CALL parser_release(my_parser)
     857           85 :                DEALLOCATE (my_parser)
     858              :             ELSE
     859            4 :                output_file = farming_env%Job(i)%output
     860              :             END IF
     861              : 
     862              :             CALL open_file(file_name=TRIM(output_file), &
     863              :                            file_action="WRITE", &
     864              :                            file_status="UNKNOWN", &
     865              :                            file_position="APPEND", &
     866           89 :                            unit_number=new_output_unit)
     867              :          ELSE
     868              :             ! this unit should be negative, otherwise all processors that get a default unit
     869              :             ! start writing output (to the same file, adding to confusion).
     870              :             ! error handling should be careful, asking for a local output unit if required
     871           33 :             new_output_unit = -1
     872              :          END IF
     873              : 
     874          122 :          CALL cp2k_run(input_declaration, TRIM(farming_env%Job(i)%input), new_output_unit, new_group, initial_variables)
     875          122 :          CALL dbcsr_finalize_lib()
     876              : 
     877          122 :          IF (new_rank == 0) CALL close_file(unit_number=new_output_unit)
     878              : 
     879              :          ! change to the original working directory
     880          122 :          CALL m_chdir(TRIM(farming_env%cwd), ierr)
     881          122 :          CPASSERT(ierr == 0)
     882              : 
     883          122 :       END SUBROUTINE execute_job
     884              :    END SUBROUTINE farming_run
     885              : 
     886              : ! **************************************************************************************************
     887              : !> \brief ...
     888              : ! **************************************************************************************************
     889            0 :    SUBROUTINE write_xml_file()
     890              : 
     891              :       INTEGER                                            :: i, unit_number
     892              :       TYPE(section_type), POINTER                        :: root_section
     893              : 
     894            0 :       NULLIFY (root_section)
     895            0 :       CALL create_cp2k_root_section(root_section)
     896            0 :       CALL keyword_release(root_section%keywords(0)%keyword)
     897              :       CALL open_file(unit_number=unit_number, &
     898              :                      file_name="cp2k_input.xml", &
     899              :                      file_action="WRITE", &
     900            0 :                      file_status="REPLACE")
     901              : 
     902            0 :       WRITE (UNIT=unit_number, FMT="(A)") '<?xml version="1.0" encoding="utf-8"?>'
     903              : 
     904              :       !MK CP2K input structure
     905              :       WRITE (UNIT=unit_number, FMT="(A)") &
     906            0 :          "<CP2K_INPUT>", &
     907            0 :          " <CP2K_VERSION>"//TRIM(cp2k_version)//"</CP2K_VERSION>", &
     908            0 :          " <CP2K_YEAR>"//TRIM(cp2k_year)//"</CP2K_YEAR>", &
     909            0 :          " <COMPILE_DATE>"//TRIM(compile_date)//"</COMPILE_DATE>", &
     910            0 :          " <COMPILE_REVISION>"//TRIM(compile_revision)//"</COMPILE_REVISION>"
     911              : 
     912            0 :       CALL export_references_as_xml(unit_number)
     913            0 :       CALL export_units_as_xml(unit_number)
     914              : 
     915            0 :       DO i = 1, root_section%n_subsections
     916            0 :          CALL write_section_xml(root_section%subsections(i)%section, 1, unit_number)
     917              :       END DO
     918              : 
     919            0 :       WRITE (UNIT=unit_number, FMT="(A)") "</CP2K_INPUT>"
     920            0 :       CALL close_file(unit_number=unit_number)
     921            0 :       CALL section_release(root_section)
     922              : 
     923            0 :    END SUBROUTINE write_xml_file
     924              : 
     925              : ! **************************************************************************************************
     926              : !> \brief runs the given input
     927              : !> \param input_declaration ...
     928              : !> \param input_file_path the path of the input file
     929              : !> \param output_file_path path of the output file (to which it is appended)
     930              : !>        if it is "__STD_OUT__" the default_output_unit is used
     931              : !> \param initial_variables key-value list of initial preprocessor variables
     932              : !> \param mpi_comm the mpi communicator to be used for this environment
     933              : !>        it will not be freed
     934              : !> \author fawzi
     935              : !> \note
     936              : !>      moved here because of circular dependencies
     937              : ! **************************************************************************************************
     938        11182 :    SUBROUTINE run_input(input_declaration, input_file_path, output_file_path, initial_variables, mpi_comm)
     939              :       TYPE(section_type), POINTER                        :: input_declaration
     940              :       CHARACTER(len=*), INTENT(in)                       :: input_file_path, output_file_path
     941              :       CHARACTER(len=default_path_length), &
     942              :          DIMENSION(:, :), INTENT(IN)                     :: initial_variables
     943              :       TYPE(mp_comm_type), INTENT(in), OPTIONAL           :: mpi_comm
     944              : 
     945              :       INTEGER                                            :: unit_nr
     946              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     947              : 
     948        11182 :       IF (PRESENT(mpi_comm)) THEN
     949            4 :          ALLOCATE (para_env)
     950            4 :          para_env = mpi_comm
     951              :       ELSE
     952        11178 :          para_env => f77_default_para_env
     953        11178 :          CALL para_env%retain()
     954              :       END IF
     955        11182 :       IF (para_env%is_source()) THEN
     956         5591 :          IF (output_file_path == "__STD_OUT__") THEN
     957         5589 :             unit_nr = default_output_unit
     958              :          ELSE
     959            2 :             INQUIRE (FILE=output_file_path, NUMBER=unit_nr)
     960              :          END IF
     961              :       ELSE
     962         5591 :          unit_nr = -1
     963              :       END IF
     964        11182 :       CALL cp2k_run(input_declaration, input_file_path, unit_nr, para_env, initial_variables)
     965              :       ! Restore DBCSR diagnostics before the caller closes its output unit.
     966        11182 :       CALL configure_dbcsr(f77_default_para_env, cp_logger_get_default_io_unit())
     967        11182 :       CALL mp_para_env_release(para_env)
     968        11182 :    END SUBROUTINE run_input
     969              : 
     970              : END MODULE cp2k_runs
        

Generated by: LCOV version 2.0-1