test_check_correctness_template.X90 11.8 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
60
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
102
103
104
105
106
!    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&
    & (na, nev, as, z, ev, sc_desc, myid) result(status)
      implicit none
      integer(kind=ik)                 :: status
      integer(kind=ik), intent(in)     :: na, nev, myid
#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

#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
      complex(kind=C_DATATYPE_KIND), parameter     :: CZERO = (0.0_rk8,0.0_rk8), CONE = (1.0_rk8,0.0_rk8)
#ifndef WITH_MPI
      complex(kind=C_DATATYPE_KIND)                :: zdotc, cdotc
#endif


#else /* DOUBLE_PRECISION_COMPLEX */
      complex(kind=C_DATATYPE_KIND), parameter     :: CZERO = (0.0_rk4,0.0_rk4), CONE = (1.0_rk4,0.0_rk4)
#ifndef WITH_MPI
      complex(kind=C_DATATYPE_KIND)                :: zdotc, cdotc
#endif

#endif /* DOUBLE_PRECISION_COMPLEX */

#endif /* COMPLEXCASE */

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

      integer(kind=ik)                 :: i
      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

#if REALCASE == 1
#ifdef WITH_MPI
107
108
      call scal_PRECISION_GEMM('N', 'N', na, nev, na, CONST_1_0, as, 1, 1, sc_desc, &
                  z, 1, 1, sc_desc, CONST_0_0, tmp1, 1, 1, sc_desc)
109
#else /* WITH_MPI */
110
     call PRECISION_GEMM('N','N',na,nev,na,CONST_1_0,as,na,z,na,CONST_0_0,tmp1,na)
111
112
113
114
115
#endif /* WITH_MPI */
#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef WITH_MPI
116
      call scal_PRECISION_GEMM('N', 'N', na, nev, na, CONE, as, 1, 1, sc_desc, &
117
118
                  z, 1, 1, sc_desc, CZERO, tmp1, 1, 1, sc_desc)
#else /* WITH_MPI */
119
      call PRECISION_GEMM('N','N',na,nev,na,CONE,as,na,z,na,CZERO,tmp1,na)
120
121
122
123
124
125
126
127
128
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
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
#endif /* WITH_MPI */
#endif /* COMPLEXCASE */


      ! tmp2 = Zi*EVi
      tmp2(:,:) = z(:,:)
      do i=1,nev
#if REALCASE == 1
#ifdef WITH_MPI

#ifdef DOUBLE_PRECISION_REAL
        call pdscal(na, ev(i), tmp2, 1, i, sc_desc, 1)
#else
        call psscal(na, ev(i), tmp2, 1, i, sc_desc, 1)
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_REAL
        call dscal(na,ev(i),tmp2(:,i),1)
#else
        call sscal(na,ev(i),tmp2(:,i),1)
#endif

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

#if COMPLEXCASE == 1
        xc = ev(i)
#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
#if REALCASE == 1
#ifdef DOUBLE_PRECISION_REAL
      errmax = 0.0_rk8
#else
      errmax = 0.0_rk4
#endif
#endif
#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
      errmax = 0.0_rk8
#else
      errmax = 0.0_rk4
#endif
#endif

      do i=1,nev
#if REALCASE == 1
#ifdef DOUBLE_PRECISION_REAL
        err = 0.0_rk8
#else
        err = 0.0_rk4
#endif

#ifdef WITH_MPI

#ifdef DOUBLE_PRECISION_REAL
        call pdnrm2(na, err, tmp1, 1, i, sc_desc, 1)
#else
        call psnrm2(na, err, tmp1, 1, i, sc_desc, 1)
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_REAL
!        call dnrm2(na,err,tmp1,1,i,sc_desc,1)
        err = dnrm2(na,tmp1(1,i),1)
#else
        err = snrm2(na,tmp1(1,i),1)
#endif

#endif /* WITH_MPI */
        errmax = max(errmax, err)
#endif /* REALCASE */

#if COMPLEXCASE == 1
        xc = 0
#ifdef WITH_MPI

#ifdef DOUBLE_PRECISION_COMPLEX
        call pzdotc(na, xc, tmp1, 1, i, sc_desc, 1, tmp1, 1, i, sc_desc, 1)
#else
        call pcdotc(na, xc, tmp1, 1, i, sc_desc, 1, tmp1, 1, i, sc_desc, 1)
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_COMPLEX
        xc = zdotc(na,tmp1,1,tmp1,1)
#else
        xc = cdotc(na,tmp1,1,tmp1,1)
#endif

#endif /* WITH_MPI */


#ifdef DOUBLE_PRECISION_COMPLEX
        errmax = max(errmax, sqrt(real(xc,kind=rk8)))
