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 : !> \brief types used to handle many replica of the same system that differ only
10 : !> in atom positions, and velocity.
11 : !> This is useful for things like path integrals or nudged elastic band
12 : !> \note
13 : !> this is a stupid implementation that replicates all the information
14 : !> about the replicas, if you really want to do a *lot* of replicas on
15 : !> a lot of processors you should think about distributiong also that
16 : !> information
17 : !> \par History
18 : !> 09.2005 created [fawzi]
19 : !> \author fawzi
20 : ! **************************************************************************************************
21 : MODULE replica_types
22 : USE cp_log_handling, ONLY: cp_get_default_logger,&
23 : cp_logger_type,&
24 : cp_to_string
25 : USE cp_output_handling, ONLY: cp_rm_iter_level
26 : USE cp_result_methods, ONLY: cp_results_mp_bcast
27 : USE cp_result_types, ONLY: cp_result_p_type,&
28 : cp_result_release
29 : USE f77_interface, ONLY: destroy_force_env,&
30 : f_env_add_defaults,&
31 : f_env_rm_defaults,&
32 : f_env_type
33 : USE kinds, ONLY: default_path_length,&
34 : dp
35 : USE message_passing, ONLY: mp_para_cart_release,&
36 : mp_para_cart_type,&
37 : mp_para_env_release,&
38 : mp_para_env_type
39 : USE qs_wf_history_types, ONLY: qs_wf_history_p_type,&
40 : wfi_release
41 : #include "./base/base_uses.f90"
42 :
43 : IMPLICIT NONE
44 : PRIVATE
45 :
46 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
47 : LOGICAL, SAVE, PRIVATE :: module_initialized = .FALSE.
48 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'replica_types'
49 :
50 : PUBLIC :: replica_env_type
51 : PUBLIC :: rep_env_release
52 : PUBLIC :: rep_env_sync, rep_env_sync_results, rep_envs_add_rep_env
53 : PUBLIC :: rep_envs_get_rep_env
54 :
55 : ! **************************************************************************************************
56 : !> \brief keeps replicated information about the replicas
57 : !> \param ref_count reference count
58 : !> \param id_nr identity number (unique or each replica_env)
59 : !> \param nrep number of replicas
60 : !> \param nparticle number of particles (usually atoms) in each replica
61 : !> \param ndim = 3*nparticle
62 : !> \param f_env_id id of the force env that will do the calculations for the
63 : !> replicas owned by this processor
64 : !> \param r ,v,f: positions, velocities and forces of the replicas.
65 : !> the indexing is as follow (idir,iat,irep)
66 : !> \param replica_owner which replica group number owns the replica irep
67 : !> \param cart 2d distribution of the processors for the replicas,
68 : !> a column (or row if row_force was true in the rep_env_create call)
69 : !> work together on the same force_env (i.e. changing the
70 : !> row (column) you stay in the same replica), rows (columns) have
71 : !> different replicas
72 : !> \param force_dim which dimension of cart works on forces together
73 : !> used to be hardcoded to 1. Default is still 1, will
74 : !> be 2 if row_force is true in the rep_env_create call.
75 : !> \param para_env the global para env that contains all the replicas,
76 : !> this is just the cart as para_env
77 : !> \param para_env_f parallel environment of the underlying force
78 : !> environment
79 : !> \param inter_rep_rank mapping replica group number -> rank in para_env_inter_rep
80 : !> (this used to be col_rank)
81 : !> \param para_env_inter_rep parallel environment between replica
82 : !> \param force_rank mapping number of processor in force env -> rank in para_env_f
83 : !> (this used to be row_rank)
84 : !> \param local_rep_indices indices of the local replicas, starting at 1
85 : !> \param rep_is_local logical if specific replica is a local one.
86 : !> \param my_rep_group which replica group number this process belongs to
87 : !> (this used to be just cart%mepos(2) but with transposing the cart
88 : !> (row_force=.true.) became cart%mepos(1), and to generalize this it
89 : !> is now a separate variable, so one does not need to know
90 : !> which way the cart is mapped.)
91 : !> \param wf_history wavefunction history for the owned replicas
92 : !> \param keep_wf_history if the wavefunction history for the owned replicas
93 : !> should be kept
94 : !> \param n_wf_history number of wavefunction history slots owned by this group
95 : !> \author fawzi
96 : ! **************************************************************************************************
97 : TYPE replica_env_type
98 : INTEGER :: ref_count = -1, id_nr = -1, f_env_id = -1, &
99 : nrep = -1, ndim = -1, nparticle = -1, &
100 : my_rep_group = -1, force_dim = -1, &
101 : n_wf_history = -1
102 : REAL(kind=dp), DIMENSION(:, :), POINTER :: r => NULL(), v => NULL(), f => NULL()
103 : LOGICAL :: sync_v = .FALSE., keep_wf_history = .FALSE.
104 : CHARACTER(LEN=default_path_length) :: original_project_name = ""
105 : TYPE(qs_wf_history_p_type), DIMENSION(:), POINTER :: wf_history => NULL()
106 : TYPE(cp_result_p_type), DIMENSION(:), POINTER :: results => NULL()
107 : INTEGER, DIMENSION(:), POINTER :: local_rep_indices => NULL()
108 : INTEGER, DIMENSION(:), POINTER :: replica_owner => NULL(), force_rank => NULL(), &
109 : inter_rep_rank => NULL()
110 : LOGICAL, DIMENSION(:), POINTER :: rep_is_local => NULL()
111 : TYPE(mp_para_cart_type), POINTER :: cart => NULL()
112 : TYPE(mp_para_env_type), POINTER :: para_env => NULL(), para_env_f => NULL(), &
113 : para_env_inter_rep => NULL()
114 : END TYPE replica_env_type
115 :
116 : ! **************************************************************************************************
117 : !> \brief ****s* replica_types/replica_env_p_type *
118 : !>
119 : !> to build arrays of pointers to a replica_env_type
120 : !> \param rep_env the pointer to the replica_env
121 : !> \author fawzi
122 : ! **************************************************************************************************
123 : TYPE replica_env_p_type
124 : TYPE(replica_env_type), POINTER :: rep_env => NULL()
125 : END TYPE replica_env_p_type
126 :
127 : TYPE(replica_env_p_type), POINTER, DIMENSION(:), PRIVATE :: rep_envs
128 :
129 : CONTAINS
130 :
131 : ! **************************************************************************************************
132 : !> \brief releases the given replica environment
133 : !> \param rep_env the replica environment to release
134 : !> \author fawzi
135 : !> \note
136 : !> here and not in replica_types to allow the use of replica_env_type
137 : !> in a force_env (call to destroy_force_env gives circular dep)
138 : ! **************************************************************************************************
139 158 : SUBROUTINE rep_env_release(rep_env)
140 : TYPE(replica_env_type), POINTER :: rep_env
141 :
142 : CHARACTER(len=*), PARAMETER :: routineN = 'rep_env_release'
143 :
144 : INTEGER :: handle, i, ierr
145 :
146 158 : CALL timeset(routineN, handle)
147 158 : IF (ASSOCIATED(rep_env)) THEN
148 158 : CPASSERT(rep_env%ref_count > 0)
149 158 : rep_env%ref_count = rep_env%ref_count - 1
150 158 : IF (rep_env%ref_count == 0) THEN
151 158 : CALL rep_env_destroy_low(rep_env%id_nr, ierr)
152 158 : IF (rep_env%f_env_id > 0) THEN
153 158 : CALL destroy_force_env(rep_env%f_env_id, ierr)
154 158 : CPASSERT(ierr == 0)
155 : END IF
156 158 : IF (ASSOCIATED(rep_env%r)) THEN
157 158 : DEALLOCATE (rep_env%r)
158 : END IF
159 158 : IF (ASSOCIATED(rep_env%v)) THEN
160 158 : DEALLOCATE (rep_env%v)
161 : END IF
162 158 : IF (ASSOCIATED(rep_env%f)) THEN
163 158 : DEALLOCATE (rep_env%f)
164 : END IF
165 158 : IF (ASSOCIATED(rep_env%wf_history)) THEN
166 66 : DO i = 1, SIZE(rep_env%wf_history)
167 66 : CALL wfi_release(rep_env%wf_history(i)%wf_history)
168 : END DO
169 30 : DEALLOCATE (rep_env%wf_history)
170 : END IF
171 158 : IF (ASSOCIATED(rep_env%results)) THEN
172 676 : DO i = 1, SIZE(rep_env%results)
173 676 : CALL cp_result_release(rep_env%results(i)%results)
174 : END DO
175 158 : DEALLOCATE (rep_env%results)
176 : END IF
177 158 : DEALLOCATE (rep_env%local_rep_indices)
178 158 : DEALLOCATE (rep_env%rep_is_local)
179 158 : IF (ASSOCIATED(rep_env%replica_owner)) THEN
180 158 : DEALLOCATE (rep_env%replica_owner)
181 : END IF
182 158 : DEALLOCATE (rep_env%inter_rep_rank, rep_env%force_rank)
183 158 : CALL mp_para_cart_release(rep_env%cart)
184 158 : CALL mp_para_env_release(rep_env%para_env)
185 158 : CALL mp_para_env_release(rep_env%para_env_f)
186 158 : CALL mp_para_env_release(rep_env%para_env_inter_rep)
187 158 : CALL rep_envs_rm_rep_env(rep_env)
188 158 : DEALLOCATE (rep_env)
189 : END IF
190 : END IF
191 158 : NULLIFY (rep_env)
192 158 : CALL timestop(handle)
193 158 : END SUBROUTINE rep_env_release
194 :
195 : ! **************************************************************************************************
196 : !> \brief initializes the destruction of the replica_env
197 : !> \param rep_env_id id_nr of the replica environment that should be initialized
198 : !> \param ierr will be non zero if there is an initialization error
199 : !> \author fawzi
200 : ! **************************************************************************************************
201 316 : SUBROUTINE rep_env_destroy_low(rep_env_id, ierr)
202 : INTEGER, INTENT(in) :: rep_env_id
203 : INTEGER, INTENT(out) :: ierr
204 :
205 : INTEGER :: stat
206 : TYPE(cp_logger_type), POINTER :: logger
207 : TYPE(f_env_type), POINTER :: f_env
208 : TYPE(replica_env_type), POINTER :: rep_env
209 :
210 158 : rep_env => rep_envs_get_rep_env(rep_env_id, ierr=stat)
211 158 : IF (.NOT. ASSOCIATED(rep_env)) THEN
212 0 : CPABORT("could not find rep_env with id_nr"//cp_to_string(rep_env_id))
213 : END IF
214 158 : CALL f_env_add_defaults(f_env_id=rep_env%f_env_id, f_env=f_env)
215 158 : logger => cp_get_default_logger()
216 158 : CALL cp_rm_iter_level(logger, "REPLICA_EVAL")
217 158 : CALL f_env_rm_defaults(f_env, ierr)
218 158 : CPASSERT(ierr == 0)
219 158 : END SUBROUTINE rep_env_destroy_low
220 :
221 : ! **************************************************************************************************
222 : !> \brief sends the data from each replica to all the other
223 : !> on replica j/=i data from replica i overwrites val(:,i)
224 : !> \param rep_env replica environment
225 : !> \param vals the values to synchronize (second index runs over replicas)
226 : !> \author fawzi
227 : !> \note
228 : !> could be optimized: bcast in inter_rep, all2all or shift vs sum
229 : ! **************************************************************************************************
230 4994 : SUBROUTINE rep_env_sync(rep_env, vals)
231 : TYPE(replica_env_type), POINTER :: rep_env
232 : REAL(kind=dp), DIMENSION(:, :), INTENT(inout) :: vals
233 :
234 : CHARACTER(len=*), PARAMETER :: routineN = 'rep_env_sync'
235 :
236 : INTEGER :: handle, irep
237 :
238 4994 : CALL timeset(routineN, handle)
239 4994 : CPASSERT(ASSOCIATED(rep_env))
240 4994 : CPASSERT(rep_env%ref_count > 0)
241 4994 : CPASSERT(SIZE(vals, 2) == rep_env%nrep)
242 18086 : DO irep = 1, rep_env%nrep
243 18086 : IF (.NOT. rep_env%rep_is_local(irep)) THEN
244 1846230 : vals(:, irep) = 0._dp
245 : END IF
246 : END DO
247 7683954 : CALL rep_env%para_env_inter_rep%sum(vals)
248 4994 : CALL timestop(handle)
249 4994 : END SUBROUTINE rep_env_sync
250 :
251 : ! **************************************************************************************************
252 : !> \brief sends the data from each replica to all the other
253 : !> in this case the result type is passed
254 : !> \param rep_env replica environment
255 : !> \param results is an array of result_types
256 : !> \author fschiff
257 : ! **************************************************************************************************
258 4220 : SUBROUTINE rep_env_sync_results(rep_env, results)
259 : TYPE(replica_env_type), POINTER :: rep_env
260 : TYPE(cp_result_p_type), DIMENSION(:), POINTER :: results
261 :
262 : CHARACTER(len=*), PARAMETER :: routineN = 'rep_env_sync_results'
263 :
264 : INTEGER :: handle, irep, nrep, source
265 :
266 4220 : CALL timeset(routineN, handle)
267 4220 : nrep = rep_env%nrep
268 4220 : CPASSERT(ASSOCIATED(rep_env))
269 4220 : CPASSERT(rep_env%ref_count > 0)
270 4220 : CPASSERT(SIZE(results) == rep_env%nrep)
271 15158 : DO irep = 1, nrep
272 10938 : source = rep_env%inter_rep_rank(rep_env%replica_owner(irep))
273 15158 : CALL cp_results_mp_bcast(results(irep)%results, source, rep_env%para_env_inter_rep)
274 : END DO
275 4220 : CALL timestop(handle)
276 4220 : END SUBROUTINE rep_env_sync_results
277 :
278 : ! **************************************************************************************************
279 : !> \brief returns the replica environment with the given id_nr
280 : !> \param id_nr the id_nr of the requested rep_envs
281 : !> \param ierr ...
282 : !> \return ...
283 : !> \author fawzi
284 : ! **************************************************************************************************
285 4694 : FUNCTION rep_envs_get_rep_env(id_nr, ierr) RESULT(res)
286 : INTEGER, INTENT(in) :: id_nr
287 : INTEGER, INTENT(OUT) :: ierr
288 : TYPE(replica_env_type), POINTER :: res
289 :
290 : INTEGER :: i
291 :
292 4694 : NULLIFY (res)
293 4694 : ierr = -1
294 4694 : IF (module_initialized) THEN
295 4664 : IF (ASSOCIATED(rep_envs)) THEN
296 4536 : DO i = 1, SIZE(rep_envs)
297 4536 : IF (rep_envs(i)%rep_env%id_nr == id_nr) THEN
298 4536 : res => rep_envs(i)%rep_env
299 4536 : ierr = 0
300 4536 : EXIT
301 : END IF
302 : END DO
303 : END IF
304 : END IF
305 4694 : END FUNCTION rep_envs_get_rep_env
306 :
307 : ! **************************************************************************************************
308 : !> \brief adds the given rep_env to the list of controlled rep_envs.
309 : !> \param rep_env the rep_env to add
310 : !> \author fawzi
311 : ! **************************************************************************************************
312 158 : SUBROUTINE rep_envs_add_rep_env(rep_env)
313 : TYPE(replica_env_type), POINTER :: rep_env
314 :
315 : INTEGER :: i, stat
316 158 : TYPE(replica_env_p_type), DIMENSION(:), POINTER :: new_rep_envs
317 : TYPE(replica_env_type), POINTER :: rep_env2
318 :
319 158 : IF (ASSOCIATED(rep_env)) THEN
320 158 : rep_env2 => rep_envs_get_rep_env(rep_env%id_nr, ierr=stat)
321 158 : IF (.NOT. ASSOCIATED(rep_env2)) THEN
322 158 : IF (module_initialized) THEN
323 128 : IF (.NOT. ASSOCIATED(rep_envs)) THEN
324 256 : ALLOCATE (rep_envs(1))
325 : ELSE
326 0 : ALLOCATE (new_rep_envs(SIZE(rep_envs) + 1))
327 0 : DO i = 1, SIZE(rep_envs)
328 0 : new_rep_envs(i)%rep_env => rep_envs(i)%rep_env
329 : END DO
330 0 : DEALLOCATE (rep_envs)
331 0 : rep_envs => new_rep_envs
332 : END IF
333 : ELSE
334 60 : ALLOCATE (rep_envs(1))
335 : END IF
336 158 : rep_envs(SIZE(rep_envs))%rep_env => rep_env
337 158 : module_initialized = .TRUE.
338 : END IF
339 : END IF
340 158 : END SUBROUTINE rep_envs_add_rep_env
341 :
342 : ! **************************************************************************************************
343 : !> \brief removes the given rep_env to the list of controlled rep_envs.
344 : !> \param rep_env the rep_env to remove
345 : !> \author fawzi
346 : ! **************************************************************************************************
347 158 : SUBROUTINE rep_envs_rm_rep_env(rep_env)
348 : TYPE(replica_env_type), POINTER :: rep_env
349 :
350 : INTEGER :: i, ii
351 158 : TYPE(replica_env_p_type), DIMENSION(:), POINTER :: new_rep_envs
352 :
353 158 : IF (ASSOCIATED(rep_env)) THEN
354 158 : CPASSERT(module_initialized)
355 316 : ALLOCATE (new_rep_envs(SIZE(rep_envs) - 1))
356 : ii = 0
357 316 : DO i = 1, SIZE(rep_envs)
358 316 : IF (rep_envs(i)%rep_env%id_nr /= rep_env%id_nr) THEN
359 0 : ii = ii + 1
360 0 : new_rep_envs(ii)%rep_env => rep_envs(i)%rep_env
361 : END IF
362 : END DO
363 158 : CPASSERT(ii == SIZE(new_rep_envs))
364 158 : DEALLOCATE (rep_envs)
365 158 : rep_envs => new_rep_envs
366 158 : IF (SIZE(rep_envs) == 0) THEN
367 158 : DEALLOCATE (rep_envs)
368 : END IF
369 : END IF
370 158 : END SUBROUTINE rep_envs_rm_rep_env
371 :
372 0 : END MODULE replica_types
|