(git:8ddd909)
Loading...
Searching...
No Matches
cp_fm_struct.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 represent the structure of a full matrix
10!> \par History
11!> 08.2002 created [fawzi]
12!> \author Fawzi Mohamed
13! **************************************************************************************************
21 USE machine, ONLY: m_cpuid_vlen,&
25#include "../base/base_uses.f90"
26
27 IMPLICIT NONE
28 PRIVATE
29
30 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
31 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_fm_struct'
32
33! the default blacs block sizes
34! consider using #ifdefs to give them the optimal values
35! these can be changed using scf_control
36! *** these are used by default
37 INTEGER, PRIVATE :: optimal_blacs_col_block_size = 64
38 INTEGER, PRIVATE :: optimal_blacs_row_block_size = 64
39 LOGICAL, PRIVATE :: force_block_size = .false.
40
47
48! **************************************************************************************************
49!> \brief keeps the information about the structure of a full matrix
50!> \param para_env the parallel environment of the matrices with this structure
51!> \param context the blacs context (parallel environment for scalapack),
52!> should be compatible with para_env
53!> \param descriptor the scalapack descriptor of the matrices, when using
54!> scalapack (ncol_block=descriptor(6), ncol_global=descriptor(4),
55!> nrow_block=descriptor(5), nrow_global=descriptor(3))
56!> \param ncol_block number of columns of a scalapack block
57!> \param nrow_block number of rows of a scalapack block
58!> \param nrow_global number of rows of the matrix
59!> \param ncol_global number of rows
60!> \param first_p_pos position of the first processor (for scalapack)
61!> \param row_indices real (global) indices of the rows (defined only for
62!> the local rows really used)
63!> \param col_indices real (global) indices of the cols (defined only for
64!> the local cols really used)
65!> \param nrow_locals nrow_locals(i) number of local rows of the matrix really
66!> used on the processors with context%mepos(1)==i
67!> \param ncol_locals ncol_locals(i) number of local rows of the matrix really
68!> used on the processors with context%mepos(2)==i
69!> \param ref_count reference count (see doc/ReferenceCounting.html)
70!> \param local_leading_dimension leading dimension of the data that is
71!> stored on this processor
72!>
73!> readonly attributes:
74!> \param nrow_local number of local rows really used on the actual processor
75!> \param ncol_local number of local cols really used on the actual processor
76!> \note
77!> use cp_fm_struct_get to extract information from this structure
78!> \par History
79!> 08.2002 created [fawzi]
80!> \author Fawzi Mohamed
81! **************************************************************************************************
83 TYPE(mp_para_env_type), POINTER :: para_env => null()
84 TYPE(cp_blacs_env_type), POINTER :: context => null()
85 INTEGER, DIMENSION(9) :: descriptor = -1
86 INTEGER :: nrow_block = -1, ncol_block = -1, nrow_global = -1, ncol_global = -1
87 INTEGER, DIMENSION(2) :: first_p_pos = -1
88 INTEGER, DIMENSION(:), POINTER :: row_indices => null(), col_indices => null(), &
89 nrow_locals => null(), ncol_locals => null()
90 INTEGER :: ref_count = -1, local_leading_dimension = -1
91 CONTAINS
92 PROCEDURE, pass(struct), non_overridable :: g2p_row => cp_fm_indxg2p_row
93 PROCEDURE, pass(struct), non_overridable :: g2p_col => cp_fm_indxg2p_col
94 PROCEDURE, pass(struct), non_overridable :: g2l_row => cp_fm_indxg2l_row
95 PROCEDURE, pass(struct), non_overridable :: g2l_col => cp_fm_indxg2l_col
96 PROCEDURE, pass(struct), non_overridable :: l2g_row => cp_fm_indxl2g_row
97 PROCEDURE, pass(struct), non_overridable :: l2g_col => cp_fm_indxl2g_col
98 END TYPE cp_fm_struct_type
99! **************************************************************************************************
101 TYPE(cp_fm_struct_type), POINTER :: struct => null()
102 END TYPE cp_fm_struct_p_type
103
104CONTAINS
105
106! **************************************************************************************************
107!> \brief allocates and initializes a full matrix structure
108!> \param fmstruct the pointer that will point to the new structure
109!> \param para_env the parallel environment
110!> \param context the blacs context of this matrix
111!> \param nrow_global the number of row of the full matrix
112!> \param ncol_global the number of columns of the full matrix
113!> \param nrow_block the number of rows of a block of the matrix,
114!> omit or set to -1 to use the built-in defaults
115!> \param ncol_block the number of columns of a block of the matrix,
116!> omit or set to -1 to use the built-in defaults
117!> \param descriptor the scalapack descriptor of the matrix (if not given
118!> a new one is allocated
119!> \param first_p_pos ...
120!> \param local_leading_dimension the leading dimension of the locally stored
121!> data block
122!> \param template_fmstruct a matrix structure where to take the default values
123!> \param square_blocks ...
124!> \param force_block ...
125!> \par History
126!> 08.2002 created [fawzi]
127!> \author Fawzi Mohamed
128! **************************************************************************************************
129 SUBROUTINE cp_fm_struct_create(fmstruct, para_env, context, nrow_global, &
130 ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, &
131 local_leading_dimension, template_fmstruct, square_blocks, force_block)
132
133 TYPE(cp_fm_struct_type), POINTER :: fmstruct
134 TYPE(mp_para_env_type), TARGET, OPTIONAL :: para_env
135 INTEGER, INTENT(in), OPTIONAL :: nrow_global, ncol_global
136 INTEGER, INTENT(in), OPTIONAL :: nrow_block, ncol_block
137 INTEGER, INTENT(in), OPTIONAL :: local_leading_dimension
138 TYPE(cp_blacs_env_type), TARGET, OPTIONAL :: context
139 INTEGER, DIMENSION(9), INTENT(in), OPTIONAL :: descriptor
140 INTEGER, OPTIONAL, DIMENSION(2) :: first_p_pos
141 TYPE(cp_fm_struct_type), TARGET, OPTIONAL :: template_fmstruct
142 LOGICAL, OPTIONAL, INTENT(in) :: square_blocks
143 LOGICAL, OPTIONAL, INTENT(in) :: force_block
144
145 INTEGER :: i, nmax_block, vlen
146#if defined(__parallel)
147 INTEGER :: iunit, stat
148 INTEGER, EXTERNAL :: numroc
149 TYPE(cp_logger_type), POINTER :: logger
150#endif
151
152 LOGICAL :: my_square_blocks, my_force_block
153
154 ALLOCATE (fmstruct)
155
156 IF (.NOT. PRESENT(template_fmstruct)) THEN
157 cpassert(PRESENT(context))
158 cpassert(PRESENT(nrow_global))
159 cpassert(PRESENT(ncol_global))
160 fmstruct%local_leading_dimension = 1
161 fmstruct%nrow_block = 0 ! populate default later
162 fmstruct%ncol_block = 0 ! populate default later
163 ELSE
164 fmstruct%context => template_fmstruct%context
165 fmstruct%para_env => template_fmstruct%para_env
166 fmstruct%descriptor = template_fmstruct%descriptor
167 fmstruct%nrow_block = template_fmstruct%nrow_block
168 fmstruct%nrow_global = template_fmstruct%nrow_global
169 fmstruct%ncol_block = template_fmstruct%ncol_block
170 fmstruct%ncol_global = template_fmstruct%ncol_global
171 fmstruct%first_p_pos = template_fmstruct%first_p_pos
172 fmstruct%local_leading_dimension = &
173 template_fmstruct%local_leading_dimension
174 END IF
175
176 ! allow to request default block size (zero or negative value)
177 IF (PRESENT(nrow_block)) fmstruct%nrow_block = nrow_block
178 IF (PRESENT(ncol_block)) fmstruct%ncol_block = ncol_block
179 IF (0 >= fmstruct%nrow_block) THEN
180 fmstruct%nrow_block = optimal_blacs_row_block_size
181 END IF
182 IF (0 >= fmstruct%ncol_block) THEN
183 fmstruct%ncol_block = optimal_blacs_col_block_size
184 END IF
185 cpassert(0 < fmstruct%nrow_block .AND. 0 < fmstruct%ncol_block)
186
187 IF (PRESENT(context)) THEN
188 fmstruct%context => context
189 fmstruct%para_env => context%para_env
190 END IF
191 IF (PRESENT(para_env)) fmstruct%para_env => para_env
192 CALL fmstruct%context%retain()
193 CALL fmstruct%para_env%retain()
194
195 IF (PRESENT(nrow_global)) THEN
196 fmstruct%nrow_global = nrow_global
197 fmstruct%local_leading_dimension = 1
198 END IF
199 IF (PRESENT(ncol_global)) THEN
200 fmstruct%ncol_global = ncol_global
201 END IF
202
203 my_force_block = force_block_size
204 IF (PRESENT(force_block)) my_force_block = force_block
205 IF (.NOT. my_force_block) THEN
206 vlen = m_cpuid_vlen()
207 nmax_block = (fmstruct%nrow_global + fmstruct%context%num_pe(1) - 1)/ &
208 (fmstruct%context%num_pe(1))
209 IF (1 < vlen) THEN ! flooring not ceiling (OOB)
210 fmstruct%nrow_block = fmstruct%nrow_block/vlen*vlen
211 nmax_block = nmax_block/vlen*vlen
212 END IF
213 fmstruct%nrow_block = max(min(fmstruct%nrow_block, nmax_block), 1)
214
215 nmax_block = (fmstruct%ncol_global + fmstruct%context%num_pe(2) - 1)/ &
216 (fmstruct%context%num_pe(2))
217 IF (1 < vlen) THEN ! flooring not ceiling (OOB)
218 fmstruct%ncol_block = fmstruct%ncol_block/vlen*vlen
219 nmax_block = nmax_block/vlen*vlen
220 END IF
221 fmstruct%ncol_block = max(min(fmstruct%ncol_block, nmax_block), 1)
222 END IF
223
224 ! square matrix -> square blocks (otherwise, e.g., PDPOTRF fails)
225 my_square_blocks = fmstruct%nrow_global == fmstruct%ncol_global
226 ! however, requesting non-square blocks takes precedence
227 IF (PRESENT(square_blocks)) my_square_blocks = square_blocks
228 IF (my_square_blocks) THEN
229 fmstruct%nrow_block = min(fmstruct%nrow_block, fmstruct%ncol_block)
230 fmstruct%ncol_block = fmstruct%nrow_block
231 END IF
232
233 ALLOCATE (fmstruct%nrow_locals(0:(fmstruct%context%num_pe(1) - 1)), &
234 fmstruct%ncol_locals(0:(fmstruct%context%num_pe(2) - 1)))
235 IF (.NOT. PRESENT(template_fmstruct)) THEN
236 fmstruct%first_p_pos = [0, 0]
237 END IF
238 IF (PRESENT(first_p_pos)) fmstruct%first_p_pos = first_p_pos
239
240 fmstruct%nrow_locals = 0
241 fmstruct%ncol_locals = 0
242#if defined(__parallel)
243 fmstruct%nrow_locals(fmstruct%context%mepos(1)) = &
244 numroc(fmstruct%nrow_global, fmstruct%nrow_block, &
245 fmstruct%context%mepos(1), fmstruct%first_p_pos(1), &
246 fmstruct%context%num_pe(1))
247 fmstruct%ncol_locals(fmstruct%context%mepos(2)) = &
248 numroc(fmstruct%ncol_global, fmstruct%ncol_block, &
249 fmstruct%context%mepos(2), fmstruct%first_p_pos(2), &
250 fmstruct%context%num_pe(2))
251 CALL fmstruct%para_env%sum(fmstruct%nrow_locals)
252 CALL fmstruct%para_env%sum(fmstruct%ncol_locals)
253 fmstruct%nrow_locals(:) = fmstruct%nrow_locals(:)/fmstruct%context%num_pe(2)
254 fmstruct%ncol_locals(:) = fmstruct%ncol_locals(:)/fmstruct%context%num_pe(1)
255
256 IF (sum(fmstruct%ncol_locals) /= fmstruct%ncol_global .OR. &
257 sum(fmstruct%nrow_locals) /= fmstruct%nrow_global) THEN
258 ! try to collect some output if this is going to happen again
259 ! this seems to trigger on blanc, but should really never happen
260 logger => cp_get_default_logger()
261 iunit = cp_logger_get_default_unit_nr(logger, local=.true.)
262 WRITE (iunit, *) "mepos", fmstruct%context%mepos(1:2), "numpe", fmstruct%context%num_pe(1:2)
263 WRITE (iunit, *) "ncol_global", fmstruct%ncol_global
264 WRITE (iunit, *) "nrow_global", fmstruct%nrow_global
265 WRITE (iunit, *) "ncol_locals", fmstruct%ncol_locals
266 WRITE (iunit, *) "nrow_locals", fmstruct%nrow_locals
267 CALL m_flush(iunit)
268 END IF
269
270 IF (sum(fmstruct%ncol_locals) /= fmstruct%ncol_global) THEN
271 cpabort("sum of local cols not equal global cols")
272 END IF
273 IF (sum(fmstruct%nrow_locals) /= fmstruct%nrow_global) THEN
274 cpabort("sum of local row not equal global rows")
275 END IF
276#else
277 ! block = full matrix
278 fmstruct%nrow_block = fmstruct%nrow_global
279 fmstruct%ncol_block = fmstruct%ncol_global
280 fmstruct%nrow_locals(fmstruct%context%mepos(1)) = fmstruct%nrow_global
281 fmstruct%ncol_locals(fmstruct%context%mepos(2)) = fmstruct%ncol_global
282#endif
283
284 fmstruct%local_leading_dimension = max(fmstruct%local_leading_dimension, &
285 fmstruct%nrow_locals(fmstruct%context%mepos(1)))
286 IF (PRESENT(local_leading_dimension)) THEN
287 IF (max(1, fmstruct%nrow_locals(fmstruct%context%mepos(1))) > local_leading_dimension) THEN
288 CALL cp_abort(__location__, "local_leading_dimension too small ("// &
289 cp_to_string(local_leading_dimension)//"<"// &
290 cp_to_string(fmstruct%local_leading_dimension)//")")
291 END IF
292 fmstruct%local_leading_dimension = local_leading_dimension
293 END IF
294
295 NULLIFY (fmstruct%row_indices, fmstruct%col_indices)
296
297 ! the max should go away
298 ALLOCATE (fmstruct%row_indices(max(fmstruct%nrow_locals(fmstruct%context%mepos(1)), 1)))
299 DO i = 1, SIZE(fmstruct%row_indices)
300#ifdef __parallel
301 fmstruct%row_indices(i) = fmstruct%l2g_row(i, fmstruct%context%mepos(1))
302#else
303 fmstruct%row_indices(i) = i
304#endif
305 END DO
306 ALLOCATE (fmstruct%col_indices(max(fmstruct%ncol_locals(fmstruct%context%mepos(2)), 1)))
307 DO i = 1, SIZE(fmstruct%col_indices)
308#ifdef __parallel
309 fmstruct%col_indices(i) = fmstruct%l2g_col(i, fmstruct%context%mepos(2))
310#else
311 fmstruct%col_indices(i) = i
312#endif
313 END DO
314
315 fmstruct%ref_count = 1
316
317 IF (PRESENT(descriptor)) THEN
318 fmstruct%descriptor = descriptor
319 ELSE
320 fmstruct%descriptor = 0
321#if defined(__parallel)
322 ! local leading dimension needs to be at least 1
323 CALL descinit(fmstruct%descriptor, fmstruct%nrow_global, &
324 fmstruct%ncol_global, fmstruct%nrow_block, &
325 fmstruct%ncol_block, fmstruct%first_p_pos(1), &
326 fmstruct%first_p_pos(2), fmstruct%context, &
327 fmstruct%local_leading_dimension, stat)
328 cpassert(stat == 0)
329#endif
330 END IF
331 END SUBROUTINE cp_fm_struct_create
332
333! **************************************************************************************************
334!> \brief retains a full matrix structure
335!> \param fmstruct the structure to retain
336!> \par History
337!> 08.2002 created [fawzi]
338!> \author Fawzi Mohamed
339! **************************************************************************************************
340 SUBROUTINE cp_fm_struct_retain(fmstruct)
341 TYPE(cp_fm_struct_type), INTENT(INOUT) :: fmstruct
342
343 cpassert(fmstruct%ref_count > 0)
344 fmstruct%ref_count = fmstruct%ref_count + 1
345 END SUBROUTINE cp_fm_struct_retain
346
347! **************************************************************************************************
348!> \brief releases a full matrix structure
349!> \param fmstruct the structure to release
350!> \par History
351!> 08.2002 created [fawzi]
352!> \author Fawzi Mohamed
353! **************************************************************************************************
354 SUBROUTINE cp_fm_struct_release(fmstruct)
355 TYPE(cp_fm_struct_type), POINTER :: fmstruct
356
357 IF (ASSOCIATED(fmstruct)) THEN
358 cpassert(fmstruct%ref_count > 0)
359 fmstruct%ref_count = fmstruct%ref_count - 1
360 IF (fmstruct%ref_count < 1) THEN
361 CALL cp_blacs_env_release(fmstruct%context)
362 CALL mp_para_env_release(fmstruct%para_env)
363 IF (ASSOCIATED(fmstruct%row_indices)) THEN
364 DEALLOCATE (fmstruct%row_indices)
365 END IF
366 IF (ASSOCIATED(fmstruct%col_indices)) THEN
367 DEALLOCATE (fmstruct%col_indices)
368 END IF
369 IF (ASSOCIATED(fmstruct%nrow_locals)) THEN
370 DEALLOCATE (fmstruct%nrow_locals)
371 END IF
372 IF (ASSOCIATED(fmstruct%ncol_locals)) THEN
373 DEALLOCATE (fmstruct%ncol_locals)
374 END IF
375 DEALLOCATE (fmstruct)
376 END IF
377 END IF
378 NULLIFY (fmstruct)
379 END SUBROUTINE cp_fm_struct_release
380
381! **************************************************************************************************
382!> \brief returns true if the two matrix structures are equivalent, false
383!> otherwise.
384!> \param fmstruct1 one of the full matrix structures to compare
385!> \param fmstruct2 the second of the full matrix structures to compare
386!> \return ...
387!> \par History
388!> 08.2002 created [fawzi]
389!> \author Fawzi Mohamed
390! **************************************************************************************************
391 FUNCTION cp_fm_struct_equivalent(fmstruct1, fmstruct2) RESULT(res)
392 TYPE(cp_fm_struct_type), POINTER :: fmstruct1, fmstruct2
393 LOGICAL :: res
394
395 INTEGER :: i
396
397 cpassert(ASSOCIATED(fmstruct1))
398 cpassert(ASSOCIATED(fmstruct2))
399 cpassert(fmstruct1%ref_count > 0)
400 cpassert(fmstruct2%ref_count > 0)
401 IF (ASSOCIATED(fmstruct1, fmstruct2)) THEN
402 res = .true.
403 ELSE
404 res = (fmstruct1%context == fmstruct2%context) .AND. &
405 (fmstruct1%nrow_global == fmstruct2%nrow_global) .AND. &
406 (fmstruct1%ncol_global == fmstruct2%ncol_global) .AND. &
407 (fmstruct1%nrow_block == fmstruct2%nrow_block) .AND. &
408 (fmstruct1%ncol_block == fmstruct2%ncol_block) .AND. &
409 (fmstruct1%local_leading_dimension == &
410 fmstruct2%local_leading_dimension)
411 DO i = 1, 9
412 res = res .AND. (fmstruct1%descriptor(i) == fmstruct1%descriptor(i))
413 END DO
414 END IF
415 END FUNCTION cp_fm_struct_equivalent
416
417! **************************************************************************************************
418!> \brief returns the values of various attributes of the matrix structure
419!> \param fmstruct the structure you want info about
420!> \param para_env ...
421!> \param context ...
422!> \param descriptor ...
423!> \param ncol_block ...
424!> \param nrow_block ...
425!> \param nrow_global ...
426!> \param ncol_global ...
427!> \param first_p_pos ...
428!> \param row_indices ...
429!> \param col_indices ...
430!> \param nrow_local ...
431!> \param ncol_local ...
432!> \param nrow_locals ...
433!> \param ncol_locals ...
434!> \param local_leading_dimension ...
435!> \par History
436!> 08.2002 created [fawzi]
437!> \author Fawzi Mohamed
438! **************************************************************************************************
439 SUBROUTINE cp_fm_struct_get(fmstruct, para_env, context, &
440 descriptor, ncol_block, nrow_block, nrow_global, &
441 ncol_global, first_p_pos, row_indices, &
442 col_indices, nrow_local, ncol_local, nrow_locals, ncol_locals, &
443 local_leading_dimension)
444 TYPE(cp_fm_struct_type), INTENT(IN) :: fmstruct
445 TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env
446 TYPE(cp_blacs_env_type), OPTIONAL, POINTER :: context
447 INTEGER, DIMENSION(9), INTENT(OUT), OPTIONAL :: descriptor
448 INTEGER, INTENT(out), OPTIONAL :: ncol_block, nrow_block, nrow_global, &
449 ncol_global
450 INTEGER, DIMENSION(2), INTENT(out), OPTIONAL :: first_p_pos
451 INTEGER, DIMENSION(:), OPTIONAL, POINTER :: row_indices, col_indices
452 INTEGER, INTENT(out), OPTIONAL :: nrow_local, ncol_local
453 INTEGER, DIMENSION(:), OPTIONAL, POINTER :: nrow_locals, ncol_locals
454 INTEGER, INTENT(out), OPTIONAL :: local_leading_dimension
455
456 IF (PRESENT(para_env)) para_env => fmstruct%para_env
457 IF (PRESENT(context)) context => fmstruct%context
458 IF (PRESENT(descriptor)) descriptor = fmstruct%descriptor
459 IF (PRESENT(ncol_block)) ncol_block = fmstruct%ncol_block
460 IF (PRESENT(nrow_block)) nrow_block = fmstruct%nrow_block
461 IF (PRESENT(nrow_global)) nrow_global = fmstruct%nrow_global
462 IF (PRESENT(ncol_global)) ncol_global = fmstruct%ncol_global
463 IF (PRESENT(first_p_pos)) first_p_pos = fmstruct%first_p_pos
464 IF (PRESENT(nrow_locals)) nrow_locals => fmstruct%nrow_locals
465 IF (PRESENT(ncol_locals)) ncol_locals => fmstruct%ncol_locals
466 IF (PRESENT(local_leading_dimension)) local_leading_dimension = &
467 fmstruct%local_leading_dimension
468
469 IF (PRESENT(nrow_local)) nrow_local = fmstruct%nrow_locals(fmstruct%context%mepos(1))
470 IF (PRESENT(ncol_local)) ncol_local = fmstruct%ncol_locals(fmstruct%context%mepos(2))
471
472 IF (PRESENT(row_indices)) row_indices => fmstruct%row_indices
473 IF (PRESENT(col_indices)) col_indices => fmstruct%col_indices
474 END SUBROUTINE cp_fm_struct_get
475
476! **************************************************************************************************
477!> \brief Write nicely formatted info about the FM struct to the given I/O unit
478!> \param fmstruct a cp_fm_struct_type instance
479!> \param io_unit the I/O unit to use for writing
480! **************************************************************************************************
481 SUBROUTINE cp_fm_struct_write_info(fmstruct, io_unit)
482 TYPE(cp_fm_struct_type), INTENT(IN) :: fmstruct
483 INTEGER, INTENT(IN) :: io_unit
484
485 INTEGER, PARAMETER :: oblock_size = 8
486
487 CHARACTER(len=30) :: fm
488 INTEGER :: oblock
489
490 WRITE (fm, "(A,I2,A)") "(A,I5,A,I5,A,", oblock_size, "I6)"
491
492 WRITE (io_unit, '(A,I12)') "CP_FM_STRUCT | No. of matrix columns: ", fmstruct%ncol_global
493 WRITE (io_unit, '(A,I12)') "CP_FM_STRUCT | No. of matrix rows: ", fmstruct%nrow_global
494 WRITE (io_unit, '(A,I12)') "CP_FM_STRUCT | No. of block columns: ", fmstruct%ncol_block
495 WRITE (io_unit, '(A,I12)') "CP_FM_STRUCT | No. of block rows: ", fmstruct%nrow_block
496
497 WRITE (io_unit, '(A)') "CP_FM_STRUCT | Number of local columns: "
498 DO oblock = 0, (SIZE(fmstruct%ncol_locals) - 1)/oblock_size
499 WRITE (io_unit, fm) "CP_FM_STRUCT | CPUs ", &
500 oblock*oblock_size, "..", (oblock + 1)*oblock_size - 1, ": ", &
501 fmstruct%ncol_locals(oblock*oblock_size:min(SIZE(fmstruct%ncol_locals), (oblock + 1)*oblock_size) - 1)
502 END DO
503
504 WRITE (io_unit, '(A)') "CP_FM_STRUCT | Number of local rows: "
505 DO oblock = 0, (SIZE(fmstruct%nrow_locals) - 1)/oblock_size
506 WRITE (io_unit, fm) "CP_FM_STRUCT | CPUs ", &
507 oblock*oblock_size, "..", (oblock + 1)*oblock_size - 1, ": ", &
508 fmstruct%nrow_locals(oblock*oblock_size:min(SIZE(fmstruct%nrow_locals), (oblock + 1)*oblock_size) - 1)
509 END DO
510 END SUBROUTINE cp_fm_struct_write_info
511
512! **************************************************************************************************
513!> \brief creates a struct with twice the number of blocks on each core.
514!> If matrix A has to be multiplied with B anc C, a
515!> significant speedup of pdgemm can be acchieved by joining the matrices
516!> in a new one with this structure (see arnoldi in rt_matrix_exp)
517!> \param fmstruct the struct to create
518!> \param struct struct of either A or B
519!> \param context ...
520!> \param col in which direction the matrix should be enlarged
521!> \param row in which direction the matrix should be enlarged
522!> \par History
523!> 06.2009 created [fschiff]
524!> \author Florian Schiffmann
525! **************************************************************************************************
526 SUBROUTINE cp_fm_struct_double(fmstruct, struct, context, col, row)
527 TYPE(cp_fm_struct_type), POINTER :: fmstruct
528 TYPE(cp_fm_struct_type), INTENT(INOUT) :: struct
529 TYPE(cp_blacs_env_type), INTENT(INOUT), TARGET :: context
530 LOGICAL, INTENT(in) :: col, row
531
532 INTEGER :: n_doubled_items_in_partially_filled_block, ncol_block, ncol_global, newdim_col, &
533 newdim_row, nfilled_blocks, nfilled_blocks_remain, nprocs_col, nprocs_row, nrow_block, &
534 nrow_global
535 TYPE(mp_para_env_type), POINTER :: para_env
536
537 CALL cp_fm_struct_get(struct, nrow_global=nrow_global, &
538 ncol_global=ncol_global, nrow_block=nrow_block, &
539 ncol_block=ncol_block)
540 newdim_row = nrow_global
541 newdim_col = ncol_global
542 nprocs_row = context%num_pe(1)
543 nprocs_col = context%num_pe(2)
544 para_env => struct%para_env
545
546 IF (col) THEN
547 IF (ncol_global == 0) THEN
548 newdim_col = 0
549 ELSE
550 ! ncol_block nfilled_blocks_remain * ncol_block
551 ! |<--->| |<--->|
552 ! |-----|-----|-----|-----|---|
553 ! | 0 | 1 | 2 | 0 | 1 | <- context%mepos(2)
554 ! |-----|-----|-----|-----|---|
555 ! |<--- nfilled_blocks -->|<-> -- items (columns) in partially filled blocks
556 ! | * ncol_block |
557 n_doubled_items_in_partially_filled_block = 2*mod(ncol_global, ncol_block)
558 nfilled_blocks = ncol_global/ncol_block
559 nfilled_blocks_remain = mod(nfilled_blocks, nprocs_col)
560 newdim_col = 2*(nfilled_blocks/nprocs_col)
561 IF (n_doubled_items_in_partially_filled_block > ncol_block) THEN
562 ! doubled number of columns in a partially filled block does not fit into a single block.
563 ! Due to cyclic distribution of ScaLAPACK blocks, an extra block for each core needs to be added
564 ! |-----|-----|-----|----| |-----|-----|-----|-----|-----|-----|-----|-----|-----|---|
565 ! | 0 | 1 | 2 | 0 | --> | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0|
566 ! |-----|-----|-----|----| |-----|-----|-----|-----|-----|-----|-----|-----|-----|---|
567 ! a a a b a1 a1 a1 a2 a2 a2 b1 empty empty b2
568 newdim_col = newdim_col + 1
569
570 ! the number of columns which does not fit into the added extra block
571 n_doubled_items_in_partially_filled_block = n_doubled_items_in_partially_filled_block - ncol_block
572 ELSE IF (nfilled_blocks_remain > 0) THEN
573 ! |-----|-----|-----|-----|--| |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
574 ! | 0 | 1 | 2 | 0 | 1| -> | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 |
575 ! |-----|-----|-----|-----|--| |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
576 ! a a a b b a1 a1 a1 a2 a2 a2 b1 b1 b2 empty b2
577 newdim_col = newdim_col + 1
578 n_doubled_items_in_partially_filled_block = 0
579 END IF
580
581 newdim_col = (newdim_col*nprocs_col + nfilled_blocks_remain)*ncol_block + n_doubled_items_in_partially_filled_block
582 END IF
583 END IF
584
585 IF (row) THEN
586 IF (nrow_global == 0) THEN
587 newdim_row = 0
588 ELSE
589 n_doubled_items_in_partially_filled_block = 2*mod(nrow_global, nrow_block)
590 nfilled_blocks = nrow_global/nrow_block
591 nfilled_blocks_remain = mod(nfilled_blocks, nprocs_row)
592 newdim_row = 2*(nfilled_blocks/nprocs_row)
593 IF (n_doubled_items_in_partially_filled_block > nrow_block) THEN
594 newdim_row = newdim_row + 1
595 n_doubled_items_in_partially_filled_block = n_doubled_items_in_partially_filled_block - nrow_block
596 ELSE IF (nfilled_blocks_remain > 0) THEN
597 newdim_row = newdim_row + 1
598 n_doubled_items_in_partially_filled_block = 0
599 END IF
600
601 newdim_row = (newdim_row*nprocs_row + nfilled_blocks_remain)*nrow_block + n_doubled_items_in_partially_filled_block
602 END IF
603 END IF
604
605 ! square_blocks=.FALSE. ensures that matrix blocks of the doubled matrix will have
606 ! nrow_block x ncol_block shape even in case of a square doubled matrix
607 CALL cp_fm_struct_create(fmstruct=fmstruct, para_env=para_env, &
608 context=context, &
609 nrow_global=newdim_row, &
610 ncol_global=newdim_col, &
611 ncol_block=ncol_block, &
612 nrow_block=nrow_block, &
613 square_blocks=.false.)
614
615 END SUBROUTINE cp_fm_struct_double
616! **************************************************************************************************
617!> \brief allows to modify the default settings for matrix creation
618!> \param nrow_block ...
619!> \param ncol_block ...
620!> \param force_block ...
621! **************************************************************************************************
622 SUBROUTINE cp_fm_struct_config(nrow_block, ncol_block, force_block)
623 INTEGER, INTENT(IN), OPTIONAL :: nrow_block, ncol_block
624 LOGICAL, INTENT(IN), OPTIONAL :: force_block
625
626 INTEGER :: vlen
627
628 vlen = m_cpuid_vlen()
629 IF (PRESENT(ncol_block)) THEN
630 IF (0 < ncol_block) THEN
631 optimal_blacs_col_block_size = (ncol_block + vlen - 1)/vlen*vlen
632 END IF
633 END IF
634 IF (PRESENT(nrow_block)) THEN
635 IF (0 < nrow_block) THEN
636 optimal_blacs_row_block_size = (nrow_block + vlen - 1)/vlen*vlen
637 END IF
638 END IF
639 IF (PRESENT(force_block)) force_block_size = force_block
640
641 END SUBROUTINE cp_fm_struct_config
642
643! **************************************************************************************************
644!> \brief ...
645!> \return ...
646! **************************************************************************************************
647 FUNCTION cp_fm_struct_get_nrow_block() RESULT(res)
648 INTEGER :: res
649
650 res = optimal_blacs_row_block_size
651 END FUNCTION cp_fm_struct_get_nrow_block
652
653! **************************************************************************************************
654!> \brief ...
655!> \return ...
656! **************************************************************************************************
657 FUNCTION cp_fm_struct_get_ncol_block() RESULT(res)
658 INTEGER :: res
659
660 res = optimal_blacs_col_block_size
661 END FUNCTION cp_fm_struct_get_ncol_block
662
663! **************************************************************************************************
664!> \brief wrapper to scalapack function INDXG2P that computes the row process
665!> coordinate which possesses the entry of a distributed matrix specified
666!> by a global index INDXGLOB.
667!> \param struct ...
668!> \param INDXGLOB ...
669!> \return ...
670!> \author Mauro Del Ben [MDB] - 12.2012, modified by F. Stein
671! **************************************************************************************************
672 FUNCTION cp_fm_indxg2p_row(struct, INDXGLOB) RESULT(G2P)
673 CLASS(cp_fm_struct_type), INTENT(IN) :: struct
674 INTEGER, INTENT(IN) :: indxglob
675 INTEGER :: g2p
676
677#if defined(__parallel)
678 INTEGER :: number_of_process_rows
679 INTEGER, EXTERNAL :: indxg2p
680#endif
681
682#if defined(__parallel)
683
684 CALL struct%context%get(number_of_process_rows=number_of_process_rows)
685
686 g2p = indxg2p(indxglob, struct%nrow_block, 0, struct%first_p_pos(1), number_of_process_rows)
687
688#else
689 mark_used(struct)
690 mark_used(indxglob)
691
692 g2p = 0
693
694#endif
695
696 END FUNCTION cp_fm_indxg2p_row
697
698! **************************************************************************************************
699!> \brief wrapper to scalapack function INDXG2P that computes the col process
700!> coordinate which possesses the entry of a distributed matrix specified
701!> by a global index INDXGLOB.
702!> \param struct ...
703!> \param INDXGLOB ...
704!> \return ...
705!> \author Mauro Del Ben [MDB] - 12.2012, modified by F. Stein
706! **************************************************************************************************
707 FUNCTION cp_fm_indxg2p_col(struct, INDXGLOB) RESULT(G2P)
708 CLASS(cp_fm_struct_type), INTENT(IN) :: struct
709 INTEGER, INTENT(IN) :: indxglob
710 INTEGER :: g2p
711
712#if defined(__parallel)
713 INTEGER :: number_of_process_columns
714 INTEGER, EXTERNAL :: indxg2p
715#endif
716
717#if defined(__parallel)
718
719 CALL struct%context%get(number_of_process_columns=number_of_process_columns)
720
721 g2p = indxg2p(indxglob, struct%ncol_block, 0, struct%first_p_pos(2), number_of_process_columns)
722
723#else
724 mark_used(struct)
725 mark_used(indxglob)
726
727 g2p = 0
728
729#endif
730
731 END FUNCTION cp_fm_indxg2p_col
732
733! **************************************************************************************************
734!> \brief wrapper to scalapack function INDXG2L that computes the local index
735!> of a distributed matrix entry pointed to by the global index INDXGLOB.
736!>
737!> Arguments
738!> =========
739!>
740!> INDXGLOB (global input) INTEGER
741!> The global index of the distributed matrix entry.
742!>
743!> NB (global input) INTEGER
744!> Block size, size of the blocks the distributed matrix is
745!> split into.
746!>
747!> IPROC (local dummy) INTEGER
748!> Dummy argument in this case in order to unify the calling
749!> sequence of the tool-routines.
750!>
751!> ISRCPROC (local dummy) INTEGER
752!> Dummy argument in this case in order to unify the calling
753!> sequence of the tool-routines.
754!>
755!> NPROCS (global input) INTEGER
756!> The total number processes over which the distributed
757!> matrix is distributed.
758!>
759!> \param struct ...
760!> \param INDXGLOB ...
761!> \return ...
762!> \author Mauro Del Ben [MDB] - 12.2012
763! **************************************************************************************************
764 FUNCTION cp_fm_indxg2l_row(struct, INDXGLOB) RESULT(G2L)
765 CLASS(cp_fm_struct_type), INTENT(IN) :: struct
766 INTEGER, INTENT(IN) :: indxglob
767 INTEGER :: g2l
768
769#if defined(__parallel)
770 INTEGER :: number_of_process_rows
771 INTEGER, EXTERNAL :: indxg2l
772#endif
773
774#if defined(__parallel)
775
776 CALL struct%context%get(number_of_process_rows=number_of_process_rows)
777
778 g2l = indxg2l(indxglob, struct%nrow_block, 0, struct%first_p_pos(1), number_of_process_rows)
779
780#else
781 mark_used(struct)
782
783 g2l = indxglob
784
785#endif
786
787 END FUNCTION cp_fm_indxg2l_row
788
789! **************************************************************************************************
790!> \brief wrapper to scalapack function INDXG2L that computes the local index
791!> of a distributed matrix entry pointed to by the global index INDXGLOB.
792!>
793!> Arguments
794!> =========
795!>
796!> INDXGLOB (global input) INTEGER
797!> The global index of the distributed matrix entry.
798!>
799!> NB (global input) INTEGER
800!> Block size, size of the blocks the distributed matrix is
801!> split into.
802!>
803!> IPROC (local dummy) INTEGER
804!> Dummy argument in this case in order to unify the calling
805!> sequence of the tool-routines.
806!>
807!> ISRCPROC (local dummy) INTEGER
808!> Dummy argument in this case in order to unify the calling
809!> sequence of the tool-routines.
810!>
811!> NPROCS (global input) INTEGER
812!> The total number processes over which the distributed
813!> matrix is distributed.
814!>
815!> \param struct ...
816!> \param INDXGLOB ...
817!> \return ...
818!> \author Mauro Del Ben [MDB] - 12.2012
819! **************************************************************************************************
820 FUNCTION cp_fm_indxg2l_col(struct, INDXGLOB) RESULT(G2L)
821 CLASS(cp_fm_struct_type), INTENT(IN) :: struct
822 INTEGER, INTENT(IN) :: indxglob
823 INTEGER :: g2l
824
825#if defined(__parallel)
826 INTEGER :: number_of_process_columns
827 INTEGER, EXTERNAL :: indxg2l
828#endif
829
830#if defined(__parallel)
831
832 CALL struct%context%get(number_of_process_columns=number_of_process_columns)
833
834 g2l = indxg2l(indxglob, struct%ncol_block, 0, struct%first_p_pos(2), number_of_process_columns)
835
836#else
837 mark_used(struct)
838
839 g2l = indxglob
840
841#endif
842
843 END FUNCTION cp_fm_indxg2l_col
844
845! **************************************************************************************************
846!> \brief wrapper to scalapack function INDXL2G that computes the global index
847!> of a distributed matrix entry pointed to by the local index INDXLOC
848!> of the process indicated by IPROC.
849!>
850!> Arguments
851!> =========
852!>
853!> INDXLOC (global input) INTEGER
854!> The local index of the distributed matrix entry.
855!>
856!> NB (global input) INTEGER
857!> Block size, size of the blocks the distributed matrix is
858!> split into.
859!>
860!> IPROC (local input) INTEGER
861!> The coordinate of the process whose local array row or
862!> column is to be determined.
863!>
864!> ISRCPROC (global input) INTEGER
865!> The coordinate of the process that possesses the first
866!> row/column of the distributed matrix.
867!>
868!> NPROCS (global input) INTEGER
869!> The total number processes over which the distributed
870!> matrix is distributed.
871!>
872!> \param struct ...
873!> \param INDXLOC ...
874!> \param IPROC ...
875!> \return ...
876!> \author Mauro Del Ben [MDB] - 12.2012
877! **************************************************************************************************
878 FUNCTION cp_fm_indxl2g_row(struct, INDXLOC, IPROC) RESULT(L2G)
879 CLASS(cp_fm_struct_type), INTENT(IN) :: struct
880 INTEGER, INTENT(IN) :: indxloc, iproc
881 INTEGER :: l2g
882
883#if defined(__parallel)
884 INTEGER :: number_of_process_rows
885 INTEGER, EXTERNAL :: indxl2g
886
887 CALL struct%context%get(number_of_process_rows=number_of_process_rows)
888
889 l2g = indxl2g(indxloc, struct%nrow_block, iproc, struct%first_p_pos(1), number_of_process_rows)
890
891#else
892 mark_used(struct)
893 mark_used(indxloc)
894 mark_used(iproc)
895
896 l2g = indxloc
897
898#endif
899
900 END FUNCTION cp_fm_indxl2g_row
901
902! **************************************************************************************************
903!> \brief wrapper to scalapack function INDXL2G that computes the global index
904!> of a distributed matrix entry pointed to by the local index INDXLOC
905!> of the process indicated by IPROC.
906!>
907!> Arguments
908!> =========
909!>
910!> INDXLOC (global input) INTEGER
911!> The local index of the distributed matrix entry.
912!>
913!> NB (global input) INTEGER
914!> Block size, size of the blocks the distributed matrix is
915!> split into.
916!>
917!> IPROC (local input) INTEGER
918!> The coordinate of the process whose local array row or
919!> column is to be determined.
920!>
921!> ISRCPROC (global input) INTEGER
922!> The coordinate of the process that possesses the first
923!> row/column of the distributed matrix.
924!>
925!> NPROCS (global input) INTEGER
926!> The total number processes over which the distributed
927!> matrix is distributed.
928!>
929!> \param struct ...
930!> \param INDXLOC ...
931!> \param IPROC ...
932!> \return ...
933!> \author Mauro Del Ben [MDB] - 12.2012
934! **************************************************************************************************
935 FUNCTION cp_fm_indxl2g_col(struct, INDXLOC, IPROC) RESULT(L2G)
936 CLASS(cp_fm_struct_type), INTENT(IN) :: struct
937 INTEGER, INTENT(IN) :: indxloc, iproc
938 INTEGER :: l2g
939
940#if defined(__parallel)
941 INTEGER :: number_of_process_columns
942 INTEGER, EXTERNAL :: indxl2g
943
944 CALL struct%context%get(number_of_process_columns=number_of_process_columns)
945
946 l2g = indxl2g(indxloc, struct%ncol_block, iproc, struct%first_p_pos(2), number_of_process_columns)
947
948#else
949 mark_used(struct)
950 mark_used(indxloc)
951 mark_used(iproc)
952
953 l2g = indxloc
954
955#endif
956
957 END FUNCTION cp_fm_indxl2g_col
958
959END MODULE cp_fm_struct
methods related to the blacs parallel environment
subroutine, public cp_blacs_env_release(blacs_env)
releases the given blacs_env
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
integer function, public cp_fm_struct_get_nrow_block()
...
integer function, public cp_fm_struct_get_ncol_block()
...
subroutine, public cp_fm_struct_config(nrow_block, ncol_block, force_block)
allows to modify the default settings for matrix creation
subroutine, public cp_fm_struct_get(fmstruct, para_env, context, descriptor, ncol_block, nrow_block, nrow_global, ncol_global, first_p_pos, row_indices, col_indices, nrow_local, ncol_local, nrow_locals, ncol_locals, local_leading_dimension)
returns the values of various attributes of the matrix structure
subroutine, public cp_fm_struct_double(fmstruct, struct, context, col, row)
creates a struct with twice the number of blocks on each core. If matrix A has to be multiplied with ...
logical function, public cp_fm_struct_equivalent(fmstruct1, fmstruct2)
returns true if the two matrix structures are equivalent, false otherwise.
subroutine, public cp_fm_struct_retain(fmstruct)
retains a full matrix structure
subroutine, public cp_fm_struct_write_info(fmstruct, io_unit)
Write nicely formatted info about the FM struct to the given I/O unit.
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
various routines to log and control the output. The idea is that decisions about where to log should ...
recursive integer function, public cp_logger_get_default_unit_nr(logger, local, skip_not_ionode)
asks the default unit number of the given logger. try to use cp_logger_get_unit_nr
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
Definition machine.F:124
pure integer function, public m_cpuid_vlen(cpuid, typesize)
Determine vector-length for a given CPUID.
Definition machine.F:277
Interface to the message passing library MPI.
subroutine, public mp_para_env_release(para_env)
releases the para object (to be called when you don't want anymore the shared copy of this object)
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
keeps the information about the structure of a full matrix
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