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 Routines that link DBCSR and CP2K concepts together
10 : !> \author Ole Schuett
11 : !> \par History
12 : !> 01.2014 created
13 : ! **************************************************************************************************
14 : MODULE cp_dbcsr_cp2k_link
15 : USE ao_util, ONLY: exp_radius
16 : USE atomic_kind_types, ONLY: atomic_kind_type,&
17 : get_atomic_kind_set
18 : USE basis_set_types, ONLY: gto_basis_set_p_type,&
19 : gto_basis_set_type
20 : USE bibliography, ONLY: Borstnik2014,&
21 : Heinecke2016,&
22 : Schuett2016,&
23 : cite_reference
24 : USE cp_control_types, ONLY: dft_control_type
25 : USE cp_dbcsr_api, ONLY: &
26 : dbcsr_finalize, dbcsr_get_block_p, dbcsr_get_default_config, dbcsr_get_matrix_type, &
27 : dbcsr_has_symmetry, dbcsr_reserve_blocks, dbcsr_set, dbcsr_set_config, dbcsr_type, &
28 : dbcsr_type_no_symmetry
29 : USE cp_dbcsr_operations, ONLY: max_elements_per_block
30 : USE input_keyword_types, ONLY: keyword_create,&
31 : keyword_release,&
32 : keyword_type
33 : USE input_section_types, ONLY: section_add_keyword,&
34 : section_add_subsection,&
35 : section_create,&
36 : section_release,&
37 : section_type,&
38 : section_vals_get_subs_vals,&
39 : section_vals_type,&
40 : section_vals_val_get
41 : USE kinds, ONLY: dp,&
42 : real_8
43 : USE orbital_pointers, ONLY: nso
44 : USE qs_integral_utils, ONLY: basis_set_list_setup
45 : USE qs_kind_types, ONLY: qs_kind_type
46 : USE qs_ks_types, ONLY: get_ks_env,&
47 : qs_ks_env_type
48 : USE qs_neighbor_list_types, ONLY: get_iterator_info,&
49 : get_neighbor_list_set_p,&
50 : neighbor_list_iterate,&
51 : neighbor_list_iterator_create,&
52 : neighbor_list_iterator_p_type,&
53 : neighbor_list_iterator_release,&
54 : neighbor_list_set_p_type
55 : USE string_utilities, ONLY: s2a
56 : #include "./base/base_uses.f90"
57 :
58 : IMPLICIT NONE
59 :
60 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_dbcsr_cp2k_link'
61 :
62 : PUBLIC :: create_dbcsr_section
63 : PUBLIC :: cp_dbcsr_config
64 : PUBLIC :: cp_dbcsr_alloc_block_from_nbl
65 : PUBLIC :: cp_dbcsr_to_csr_screening
66 :
67 : PRIVATE
68 :
69 : ! Possible drivers to use for matrix multiplications
70 : INTEGER, PARAMETER :: mm_driver_auto = 0
71 : INTEGER, PARAMETER :: mm_driver_matmul = 1
72 : INTEGER, PARAMETER :: mm_driver_blas = 2
73 : INTEGER, PARAMETER :: mm_driver_smm = 3
74 : INTEGER, PARAMETER :: mm_driver_xsmm = 4
75 :
76 : CHARACTER(len=*), PARAMETER :: mm_name_auto = "AUTO", &
77 : mm_name_blas = "BLAS", &
78 : mm_name_matmul = "MATMUL", &
79 : mm_name_smm = "SMM", &
80 : mm_name_xsmm = "XSMM"
81 : CONTAINS
82 :
83 : ! **************************************************************************************************
84 : !> \brief Creates the dbcsr section for configuring DBCSR
85 : !> \param section ...
86 : !> \date 2011-04-05
87 : !> \author Urban Borstnik
88 : ! **************************************************************************************************
89 266855 : SUBROUTINE create_dbcsr_section(section)
90 : TYPE(section_type), POINTER :: section
91 :
92 : INTEGER :: idefault
93 : LOGICAL :: ldefault
94 : REAL(KIND=dp) :: rdefault
95 : TYPE(keyword_type), POINTER :: keyword
96 : TYPE(section_type), POINTER :: subsection
97 :
98 14045 : CPASSERT(.NOT. ASSOCIATED(section))
99 : CALL section_create(section, __LOCATION__, name="DBCSR", &
100 : description="Configuration options for the DBCSR library.", &
101 : n_keywords=1, n_subsections=0, repeats=.FALSE., &
102 42135 : citations=(/Borstnik2014, Schuett2016/))
103 :
104 14045 : NULLIFY (keyword)
105 :
106 : CALL keyword_create(keyword, __LOCATION__, name="mm_stack_size", &
107 : description="Size of multiplication parameter stack." &
108 : //" A negative value leaves the decision up to DBCSR.", &
109 : usage="mm_stack_size 1000", &
110 14045 : default_i_val=-1)
111 14045 : CALL section_add_keyword(section, keyword)
112 14045 : CALL keyword_release(keyword)
113 :
114 : CALL keyword_create(keyword, __LOCATION__, name="mm_driver", &
115 : description="Select which backend to use preferably "// &
116 : "for matrix block multiplications on the host.", &
117 : usage="mm_driver blas", &
118 : default_i_val=mm_driver_auto, &
119 : enum_c_vals=s2a("AUTO", "BLAS", "MATMUL", "SMM", "XSMM"), &
120 : enum_i_vals=(/mm_driver_auto, mm_driver_blas, mm_driver_matmul, mm_driver_smm, &
121 : mm_driver_xsmm/), &
122 : enum_desc=s2a("Choose automatically the best available driver", &
123 : "BLAS (requires the BLAS library at link time)", &
124 : "Fortran MATMUL", &
125 : "Library optimised for Small Matrix Multiplies "// &
126 : "(requires the SMM library at link time)", &
127 : "LIBXSMM"), &
128 28090 : citations=(/Heinecke2016/))
129 14045 : CALL section_add_keyword(section, keyword)
130 14045 : CALL keyword_release(keyword)
131 :
132 14045 : CALL dbcsr_get_default_config(avg_elements_images=idefault)
133 : CALL keyword_create(keyword, __LOCATION__, name="avg_elements_images", &
134 : description="Average number of elements (dense limit)" &
135 : //" for each image, which also corresponds to" &
136 : //" the average number of elements exchanged between MPI processes" &
137 : //" during the operations." &
138 : //" A negative or zero value means unlimited.", &
139 : usage="avg_elements_images 10000", &
140 14045 : default_i_val=idefault)
141 14045 : CALL section_add_keyword(section, keyword)
142 14045 : CALL keyword_release(keyword)
143 :
144 14045 : CALL dbcsr_get_default_config(num_mult_images=idefault)
145 : CALL keyword_create(keyword, __LOCATION__, name="num_mult_images", &
146 : description="Multiplicative factor for number of virtual images.", &
147 : usage="num_mult_images 2", &
148 14045 : default_i_val=idefault)
149 14045 : CALL section_add_keyword(section, keyword)
150 14045 : CALL keyword_release(keyword)
151 :
152 14045 : CALL dbcsr_get_default_config(use_mpi_allocator=ldefault)
153 : CALL keyword_create(keyword, __LOCATION__, name="use_mpi_allocator", &
154 : description="Use MPI allocator" &
155 : //" to allocate buffers used in MPI communications.", &
156 : usage="use_mpi_allocator T", &
157 14045 : default_l_val=ldefault)
158 14045 : CALL section_add_keyword(section, keyword)
159 14045 : CALL keyword_release(keyword)
160 :
161 14045 : CALL dbcsr_get_default_config(use_mpi_rma=ldefault)
162 : CALL keyword_create(keyword, __LOCATION__, name="use_mpi_rma", &
163 : description="Use RMA for MPI communications" &
164 : //" for each image, which also corresponds to" &
165 : //" the number of elements exchanged between MPI processes" &
166 : //" during the operations.", &
167 : usage="use_mpi_rma F", &
168 14045 : default_l_val=ldefault)
169 14045 : CALL section_add_keyword(section, keyword)
170 14045 : CALL keyword_release(keyword)
171 :
172 14045 : CALL dbcsr_get_default_config(num_layers_3D=idefault)
173 : CALL keyword_create(keyword, __LOCATION__, name="num_layers_3D", &
174 : description="Number of layers for the 3D multplication algorithm.", &
175 : usage="num_layers_3D 1", &
176 14045 : default_i_val=idefault)
177 14045 : CALL section_add_keyword(section, keyword)
178 14045 : CALL keyword_release(keyword)
179 :
180 14045 : CALL dbcsr_get_default_config(nstacks=idefault)
181 : CALL keyword_create(keyword, __LOCATION__, name="n_size_mnk_stacks", &
182 : description="Number of stacks to use for distinct atomic sizes" &
183 : //" (e.g., 2 for a system of mostly waters). ", &
184 : usage="n_size_mnk_stacks 2", &
185 14045 : default_i_val=idefault)
186 14045 : CALL section_add_keyword(section, keyword)
187 14045 : CALL keyword_release(keyword)
188 :
189 14045 : CALL dbcsr_get_default_config(use_comm_thread=ldefault)
190 : CALL keyword_create(keyword, __LOCATION__, name="use_comm_thread", &
191 : description="During multiplication, use a thread to periodically poll" &
192 : //" MPI to progress outstanding message completions. This is" &
193 : //" beneficial on systems without a DMA-capable network adapter" &
194 : //" e.g. Cray XE6.", &
195 : usage="use_comm_thread T", &
196 14045 : default_l_val=ldefault)
197 14045 : CALL section_add_keyword(section, keyword)
198 14045 : CALL keyword_release(keyword)
199 :
200 : CALL keyword_create(keyword, __LOCATION__, name="MAX_ELEMENTS_PER_BLOCK", &
201 : description="Default block size for turning dense matrices in blocked ones", &
202 : usage="MAX_ELEMENTS_PER_BLOCK 32", &
203 14045 : default_i_val=max_elements_per_block)
204 14045 : CALL section_add_keyword(section, keyword)
205 14045 : CALL keyword_release(keyword)
206 :
207 : CALL keyword_create(keyword, __LOCATION__, name="comm_thread_load", &
208 : description="If a communications thread is used, specify how much " &
209 : //"multiplication workload (%) the thread should perform in " &
210 : //"addition to communication tasks. " &
211 : //"A negative value leaves the decision up to DBCSR.", &
212 : usage="comm_thread_load 50", &
213 14045 : default_i_val=-1)
214 14045 : CALL section_add_keyword(section, keyword)
215 14045 : CALL keyword_release(keyword)
216 :
217 14045 : CALL dbcsr_get_default_config(multrec_limit=idefault)
218 : CALL keyword_create(keyword, __LOCATION__, name="multrec_limit", &
219 : description="Recursion limit of cache oblivious multrec algorithm.", &
220 14045 : default_i_val=idefault)
221 14045 : CALL section_add_keyword(section, keyword)
222 14045 : CALL keyword_release(keyword)
223 :
224 14045 : CALL dbcsr_get_default_config(use_mempools_cpu=ldefault)
225 : CALL keyword_create(keyword, __LOCATION__, name="use_mempools_cpu", &
226 : description="Enable memory pools on the CPU.", &
227 14045 : default_l_val=ldefault)
228 14045 : CALL section_add_keyword(section, keyword)
229 14045 : CALL keyword_release(keyword)
230 :
231 14045 : NULLIFY (subsection)
232 : CALL section_create(subsection, __LOCATION__, name="TENSOR", &
233 : description="Configuration options for Tensors.", &
234 14045 : n_keywords=1, n_subsections=0, repeats=.FALSE.)
235 :
236 14045 : CALL dbcsr_get_default_config(tas_split_factor=rdefault)
237 : CALL keyword_create(keyword, __LOCATION__, name="TAS_SPLIT_FACTOR", &
238 : description="Parameter for hybrid DBCSR-TAS matrix-matrix multiplication algorithm: "// &
239 : "a TAS matrix is split into s submatrices with s = N_max/(N_min*f) with f "// &
240 : "given by this parameter and N_max/N_min the max/min occupancies of the matrices "// &
241 : "involved in a multiplication. A large value makes the multiplication Cannon-based "// &
242 : "(s=1) and a small value (> 0) makes the multiplication based on TAS algorithm "// &
243 : "(s=number of MPI ranks)", &
244 14045 : default_r_val=rdefault)
245 14045 : CALL section_add_keyword(subsection, keyword)
246 14045 : CALL keyword_release(keyword)
247 :
248 14045 : CALL section_add_subsection(section, subsection)
249 14045 : CALL section_release(subsection)
250 :
251 : !---------------------------------------------------------------------------
252 14045 : NULLIFY (subsection)
253 : CALL section_create(subsection, __LOCATION__, name="ACC", &
254 : description="Configuration options for the ACC-Driver.", &
255 14045 : n_keywords=1, n_subsections=0, repeats=.FALSE.)
256 :
257 14045 : CALL dbcsr_get_default_config(accdrv_thread_buffers=idefault)
258 : CALL keyword_create(keyword, __LOCATION__, name="thread_buffers", &
259 : description="Number of transfer-buffers associated with each thread and corresponding stream.", &
260 14045 : default_i_val=idefault)
261 14045 : CALL section_add_keyword(subsection, keyword)
262 14045 : CALL keyword_release(keyword)
263 :
264 14045 : CALL dbcsr_get_default_config(accdrv_avoid_after_busy=ldefault)
265 : CALL keyword_create(keyword, __LOCATION__, name="avoid_after_busy", &
266 : description="If enabled, stacks are not processed by the acc-driver " &
267 : //"after it has signaled congestion during a round of flushing. " &
268 : //"For the next round of flusing the driver is used again.", &
269 14045 : default_l_val=ldefault)
270 14045 : CALL section_add_keyword(subsection, keyword)
271 14045 : CALL keyword_release(keyword)
272 :
273 14045 : CALL dbcsr_get_default_config(accdrv_min_flop_process=idefault)
274 : CALL keyword_create(keyword, __LOCATION__, name="min_flop_process", &
275 : description="Only process stacks with more than the given number of " &
276 : //"floating-point operations per stack-entry (2*m*n*k).", &
277 14045 : default_i_val=idefault)
278 14045 : CALL section_add_keyword(subsection, keyword)
279 14045 : CALL keyword_release(keyword)
280 :
281 14045 : CALL dbcsr_get_default_config(accdrv_stack_sort=ldefault)
282 : CALL keyword_create(keyword, __LOCATION__, name="stack_sort", &
283 : description="Sort multiplication stacks according to C-access.", &
284 14045 : default_l_val=ldefault)
285 14045 : CALL section_add_keyword(subsection, keyword)
286 14045 : CALL keyword_release(keyword)
287 :
288 14045 : CALL dbcsr_get_default_config(accdrv_min_flop_sort=idefault)
289 : CALL keyword_create(keyword, __LOCATION__, name="min_flop_sort", &
290 : description="Only sort stacks with more than the given number of " &
291 : //"floating-point operations per stack-entry (2*m*n*k). " &
292 : //"Alternatively, the stacks are roughly ordered through a " &
293 : //"binning-scheme by Peter Messmer. (Depends on ACC%STACK_SORT)", &
294 14045 : default_i_val=idefault)
295 14045 : CALL section_add_keyword(subsection, keyword)
296 14045 : CALL keyword_release(keyword)
297 :
298 14045 : CALL dbcsr_get_default_config(accdrv_do_inhomogenous=ldefault)
299 : CALL keyword_create(keyword, __LOCATION__, name="process_inhomogenous", &
300 : description="If enabled, inhomogenous stacks are also processed by the acc driver.", &
301 14045 : default_l_val=ldefault)
302 14045 : CALL section_add_keyword(subsection, keyword)
303 14045 : CALL keyword_release(keyword)
304 :
305 14045 : CALL dbcsr_get_default_config(accdrv_binning_nbins=idefault)
306 : CALL keyword_create(keyword, __LOCATION__, name="binning_nbins", &
307 : description="Number of bins used when ordering " &
308 : //"the stacks with the binning-scheme.", &
309 14045 : default_i_val=idefault)
310 14045 : CALL section_add_keyword(subsection, keyword)
311 14045 : CALL keyword_release(keyword)
312 :
313 14045 : CALL dbcsr_get_default_config(accdrv_binning_binsize=idefault)
314 : CALL keyword_create(keyword, __LOCATION__, name="binning_binsize", &
315 : description="Size of bins used when ordering " &
316 : //"the stacks with the binning-scheme.", &
317 14045 : default_i_val=idefault)
318 14045 : CALL section_add_keyword(subsection, keyword)
319 14045 : CALL keyword_release(keyword)
320 :
321 14045 : CALL section_add_subsection(section, subsection)
322 14045 : CALL section_release(subsection)
323 :
324 14045 : END SUBROUTINE create_dbcsr_section
325 :
326 : ! **************************************************************************************************
327 : !> \brief Configures options for DBCSR
328 : !> \param root_section ...
329 : ! **************************************************************************************************
330 196558 : SUBROUTINE cp_dbcsr_config(root_section)
331 : TYPE(section_vals_type), POINTER :: root_section
332 :
333 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_dbcsr_config'
334 :
335 : INTEGER :: handle, ival
336 : LOGICAL :: lval
337 : REAL(kind=real_8) :: rval
338 : TYPE(section_vals_type), POINTER :: dbcsr_section
339 :
340 8546 : CALL timeset(routineN, handle)
341 :
342 8546 : CALL cite_reference(Borstnik2014)
343 8546 : CALL cite_reference(Schuett2016)
344 :
345 8546 : dbcsr_section => section_vals_get_subs_vals(root_section, "GLOBAL%DBCSR")
346 :
347 8546 : CALL section_vals_val_get(dbcsr_section, "mm_stack_size", i_val=ival)
348 8546 : CALL dbcsr_set_config(mm_stack_size=ival)
349 :
350 8546 : CALL section_vals_val_get(dbcsr_section, "MAX_ELEMENTS_PER_BLOCK", i_val=max_elements_per_block)
351 :
352 8546 : CALL section_vals_val_get(dbcsr_section, "avg_elements_images", i_val=ival)
353 8546 : CALL dbcsr_set_config(avg_elements_images=ival)
354 :
355 8546 : CALL section_vals_val_get(dbcsr_section, "num_mult_images", i_val=ival)
356 8546 : CALL dbcsr_set_config(num_mult_images=ival)
357 :
358 8546 : CALL section_vals_val_get(dbcsr_section, "n_size_mnk_stacks", i_val=ival)
359 8546 : CALL dbcsr_set_config(nstacks=ival)
360 :
361 8546 : CALL section_vals_val_get(dbcsr_section, "use_mpi_allocator", l_val=lval)
362 8546 : CALL dbcsr_set_config(use_mpi_allocator=lval)
363 :
364 8546 : CALL section_vals_val_get(dbcsr_section, "use_mpi_rma", l_val=lval)
365 8546 : CALL dbcsr_set_config(use_mpi_rma=lval)
366 :
367 8546 : CALL section_vals_val_get(dbcsr_section, "num_layers_3D", i_val=ival)
368 8546 : CALL dbcsr_set_config(num_layers_3D=ival)
369 :
370 8546 : CALL section_vals_val_get(dbcsr_section, "use_comm_thread", l_val=lval)
371 8546 : CALL dbcsr_set_config(use_comm_thread=lval)
372 :
373 8546 : CALL section_vals_val_get(dbcsr_section, "comm_thread_load", i_val=ival)
374 8546 : CALL dbcsr_set_config(comm_thread_load=ival)
375 :
376 8546 : CALL section_vals_val_get(dbcsr_section, "multrec_limit", i_val=ival)
377 8546 : CALL dbcsr_set_config(multrec_limit=ival)
378 :
379 8546 : CALL section_vals_val_get(dbcsr_section, "use_mempools_cpu", l_val=lval)
380 8546 : CALL dbcsr_set_config(use_mempools_cpu=lval)
381 :
382 8546 : CALL section_vals_val_get(dbcsr_section, "TENSOR%tas_split_factor", r_val=rval)
383 8546 : CALL dbcsr_set_config(tas_split_factor=rval)
384 :
385 8546 : CALL section_vals_val_get(dbcsr_section, "ACC%thread_buffers", i_val=ival)
386 8546 : CALL dbcsr_set_config(accdrv_thread_buffers=ival)
387 :
388 8546 : CALL section_vals_val_get(dbcsr_section, "ACC%min_flop_process", i_val=ival)
389 8546 : CALL dbcsr_set_config(accdrv_min_flop_process=ival)
390 :
391 8546 : CALL section_vals_val_get(dbcsr_section, "ACC%stack_sort", l_val=lval)
392 8546 : CALL dbcsr_set_config(accdrv_stack_sort=lval)
393 :
394 8546 : CALL section_vals_val_get(dbcsr_section, "ACC%min_flop_sort", i_val=ival)
395 8546 : CALL dbcsr_set_config(accdrv_min_flop_sort=ival)
396 :
397 8546 : CALL section_vals_val_get(dbcsr_section, "ACC%process_inhomogenous", l_val=lval)
398 8546 : CALL dbcsr_set_config(accdrv_do_inhomogenous=lval)
399 :
400 8546 : CALL section_vals_val_get(dbcsr_section, "ACC%avoid_after_busy", l_val=lval)
401 8546 : CALL dbcsr_set_config(accdrv_avoid_after_busy=lval)
402 :
403 8546 : CALL section_vals_val_get(dbcsr_section, "ACC%binning_nbins", i_val=ival)
404 8546 : CALL dbcsr_set_config(accdrv_binning_nbins=ival)
405 :
406 8546 : CALL section_vals_val_get(dbcsr_section, "ACC%binning_binsize", i_val=ival)
407 8546 : CALL dbcsr_set_config(accdrv_binning_binsize=ival)
408 :
409 8546 : CALL section_vals_val_get(dbcsr_section, "mm_driver", i_val=ival)
410 8544 : SELECT CASE (ival)
411 : CASE (mm_driver_auto)
412 8544 : CALL dbcsr_set_config(mm_driver="AUTO")
413 : #if defined(__LIBXSMM)
414 8544 : CALL cite_reference(Heinecke2016)
415 : #endif
416 : CASE (mm_driver_blas)
417 2 : CALL dbcsr_set_config(mm_driver="BLAS")
418 : CASE (mm_driver_matmul)
419 0 : CALL dbcsr_set_config(mm_driver="MATMUL")
420 : CASE (mm_driver_smm)
421 0 : CALL dbcsr_set_config(mm_driver="SMM")
422 : CASE (mm_driver_xsmm)
423 0 : CALL dbcsr_set_config(mm_driver="XSMM")
424 0 : CALL cite_reference(Heinecke2016)
425 : CASE DEFAULT
426 8546 : CPABORT("Unknown mm_driver")
427 : END SELECT
428 :
429 8546 : CALL timestop(handle)
430 8546 : END SUBROUTINE cp_dbcsr_config
431 :
432 : ! **************************************************************************************************
433 : !> \brief allocate the blocks of a dbcsr based on the neighbor list
434 : !> \param matrix the matrix
435 : !> \param sab_orb the corresponding neighbor list
436 : !> \param desymmetrize Allocate all block of a non-symmetric matrix from a symmetric list
437 : !> \par History
438 : !> 11.2009 created vw
439 : !> 01.2014 moved here from cp_dbcsr_operations (Ole Schuett)
440 : !> \author vw
441 : !> \note
442 : ! **************************************************************************************************
443 :
444 725950 : SUBROUTINE cp_dbcsr_alloc_block_from_nbl(matrix, sab_orb, desymmetrize)
445 :
446 : TYPE(dbcsr_type) :: matrix
447 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
448 : POINTER :: sab_orb
449 : LOGICAL, INTENT(IN), OPTIONAL :: desymmetrize
450 :
451 : CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_dbcsr_alloc_block_from_nbl'
452 :
453 : CHARACTER(LEN=1) :: symmetry
454 : INTEGER :: blk_cnt, handle, iatom, icol, inode, &
455 : irow, jatom, last_jatom, nadd
456 725950 : INTEGER, ALLOCATABLE, DIMENSION(:) :: cols, rows, tmp
457 : LOGICAL :: alloc_full, is_symmetric, new_atom_b
458 : TYPE(neighbor_list_iterator_p_type), &
459 725950 : DIMENSION(:), POINTER :: nl_iterator
460 :
461 725950 : CALL timeset(routineN, handle)
462 :
463 725950 : symmetry = dbcsr_get_matrix_type(matrix)
464 :
465 725950 : CPASSERT(ASSOCIATED(sab_orb))
466 :
467 725950 : CALL get_neighbor_list_set_p(neighbor_list_sets=sab_orb, symmetric=is_symmetric)
468 725950 : alloc_full = .FALSE.
469 725950 : IF (PRESENT(desymmetrize)) THEN
470 96 : IF (desymmetrize .AND. (symmetry == dbcsr_type_no_symmetry)) THEN
471 96 : IF (is_symmetric) alloc_full = .TRUE.
472 : END IF
473 : END IF
474 :
475 725950 : CALL dbcsr_finalize(matrix)
476 725950 : ALLOCATE (rows(3), cols(3))
477 725950 : blk_cnt = 0
478 725950 : nadd = 1
479 725950 : IF (alloc_full) nadd = 2
480 :
481 725950 : CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
482 576093643 : DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
483 575367693 : CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom, inode=inode)
484 575367693 : IF (inode == 1) last_jatom = 0
485 575367693 : IF (jatom /= last_jatom) THEN
486 14295256 : new_atom_b = .TRUE.
487 14295256 : last_jatom = jatom
488 : ELSE
489 576093643 : new_atom_b = .FALSE.
490 : CYCLE
491 : END IF
492 14295256 : IF (blk_cnt + nadd .GT. SIZE(rows)) THEN
493 2271645 : ALLOCATE (tmp(blk_cnt + nadd))
494 15355009 : tmp(1:blk_cnt) = rows(1:blk_cnt)
495 757215 : DEALLOCATE (rows)
496 2271645 : ALLOCATE (rows((blk_cnt + nadd)*2))
497 15355009 : rows(1:blk_cnt) = tmp(1:blk_cnt)
498 15355009 : tmp(1:blk_cnt) = cols(1:blk_cnt)
499 757215 : DEALLOCATE (cols)
500 1514430 : ALLOCATE (cols((blk_cnt + nadd)*2))
501 15355009 : cols(1:blk_cnt) = tmp(1:blk_cnt)
502 757215 : DEALLOCATE (tmp)
503 : END IF
504 15021206 : IF (alloc_full) THEN
505 1368 : blk_cnt = blk_cnt + 1
506 1368 : rows(blk_cnt) = iatom
507 1368 : cols(blk_cnt) = jatom
508 1368 : IF (iatom /= jatom) THEN
509 1035 : blk_cnt = blk_cnt + 1
510 1035 : rows(blk_cnt) = jatom
511 1035 : cols(blk_cnt) = iatom
512 : END IF
513 : ELSE
514 14293888 : blk_cnt = blk_cnt + 1
515 14293888 : IF (symmetry == dbcsr_type_no_symmetry) THEN
516 84143 : rows(blk_cnt) = iatom
517 84143 : cols(blk_cnt) = jatom
518 : ELSE
519 14209745 : IF (iatom <= jatom) THEN
520 : irow = iatom
521 : icol = jatom
522 : ELSE
523 6573467 : irow = jatom
524 6573467 : icol = iatom
525 : END IF
526 14209745 : rows(blk_cnt) = irow
527 14209745 : cols(blk_cnt) = icol
528 : END IF
529 : END IF
530 :
531 : END DO
532 725950 : CALL neighbor_list_iterator_release(nl_iterator)
533 :
534 : !
535 725950 : CALL dbcsr_reserve_blocks(matrix, rows(1:blk_cnt), cols(1:blk_cnt))
536 725950 : DEALLOCATE (rows)
537 725950 : DEALLOCATE (cols)
538 725950 : CALL dbcsr_finalize(matrix)
539 :
540 725950 : CALL timestop(handle)
541 :
542 2177850 : END SUBROUTINE cp_dbcsr_alloc_block_from_nbl
543 :
544 : ! **************************************************************************************************
545 : !> \brief Apply distance screening to refine sparsity pattern of matrices in CSR
546 : !> format (using eps_pgf_orb). Currently this is used for the external
547 : !> library PEXSI.
548 : !> \param ks_env ...
549 : !> \param[in, out] csr_sparsity DBCSR matrix defining CSR sparsity pattern.
550 : !> This matrix must be initialized and allocated
551 : !> with exactly the same DBCSR sparsity pattern as
552 : !> the DBCSR matrix that is used to create the CSR
553 : !> matrix. It must have symmetric DBCSR format and
554 : !> must not be filtered.
555 : !> \par History
556 : !> 02.2015 created [Patrick Seewald]
557 : !> \author Patrick Seewald
558 : ! **************************************************************************************************
559 14 : SUBROUTINE cp_dbcsr_to_csr_screening(ks_env, csr_sparsity)
560 : TYPE(qs_ks_env_type), POINTER :: ks_env
561 : TYPE(dbcsr_type), INTENT(INOUT) :: csr_sparsity
562 :
563 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_dbcsr_to_csr_screening'
564 :
565 : INTEGER :: atom_a, atom_b, handle, iatom, icol, ikind, ipgf, irow, iset, isgf, ishell, &
566 : jatom, jkind, jpgf, jset, jsgf, jshell, nkind, nset_a, nset_b
567 14 : INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind
568 14 : INTEGER, DIMENSION(:), POINTER :: npgf_a, npgf_b, nshell_a, nshell_b
569 14 : INTEGER, DIMENSION(:, :), POINTER :: l_a, l_b
570 : LOGICAL :: do_symmetric, found
571 : REAL(KIND=dp) :: dab, eps_pgf_orb, r_a, r_b
572 : REAL(KIND=dp), DIMENSION(3) :: rab
573 14 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: rpgfa, rpgfb, zet_a, zet_b
574 14 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: gcc_a, gcc_b
575 14 : REAL(KIND=real_8), DIMENSION(:, :), POINTER :: screen_blk
576 14 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
577 : TYPE(dft_control_type), POINTER :: dft_control
578 14 : TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list_a, basis_set_list_b
579 : TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
580 : TYPE(neighbor_list_iterator_p_type), &
581 14 : DIMENSION(:), POINTER :: nl_iterator
582 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
583 14 : POINTER :: neighbour_list
584 14 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
585 :
586 14 : NULLIFY (screen_blk, atomic_kind_set, basis_set_list_a, &
587 14 : basis_set_list_b, basis_set_a, basis_set_b, nl_iterator, &
588 14 : qs_kind_set, dft_control)
589 :
590 14 : CALL timeset(routineN, handle)
591 :
592 14 : CPASSERT(dbcsr_has_symmetry(csr_sparsity))
593 :
594 : CALL get_ks_env(ks_env, &
595 : sab_orb=neighbour_list, &
596 : atomic_kind_set=atomic_kind_set, &
597 : qs_kind_set=qs_kind_set, &
598 14 : dft_control=dft_control)
599 :
600 14 : eps_pgf_orb = dft_control%qs_control%eps_pgf_orb
601 :
602 14 : nkind = SIZE(qs_kind_set)
603 14 : CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind)
604 14 : CPASSERT(SIZE(neighbour_list) > 0)
605 14 : CALL get_neighbor_list_set_p(neighbor_list_sets=neighbour_list, symmetric=do_symmetric)
606 14 : CPASSERT(do_symmetric)
607 104 : ALLOCATE (basis_set_list_a(nkind), basis_set_list_b(nkind))
608 14 : CALL basis_set_list_setup(basis_set_list_a, "ORB", qs_kind_set)
609 14 : CALL basis_set_list_setup(basis_set_list_b, "ORB", qs_kind_set)
610 :
611 : ! csr_sparsity can obtain values 0 (if zero element) or 1 (if non-zero element)
612 14 : CALL dbcsr_set(csr_sparsity, 0.0_dp)
613 :
614 14 : CALL neighbor_list_iterator_create(nl_iterator, neighbour_list)
615 :
616 : ! Iterate over interacting pairs of atoms corresponding to non-zero
617 : ! DBCSR blocks
618 5398 : DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
619 : CALL get_iterator_info(nl_iterator, &
620 : ikind=ikind, jkind=jkind, &
621 : iatom=iatom, jatom=jatom, &
622 5384 : r=rab)
623 :
624 5384 : basis_set_a => basis_set_list_a(ikind)%gto_basis_set
625 5384 : IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
626 5384 : basis_set_b => basis_set_list_b(jkind)%gto_basis_set
627 5384 : IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
628 :
629 5384 : atom_a = atom_of_kind(iatom)
630 5384 : atom_b = atom_of_kind(jatom)
631 :
632 5384 : nset_a = basis_set_a%nset
633 5384 : nset_b = basis_set_b%nset
634 5384 : npgf_a => basis_set_a%npgf
635 5384 : npgf_b => basis_set_b%npgf
636 5384 : nshell_a => basis_set_a%nshell
637 5384 : nshell_b => basis_set_b%nshell
638 :
639 5384 : l_a => basis_set_a%l
640 5384 : l_b => basis_set_b%l
641 5384 : gcc_a => basis_set_a%gcc
642 5384 : gcc_b => basis_set_b%gcc
643 5384 : zet_a => basis_set_a%zet
644 5384 : zet_b => basis_set_b%zet
645 :
646 5384 : rpgfa => basis_set_a%pgf_radius
647 5384 : rpgfb => basis_set_b%pgf_radius
648 :
649 5384 : IF (iatom <= jatom) THEN
650 3028 : irow = iatom
651 3028 : icol = jatom
652 : ELSE
653 2356 : irow = jatom
654 2356 : icol = iatom
655 : END IF
656 :
657 : CALL dbcsr_get_block_p(matrix=csr_sparsity, row=irow, col=icol, &
658 5384 : block=screen_blk, found=found)
659 :
660 5384 : CPASSERT(found)
661 :
662 : ! Distance between atoms a and b
663 5384 : dab = SQRT(rab(1)**2 + rab(2)**2 + rab(3)**2)
664 :
665 : ! iterate over pairs of primitive GTOs i,j, get their radii r_i, r_j according
666 : ! to eps_pgf_orb. Define all matrix elements as non-zero to which a
667 : ! contribution from two Gaussians i,j exists with r_i + r_j >= dab.
668 :
669 5384 : isgf = 0
670 16166 : DO iset = 1, nset_a
671 42352 : DO ishell = 1, nshell_a(iset)
672 : jsgf = 0
673 78600 : DO jset = 1, nset_b
674 207002 : DO jshell = 1, nshell_b(jset)
675 432252 : gto_loop: DO ipgf = 1, npgf_a(iset)
676 1619644 : DO jpgf = 1, npgf_b(jset)
677 1573342 : IF (rpgfa(ipgf, iset) + rpgfb(jpgf, jset) .GE. dab) THEN
678 : ! more selective screening with radius calculated for each primitive GTO
679 : r_a = exp_radius(l_a(ishell, iset), &
680 : zet_a(ipgf, iset), &
681 : eps_pgf_orb, &
682 166054 : gcc_a(ipgf, ishell, iset))
683 : r_b = exp_radius(l_b(jshell, jset), &
684 : zet_b(jpgf, jset), &
685 : eps_pgf_orb, &
686 166054 : gcc_b(jpgf, jshell, jset))
687 166054 : IF (r_a + r_b .GE. dab) THEN
688 82100 : IF (irow .EQ. iatom) THEN
689 : screen_blk(isgf + 1:isgf + nso(l_a(ishell, iset)), &
690 410266 : jsgf + 1:jsgf + nso(l_b(jshell, jset))) = 1.0_dp
691 : ELSE
692 : screen_blk(jsgf + 1:jsgf + nso(l_b(jshell, jset)), &
693 318200 : isgf + 1:isgf + nso(l_a(ishell, iset))) = 1.0_dp
694 : END IF
695 : EXIT gto_loop
696 : END IF
697 : END IF
698 : END DO
699 : END DO gto_loop
700 180802 : jsgf = jsgf + nso(l_b(jshell, jset))
701 : END DO
702 : END DO
703 36968 : isgf = isgf + nso(l_a(ishell, iset))
704 : END DO
705 : END DO
706 : END DO
707 :
708 14 : CALL neighbor_list_iterator_release(nl_iterator)
709 14 : DEALLOCATE (basis_set_list_a, basis_set_list_b)
710 :
711 14 : CALL timestop(handle)
712 42 : END SUBROUTINE cp_dbcsr_to_csr_screening
713 :
714 : END MODULE cp_dbcsr_cp2k_link
|