44 USE omp_lib,
ONLY: omp_get_max_threads
47#if defined(__HAS_IEEE_EXCEPTIONS)
48 USE ieee_exceptions,
ONLY: ieee_get_halting_mode, &
49 ieee_set_halting_mode, &
52#include "../base/base_uses.f90"
55 USE elpa_constants,
ONLY: elpa_solver_1stage, elpa_solver_2stage, elpa_ok, &
56 elpa_2stage_complex_invalid, &
57 elpa_2stage_complex_default, &
58 elpa_2stage_complex_generic, &
59 elpa_2stage_complex_generic_simple, &
60 elpa_2stage_complex_bgp, &
61 elpa_2stage_complex_bgq, &
62 elpa_2stage_complex_sse_block1, &
63 elpa_2stage_complex_sse_block2, &
64 elpa_2stage_complex_avx_block1, &
65 elpa_2stage_complex_avx_block2, &
66 elpa_2stage_complex_avx2_block1, &
67 elpa_2stage_complex_avx2_block2, &
68 elpa_2stage_complex_avx512_block1, &
69 elpa_2stage_complex_avx512_block2, &
70 elpa_2stage_complex_nvidia_gpu, &
71 elpa_2stage_complex_amd_gpu, &
72 elpa_2stage_complex_intel_gpu_sycl
74 USE elpa,
ONLY: elpa_t, &
75 elpa_allocate, elpa_deallocate
82 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'cp_cfm_elpa'
85 INTEGER,
DIMENSION(16),
PARAMETER :: elpa_c_kernel_ids = [ &
86 elpa_2stage_complex_invalid, &
87 elpa_2stage_complex_generic, &
88 elpa_2stage_complex_generic_simple, &
89 elpa_2stage_complex_bgp, &
90 elpa_2stage_complex_bgq, &
91 elpa_2stage_complex_sse_block1, &
92 elpa_2stage_complex_sse_block2, &
93 elpa_2stage_complex_avx_block1, &
94 elpa_2stage_complex_avx_block2, &
95 elpa_2stage_complex_avx2_block1, &
96 elpa_2stage_complex_avx2_block2, &
97 elpa_2stage_complex_avx512_block1, &
98 elpa_2stage_complex_avx512_block2, &
99 elpa_2stage_complex_nvidia_gpu, &
100 elpa_2stage_complex_amd_gpu, &
101 elpa_2stage_complex_intel_gpu_sycl]
103 CHARACTER(len=14),
DIMENSION(SIZE(elpa_c_kernel_ids)),
PARAMETER :: &
104 elpa_c_kernel_names = [character(len=14) :: &
122 CHARACTER(len=44),
DIMENSION(SIZE(elpa_c_kernel_ids)),
PARAMETER :: &
123 elpa_c_kernel_descriptions = [character(len=44) :: &
124 "Automatically selected kernel", &
126 "Simplified generic kernel", &
127 "Kernel optimized for IBM BGP", &
128 "Kernel optimized for IBM BGQ", &
129 "Kernel optimized for x86_64/SSE (block=1)", &
130 "Kernel optimized for x86_64/SSE (block=2)", &
131 "Kernel optimized for Intel AVX (block=1)", &
132 "Kernel optimized for Intel AVX (block=2)", &
133 "Kernel optimized for Intel AVX2 (block=1)", &
134 "Kernel optimized for Intel AVX2 (block=2)", &
135 "Kernel optimized for Intel AVX-512 (block=1)", &
136 "Kernel optimized for Intel AVX-512 (block=2)", &
137 "Kernel targeting Nvidia GPUs", &
138 "Kernel targeting AMD GPUs", &
139 "Kernel targeting Intel GPUs"]
141 INTEGER,
DIMENSION(1),
PARAMETER :: elpa_c_kernel_ids = [-1]
142 CHARACTER(len=14),
DIMENSION(1),
PARAMETER :: elpa_c_kernel_names = [
"AUTO"]
143 CHARACTER(len=44),
DIMENSION(1),
PARAMETER :: elpa_c_kernel_descriptions = [
"Automatically selected kernel"]
147 INTEGER,
SAVE :: elpa_c_kernel = elpa_c_kernel_ids(1)
151 LOGICAL,
SAVE,
PRIVATE :: elpa_c_correctness_checked = .false.
152 LOGICAL,
SAVE,
PRIVATE :: elpa_c_broken = .false.
159 elpa_c_kernel_names, &
160 elpa_c_kernel_descriptions
170 FUNCTION get_elpa_c_kernel_name(kernel)
RESULT(kernel_name)
171 INTEGER,
INTENT(IN) :: kernel
172 CHARACTER(len=default_string_length) :: kernel_name
176 kernel_name =
"id: "//trim(adjustl(
cp_to_string(kernel)))
177 DO i = 1,
SIZE(elpa_c_kernel_ids)
178 IF (elpa_c_kernel_ids(i) == kernel)
THEN
179 kernel_name = elpa_c_kernel_names(i)
183 END FUNCTION get_elpa_c_kernel_name
191 INTEGER,
INTENT(IN) :: requested_kernel
196 elpa_c_kernel = requested_kernel
199 IF (elpa_c_kernel == elpa_2stage_complex_invalid)
THEN
203 elpa_c_kernel = elpa_2stage_complex_generic
205 elpa_c_kernel = elpa_2stage_complex_sse_block2
207 elpa_c_kernel = elpa_2stage_complex_avx_block2
209 elpa_c_kernel = elpa_2stage_complex_avx2_block2
211 elpa_c_kernel = elpa_2stage_complex_avx512_block2
215#if !defined(__NO_OFFLOAD_ELPA)
216#if defined(__OFFLOAD_CUDA)
217 elpa_c_kernel = elpa_2stage_complex_nvidia_gpu
219#if defined(__OFFLOAD_HIP)
220 elpa_c_kernel = elpa_2stage_complex_amd_gpu
222#if defined(__OFFLOAD_OPENCL)
223 elpa_c_kernel = elpa_2stage_complex_intel_gpu_sycl
227 IF (elpa_c_kernel == elpa_2stage_complex_invalid)
THEN
228 elpa_c_kernel = elpa_2stage_complex_default
232 mark_used(requested_kernel)
243 FUNCTION is_block2_c_kernel(kernel)
RESULT(is_block2)
244 INTEGER,
INTENT(IN) :: kernel
247 is_block2 = (kernel == elpa_2stage_complex_sse_block2) .OR. &
248 (kernel == elpa_2stage_complex_avx_block2) .OR. &
249 (kernel == elpa_2stage_complex_avx2_block2) .OR. &
250 (kernel == elpa_2stage_complex_avx512_block2)
251 END FUNCTION is_block2_c_kernel
267 CHARACTER(LEN=*),
PARAMETER :: routinen =
'check_elpa_c_kernel_correctness'
268 CHARACTER(LEN=4*default_string_length) :: message
269 INTEGER :: handle, i, is_broken, j
271 INTEGER,
PARAMETER :: na_test = 20, nev_test = 18, nblk_test = 8
272 REAL(kind=
dp) :: eps_ortho, eps_residual, rnd_im, rnd_re, test
273 REAL(kind=
dp),
PARAMETER :: th = 1.0e-8_dp
274 REAL(kind=
dp),
DIMENSION(:),
ALLOCATABLE :: eigenvalues
276 TYPE(
cp_cfm_type) :: eigenvectors, matrix, matrix_ref
282 IF (elpa_c_correctness_checked)
RETURN
284 IF (.NOT. is_block2_c_kernel(elpa_c_kernel))
RETURN
286 elpa_c_correctness_checked = .true.
288 CALL timeset(routinen, handle)
295 nrow_global=na_test, ncol_global=na_test, &
296 nrow_block=nblk_test, ncol_block=nblk_test)
298 CALL cp_cfm_create(matrix_ref, fmstruct, name=
"elpa_check_ref")
299 CALL cp_cfm_create(eigenvectors, fmstruct, name=
"elpa_check_vec")
306 rnd_re = 2.0_dp*rng_stream%next() - 1.0_dp
307 rnd_im = 2.0_dp*rng_stream%next() - 1.0_dp
308 matrix%local_data(i, j) = cmplx(rnd_re, rnd_im, kind=
dp)
309 matrix%local_data(j, i) = conjg(matrix%local_data(i, j))
311 rnd_re = 2.0_dp*rng_stream%next() - 1.0_dp
312 matrix%local_data(j, j) = cmplx(rnd_re, 0.0_dp, kind=
dp)
316 ALLOCATE (eigenvalues(nev_test))
317 CALL cp_cfm_diag_elpa_base(matrix, eigenvectors, eigenvalues, rdinfo)
320 CALL cp_cfm_gemm(
"C",
"N", nev_test, nev_test, na_test, (1.0_dp, 0.0_dp), &
321 eigenvectors, eigenvectors, (0.0_dp, 0.0_dp), matrix)
323 outer_ortho:
DO i = 1, nev_test
325 test = abs(matrix%local_data(i, j))
326 IF (i == j) test = abs(matrix%local_data(i, j) - (1.0_dp, 0.0_dp))
327 IF (test > eps_ortho) eps_ortho = test
328 IF (eps_ortho > th)
EXIT outer_ortho
333 CALL cp_cfm_gemm(
"N",
"N", na_test, nev_test, na_test, (1.0_dp, 0.0_dp), &
334 matrix_ref, eigenvectors, (0.0_dp, 0.0_dp), matrix)
335 eps_residual = 0.0_dp
336 outer_res:
DO i = 1, na_test
338 test = abs(matrix%local_data(i, j) - eigenvectors%local_data(i, j)*eigenvalues(j))
339 IF (test > eps_residual) eps_residual = test
340 IF (eps_residual > th)
EXIT outer_res
345 elpa_c_broken = (eps_ortho > th) .OR. (eps_residual > th)
349 IF (elpa_c_broken) is_broken = 1
350 CALL para_env%max(is_broken)
351 elpa_c_broken = is_broken == 1
352 IF (elpa_c_broken)
THEN
353 message =
"The ELPA complex kernel "//trim(get_elpa_c_kernel_name(elpa_c_kernel))// &
354 " failed a runtime correctness check (orthogonality: "//trim(
cp_to_string(eps_ortho))// &
355 ", residual: "//trim(
cp_to_string(eps_residual))//
") and may return incorrect"// &
356 " eigenvectors. Complex matrices fall back to ScaLAPACK. Consider the GENERIC"// &
357 " kernel or rebuilding ELPA with -fno-tree-slp-vectorize."
358 CALL cp_warn(__location__, trim(message))
361 DEALLOCATE (eigenvalues)
368 CALL timestop(handle)
381 broken = elpa_c_broken
391 TYPE(
cp_cfm_type),
INTENT(IN) :: matrix, eigenvectors
392 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: eigenvalues
395 CHARACTER(len=*),
PARAMETER :: routinen =
'cp_cfm_diag_elpa'
401 CALL timeset(routinen, handle)
409 caller_is_elpa=.true., redist_info=rdinfo)
412 IF (
ASSOCIATED(matrix_new%matrix_struct))
THEN
413 CALL cp_cfm_diag_elpa_base(matrix_new, eigenvectors_new, eigenvalues, rdinfo)
419 CALL timestop(handle)
423 mark_used(eigenvectors)
425 cpabort(
"CP2K compiled without the ELPA library.")
437 SUBROUTINE cp_cfm_diag_elpa_base(matrix, eigenvectors, eigenvalues, rdinfo)
439 TYPE(
cp_cfm_type),
INTENT(IN) :: matrix, eigenvectors
440 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: eigenvalues
443 CHARACTER(len=*),
PARAMETER :: routinen =
'cp_cfm_diag_elpa_base'
447 CLASS(elpa_t),
POINTER :: elpa_obj
448 CHARACTER(len=default_string_length) :: kernel_name
449 CHARACTER(len=2*default_string_length) :: message
451 INTEGER :: fallback_kernel, &
454 nblk, neig, io_unit, &
456 REAL(kind=
dp),
DIMENSION(:),
ALLOCATABLE :: eval
459 INTEGER,
DIMENSION(:),
POINTER :: ncol_locals
460#if defined(__HAS_IEEE_EXCEPTIONS)
461 LOGICAL,
DIMENSION(5) :: halt
464 CALL timeset(routinen, handle)
466 NULLIFY (ncol_locals)
471 n = matrix%matrix_struct%nrow_global
472 context => matrix%matrix_struct%context
473 group = matrix%matrix_struct%para_env
475 myprow = context%mepos(1)
476 mypcol = context%mepos(2)
482 local_leading_dimension=n_rows, &
485 ncol_locals=ncol_locals)
488 IF (io_unit > 0 .AND. any(ncol_locals == 0))
THEN
489 CALL rdinfo%write(io_unit)
490 cpabort(
"ELPA [pre-fail]: Problem contains processor column with zero width.")
493 neig =
SIZE(eigenvalues, 1)
497 WRITE (unit=io_unit, fmt=
"(/,T2,A)") &
498 "ELPA| Matrix diagonalization information"
500 kernel_name = get_elpa_c_kernel_name(elpa_c_kernel)
502 WRITE (unit=io_unit, fmt=
"(T2,A,T71,I10)") &
503 "ELPA| Matrix order (NA) ", n, &
504 "ELPA| Matrix block size (NBLK) ", nblk, &
505 "ELPA| Number of eigenvectors (NEV) ", neig, &
506 "ELPA| Local rows (LOCAL_NROWS) ", n_rows, &
507 "ELPA| Local columns (LOCAL_NCOLS) ", n_cols
508 WRITE (unit=io_unit, fmt=
"(T2,A,T61,A20)") &
509 "ELPA| Kernel ", adjustr(trim(kernel_name))
515 elpa_obj => elpa_allocate()
517 CALL elpa_obj%set(
"na", n, success)
518 cpassert(success == elpa_ok)
520 CALL elpa_obj%set(
"nev", neig, success)
521 cpassert(success == elpa_ok)
523 CALL elpa_obj%set(
"local_nrows", n_rows, success)
524 cpassert(success == elpa_ok)
526 CALL elpa_obj%set(
"local_ncols", n_cols, success)
527 cpassert(success == elpa_ok)
529 CALL elpa_obj%set(
"nblk", nblk, success)
530 cpassert(success == elpa_ok)
532 CALL elpa_obj%set(
"mpi_comm_parent", group%get_handle(), success)
533 cpassert(success == elpa_ok)
535 CALL elpa_obj%set(
"process_row", myprow, success)
536 cpassert(success == elpa_ok)
538 CALL elpa_obj%set(
"process_col", mypcol, success)
539 cpassert(success == elpa_ok)
541 success = elpa_obj%setup()
542 cpassert(success == elpa_ok)
544 CALL elpa_obj%set(
"solver", &
547 IF (success /= elpa_ok)
THEN
548 cpabort(
"Setting solver for ELPA failed")
552 SELECT CASE (elpa_c_kernel)
553 CASE (elpa_2stage_complex_nvidia_gpu)
554 CALL elpa_obj%set(
"nvidia-gpu", 1, success)
555 cpassert(success == elpa_ok)
556 CASE (elpa_2stage_complex_amd_gpu)
557 CALL elpa_obj%set(
"amd-gpu", 1, success)
558 cpassert(success == elpa_ok)
559 CASE (elpa_2stage_complex_intel_gpu_sycl)
560 CALL elpa_obj%set(
"intel-gpu", 1, success)
561 cpassert(success == elpa_ok)
566 CALL elpa_obj%get(
"complex_kernel", fallback_kernel, success)
567 cpassert(success == elpa_ok)
569 CALL elpa_obj%set(
"complex_kernel", elpa_c_kernel, success)
570 IF (success /= elpa_ok)
THEN
571 message =
"Requested ELPA complex kernel "//trim(get_elpa_c_kernel_name(elpa_c_kernel))// &
572 " is unavailable; falling back to "//trim(get_elpa_c_kernel_name(fallback_kernel))
573 CALL cp_warn(__location__, trim(message))
574 CALL elpa_obj%set(
"complex_kernel", fallback_kernel, success)
575 cpassert(success == elpa_ok)
577 elpa_c_kernel = fallback_kernel
582 IF (elpa_obj%can_set(
"omp_threads", omp_get_max_threads()) == elpa_ok)
THEN
583 CALL elpa_obj%set(
"omp_threads", omp_get_max_threads(), success)
584 cpassert(success == elpa_ok)
588#if defined(__HAS_IEEE_EXCEPTIONS)
589 CALL ieee_get_halting_mode(ieee_all, halt)
590 CALL ieee_set_halting_mode(ieee_all, .false.)
592 CALL elpa_obj%eigenvectors(matrix%local_data, eval, eigenvectors%local_data, success)
593#if defined(__HAS_IEEE_EXCEPTIONS)
594 CALL ieee_set_halting_mode(ieee_all, halt)
597 IF (success /= elpa_ok)
THEN
598 cpabort(
"ELPA failed to diagonalize a matrix")
601 CALL elpa_deallocate(elpa_obj, success)
602 cpassert(success == elpa_ok)
604 eigenvalues(1:neig) = eval(1:neig)
607 CALL timestop(handle)
609 END SUBROUTINE cp_cfm_diag_elpa_base
Target architecture or instruction set extension according to compiler target flags.
methods related to the blacs parallel environment
subroutine, public cp_blacs_env_release(blacs_env)
releases the given blacs_env
subroutine, public cp_blacs_env_create(blacs_env, para_env, blacs_grid_layout, blacs_repeatable, row_major, grid_2d)
allocates and initializes a type that represent a blacs context
Basic linear algebra operations for complex full matrices.
subroutine, public cp_cfm_gemm(transa, transb, m, n, k, alpha, matrix_a, matrix_b, beta, matrix_c, a_first_col, a_first_row, b_first_col, b_first_row, c_first_col, c_first_row)
Performs one of the matrix-matrix operations: matrix_c = alpha * op1( matrix_a ) * op2( matrix_b ) + ...
subroutine, public cp_cfm_uplo_to_full(matrix, workspace, uplo)
...
Wrapper for ELPA (complex matrices, i.e. cp_cfm_type)
subroutine, public cp_cfm_diag_elpa(matrix, eigenvectors, eigenvalues)
Driver routine to diagonalize a CFM matrix with the ELPA library.
subroutine, public check_elpa_c_kernel_correctness(para_env)
One-time runtime correctness check for ELPA complex BLOCK2 kernels. A small deterministic eigenproble...
subroutine, public set_elpa_c_kernel(requested_kernel)
Sets the active ELPA kernel for complex matrices.
logical function, public is_elpa_c_broken()
Returns .TRUE. if the runtime check detected broken BLOCK2 kernels.
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
Auxiliary tools to redistribute cp_fm_type and cp_cfm_type matrices before and after diagonalization....
subroutine, public cp_cfm_redistribute_start(matrix, eigenvectors, matrix_new, eigenvectors_new, caller_is_elpa, redist_info)
Determines the optimal number of CPUs for matrix diagonalization and redistributes the input matrices...
subroutine, public cp_cfm_redistribute_end(matrix, eigenvectors, eig, matrix_new, eigenvectors_new)
Redistributes eigenvectors and eigenvalues back to the original communicator group.
logical, save, public elpa_print
logical, save, public elpa_one_stage
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
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_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 ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
Defines the basic variable types.
integer, parameter, public dp
integer, parameter, public default_string_length
Machine interface based on Fortran 2003 and POSIX.
integer, parameter, public machine_x86_avx
integer, parameter, public machine_x86_sse4
integer, parameter, public machine_cpu_generic
integer, parameter, public machine_x86_avx2
integer, parameter, public machine_x86_avx512
Interface to the message passing library MPI.
type(mp_comm_type), parameter, public mp_comm_self
Parallel (pseudo)random number generator (RNG) for multiple streams and substreams of random numbers.
integer, parameter, public uniform
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
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