elpa1_solve_tridi_real_template.X90 24 KB
Newer Older
Pavel Kus's avatar
Pavel Kus committed
1
2
3
4
5
6
7
8
9
10
11
12
13
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
#if 0
!    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,
!    - Max-Plack-Institut für Mathematik in den Naturwissenschaften,
!      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".
#endif

55
56
57
subroutine solve_tridi_&
&PRECISION &
( na, nev, d, e, q, ldq, nblk, matrixCols, mpi_comm_rows, &
Pavel Kus's avatar
Pavel Kus committed
58
59
60
61
                                           mpi_comm_cols, wantDebug, success )

#ifdef HAVE_DETAILED_TIMINGS
      use timings
62
63
#else
      use timings_dummy
Pavel Kus's avatar
Pavel Kus committed
64
65
66
67
#endif
      use precision
      implicit none

68
69
      integer(kind=ik), intent(in)           :: na, nev, ldq, nblk, matrixCols, mpi_comm_rows, mpi_comm_cols
      real(kind=REAL_DATATYPE), intent(inout)   :: d(na), e(na)
Pavel Kus's avatar
Pavel Kus committed
70
#ifdef USE_ASSUMED_SIZE
71
      real(kind=REAL_DATATYPE), intent(inout)   :: q(ldq,*)
Pavel Kus's avatar
Pavel Kus committed
72
#else
73
      real(kind=REAL_DATATYPE), intent(inout)   :: q(ldq,matrixCols)
Pavel Kus's avatar
Pavel Kus committed
74
#endif
75
76
      logical, intent(in)           :: wantDebug
      logical, intent(out)          :: success
Pavel Kus's avatar
Pavel Kus committed
77
78
79
80
81
82
83
84
85

      integer(kind=ik)              :: i, j, n, np, nc, nev1, l_cols, l_rows
      integer(kind=ik)              :: my_prow, my_pcol, np_rows, np_cols, mpierr

      integer(kind=ik), allocatable :: limits(:), l_col(:), p_col(:), l_col_bc(:), p_col_bc(:)

      integer(kind=ik)              :: istat
      character(200)                :: errorMessage

86
      call timer%start("solve_tridi" // PRECISION_SUFFIX)
87

Pavel Kus's avatar
Pavel Kus committed
88
89
90
91
92
93
      call timer%start("mpi_communication")
      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)
      call timer%stop("mpi_communication")
94

Pavel Kus's avatar
Pavel Kus committed
95
96
97
98
99
100
      success = .true.

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

      ! Set Q to 0
101
      q(1:l_rows, 1:l_cols) = CONST_0_0
