40 #include "../base/base_uses.f90"
45 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'swarm_master'
47 PUBLIC :: swarm_master_type
51 TYPE swarm_message_p_type
52 TYPE(swarm_message_type),
POINTER :: p => null()
53 END TYPE swarm_message_p_type
55 TYPE swarm_master_type
57 INTEGER :: behavior = -1
58 TYPE(glbopt_master_type),
POINTER :: glbopt => null()
61 INTEGER :: i_iteration = 0
62 INTEGER :: max_iter = 0
63 LOGICAL :: should_stop = .false.
64 INTEGER :: n_workers = -1
65 INTEGER :: comlog_unit = -1
66 TYPE(section_vals_type),
POINTER :: swarm_section => null()
67 TYPE(mp_para_env_type),
POINTER :: para_env => null()
68 TYPE(swarm_message_p_type),
DIMENSION(:),
POINTER :: queued_commands => null()
69 TYPE(global_environment_type),
POINTER :: globenv => null()
70 LOGICAL :: ignore_last_iteration = .false.
71 INTEGER :: n_waiting = 0
72 END TYPE swarm_master_type
86 TYPE(swarm_master_type) :: master
87 TYPE(mp_para_env_type),
POINTER :: para_env
88 TYPE(global_environment_type),
POINTER :: globenv
89 TYPE(section_vals_type),
POINTER :: root_section
90 INTEGER,
INTENT(IN) :: n_workers
92 TYPE(cp_logger_type),
POINTER :: logger
97 master%n_workers = n_workers
98 master%para_env => para_env
99 master%globenv => globenv
100 ALLOCATE (master%queued_commands(master%n_workers))
102 "PRINT%MASTER_RUN_INFO", extension=
".masterLog")
107 master%comlog_unit =
cp_print_key_unit_nr(logger, master%swarm_section,
"PRINT%COMMUNICATION_LOG", &
109 extension=
".comlog", &
110 file_action=
"WRITE", file_position=
"REWIND")
114 SELECT CASE (master%behavior)
116 ALLOCATE (master%glbopt)
119 cpabort(
"got unknown behavior")
122 CALL replay_comlog(master)
130 SUBROUTINE replay_comlog(master)
131 TYPE(swarm_master_type) :: master
133 CHARACTER(LEN=default_path_length) :: filename
134 CHARACTER(LEN=default_string_length) :: command_log
135 INTEGER :: handle, i, worker_id
136 LOGICAL :: at_end, explicit
137 TYPE(cp_parser_type) :: parser
138 TYPE(swarm_message_type) :: cmd_log, report_log
139 TYPE(swarm_message_type), &
140 DIMENSION(master%n_workers) :: last_commands
141 TYPE(swarm_message_type),
POINTER :: cmd_now
145 c_val=filename, explicit=explicit)
147 IF (.NOT. explicit)
RETURN
148 IF (master%iw > 0)
WRITE (master%iw,
'(A,A)') &
149 " SWARM| Starting replay of communication-log: ", trim(filename)
151 CALL timeset(
"swarm_master_replay_comlog", handle)
152 CALL parser_create(parser, filename, para_env=master%para_env)
166 CALL swarm_message_get(cmd_log,
"command", command_log)
167 IF (trim(command_log) /=
"shutdown")
THEN
168 IF (.NOT. commands_equal(cmd_now, cmd_log, master%iw)) cpabort(
"wrong behaviour")
173 CALL swarm_message_get(cmd_now,
"worker_id", worker_id)
175 last_commands(worker_id) = cmd_now
182 IF (master%iw > 0)
WRITE (master%iw,
'(A,A)') &
183 " SWARM| Reached end of communication log. Queueing last commands."
185 DO i = 1, master%n_workers
186 ALLOCATE (master%queued_commands(i)%p)
187 master%queued_commands(i)%p = last_commands(i)
191 CALL timestop(handle)
192 END SUBROUTINE replay_comlog
202 FUNCTION commands_equal(cmd1, cmd2, iw)
RESULT(res)
203 TYPE(swarm_message_type) :: cmd1, cmd2
208 IF (.NOT. res .AND. iw > 0)
THEN
209 WRITE (iw, *)
"Command 1:"
211 WRITE (iw, *)
"Command 2:"
214 END FUNCTION commands_equal
224 TYPE(swarm_master_type),
INTENT(INOUT) :: master
225 TYPE(swarm_message_type),
INTENT(IN) :: report
226 TYPE(swarm_message_type),
INTENT(OUT) :: cmd
228 CHARACTER(len=default_string_length) :: command, status
229 INTEGER :: handle, worker_id
230 LOGICAL :: should_stop
232 should_stop = .false.
234 CALL timeset(
"swarm_master_steer", handle)
237 CALL swarm_message_get(report,
"worker_id", worker_id)
239 IF (
ASSOCIATED(master%queued_commands(worker_id)%p))
THEN
240 cmd = master%queued_commands(worker_id)%p
241 DEALLOCATE (master%queued_commands(worker_id)%p)
242 IF (master%iw > 0)
WRITE (master%iw,
'(A,A,A,I9,1X,A)')
' SWARM| ', &
243 repeat(
"*", 9),
" Sending out queued command to worker: ", &
244 worker_id, repeat(
"*", 9)
245 CALL timestop(handle)
249 IF (.NOT. master%ignore_last_iteration)
THEN
251 master%i_iteration = master%i_iteration + 1
253 IF (master%iw > 0)
WRITE (master%iw,
'(A,A,1X,I8,A,A)')
' SWARM| ', repeat(
"*", 15), &
254 master%i_iteration,
' Master / Worker Communication ', repeat(
"*", 15)
257 IF (master%i_iteration >= master%max_iter .AND. .NOT. master%should_stop)
THEN
258 IF (master%iw > 0)
WRITE (master%iw,
'(A)')
" SWARM| Reached MAX_ITER. Quitting."
259 master%should_stop = .true.
262 IF (.NOT. master%should_stop)
THEN
264 IF (master%should_stop .AND. master%iw > 0) &
265 WRITE (master%iw, *)
" SWARM| Received stop from external_control. Quitting."
270 IF (master%should_stop)
THEN
271 CALL swarm_message_add(cmd,
"command",
"shutdown")
272 IF (master%iw > 0)
WRITE (master%iw,
'(1X,A,T71,I10)') &
273 "SWARM| Sending shutdown command to worker", worker_id
275 SELECT CASE (master%behavior)
279 cpabort(
"got unknown behavior")
282 IF (should_stop)
THEN
284 CALL swarm_message_add(cmd,
"command",
"shutdown")
285 IF (master%iw > 0)
WRITE (master%iw,
'(1X,A,T71,I10)') &
286 "SWARM| Sending shutdown command to worker", worker_id
287 master%should_stop = .true.
291 CALL swarm_message_add(cmd,
"worker_id", worker_id)
294 CALL swarm_message_get(report,
"status", status)
295 CALL swarm_message_get(cmd,
"command", command)
296 IF (trim(status) ==
"wait_done") master%n_waiting = master%n_waiting - 1
297 IF (trim(command) ==
"wait") master%n_waiting = master%n_waiting + 1
298 IF (master%n_waiting < 0) cpabort(
"master%n_waiting < 0")
299 IF (trim(status) /=
"wait_done" .OR. trim(command) /=
"wait")
THEN
302 IF (master%n_waiting > 0 .AND. master%iw > 0)
WRITE (master%iw,
'(1X,A,T71,I10)') &
303 "SWARM| Number of waiting workers:", master%n_waiting
304 master%ignore_last_iteration = .false.
306 master%ignore_last_iteration = .true.
308 CALL timestop(handle)
317 TYPE(swarm_master_type) :: master
319 TYPE(cp_logger_type),
POINTER :: logger
321 IF (master%iw > 0)
THEN
322 WRITE (master%iw,
"(1X,A,T71,I10)")
"SWARM| Total number of iterations ", master%i_iteration
323 WRITE (master%iw,
"(A)")
" SWARM| Shutting down the master."
326 SELECT CASE (master%behavior)
329 DEALLOCATE (master%glbopt)
331 cpabort(
"got unknown behavior")
334 DEALLOCATE (master%queued_commands)
338 master%swarm_section,
"PRINT%MASTER_RUN_INFO")
340 master%swarm_section,
"PRINT%COMMUNICATION_LOG")
Routines to handle the external control of CP2K.
subroutine, public external_control(should_stop, flag, globenv, target_time, start_time, force_check)
External manipulations during a run : when the <PROJECT_NAME>.EXIT_$runtype command is sent the progr...
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...
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)
...
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,...
Utility routines to read data from files. Kept as close as possible to the old parser because.
subroutine, public parser_release(parser)
releases the parser
subroutine, public parser_create(parser, file_name, unit_nr, para_env, end_section_label, separator_chars, comment_char, continuation_char, quote_char, section_char, parse_white_lines, initial_variables, apply_preprocessing)
Start a parser run. Initial variables allow to @SET stuff before opening the file.
Master's routines for global optimization.
subroutine, public glbopt_master_steer(this, report, cmd, should_stop)
Central steering routine of global optimizer.
subroutine, public glbopt_master_init(this, para_env, root_section, n_walkers, iw)
Initializes the master of the global optimizer.
subroutine, public glbopt_master_finalize(this)
Finalized the master of the global optimizer.
Define type storing the global information of a run. Keep the amount of stored data small....
Defines the basic variable types.
integer, parameter, public default_string_length
integer, parameter, public default_path_length
Interface to the message passing library MPI.
Master's routines for the swarm-framework.
subroutine, public swarm_master_finalize(master)
Finalizes the swarm master.
subroutine, public swarm_master_steer(master, report, cmd)
Central steering routine of the swarm master.
subroutine, public swarm_master_init(master, para_env, globenv, root_section, n_workers)
Initializes the swarm master.
Swarm-message, a convenient data-container for with build-in serialization.
subroutine, public swarm_message_file_write(msg, unit)
Write a swarm-message to a given file / unit.
logical function, public swarm_message_equal(msg1, msg2)
Checks if two swarm-messages are equal.
subroutine, public swarm_message_free(msg)
Deallocates all entries contained in a swarm-message.
subroutine, public swarm_message_file_read(msg, parser, at_end)
Reads a swarm-message from a given file / unit.