elpa_tests.F90 57 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
59
!    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".
!
!-------------------------------------------------------------------------------
   use precision
60
   use elpa1_legacy
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
   use elpa2
   use elpa_utilities, only : error_unit, map_global_array_index_to_local_index
   use elpa2_utilities
#ifdef WITH_OPENMP
   use test_util
#endif

   use mod_read_input_parameters
   use mod_check_correctness
   use mod_setup_mpi
   use mod_blacs_infrastructure
   use mod_prepare_matrix

   use elpa_mpi
#ifdef HAVE_REDIRECT
   use redirect
#endif
#ifdef HAVE_DETAILED_TIMINGS
  use timings
#endif
  use output_types

   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
102
   real(kind=rk8), allocatable            :: ev(:)
103
104
105
   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
106
107
108
   real(kind=rk8), allocatable, target    :: b_real(:,:), bs_real(:,:), c_real(:,:)
   complex(kind=ck8), allocatable, target :: b_complex(:,:), bs_complex(:,:), c_complex(:,:)

109
110
111
   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
112
   complex(kind=ck8)                      :: diagonalElement_complex
113
   real(kind=rk8), target                 :: tmp
Andreas Marek's avatar
Andreas Marek committed
114
   real(kind=rk8)                         :: norm, normmax
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

   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
132
133
134
135
136
#ifdef WITH_MPI
   real(kind=rk8)                         :: pzlange, pdlange
#else
   real(kind=rk8)                         :: zlange, dlange
#endif
137
138
139
140
141
142
143
144
145
146
147
148

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


   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
149
150
151
152
   if (input_options%justHelpMessage) then
     call EXIT(0)
   endif

153
154
155
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

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


   STATUS = 0

!#define DATATYPE REAL !!! check here
!#include "elpa_print_headers.X90"

#ifdef HAVE_DETAILED_TIMINGS

   ! initialise the timing functionality

#ifdef HAVE_LIBPAPI
   call timer%measure_flops(.true.)
#endif

   call timer%measure_allocated_memory(.true.)
   call timer%measure_virtual_memory(.true.)
   call timer%measure_max_allocated_memory(.true.)

   call timer%set_print_options(&
#ifdef HAVE_LIBPAPI
                print_flop_count=.true., &
                print_flop_rate=.true., &
#endif
                print_allocated_memory = .true. , &
                print_virtual_memory=.true., &
                print_max_allocated_memory=.true.)


  call timer%enable()

  call timer%start("program")
#endif

   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
   ! rows or columns of processes, these are set in get_elpa_communicators.

   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
#ifdef HAVE_DETAILED_TIMINGS
   call timer%start("allocate arrays")
#endif

   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
256
257
258
259
260
261
262
263
264

     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
265
266
267
268
269
270
   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
271
272
273
274
275
276
277
278
     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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
   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

#ifdef HAVE_DETAILED_TIMINGS
   call timer%stop("allocate arrays")
#endif
Andreas Marek's avatar
Andreas Marek committed
301
302
303
304
305
306
307


#ifdef HAVE_DETAILED_TIMINGS
   call timer%start("set up matrix")
#endif

   if (input_options%datatype .eq. 1) then
308
     call prepare_matrix_double(na, myid, sc_desc, a_real, z_real, as_real)
Andreas Marek's avatar
Andreas Marek committed
309
310
311
312
313
314
315

     if (input_options%doInvertTrm) then
       b_real(:,:) = a_real(:,:)
       bs_real(:,:) = a_real(:,:)
     endif
   endif
   if (input_options%datatype .eq. 2) then
316
     call prepare_matrix_double(na, myid, sc_desc, a_complex, z_complex, as_complex)
Andreas Marek's avatar
Andreas Marek committed
317
318
319
320
321
322
323
324
325
326
327
328
     if (input_options%doInvertTrm) then
       b_complex(:,:) = a_complex(:,:)
       bs_complex(:,:) = a_complex(:,:)
     endif

   endif


#ifdef HAVE_DETAILED_TIMINGS
   call timer%stop("set up matrix")
#endif

329
330
331
332
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
   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
#ifdef HAVE_DETAILED_TIMINGS
       call timer%start("set up matrix")
#endif

       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

#ifdef HAVE_DETAILED_TIMINGS
       call timer%stop("set up matrix")
#endif

       !-------------------------------------------------------------------------------
       ! 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)
#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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    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
462
463
#endif

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

Andreas Marek's avatar
Andreas Marek committed
466
467
468
469
470
471
        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)
#endif
        endif
472

Andreas Marek's avatar
Andreas Marek committed
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490

        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)
491
492
#endif

Andreas Marek's avatar
Andreas Marek committed
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
570
571
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
597
598
599
600
601
602
603
604
605
606
607
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
        ! 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)
#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)
#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)
#endif
        endif


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

Andreas Marek's avatar
Andreas Marek committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
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
781
782
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
851
852
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946

        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)
#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)
#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)
#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)
#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

!#ifdef HAVE_DETAILED_TIMINGS
!   call timer%start("set up matrix")
!#endif
!
!   if (input_options%datatype .eq. 0) then
947
!     call prepare_matrix_double(na, myid, sc_desc, a_real, z_real, as_real)
Andreas Marek's avatar
Andreas Marek committed
948
949
!   endif
!   if (input_options%datatype .eq. 1) then
950
!     call prepare_matrix_double(na, myid, sc_desc, a_complex, z_complex, as_complex)
Andreas Marek's avatar
Andreas Marek committed
951
952
953
954
955
956
!   endif
!
!
!#ifdef HAVE_DETAILED_TIMINGS
!   call timer%stop("set up matrix")
!#endif
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
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


   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
     tStart = mpi_wtime()

     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)
#endif
     endif


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