elpa1_auxiliary.F90 139 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
!    This file is part of ELPA.
!
!    The ELPA library was originally created by the ELPA consortium,
!    consisting of the following organizations:
!
!    - Max Planck Computing and Data Facility (MPCDF), formerly known as
!      Rechenzentrum Garching der Max-Planck-Gesellschaft (RZG),
!    - Bergische Universität Wuppertal, Lehrstuhl für angewandte
!      Informatik,
!    - Technische Universität München, Lehrstuhl für Informatik mit
!      Schwerpunkt Wissenschaftliches Rechnen ,
!    - Fritz-Haber-Institut, Berlin, Abt. Theorie,
13
!    - Max-Plack-Institut für Mathematik in den Naturwissenschaften,
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
!      Leipzig, Abt. Komplexe Strukutren in Biologie und Kognition,
!      and
!    - IBM Deutschland GmbH
!
!    This particular source code file contains additions, changes and
!    enhancements authored by Intel Corporation which is not part of
!    the ELPA consortium.
!
!    More information can be found here:
!    http://elpa.mpcdf.mpg.de/
!
!    ELPA is free software: you can redistribute it and/or modify
!    it under the terms of the version 3 of the license of the
!    GNU Lesser General Public License as published by the Free
!    Software Foundation.
!
!    ELPA is distributed in the hope that it will be useful,
!    but WITHOUT ANY WARRANTY; without even the implied warranty of
!    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!    GNU Lesser General Public License for more details.
!
!    You should have received a copy of the GNU Lesser General Public License
!    along with ELPA.  If not, see <http://www.gnu.org/licenses/>
!
!    ELPA reflects a substantial effort on the part of the original
!    ELPA consortium, and we ask you to respect the spirit of the
!    license that we chose: i.e., please contribute any changes you
!    may have back to the original ELPA library distribution, and keep
!    any derivatives of ELPA under the same license that we chose for
!    the original distribution, the GNU Lesser General Public License.
!
!
! ELPA1 -- Faster replacements for ScaLAPACK symmetric eigenvalue routines
!
! Copyright of the original code rests with the authors inside the ELPA
! consortium. The copyright of any additional modifications shall rest
! with their original authors, but shall adhere to the licensing terms
! distributed along with the original code in the file "COPYING".

#include "config-f90.h"

Andreas Marek's avatar
Andreas Marek committed
55
56
!> \brief Fortran module which provides helper routines for matrix calculations
module ELPA1_AUXILIARY
57
58
  implicit none

59
60
  public :: elpa_mult_at_b_real_double      !< Multiply double-precision real matrices A**T * B
  public :: mult_at_b_real                  !< Old, deprecated interface to multiply double-precision real matrices A**T * B. DO NOT USE
61

62
63
  public :: elpa_mult_ah_b_complex_double   !< Multiply double-precision complex matrices A**H * B
  public :: mult_ah_b_complex               !< Old, deprecated interface to multiply double-precision complex matrices A**H * B. DO NOT USE
64

65
66
  public :: elpa_invert_trm_real_double     !< Invert double-precision real triangular matrix
  public :: invert_trm_real                 !< Old, deprecated interface for inversion of double-precision real triangular matrix. DO NOT USE
67

68
69
  public :: elpa_invert_trm_complex_double  !< Invert double-precision complex triangular matrix
  public :: invert_trm_complex              !< Old, deprecated interface to invert double-precision complex triangular matrix. DO NOT USE
70

71
72
  public :: elpa_cholesky_real_double       !< Cholesky factorization of a double-precision real matrix
  public :: cholesky_real                   !< Old, deprecated name for Cholesky factorization of a double-precision real matrix. DO NOT USE
73

74
75
  public :: elpa_cholesky_complex_double    !< Cholesky factorization of a double-precision complex matrix
  public :: cholesky_complex                !< Old, deprecated interface for a Cholesky factorization of a double-precision complex matrix. DO NOT USE
76

77
78
  public :: elpa_solve_tridi_double         !< Solve tridiagonal eigensystem for a double-precision matrix with divide and conquer method
  public :: solve_tridi                     !< Old, deprecated interface to solve tridiagonal eigensystem for a double-precision matrix with divide and conquer method
79
80

#ifdef WANT_SINGLE_PRECISION_REAL
81
82
83
84
  public :: elpa_cholesky_real_single       !< Cholesky factorization of a single-precision real matrix
  public :: elpa_invert_trm_real_single     !< Invert single-precision real triangular matrix
  public :: elpa_mult_at_b_real_single      !< Multiply single-precision real matrices A**T * B
  public :: elpa_solve_tridi_single         !< Solve tridiagonal eigensystem for a single-precision matrix with divide and conquer method
85
86
87
#endif

#ifdef WANT_SINGLE_PRECISION_COMPLEX
88
89
90
  public :: elpa_cholesky_complex_single    !< Cholesky factorization of a single-precision complex matrix
  public :: elpa_invert_trm_complex_single  !< Invert single-precision complex triangular matrix
  public :: elpa_mult_ah_b_complex_single   !< Multiply single-precision complex matrices A**H * B
91
92
#endif

93
!> \brief  cholesky_real: old, deprecated interface for Cholesky factorization of a double-precision real symmetric matrix
94
95
96
97
98
!> \details
!>
!> \param  na                   Order of matrix
!> \param  a(lda,matrixCols)    Distributed matrix which should be factorized.
!>                              Distribution is like in Scalapack.
Andreas Marek's avatar
Andreas Marek committed
99
!>                              Only upper triangle needs to be set.
100
101
102
103
104
105
106
107
!>                              On return, the upper triangle contains the Cholesky factor
!>                              and the lower triangle is set to 0.
!> \param  lda                  Leading dimension of a
!> \param                       matrixCols  local columns of matrix a
!> \param  nblk                 blocksize of cyclic distribution, must be the same in both directions!
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param wantDebug             logical, more debug information on failure
108
!> \result succes                logical, reports success or failure
109
  interface cholesky_real
110
    module procedure elpa_cholesky_real_double
111
112
  end interface

113
!> \brief  Old, deprecated interface invert_trm_real: Inverts a upper double-precision triangular matrix
114
115
116
117
!> \details
!> \param  na                   Order of matrix
!> \param  a(lda,matrixCols)    Distributed matrix which should be inverted
!>                              Distribution is like in Scalapack.
Andreas Marek's avatar
Andreas Marek committed
118
!>                              Only upper triangle needs to be set.
119
120
121
!>                              The lower triangle is not referenced.
!> \param  lda                  Leading dimension of a
!> \param  nblk                 blocksize of cyclic distribution, must be the same in both directions!
Andreas Marek's avatar
Andreas Marek committed
122
!> \param  matrixCols           local columns of matrix a
123
124
125
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param wantDebug             logical, more debug information on failure
126
!> \param result                logical, reports success or failure
127
128

  interface invert_trm_real
129
    module procedure elpa_invert_trm_real_double
130
131
  end interface

