(git:3f0d10b)
Loading...
Searching...
No Matches
cp_fm_pool_types.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! **************************************************************************************************
9!> \brief pool for for elements that are retained and released
10!> \par History
11!> 08.2002 created [fawzi]
12!> \author Fawzi Mohamed
13! **************************************************************************************************
18 USE cp_fm_types, ONLY: cp_fm_create, &
29#include "../base/base_uses.f90"
30
31 IMPLICIT NONE
32 PRIVATE
33
34 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
35 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_fm_pool_types'
36
38 PUBLIC :: fm_pool_create, &
42 PUBLIC :: fm_pools_dealloc, &
45!***
46
47! **************************************************************************************************
48!> \brief represent a pool of elements with the same structure
49!> \param el_struct the structure of the elements stored in this pool
50!> \param cache linked list with the elements in the pool
51!> \par History
52!> 08.2002 created [fawzi]
53!> \author Fawzi Mohamed
54! **************************************************************************************************
56 PRIVATE
57 TYPE(cp_fm_struct_type), POINTER :: el_struct => null()
58 TYPE(cp_sll_fm_type), POINTER :: cache => null()
59 END TYPE cp_fm_pool_type
60
61! **************************************************************************************************
62!> \brief to create arrays of pools
63!> \param pool the pool
64!> \par History
65!> 08.2002 created [fawzi]
66!> \author Fawzi Mohamed
67! **************************************************************************************************
69 TYPE(cp_fm_pool_type), POINTER :: pool => null()
70 END TYPE cp_fm_pool_p_type
71
73 MODULE PROCEDURE fm_pools_create_fm_m1_p_type_pointer
74 MODULE PROCEDURE fm_pools_create_fm_m1_p_type_alloc
75 MODULE PROCEDURE fm_pools_create_fm_m1_array_pointer
76 MODULE PROCEDURE fm_pools_create_fm_m1_array_alloc
77 END INTERFACE
78
80 MODULE PROCEDURE fm_pools_give_back_fm_m1_p_type_pointer
81 MODULE PROCEDURE fm_pools_give_back_fm_m1_p_type_alloc
82 MODULE PROCEDURE fm_pools_give_back_fm_m1_array_pointer
83 MODULE PROCEDURE fm_pools_give_back_fm_m1_array_alloc
84 END INTERFACE
85
86CONTAINS
87
88! **************************************************************************************************
89!> \brief creates a pool of elements
90!> \param pool the pool to create
91!> \param el_struct the structure of the elements that are stored in
92!> this pool
93!> \par History
94!> 08.2002 created [fawzi]
95!> \author Fawzi Mohamed
96! **************************************************************************************************
97 SUBROUTINE fm_pool_create(pool, el_struct)
98 TYPE(cp_fm_pool_type), POINTER :: pool
99 TYPE(cp_fm_struct_type), TARGET :: el_struct
100
101 ALLOCATE (pool)
102 pool%el_struct => el_struct
103 CALL cp_fm_struct_retain(pool%el_struct)
104
105 END SUBROUTINE fm_pool_create
106
107! **************************************************************************************************
108!> \brief deallocates all the cached elements
109!> \param pool the pool to flush
110!> \par History
111!> 08.2002 created [fawzi]
112!> \author Fawzi Mohamed
113! **************************************************************************************************
114 SUBROUTINE fm_pool_flush_cache(pool)
115 TYPE(cp_fm_pool_type), INTENT(IN) :: pool
116
117 TYPE(cp_fm_type), POINTER :: el_att
118 TYPE(cp_sll_fm_type), POINTER :: iterator
119
120 iterator => pool%cache
121 DO
122 IF (.NOT. cp_sll_fm_next(iterator, el_att=el_att)) EXIT
123 CALL cp_fm_release(el_att)
124 DEALLOCATE (el_att)
125 NULLIFY (el_att)
126 END DO
127 CALL cp_sll_fm_dealloc(pool%cache)
128 END SUBROUTINE fm_pool_flush_cache
129
130! **************************************************************************************************
131!> \brief releases the given pool (see cp2k/doc/ReferenceCounting.html)
132!> \param pool the pool to release
133!> \par History
134!> 08.2002 created [fawzi]
135!> \author Fawzi Mohamed
136! **************************************************************************************************
137 SUBROUTINE fm_pool_release(pool)
138 TYPE(cp_fm_pool_type), POINTER :: pool
139
140 IF (ASSOCIATED(pool)) THEN
141 CALL fm_pool_flush_cache(pool)
142 CALL cp_fm_struct_release(pool%el_struct)
143 DEALLOCATE (pool)
144 END IF
145 NULLIFY (pool)
146 END SUBROUTINE fm_pool_release
147
148! **************************************************************************************************
149!> \brief returns an element, allocating it if none is in the pool
150!> \param pool the pool from where you get the element
151!> \param element will contain the new element
152!>\param name the name for the new matrix (optional)
153!> \param name ...
154!> \par History
155!> 08.2002 created [fawzi]
156!> \author Fawzi Mohamed
157! **************************************************************************************************
158 SUBROUTINE fm_pool_create_fm(pool, element, &
159 name)
160 TYPE(cp_fm_pool_type), INTENT(IN) :: pool
161 TYPE(cp_fm_type), INTENT(OUT) :: element
162 CHARACTER(len=*), INTENT(in), OPTIONAL :: name
163
164 TYPE(cp_fm_type), POINTER :: el
165
166 NULLIFY (el)
167 IF (ASSOCIATED(pool%cache)) THEN
168 el => cp_sll_fm_get_first_el(pool%cache)
169 CALL cp_sll_fm_rm_first_el(pool%cache)
170 END IF
171 IF (ASSOCIATED(el)) THEN
172 element = el
173 DEALLOCATE (el)
174 ELSE
175 CALL cp_fm_create(element, matrix_struct=pool%el_struct, set_zero=.true.)
176 END IF
177
178 IF (PRESENT(name)) THEN
179 element%name = name
180 ELSE
181 element%name = "tmp"
182 END IF
183
184 END SUBROUTINE fm_pool_create_fm
185
186! **************************************************************************************************
187!> \brief returns the element to the pool
188!> \param pool the pool where to cache the element
189!> \param element the element to give back
190!> \par History
191!> 08.2002 created [fawzi]
192!> \author Fawzi Mohamed
193!> \note
194!> transfers the ownership of the element to the pool
195!> (it is as if you had called cp_fm_release)
196!> Accept give_backs of non associated elements?
197! **************************************************************************************************
198 SUBROUTINE fm_pool_give_back_fm(pool, element)
199 TYPE(cp_fm_pool_type), INTENT(IN) :: pool
200 TYPE(cp_fm_type), INTENT(INOUT) :: element
201
202 IF (.NOT. ASSOCIATED(pool%el_struct, element%matrix_struct)) THEN
203 CALL cp_fm_release(element)
204 ELSE
205 block
206 TYPE(cp_fm_type), POINTER :: el
207 ALLOCATE (el)
208 el = element
209 CALL cp_sll_fm_insert_el(pool%cache, el=el)
210 NULLIFY (element%matrix_struct, element%local_data)
211 END block
212 END IF
213 END SUBROUTINE fm_pool_give_back_fm
214
215! **************************************************************************************************
216!> \brief returns the structure of the elements in this pool
217!> \param pool the pool you are interested in
218!> \return ...
219!> \par History
220!> 05.2002 created [fawzi]
221!> \author Fawzi Mohamed
222! **************************************************************************************************
223 FUNCTION fm_pool_get_el_struct(pool) RESULT(res)
224 TYPE(cp_fm_pool_type), INTENT(IN) :: pool
225 TYPE(cp_fm_struct_type), POINTER :: res
226
227 res => pool%el_struct
228 END FUNCTION fm_pool_get_el_struct
229
230!================== pools ================
231
232! **************************************************************************************************
233!> \brief deallocate an array of pools (releasing each pool)
234!> \param pools the pools to release
235!> \par History
236!> 11.2002 created [fawzi]
237!> \author Fawzi Mohamed
238! **************************************************************************************************
239 SUBROUTINE fm_pools_dealloc(pools)
240 TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER :: pools
241
242 INTEGER :: i
243
244 IF (ASSOCIATED(pools)) THEN
245 DO i = 1, SIZE(pools)
246 CALL fm_pool_release(pools(i)%pool)
247 END DO
248 DEALLOCATE (pools)
249 END IF
250 END SUBROUTINE fm_pools_dealloc
251
252
253! **************************************************************************************************
254!> \brief Returns a vector with an element from each pool
255!> \param pools the pools to create the elements from
256!> \param elements will contain the vector of elements
257!> \param name the name for the new matrixes (optional)
258!> \par History
259!> 09.2002 created [fawzi]
260!> \author Fawzi Mohamed
261! **************************************************************************************************
262 SUBROUTINE fm_pools_create_fm_m1_array_alloc (pools, elements, &
263 name)
264 TYPE(cp_fm_pool_p_type), DIMENSION(:), INTENT(IN) :: pools
265 TYPE(cp_fm_type), DIMENSION(:), ALLOCATABLE :: elements
266 CHARACTER(len=*), INTENT(in), OPTIONAL :: name
267
268 INTEGER :: i
269 TYPE(cp_fm_pool_type), POINTER :: pool
270
271 NULLIFY (pool)
272
273 ALLOCATE (elements(SIZE(pools)))
274 DO i = 1, SIZE(pools)
275 pool => pools(i)%pool
276 IF (PRESENT(name)) THEN
277 CALL fm_pool_create_fm(pool, elements(i) , &
278 name=name//"-"//adjustl(cp_to_string(i)))
279 ELSE
280 CALL fm_pool_create_fm(pool, elements(i) )
281 END IF
282
283 END DO
284
285 END SUBROUTINE fm_pools_create_fm_m1_array_alloc
286
287! **************************************************************************************************
288!> \brief returns a vector to the pools. The vector is deallocated
289!> (like cp_fm_vect_dealloc)
290!> \param pools the pool where to give back the vector
291!> \param elements the vector of elements to give back
292!> \par History
293!> 09.2002 created [fawzi]
294!> \author Fawzi Mohamed
295!> \note
296!> accept unassociated vect?
297! **************************************************************************************************
298 SUBROUTINE fm_pools_give_back_fm_m1_array_alloc (pools, elements)
299 TYPE(cp_fm_pool_p_type), DIMENSION(:), INTENT(IN) :: pools
300 TYPE(cp_fm_type), DIMENSION(:), ALLOCATABLE :: elements
301
302 INTEGER :: i
303
304 IF (ALLOCATED (elements)) THEN
305 cpassert(SIZE(pools) == SIZE(elements))
306 DO i = 1, SIZE(pools)
307 CALL fm_pool_give_back_fm(pools(i)%pool, &
308 elements(i) )
309 END DO
310 DEALLOCATE (elements)
311 END IF
312 END SUBROUTINE fm_pools_give_back_fm_m1_array_alloc
313! **************************************************************************************************
314!> \brief Returns a vector with an element from each pool
315!> \param pools the pools to create the elements from
316!> \param elements will contain the vector of elements
317!> \param name the name for the new matrixes (optional)
318!> \par History
319!> 09.2002 created [fawzi]
320!> \author Fawzi Mohamed
321! **************************************************************************************************
322 SUBROUTINE fm_pools_create_fm_m1_array_pointer (pools, elements, &
323 name)
324 TYPE(cp_fm_pool_p_type), DIMENSION(:), INTENT(IN) :: pools
325 TYPE(cp_fm_type), DIMENSION(:), POINTER :: elements
326 CHARACTER(len=*), INTENT(in), OPTIONAL :: name
327
328 INTEGER :: i
329 TYPE(cp_fm_pool_type), POINTER :: pool
330
331 NULLIFY (pool)
332
333 ALLOCATE (elements(SIZE(pools)))
334 DO i = 1, SIZE(pools)
335 pool => pools(i)%pool
336 IF (PRESENT(name)) THEN
337 CALL fm_pool_create_fm(pool, elements(i) , &
338 name=name//"-"//adjustl(cp_to_string(i)))
339 ELSE
340 CALL fm_pool_create_fm(pool, elements(i) )
341 END IF
342
343 END DO
344
345 END SUBROUTINE fm_pools_create_fm_m1_array_pointer
346
347! **************************************************************************************************
348!> \brief returns a vector to the pools. The vector is deallocated
349!> (like cp_fm_vect_dealloc)
350!> \param pools the pool where to give back the vector
351!> \param elements the vector of elements to give back
352!> \par History
353!> 09.2002 created [fawzi]
354!> \author Fawzi Mohamed
355!> \note
356!> accept unassociated vect?
357! **************************************************************************************************
358 SUBROUTINE fm_pools_give_back_fm_m1_array_pointer (pools, elements)
359 TYPE(cp_fm_pool_p_type), DIMENSION(:), INTENT(IN) :: pools
360 TYPE(cp_fm_type), DIMENSION(:), POINTER :: elements
361
362 INTEGER :: i
363
364 IF (ASSOCIATED (elements)) THEN
365 cpassert(SIZE(pools) == SIZE(elements))
366 DO i = 1, SIZE(pools)
367 CALL fm_pool_give_back_fm(pools(i)%pool, &
368 elements(i) )
369 END DO
370 DEALLOCATE (elements)
371 NULLIFY (elements)
372 END IF
373 END SUBROUTINE fm_pools_give_back_fm_m1_array_pointer
374! **************************************************************************************************
375!> \brief Returns a vector with an element from each pool
376!> \param pools the pools to create the elements from
377!> \param elements will contain the vector of elements
378!> \param name the name for the new matrixes (optional)
379!> \par History
380!> 09.2002 created [fawzi]
381!> \author Fawzi Mohamed
382! **************************************************************************************************
383 SUBROUTINE fm_pools_create_fm_m1_p_type_alloc (pools, elements, &
384 name)
385 TYPE(cp_fm_pool_p_type), DIMENSION(:), INTENT(IN) :: pools
386 TYPE(cp_fm_p_type), DIMENSION(:), ALLOCATABLE :: elements
387 CHARACTER(len=*), INTENT(in), OPTIONAL :: name
388
389 INTEGER :: i
390 TYPE(cp_fm_pool_type), POINTER :: pool
391
392 NULLIFY (pool)
393
394 ALLOCATE (elements(SIZE(pools)))
395 DO i = 1, SIZE(pools)
396 pool => pools(i)%pool
397 ALLOCATE (elements(i)%matrix)
398 IF (PRESENT(name)) THEN
399 CALL fm_pool_create_fm(pool, elements(i) %matrix, &
400 name=name//"-"//adjustl(cp_to_string(i)))
401 ELSE
402 CALL fm_pool_create_fm(pool, elements(i) %matrix)
403 END IF
404
405 END DO
406
407 END SUBROUTINE fm_pools_create_fm_m1_p_type_alloc
408
409! **************************************************************************************************
410!> \brief returns a vector to the pools. The vector is deallocated
411!> (like cp_fm_vect_dealloc)
412!> \param pools the pool where to give back the vector
413!> \param elements the vector of elements to give back
414!> \par History
415!> 09.2002 created [fawzi]
416!> \author Fawzi Mohamed
417!> \note
418!> accept unassociated vect?
419! **************************************************************************************************
420 SUBROUTINE fm_pools_give_back_fm_m1_p_type_alloc (pools, elements)
421 TYPE(cp_fm_pool_p_type), DIMENSION(:), INTENT(IN) :: pools
422 TYPE(cp_fm_p_type), DIMENSION(:), ALLOCATABLE :: elements
423
424 INTEGER :: i
425
426 IF (ALLOCATED (elements)) THEN
427 cpassert(SIZE(pools) == SIZE(elements))
428 DO i = 1, SIZE(pools)
429 CALL fm_pool_give_back_fm(pools(i)%pool, &
430 elements(i) %matrix)
431 DEALLOCATE (elements(i)%matrix)
432 END DO
433 DEALLOCATE (elements)
434 END IF
435 END SUBROUTINE fm_pools_give_back_fm_m1_p_type_alloc
436! **************************************************************************************************
437!> \brief Returns a vector with an element from each pool
438!> \param pools the pools to create the elements from
439!> \param elements will contain the vector of elements
440!> \param name the name for the new matrixes (optional)
441!> \par History
442!> 09.2002 created [fawzi]
443!> \author Fawzi Mohamed
444! **************************************************************************************************
445 SUBROUTINE fm_pools_create_fm_m1_p_type_pointer (pools, elements, &
446 name)
447 TYPE(cp_fm_pool_p_type), DIMENSION(:), INTENT(IN) :: pools
448 TYPE(cp_fm_p_type), DIMENSION(:), POINTER :: elements
449 CHARACTER(len=*), INTENT(in), OPTIONAL :: name
450
451 INTEGER :: i
452 TYPE(cp_fm_pool_type), POINTER :: pool
453
454 NULLIFY (pool)
455
456 ALLOCATE (elements(SIZE(pools)))
457 DO i = 1, SIZE(pools)
458 pool => pools(i)%pool
459 ALLOCATE (elements(i)%matrix)
460 IF (PRESENT(name)) THEN
461 CALL fm_pool_create_fm(pool, elements(i) %matrix, &
462 name=name//"-"//adjustl(cp_to_string(i)))
463 ELSE
464 CALL fm_pool_create_fm(pool, elements(i) %matrix)
465 END IF
466
467 END DO
468
469 END SUBROUTINE fm_pools_create_fm_m1_p_type_pointer
470
471! **************************************************************************************************
472!> \brief returns a vector to the pools. The vector is deallocated
473!> (like cp_fm_vect_dealloc)
474!> \param pools the pool where to give back the vector
475!> \param elements the vector of elements to give back
476!> \par History
477!> 09.2002 created [fawzi]
478!> \author Fawzi Mohamed
479!> \note
480!> accept unassociated vect?
481! **************************************************************************************************
482 SUBROUTINE fm_pools_give_back_fm_m1_p_type_pointer (pools, elements)
483 TYPE(cp_fm_pool_p_type), DIMENSION(:), INTENT(IN) :: pools
484 TYPE(cp_fm_p_type), DIMENSION(:), POINTER :: elements
485
486 INTEGER :: i
487
488 IF (ASSOCIATED (elements)) THEN
489 cpassert(SIZE(pools) == SIZE(elements))
490 DO i = 1, SIZE(pools)
491 CALL fm_pool_give_back_fm(pools(i)%pool, &
492 elements(i) %matrix)
493 DEALLOCATE (elements(i)%matrix)
494 END DO
495 DEALLOCATE (elements)
496 NULLIFY (elements)
497 END IF
498 END SUBROUTINE fm_pools_give_back_fm_m1_p_type_pointer
499
500END MODULE cp_fm_pool_types
pool for for elements that are retained and released
subroutine, public fm_pool_create_fm(pool, element, name)
returns an element, allocating it if none is in the pool
subroutine, public fm_pool_create(pool, el_struct)
creates a pool of elements
subroutine, public fm_pool_release(pool)
releases the given pool (see cp2k/doc/ReferenceCounting.html)
subroutine, public fm_pool_give_back_fm(pool, element)
returns the element to the pool
subroutine, public fm_pools_dealloc(pools)
deallocate an array of pools (releasing each pool)
type(cp_fm_struct_type) function, pointer, public fm_pool_get_el_struct(pool)
returns the structure of the elements in this pool
represent the structure of a full matrix
subroutine, public cp_fm_struct_retain(fmstruct)
retains a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
subroutine, public cp_sll_fm_insert_el(sll, el)
insert an element at the beginning of the list
logical function, public cp_sll_fm_next(iterator, el_att)
returns true if the actual element is valid (i.e. iterator ont at end) moves the iterator to the next...
type(cp_fm_type) function, pointer, public cp_sll_fm_get_first_el(sll)
returns the first element stored in the list
subroutine, public cp_sll_fm_dealloc(sll)
deallocates the singly linked list starting at sll. Does not work if loops are present!
subroutine, public cp_sll_fm_rm_first_el(sll)
remove the first element of the linked list
various routines to log and control the output. The idea is that decisions about where to log should ...
represent a pool of elements with the same structure
keeps the information about the structure of a full matrix
just to build arrays of pointers to matrices
represent a full matrix
represent a single linked list that stores pointers to the elements