elpa_tests.F90 56.5 KB
Newer Older
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
55
56
57
58
!    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
!
!
!    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.
!
!
#include "config-f90.h"

program test_all_real

!-------------------------------------------------------------------------------
! Standard eigenvalueGproblem - REAL version
!
! This program demonstrates the use of the ELPA module
! together with standard scalapack 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".
!
!-------------------------------------------------------------------------------
59
60
   use elpa1
   use elpa2
61
62
63
   use elpa_utilities, only : error_unit, map_global_array_index_to_local_index
   use elpa2_utilities

64
65
66
67
68
69
   use test_util
   use test_read_input_parameters
   use test_check_correctness
   use test_setup_mpi
   use test_blacs_infrastructure
   use test_prepare_matrix
70
71

#ifdef HAVE_REDIRECT
72
   use test_redirect
73
#endif
74
   use test_output_type
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

   implicit none

   !-------------------------------------------------------------------------------
   ! Please set system size parameters below!
   ! na:   System size
   ! nev:  Number of eigenvectors to be calculated
   ! nblk: Blocking factor in block cyclic distribution
   !-------------------------------------------------------------------------------
   integer(kind=ik)                       :: nblk
   integer(kind=ik)                       :: na, nev

   integer(kind=ik)                       :: np_rows, np_cols, na_rows, na_cols

   integer(kind=ik)                       :: myid, nprocs, my_prow, my_pcol, mpi_comm_rows, mpi_comm_cols
   integer(kind=ik)                       :: i, mpierr, my_blacs_ctxt, sc_desc(9), info, nprow, npcol

   integer, external                      :: numroc

   logical                                :: wantDebug
95
   real(kind=rk8), allocatable            :: ev(:)
96
97
98
   real(kind=rk8), allocatable, target    :: a_real(:,:), z_real(:,:), tmp1_real(:,:), tmp2_real(:,:), as_real(:,:)
   complex(kind=ck8), allocatable, target :: a_complex(:,:), z_complex(:,:), tmp1_complex(:,:), tmp2_complex(:,:), as_complex(:,:)

Andreas Marek's avatar
Andreas Marek committed
99
100
101
   real(kind=rk8), allocatable, target    :: b_real(:,:), bs_real(:,:), c_real(:,:)
   complex(kind=ck8), allocatable, target :: b_complex(:,:), bs_complex(:,:), c_complex(:,:)

102
103
104
   complex(kind=ck8), parameter           :: CZERO = (0.0_rk8,0.0_rk8), CONE = (1.0_rk8,0.0_rk8)
   real(kind=rk8), allocatable, target    :: d_real(:), e_real(:), ev_analytic_real(:)
   real(kind=rk8),  target                :: diagonalELement_real, subdiagonalElement_real
Andreas Marek's avatar
Andreas Marek committed
105
   complex(kind=ck8)                      :: diagonalElement_complex
106
   real(kind=rk8), target                 :: tmp
Andreas Marek's avatar
Andreas Marek committed
107
   real(kind=rk8)                         :: norm, normmax
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

   real(kind=rk8), parameter              :: pi = 3.141592653589793238462643383279_rk8
   integer(kind=ik)                       :: STATUS
#ifdef WITH_OPENMP
   integer(kind=ik)                       :: omp_get_max_threads,  required_mpi_thread_level, &
                                             provided_mpi_thread_level
#endif
   type(input_options_t)                  :: input_options
   logical                                :: success
   character(len=8)                       :: task_suffix
   integer(kind=ik)                       :: j, this_kernel
   logical                                :: this_gpu
   logical                                :: this_qr
   integer(kind=ik)                       :: loctmp ,rowLocal, colLocal
   real(kind=rk8)                         :: tStart, tEnd
   real(kind=rk8)                         :: maxerr
   logical                                :: gpuAvailable
Andreas Marek's avatar
Andreas Marek committed
125
126
127
128
129
#ifdef WITH_MPI
   real(kind=rk8)                         :: pzlange, pdlange
#else
   real(kind=rk8)                         :: zlange, dlange
#endif
130
131
132
133
134
135
136
137
138
139
140
141

   !-------------------------------------------------------------------------------


   success = .true.

   call read_input_parameters(input_options)

   na =  input_options%na
   nev = input_options%nev
   nblk = input_options%nblk

Andreas Marek's avatar
Andreas Marek committed
142
143
144
145
   if (input_options%justHelpMessage) then
     call EXIT(0)
   endif

146
147
148
149
150
151
152
153
154

   !-------------------------------------------------------------------------------
   !  MPI Initialization
   call setup_mpi(myid, nprocs)


   STATUS = 0

!#define DATATYPE REAL !!! check here
155
!#include "elpa_print_headers.F90"
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

   do np_cols = NINT(SQRT(REAL(nprocs))),2,-1
      if(mod(nprocs,np_cols) == 0 ) exit
   enddo

   ! at the end of the above loop, nprocs is always divisible by np_cols

   np_rows = nprocs/np_cols

   if(myid==0) then
      print *
      if (input_options%datatype .eq. 1) then
        print '(a)','Standard eigenvalue problem - REAL version'
      else if (input_options%datatype .eq. 2) then
        print '(a)','Standard eigenvalue problem - COMPLEX version'
      endif
      print *
      print '(3(a,i0))','Matrix size=',na,', Number of eigenvectors=',nev,', Block size=',nblk
      print '(3(a,i0))','Number of processor rows=',np_rows,', cols=',np_cols,', total=',nprocs
      print *
   endif

   !-------------------------------------------------------------------------------
   ! Set up BLACS context and MPI communicators
   !
   ! The BLACS context is only necessary for using Scalapack.
   !
   ! For ELPA, the MPI communicators along rows/cols are sufficient,
   ! and the grid setup may be done in an arbitrary way as long as it is
   ! consistent (i.e. 0<=my_prow<np_rows, 0<=my_pcol<np_cols and every
   ! process has a unique (my_prow,my_pcol) pair).

   call set_up_blacsgrid(mpi_comm_world, my_blacs_ctxt, np_rows, np_cols, &
                         nprow, npcol, my_prow, my_pcol)

   if (myid==0) then
     print '(a)','| Past BLACS_Gridinfo.'
   end if

   ! All ELPA routines need MPI communicators for communicating within
196
   ! rows or columns of processes, these are set in elpa_get_communicators.
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

   mpierr = elpa_get_communicators(mpi_comm_world, my_prow, my_pcol, &
                                   mpi_comm_rows, mpi_comm_cols)

   if (myid==0) then
     print '(a)','| Past split communicator setup for rows and columns.'
   end if

   call set_up_blacs_descriptor(na ,nblk, my_prow, my_pcol, np_rows, np_cols, &
                                na_rows, na_cols, sc_desc, my_blacs_ctxt, info)

   if (myid==0) then
     print '(a)','| Past scalapack descriptor setup.'
   end if

   !-------------------------------------------------------------------------------
   ! Allocate matrices and set up a test matrix for the eigenvalue problem

   if (input_options%datatype .eq. 1) then
     allocate(a_real (na_rows,na_cols))
     allocate(z_real (na_rows,na_cols))
     allocate(as_real(na_rows,na_cols))
Andreas Marek's avatar
Andreas Marek committed
219
220
221
222
223
224
225
226
227

     if (input_options%doInvertTrm .or. input_options%doTransposeMultiply) then
       allocate(b_real(na_rows,na_cols))
       allocate(bs_real(na_rows,na_cols))
     endif

     if (input_options%doTransposeMultiply) then
       allocate(c_real(na_rows,na_cols))
     endif
