test_check_correctness_template.F90 26.4 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
!    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.
!
! Author: A. Marek, MPCDF

44
    function check_correctness_evp_numeric_residuals_&
45 46 47
    &MATH_DATATYPE&
    &_&
    &PRECISION&
Pavel Kus's avatar
Pavel Kus committed
48
    & (na, nev, as, z, ev, sc_desc, nblk, myid, np_rows, np_cols, my_prow, my_pcol) result(status)
49
      implicit none
50
#include "../../src/general/precision_kinds.F90"
51
      integer(kind=ik)                 :: status
Pavel Kus's avatar
Pavel Kus committed
52
      integer(kind=ik), intent(in)     :: na, nev, nblk, myid, np_rows, np_cols, my_prow, my_pcol
53
#if REALCASE == 1
54 55 56 57
      real(kind=rck), intent(in)     :: as(:,:), z(:,:)
      real(kind=rck)                 :: ev(:)
      real(kind=rck), dimension(size(as,dim=1),size(as,dim=2)) :: tmp1, tmp2
      real(kind=rck)                :: xc
58

59 60 61
#ifndef WITH_MPI

#ifdef DOUBLE_PRECISION_REAL
62
      real(kind=rck)                 :: dnrm2
63
#else
64
      real(kind=rck)                 :: snrm2
65 66 67 68 69 70
#endif

#endif
#endif /* REALCASE */

#if COMPLEXCASE == 1
71 72 73 74
      complex(kind=rck), intent(in)    :: as(:,:), z(:,:)
      real(kind=rck)                   :: ev(:)
      complex(kind=rck), dimension(size(as,dim=1),size(as,dim=2)) :: tmp1, tmp2
      complex(kind=rck)                :: xc
75 76
#ifdef DOUBLE_PRECISION_COMPLEX
#ifndef WITH_MPI
77
      complex(kind=rck)                :: zdotc, cdotc
78 79 80 81
#endif

#else /* DOUBLE_PRECISION_COMPLEX */
#ifndef WITH_MPI
82
      complex(kind=rck)                :: zdotc, cdotc
83 84 85 86 87 88 89 90
#endif

#endif /* DOUBLE_PRECISION_COMPLEX */

#endif /* COMPLEXCASE */

      integer(kind=ik)                 :: sc_desc(:)

Pavel Kus's avatar
Pavel Kus committed
91
      integer(kind=ik)                 :: i, rowLocal, colLocal
92
      real(kind=rck)                   :: err, errmax
93 94 95 96 97 98 99 100 101 102

      integer :: mpierr

      status = 0

      ! 1. Residual (maximum of || A*Zi - Zi*EVi ||)
      ! tmp1 =  A * Z
      ! as is original stored matrix, Z are the EVs

#ifdef WITH_MPI
103 104
      call scal_PRECISION_GEMM('N', 'N', na, nev, na, ONE, as, 1, 1, sc_desc, &
                  z, 1, 1, sc_desc, ZERO, tmp1, 1, 1, sc_desc)
105
#else /* WITH_MPI */
106
      call PRECISION_GEMM('N','N',na,nev,na,ONE,as,na,z,na,ZERO,tmp1,na)
107 108 109 110 111 112
#endif /* WITH_MPI */


      ! tmp2 = Zi*EVi
      tmp2(:,:) = z(:,:)
      do i=1,nev
Pavel Kus's avatar
Pavel Kus committed
113
        xc = ev(i)
114 115 116 117
#if REALCASE == 1
#ifdef WITH_MPI

#ifdef DOUBLE_PRECISION_REAL
Pavel Kus's avatar
Pavel Kus committed
118
        call pdscal(na, xc, tmp2, 1, i, sc_desc, 1)
119
#else
Pavel Kus's avatar
Pavel Kus committed
120
        call psscal(na, xc, tmp2, 1, i, sc_desc, 1)
121 122 123 124 125
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_REAL
Pavel Kus's avatar
Pavel Kus committed
126
        call dscal(na,xc,tmp2(:,i),1)
127
#else
Pavel Kus's avatar
Pavel Kus committed
128
        call sscal(na,xc,tmp2(:,i),1)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#endif

#endif /* WITH_MPI */
#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef WITH_MPI

#ifdef DOUBLE_PRECISION_COMPLEX
        call pzscal(na, xc, tmp2, 1, i, sc_desc, 1)
