Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Workers's routines for the swarm-framework
10 : !> \author Ole Schuett
11 : ! **************************************************************************************************
12 : MODULE swarm_worker
13 : USE cp_log_handling, ONLY: cp_get_default_logger,&
14 : cp_logger_type
15 : USE cp_output_handling, ONLY: cp_print_key_unit_nr
16 : USE glbopt_worker, ONLY: glbopt_worker_execute,&
17 : glbopt_worker_finalize,&
18 : glbopt_worker_init,&
19 : glbopt_worker_type
20 : USE input_constants, ONLY: swarm_do_glbopt
21 : USE input_section_types, ONLY: section_type,&
22 : section_vals_type,&
23 : section_vals_val_get
24 : USE kinds, ONLY: default_string_length
25 : USE message_passing, ONLY: mp_para_env_type
26 : USE swarm_message, ONLY: swarm_message_add,&
27 : swarm_message_get,&
28 : swarm_message_haskey,&
29 : swarm_message_type
30 : #include "../base/base_uses.f90"
31 :
32 : IMPLICIT NONE
33 : PRIVATE
34 :
35 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'swarm_worker'
36 :
37 : PUBLIC :: swarm_worker_init, swarm_worker_finalize
38 : PUBLIC :: swarm_worker_execute
39 : PUBLIC :: swarm_worker_type
40 :
41 : TYPE swarm_worker_type
42 : PRIVATE
43 : INTEGER :: id = -1
44 : INTEGER :: iw = -1
45 : INTEGER :: behavior = -1
46 : TYPE(glbopt_worker_type), POINTER :: glbopt => Null()
47 : !possibly more behaviors...
48 : END TYPE swarm_worker_type
49 :
50 : CONTAINS
51 :
52 : ! **************************************************************************************************
53 : !> \brief Initializes a swarm worker
54 : !> \param worker ...
55 : !> \param para_env ...
56 : !> \param input_declaration ...
57 : !> \param root_section ...
58 : !> \param input_path ...
59 : !> \param worker_id ...
60 : !> \author Ole Schuett
61 : ! **************************************************************************************************
62 3 : SUBROUTINE swarm_worker_init(worker, para_env, input_declaration, root_section, &
63 : input_path, worker_id)
64 : TYPE(swarm_worker_type), INTENT(INOUT) :: worker
65 : TYPE(mp_para_env_type), POINTER :: para_env
66 : TYPE(section_type), POINTER :: input_declaration
67 : TYPE(section_vals_type), POINTER :: root_section
68 : CHARACTER(LEN=*), INTENT(IN) :: input_path
69 : INTEGER, INTENT(in) :: worker_id
70 :
71 : TYPE(cp_logger_type), POINTER :: logger
72 :
73 3 : worker%id = worker_id
74 :
75 : ! getting an output unit for logging
76 3 : logger => cp_get_default_logger()
77 : worker%iw = cp_print_key_unit_nr(logger, root_section, &
78 3 : "SWARM%PRINT%WORKER_RUN_INFO", extension=".workerLog")
79 :
80 3 : CALL section_vals_val_get(root_section, "SWARM%BEHAVIOR", i_val=worker%behavior)
81 :
82 6 : SELECT CASE (worker%behavior)
83 : CASE (swarm_do_glbopt)
84 3 : ALLOCATE (worker%glbopt)
85 : CALL glbopt_worker_init(worker%glbopt, input_declaration, para_env, &
86 3 : root_section, input_path, worker_id, worker%iw)
87 : CASE DEFAULT
88 3 : CPABORT("got unknown behavior")
89 : END SELECT
90 :
91 3 : END SUBROUTINE swarm_worker_init
92 :
93 : ! **************************************************************************************************
94 : !> \brief Central execute routine of the swarm worker
95 : !> \param worker ...
96 : !> \param cmd ...
97 : !> \param report ...
98 : !> \param should_stop ...
99 : !> \author Ole Schuett
100 : ! **************************************************************************************************
101 51 : SUBROUTINE swarm_worker_execute(worker, cmd, report, should_stop)
102 : TYPE(swarm_worker_type), INTENT(INOUT) :: worker
103 : TYPE(swarm_message_type), INTENT(IN) :: cmd
104 : TYPE(swarm_message_type), INTENT(OUT) :: report
105 : LOGICAL, INTENT(INOUT) :: should_stop
106 :
107 : CHARACTER(LEN=default_string_length) :: command
108 :
109 51 : CALL swarm_message_get(cmd, "command", command)
110 51 : CALL swarm_message_add(report, "worker_id", worker%id)
111 :
112 51 : IF (TRIM(command) == "shutdown") THEN
113 3 : IF (worker%iw > 0) WRITE (worker%iw, *) "SWARM| Received shutdown command, quitting."
114 3 : should_stop = .TRUE.
115 48 : ELSE IF (TRIM(command) == "wait") THEN !only needed for serial driver
116 0 : CALL swarm_message_add(report, "status", "wait_done")
117 : ELSE
118 96 : SELECT CASE (worker%behavior)
119 : CASE (swarm_do_glbopt)
120 48 : CALL glbopt_worker_execute(worker%glbopt, cmd, report)
121 : CASE DEFAULT
122 48 : CPABORT("got unknown behavior")
123 : END SELECT
124 : END IF
125 :
126 51 : IF (.NOT. swarm_message_haskey(report, "status")) &
127 51 : CALL swarm_message_add(report, "status", "ok")
128 :
129 51 : END SUBROUTINE swarm_worker_execute
130 :
131 : ! **************************************************************************************************
132 : !> \brief Finalizes a swarm worker
133 : !> \param worker ...
134 : !> \author Ole Schuett
135 : ! **************************************************************************************************
136 3 : SUBROUTINE swarm_worker_finalize(worker)
137 : TYPE(swarm_worker_type), INTENT(INOUT) :: worker
138 :
139 6 : SELECT CASE (worker%behavior)
140 : CASE (swarm_do_glbopt)
141 3 : CALL glbopt_worker_finalize(worker%glbopt)
142 3 : DEALLOCATE (worker%glbopt)
143 : CASE DEFAULT
144 3 : CPABORT("got unknown behavior")
145 : END SELECT
146 :
147 3 : END SUBROUTINE swarm_worker_finalize
148 :
149 0 : END MODULE swarm_worker
150 :
|