228
229
230
231
232
233
   endif
   if (input_options%datatype .eq. 2) then
     allocate(a_complex (na_rows,na_cols))
     allocate(z_complex (na_rows,na_cols))
     allocate(as_complex(na_rows,na_cols))

Andreas Marek's avatar
Andreas Marek committed
234
235
236
237
238
239
240
241
     if (input_options%doInvertTrm .or. input_options%doTransposeMultiply) then
       allocate(b_complex(na_rows,na_cols))
       allocate(bs_complex(na_rows,na_cols))
     endif

     if (input_options%doTransposeMultiply) then
       allocate(c_complex(na_rows,na_cols))
     endif
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
   endif

   if (input_options%datatype .eq. 1) then
     allocate(d_real (na))
     allocate(e_real (na))
     allocate(ev_analytic_real(na))
   endif

   allocate(ev(na))

   if (input_options%datatype .eq. 1) then
     allocate(tmp1_real(na_rows,na_cols))
     allocate(tmp2_real(na_rows,na_cols))
   endif
   if (input_options%datatype .eq. 2) then
     allocate(tmp1_complex(na_rows,na_cols))
     allocate(tmp2_complex(na_rows,na_cols))
   endif

Andreas Marek's avatar
Andreas Marek committed
261
   if (input_options%datatype .eq. 1) then
262
     call prepare_matrix_random(na, myid, sc_desc, a_real, z_real, as_real)
Andreas Marek's avatar
Andreas Marek committed
263
264
265
266
267
268
269

     if (input_options%doInvertTrm) then
       b_real(:,:) = a_real(:,:)
       bs_real(:,:) = a_real(:,:)
     endif
   endif
   if (input_options%datatype .eq. 2) then
270
     call prepare_matrix_random(na, myid, sc_desc, a_complex, z_complex, as_complex)
Andreas Marek's avatar
Andreas Marek committed
271
272
273
274
275
276
277
278
     if (input_options%doInvertTrm) then
       b_complex(:,:) = a_complex(:,:)
       bs_complex(:,:) = a_complex(:,:)
     endif

   endif


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
   if (input_options%doSolveTridi) then
     ! first the toeplitz test only in real case
     ! changeable numbers here would be nice
     if (input_options%datatype .eq. 1) then

       diagonalElement_real = 0.45_rk8
       subdiagonalElement_real =  0.78_rk8

       d_real(:) = diagonalElement_real
       e_real(:) = subdiagonalElement_real


       ! set up the diagonal and subdiagonals (for general solver test)
       do i=1, na ! for diagonal elements
         if (map_global_array_index_to_local_index(i, i, rowLocal, colLocal, nblk, np_rows, np_cols, my_prow, my_pcol)) then
           a_real(rowLocal,colLocal) = diagonalElement_real
         endif
       enddo

       do i=1, na-1
         if (map_global_array_index_to_local_index(i, i+1, rowLocal, colLocal, nblk, np_rows, np_cols, my_prow, my_pcol)) then
           a_real(rowLocal,colLocal) = subdiagonalElement_real
         endif
       enddo

       do i=2, na
         if (map_global_array_index_to_local_index(i, i-1, rowLocal, colLocal, nblk, np_rows, np_cols, my_prow, my_pcol)) then
           a_real(rowLocal,colLocal) = subdiagonalElement_real
         endif
       enddo

       as_real = a_real

       !-------------------------------------------------------------------------------
       ! Calculate eigenvalues/eigenvectors

       if (myid==0) then
         print '(a)','| Entering elpa_solve_tridi ... '
         print *
       end if

#ifdef WITH_MPI
       call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

       success = elpa_solve_tridi_double(na, nev, d_real, e_real, a_real, na_rows, nblk, na_cols, mpi_comm_rows, &
                                         mpi_comm_cols, wantDebug)

       if (.not.(success)) then
         write(error_unit,*) "elpa_solve_tridi produced an error! Aborting..."
#ifdef WITH_MPI
         call MPI_ABORT(mpi_comm_world, 1, mpierr)
331
332
#else
         call exit(1)
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
       endif

       if (myid==0) then
         print '(a)','| elpa_solve_tridi complete.'
         print *
       end if


       ev = d_real

       ! analytic solution
       do i=1, na
         ev_analytic_real(i) = diagonalElement_real + 2.0_rk8 * subdiagonalElement_real * &
                                cos( pi*real(i,kind=rk8)/ real(na+1,kind=rk8) )
       enddo

       ! sort analytic solution:

       ! this hack is neither elegant, nor optimized: for huge matrixes it might be expensive
       ! a proper sorting algorithmus might be implemented here

       tmp    = minval(ev_analytic_real)
       loctmp = minloc(ev_analytic_real, 1)

       ev_analytic_real(loctmp) = ev_analytic_real(1)
       ev_analytic_real(1) = tmp

       do i=2, na
         tmp = ev_analytic_real(i)
         do j= i, na
           if (ev_analytic_real(j) .lt. tmp) then
             tmp    = ev_analytic_real(j)
             loctmp = j
           endif
         enddo
         ev_analytic_real(loctmp) = ev_analytic_real(i)
         ev_analytic_real(i) = tmp
       enddo

       ! compute a simple error max of eigenvalues
       maxerr = 0.0_rk8
       maxerr = maxval( (d_real(:) - ev_analytic_real(:))/ev_analytic_real(:) , 1)

       if (maxerr .gt. 8.e-13_rk8) then
         if (myid .eq. 0) then
           print *,"Eigenvalues differ from analytic solution: maxerr = ",maxerr
         endif

         status = 1

       endif
     endif ! datatype == 1 for toeplitz
    endif ! doSolve-tridi

Andreas Marek's avatar
Andreas Marek committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
    if (input_options%doCholesky) then
      if (input_options%datatype .eq. 1) then
        a_real(:,:) = 0.0_rk8
        diagonalElement_real = 2.546_rk8

        do i = 1, na
          if (map_global_array_index_to_local_index(i, i, rowLocal, colLocal, nblk, np_rows, np_cols, my_prow, my_pcol)) then
            a_real(rowLocal,colLocal) = diagonalElement_real * abs(cos( pi*real(i,kind=rk8)/ real(na+1,kind=rk8) ))
          endif
        enddo

        as_real(:,:) = a_real(:,:)

        if (myid==0) then
          print '(a)','| Compute real cholesky decomposition ... '
          print *
        end if
#ifdef WITH_MPI
        call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
407
408
#endif

Andreas Marek's avatar
Andreas Marek committed
409
        success = elpa_cholesky_real_double(na, a_real, na_rows, nblk, na_cols, mpi_comm_rows, mpi_comm_cols, .true.)
410

Andreas Marek's avatar
Andreas Marek committed
411
412
413
414
        if (.not.(success)) then
          write(error_unit,*) "elpa_cholseky_real produced an error! Aborting..."
#ifdef WITH_MPI
          call MPI_ABORT(mpi_comm_world, 1, mpierr)
415
416
#else
          call exit(1)
Andreas Marek's avatar
Andreas Marek committed
417
418
#endif
        endif
419

Andreas Marek's avatar
Andreas Marek committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437

        if (myid==0) then
          print '(a)','| Real cholesky decomposition complete.'
          print *
        end if

        tmp1_real(:,:) = 0.0_rk8
#ifdef WITH_MPI
        call pdtran(na, na, 1.0_rk8, a_real, 1, 1, sc_desc, 0.0_rk8, tmp1_real, 1, 1, sc_desc)
#else
        tmp1_real = transpose(a_real)
#endif
        ! tmp2 = a * a**T
#ifdef WITH_MPI
        call pdgemm("N","N", na, na, na, 1.0_rk8, a_real, 1, 1, sc_desc, tmp1_real, 1, 1, &
                    sc_desc, 0.0_rk8, tmp2_real, 1, 1, sc_desc)
