test_check_correctness_template.F90 14.6 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
!    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

    function check_correctness_&
    &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
50
      implicit none
      integer(kind=ik)                 :: status
Pavel Kus's avatar
Pavel Kus committed
51
      integer(kind=ik), intent(in)     :: na, nev, nblk, myid, np_rows, np_cols, my_prow, my_pcol
52
53
54
55
#if REALCASE == 1
      real(kind=C_DATATYPE_KIND), intent(in)     :: as(:,:), z(:,:)
      real(kind=C_DATATYPE_KIND)                 :: ev(:)
      real(kind=C_DATATYPE_KIND), dimension(size(as,dim=1),size(as,dim=2)) :: tmp1, tmp2
Pavel Kus's avatar
Pavel Kus committed
56
      real(kind=C_DATATYPE_KIND)                :: xc
57

58
59
60
61
62
63
#ifdef DOUBLE_PRECISION_REAL
      real(kind=C_DATATYPE_KIND), parameter      :: ZERO=0.0_rk8, ONE = 1.0_rk8
#else
      real(kind=C_DATATYPE_KIND), parameter      :: ZERO=0.0_rk4, ONE = 1.0_rk4
#endif

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef WITH_MPI

#ifdef DOUBLE_PRECISION_REAL
      real(kind=C_DATATYPE_KIND)                 :: dnrm2
#else
      real(kind=C_DATATYPE_KIND)                 :: snrm2
#endif

#endif
#endif /* REALCASE */

#if COMPLEXCASE == 1
      complex(kind=C_DATATYPE_KIND), intent(in)    :: as(:,:), z(:,:)
      real(kind=C_DATATYPE_KIND)                   :: ev(:)
      complex(kind=C_DATATYPE_KIND), dimension(size(as,dim=1),size(as,dim=2)) :: tmp1, tmp2
      complex(kind=C_DATATYPE_KIND)                :: xc
#ifdef DOUBLE_PRECISION_COMPLEX
81
      complex(kind=C_DATATYPE_KIND), parameter     :: ZERO = (0.0_rk8,0.0_rk8), ONE = (1.0_rk8,0.0_rk8)
82
83
84
85
86
#ifndef WITH_MPI
      complex(kind=C_DATATYPE_KIND)                :: zdotc, cdotc
#endif

#else /* DOUBLE_PRECISION_COMPLEX */
87
      complex(kind=C_DATATYPE_KIND), parameter     :: ZERO = (0.0_rk4,0.0_rk4), ONE = (1.0_rk4,0.0_rk4)
88
89
90
91
92
93
94
95
96
97
#ifndef WITH_MPI
      complex(kind=C_DATATYPE_KIND)                :: zdotc, cdotc
#endif

#endif /* DOUBLE_PRECISION_COMPLEX */

#endif /* COMPLEXCASE */

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

Pavel Kus's avatar
Pavel Kus committed
98
      integer(kind=ik)                 :: i, rowLocal, colLocal
99
100
101
102
103
104
105
106
107
108
109
      real(kind=C_DATATYPE_KIND)                   :: err, errmax

      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
110
111
      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)
112
#else /* WITH_MPI */
113
      call PRECISION_GEMM('N','N',na,nev,na,ONE,as,na,z,na,ZERO,tmp1,na)
114
115
116
117
118
119
#endif /* WITH_MPI */


      ! tmp2 = Zi*EVi
      tmp2(:,:) = z(:,:)
      do i=1,nev
Pavel Kus's avatar
Pavel Kus committed
120
        xc = ev(i)
121
122
123
124
#if REALCASE == 1
#ifdef WITH_MPI

#ifdef DOUBLE_PRECISION_REAL
Pavel Kus's avatar
Pavel Kus committed
125
        call pdscal(na, xc, tmp2, 1, i, sc_desc, 1)
126
#else
Pavel Kus's avatar
Pavel Kus committed
127
        call psscal(na, xc, tmp2, 1, i, sc_desc, 1)
128
129
130
131
132
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_REAL
Pavel Kus's avatar
Pavel Kus committed
133
        call dscal(na,xc,tmp2(:,i),1)
134
#else
Pavel Kus's avatar
Pavel Kus committed
135
        call sscal(na,xc,tmp2(:,i),1)
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#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
166
      errmax = CONST_REAL_0_0
167
168
169

      do i=1,nev
#if REALCASE == 1
170
        err = CONST_0_0
171
#ifdef WITH_MPI
Pavel Kus's avatar
Pavel Kus committed
172
        call scal_PRECISION_NRM2(na, err, tmp1, 1, i, sc_desc, 1)