#else
        call pcscal(na, xc, tmp2, 1, i, sc_desc, 1)
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_COMPLEX
        call zscal(na,xc,tmp2(1,i),1)
#else
        call cscal(na,xc,tmp2(1,i),1)
#endif

#endif /* WITH_MPI */
#endif /* COMPLEXCASE */
      enddo

      !  tmp1 = A*Zi - Zi*EVi
      tmp1(:,:) =  tmp1(:,:) - tmp2(:,:)

      ! Get maximum norm of columns of tmp1
159
      errmax = 0.0_rk
160 161 162

      do i=1,nev
#if REALCASE == 1
163
        err = 0.0_rk
164
#ifdef WITH_MPI
Pavel Kus's avatar
Pavel Kus committed
165
        call scal_PRECISION_NRM2(na, err, tmp1, 1, i, sc_desc, 1)
166
#else /* WITH_MPI */
Pavel Kus's avatar
Pavel Kus committed
167
        err = PRECISION_NRM2(na,tmp1(1,i),1)
168 169 170 171 172 173 174
#endif /* WITH_MPI */
        errmax = max(errmax, err)
#endif /* REALCASE */

#if COMPLEXCASE == 1
        xc = 0
#ifdef WITH_MPI
Pavel Kus's avatar
Pavel Kus committed
175
        call scal_PRECISION_DOTC(na, xc, tmp1, 1, i, sc_desc, 1, tmp1, 1, i, sc_desc, 1)
176
#else /* WITH_MPI */
Pavel Kus's avatar
Pavel Kus committed
177
        xc = PRECISION_DOTC(na,tmp1,1,tmp1,1)
178
#endif /* WITH_MPI */
179
        errmax = max(errmax, sqrt(real(xc,kind=REAL_DATATYPE)))
180 181 182 183 184 185
#endif /* COMPLEXCASE */
      enddo

      ! Get maximum error norm over all processors
      err = errmax
#ifdef WITH_MPI
186
      call mpi_allreduce(err, errmax, 1, MPI_REAL_PRECISION, MPI_MAX, MPI_COMM_WORLD, mpierr)
187 188 189
#else /* WITH_MPI */
      errmax = err
#endif /* WITH_MPI */
190
      if (myid==0) print *,'Results of numerical residual checks:'
191 192
      if (myid==0) print *,'Error Residual     :',errmax
#if REALCASE == 1
193
      if (nev .ge. 2) then
194
#ifdef DOUBLE_PRECISION_REAL
195
        if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
196
#else
197
        if (errmax .gt. 3e-3_rk4 .or. errmax .eq. 0.0_rk4) then
198
#endif
199 200 201 202 203 204 205 206 207 208 209
          status = 1
        endif
      else
#ifdef DOUBLE_PRECISION_REAL
        if (errmax .gt. 5e-12_rk8) then
#else
        if (errmax .gt. 3e-3_rk4) then
#endif
          status = 1
        endif
      endif
210 211
#endif
#if COMPLEXCASE == 1
212
      if (nev .gt. 2) then
213
#ifdef DOUBLE_PRECISION_COMPLEX
214
        if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
215
#else
216
        if (errmax .gt. 3e-3_rk4 .or. errmax .eq. 0.0_rk4) then
217
#endif
218 219 220 221 222 223 224
          status =1
        endif
      else
#ifdef DOUBLE_PRECISION_COMPLEX
        if (errmax .gt. 5e-12_rk8) then
#else
        if (errmax .gt. 3e-3_rk4) then
225
#endif
226 227
          status = 1
        endif
228
      endif
229
#endif
230 231 232 233 234 235

      ! 2. Eigenvector orthogonality

      ! tmp1 = Z**T * Z
      tmp1 = 0
#ifdef WITH_MPI
236 237
      call scal_PRECISION_GEMM(BLAS_TRANS_OR_CONJ, 'N', nev, nev, na, ONE, z, 1, 1, &
                        sc_desc, z, 1, 1, sc_desc, ZERO, tmp1, 1, 1, sc_desc)
238
#else /* WITH_MPI */
239
      call PRECISION_GEMM(BLAS_TRANS_OR_CONJ,'N',nev,nev,na,ONE,z,na,z,na,ZERO,tmp1,na)
240
#endif /* WITH_MPI */
Pavel Kus's avatar
Pavel Kus committed
241 242
      !TODO for the C interface, not all information is passed (zeros instead)
      !TODO than this part of the test cannot be done