#else
        call dgemm("N","N", na, na, na, 1.0_rk8, a_real, na, tmp1_real, na, 0.0_rk8, tmp2_real, na)
438
439
#endif

Andreas Marek's avatar
Andreas Marek committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
        ! compare tmp2 with original matrix
        tmp2_real(:,:) = tmp2_real(:,:) - as_real(:,:)

#ifdef WITH_MPI
        norm = pdlange("M",na, na, tmp2_real, 1, 1, sc_desc, tmp1_real)
#else
        norm = dlange("M", na, na, tmp2_real, na_rows, tmp1_real)
#endif

#ifdef WITH_MPI
        call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
        normmax = norm
#endif
        if (myid .eq. 0) then
          print *," Maximum error of result: ", normmax
        endif

        if (normmax .gt. 5e-12_rk8) then
             status = 1
        endif

      endif ! real case

      if (input_options%datatype .eq. 2) then
        a_complex(:,:) = CONE - CONE
        diagonalElement_complex = (2.546_rk8, 0.0_rk8)
        do i = 1, na
          if (map_global_array_index_to_local_index(i, i, rowLocal, colLocal, nblk, np_rows, np_cols, my_prow, my_pcol)) then
            a_complex(rowLocal,colLocal) = diagonalElement_complex * abs(cos( pi*real(i,kind=rk8)/ real(na+1,kind=rk8) ))
          endif
        enddo
        as_complex(:,:) = a_complex(:,:)

        if (myid==0) then
          print '(a)','| Compute complex cholesky decomposition ... '
          print *
        end if
#ifdef WITH_MPI
        call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

        success = elpa_cholesky_complex_double(na, a_complex, na_rows, nblk, na_cols, mpi_comm_rows, mpi_comm_cols, .true.)

        if (.not.(success)) then
          write(error_unit,*) " elpa_cholesky_complex produced an error! Aborting..."
#ifdef WITH_MPI
          call MPI_ABORT(mpi_comm_world, 1, mpierr)
488
489
#else
          call exit(1)
Andreas Marek's avatar
Andreas Marek committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#endif
        endif


        if (myid==0) then
          print '(a)','| Solve cholesky decomposition complete.'
          print *
        end if

        tmp1_complex(:,:) = 0.0_ck8

        ! tmp1 = a**H
#ifdef WITH_MPI
        call pztranc(na, na, CONE, a_complex, 1, 1, sc_desc, CZERO, tmp1_complex, 1, 1, sc_desc)
#else
        tmp1_complex = transpose(conjg(a_complex))
#endif
        ! tmp2 = a * a**H
#ifdef WITH_MPI
        call pzgemm("N","N", na, na, na, CONE, a_complex, 1, 1, sc_desc, tmp1_complex, 1, 1, &
                     sc_desc, CZERO, tmp2_complex, 1, 1, sc_desc)
#else
        call zgemm("N","N", na, na, na, CONE, a_complex, na, tmp1_complex, na, CZERO, tmp2_complex, na)
#endif

        ! compare tmp2 with c
        tmp2_complex(:,:) = tmp2_complex(:,:) - as_complex(:,:)

#ifdef WITH_MPI
        norm = pzlange("M",na, na, tmp2_complex, 1, 1, sc_desc,tmp1_complex)
#else
        norm = zlange("M",na, na, tmp2_complex, na_rows, tmp1_complex)
#endif
#ifdef WITH_MPI
        call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
        normmax = norm
#endif
        if (myid .eq. 0) then
          print *," Maximum error of result: ", normmax
        endif

        if (normmax .gt. 5e-11_rk8) then
             status = 1
        endif

      endif ! complex case


    endif ! doCholesky


    if (input_options%doInvertTrm) then
      if (input_options%datatype .eq. 1) then

        a_real(:,:) = 0.0_rk8
        diagonalElement_real = 2.546_rk8

        do i = 1, na
          if (map_global_array_index_to_local_index(i, i, rowLocal, colLocal, nblk, np_rows, np_cols, my_prow, my_pcol)) then
            a_real(rowLocal,colLocal) = diagonalElement_real * abs(cos( pi*real(i,kind=rk8)/ real(na+1,kind=rk8) ))
          endif
        enddo

        as_real(:,:) = a_real(:,:)

        if (myid==0) then
          print '(a)','| Setup an upper triangular matrix ... '
          print *
        end if
#ifdef WITH_MPI
        call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

        success = elpa_cholesky_real_double(na, a_real, na_rows, nblk, na_cols, mpi_comm_rows, mpi_comm_cols, .true.)

        if (.not.(success)) then
           write(error_unit,*) "elpa_cholseky_real produced an error! Aborting..."
#ifdef WITH_MPI
           call MPI_ABORT(mpi_comm_world, 1, mpierr)
570
571
#else
           call exit(1)
Andreas Marek's avatar
Andreas Marek committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#endif
        endif


        if (myid==0) then
          print '(a)','| Upper triangular matrix created.'
          print *
        end if

        as_real(:,:) = a_real(:,:)

        if (myid==0) then
          print '(a)','| Invert the upper triangular matrix ... '
          print *
        end if
#ifdef WITH_MPI
        call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

        success = elpa_invert_trm_real_double(na, a_real, na_rows, nblk, na_cols, mpi_comm_rows, mpi_comm_cols, .true.)

        if (.not.(success)) then
          write(error_unit,*) "elpa_cholseky_real produced an error! Aborting..."
#ifdef WITH_MPI
          call MPI_ABORT(mpi_comm_world, 1, mpierr)
597
598
#else
          call exit(1)
Andreas Marek's avatar
Andreas Marek committed
599
600
601
602
603
604
605
606
#endif
        endif


        if (myid==0) then
          print '(a)','| Upper triangular matrix inverted.'
          print *
        end if
607

Andreas Marek's avatar
Andreas Marek committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677

        tmp1_real(:,:) = 0.0_rk8

        ! tmp1 = as * a^-1 ! should give unity matrix

#ifdef WITH_MPI
        call pdgemm("N","N", na, na, na, 1.0_rk8, as_real, 1, 1, sc_desc, a_real, 1, 1, &
                    sc_desc, 0.0_rk8, tmp1_real, 1, 1, sc_desc)
#else
        call dgemm("N","N", na, na, na, 1.0_rk8, as_real, na, a_real, na, 0.0_rk8, tmp1_real, na)
#endif

        ! check the quality of unity matrix

        ! tmp2 = b * tmp1
#ifdef WITH_MPI
        call pdgemm("N","N", na, na, na, 1.0_rk8, b_real, 1, 1, sc_desc, tmp1_real, 1, 1, &
                    sc_desc, 0.0_rk8, tmp2_real, 1, 1, sc_desc)
#else
        call dgemm("N","N", na, na, na, 1.0_rk8, b_real, na, tmp1_real, na, 0.0_rk8, tmp2_real, na)
#endif

        ! compare tmp2 with original matrix b
        tmp2_real(:,:) = tmp2_real(:,:) - bs_real(:,:)

#ifdef WITH_MPI
        norm = pdlange("M",na, na, tmp2_real, 1, 1, sc_desc, bs_real)
#else
        norm = dlange("M", na, na, tmp2_real, na_rows, bs_real)
#endif

#ifdef WITH_MPI
        call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
        normmax = norm
