(git:27c966b)
Loading...
Searching...
No Matches
farming_methods.F
Go to the documentation of this file.
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! **************************************************************************************************
10 USE cp_files, ONLY: close_file,&
27#include "./base/base_uses.f90"
28
29 IMPLICIT NONE
30 PRIVATE
32
33 ! must be negative in order to avoid confusion with job numbers
34 INTEGER, PARAMETER, PUBLIC :: do_nothing = -1, &
35 do_wait = -2, &
36 do_deadlock = -3
37
38 CHARACTER(len=*), PARAMETER, PRIVATE :: modulen = 'farming_methods'
39
40CONTAINS
41
42! **************************************************************************************************
43!> \brief ...
44!> \param farming_env ...
45!> \param start ...
46!> \param END ...
47!> \param current ...
48!> \param todo ...
49! **************************************************************************************************
50 SUBROUTINE get_next_job(farming_env, start, END, current, todo)
51 TYPE(farming_env_type), POINTER :: farming_env
52 INTEGER, INTENT(IN) :: start, end
53 INTEGER, INTENT(INOUT) :: current
54 INTEGER, INTENT(OUT) :: todo
55
56 INTEGER :: icheck, idep, itry, ndep
57 LOGICAL :: dep_ok
58
59 IF (farming_env%cycle) THEN
60 IF (current < start) THEN
61 current = start
62 ELSE
63 current = current + 1
64 END IF
65 IF (current > END) then
66 todo = do_nothing
67 ELSE
68 todo = modulo(current - 1, farming_env%njobs) + 1
69 END IF
70 ELSE
71 ! find a pending job
72 itry = start
73 todo = do_nothing
74 DO itry = start, END
75 IF (farming_env%job(itry)%status == job_pending) THEN
76
77 ! see if all dependencies are OK
78 ndep = SIZE(farming_env%job(itry)%dependencies)
79 dep_ok = .true.
80 dep: DO idep = 1, ndep
81 DO icheck = start, END
82 IF (farming_env%job(icheck)%status /= job_finished) THEN
83 IF (farming_env%job(icheck)%id == farming_env%job(itry)%dependencies(idep)) THEN
84 dep_ok = .false.
85 EXIT dep
86 END IF
87 END IF
88 END DO
89 END DO dep
90
91 ! if there are pending jobs, the minion can not be told to stop
92 ! at least wait if there are unresolved dependencies
93 IF (dep_ok) THEN
94 todo = itry
95 EXIT
96 ELSE
97 todo = do_wait
98 END IF
99 END IF
100 END DO
101 ! If we have to wait, but there are no running jobs we are deadlocked
102 ! which we signal
103 IF (todo == do_wait) THEN
104 dep_ok = .false.
105 DO itry = start, END
106 IF (farming_env%job(itry)%status == job_running) dep_ok = .true.
107 END DO
108 IF (.NOT. dep_ok) todo = do_deadlock
109 END IF
110 END IF
111 END SUBROUTINE get_next_job
112
113! **************************************************************************************************
114!> \brief ...
115!> \param farming_env ...
116!> \param root_section ...
117!> \param para_env ...
118! **************************************************************************************************
119 SUBROUTINE farming_parse_input(farming_env, root_section, para_env)
120 TYPE(farming_env_type), POINTER :: farming_env
121 TYPE(section_vals_type), POINTER :: root_section
122 TYPE(mp_para_env_type), POINTER :: para_env
123
124 CHARACTER(LEN=3) :: text
125 INTEGER :: i, iunit, n_rep_val, num_minions, &
126 output_unit, stat
127 INTEGER, DIMENSION(:), POINTER :: dependencies, i_vals
128 LOGICAL :: exists, explicit, has_dep
129 TYPE(cp_logger_type), POINTER :: logger
130 TYPE(section_vals_type), POINTER :: farming_section, jobs_section, print_key
131
132 NULLIFY (farming_section, jobs_section, print_key, logger, dependencies, i_vals)
133 logger => cp_get_default_logger()
134 farming_env%group_size_wish_set = .false.
135 farming_env%ngroup_wish_set = .false.
136 farming_section => section_vals_get_subs_vals(root_section, "FARMING")
137
138 IF (ASSOCIATED(farming_env%group_partition)) THEN
139 DEALLOCATE (farming_env%group_partition)
140 END IF
141
142 ! The following input order is used
143 ! 1) GROUP_PARTITION
144 ! 2) NGROUP
145 ! 3) GROUP_SIZE (default 8)
146 CALL section_vals_val_get(farming_section, "GROUP_PARTITION", &
147 n_rep_val=n_rep_val)
148 IF (n_rep_val > 0) THEN
149 CALL section_vals_val_get(farming_section, "GROUP_PARTITION", &
150 i_vals=i_vals)
151 ALLOCATE (farming_env%group_partition(0:SIZE(i_vals) - 1))
152 farming_env%group_partition(:) = i_vals
153 farming_env%ngroup_wish_set = .true.
154 farming_env%ngroup_wish = SIZE(i_vals)
155 ELSE
156 CALL section_vals_val_get(farming_section, "NGROUP", &
157 n_rep_val=n_rep_val)
158 IF (n_rep_val > 0) THEN
159 CALL section_vals_val_get(farming_section, "NGROUP", &
160 i_val=farming_env%ngroup_wish)
161 farming_env%ngroup_wish_set = .true.
162 ELSE
163 CALL section_vals_val_get(farming_section, "GROUP_SIZE", &
164 i_val=farming_env%group_size_wish)
165 farming_env%group_size_wish_set = .true.
166 END IF
167 END IF
168 CALL section_vals_val_get(farming_section, "STRIDE", &
169 i_val=farming_env%stride)
170
171 CALL section_vals_val_get(farming_section, "RESTART_FILE_NAME", &
172 explicit=explicit)
173 IF (explicit) THEN
174 CALL section_vals_val_get(farming_section, "RESTART_FILE_NAME", &
175 c_val=farming_env%restart_file_name)
176 ELSE
177 print_key => section_vals_get_subs_vals(farming_section, "RESTART")
178 farming_env%restart_file_name = cp_print_key_generate_filename(logger, print_key, extension=".restart", &
179 my_local=.false.)
180 END IF
181
182 CALL section_vals_val_get(farming_section, "DO_RESTART", &
183 l_val=farming_env%restart)
184 CALL section_vals_val_get(farming_section, "MAX_JOBS_PER_GROUP", &
185 i_val=farming_env%max_steps)
186 CALL section_vals_val_get(farming_section, "CYCLE", &
187 l_val=farming_env%cycle)
188 CALL section_vals_val_get(farming_section, "WAIT_TIME", &
189 r_val=farming_env%wait_time)
190
191 CALL section_vals_val_get(farming_section, "CAPTAIN_MINION", &
192 l_val=farming_env%captain_minion)
193
194 jobs_section => section_vals_get_subs_vals(farming_section, "JOB")
195 CALL section_vals_get(jobs_section, n_repetition=farming_env%njobs)
196
197 ALLOCATE (farming_env%Job(farming_env%njobs))
198 CALL init_job_type(farming_env%job)
199
200 has_dep = .false.
201 DO i = 1, farming_env%njobs
202 CALL section_vals_val_get(jobs_section, i_rep_section=i, &
203 keyword_name="DIRECTORY", c_val=farming_env%Job(i)%cwd)
204 CALL section_vals_val_get(jobs_section, i_rep_section=i, &
205 keyword_name="INPUT_FILE_NAME", c_val=farming_env%Job(i)%input)
206 CALL section_vals_val_get(jobs_section, i_rep_section=i, &
207 keyword_name="OUTPUT_FILE_NAME", c_val=farming_env%Job(i)%output)
208
209 ! if job id is not specified the job id is the index
210 CALL section_vals_val_get(jobs_section, i_rep_section=i, &
211 keyword_name="JOB_ID", n_rep_val=n_rep_val)
212 IF (n_rep_val == 0) THEN
213 farming_env%Job(i)%id = i
214 ELSE
215 CALL section_vals_val_get(jobs_section, i_rep_section=i, &
216 keyword_name="JOB_ID", i_val=farming_env%Job(i)%id)
217 END IF
218
219 ! get dependencies
220 CALL section_vals_val_get(jobs_section, i_rep_section=i, &
221 keyword_name="DEPENDENCIES", n_rep_val=n_rep_val)
222 IF (n_rep_val == 0) THEN
223 ALLOCATE (farming_env%Job(i)%dependencies(0))
224 ELSE
225 CALL section_vals_val_get(jobs_section, i_rep_section=i, &
226 keyword_name="DEPENDENCIES", i_vals=dependencies)
227 ALLOCATE (farming_env%Job(i)%dependencies(SIZE(dependencies, 1)))
228 farming_env%Job(i)%dependencies = dependencies
229 IF (SIZE(dependencies, 1) /= 0) has_dep = .true.
230 END IF
231 END DO
232
233 IF (has_dep) THEN
234 cpassert(farming_env%captain_minion)
235 cpassert(.NOT. farming_env%cycle)
236 END IF
237
238 output_unit = cp_print_key_unit_nr(logger, farming_section, "PROGRAM_RUN_INFO", &
239 extension=".log")
240
241 ! Captain/Minion not supported
242 IF (para_env%num_pe == 1) THEN
243 farming_env%captain_minion = .false.
244 WRITE (output_unit, fmt="(T2,A)") "FARMING| Captain-Minion setup not supported for serial runs"
245 END IF
246 IF (farming_env%captain_minion) THEN
247 num_minions = para_env%num_pe - 1
248 ELSE
249 num_minions = para_env%num_pe
250 END IF
251
252 IF (output_unit > 0) THEN
253 WRITE (output_unit, fmt="(T2,A,T71,I10)") "FARMING| Number of jobs found", farming_env%njobs
254 IF (farming_env%ngroup_wish_set) THEN
255 WRITE (output_unit, fmt="(T2,A,T71,I10)") "FARMING| Ngroup wish:", farming_env%ngroup_wish
256 IF (ASSOCIATED(farming_env%group_partition)) THEN
257 WRITE (output_unit, fmt="(T2,A)", advance="NO") "FARMING| User partition:"
258 DO i = 0, SIZE(farming_env%group_partition) - 1
259 IF (modulo(i, 4) == 0) WRITE (output_unit, *)
260 WRITE (output_unit, fmt='(I4)', advance="NO") farming_env%group_partition(i)
261 END DO
262 WRITE (output_unit, *)
263 IF (sum(farming_env%group_partition) /= num_minions) THEN
264 WRITE (output_unit, fmt="(T2,A,T61,I10,T71,I10)") &
265 "FARMING| WARNING : group partition CPUs not equal to the available number (ignoring Captain) ", &
266 num_minions, sum(farming_env%group_partition)
267 WRITE (output_unit, fmt="(T2,A)") "FARMING| partition data ignored" ! any better idea ??
268 DEALLOCATE (farming_env%group_partition)
269 END IF
270 END IF
271 END IF
272 IF (farming_env%group_size_wish_set) THEN
273 WRITE (output_unit, fmt="(T2,A,T71,I10)") "FARMING| Group size wish:", &
274 farming_env%group_size_wish
275 END IF
276 WRITE (output_unit, fmt="(T2,A,T71,I10)") "FARMING| Max steps :", farming_env%max_steps
277 IF (farming_env%cycle) THEN
278 text = "YES"
279 ELSE
280 text = " NO"
281 END IF
282 WRITE (output_unit, fmt="(T2,A,T78,A3)") "FARMING| Cyclic jobs execution:", text
283 IF (farming_env%restart) THEN
284 text = "YES"
285 ELSE
286 text = " NO"
287 END IF
288 WRITE (output_unit, fmt="(T2,A,T78,A3)") "FARMING| Restarting farm:", text
289 farming_env%restart_n = 1
290 IF (farming_env%restart) THEN
291 ! A missing restart file is not an error: the farm then starts with the first job
292 INQUIRE (file=farming_env%restart_file_name, exist=exists)
293 IF (exists) THEN
294 CALL open_file(unit_number=iunit, &
295 file_name=farming_env%restart_file_name)
296 READ (unit=iunit, fmt=*, iostat=stat) farming_env%restart_n
297 IF (stat /= 0) THEN
298 farming_env%restart_n = 1
299 WRITE (unit=output_unit, fmt="(T2,A)") &
300 "FARMING| ---- WARNING ---- failed to read from ("// &
301 trim(farming_env%restart_file_name)//") starting at 1"
302 ELSE
303 WRITE (unit=output_unit, fmt="(T2,A)") &
304 "FARMING| restarting from ("//trim(farming_env%restart_file_name)//")"
305 WRITE (unit=output_unit, fmt="(T2,A,T71,I10)") &
306 "FARMING| restarting at ", farming_env%restart_n
307 END IF
308 CALL close_file(unit_number=iunit)
309 ELSE
310 WRITE (unit=output_unit, fmt="(T2,A)") &
311 "FARMING| ---- WARNING ---- restart file ("// &
312 trim(farming_env%restart_file_name)//") not found, starting at 1"
313 END IF
314 END IF
315
316 CALL cp_print_key_finished_output(output_unit, logger, farming_section, &
317 "PROGRAM_RUN_INFO")
318 END IF
319 CALL para_env%bcast(farming_env%restart_n)
320
321 END SUBROUTINE farming_parse_input
322
323END MODULE farming_methods
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
Utility routines to open and close files.
Definition cp_files.F:22
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
Definition cp_files.F:197
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
Definition cp_files.F:64
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
character(len=default_path_length) function, public cp_print_key_generate_filename(logger, print_key, middle_name, extension, my_local)
Utility function that returns a unit number to write the print key. Might open a file with a unique f...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout, file_access)
...
subroutine, public farming_parse_input(farming_env, root_section, para_env)
...
integer, parameter, public do_nothing
integer, parameter, public do_wait
subroutine, public get_next_job(farming_env, start, end, current, todo)
...
integer, parameter, public do_deadlock
elemental subroutine, public init_job_type(job)
provide a default initialization
integer, parameter, public job_finished
integer, parameter, public job_running
integer, parameter, public job_pending
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Interface to the message passing library MPI.
type of a logger, at the moment it contains just a print level starting at which level it should be l...
stores all the informations relevant to an mpi environment