243
      !TODO either we will not have this part of test at all, or it will be improved
Pavel Kus's avatar
Pavel Kus committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
      if(nblk > 0) then
        ! First check, whether the elements on diagonal are 1 .. "normality" of the vectors
        err = CONST_REAL_0_0
        do i=1, nev
          if (map_global_array_index_to_local_index(i, i, rowLocal, colLocal, nblk, np_rows, np_cols, my_prow, my_pcol)) then
             err = max(err, abs(tmp1(rowLocal,colLocal) - CONST_REAL_1_0))
           endif
        end do
#ifdef WITH_MPI
        call mpi_allreduce(err, errmax, 1, MPI_REAL_PRECISION, MPI_MAX, MPI_COMM_WORLD, mpierr)
#else /* WITH_MPI */
        errmax = err
#endif /* WITH_MPI */
        if (myid==0) print *,'Maximal error in eigenvector lengths:',errmax
      end if
259

Pavel Kus's avatar
Pavel Kus committed
260
      ! Second, find the maximal error in the whole Z**T * Z matrix (its diference from identity matrix)
261 262 263
      ! Initialize tmp2 to unit matrix
      tmp2 = 0
#ifdef WITH_MPI
Pavel Kus's avatar
Pavel Kus committed
264
      call scal_PRECISION_LASET('A', nev, nev, ZERO, ONE, tmp2, 1, 1, sc_desc)
265
#else /* WITH_MPI */
Pavel Kus's avatar
Pavel Kus committed
266
      call PRECISION_LASET('A',nev,nev,ZERO,ONE,tmp2,na)
267 268 269 270 271 272 273 274
#endif /* WITH_MPI */

      !      ! tmp1 = Z**T * Z - Unit Matrix
      tmp1(:,:) =  tmp1(:,:) - tmp2(:,:)

      ! Get maximum error (max abs value in tmp1)
      err = maxval(abs(tmp1))
#ifdef WITH_MPI
275
      call mpi_allreduce(err, errmax, 1, MPI_REAL_PRECISION, MPI_MAX, MPI_COMM_WORLD, mpierr)
276 277 278 279 280 281
#else /* WITH_MPI */
      errmax = err
#endif /* WITH_MPI */

      if (myid==0) print *,'Error Orthogonality:',errmax
#if REALCASE == 1
282
      if (nev .ge. 2) then
283
#ifdef DOUBLE_PRECISION_REAL
284
        if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
285
#else
286
        if (errmax .gt. 9e-4_rk4 .or. errmax .eq. 0.0_rk4) then
287
#endif
288 289 290 291 292 293 294 295 296 297 298
          status = 1
        endif
      else
#ifdef DOUBLE_PRECISION_REAL
        if (errmax .gt. 5e-12_rk8) then
#else
        if (errmax .gt. 9e-4_rk4) then
#endif
          status = 1
        endif
      endif
299 300
#endif
#if COMPLEXCASE == 1
301 302 303 304 305 306 307 308 309
      if (nev .ge. 2) then
#ifdef DOUBLE_PRECISION_COMPLEX
        if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
#else
        if (errmax .gt. 9e-4_rk4 .or. errmax .eq. 0.0_rk4) then
#endif
          status = 1
        endif
      else
310
#ifdef DOUBLE_PRECISION_COMPLEX
311
        if (errmax .gt. 5e-12_rk8) then
312
#else
313
        if (errmax .gt. 9e-4_rk4) then
314
#endif
315 316 317
          status = 1
        endif
      endif
318 319 320 321 322 323 324 325
#endif

    end function


#if REALCASE == 1

#ifdef DOUBLE_PRECISION_REAL
326
    !c> int check_correctness_evp_numeric_residuals_real_double_f(int na, int nev, int na_rows, int na_cols,
327 328 329
    !c>                                         double *as, double *z, double *ev,
    !c>                                         int sc_desc[9], int myid);
#else
330
    !c> int check_correctness_evp_numeric_residuals_real_single_f(int na, int nev, int na_rows, int na_cols,
331 332 333 334 335 336 337 338
    !c>                                         float *as, float *z, float *ev,
    !c>                                         int sc_desc[9], int myid);
#endif

#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
339
    !c> int check_correctness_evp_numeric_residuals_complex_double_f(int na, int nev, int na_rows, int na_cols,
340 341 342
    !c>                                         complex double *as, complex double *z, double *ev,
    !c>                                         int sc_desc[9], int myid);