#endif
        if (myid .eq. 0) then
          print *," Maximum error of result: ", normmax
        endif

        if (normmax .gt. 5e-12_rk8) then
          status = 1
        endif

      endif ! realcase

      if (input_options%datatype .eq. 2) then
        a_complex(:,:) = CONE - CONE
        diagonalElement_complex = (2.546_rk8, 0.0_rk8)
        do i = 1, na
          if (map_global_array_index_to_local_index(i, i, rowLocal, colLocal, nblk, np_rows, np_cols, my_prow, my_pcol)) then
            a_complex(rowLocal,colLocal) = diagonalElement_complex * abs(cos( pi*real(i,kind=rk8)/ real(na+1,kind=rk8) ))
          endif
        enddo
        as_complex(:,:) = a_complex(:,:)

        if (myid==0) then
          print '(a)','| Setting up tridiagonal matrix ... '
          print *
        end if
#ifdef WITH_MPI
        call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

        success = elpa_cholesky_complex_double(na, a_complex, na_rows, nblk, na_cols, mpi_comm_rows, mpi_comm_cols, .true.)

        if (.not.(success)) then
          write(error_unit,*) " elpa_cholesky_complex produced an error! Aborting..."
#ifdef WITH_MPI
          call MPI_ABORT(mpi_comm_world, 1, mpierr)
678
679
#else
          call exit(1)
Andreas Marek's avatar
Andreas Marek committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#endif
        endif

        as_complex(:,:) = a_complex(:,:)

        if (myid==0) then
          print '(a)','| Setting up tridiagonal matrix complete.'
          print *
        end if

        if (myid==0) then
          print '(a)','| Inverting tridiagonal matrix ... '
          print *
        end if
#ifdef WITH_MPI
        call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

        success = elpa_invert_trm_complex_double(na, a_complex, na_rows, nblk, na_cols, mpi_comm_rows, mpi_comm_cols, .true.)

        if (.not.(success)) then
           write(error_unit,*) " elpa_invert_trm_complex produced an error! Aborting..."
#ifdef WITH_MPI
           call MPI_ABORT(mpi_comm_world, 1, mpierr)
704
705
#else
           call exit(1)
Andreas Marek's avatar
Andreas Marek committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#endif
        endif


        if (myid==0) then
          print '(a)','| Inversion of tridiagonal matrix complete.'
          print *
        end if


        tmp1_complex(:,:) = 0.0_ck8

        ! tmp1 = a * a^-1 ! should be unity matrix
#ifdef WITH_MPI
        call pzgemm("N","N", na, na, na, CONE, as_complex, 1, 1, sc_desc, a_complex, 1, 1, &
                    sc_desc, CZERO, tmp1_complex, 1, 1, sc_desc)
#else
        call zgemm("N","N", na, na, na, CONE, as_complex, na, a_complex, na, CZERO, tmp1_complex, na)
#endif

        ! tmp2 = b * tmp1
        tmp2_complex(:,:) = 0.0_ck8
#ifdef WITH_MPI
        call pzgemm("N","N", na, na, na, CONE, b_complex, 1, 1, sc_desc, tmp1_complex, 1, 1, &
                    sc_desc, CZERO, tmp2_complex, 1, 1, sc_desc)
#else
        call zgemm("N","N", na, na, na, CONE, b_complex, na, tmp1_complex, na, CZERO, tmp2_complex, na)
#endif

        ! compare tmp2 with c
        tmp2_complex(:,:) = tmp2_complex(:,:) - bs_complex(:,:)
        tmp1_complex(:,:) = 0.0_ck8
#ifdef WITH_MPI
        norm = pzlange("M",na, na, tmp2_complex, 1, 1, sc_desc,tmp1_complex)
#else
        norm = zlange("M",na, na, tmp2_complex, na_rows, tmp1_complex)
#endif
#ifdef WITH_MPI
        call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
        normmax = norm
#endif
        if (myid .eq. 0) then
          print *," Maximum error of result: ", normmax
        endif

        if (normmax .gt. 5e-11_rk8) then
          status = 1
        endif

      endif ! complexcase

    endif ! input_options%doInvertTrm

    if (input_options%doTransposeMultiply) then
      if (input_options%datatype .eq. 1) then
        b_real(:,:) = 2.0_rk8 * a_real(:,:)
        c_real(:,:) = 0.0_rk8

        if (myid==0) then
          print '(a)','| Compute c= a**T * b ... '
          print *
        end if
#ifdef WITH_MPI
        call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

        success = elpa_mult_at_b_real_double("F","F", na, na, a_real, na_rows, na_cols, b_real, na_rows, &
                                             na_cols, nblk, mpi_comm_rows, mpi_comm_cols, c_real,   &
                                             na_rows, na_cols)

        if (.not.(success)) then
          write(error_unit,*) "elpa_mult_at_b_real produced an error! Aborting..."
#ifdef WITH_MPI
          call MPI_ABORT(mpi_comm_world, 1, mpierr)
781
782
#else
          call exit(1)
Andreas Marek's avatar
Andreas Marek committed
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
#endif
        endif


        if (myid==0) then
          print '(a)','| Solve c = a**T * b complete.'
          print *
        end if

        tmp1_real(:,:) = 0.0_rk8

        ! tmp1 = a**T
#ifdef WITH_MPI
        call pdtran(na, na, 1.0_rk8, a_real, 1, 1, sc_desc, 0.0_rk8, tmp1_real, 1, 1, sc_desc)
#else
        tmp1_real = transpose(a_real)
#endif
        ! tmp2 = tmp1 * b
#ifdef WITH_MPI
        call pdgemm("N","N", na, na, na, 1.0_rk8, tmp1_real, 1, 1, sc_desc, b_real, 1, 1, &
                    sc_desc, 0.0_rk8, tmp2_real, 1, 1, sc_desc)
#else
        call dgemm("N","N", na, na, na, 1.0_rk8, tmp1_real, na, b_real, na, 0.0_rk8, tmp2_real, na)
#endif

        ! compare tmp2 with c
        tmp2_real(:,:) = tmp2_real(:,:) - c_real(:,:)

#ifdef WITH_MPI
        norm = pdlange("M", na, na, tmp2_real, 1, 1, sc_desc, tmp1_real)
#else
        norm = dlange("M", na, na, tmp2_real, na_rows, tmp1_real)
#endif

#ifdef WITH_MPI
        call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
        normmax = norm
#endif
        if (myid .eq. 0) then
          print *," Maximum error of result: ", normmax
        endif

        if (normmax .gt. 5e-11_rk8) then
             status = 1
        endif

      endif ! realcase

      if (input_options%datatype .eq. 2) then
        b_complex(:,:) = 2.0_ck8 * a_complex(:,:)
        c_complex(:,:) = 0.0_ck8

        if (myid==0) then
          print '(a)','| Compute c= a**T * b ... '
          print *
        end if
#ifdef WITH_MPI
        call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

        success = elpa_mult_ah_b_complex_double("F","F", na, na, a_complex, na_rows, na_cols, b_complex, na_rows, na_cols, &
                                                nblk, mpi_comm_rows, mpi_comm_cols, c_complex, na_rows, na_cols)

        if (.not.(success)) then
          write(error_unit,*) " elpa_mult_at_b_complex produced an error! Aborting..."
#ifdef WITH_MPI
          call MPI_ABORT(mpi_comm_world, 1, mpierr)
851
852
#else
          call exit(1)
Andreas Marek's avatar
Andreas Marek committed
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
#endif
        endif

        if (myid==0) then
          print '(a)','| Solve c = a**T * b complete.'
          print *
        end if

        tmp1_complex(:,:) = 0.0_ck8

        ! tmp1 = a**T
#ifdef WITH_MPI
        call pztranc(na, na, CONE, a_complex, 1, 1, sc_desc, CZERO, tmp1_complex, 1, 1, sc_desc)
#else
        tmp1_complex = transpose(conjg(a_complex))
#endif
        ! tmp2 = tmp1 * b