#else
        errmax = max(errmax, sqrt(real(xc,kind=rk4)))
#endif
#endif /* COMPLEXCASE */
      enddo

      ! Get maximum error norm over all processors
      err = errmax
#ifdef WITH_MPI
249
      call mpi_allreduce(err, errmax, 1, MPI_REAL_PRECISION, MPI_MAX, MPI_COMM_WORLD, mpierr)
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#else /* WITH_MPI */
      errmax = err
#endif /* WITH_MPI */

      if (myid==0) print *,'Error Residual     :',errmax
#if REALCASE == 1
#ifdef DOUBLE_PRECISION_REAL
      if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
#else
      if (errmax .gt. 3e-3_rk4 .or. errmax .eq. 0.0_rk4) then
#endif
#endif
#if COMPLEXCASE == 1
#ifdef DOUBLE_PRECISION_COMPLEX
      if (errmax .gt. 5e-12_rk8 .or. errmax .eq. 0.0_rk8) then
#else
      if (errmax .gt. 3e-3_rk4 .or. errmax .eq. 0.0_rk4) then
#endif
#endif

        status = 1
      endif

      ! 2. Eigenvector orthogonality

      ! tmp1 = Z**T * Z
      tmp1 = 0
#if REALCASE == 1

#ifdef WITH_MPI
280
281
      call scal_PRECISION_GEMM('T', 'N', nev, nev, na, CONST_1_0, z, 1, 1, sc_desc, &
                  z, 1, 1, sc_desc, CONST_0_0, tmp1, 1, 1, sc_desc)
282
#else  /* WITH_MPI */
283
284
      call PRECISION_GEMM('T','N',nev,nev,na,CONST_1_0,z,na, &
                  z,na,CONST_0_0,tmp1,na)
285
286
287
288
289
290
#endif /* WITH_MPI */

#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef WITH_MPI
291
      call scal_PRECISION_GEMM('C', 'N', nev, nev, na, CONE, z, 1, 1, sc_desc, &
292
293
                  z, 1, 1, sc_desc, CZERO, tmp1, 1, 1, sc_desc)
#else /* WITH_MPI */
294
      call PRECISION_GEMM('C','N',nev,nev,na,CONE,z,na,z,na,CZERO,tmp1,na)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#endif /* WITH_MPI */
#endif /* COMPLEXCASE */

      ! Initialize tmp2 to unit matrix
      tmp2 = 0
#if REALCASE == 1

#ifdef WITH_MPI

#ifdef DOUBLE_PRECISION_REAL
      call pdlaset('A', nev, nev, 0.0_rk8, 1.0_rk8, tmp2, 1, 1, sc_desc)
#else
      call pslaset('A', nev, nev, 0.0_rk4, 1.0_rk4, tmp2, 1, 1, sc_desc)
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_REAL
      call dlaset('A',nev,nev,0.0_rk8,1.0_rk8,tmp2,na)
#else
      call slaset('A',nev,nev,0.0_rk4,1.0_rk4,tmp2,na)
#endif

#endif /* WITH_MPI */

#endif /* REALCASE */

#if COMPLEXCASE == 1
#ifdef WITH_MPI

#ifdef DOUBLE_PRECISION_COMPLEX
      call pzlaset('A', nev, nev, CZERO, CONE, tmp2, 1, 1, sc_desc)
#else
      call pclaset('A', nev, nev, CZERO, CONE, tmp2, 1, 1, sc_desc)
#endif

#else /* WITH_MPI */

#ifdef DOUBLE_PRECISION_COMPLEX
      call zlaset('A',nev,nev,CZERO,CONE,tmp2,na)
#else
      call claset('A',nev,nev,CZERO,CONE,tmp2,na)
#endif

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


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

      ! Get maximum error (max abs value in tmp1)
      err = maxval(abs(tmp1))
#ifdef WITH_MPI
349
      call mpi_allreduce(err, errmax, 1, MPI_REAL_PRECISION, MPI_MAX, MPI_COMM_WORLD, mpierr)
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
#else /* WITH_MPI */
      errmax = err
#endif /* WITH_MPI */

      if (myid==0) print *,'Error Orthogonality:',errmax
#if REALCASE == 1
#ifdef DOUBLE_PRECISION_REAL
      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
#endif
#if COMPLEXCASE == 1
#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
#endif

        status = 1
      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
396
    !c>                                         complex float *as, complex float *z, float *ev,
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
    !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)

      status = check_correctness_&
      &MATH_DATATYPE&
      &_&
      &PRECISION&
      & (na, nev, as, z, ev, sc_desc, myid)

    end function

436
437

! vim: syntax=fortran