#else
343
    !c> int check_correctness_evp_numeric_residuals_complex_single_f(int na, int nev, int na_rows, int na_cols,
Andreas Marek's avatar
Andreas Marek committed
344
    !c>                                         complex float *as, complex float *z, float *ev,
345 346 347 348
    !c>                                         int sc_desc[9], int myid);
#endif
#endif /* COMPLEXCASE */

349
function check_correctness_evp_numeric_residuals_&
350 351 352 353
&MATH_DATATYPE&
&_&
&PRECISION&
&_f (na, nev, na_rows, na_cols, as, z, ev, sc_desc, myid) result(status) &
354
      bind(C,name="check_correctness_evp_numeric_residuals_&
355 356 357 358 359 360 361 362
      &MATH_DATATYPE&
      &_&
      &PRECISION&
      &_f")

      use iso_c_binding

      implicit none
363
#include "../../src/general/precision_kinds.F90"
364 365 366

      integer(kind=c_int)            :: status
      integer(kind=c_int), value     :: na, nev, myid, na_rows, na_cols
367 368
      MATH_DATATYPE(kind=rck)     :: as(1:na_rows,1:na_cols), z(1:na_rows,1:na_cols)
      real(kind=rck)    :: ev(1:na)
369 370
      integer(kind=c_int)            :: sc_desc(1:9)

Pavel Kus's avatar
Pavel Kus committed
371 372 373
      ! TODO: I did not want to add all the variables to the C interface as well
      ! TODO: I think that we should find a better way to pass this information
      ! TODO: to all the functions anyway (get it from sc_desc, pass elpa_t, etc..)
374
      status = check_correctness_evp_numeric_residuals_&
375 376 377
      &MATH_DATATYPE&
      &_&
      &PRECISION&
Pavel Kus's avatar
Pavel Kus committed
378
      & (na, nev, as, z, ev, sc_desc, 0, myid, 0, 0, 0, 0)
379 380 381

    end function

Andreas Marek's avatar
Andreas Marek committed
382 383 384 385 386 387 388
    function check_correctness_eigenvalues_toeplitz_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    & (na, diagonalElement, subdiagonalElement, ev, z, myid) result(status)
      use iso_c_binding
      implicit none
389
#include "../../src/general/precision_kinds.F90"
Andreas Marek's avatar
Andreas Marek committed
390 391 392

      integer               :: status, ii, j, myid
      integer, intent(in)   :: na
393 394 395
      real(kind=rck) :: diagonalElement, subdiagonalElement
      real(kind=rck) :: ev_analytic(na), ev(na)
      MATH_DATATYPE(kind=rck) :: z(:,:)
Andreas Marek's avatar
Andreas Marek committed
396 397

#if defined(DOUBLE_PRECISION_REAL) || defined(DOUBLE_PRECISION_COMPLEX)
398
      real(kind=rck), parameter   :: pi = 3.141592653589793238462643383279_c_double
Andreas Marek's avatar
Andreas Marek committed
399
#else
400
      real(kind=rck), parameter   :: pi = 3.1415926535897932_c_float
Andreas Marek's avatar
Andreas Marek committed
401
#endif
402
      real(kind=rck)              :: tmp, maxerr
403
      integer                     :: loctmp
Andreas Marek's avatar
Andreas Marek committed
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 443 444 445
      status = 0

     ! analytic solution
     do ii=1, na
#if defined(DOUBLE_PRECISION_REAL) || defined(DOUBLE_PRECISION_COMPLEX)
       ev_analytic(ii) = diagonalElement + 2.0_c_double * &
                         subdiagonalElement *cos( pi*real(ii,kind=c_double)/ &
			 real(na+1,kind=c_double) )
#else
       ev_analytic(ii) = diagonalElement + 2.0_c_float * &
                         subdiagonalElement *cos( pi*real(ii,kind=c_float)/ &
                         real(na+1,kind=c_float) )
#endif
     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)
     loctmp = minloc(ev_analytic, 1)

     ev_analytic(loctmp) = ev_analytic(1)
     ev_analytic(1) = tmp
     do ii=2, na
       tmp = ev_analytic(ii)
       do j= ii, na
         if (ev_analytic(j) .lt. tmp) then
           tmp    = ev_analytic(j)
           loctmp = j
         endif
       enddo
       ev_analytic(loctmp) = ev_analytic(ii)
       ev_analytic(ii) = tmp
     enddo

     ! compute a simple error max of eigenvalues
     maxerr = 0.0
     maxerr = maxval( (ev(:) - ev_analytic(:))/ev_analytic(:) , 1)