#ifdef WITH_MPI
        call pzgemm("N","N", na, na, na, CONE, tmp1_complex, 1, 1, sc_desc, b_complex, 1, 1, &
                    sc_desc, CZERO, tmp2_complex, 1, 1, sc_desc)
#else
        call zgemm("N","N", na, na, na, CONE, tmp1_complex, na, b_complex, na, CZERO, tmp2_complex, na)
#endif

        ! compare tmp2 with c
        tmp2_complex(:,:) = tmp2_complex(:,:) - c_complex(:,:)

#ifdef WITH_MPI
        norm = pzlange("M",na, na, tmp2_complex, 1, 1, sc_desc,tmp1_complex)
#else
        norm = zlange("M",na, na, tmp2_complex, na_rows, tmp1_complex)
#endif
#ifdef WITH_MPI
        call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
        normmax = norm
#endif
        if (myid .eq. 0) then
          print *," Maximum error of result: ", normmax
        endif

        if (normmax .gt. 5e-11_rk8) then
             status = 1
        endif

      endif ! complexcase


    endif ! input_options%doTransposeMultiply

!   if (input_options%datatype .eq. 0) then
904
!     call prepare_matrix_random(na, myid, sc_desc, a_real, z_real, as_real)
Andreas Marek's avatar
Andreas Marek committed
905
906
!   endif
!   if (input_options%datatype .eq. 1) then
907
!     call prepare_matrix_random(na, myid, sc_desc, a_complex, z_complex, as_complex)
Andreas Marek's avatar
Andreas Marek committed
908
!   endif
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924

   if (input_options%do1stage) then
     !-------------------------------------------------------------------------------
     ! Calculate eigenvalues/eigenvectors

     if (myid==0) then
       print '(a)','| Entering one-step ELPA solver ... '
       print *
     end if

     if (input_options%useGPUIsSet .eq. 1) this_gpu = .true.
     if (input_options%useGPUIsSet .eq. 0) this_gpu = .false.

#ifdef WITH_MPI
     call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif
925
     tStart = seconds()
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945

     if (input_options%datatype .eq. 1) then
       success = elpa_solve_evp_real_1stage_double(na, nev, a_real, na_rows, ev, z_real, na_rows, nblk, &
                                                   na_cols, mpi_comm_rows, mpi_comm_cols, mpi_comm_world, useGPU=this_gpu)
     endif
     if (input_options%datatype .eq. 2) then
       success = elpa_solve_evp_complex_1stage_double(na, nev, a_complex, na_rows, ev, z_complex, na_rows, nblk, &
                                                      na_cols, mpi_comm_rows, mpi_comm_cols, mpi_comm_world, useGPU=this_gpu)
     endif

     if (.not.(success)) then
       if (input_options%datatype .eq. 1) then
         write(error_unit,*) "elpa_solve_evp_real_1stage_double produced an error! Aborting..."
       endif
       if (input_options%datatype .eq. 2) then
         write(error_unit,*) "elpa_solve_evp_complex_1stage_double produced an error! Aborting..."
       endif

#ifdef WITH_MPI
       call MPI_ABORT(mpi_comm_world, 1, mpierr)
946
947
#else
       call exit(1)
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
#endif
     endif


     if (myid==0) then
       print '(a)','| One-step ELPA solver complete.'
       print *
     end if

     if (input_options%datatype .eq. 1) then
       if (myid == 0) print *,'Time tridiag_real     :',time_evp_fwd
       if (myid == 0) print *,'Time solve_tridi      :',time_evp_solve
       if (myid == 0) print *,'Time trans_ev_real    :',time_evp_back
       if (myid == 0) print *,'Total time (sum above):',time_evp_back+time_evp_solve+time_evp_fwd
       if (myid == 0) print *," "
     endif
     if (input_options%datatype .eq. 2) then
       if (myid == 0) print *,'Time tridiag_complex     :',time_evp_fwd
       if (myid == 0) print *,'Time solve_tridi         :',time_evp_solve
       if (myid == 0) print *,'Time trans_ev_complex    :',time_evp_back
       if (myid == 0) print *,'Total time (sum above)   :',time_evp_back+time_evp_solve+time_evp_fwd
       if (myid == 0) print *," "
     endif
#ifdef WITH_MPI
     call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif
974
     tEnd = seconds()
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
     if(myid == 0) print *," "
     if (input_options%datatype .eq. 1) then
       if(myid == 0) print *,'Total time for solve_evp_real_1stage:',tEnd - tStart
     endif
     if (input_options%datatype .eq. 2) then
       if(myid == 0) print *,'Total time for solve_evp_complex_1stage:',tEnd - tStart
     endif

     if(myid == 0) print *," "

! TODO write of output