173
#else /* WITH_MPI */
Pavel Kus's avatar
Pavel Kus committed
174
        err = PRECISION_NRM2(na,tmp1(1,i),1)
175
176
177
178
179
180
181
#endif /* WITH_MPI */
        errmax = max(errmax, err)
#endif /* REALCASE */

#if COMPLEXCASE == 1
        xc = 0
#ifdef WITH_MPI
Pavel Kus's avatar
Pavel Kus committed
182
        call scal_PRECISION_DOTC(na, xc, tmp1, 1, i, sc_desc, 1, tmp1, 1, i, sc_desc, 1)
183
#else /* WITH_MPI */
Pavel Kus's avatar
Pavel Kus committed
184
        xc = PRECISION_DOTC(na,tmp1,1,tmp1,1)
185
#endif /* WITH_MPI */
186
        errmax = max(errmax, sqrt(real(xc,kind=REAL_DATATYPE)))
187
188
189
190
191
192
#endif /* COMPLEXCASE */
      enddo

      ! Get maximum error norm over all processors
      err = errmax
#ifdef WITH_MPI
193
      call mpi_allreduce(err, errmax, 1, MPI_REAL_PRECISION, MPI_MAX, MPI_COMM_WORLD, mpierr)
194
195
196
197
198
199
#else /* WITH_MPI */
      errmax = err
#endif /* WITH_MPI */

      if (myid==0) print *,'Error Residual     :',errmax
#if REALCASE == 1
200
      if (nev .ge. 2) then
201
#ifdef DOUBLE_PRECISION_REAL
202
        if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
203
#else
204
        if (errmax .gt. 3e-3_rk4 .or. errmax .eq. 0.0_rk4) then
205
#endif
206
207
208
209
210
211
212
213
214
215
216
          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
217
218
#endif
#if COMPLEXCASE == 1
219
      if (nev .gt. 2) then
220
#ifdef DOUBLE_PRECISION_COMPLEX
221
        if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
222
#else
223
        if (errmax .gt. 3e-3_rk4 .or. errmax .eq. 0.0_rk4) then
224
#endif
225
226
227
228
229
230
231
          status =1
        endif
      else
#ifdef DOUBLE_PRECISION_COMPLEX
        if (errmax .gt. 5e-12_rk8) then
#else
        if (errmax .gt. 3e-3_rk4) then
232
#endif
233
234
          status = 1
        endif
235
      endif
236
#endif
237
238
239
240
241
242

      ! 2. Eigenvector orthogonality

      ! tmp1 = Z**T * Z
      tmp1 = 0
#ifdef WITH_MPI
243
244
      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)
245
#else /* WITH_MPI */
246
      call PRECISION_GEMM(BLAS_TRANS_OR_CONJ,'N',nev,nev,na,ONE,z,na,z,na,ZERO,tmp1,na)
247
#endif /* WITH_MPI */
Pavel Kus's avatar
Pavel Kus committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
      !TODO for the C interface, not all information is passed (zeros instead)
      !TODO than this part of the test cannot be done
      !TODO either we will not have this part of test at all, or it will be improved 
      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
266

Pavel Kus's avatar
Pavel Kus committed
267
      ! Second, find the maximal error in the whole Z**T * Z matrix (its diference from identity matrix)
268
269
270
      ! Initialize tmp2 to unit matrix
      tmp2 = 0
#ifdef WITH_MPI
Pavel Kus's avatar
Pavel Kus committed
271
      call scal_PRECISION_LASET('A', nev, nev, ZERO, ONE, tmp2, 1, 1, sc_desc)
272
#else /* WITH_MPI */
Pavel Kus's avatar
Pavel Kus committed
273
      call PRECISION_LASET('A',nev,nev,ZERO,ONE,tmp2,na)
274
275
276
277
278
279
280
281
#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
282
      call mpi_allreduce(err, errmax, 1, MPI_REAL_PRECISION, MPI_MAX, MPI_COMM_WORLD, mpierr)
283
284
285
286
287
288
#else /* WITH_MPI */
      errmax = err
#endif /* WITH_MPI */

      if (myid==0) print *,'Error Orthogonality:',errmax
#if REALCASE == 1
289
      if (nev .ge. 2) then
290
#ifdef DOUBLE_PRECISION_REAL
291
        if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
292
#else
293
        if (errmax .gt. 9e-4_rk4 .or. errmax .eq. 0.0_rk4) then