#if defined(DOUBLE_PRECISION_REAL) || defined(DOUBLE_PRECISION_COMPLEX)
446
     if (maxerr .gt. 8.e-13_c_double .or. maxerr .eq. 0.0_c_double) then
Andreas Marek's avatar
Andreas Marek committed
447
#else
448
     if (maxerr .gt. 8.e-4_c_float .or. maxerr .eq. 0.0_c_float) then
Andreas Marek's avatar
Andreas Marek committed
449 450 451 452 453 454 455
#endif
       status = 1
       if (myid .eq. 0) then
         print *,"Eigenvalues differ from analytic solution: maxerr = ",maxerr
       endif
     endif
    end function
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 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    function check_correctness_cholesky_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    & (na, a, as, na_rows, sc_desc, myid) result(status)
      implicit none
#include "../../src/general/precision_kinds.F90"
      integer(kind=ik)                 :: status
      integer(kind=ik), intent(in)     :: na, myid, na_rows
#if REALCASE == 1
      real(kind=rck), intent(in)       :: a(:,:), as(:,:)
      real(kind=rck), dimension(size(as,dim=1),size(as,dim=2)) :: tmp1, tmp2
      real(kind=rck)                   :: norm, normmax

#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL
      real(kind=rck)                   :: pdlange
#else
      real(kind=rck)                   :: pslange
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_REAL
      real(kind=rck)                   :: dlange
#else
      real(kind=rck)                   :: slange
#endif

#endif /* WITH_MPI */

#endif /* REALCASE */

#if COMPLEXCASE == 1
      complex(kind=rck), intent(in)    :: a(:,:), as(:,:)
      complex(kind=rck), dimension(size(as,dim=1),size(as,dim=2)) :: tmp1, tmp2
      real(kind=rck)                :: norm, normmax
#ifdef DOUBLE_PRECISION_COMPLEX
      complex(kind=ck8), parameter   :: CZERO = (0.0_rk8,0.0_rk8), CONE = (1.0_rk8,0.0_rk8)
#else
      complex(kind=ck4), parameter   :: CZERO = (0.0_rk4,0.0_rk4), CONE = (1.0_rk4,0.0_rk8)
#endif

#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX
Andreas Marek's avatar
Andreas Marek committed
502
      real(kind=rck)                   :: pzlange
503
#else
Andreas Marek's avatar
Andreas Marek committed
504
      real(kind=rck)                   :: pclange
505 506 507 508 509
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_COMPLEX
Andreas Marek's avatar
Andreas Marek committed
510
      real(kind=rck)                   :: zlange
511
#else
Andreas Marek's avatar
Andreas Marek committed
512
      real(kind=rck)                   :: clange
513 514 515 516 517 518 519 520 521 522 523 524 525 526
#endif

#endif /* WITH_MPI */
#endif /* COMPLEXCASE */

      integer(kind=ik)                 :: sc_desc(:)

      real(kind=rck)                   :: err, errmax

      integer :: mpierr

      status = 0

#if REALCASE == 1
527
#ifdef DOUBLE_PRECISION_REAL
528
      tmp1(:,:) = 0.0_rk8
529 530
#else
      tmp1(:,:) = 0.0_rk4
531
#endif
532 533
#endif /* REALCASE */

534
#if COMPLEXCASE == 1
535
#ifdef DOUBLE_PRECISION_COMPLEX
536
      tmp1(:,:) = 0.0_ck8
537 538
#else
      tmp1(:,:) = 0.0_ck4
539
#endif
540
#endif /* COMPLEXCASE */
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


#if REALCASE == 1
      ! tmp1 = a**T
#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL
      call pdtran(na, na, 1.0_rk8, a, 1, 1, sc_desc, 0.0_rk8, tmp1, 1, 1, sc_desc)
#else

      call pstran(na, na, 1.0_rk4, a, 1, 1, sc_desc, 0.0_rk4, tmp1, 1, 1, sc_desc)
#endif

#else /* WITH_MPI */
      tmp1 = transpose(a)
#endif /* WITH_MPI */

      ! tmp2 = a * a**T