!   if(write_to_file%eigenvectors) then
!     write(unit = task_suffix, fmt = '(i8.8)') myid
!     open(17,file="EVs_real_out_task_"//task_suffix(1:8)//".txt",form='formatted',status='new')
!     write(17,*) "Part of eigenvectors: na_rows=",na_rows,"of na=",na," na_cols=",na_cols," of na=",na
!
!     do i=1,na_rows
!       do j=1,na_cols
!         write(17,*) "row=",i," col=",j," element of eigenvector=",z(i,j)
!       enddo
!     enddo
!     close(17)
!   endif
!
!   if(write_to_file%eigenvalues) then
!      if (myid == 0) then
!         open(17,file="Eigenvalues_real_out.txt",form='formatted',status='new')
!         do i=1,na
!            write(17,*) i,ev(i)
!         enddo
!         close(17)
!      endif
!   endif


     !-------------------------------------------------------------------------------
     ! Test correctness of result (using plain scalapack routines)


     if (input_options%datatype .eq. 1) then
1016
       status = check_correctness_evp_numeric_residuals(na, nev, as_real, z_real, ev, sc_desc, myid)
1017
1018
     endif
     if (input_options%datatype .eq. 2) then
1019
       status = check_correctness_evp_numeric_residuals(na, nev, as_complex, z_complex, ev, sc_desc, myid)
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
     endif

     if (status .eq. 1) then
#ifdef WITH_MPI
       call blacs_gridexit(my_blacs_ctxt)
       call mpi_finalize(mpierr)
#endif

       call EXIT(STATUS)
     endif
   endif !do1Stage

   if (input_options%do2Stage) then
     if (input_options%datatype .eq. 1) then
       ! real cases

       if (.not.(input_options%realKernelIsSet)) then
         ! start again with ELPA2 generic and so forth

         ! first default kernel

         if (myid .eq. 0) print *," "
1042
1043
         if (myid .eq. 0) print *,"Testing 2stage solver with default kernel: ", &
           elpa_int_value_to_string("real_kernel", ELPA_2STAGE_REAL_DEFAULT)
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
         if (myid .eq. 0) print *," "

         a_real = as_real
         z_real = a_real

        if (input_options%useGPUIsSet .eq. 1) this_gpu = .true.
        if (input_options%useGPUIsSet .eq. 0) this_gpu = .false.

        if (input_options%useQrIsSet .eq. 1) this_qr = .true.
        if (input_options%useQrIsSet .eq. 0) this_qr = .false.

         if (myid==0) then
           print *," "
           print '(a)','| Entering two-stage ELPA solver ... '
           print *
         end if

#ifdef WITH_MPI
         call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

1065
         tStart = seconds()
1066
1067
1068
1069
1070

         success = elpa_solve_evp_real_2stage_double(na, nev, a_real, na_rows, ev, z_real, na_rows,  nblk, na_cols, &
                                                     mpi_comm_rows, mpi_comm_cols, mpi_comm_world, useQr=this_qr, useGPU=this_gpu)

         if (.not.(success)) then
1071
1072
           write(error_unit,*) "solve_evp_real_2stage with default kernel ", &
             elpa_int_value_to_string("real_kernel", ELPA_2STAGE_REAL_DEFAULT), &
1073
1074
1075
                               " produced an error! Aborting..."
#ifdef WITH_MPI
           call MPI_ABORT(mpi_comm_world, 1, mpierr)
1076
1077
#else
           call exit(1)
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
#endif
         endif

         if (myid==0) then
           print '(a)','| Two-step ELPA solver complete.'
           print *
         end if

         if (myid == 0) print *,'Time transform to tridi :',time_evp_fwd
         if (myid == 0) print *,'Time solve tridi        :',time_evp_solve
         if (myid == 0) print *,'Time transform back EVs :',time_evp_back
         if (myid == 0) print *,'Total time (sum above)  :',time_evp_back+time_evp_solve+time_evp_fwd
         if (myid == 0) print *," "

#ifdef WITH_MPI
         call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif
1095
         tEnd = seconds()
1096
1097
1098

         if (myid == 0) print *," "
         if (myid == 0) print *,'Total time for solve_evp_real2_stage with ', &
1099
                        elpa_int_value_to_string("real_kernel", ELPA_2STAGE_REAL_DEFAULT),' default kernel:',tEnd - tStart
1100
1101
         if (myid == 0) print *," "

1102
         status = check_correctness_evp_numeric_residuals(na, nev, as_real, z_real, ev, sc_desc, myid)
1103
1104
1105
         if (myid == 0) print *," "

         if (status .eq. 1) then
1106
1107
           if (myid == 0) print *," ERROR in solve_evp_real2_stage with ", &
             elpa_int_value_to_string("real_kernel", ELPA_2STAGE_REAL_DEFAULT), &
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
             ' kernel!'
#ifdef WITH_MPI
           call blacs_gridexit(my_blacs_ctxt)
           call mpi_finalize(mpierr)
#endif

           call EXIT(STATUS)
         endif
         if (myid .eq. 0) print *," "


         if (myid .eq. 0) print *," "
         if (myid .eq. 0) print *,"Iterating over all available ELPA2 real kernels ..."
         if (myid .eq. 0) print *," "

1123
1124
1125
         do i = 0, elpa_option_cardinality("real_kernel")
           this_kernel = elpa_option_enumerate("real_kernel", i)

1126
1127
1128
1129
1130
1131
1132
1133
1134
           if (input_options%useGPUIsSet .eq. 1) this_gpu = .true.
           if (input_options%useGPUIsSet .eq. 0) this_gpu = .false.

           if (input_options%useQrIsSet .eq. 1) this_qr = .true.
           if (input_options%useQrIsSet .eq. 0) this_qr = .false.


           a_real = as_real
           z_real = a_real
1135
           if (AVAILABLE_REAL_ELPA_KERNELS(this_kernel) == 1) then
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
             if (input_options%useQrIsSet .eq. 0) then
               if (myid == 0) print *,"ELPA2 kernel ",trim(elpa_real_kernel_name(this_kernel)),":"
             else
               if (myid == 0) print *,"ELPA2 kernel ",trim(elpa_real_kernel_name(this_kernel))," with qr decompostion:"
             endif
             if (myid==0) then
               print *," "
               print '(a)','| Entering two-stage ELPA solver ... '
               print *
             end if
#ifdef WITH_MPI
             call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

1150
             tStart = seconds()
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165

             success = elpa_solve_evp_real_2stage_double(na, nev, a_real, na_rows, ev, z_real, na_rows,  nblk, na_cols, &
                                                         mpi_comm_rows, mpi_comm_cols, mpi_comm_world,        &
                                                         THIS_ELPA_KERNEL_API = this_kernel, useQR=this_qr, useGPU=this_gpu)

             if (.not.(success)) then
               if (input_options%useQrIsSet .eq. 0) then
                 write(error_unit,*) "solve_evp_real_2stage with kernel ",trim(elpa_real_kernel_name(this_kernel)), &
                                     " produced an error! Aborting..."
               else
                 write(error_unit,*) "solve_evp_real_2stage with kernel ",trim(elpa_real_kernel_name(this_kernel)), &
                                     " and qr-decompostion produced an error! Aborting..."

               endif
#ifdef WITH_MPI
1166
1167
1168
               call MPI_ABORT(mpi_comm_world, 1, mpierr)
#else
               call exit(1)
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
#endif
             endif

             if (myid==0) then
               print '(a)','| Two-step ELPA solver complete.'
               print *
             end if

             if (myid == 0) print *,'Time transform to tridi :',time_evp_fwd
             if (myid == 0) print *,'Time solve tridi        :',time_evp_solve
             if (myid == 0) print *,'Time transform back EVs :',time_evp_back
             if (myid == 0) print *,'Total time (sum above)  :',time_evp_back+time_evp_solve+time_evp_fwd
             if (myid == 0) print *," "
#ifdef WITH_MPI
             call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif
1185
             tEnd = seconds()
1186
1187
1188
1189
1190
1191

             if (myid == 0) print *," "
             if (myid == 0) print *,'Total time for solve_evp_real2_stage with ', &
                            trim(elpa_real_kernel_name(this_kernel)),' kernel:',tEnd - tStart
             if (myid == 0) print *," "

1192
             status = check_correctness_evp_numeric_residuals(na, nev, as_real, z_real, ev, sc_desc, myid)
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
             if (myid == 0) print *," "

             if (status .eq. 1) then
               if (input_options%useQrIsSet.eq. 0) then
                 if (myid == 0) print *," ERROR in solve_evp_real2_stage with ",trim(elpa_real_kernel_name(this_kernel)), &
                 ' kernel!'
               else
                 if (myid == 0) print *," ERROR in solve_evp_real2_stage with ",trim(elpa_real_kernel_name(this_kernel)), &
                 ' kernel and qr-decompostion!'
               endif
#ifdef WITH_MPI
               call blacs_gridexit(my_blacs_ctxt)
               call mpi_finalize(mpierr)
#endif

               call EXIT(STATUS)
             endif ! status == 1

           endif ! elpa_real_kernel
         enddo  ! kernel loop

       else ! realKernelSet

         if (input_options%useGPUIsSet .eq. 1) this_gpu=.true.
         if (input_options%useGPUIsSet .eq. 0) this_gpu=.false.

         if (input_options%useQrIsSet .eq. 1) this_qr = .true.
         if (input_options%useQrIsSet .eq. 0) this_qr = .false.


         a_real = as_real
         z_real = a_real
1225
         if (AVAILABLE_REAL_ELPA_KERNELS(input_options%this_real_kernel) == 1) then
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
           if (input_options%useQrIsSet  .eq. 0) then
             if (myid == 0) print *,"ELPA2 kernel ",trim(elpa_real_kernel_name(input_options%this_real_kernel)),":"
           else
             if (myid == 0) print *,"ELPA2 kernel ",trim(elpa_real_kernel_name(input_options%this_real_kernel)), &
                 " with qr decompostion:"
           endif
           if (myid==0) then
             print *," "
             print '(a)','| Entering two-stage ELPA solver ... '
             print *
           end if
#ifdef WITH_MPI
           call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

1241
           tStart = seconds()
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260

           success = elpa_solve_evp_real_2stage_double(na, nev, a_real, na_rows, ev, z_real, na_rows,  nblk, na_cols, &
                                                       mpi_comm_rows, mpi_comm_cols, mpi_comm_world,        &
                                                       THIS_ELPA_KERNEL_API = input_options%this_real_kernel, useQR=this_qr, &
                                                       useGPU=this_gpu)

           if (.not.(success)) then
             if (input_options%useQrIsSet  .eq. 0) then
               write(error_unit,*) "solve_evp_real_2stage with kernel ", &
                    trim(elpa_real_kernel_name(input_options%this_real_kernel)), &
                                   " produced an error! Aborting..."
             else
               write(error_unit,*) "solve_evp_real_2stage with kernel ", &
                   trim(elpa_real_kernel_name(input_options%this_real_kernel)), &
                                   " and qr-decompostion produced an error! Aborting..."

             endif
#ifdef WITH_MPI
             call MPI_ABORT(mpi_comm_world, 1, mpierr)
1261
1262
#else
             call exit(1)
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
#endif
           endif

           if (myid==0) then
             print '(a)','| Two-step ELPA solver complete.'
             print *
           end if

           if (myid == 0) print *,'Time transform to tridi :',time_evp_fwd
           if (myid == 0) print *,'Time solve tridi        :',time_evp_solve
           if (myid == 0) print *,'Time transform back EVs :',time_evp_back
           if (myid == 0) print *,'Total time (sum above)  :',time_evp_back+time_evp_solve+time_evp_fwd
           if (myid == 0) print *," "
#ifdef WITH_MPI
           call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif
1279
           tEnd = seconds()
1280
1281
1282
1283
1284
1285

           if (myid == 0) print *," "
           if (myid == 0) print *,'Total time for solve_evp_real2_stage with ', &
                          trim(elpa_real_kernel_name(input_options%this_real_kernel)),' kernel:',tEnd - tStart
           if (myid == 0) print *," "

1286
           status = check_correctness_evp_numeric_residuals(na, nev, as_real, z_real, ev, sc_desc, myid)
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
           if (myid == 0) print *," "

           if (status .eq. 1) then
             if (input_options%useQrIsSet  .eq. 0) then
               if (myid == 0) print *," ERROR in solve_evp_real2_stage with ", &
                   trim(elpa_real_kernel_name(input_options%this_real_kernel)), &
               ' kernel!'
             else
               if (myid == 0) print *," ERROR in solve_evp_real2_stage with ", &
                   trim(elpa_real_kernel_name(input_options%this_real_kernel)), &
               ' kernel and qr-decompostion!'
             endif
#ifdef WITH_MPI
             call blacs_gridexit(my_blacs_ctxt)
             call mpi_finalize(mpierr)
#endif

             call EXIT(STATUS)
           endif

         endif
       endif ! realKernelSet
     endif ! datatype == 1
   endif ! do2Stage

   if (input_options%do2stage) then
     if (input_options%datatype .eq. 2) then
       ! complex cases

       if (.not.(input_options%complexKernelIsSet)) then
         ! start again with ELPA2 generic and so forth

         ! first default kernel

         if (myid .eq. 0) print *," "
1322
1323
         if (myid .eq. 0) print *,"Testing 2stage solver with default kernel: ", &
           elpa_int_value_to_string("complex_kernel", ELPA_2STAGE_COMPLEX_DEFAULT)
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
         if (myid .eq. 0) print *," "

         a_complex = as_complex
         z_complex = a_complex

         if (myid==0) then
           print *," "
           print '(a)','| Entering two-stage ELPA solver ... '
           print *
         end if

         if (input_options%useGPUIsSet .eq. 1) this_gpu = .true.
         if (input_options%useGPUIsSet .eq. 0) this_gpu = .false.

         if (input_options%useQrIsSet .eq. 1) then
           print *,"error: nr QR decomposition for complex case available!"
           stop 1
         endif
#ifdef WITH_MPI
         call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

1346
         tStart = seconds()
1347
1348
1349
1350
1351

         success = elpa_solve_evp_complex_2stage_double(na, nev, a_complex, na_rows, ev, z_complex, na_rows,  nblk, na_cols, &
                                                        mpi_comm_rows, mpi_comm_cols, mpi_comm_world, useGPU=this_gpu)

         if (.not.(success)) then
1352
1353
           write(error_unit,*) "solve_evp_complex_2stage with default kernel ", &
             elpa_int_value_to_string("complex_kernel", ELPA_2STAGE_COMPLEX_DEFAULT), &
1354
1355
1356
                               " produced an error! Aborting..."
#ifdef WITH_MPI
           call MPI_ABORT(mpi_comm_world, 1, mpierr)
1357
1358
#else
           call exit(1)
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
#endif
         endif

         if (myid==0) then
           print '(a)','| Two-step ELPA solver complete.'
           print *
         end if

         if (myid == 0) print *,'Time transform to tridi :',time_evp_fwd
         if (myid == 0) print *,'Time solve tridi        :',time_evp_solve
         if (myid == 0) print *,'Time transform back EVs :',time_evp_back
         if (myid == 0) print *,'Total time (sum above)  :',time_evp_back+time_evp_solve+time_evp_fwd
         if (myid == 0) print *," "

#ifdef WITH_MPI
         call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif
1376
         tEnd = seconds()
1377
1378
1379

         if (myid == 0) print *," "
         if (myid == 0) print *,'Total time for solve_evp_complex_2stage with ', &
1380
                        elpa_int_value_to_string("complex_kernel", ELPA_2STAGE_COMPLEX_DEFAULT),' default kernel:',tEnd - tStart
1381
1382
         if (myid == 0) print *," "

1383
         status = check_correctness_evp_numeric_residuals(na, nev, as_complex, z_complex, ev, sc_desc, myid)
1384
1385
1386
         if (myid == 0) print *," "

         if (status .eq. 1) then
1387
1388
           if (myid == 0) print *," ERROR in solve_evp_complex_2stage with ", &
             elpa_int_value_to_string("complex_kernel", ELPA_2STAGE_COMPLEX_DEFAULT), &
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
             ' kernel!'
#ifdef WITH_MPI
           call blacs_gridexit(my_blacs_ctxt)
           call mpi_finalize(mpierr)
#endif

           call EXIT(STATUS)
         endif
         if (myid .eq. 0) print *," "


         if (myid .eq. 0) print *," "
         if (myid .eq. 0) print *,"Iterating over all available ELPA2 complex kernels ..."
         if (myid .eq. 0) print *," "

1404
1405
1406
         do i = 0, elpa_option_cardinality("complex_kernel")
           this_kernel = elpa_option_enumerate("complex_kernel", i)

1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
           if (input_options%useGPUIsSet .eq. 1) this_gpu=.true.
           if (input_options%useGPUIsSet .eq. 0) this_gpu=.false.

           if (input_options%useQrIsSet .eq. 1) then
             print *,"error: no QR-decomposition for complex case available!"
             stop 1
           endif

           a_complex = as_complex
           z_complex = a_complex
1417
           if (AVAILABLE_COMPLEX_ELPA_KERNELS(this_kernel) == 1) then
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
             if (input_options%useQrIsSet  .eq. 0) then
               if (myid == 0) print *,"ELPA2 kernel ",trim(elpa_complex_kernel_name(this_kernel)),":"
             else
               if (myid == 0) print *,"ELPA2 kernel ",trim(elpa_complex_kernel_name(this_kernel))," with qr decompostion:"
             endif
             if (myid==0) then
               print *," "
               print '(a)','| Entering two-stage ELPA solver ... '
               print *
             end if
#ifdef WITH_MPI
             call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

1432
             tStart = seconds()
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448

             success = elpa_solve_evp_complex_2stage_double(na, nev, a_complex, na_rows, ev, z_complex, na_rows,  nblk, na_cols, &
                                                            mpi_comm_rows, mpi_comm_cols, mpi_comm_world,        &
                                                            THIS_ELPA_KERNEL_API = this_kernel, useGPU=this_gpu)

             if (.not.(success)) then
               if (input_options%useQrIsSet  .eq. 0) then
                 write(error_unit,*) "solve_evp_complex_2stage with kernel ",trim(elpa_complex_kernel_name(this_kernel)), &
                                     " produced an error! Aborting..."
               else
                 write(error_unit,*) "solve_evp_complex_2stage with kernel ",trim(elpa_complex_kernel_name(this_kernel)), &
                                     " and qr-decompostion produced an error! Aborting..."

               endif
#ifdef WITH_MPI
               call MPI_ABORT(mpi_comm_world, 1, mpierr)
1449
1450
#else
               call exit(1)
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
#endif
             endif

             if (myid==0) then
               print '(a)','| Two-step ELPA solver complete.'
               print *
             end if

             if (myid == 0) print *,'Time transform to tridi :',time_evp_fwd
             if (myid == 0) print *,'Time solve tridi        :',time_evp_solve
             if (myid == 0) print *,'Time transform back EVs :',time_evp_back
             if (myid == 0) print *,'Total time (sum above)  :',time_evp_back+time_evp_solve+time_evp_fwd
             if (myid == 0) print *," "
#ifdef WITH_MPI
             call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif
1467
             tEnd = seconds()
1468
1469
1470
1471
1472
1473

             if (myid == 0) print *," "
             if (myid == 0) print *,'Total time for solve_evp_complex_2stage with ', &
                              trim(elpa_complex_kernel_name(this_kernel)),' kernel:',tEnd - tStart
             if (myid == 0) print *," "

1474
             status = check_correctness_evp_numeric_residuals(na, nev, as_complex, z_complex, ev, sc_desc, myid)
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
             if (myid == 0) print *," "

             if (status .eq. 1) then
               if (input_options%useQrIsSet  .eq. 0) then
                 if (myid == 0) print *," ERROR in solve_evp_complex_2stage with ",trim(elpa_complex_kernel_name(this_kernel)), &
                 ' kernel!'
               else
                 if (myid == 0) print *," ERROR in solve_evp_complex_2stage with ",trim(elpa_complex_kernel_name(this_kernel)), &
                 ' kernel and qr-decompostion!'
               endif
#ifdef WITH_MPI
               call blacs_gridexit(my_blacs_ctxt)
               call mpi_finalize(mpierr)
#endif

               call EXIT(STATUS)
             endif

           endif
         enddo

       else ! complexKernelSet

         a_complex = as_complex
         z_complex = a_complex
1500
         if (AVAILABLE_COMPLEX_ELPA_KERNELS(input_options%this_complex_kernel) == 1) then
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
           if (input_options%useQrIsSet  .eq. 0) then
             if (myid == 0) print *,"ELPA2 kernel ",trim(elpa_complex_kernel_name(input_options%this_complex_kernel)),":"
           else
             if (myid == 0) print *,"ELPA2 kernel ",trim(elpa_complex_kernel_name(input_options%this_complex_kernel)), &
                 " with qr decompostion:"
           endif
           if (myid==0) then
             print *," "
             print '(a)','| Entering two-stage ELPA solver ... '
             print *
           end if

           if (input_options%useGPUIsSet .eq. 1) this_gpu=.true.
           if (input_options%useGPUIsSet .eq. 0) this_gpu=.false.

           if (input_options%useQrIsSet .eq. 1) then
             print *,"error: no QR-decomposition for complex case available!"
             stop 1
           endif


#ifdef WITH_MPI
           call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif

1526
           tStart = seconds()
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545

           success = elpa_solve_evp_complex_2stage_double(na, nev, a_complex, na_rows, ev, z_complex, na_rows,  nblk, na_cols, &
                                                          mpi_comm_rows, mpi_comm_cols, mpi_comm_world,        &
                                                          THIS_ELPA_KERNEL_API = input_options%this_complex_kernel, &
                                                          useGPU=this_gpu)

           if (.not.(success)) then
             if (input_options%useQrIsSet  .eq. 0) then
               write(error_unit,*) "solve_evp_complex_2stage with kernel ", &
                   trim(elpa_complex_kernel_name(input_options%this_complex_kernel)), &
                                   " produced an error! Aborting..."
             else
               write(error_unit,*) "solve_evp_complex_2stage with kernel ", &
                   trim(elpa_complex_kernel_name(input_options%this_complex_kernel)), &
                                   " and qr-decompostion produced an error! Aborting..."

             endif
#ifdef WITH_MPI
             call MPI_ABORT(mpi_comm_world, 1, mpierr)
1546
1547
#else
             call exit(1)
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
#endif
           endif

           if (myid==0) then
             print '(a)','| Two-step ELPA solver complete.'
             print *
           end if

           if (myid == 0) print *,'Time transform to tridi :',time_evp_fwd
           if (myid == 0) print *,'Time solve tridi        :',time_evp_solve
           if (myid == 0) print *,'Time transform back EVs :',time_evp_back
           if (myid == 0) print *,'Total time (sum above)  :',time_evp_back+time_evp_solve+time_evp_fwd
           if (myid == 0) print *," "
#ifdef WITH_MPI
           call mpi_barrier(mpi_comm_world, mpierr) ! for correct timings only
#endif
1564
           tEnd = seconds()
1565
1566
1567
1568
1569
1570

           if (myid == 0) print *," "
           if (myid == 0) print *,'Total time for solve_evp_complex_2stage with ', &
                          trim(elpa_complex_kernel_name(input_options%this_complex_kernel)),' kernel:',tEnd - tStart
           if (myid == 0) print *," "

1571
           status = check_correctness_evp_numeric_residuals(na, nev, as_complex, z_complex, ev, sc_desc, myid)
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
           if (myid == 0) print *," "

           if (status .eq. 1) then
             if (input_options%useQrIsSet  .eq. 0) then
               if (myid == 0) print *," ERROR in solve_evp_complex_2stage with ", &
                 trim(elpa_complex_kernel_name(input_options%this_complex_kernel)), ' kernel!'
             else
               if (myid == 0) print *," ERROR in solve_evp_complex_2stage with ", &
                 trim(elpa_complex_kernel_name(input_options%this_complex_kernel)), ' kernel and qr-decompostion!'
             endif
#ifdef WITH_MPI
             call blacs_gridexit(my_blacs_ctxt)
             call mpi_finalize(mpierr)
#endif

             call EXIT(STATUS)
           endif

         endif
       endif ! realKernelSet
     endif ! datatype == 1
   endif ! do2Stage

   if (input_options%datatype .eq. 1) then
     deallocate(a_real)
     deallocate(as_real)

     deallocate(z_real)
     deallocate(tmp1_real)
     deallocate(tmp2_real)
Andreas Marek's avatar
Andreas Marek committed
1602
1603
1604
1605
1606
1607
1608
1609
1610

     if (input_options%doInvertTrm .or. input_options%doTransposeMultiply) then
       deallocate(b_real)
       deallocate(bs_real)
     endif

     if (input_options%doTransposeMultiply) then
       deallocate(c_real)
     endif
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
   endif

   if (input_options%datatype .eq. 2) then
     deallocate(a_complex)
     deallocate(as_complex)

     deallocate(z_complex)
     deallocate(tmp1_complex)
     deallocate(tmp2_complex)

Andreas Marek's avatar
Andreas Marek committed
1621
1622
1623
1624
1625
1626
1627
1628
     if (input_options%doInvertTrm .or. input_options%doTransposeMultiply) then
       deallocate(b_complex)
       deallocate(bs_complex)
     endif

     if (input_options%doTransposeMultiply) then
       deallocate(c_complex)
     endif
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
   endif

   deallocate(ev)

#ifdef WITH_MPI
   call blacs_gridexit(my_blacs_ctxt)
   call mpi_finalize(mpierr)
#endif

   call EXIT(STATUS)


end

!-------------------------------------------------------------------------------