294
#endif
295
296
297
298
299
300
301
302
303
304
305
          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
306
307
#endif
#if COMPLEXCASE == 1
308
309
310
311
312
313
314
315
316
      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
317
#ifdef DOUBLE_PRECISION_COMPLEX
318
        if (errmax .gt. 5e-12_rk8) then
319
#else
320
        if (errmax .gt. 9e-4_rk4) then
321
#endif
322
323
324
          status = 1
        endif
      endif
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#endif

    end function


#if REALCASE == 1

#ifdef DOUBLE_PRECISION_REAL
    !c> int check_correctness_real_double_f(int na, int nev, int na_rows, int na_cols,
    !c>                                         double *as, double *z, double *ev,
    !c>                                         int sc_desc[9], int myid);
#else
    !c> int check_correctness_real_single_f(int na, int nev, int na_rows, int na_cols,
    !c>                                         float *as, float *z, float *ev,
    !c>                                         int sc_desc[9], int myid);
#endif

#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
    !c> int check_correctness_complex_double_f(int na, int nev, int na_rows, int na_cols,
    !c>                                         complex double *as, complex double *z, double *ev,
    !c>                                         int sc_desc[9], int myid);
#else
    !c> int check_correctness_complex_single_f(int na, int nev, int na_rows, int na_cols,
Andreas Marek's avatar
Andreas Marek committed
351
    !c>                                         complex float *as, complex float *z, float *ev,
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
    !c>                                         int sc_desc[9], int myid);
#endif
#endif /* COMPLEXCASE */

function check_correctness_&
&MATH_DATATYPE&
&_&
&PRECISION&
&_f (na, nev, na_rows, na_cols, as, z, ev, sc_desc, myid) result(status) &
      bind(C,name="check_correctness_&
      &MATH_DATATYPE&
      &_&
      &PRECISION&
      &_f")

      use iso_c_binding

      implicit none

      integer(kind=c_int)            :: status
      integer(kind=c_int), value     :: na, nev, myid, na_rows, na_cols
#if REALCASE == 1
      real(kind=C_DATATYPE_KIND)     :: as(1:na_rows,1:na_cols), z(1:na_rows,1:na_cols)
#endif

#if COMPLEXCASE == 1
      complex(kind=C_DATATYPE_KIND) :: as(1:na_rows,1:na_cols), z(1:na_rows,1:na_cols)
#endif
      real(kind=C_DATATYPE_KIND)    :: ev(1:na)
      integer(kind=c_int)            :: sc_desc(1:9)

Pavel Kus's avatar
Pavel Kus committed
383
384
385
      ! 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..)
386
387
388
389
      status = check_correctness_&
      &MATH_DATATYPE&
      &_&
      &PRECISION&
Pavel Kus's avatar
Pavel Kus committed
390
      & (na, nev, as, z, ev, sc_desc, 0, myid, 0, 0, 0, 0)
391
392
393

    end function

Andreas Marek's avatar
Andreas Marek committed
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
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
    function check_correctness_eigenvalues_toeplitz_&
    &MATH_DATATYPE&
    &_&
    &PRECISION&
    & (na, diagonalElement, subdiagonalElement, ev, z, myid) result(status)
      use iso_c_binding
      implicit none

      integer               :: status, ii, j, myid
      integer, intent(in)   :: na
      real(kind=C_DATATYPE_KIND) :: diagonalElement, subdiagonalElement
      real(kind=C_DATATYPE_KIND) :: ev_analytic(na), ev(na)
#if REALCASE == 1
      real(kind=C_DATATYPE_KIND) :: z(:,:)
#endif
#if COMPLEXCASE == 1
      complex(kind=C_DATATYPE_KIND) :: z(:,:)
#endif

#if defined(DOUBLE_PRECISION_REAL) || defined(DOUBLE_PRECISION_COMPLEX)
      real(kind=C_DATATYPE_KIND), parameter   :: pi = 3.141592653589793238462643383279_c_double
#else
      real(kind=C_DATATYPE_KIND), parameter   :: pi = 3.1415926535897932_c_float
#endif
      real(kind=C_DATATYPE_KIND)              :: tmp, maxerr
      integer                                 :: loctmp
      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)
     if (maxerr .gt. 8.e-13_c_double) then
#else
     if (maxerr .gt. 8.e-4_c_float) then
#endif
       status = 1
       if (myid .eq. 0) then
         print *,"Eigenvalues differ from analytic solution: maxerr = ",maxerr
       endif
     endif
    end function
472
473

! vim: syntax=fortran