#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL
      call pdgemm("N","N", na, na, na, 1.0_rk8, a, 1, 1, sc_desc, tmp1, 1, 1, &
               sc_desc, 0.0_rk8, tmp2, 1, 1, sc_desc)
#else
      call psgemm("N","N", na, na, na, 1.0_rk4, a, 1, 1, sc_desc, tmp1, 1, 1, &
               sc_desc, 0.0_rk4, tmp2, 1, 1, sc_desc)

#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_REAL
      call dgemm("N","N", na, na, na, 1.0_rk8, a, na, tmp1, na, 0.0_rk8, tmp2, na)
#else
      call sgemm("N","N", na, na, na, 1.0_rk4, a, na, tmp1, na, 0.0_rk4, tmp2, na)
#endif
#endif /* WITH_MPI */


#endif /* REALCASE == 1 */

#if COMPLEXCASE == 1
      ! tmp1 = a**H
#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX
      call pztranc(na, na, CONE, a, 1, 1, sc_desc, CZERO, tmp1, 1, 1, sc_desc)
#else

      call pctranc(na, na, CONE, a, 1, 1, sc_desc, CZERO, tmp1, 1, 1, sc_desc)
#endif

#else /* WITH_MPI */
      tmp1 = transpose(conjg(a))
#endif /* WITH_MPI */

      ! tmp2 = a * a**T
#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX
      call pzgemm("N","N", na, na, na, CONE, a, 1, 1, sc_desc, tmp1, 1, 1, &
               sc_desc, CZERO, tmp2, 1, 1, sc_desc)
#else
      call pcgemm("N","N", na, na, na, CONE, a, 1, 1, sc_desc, tmp1, 1, 1, &
               sc_desc, CZERO, tmp2, 1, 1, sc_desc)

#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_COMPLEX
      call zgemm("N","N", na, na, na, CONE, a, na, tmp1, na, CZERO, tmp2, na)
#else
      call cgemm("N","N", na, na, na, CONE, a, na, tmp1, na, CZERO, tmp2, na)
#endif
#endif /* WITH_MPI */


#endif /* COMPLEXCASE == 1 */

      ! compare tmp2 with original matrix
      tmp2(:,:) = tmp2(:,:) - as(:,:)
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 678 679 680 681 682 683
#if REALCASE == 1

#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL

      norm = pdlange("M",na, na, tmp2, 1, 1, sc_desc, tmp1)
#else
      norm = pslange("M",na, na, tmp2, 1, 1, sc_desc, tmp1)
#endif

#else /* WITH_MPI */
#ifdef DOUBLE_PRECISION_REAL
      norm = dlange("M", na, na, tmp2, na_rows, tmp1)
#else
      norm = slange("M", na, na, tmp2, na_rows, tmp1)

#endif
#endif /* WITH_MPI */


#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL
      call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
      call mpi_allreduce(norm,normmax,1,MPI_REAL4,MPI_MAX,MPI_COMM_WORLD,mpierr)
#endif
#else /* WITH_MPI */
      normmax = norm
#endif /* WITH_MPI */


#endif /* REALCASE == 1 */

#if COMPLEXCASE == 1

#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX

      norm = pzlange("M",na, na, tmp2, 1, 1, sc_desc, tmp1)
#else
      norm = pclange("M",na, na, tmp2, 1, 1, sc_desc, tmp1)
#endif

#else /* WITH_MPI */
#ifdef DOUBLE_PRECISION_COMPLEX
      norm = zlange("M", na, na, tmp2, na_rows, tmp1)
#else
      norm = clange("M", na, na, tmp2, na_rows, tmp1)

#endif
#endif /* WITH_MPI */


#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX
      call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
      call mpi_allreduce(norm,normmax,1,MPI_REAL4,MPI_MAX,MPI_COMM_WORLD,mpierr)
#endif
#else /* WITH_MPI */
      normmax = norm
#endif /* WITH_MPI */


684
#endif /* COMPLEXCASE == 1 */
685 686 687 688 689

      if (myid .eq. 0) then
        print *," Maximum error of result: ", normmax
      endif

690
#if REALCASE == 1
691
#ifdef DOUBLE_PRECISION_REAL
692
!      if (normmax .gt. 5e-12_rk8 .or. normmax .eq. 0.0_rk8) then
693 694 695 696
      if (normmax .gt. 5e-12_rk8) then
        status = 1
      endif