132
!> \brief  old, deprecated interface cholesky_complex: Cholesky factorization of a double-precision complex hermitian matrix
133
134
135
136
!> \details
!> \param  na                   Order of matrix
!> \param  a(lda,matrixCols)    Distributed matrix which should be factorized.
!>                              Distribution is like in Scalapack.
Andreas Marek's avatar
Andreas Marek committed
137
!>                              Only upper triangle needs to be set.
138
139
140
141
!>                              On return, the upper triangle contains the Cholesky factor
!>                              and the lower triangle is set to 0.
!> \param  lda                  Leading dimension of a
!> \param  nblk                 blocksize of cyclic distribution, must be the same in both directions!
Andreas Marek's avatar
Andreas Marek committed
142
!> \param  matrixCols           local columns of matrix a
143
144
145
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param wantDebug             logical, more debug information on failure
146
!> \result succes               logical, reports success or failure
147
148
149


  interface cholesky_complex
150
    module procedure elpa_cholesky_real_double
151
152
  end interface

153
!> \brief  old, deprecated interface invert_trm_complex: Inverts a double-precision complex upper triangular matrix
154
155
156
157
!> \details
!> \param  na                   Order of matrix
!> \param  a(lda,matrixCols)    Distributed matrix which should be inverted
!>                              Distribution is like in Scalapack.
Andreas Marek's avatar
Andreas Marek committed
158
!>                              Only upper triangle needs to be set.
159
160
161
!>                              The lower triangle is not referenced.
!> \param  lda                  Leading dimension of a
!> \param  nblk                 blocksize of cyclic distribution, must be the same in both directions!
Andreas Marek's avatar
Andreas Marek committed
162
!> \param  matrixCols           local columns of matrix a
163
164
165
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param wantDebug             logical, more debug information on failure
166
!> \result succes               logical, reports success or failure
167
168

  interface invert_trm_complex
169
    module procedure elpa_invert_trm_complex_double
170
171
  end interface

172
!> \brief  mult_at_b_real: Performs C : = A**T * B for double matrices
173
!> this is the old, deprecated interface for the newer elpa_mult_at_b_real
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
!>         where   A is a square matrix (na,na) which is optionally upper or lower triangular
!>                 B is a (na,ncb) matrix
!>                 C is a (na,ncb) matrix where optionally only the upper or lower
!>                   triangle may be computed
!> \details

!> \param  uplo_a               'U' if A is upper triangular
!>                              'L' if A is lower triangular
!>                              anything else if A is a full matrix
!>                              Please note: This pertains to the original A (as set in the calling program)
!>                                           whereas the transpose of A is used for calculations
!>                              If uplo_a is 'U' or 'L', the other triangle is not used at all,
!>                              i.e. it may contain arbitrary numbers
!> \param uplo_c                'U' if only the upper diagonal part of C is needed
!>                              'L' if only the upper diagonal part of C is needed
!>                              anything else if the full matrix C is needed
!>                              Please note: Even when uplo_c is 'U' or 'L', the other triangle may be
!>                                            written to a certain extent, i.e. one shouldn't rely on the content there!
!> \param na                    Number of rows/columns of A, number of rows of B and C
!> \param ncb                   Number of columns  of B and C
!> \param a                     matrix a
!> \param lda                   leading dimension of matrix a
!> \param b                     matrix b
!> \param ldb                   leading dimension of matrix b
!> \param nblk                  blocksize of cyclic distribution, must be the same in both directions!
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param c                     matrix c
!> \param ldc                   leading dimension of matrix c
  interface mult_at_b_real
204
    module procedure elpa_mult_at_b_real_double
205
206
  end interface

207
!> \brief  Old, deprecated interface mult_ah_b_complex: Performs C : = A**H * B for double-precision matrices
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
!>         where   A is a square matrix (na,na) which is optionally upper or lower triangular
!>                 B is a (na,ncb) matrix
!>                 C is a (na,ncb) matrix where optionally only the upper or lower
!>                   triangle may be computed
!> \details
!>
!> \param  uplo_a               'U' if A is upper triangular
!>                              'L' if A is lower triangular
!>                              anything else if A is a full matrix
!>                              Please note: This pertains to the original A (as set in the calling program)
!>                                           whereas the transpose of A is used for calculations
!>                              If uplo_a is 'U' or 'L', the other triangle is not used at all,
!>                              i.e. it may contain arbitrary numbers
!> \param uplo_c                'U' if only the upper diagonal part of C is needed
!>                              'L' if only the upper diagonal part of C is needed
!>                              anything else if the full matrix C is needed
!>                              Please note: Even when uplo_c is 'U' or 'L', the other triangle may be
!>                                            written to a certain extent, i.e. one shouldn't rely on the content there!
!> \param na                    Number of rows/columns of A, number of rows of B and C
!> \param ncb                   Number of columns  of B and C
!> \param a                     matrix a
!> \param lda                   leading dimension of matrix a
!> \param b                     matrix b
!> \param ldb                   leading dimension of matrix b
!> \param nblk                  blocksize of cyclic distribution, must be the same in both directions!
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param c                     matrix c
!> \param ldc                   leading dimension of matrix c
  interface mult_ah_b_complex
238
    module procedure elpa_mult_ah_b_complex_double
239
240
241
  end interface


242
!> \brief  solve_tridi: Old, deprecated interface to solve a double-precision tridiagonal eigensystem for a double-precision matrix with divide and conquer method
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
!> \details
!>
!> \param na                    Matrix dimension
!> \param nev                   number of eigenvalues/vectors to be computed
!> \param d                     array d(na) on input diagonal elements of tridiagonal matrix, on
!>                              output the eigenvalues in ascending order
!> \param e                     array e(na) on input subdiagonal elements of matrix, on exit destroyed
!> \param q                     on exit : matrix q(ldq,matrixCols) contains the eigenvectors
!> \param ldq                   leading dimension of matrix q
!> \param nblk                  blocksize of cyclic distribution, must be the same in both directions!
!> \param matrixCols            columns of matrix q
!> \param mpi_comm_rows         MPI communicator for rows
!> \param mpi_comm_cols         MPI communicator for columns
!> \param wantDebug             logical, give more debug information if .true.
!> \result success              logical, .true. on success, else .false.
  interface solve_tridi
259
    module procedure elpa_solve_tridi_double
260
  end interface
261

262
263
  contains

264
!> \brief  cholesky_real_double: Cholesky factorization of a double-precision real symmetric matrix
265
!> \details
266
267
268
269
!>
!> \param  na                   Order of matrix
!> \param  a(lda,matrixCols)    Distributed matrix which should be factorized.
!>                              Distribution is like in Scalapack.
Andreas Marek's avatar
Andreas Marek committed
270
!>                              Only upper triangle needs to be set.
271
272
273
274
!>                              On return, the upper triangle contains the Cholesky factor
!>                              and the lower triangle is set to 0.
!> \param  lda                  Leading dimension of a
!> \param  nblk                 blocksize of cyclic distribution, must be the same in both directions!
275
!> \param  matrixCols           local columns of matrix a
276
277
278
279
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param wantDebug             logical, more debug information on failure
!> \param succes                logical, reports success or failure
280
281
282
283
284
285

