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

Generated by: LCOV version 2.0-1