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