#undef DOUBLE_PRECISION_REAL
#undef REAL_DATATYPE
#define DOUBLE_PRECISION_REAL 1
#define REAL_DATATYPE rk8

286
287
   function elpa_cholesky_real_double(na, a, lda, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols, &
                                            wantDebug) result(success)
288
289
290
     use elpa1_compute
     use elpa_utilities
     use elpa_mpi
291
#ifdef HAVE_DETAILED_TIMINGS
292
     use timings
293
#endif
294
     use precision
295
296
297
      implicit none

      integer(kind=ik)              :: na, lda, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols
298
299
#ifdef USE_ASSUMED_SIZE
      real(kind=REAL_DATATYPE)      :: a(lda,*)
300
#else
301
      real(kind=REAL_DATATYPE)      :: a(lda,matrixCols)
302
#endif
303
304
305
306
307
308
      integer(kind=ik)              :: my_prow, my_pcol, np_rows, np_cols, mpierr
      integer(kind=ik)              :: l_cols, l_rows, l_col1, l_row1, l_colx, l_rowx
      integer(kind=ik)              :: n, nc, i, info
      integer(kind=ik)              :: lcs, lce, lrs, lre
      integer(kind=ik)              :: tile_size, l_rows_tile, l_cols_tile

309
      real(kind=REAL_DATATYPE), allocatable    :: tmp1(:), tmp2(:,:), tmatr(:,:), tmatc(:,:)
310
311

      logical, intent(in)           :: wantDebug
312
      logical                       :: success
313
314
315
316
      integer(kind=ik)              :: istat
      character(200)                :: errorMessage

#ifdef HAVE_DETAILED_TIMINGS
317
#ifdef DOUBLE_PRECISION_REAL
318
      call timer%start("elpa_cholesky_real_double")
319
#else
320
      call timer%start("elpa_cholesky_real_single")
321
#endif
322
323
324
325
#endif

#ifdef HAVE_DETAILED_TIMINGS
   call timer%start("mpi_communication")
326
327
328
329
330
#endif
      call mpi_comm_rank(mpi_comm_rows,my_prow,mpierr)
      call mpi_comm_size(mpi_comm_rows,np_rows,mpierr)
      call mpi_comm_rank(mpi_comm_cols,my_pcol,mpierr)
      call mpi_comm_size(mpi_comm_cols,np_cols,mpierr)
331
332
333
#ifdef HAVE_DETAILED_TIMINGS
   call timer%stop("mpi_communication")