#else
697 698
!      if (normmax .gt. 5e-4_rk4 .or. normmax .eq. 0.0_rk4) then
      if (normmax .gt. 5e-4_rk4 ) then
699 700 701
        status = 1
      endif
#endif
702
#endif
703

704 705
#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
706 707
!      if (normmax .gt. 5e-11_rk8 .or. normmax .eq. 0.0_rk8) then
      if (normmax .gt. 5e-11_rk8 ) then
708 709 710
        status = 1
      endif
#else
711
!      if (normmax .gt. 5e-3_rk4 .or. normmax .eq. 0.0_rk4) then
712 713 714 715 716
      if (normmax .gt. 5e-3_rk4) then
        status = 1
      endif
#endif
#endif
717 718
    end function

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
    function check_correctness_hermitian_multiply_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    & (na, a, b, c, na_rows, sc_desc, myid) result(status)
      implicit none
#include "../../src/general/precision_kinds.F90"
      integer(kind=ik)                 :: status
      integer(kind=ik), intent(in)     :: na, myid, na_rows
#if REALCASE == 1
      real(kind=rck), intent(in)       :: a(:,:), b(:,:), c(:,:)
      real(kind=rck), dimension(size(a,dim=1),size(a,dim=2)) :: tmp1, tmp2
      real(kind=rck)                   :: norm, normmax

#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL
      real(kind=rck)                   :: pdlange
#else
      real(kind=rck)                   :: pslange
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_REAL
      real(kind=rck)                   :: dlange
#else
      real(kind=rck)                   :: slange
#endif

#endif /* WITH_MPI */

#endif /* REALCASE */

#if COMPLEXCASE == 1
      complex(kind=rck), intent(in)    :: a(:,:), b(:,:), c(:,:)
      complex(kind=rck), dimension(size(a,dim=1),size(a,dim=2)) :: tmp1, tmp2
      real(kind=rck)                :: norm, normmax
#ifdef DOUBLE_PRECISION_COMPLEX
      complex(kind=ck8), parameter   :: CZERO = (0.0_rk8,0.0_rk8), CONE = (1.0_rk8,0.0_rk8)
#else
      complex(kind=ck4), parameter   :: CZERO = (0.0_rk4,0.0_rk4), CONE = (1.0_rk4,0.0_rk8)
#endif

#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX
Andreas Marek's avatar
Andreas Marek committed
764
      real(kind=rck)                   :: pzlange
765
#else
Andreas Marek's avatar
Andreas Marek committed
766
      real(kind=rck)                   :: pclange
767 768 769 770 771
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_COMPLEX
Andreas Marek's avatar
Andreas Marek committed
772
      real(kind=rck)                   :: zlange
773
#else
Andreas Marek's avatar
Andreas Marek committed
774
      real(kind=rck)                   :: clange
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
#endif

#endif /* WITH_MPI */
#endif /* COMPLEXCASE */

      integer(kind=ik)                 :: sc_desc(:)

      real(kind=rck)                   :: err, errmax

      integer :: mpierr

      status = 0

#if REALCASE == 1

#ifdef DOUBLE_PRECISION_REAL
      tmp1(:,:) = 0.0_rk8
#else
      tmp1(:,:) = 0.0_rk4
#endif

#endif /* REALCASE */
797

798 799
#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
800
      tmp1(:,:) = (0.0_c_double, 0.0_c_double)
801
#else
802
      tmp1(:,:) = (0.0_c_float, 0.0_c_float)
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 947 948
#endif
#endif /* COMPLEXCASE */


#if REALCASE == 1
      ! tmp1 = a**T
#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL
      call pdtran(na, na, 1.0_rk8, a, 1, 1, sc_desc, 0.0_rk8, tmp1, 1, 1, sc_desc)
#else

      call pstran(na, na, 1.0_rk4, a, 1, 1, sc_desc, 0.0_rk4, tmp1, 1, 1, sc_desc)
#endif

#else /* WITH_MPI */
      tmp1 = transpose(a)
#endif /* WITH_MPI */

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

#else /* DOUBLE_PRECISION_REAL */
#ifdef WITH_MPI
   call psgemm("N","N", na, na, na, 1.0_rk4, tmp1, 1, 1, sc_desc, b, 1, 1, &
               sc_desc, 0.0_rk4, tmp2, 1, 1, sc_desc)
#else
   call sgemm("N","N", na, na, na, 1.0_rk4, tmp1, na, b, na, 0.0_rk4, tmp2, na)