Pavel Kus's avatar
Pavel Kus committed
102
103
104
105
106
107
108

      ! Get the limits of the subdivisons, each subdivison has as many cols
      ! as fit on the respective processor column

      allocate(limits(0:np_cols), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi: error when allocating limits "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
109
        stop 1
Pavel Kus's avatar
Pavel Kus committed
110
111
112
113
114
115
116
117
118
119
120
      endif

      limits(0) = 0
      do np=0,np_cols-1
        nc = local_index(na, np, np_cols, nblk, -1) ! number of columns on proc column np

        ! Check for the case that a column has have zero width.
        ! This is not supported!
        ! Scalapack supports it but delivers no results for these columns,
        ! which is rather annoying
        if (nc==0) then
121
          call timer%stop("solve_tridi" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
          if (wantDebug) write(error_unit,*) 'ELPA1_solve_tridi: ERROR: Problem contains processor column with zero width'
          success = .false.
          return
        endif
        limits(np+1) = limits(np) + nc
      enddo

      ! Subdivide matrix by subtracting rank 1 modifications

      do i=1,np_cols-1
        n = limits(i)
        d(n) = d(n)-abs(e(n))
        d(n+1) = d(n+1)-abs(e(n))
      enddo

      ! Solve sub problems on processsor columns

      nc = limits(my_pcol) ! column after which my problem starts

      if (np_cols>1) then
        nev1 = l_cols ! all eigenvectors are needed
      else
        nev1 = MIN(nev,l_cols)
      endif
146
      call solve_tridi_col_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
147
           &PRECISION &
148
           (l_cols, nev1, nc, d(nc+1), e(nc+1), q, ldq, nblk,  &
Pavel Kus's avatar
Pavel Kus committed
149
150
                        matrixCols, mpi_comm_rows, wantDebug, success)
      if (.not.(success)) then
151
        call timer%stop("solve_tridi" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
152
153
154
155
156
157
158
159
        return
      endif
      ! If there is only 1 processor column, we are done

      if (np_cols==1) then
        deallocate(limits, stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"solve_tridi: error when deallocating limits "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
160
          stop 1
Pavel Kus's avatar
Pavel Kus committed
161
162
        endif

163
        call timer%stop("solve_tridi" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
164
165
166
167
168
169
170
171
172
173
        return
      endif

      ! Set index arrays for Q columns

      ! Dense distribution scheme:

      allocate(l_col(na), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi: error when allocating l_col "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
174
        stop 1
Pavel Kus's avatar
Pavel Kus committed
175
176
177
178
179
      endif

      allocate(p_col(na), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi: error when allocating p_col "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
180
        stop 1
Pavel Kus's avatar
Pavel Kus committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
      endif

      n = 0
      do np=0,np_cols-1
        nc = local_index(na, np, np_cols, nblk, -1)
        do i=1,nc
          n = n+1
          l_col(n) = i
          p_col(n) = np
        enddo
      enddo

      ! Block cyclic distribution scheme, only nev columns are set:

      allocate(l_col_bc(na), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi: error when allocating l_col_bc "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
198
        stop 1
Pavel Kus's avatar
Pavel Kus committed
199
200
201
202
203
      endif

      allocate(p_col_bc(na), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi: error when allocating p_col_bc "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
204
        stop 1
Pavel Kus's avatar
Pavel Kus committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
      endif

      p_col_bc(:) = -1
      l_col_bc(:) = -1

      do i = 0, na-1, nblk*np_cols
        do j = 0, np_cols-1
          do n = 1, nblk
            if (i+j*nblk+n <= MIN(nev,na)) then
              p_col_bc(i+j*nblk+n) = j
              l_col_bc(i+j*nblk+n) = i/np_cols + n
             endif
           enddo
         enddo
      enddo

      ! Recursively merge sub problems
222
      call merge_recursive_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
223
           &PRECISION &
224
           (0, np_cols, wantDebug, success)
Pavel Kus's avatar
Pavel Kus committed
225
      if (.not.(success)) then
226
        call timer%stop("solve_tridi" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
227
228
229
230
231
232
        return
      endif

      deallocate(limits,l_col,p_col,l_col_bc,p_col_bc, stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi: error when deallocating l_col "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
233
        stop 1
Pavel Kus's avatar
Pavel Kus committed
234
235
      endif

236
      call timer%stop("solve_tridi" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
237
238
239
      return

      contains
240
        recursive subroutine merge_recursive_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
241
242
                  &PRECISION &
           (np_off, nprocs, wantDebug, success)
Pavel Kus's avatar
Pavel Kus committed
243
244
245
           use precision
#ifdef HAVE_DETAILED_TIMINGS
           use timings
246
#else
247
           use timings_dummy
Pavel Kus's avatar
Pavel Kus committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#endif
           implicit none

           ! noff is always a multiple of nblk_ev
           ! nlen-noff is always > nblk_ev

           integer(kind=ik)     :: np_off, nprocs
           integer(kind=ik)     :: np1, np2, noff, nlen, nmid, n
#ifdef WITH_MPI
!           integer(kind=ik)     :: my_mpi_status(mpi_status_size)
#endif
           logical, intent(in)  :: wantDebug
           logical, intent(out) :: success

           success = .true.

           if (nprocs<=1) then
             ! Safety check only
             if (wantDebug) write(error_unit,*) "ELPA1_merge_recursive: INTERNAL error merge_recursive: nprocs=",nprocs
             success = .false.
             return
           endif
           ! Split problem into 2 subproblems of size np1 / np2

           np1 = nprocs/2
           np2 = nprocs-np1

275
           if (np1 > 1) call merge_recursive_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
276
                        &PRECISION &
277
           (np_off, np1, wantDebug, success)
Pavel Kus's avatar
Pavel Kus committed
278
           if (.not.(success)) return
279
           if (np2 > 1) call merge_recursive_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
280
                        &PRECISION &
281
           (np_off+np1, np2, wantDebug, success)
Pavel Kus's avatar
Pavel Kus committed
282
283
284
285
286
287
288
289
290
291
           if (.not.(success)) return

           noff = limits(np_off)
           nmid = limits(np_off+np1) - noff
           nlen = limits(np_off+nprocs) - noff

#ifdef WITH_MPI
           call timer%start("mpi_communication")
           if (my_pcol==np_off) then
             do n=np_off+np1,np_off+nprocs-1
292
               call mpi_send(d(noff+1), nmid, MPI_REAL_PRECISION, n, 1, mpi_comm_cols, mpierr)
Pavel Kus's avatar
Pavel Kus committed
293
294
295
296
297
298
299
300
             enddo
           endif
           call timer%stop("mpi_communication")
#endif /* WITH_MPI */

           if (my_pcol>=np_off+np1 .and. my_pcol<np_off+nprocs) then
#ifdef WITH_MPI
             call timer%start("mpi_communication")
301
             call mpi_recv(d(noff+1), nmid, MPI_REAL_PRECISION, np_off, 1, mpi_comm_cols, MPI_STATUS_IGNORE, mpierr)
Pavel Kus's avatar
Pavel Kus committed
302
303
304
305
306
307
308
309
310
311
             call timer%stop("mpi_communication")
#else /* WITH_MPI */
!             d(noff+1:noff+1+nmid-1) = d(noff+1:noff+1+nmid-1)
#endif /* WITH_MPI */
           endif

           if (my_pcol==np_off+np1) then
             do n=np_off,np_off+np1-1
#ifdef WITH_MPI
               call timer%start("mpi_communication")
312
               call mpi_send(d(noff+nmid+1), nlen-nmid, MPI_REAL_PRECISION, n, 1, mpi_comm_cols, mpierr)
Pavel Kus's avatar
Pavel Kus committed
313
314
315
316
317
318
319
320
               call timer%stop("mpi_communication")
#endif /* WITH_MPI */

             enddo
           endif
           if (my_pcol>=np_off .and. my_pcol<np_off+np1) then
#ifdef WITH_MPI
             call timer%start("mpi_communication")
321
             call mpi_recv(d(noff+nmid+1), nlen-nmid, MPI_REAL_PRECISION, np_off+np1, 1,mpi_comm_cols, MPI_STATUS_IGNORE, mpierr)
Pavel Kus's avatar
Pavel Kus committed
322
323
324
325
326
327
328
329
330
             call timer%stop("mpi_communication")
#else /* WITH_MPI */
!             d(noff+nmid+1:noff+nmid+1+nlen-nmid-1) = d(noff+nmid+1:noff+nmid+1+nlen-nmid-1)
#endif /* WITH_MPI */
           endif
           if (nprocs == np_cols) then

             ! Last merge, result distribution must be block cyclic, noff==0,
             ! p_col_bc is set so that only nev eigenvalues are calculated
331
             call merge_systems_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
332
                  &PRECISION &
333
                                 (nlen, nmid, d(noff+1), e(noff+nmid), q, ldq, noff, &
Pavel Kus's avatar
Pavel Kus committed
334
335
336
337
338
                                 nblk, matrixCols, mpi_comm_rows, mpi_comm_cols, l_col, p_col, &
                                 l_col_bc, p_col_bc, np_off, nprocs, wantDebug, success )
             if (.not.(success)) return
           else
             ! Not last merge, leave dense column distribution
339
             call merge_systems_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
340
                  &PRECISION &
341
                                (nlen, nmid, d(noff+1), e(noff+nmid), q, ldq, noff, &
Pavel Kus's avatar
Pavel Kus committed
342
343
344
345
                                 nblk, matrixCols, mpi_comm_rows, mpi_comm_cols, l_col(noff+1), p_col(noff+1), &
                                 l_col(noff+1), p_col(noff+1), np_off, nprocs, wantDebug, success )
             if (.not.(success)) return
           endif
346
       end subroutine merge_recursive_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
347
           &PRECISION
Pavel Kus's avatar
Pavel Kus committed
348

349
    end subroutine solve_tridi_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
350
        &PRECISION
Pavel Kus's avatar
Pavel Kus committed
351

352
    subroutine solve_tridi_col_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
353
    &PRECISION &
354
    ( na, nev, nqoff, d, e, q, ldq, nblk, matrixCols, mpi_comm_rows, wantDebug, success )
Pavel Kus's avatar
Pavel Kus committed
355
356
357
358
359
360

   ! Solves the symmetric, tridiagonal eigenvalue problem on one processor column
   ! with the divide and conquer method.
   ! Works best if the number of processor rows is a power of 2!
#ifdef HAVE_DETAILED_TIMINGS
      use timings
361
362
#else
      use timings_dummy
Pavel Kus's avatar
Pavel Kus committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#endif
      use precision
      implicit none

      integer(kind=ik)              :: na, nev, nqoff, ldq, nblk, matrixCols, mpi_comm_rows
      real(kind=REAL_DATATYPE)                 :: d(na), e(na)
#ifdef USE_ASSUMED_SIZE
      real(kind=REAL_DATATYPE)                 :: q(ldq,*)
#else
      real(kind=REAL_DATATYPE)                 :: q(ldq,matrixCols)
#endif

      integer(kind=ik), parameter   :: min_submatrix_size = 16 ! Minimum size of the submatrices to be used

      real(kind=REAL_DATATYPE), allocatable    :: qmat1(:,:), qmat2(:,:)
      integer(kind=ik)              :: i, n, np
      integer(kind=ik)              :: ndiv, noff, nmid, nlen, max_size
      integer(kind=ik)              :: my_prow, np_rows, mpierr

      integer(kind=ik), allocatable :: limits(:), l_col(:), p_col_i(:), p_col_o(:)
      logical, intent(in)           :: wantDebug
      logical, intent(out)          :: success
      integer(kind=ik)              :: istat
      character(200)                :: errorMessage

388
      call timer%start("solve_tridi_col" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
      call timer%start("mpi_communication")
      call mpi_comm_rank(mpi_comm_rows,my_prow,mpierr)
      call mpi_comm_size(mpi_comm_rows,np_rows,mpierr)
      call timer%stop("mpi_communication")
      success = .true.
      ! Calculate the number of subdivisions needed.

      n = na
      ndiv = 1
      do while(2*ndiv<=np_rows .and. n>2*min_submatrix_size)
        n = ((n+3)/4)*2 ! the bigger one of the two halves, we want EVEN boundaries
        ndiv = ndiv*2
      enddo

      ! If there is only 1 processor row and not all eigenvectors are needed
      ! and the matrix size is big enough, then use 2 subdivisions
      ! so that merge_systems is called once and only the needed
      ! eigenvectors are calculated for the final problem.

      if (np_rows==1 .and. nev<na .and. na>2*min_submatrix_size) ndiv = 2

      allocate(limits(0:ndiv), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi_col: error when allocating limits "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
413
        stop 1
Pavel Kus's avatar
Pavel Kus committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
      endif

      limits(0) = 0
      limits(ndiv) = na

      n = ndiv
      do while(n>1)
        n = n/2 ! n is always a power of 2
        do i=0,ndiv-1,2*n
          ! We want to have even boundaries (for cache line alignments)
          limits(i+n) = limits(i) + ((limits(i+2*n)-limits(i)+3)/4)*2
        enddo
      enddo

      ! Calculate the maximum size of a subproblem

      max_size = 0
      do i=1,ndiv
        max_size = MAX(max_size,limits(i)-limits(i-1))
      enddo

      ! Subdivide matrix by subtracting rank 1 modifications

      do i=1,ndiv-1
        n = limits(i)
        d(n) = d(n)-abs(e(n))
        d(n+1) = d(n+1)-abs(e(n))
      enddo

      if (np_rows==1)    then

        ! For 1 processor row there may be 1 or 2 subdivisions
        do n=0,ndiv-1
          noff = limits(n)        ! Start of subproblem
          nlen = limits(n+1)-noff ! Size of subproblem

450
          call solve_tridi_single_problem_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
451
          &PRECISION &
452
                                   (nlen,d(noff+1),e(noff+1), &
Pavel Kus's avatar
Pavel Kus committed
453
                                    q(nqoff+noff+1,noff+1),ubound(q,dim=1), wantDebug, success)
454

Pavel Kus's avatar
Pavel Kus committed
455
456
457
458
459
460
461
462
463
464
465
          if (.not.(success)) return
        enddo

      else

        ! Solve sub problems in parallel with solve_tridi_single
        ! There is at maximum 1 subproblem per processor

        allocate(qmat1(max_size,max_size), stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"solve_tridi_col: error when allocating qmat1 "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
466
          stop 1
Pavel Kus's avatar
Pavel Kus committed
467
468
469
470
471
        endif

        allocate(qmat2(max_size,max_size), stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"solve_tridi_col: error when allocating qmat2 "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
472
          stop 1
Pavel Kus's avatar
Pavel Kus committed
473
474
475
476
477
478
479
480
        endif

        qmat1 = 0 ! Make sure that all elements are defined

        if (my_prow < ndiv) then

          noff = limits(my_prow)        ! Start of subproblem
          nlen = limits(my_prow+1)-noff ! Size of subproblem
481
          call solve_tridi_single_problem_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
482
          &PRECISION &
483
                                   (nlen,d(noff+1),e(noff+1),qmat1, &
Pavel Kus's avatar
Pavel Kus committed
484
                                    ubound(qmat1,dim=1), wantDebug, success)
485

Pavel Kus's avatar
Pavel Kus committed
486
487
488
489
490
491
492
493
494
495
496
          if (.not.(success)) return
        endif

        ! Fill eigenvectors in qmat1 into global matrix q

        do np = 0, ndiv-1

          noff = limits(np)
          nlen = limits(np+1)-noff
#ifdef WITH_MPI
          call timer%start("mpi_communication")
497
          call MPI_Bcast(d(noff+1), nlen, MPI_REAL_PRECISION, np, mpi_comm_rows, mpierr)
Pavel Kus's avatar
Pavel Kus committed
498
          qmat2 = qmat1
499
          call MPI_Bcast(qmat2, max_size*max_size, MPI_REAL_PRECISION, np, mpi_comm_rows, mpierr)
Pavel Kus's avatar
Pavel Kus committed
500
501
502
503
504
505
506
          call timer%stop("mpi_communication")
#else /* WITH_MPI */
!          qmat2 = qmat1 ! is this correct
#endif /* WITH_MPI */
          do i=1,nlen

#ifdef WITH_MPI
507
            call distribute_global_column_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
508
            &PRECISION &
509
                     (qmat2(1,i), q(1,noff+i), nqoff+noff, nlen, my_prow, np_rows, nblk)
Pavel Kus's avatar
Pavel Kus committed
510
#else /* WITH_MPI */
511
            call distribute_global_column_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
512
            &PRECISION &
513
                     (qmat1(1,i), q(1,noff+i), nqoff+noff, nlen, my_prow, np_rows, nblk)
Pavel Kus's avatar
Pavel Kus committed
514
515
516
517
518
519
520
521
#endif /* WITH_MPI */
          enddo

        enddo

        deallocate(qmat1, qmat2, stat=istat, errmsg=errorMessage)
        if (istat .ne. 0) then
          print *,"solve_tridi_col: error when deallocating qmat2 "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
522
          stop 1
Pavel Kus's avatar
Pavel Kus committed
523
524
525
526
527
528
529
530
531
        endif

      endif

      ! Allocate and set index arrays l_col and p_col

      allocate(l_col(na), p_col_i(na),  p_col_o(na), stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi_col: error when allocating l_col "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
532
        stop 1
Pavel Kus's avatar
Pavel Kus committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
      endif

      do i=1,na
        l_col(i) = i
        p_col_i(i) = 0
        p_col_o(i) = 0
      enddo

      ! Merge subproblems

      n = 1
      do while(n<ndiv) ! if ndiv==1, the problem was solved by single call to solve_tridi_single

        do i=0,ndiv-1,2*n

          noff = limits(i)
          nmid = limits(i+n) - noff
          nlen = limits(i+2*n) - noff

          if (nlen == na) then
            ! Last merge, set p_col_o=-1 for unneeded (output) eigenvectors
            p_col_o(nev+1:na) = -1
          endif
556
          call merge_systems_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
557
          &PRECISION &
558
                              (nlen, nmid, d(noff+1), e(noff+nmid), q, ldq, nqoff+noff, nblk, &
Pavel Kus's avatar
Pavel Kus committed
559
560
561
562
563
564
565
566
567
568
569
570
571
                               matrixCols, mpi_comm_rows, mpi_comm_self, l_col(noff+1), p_col_i(noff+1), &
                               l_col(noff+1), p_col_o(noff+1), 0, 1, wantDebug, success)
          if (.not.(success)) return

        enddo

        n = 2*n

      enddo

      deallocate(limits, l_col, p_col_i, p_col_o, stat=istat, errmsg=errorMessage)
      if (istat .ne. 0) then
        print *,"solve_tridi_col: error when deallocating l_col "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
572
        stop 1
Pavel Kus's avatar
Pavel Kus committed
573
574
      endif

575
      call timer%stop("solve_tridi_col" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
576

577
    end subroutine solve_tridi_col_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
578
    &PRECISION
Pavel Kus's avatar
Pavel Kus committed
579

580
    recursive subroutine solve_tridi_single_problem_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
581
    &PRECISION &
582
              (nlen, d, e, q, ldq, wantDebug, success)
Pavel Kus's avatar
Pavel Kus committed
583
584
585
586
587

   ! Solves the symmetric, tridiagonal eigenvalue problem on a single processor.
   ! Takes precautions if DSTEDC fails or if the eigenvalues are not ordered correctly.
#ifdef HAVE_DETAILED_TIMINGS
     use timings
588
589
#else
     use timings_dummy
Pavel Kus's avatar
Pavel Kus committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#endif
     use precision
     implicit none

     integer(kind=ik)              :: nlen, ldq
     real(kind=REAL_DATATYPE)                 :: d(nlen), e(nlen), q(ldq,nlen)

     real(kind=REAL_DATATYPE), allocatable    :: work(:), qtmp(:), ds(:), es(:)
     real(kind=REAL_DATATYPE)                 :: dtmp

     integer(kind=ik)              :: i, j, lwork, liwork, info, mpierr
     integer(kind=ik), allocatable :: iwork(:)

     logical, intent(in)           :: wantDebug
     logical, intent(out)          :: success
      integer(kind=ik)             :: istat
      character(200)               :: errorMessage

608
     call timer%start("solve_tridi_single" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
609
610
611
612
613

     success = .true.
     allocate(ds(nlen), es(nlen), stat=istat, errmsg=errorMessage)
     if (istat .ne. 0) then
       print *,"solve_tridi_single: error when allocating ds "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
614
       stop 1
Pavel Kus's avatar
Pavel Kus committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
     endif

     ! Save d and e for the case that dstedc fails

     ds(:) = d(:)
     es(:) = e(:)

     ! First try dstedc, this is normally faster but it may fail sometimes (why???)

     lwork = 1 + 4*nlen + nlen**2
     liwork =  3 + 5*nlen
     allocate(work(lwork), iwork(liwork), stat=istat, errmsg=errorMessage)
     if (istat .ne. 0) then
       print *,"solve_tridi_single: error when allocating work "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
629
       stop 1
Pavel Kus's avatar
Pavel Kus committed
630
     endif
631
     call timer%start("blas")
632
     call PRECISION_STEDC('I', nlen, d, e, q, ldq, work, lwork, iwork, liwork, info)
633
     call timer%stop("blas")
Pavel Kus's avatar
Pavel Kus committed
634
635
636
637
638
639
640
641
642

     if (info /= 0) then

       ! DSTEDC failed, try DSTEQR. The workspace is enough for DSTEQR.

       write(error_unit,'(a,i8,a)') 'Warning: Lapack routine DSTEDC failed, info= ',info,', Trying DSTEQR!'

       d(:) = ds(:)
       e(:) = es(:)
643
       call timer%start("blas")
644
       call PRECISION_STEQR('I', nlen, d, e, q, ldq, work, info)
645
646
       call timer%stop("blas")

Pavel Kus's avatar
Pavel Kus committed
647
648
649
650
651
652
653
654
655
656
657
658
659
       ! If DSTEQR fails also, we don't know what to do further ...

       if (info /= 0) then
         if (wantDebug) &
           write(error_unit,'(a,i8,a)') 'ELPA1_solve_tridi_single: ERROR: Lapack routine DSTEQR failed, info= ',info,', Aborting!'
           success = .false.
           return
         endif
       end if

       deallocate(work,iwork,ds,es, stat=istat, errmsg=errorMessage)
       if (istat .ne. 0) then
         print *,"solve_tridi_single: error when deallocating ds "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
660
         stop 1
Pavel Kus's avatar
Pavel Kus committed
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
       endif

      ! Check if eigenvalues are monotonically increasing
      ! This seems to be not always the case  (in the IBM implementation of dstedc ???)

      do i=1,nlen-1
        if (d(i+1)<d(i)) then
#ifdef DOUBLE_PRECISION_REAL
          if (abs(d(i+1) - d(i)) / abs(d(i+1) + d(i)) > 1e-14_rk8) then
#else
          if (abs(d(i+1) - d(i)) / abs(d(i+1) + d(i)) > 1e-14_rk4) then
#endif
            write(error_unit,'(a,i8,2g25.16)') '***WARNING: Monotony error dste**:',i+1,d(i),d(i+1)
          else
            write(error_unit,'(a,i8,2g25.16)') 'Info: Monotony error dste{dc,qr}:',i+1,d(i),d(i+1)
            write(error_unit,'(a)') 'The eigenvalues from a lapack call are not sorted to machine precision.'
            write(error_unit,'(a)') 'In this extent, this is completely harmless.'
            write(error_unit,'(a)') 'Still, we keep this info message just in case.'
          end if
          allocate(qtmp(nlen), stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"solve_tridi_single: error when allocating qtmp "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
683
            stop 1
Pavel Kus's avatar
Pavel Kus committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
          endif

          dtmp = d(i+1)
          qtmp(1:nlen) = q(1:nlen,i+1)
          do j=i,1,-1
            if (dtmp<d(j)) then
              d(j+1)        = d(j)
              q(1:nlen,j+1) = q(1:nlen,j)
            else
              exit ! Loop
            endif
          enddo
          d(j+1)        = dtmp
          q(1:nlen,j+1) = qtmp(1:nlen)
          deallocate(qtmp, stat=istat, errmsg=errorMessage)
          if (istat .ne. 0) then
            print *,"solve_tridi_single: error when deallocating qtmp "//errorMessage
Andreas Marek's avatar
Andreas Marek committed
701
            stop 1
Pavel Kus's avatar
Pavel Kus committed
702
703
704
705
          endif

       endif
     enddo
706
     call timer%stop("solve_tridi_single" // PRECISION_SUFFIX)
Pavel Kus's avatar
Pavel Kus committed
707

708
    end subroutine solve_tridi_single_problem_&
Andreas Marek's avatar
cleanup    
Andreas Marek committed
709
    &PRECISION
Pavel Kus's avatar
Pavel Kus committed
710