#endif
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
      success = .true.

      ! Matrix is split into tiles; work is done only for tiles on the diagonal or above

      tile_size = nblk*least_common_multiple(np_rows,np_cols) ! minimum global tile size
      tile_size = ((128*max(np_rows,np_cols)-1)/tile_size+1)*tile_size ! make local tiles at least 128 wide

      l_rows_tile = tile_size/np_rows ! local rows of a tile
      l_cols_tile = tile_size/np_cols ! local cols of a tile

      l_rows = local_index(na, my_prow, np_rows, nblk, -1) ! Local rows of a
      l_cols = local_index(na, my_pcol, np_cols, nblk, -1) ! Local cols of a

      allocate(tmp1(nblk*nblk), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
349
        print *,"elpa_cholesky_real: error when allocating tmp1 "//errorMessage
350
351
352
353
354
        stop
      endif

      allocate(tmp2(nblk,nblk), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
355
        print *,"elpa_cholesky_real: error when allocating tmp2 "//errorMessage
356
357
358
359
360
361
362
363
        stop
      endif

      tmp1 = 0
      tmp2 = 0

      allocate(tmatr(l_rows,nblk), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
364
        print *,"elpa_cholesky_real: error when allocating tmatr "//errorMessage
365
366
367
368
369
        stop
      endif

      allocate(tmatc(l_cols,nblk), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
370
        print *,"elpa_cholesky_real: error when allocating tmatc "//errorMessage
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
        stop
      endif

      tmatr = 0
      tmatc = 0

      do n = 1, na, nblk
        ! Calculate first local row and column of the still remaining matrix
        ! on the local processor

        l_row1 = local_index(n, my_prow, np_rows, nblk, +1)
        l_col1 = local_index(n, my_pcol, np_cols, nblk, +1)

        l_rowx = local_index(n+nblk, my_prow, np_rows, nblk, +1)
        l_colx = local_index(n+nblk, my_pcol, np_cols, nblk, +1)

        if (n+nblk > na) then

          ! This is the last step, just do a Cholesky-Factorization
          ! of the remaining block

          if (my_prow==prow(n, nblk, np_rows) .and. my_pcol==pcol(n, nblk, np_cols)) then
393
394
395
396
397
#ifdef DOUBLE_PRECISION_REAL
            call dpotrf('U', na-n+1, a(l_row1,l_col1), lda, info)
#else
            call spotrf('U', na-n+1, a(l_row1,l_col1), lda, info)
#endif
398
            if (info/=0) then
399
              if (wantDebug) write(error_unit,*) "elpa_cholesky_real: Error in dpotrf 1: ",info
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
              success = .false.
              return
            endif

          endif

          exit ! Loop

        endif

        if (my_prow==prow(n, nblk, np_rows)) then

          if (my_pcol==pcol(n, nblk, np_cols)) then

            ! The process owning the upper left remaining block does the
            ! Cholesky-Factorization of this block
416
417
418
419
420
#ifdef DOUBLE_PRECISION_REAL
            call dpotrf('U', nblk, a(l_row1,l_col1), lda, info)
#else
            call spotrf('U', nblk, a(l_row1,l_col1), lda, info)
#endif
421
            if (info/=0) then
422
              if (wantDebug) write(error_unit,*) "elpa_cholesky_real: Error in dpotrf 2: ",info
423
424
425
426
427
428
429
430
431
432
433
              success = .false.
              return
            endif

            nc = 0
            do i=1,nblk
              tmp1(nc+1:nc+i) = a(l_row1:l_row1+i-1,l_col1+i-1)
              nc = nc+i
            enddo
          endif
#ifdef WITH_MPI
434
435
436
#ifdef HAVE_DETAILED_TIMINGS
          call timer%start("mpi_communication")
#endif
437
438
439
440
441

#ifdef DOUBLE_PRECISION_REAL
          call MPI_Bcast(tmp1, nblk*(nblk+1)/2, MPI_REAL8, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#else
          call MPI_Bcast(tmp1, nblk*(nblk+1)/2, MPI_REAL4, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
442
#endif
443

444
445
446
447
#ifdef HAVE_DETAILED_TIMINGS
          call timer%stop("mpi_communication")
#endif

448
#endif /* WITH_MPI */
449
450
451
452
453
454
455
          nc = 0
          do i=1,nblk
            tmp2(1:i,i) = tmp1(nc+1:nc+i)
            nc = nc+i
          enddo

          if (l_cols-l_colx+1>0) &
456
457
458
459
460
#ifdef DOUBLE_PRECISION_REAL
              call dtrsm('L', 'U', 'T', 'N', nblk, l_cols-l_colx+1, 1.0_rk8, tmp2, ubound(tmp2,dim=1), a(l_row1,l_colx), lda)
#else
              call strsm('L', 'U', 'T', 'N', nblk, l_cols-l_colx+1, 1.0_rk4, tmp2, ubound(tmp2,dim=1), a(l_row1,l_colx), lda)
#endif
461
462
463
464
465
466
        endif

        do i=1,nblk

          if (my_prow==prow(n, nblk, np_rows)) tmatc(l_colx:l_cols,i) = a(l_row1+i-1,l_colx:l_cols)
#ifdef WITH_MPI
467
468
469
470

#ifdef HAVE_DETAILED_TIMINGS
   call timer%start("mpi_communication")
#endif
471
          if (l_cols-l_colx+1>0) &
472
473
474
475
#ifdef DOUBLE_PRECISION_REAL
              call MPI_Bcast(tmatc(l_colx,i), l_cols-l_colx+1, MPI_REAL8, prow(n, nblk, np_rows), mpi_comm_rows, mpierr)
#else
              call MPI_Bcast(tmatc(l_colx,i), l_cols-l_colx+1, MPI_REAL4, prow(n, nblk, np_rows), mpi_comm_rows, mpierr)
476
#endif
477

478
479
480
#ifdef HAVE_DETAILED_TIMINGS
          call timer%stop("mpi_communication")
#endif
481
#endif /* WITH_MPI */
482
483
        enddo
        ! this has to be checked since it was changed substantially when doing type safe
484
485
486
487
488
489
#ifdef DOUBLE_PRECISION_REAL
        call elpa_transpose_vectors_real_double  (tmatc, ubound(tmatc,dim=1), mpi_comm_cols, &
                                      tmatr, ubound(tmatr,dim=1), mpi_comm_rows, &
                                      n, na, nblk, nblk)
#else
        call elpa_transpose_vectors_real_single  (tmatc, ubound(tmatc,dim=1), mpi_comm_cols, &
490
491
                                      tmatr, ubound(tmatr,dim=1), mpi_comm_rows, &
                                      n, na, nblk, nblk)
492
#endif
493
494
495
496
497
498
499

        do i=0,(na-1)/tile_size
          lcs = max(l_colx,i*l_cols_tile+1)
          lce = min(l_cols,(i+1)*l_cols_tile)
          lrs = l_rowx
          lre = min(l_rows,(i+1)*l_rows_tile)
          if (lce<lcs .or. lre<lrs) cycle
500
501
502
503
504
505
506
507
508
#ifdef DOUBLE_PRECISION_REAL
          call DGEMM('N', 'T', lre-lrs+1, lce-lcs+1, nblk, -1.0_rk8,                        &
                     tmatr(lrs,1), ubound(tmatr,dim=1), tmatc(lcs,1), ubound(tmatc,dim=1), &
                     1.0_rk8, a(lrs,lcs), lda)
#else
          call SGEMM('N', 'T', lre-lrs+1, lce-lcs+1, nblk, -1.0_rk4,                        &
                     tmatr(lrs,1), ubound(tmatr,dim=1), tmatc(lcs,1), ubound(tmatc,dim=1), &
                     1.0_rk4, a(lrs,lcs), lda)
#endif
509
510
511
512
513
514
        enddo

      enddo

      deallocate(tmp1, tmp2, tmatr, tmatc, stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
515
        print *,"elpa_cholesky_real: error when deallocating tmp1 "//errorMessage
516
517
518
519
520
521
522
523
524
525
526
527
528
529
        stop
      endif

      ! Set the lower triangle to 0, it contains garbage (form the above matrix multiplications)

      do i=1,na
        if (my_pcol==pcol(i, nblk, np_cols)) then
          ! column i is on local processor
          l_col1 = local_index(i  , my_pcol, np_cols, nblk, +1) ! local column number
          l_row1 = local_index(i+1, my_prow, np_rows, nblk, +1) ! first row below diagonal
          a(l_row1:l_rows,l_col1) = 0
        endif
      enddo
#ifdef HAVE_DETAILED_TIMINGS
530
#ifdef DOUBLE_PRECISION_REAL
531
      call timer%stop("elpa_cholesky_real_double")
532
#else
533
      call timer%stop("elpa_cholesky_real_single")
534
#endif
535
536
#endif

537
    end function elpa_cholesky_real_double
538

539
540
541
542
#ifdef WANT_SINGLE_PRECISION_REAL
#undef DOUBLE_PRECISION_REAL
#undef REAL_DATATYPE
#define REAL_DATATYPE rk4
543

544
!> \brief  cholesky_real_single: Cholesky factorization of a single-precision real symmetric matrix
545
!> \details
546
!>
547
!> \param  na                   Order of matrix
548
!> \param  a(lda,matrixCols)    Distributed matrix which should be factorized.
549
!>                              Distribution is like in Scalapack.
Andreas Marek's avatar
Andreas Marek committed
550
!>                              Only upper triangle needs to be set.
551
552
!>                              On return, the upper triangle contains the Cholesky factor
!>                              and the lower triangle is set to 0.
553
554
!> \param  lda                  Leading dimension of a
!> \param  nblk                 blocksize of cyclic distribution, must be the same in both directions!
Andreas Marek's avatar
Andreas Marek committed
555
!> \param  matrixCols           local columns of matrix a
556
557
558
559
560
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param wantDebug             logical, more debug information on failure
!> \param succes                logical, reports success or failure

561
562
   function elpa_cholesky_real_single(na, a, lda, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols, &
                                            wantDebug) result(success)
563
564
565
566
     use elpa1_compute
     use elpa_utilities
     use elpa_mpi
#ifdef HAVE_DETAILED_TIMINGS
567
     use timings
568
#endif
569
570
571
     use precision

     implicit none
572

573
      integer(kind=ik)              :: na, lda, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols
574
575
576
577
578
#ifdef USE_ASSUMED_SIZE
      real(kind=REAL_DATATYPE)      :: a(lda,*)
#else
      real(kind=REAL_DATATYPE)      :: a(lda,matrixCols)
#endif
579
580
581
582
583
      integer(kind=ik)              :: my_prow, my_pcol, np_rows, np_cols, mpierr
      integer(kind=ik)              :: l_cols, l_rows, l_col1, l_row1, l_colx, l_rowx
      integer(kind=ik)              :: n, nc, i, info
      integer(kind=ik)              :: lcs, lce, lrs, lre
      integer(kind=ik)              :: tile_size, l_rows_tile, l_cols_tile
584

585
      real(kind=REAL_DATATYPE), allocatable    :: tmp1(:), tmp2(:,:), tmatr(:,:), tmatc(:,:)
586

587
      logical, intent(in)           :: wantDebug
588
      logical                       :: success
589
590
      integer(kind=ik)              :: istat
      character(200)                :: errorMessage
591

592
593
#ifdef HAVE_DETAILED_TIMINGS
#ifdef DOUBLE_PRECISION_REAL
594
      call timer%start("elpa_cholesky_real_double")
595
#else
596
      call timer%start("elpa_cholesky_real_single")
597
598
#endif
#endif
599
600
601
602
603

#ifdef HAVE_DETAILED_TIMINGS
      call timer%start("mpi_communication")
#endif

604
605
606
607
      call mpi_comm_rank(mpi_comm_rows,my_prow,mpierr)
      call mpi_comm_size(mpi_comm_rows,np_rows,mpierr)
      call mpi_comm_rank(mpi_comm_cols,my_pcol,mpierr)
      call mpi_comm_size(mpi_comm_cols,np_cols,mpierr)
608
609
610
611
612

#ifdef HAVE_DETAILED_TIMINGS
      call timer%stop("mpi_communication")
#endif

613
      success = .true.
614

615
      ! Matrix is split into tiles; work is done only for tiles on the diagonal or above
616

617
618
      tile_size = nblk*least_common_multiple(np_rows,np_cols) ! minimum global tile size
      tile_size = ((128*max(np_rows,np_cols)-1)/tile_size+1)*tile_size ! make local tiles at least 128 wide
619
620
621
622
623
624
625
626
627

      l_rows_tile = tile_size/np_rows ! local rows of a tile
      l_cols_tile = tile_size/np_cols ! local cols of a tile

      l_rows = local_index(na, my_prow, np_rows, nblk, -1) ! Local rows of a
      l_cols = local_index(na, my_pcol, np_cols, nblk, -1) ! Local cols of a

      allocate(tmp1(nblk*nblk), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
628
        print *,"elpa_cholesky_real: error when allocating tmp1 "//errorMessage
629
630
631
632
633
        stop
      endif

      allocate(tmp2(nblk,nblk), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
634
        print *,"elpa_cholesky_real: error when allocating tmp2 "//errorMessage
635
636
637
638
639
640
641
642
        stop
      endif

      tmp1 = 0
      tmp2 = 0

      allocate(tmatr(l_rows,nblk), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
643
        print *,"elpa_cholesky_real: error when allocating tmatr "//errorMessage
644
645
646
647
648
        stop
      endif

      allocate(tmatc(l_cols,nblk), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
649
        print *,"elpa_cholesky_real: error when allocating tmatc "//errorMessage
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
        stop
      endif

      tmatr = 0
      tmatc = 0

      do n = 1, na, nblk

        ! Calculate first local row and column of the still remaining matrix
        ! on the local processor

        l_row1 = local_index(n, my_prow, np_rows, nblk, +1)
        l_col1 = local_index(n, my_pcol, np_cols, nblk, +1)

        l_rowx = local_index(n+nblk, my_prow, np_rows, nblk, +1)
        l_colx = local_index(n+nblk, my_pcol, np_cols, nblk, +1)

        if (n+nblk > na) then

          ! This is the last step, just do a Cholesky-Factorization
          ! of the remaining block

          if (my_prow==prow(n, nblk, np_rows) .and. my_pcol==pcol(n, nblk, np_cols)) then
673
674
675
676
677
#ifdef DOUBLE_PRECISION_REAL
            call dpotrf('U', na-n+1, a(l_row1,l_col1), lda, info)
#else
            call spotrf('U', na-n+1, a(l_row1,l_col1), lda, info)
#endif
678
            if (info/=0) then
679
              if (wantDebug) write(error_unit,*) "elpa_cholesky_real: Error in dpotrf"
680
681
682
683
684
685
686
              success = .false.
              return
            endif

          endif

          exit ! Loop
687

688
689
690
691
692
693
694
695
        endif

        if (my_prow==prow(n, nblk, np_rows)) then

          if (my_pcol==pcol(n, nblk, np_cols)) then

            ! The process owning the upper left remaining block does the
            ! Cholesky-Factorization of this block
696
697
698
699
700
#ifdef DOUBLE_PRECISION_REAL
            call dpotrf('U', nblk, a(l_row1,l_col1), lda, info)
#else
            call spotrf('U', nblk, a(l_row1,l_col1), lda, info)
#endif
701
            if (info/=0) then
702
              if (wantDebug) write(error_unit,*) "elpa_cholesky_real: Error in dpotrf"
703
704
705
706
707
708
709
710
711
712
713
              success = .false.
              return
            endif

            nc = 0
            do i=1,nblk
              tmp1(nc+1:nc+i) = a(l_row1:l_row1+i-1,l_col1+i-1)
              nc = nc+i
            enddo
          endif
#ifdef WITH_MPI
714
715
716
#ifdef HAVE_DETAILED_TIMINGS
          call timer%start("mpi_communication")
#endif
717
718
719
720
721

#ifdef DOUBLE_PRECISION_REAL
          call MPI_Bcast(tmp1, nblk*(nblk+1)/2, MPI_REAL8, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#else
          call MPI_Bcast(tmp1, nblk*(nblk+1)/2, MPI_REAL4, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
722
#endif
723
724
725
#ifdef HAVE_DETAILED_TIMINGS
          call timer%stop("mpi_communication")
#endif
726
#endif /* WITH_MPI */
727
728
729
730
731
732
733
          nc = 0
          do i=1,nblk
            tmp2(1:i,i) = tmp1(nc+1:nc+i)
            nc = nc+i
          enddo

          if (l_cols-l_colx+1>0) &
734
735
736
737
738
#ifdef DOUBLE_PRECISION_REAL
              call dtrsm('L', 'U', 'T', 'N', nblk, l_cols-l_colx+1, 1.0_rk8, tmp2, ubound(tmp2,dim=1), a(l_row1,l_colx), lda)
#else
              call strsm('L', 'U', 'T', 'N', nblk, l_cols-l_colx+1, 1.0_rk4, tmp2, ubound(tmp2,dim=1), a(l_row1,l_colx), lda)
#endif
739
740
741
742
        endif

        do i=1,nblk

743
          if (my_prow==prow(n, nblk, np_rows)) tmatc(l_colx:l_cols,i) = a(l_row1+i-1,l_colx:l_cols)
744
#ifdef WITH_MPI
745
746
747
#ifdef HAVE_DETAILED_TIMINGS
          call timer%start("mpi_communication")
#endif
748
          if (l_cols-l_colx+1>0) &
749
750
751
752
#ifdef DOUBLE_PRECISION_REAL
              call MPI_Bcast(tmatc(l_colx,i), l_cols-l_colx+1, MPI_REAL8, prow(n, nblk, np_rows), mpi_comm_rows, mpierr)
#else
              call MPI_Bcast(tmatc(l_colx,i), l_cols-l_colx+1, MPI_REAL4, prow(n, nblk, np_rows), mpi_comm_rows, mpierr)
753
#endif
754
755
756
#ifdef HAVE_DETAILED_TIMINGS
          call timer%stop("mpi_communication")
#endif
757
#endif /* WITH_MPI */
758
759
        enddo
        ! this has to be checked since it was changed substantially when doing type safe
760
761
762
763
764
765
766
767
768
769
#ifdef DOUBLE_PRECISION_REAL
        call elpa_transpose_vectors_real_double  (tmatc, ubound(tmatc,dim=1), mpi_comm_cols, &
                                      tmatr, ubound(tmatr,dim=1), mpi_comm_rows, &
                                      n, na, nblk, nblk)
#else
        call elpa_transpose_vectors_real_single  (tmatc, ubound(tmatc,dim=1), mpi_comm_cols, &
                                      tmatr, ubound(tmatr,dim=1), mpi_comm_rows, &
                                      n, na, nblk, nblk)
#endif

770
771
772
773
774
775
        do i=0,(na-1)/tile_size
          lcs = max(l_colx,i*l_cols_tile+1)
          lce = min(l_cols,(i+1)*l_cols_tile)
          lrs = l_rowx
          lre = min(l_rows,(i+1)*l_rows_tile)
          if (lce<lcs .or. lre<lrs) cycle
776
777
778
779
780
781
782
783
784
#ifdef DOUBLE_PRECISION_REAL
          call DGEMM('N', 'T', lre-lrs+1, lce-lcs+1, nblk, -1.0_rk8,                        &
                     tmatr(lrs,1), ubound(tmatr,dim=1), tmatc(lcs,1), ubound(tmatc,dim=1), &
                     1.0_rk8, a(lrs,lcs), lda)
#else
          call SGEMM('N', 'T', lre-lrs+1, lce-lcs+1, nblk, -1.0_rk4,                        &
                     tmatr(lrs,1), ubound(tmatr,dim=1), tmatc(lcs,1), ubound(tmatc,dim=1), &
                     1.0_rk4, a(lrs,lcs), lda)
#endif
785
786
787
788
789
790
        enddo

      enddo

      deallocate(tmp1, tmp2, tmatr, tmatc, stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
791
        print *,"elpa_cholesky_real: error when deallocating tmp1 "//errorMessage
792
793
794
795
796
797
798
799
800
801
802
803
804
805
        stop
      endif

      ! Set the lower triangle to 0, it contains garbage (form the above matrix multiplications)

      do i=1,na
        if (my_pcol==pcol(i, nblk, np_cols)) then
          ! column i is on local processor
          l_col1 = local_index(i  , my_pcol, np_cols, nblk, +1) ! local column number
          l_row1 = local_index(i+1, my_prow, np_rows, nblk, +1) ! first row below diagonal
          a(l_row1:l_rows,l_col1) = 0
        endif
      enddo
#ifdef HAVE_DETAILED_TIMINGS
806
#ifdef DOUBLE_PRECISION_REAL
807
      call timer%stop("elpa_cholesky_real_double")
808
#else
809
      call timer%stop("elpa_cholesky_real_single")
810
#endif
811
812
#endif

813
    end function elpa_cholesky_real_single
814

815
#endif /* WANT_SINGLE_PRECSION_REAL */
816

817
818
819
820
821
#undef DOUBLE_PRECISION_REAL
#undef REAL_DATATYPE
#define DOUBLE_PRECISION_REAL 1
#define REAL_DATATYPE rk8
!> \brief  elpa_invert_trm_real_double: Inverts a double-precision real upper triangular matrix
822
!> \details
823
824
825
!> \param  na                   Order of matrix
!> \param  a(lda,matrixCols)    Distributed matrix which should be inverted
!>                              Distribution is like in Scalapack.
Andreas Marek's avatar
Andreas Marek committed
826
!>                              Only upper triangle needs to be set.
827
828
829
!>                              The lower triangle is not referenced.
!> \param  lda                  Leading dimension of a
!> \param  nblk                 blocksize of cyclic distribution, must be the same in both directions!
Andreas Marek's avatar
Andreas Marek committed
830
!> \param  matrixCols           local columns of matrix a
831
832
833
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param wantDebug             logical, more debug information on failure
834
835
!> \result succes               logical, reports success or failure
    function elpa_invert_trm_real_double(na, a, lda, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols, wantDebug) result(success)
836
837
838
839
       use precision
       use elpa1_compute
       use elpa_utilities
       use elpa_mpi
840
841
842
#ifdef HAVE_DETAILED_TIMINGS
       use timings
#endif
843
844
       implicit none

845
       integer(kind=ik)             :: na, lda, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols
846
847
#ifdef USE_ASSUMED_SIZE
       real(kind=REAL_DATATYPE)     :: a(lda,*)
848
#else
849
       real(kind=REAL_DATATYPE)     :: a(lda,matrixCols)
850
#endif
851
852
853
       integer(kind=ik)             :: my_prow, my_pcol, np_rows, np_cols, mpierr
       integer(kind=ik)             :: l_cols, l_rows, l_col1, l_row1, l_colx, l_rowx
       integer(kind=ik)             :: n, nc, i, info, ns, nb
854

855
       real(kind=REAL_DATATYPE), allocatable   :: tmp1(:), tmp2(:,:), tmat1(:,:), tmat2(:,:)
856

857
       logical, intent(in)          :: wantDebug
858
       logical                      :: success
859
860
       integer(kind=ik)             :: istat
       character(200)               :: errorMessage
861

862
863
864
865
#ifdef HAVE_DETAILED_TIMINGS
   call timer%start("mpi_communication")
#endif

866
867
868
869
       call mpi_comm_rank(mpi_comm_rows,my_prow,mpierr)
       call mpi_comm_size(mpi_comm_rows,np_rows,mpierr)
       call mpi_comm_rank(mpi_comm_cols,my_pcol,mpierr)
       call mpi_comm_size(mpi_comm_cols,np_cols,mpierr)
870
871
872
#ifdef HAVE_DETAILED_TIMINGS
   call timer%stop("mpi_communication")
#endif
873
874
875
876
877
878
879
       success = .true.

       l_rows = local_index(na, my_prow, np_rows, nblk, -1) ! Local rows of a
       l_cols = local_index(na, my_pcol, np_cols, nblk, -1) ! Local cols of a

       allocate(tmp1(nblk*nblk), stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
880
         print *,"elpa_invert_trm_real: error when allocating tmp1 "//errorMessage
881
882
883
884
885
         stop
       endif

       allocate(tmp2(nblk,nblk), stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
886
         print *,"elpa_invert_trm_real: error when allocating tmp2 "//errorMessage
887
888
889
890
891
892
893
894
         stop
       endif

       tmp1 = 0
       tmp2 = 0

       allocate(tmat1(l_rows,nblk), stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
895
         print *,"elpa_invert_trm_real: error when allocating tmat1 "//errorMessage
896
897
898
899
900
         stop
       endif

       allocate(tmat2(nblk,l_cols), stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
901
         print *,"elpa_invert_trm_real: error when allocating tmat2 "//errorMessage
902
903
904
         stop
       endif

905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
       tmat1 = 0
       tmat2 = 0


       ns = ((na-1)/nblk)*nblk + 1

       do n = ns,1,-nblk

         l_row1 = local_index(n, my_prow, np_rows, nblk, +1)
         l_col1 = local_index(n, my_pcol, np_cols, nblk, +1)

         nb = nblk
         if (na-n+1 < nblk) nb = na-n+1

         l_rowx = local_index(n+nb, my_prow, np_rows, nblk, +1)
         l_colx = local_index(n+nb, my_pcol, np_cols, nblk, +1)

         if (my_prow==prow(n, nblk, np_rows)) then

           if (my_pcol==pcol(n, nblk, np_cols)) then
#ifdef DOUBLE_PRECISION_REAL
             call DTRTRI('U', 'N', nb, a(l_row1,l_col1), lda, info)
#else
             call STRTRI('U', 'N', nb, a(l_row1,l_col1), lda, info)
#endif
             if (info/=0) then
931
               if (wantDebug) write(error_unit,*) "elpa_invert_trm_real: Error in DTRTRI"
932
933
934
935
936
937
938
939
940
941
942
               success = .false.
               return
             endif

             nc = 0
             do i=1,nb
               tmp1(nc+1:nc+i) = a(l_row1:l_row1+i-1,l_col1+i-1)
               nc = nc+i
             enddo
           endif
#ifdef WITH_MPI
943
944
945
#ifdef HAVE_DETAILED_TIMINGS
           call timer%start("mpi_communication")
#endif
946
947
948
949
950
#ifdef DOUBLE_PRECISION_REAL
           call MPI_Bcast(tmp1, nb*(nb+1)/2, MPI_REAL8, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#else
           call MPI_Bcast(tmp1, nb*(nb+1)/2, MPI_REAL4, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#endif
951
952
953
#ifdef HAVE_DETAILED_TIMINGS
           call timer%stop("mpi_communication")
#endif
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
#endif /* WITH_MPI */
           nc = 0
           do i=1,nb
             tmp2(1:i,i) = tmp1(nc+1:nc+i)
             nc = nc+i
           enddo

           if (l_cols-l_colx+1>0) &
#ifdef DOUBLE_PRECISION_REAL
               call DTRMM('L', 'U', 'N', 'N', nb, l_cols-l_colx+1, 1.0_rk8, tmp2, ubound(tmp2,dim=1), a(l_row1,l_colx), lda)
#else
               call STRMM('L', 'U', 'N', 'N', nb, l_cols-l_colx+1, 1.0_rk4, tmp2, ubound(tmp2,dim=1), a(l_row1,l_colx), lda)
#endif
           if (l_colx<=l_cols)   tmat2(1:nb,l_colx:l_cols) = a(l_row1:l_row1+nb-1,l_colx:l_cols)
           if (my_pcol==pcol(n, nblk, np_cols)) tmat2(1:nb,l_col1:l_col1+nb-1) = tmp2(1:nb,1:nb) ! tmp2 has the lower left triangle 0

         endif

         if (l_row1>1) then
           if (my_pcol==pcol(n, nblk, np_cols)) then
             tmat1(1:l_row1-1,1:nb) = a(1:l_row1-1,l_col1:l_col1+nb-1)
             a(1:l_row1-1,l_col1:l_col1+nb-1) = 0
           endif

           do i=1,nb
#ifdef WITH_MPI
980
981
982
#ifdef HAVE_DETAILED_TIMINGS
             call timer%start("mpi_communication")
#endif
983
984
985
986
987
#ifdef DOUBLE_PRECISION_REAL
             call MPI_Bcast(tmat1(1,i), l_row1-1, MPI_REAL8, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#else
             call MPI_Bcast(tmat1(1,i), l_row1-1, MPI_REAL4, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#endif
988
989
990
#ifdef HAVE_DETAILED_TIMINGS
             call timer%stop("mpi_communication")
#endif
991
992
993
994
#endif /* WITH_MPI */
           enddo
         endif
#ifdef WITH_MPI
995
996
997
#ifdef HAVE_DETAILED_TIMINGS
         call timer%start("mpi_communication")
#endif
998
999
1000
1001
1002
1003
         if (l_cols-l_col1+1>0) &
#ifdef DOUBLE_PRECISION_REAL
            call MPI_Bcast(tmat2(1,l_col1), (l_cols-l_col1+1)*nblk, MPI_REAL8, prow(n, nblk, np_rows), mpi_comm_rows, mpierr)
#else
            call MPI_Bcast(tmat2(1,l_col1), (l_cols-l_col1+1)*nblk, MPI_REAL4, prow(n, nblk, np_rows), mpi_comm_rows, mpierr)
#endif
1004
1005
1006
#ifdef HAVE_DETAILED_TIMINGS
          call timer%stop("mpi_communication")
#endif
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
#endif /* WITH_MPI */
         if (l_row1>1 .and. l_cols-l_col1+1>0) &
#ifdef DOUBLE_PRECISION_REAL
            call dgemm('N', 'N', l_row1-1, l_cols-l_col1+1, nb, -1.0_rk8,                 &
                       tmat1, ubound(tmat1,dim=1), tmat2(1,l_col1), ubound(tmat2,dim=1), &
                       1.0_rk8, a(1,l_col1), lda)
#else
            call sgemm('N', 'N', l_row1-1, l_cols-l_col1+1, nb, -1.0_rk4,                 &
                       tmat1, ubound(tmat1,dim=1), tmat2(1,l_col1), ubound(tmat2,dim=1), &
                       1.0_rk4, a(1,l_col1), lda)
#endif
       enddo

       deallocate(tmp1, tmp2, tmat1, tmat2, stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
1022
         print *,"elpa_invert_trm_real: error when deallocating tmp1 "//errorMessage
1023
1024
         stop
       endif
1025
     end function elpa_invert_trm_real_double
1026
1027
1028
1029
1030

#if WANT_SINGLE_PRECISION_REAL
#undef DOUBLE_PRECISION_REAL
#undef REAL_DATATYPE
#define REAL_DATATYPE rk4
1031
!> \brief  elpa_invert_trm_real_single: Inverts a single-precision real upper triangular matrix
1032
1033
1034
1035
!> \details
!> \param  na                   Order of matrix
!> \param  a(lda,matrixCols)    Distributed matrix which should be inverted
!>                              Distribution is like in Scalapack.
1036
!>                              Only upper triangle needs to be set.
1037
1038
1039
1040
1041
1042
1043
!>                              The lower triangle is not referenced.
!> \param  lda                  Leading dimension of a
!> \param                       matrixCols  local columns of matrix a
!> \param  nblk                 blocksize of cyclic distribution, must be the same in both directions!
!> \param  mpi_comm_rows        MPI communicator for rows
!> \param  mpi_comm_cols        MPI communicator for columns
!> \param wantDebug             logical, more debug information on failure
1044
1045
!> \result succes               logical, reports success or failure
    function elpa_invert_trm_real_single(na, a, lda, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols, wantDebug) result(success)
1046
1047
1048
1049
       use precision
       use elpa1_compute
       use elpa_utilities
       use elpa_mpi
1050
1051
1052
#ifdef HAVE_DETAILED_TIMINGS
       use timings
#endif
1053
1054
1055
       implicit none

       integer(kind=ik)             :: na, lda, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols
1056
1057
#ifdef USE_ASSUMED_SIZE
       real(kind=REAL_DATATYPE)     :: a(lda,*)
1058
#else
1059
       real(kind=REAL_DATATYPE)     :: a(lda,matrixCols)
1060
1061
1062
1063
1064
1065
1066
1067
#endif
       integer(kind=ik)             :: my_prow, my_pcol, np_rows, np_cols, mpierr
       integer(kind=ik)             :: l_cols, l_rows, l_col1, l_row1, l_colx, l_rowx
       integer(kind=ik)             :: n, nc, i, info, ns, nb

       real(kind=REAL_DATATYPE), allocatable   :: tmp1(:), tmp2(:,:), tmat1(:,:), tmat2(:,:)

       logical, intent(in)          :: wantDebug
1068
       logical                      :: success
1069
1070
       integer(kind=ik)             :: istat
       character(200)               :: errorMessage
1071

1072
1073
1074
#ifdef HAVE_DETAILED_TIMINGS
   call timer%start("mpi_communication")
#endif
1075
1076
1077
1078
       call mpi_comm_rank(mpi_comm_rows,my_prow,mpierr)
       call mpi_comm_size(mpi_comm_rows,np_rows,mpierr)
       call mpi_comm_rank(mpi_comm_cols,my_pcol,mpierr)
       call mpi_comm_size(mpi_comm_cols,np_cols,mpierr)
1079
1080
1081
#ifdef HAVE_DETAILED_TIMINGS
   call timer%stop("mpi_communication")
#endif
1082
1083
1084
1085
1086
1087
1088
       success = .true.

       l_rows = local_index(na, my_prow, np_rows, nblk, -1) ! Local rows of a
       l_cols = local_index(na, my_pcol, np_cols, nblk, -1) ! Local cols of a

       allocate(tmp1(nblk*nblk), stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
1089
         print *,"elpa_invert_trm_real: error when allocating tmp1 "//errorMessage
1090
1091
1092
1093
1094
         stop
       endif

       allocate(tmp2(nblk,nblk), stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
1095
         print *,"elpa_invert_trm_real: error when allocating tmp2 "//errorMessage
1096
1097
1098
1099
1100
1101
1102
1103
         stop
       endif

       tmp1 = 0
       tmp2 = 0

       allocate(tmat1(l_rows,nblk), stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
1104
         print *,"elpa_invert_trm_real: error when allocating tmat1 "//errorMessage
1105
1106
1107
1108
1109
         stop
       endif

       allocate(tmat2(nblk,l_cols), stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
1110
         print *,"elpa_invert_trm_real: error when allocating tmat2 "//errorMessage
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
         stop
       endif

       tmat1 = 0
       tmat2 = 0


       ns = ((na-1)/nblk)*nblk + 1

       do n = ns,1,-nblk

         l_row1 = local_index(n, my_prow, np_rows, nblk, +1)
         l_col1 = local_index(n, my_pcol, np_cols, nblk, +1)

         nb = nblk
         if (na-n+1 < nblk) nb = na-n+1

         l_rowx = local_index(n+nb, my_prow, np_rows, nblk, +1)
         l_colx = local_index(n+nb, my_pcol, np_cols, nblk, +1)

         if (my_prow==prow(n, nblk, np_rows)) then

           if (my_pcol==pcol(n, nblk, np_cols)) then
#ifdef DOUBLE_PRECISION_REAL
             call DTRTRI('U', 'N', nb, a(l_row1,l_col1), lda, info)
#else
             call STRTRI('U', 'N', nb, a(l_row1,l_col1), lda, info)
#endif
             if (info/=0) then
1140
               if (wantDebug) write(error_unit,*) "elpa_invert_trm_real: Error in DTRTRI"
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
               success = .false.
               return
             endif

             nc = 0
             do i=1,nb
               tmp1(nc+1:nc+i) = a(l_row1:l_row1+i-1,l_col1+i-1)
               nc = nc+i
             enddo
           endif
#ifdef WITH_MPI
1152
1153
1154
#ifdef HAVE_DETAILED_TIMINGS
           call timer%start("mpi_communication")
#endif
1155
1156
1157
1158
1159
#ifdef DOUBLE_PRECISION_REAL
           call MPI_Bcast(tmp1, nb*(nb+1)/2, MPI_REAL8, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#else
           call MPI_Bcast(tmp1, nb*(nb+1)/2, MPI_REAL4, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#endif
1160
1161
1162
#ifdef HAVE_DETAILED_TIMINGS
           call timer%stop("mpi_communication")
#endif
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
#endif /* WITH_MPI */
           nc = 0
           do i=1,nb
             tmp2(1:i,i) = tmp1(nc+1:nc+i)
             nc = nc+i
           enddo

           if (l_cols-l_colx+1>0) &
#ifdef DOUBLE_PRECISION_REAL
               call DTRMM('L', 'U', 'N', 'N', nb, l_cols-l_colx+1, 1.0_rk8, tmp2, ubound(tmp2,dim=1), a(l_row1,l_colx), lda)
#else
               call STRMM('L', 'U', 'N', 'N', nb, l_cols-l_colx+1, 1.0_rk4, tmp2, ubound(tmp2,dim=1), a(l_row1,l_colx), lda)
#endif
           if (l_colx<=l_cols)   tmat2(1:nb,l_colx:l_cols) = a(l_row1:l_row1+nb-1,l_colx:l_cols)
           if (my_pcol==pcol(n, nblk, np_cols)) tmat2(1:nb,l_col1:l_col1+nb-1) = tmp2(1:nb,1:nb) ! tmp2 has the lower left triangle 0

         endif

         if (l_row1>1) then
           if (my_pcol==pcol(n, nblk, np_cols)) then
             tmat1(1:l_row1-1,1:nb) = a(1:l_row1-1,l_col1:l_col1+nb-1)
             a(1:l_row1-1,l_col1:l_col1+nb-1) = 0
           endif

           do i=1,nb
#ifdef WITH_MPI
1189
1190
1191
#ifdef HAVE_DETAILED_TIMINGS
             call timer%start("mpi_communication")
#endif
1192
1193
1194
1195
1196
#ifdef DOUBLE_PRECISION_REAL
             call MPI_Bcast(tmat1(1,i), l_row1-1, MPI_REAL8, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#else
             call MPI_Bcast(tmat1(1,i), l_row1-1, MPI_REAL4, pcol(n, nblk, np_cols), mpi_comm_cols, mpierr)
#endif
1197
1198
1199
#ifdef HAVE_DETAILED_TIMINGS
             call timer%stop("mpi_communication")
#endif
1200
1201
1202
1203
#endif /* WITH_MPI */
           enddo
         endif
#ifdef WITH_MPI
1204
1205
1206
#ifdef HAVE_DETAILED_TIMINGS
         call timer%start("mpi_communication")
#endif