#endif

#endif /* DOUBLE_PRECISION_REAL */

#endif /* REALCASE == 1 */

#if COMPLEXCASE == 1
      ! tmp1 = a**H
#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX
      call pztranc(na, na, CONE, a, 1, 1, sc_desc, CZERO, tmp1, 1, 1, sc_desc)
#else

      call pctranc(na, na, CONE, a, 1, 1, sc_desc, CZERO, tmp1, 1, 1, sc_desc)
#endif

#else /* WITH_MPI */
      tmp1 = transpose(conjg(a))
#endif /* WITH_MPI */

   ! tmp2 = tmp1 * b
#ifdef DOUBLE_PRECISION_COMPLEX
#ifdef WITH_MPI
   call pzgemm("N","N", na, na, na, CONE, tmp1, 1, 1, sc_desc, b, 1, 1, &
               sc_desc, CZERO, tmp2, 1, 1, sc_desc)
#else
   call zgemm("N","N", na, na, na, CONE, tmp1, na, b, na, CZERO, tmp2, na)
#endif

#else /* DOUBLE_PRECISION_COMPLEX */

#ifdef WITH_MPI
   call pcgemm("N","N", na, na, na, CONE, tmp1, 1, 1, sc_desc, b, 1, 1, &
               sc_desc, CZERO, tmp2, 1, 1, sc_desc)
#else
   call cgemm("N","N", na, na, na, CONE, tmp1, na, b, na, CZERO, tmp2, na)
#endif
#endif /* DOUBLE_PRECISION_COMPLEX */

#endif /* COMPLEXCASE == 1 */

      ! compare tmp2 with c
      tmp2(:,:) = tmp2(:,:) - c(:,:)
#if REALCASE == 1

#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL

      norm = pdlange("M",na, na, tmp2, 1, 1, sc_desc, tmp1)
#else
      norm = pslange("M",na, na, tmp2, 1, 1, sc_desc, tmp1)
#endif

#else /* WITH_MPI */
#ifdef DOUBLE_PRECISION_REAL
      norm = dlange("M", na, na, tmp2, na_rows, tmp1)
#else
      norm = slange("M", na, na, tmp2, na_rows, tmp1)

#endif
#endif /* WITH_MPI */


#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_REAL
      call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
      call mpi_allreduce(norm,normmax,1,MPI_REAL4,MPI_MAX,MPI_COMM_WORLD,mpierr)
#endif
#else /* WITH_MPI */
      normmax = norm
#endif /* WITH_MPI */


#endif /* REALCASE == 1 */

#if COMPLEXCASE == 1

#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX
      norm = pzlange("M",na, na, tmp2, 1, 1, sc_desc, tmp1)
#else
      norm = pclange("M",na, na, tmp2, 1, 1, sc_desc, tmp1)
#endif

#else /* WITH_MPI */
#ifdef DOUBLE_PRECISION_COMPLEX
      norm = zlange("M", na, na, tmp2, na_rows, tmp1)
#else
      norm = clange("M", na, na, tmp2, na_rows, tmp1)

#endif
#endif /* WITH_MPI */


#ifdef WITH_MPI
#ifdef DOUBLE_PRECISION_COMPLEX
      call mpi_allreduce(norm,normmax,1,MPI_REAL8,MPI_MAX,MPI_COMM_WORLD,mpierr)
#else
      call mpi_allreduce(norm,normmax,1,MPI_REAL4,MPI_MAX,MPI_COMM_WORLD,mpierr)
#endif
#else /* WITH_MPI */
      normmax = norm
#endif /* WITH_MPI */


#endif /* REALCASE == 1 */

      if (myid .eq. 0) then
        print *," Maximum error of result: ", normmax
      endif

#ifdef DOUBLE_PRECISION_REAL
949
      if (normmax .gt. 5e-11_rk8 ) then
950 951 952
        status = 1
      endif
#else
953
      if (normmax .gt. 5e-3_rk4 ) then
954 955 956 957
        status = 1
      endif
#endif

958 959 960 961 962 963 964 965 966
#ifdef DOUBLE_PRECISION_COMPLEX
      if (normmax .gt. 5e-11_rk8 ) then
        status = 1
      endif
#else
      if (normmax .gt. 5e-3_rk4 ) then
        status = 1
      endif
#endif
967 968 969
    end function


970 971


972
! vim: syntax=fortran