test_analytic.F90 11.9 KB
Newer Older
Andreas Marek's avatar
Andreas Marek committed
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
! (c) Copyright Pavel Kus, 2017, MPCDF
!
!    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.

44
#include "../Fortran/assert.h"
Andreas Marek's avatar
Andreas Marek committed
45
46
#include "config-f90.h"

Pavel Kus's avatar
Pavel Kus committed
47
48
49
50
51
52
53
54
55
56
57
58
module test_analytic

  use test_util

  interface prepare_matrix_analytic
    module procedure prepare_matrix_analytic_real_double
  end interface

  interface check_correctness_analytic
    module procedure check_correctness_analytic_real_double
  end interface

59
60
61
62
  integer(kind=ik), parameter, private  :: num_primes = 3
  integer(kind=ik), parameter, private  :: primes(num_primes) = (/2,3,5/)
  real(kind=rk8), parameter, private :: ZERO = 0.0_rk8, ONE = 1.0_rk8

Pavel Kus's avatar
Pavel Kus committed
63
  contains
Andreas Marek's avatar
Andreas Marek committed
64

65
66
#include "../../src/general/prow_pcol.F90"
#include "../../src/general/map_global_to_local.F90"
67

Pavel Kus's avatar
Pavel Kus committed
68
69
70
71
72
73
  subroutine prepare_matrix_analytic_real_double (na, a, nblk, myid, np_rows, &
                            np_cols, my_prow, my_pcol)
    implicit none

    integer(kind=ik), intent(in)    :: na, nblk, myid, np_rows, np_cols, my_prow, my_pcol
    real(kind=rk8), intent(inout)   :: a(:,:)
Andreas Marek's avatar
Andreas Marek committed
74

75
76
77
78
    integer(kind=ik) :: globI, globJ, locI, locJ, levels(num_primes)

    ! for debug only, do it systematicaly somehow ... unit tests
    ! call check_module_sanity(myid)
Pavel Kus's avatar
Pavel Kus committed
79

Andreas Marek's avatar
Andreas Marek committed
80
    if(.not. decompose(na, levels)) then
81
82
83
84
      if(myid == 0) then
        print *, "Analytic test can be run only with matrix sizes of the form 2^n * 3^m * 5^o"
        stop 1
      end if
Andreas Marek's avatar
Andreas Marek committed
85
86
    end if

Pavel Kus's avatar
Pavel Kus committed
87
88
89
90
    do globI = 1, na
      do globJ = 1, na
        if(map_global_array_index_to_local_index(globI, globJ, locI, locJ, &
                 nblk, np_rows, np_cols, my_prow, my_pcol)) then
91
           a(locI, locJ) = analytic_matrix(na, globI, globJ)
Pavel Kus's avatar
Pavel Kus committed
92
93
94
95
        end if
      end do
    end do

Andreas Marek's avatar
Andreas Marek committed
96
  end subroutine
Pavel Kus's avatar
Pavel Kus committed
97
98
99
100
101
102

  function check_correctness_analytic_real_double (na, nev, ev, z, nblk, myid, np_rows, &
                            np_cols, my_prow, my_pcol) result(status)
    implicit none

    integer(kind=ik), intent(in)    :: na, nev, nblk, myid, np_rows, np_cols, my_prow, my_pcol
Andreas Marek's avatar
Andreas Marek committed
103
    integer(kind=ik)                :: status, mpierr
Pavel Kus's avatar
Pavel Kus committed
104
105
    real(kind=rk8), intent(inout)   :: z(:,:)
    real(kind=rk8), intent(inout)   :: ev(:)
Andreas Marek's avatar
Andreas Marek committed
106

107
    integer(kind=ik) :: globI, globJ, locI, locJ, levels(num_primes)
Pavel Kus's avatar
Pavel Kus committed
108
109
110
    real(kind=rk8)   :: diff, max_z_diff, max_ev_diff, glob_max_z_diff, max_curr_z_diff_minus, max_curr_z_diff_plus
    real(kind=rk8)   :: computed, expected

Andreas Marek's avatar
Andreas Marek committed
111
    if(.not. decompose(na, levels)) then
Pavel Kus's avatar
Pavel Kus committed
112
113
      print *, "can not decomopse matrix size"
      stop 1
Andreas Marek's avatar
Andreas Marek committed
114
115
    end if

116
117
    max_z_diff = ZERO
    max_ev_diff = ZERO
118
    do globJ = 1, na
119
      diff = abs(ev(globJ) - analytic_eigenvalues(na, globJ))
Pavel Kus's avatar
Pavel Kus committed
120
      max_ev_diff = max(diff, max_ev_diff)
121
    end do
Pavel Kus's avatar
Pavel Kus committed
122

123
    do globJ = 1, nev
Pavel Kus's avatar
Pavel Kus committed
124
      ! calculated eigenvector can be in opposite direction
125
126
      max_curr_z_diff_minus = ZERO
      max_curr_z_diff_plus  = ZERO
Pavel Kus's avatar
Pavel Kus committed
127
128
129
130
      do globI = 1, na
        if(map_global_array_index_to_local_index(globI, globJ, locI, locJ, &
                 nblk, np_rows, np_cols, my_prow, my_pcol)) then
           computed = z(locI, locJ)
131
           expected = analytic_eigenvectors(na, globI, globJ)
Pavel Kus's avatar
Pavel Kus committed
132
133
134
135
136
137
138
139
140
141
142
143
144
           max_curr_z_diff_minus = max(abs(computed - expected), max_curr_z_diff_minus)
           max_curr_z_diff_plus = max(abs(computed + expected), max_curr_z_diff_plus)
        end if
      end do
      ! we have max difference of one of the eigenvectors, update global
      max_z_diff = max(max_z_diff, min(max_curr_z_diff_minus, max_curr_z_diff_plus))
    end do

#ifdef WITH_MPI
    call mpi_allreduce(max_z_diff, glob_max_z_diff, 1, MPI_REAL8, MPI_MAX, MPI_COMM_WORLD, mpierr)
#else
    glob_max_z_diff = max_z_diff
#endif
Andreas Marek's avatar
Andreas Marek committed
145
146
    if(myid == 0) print *, 'Maximum error in eigenvalues      :', max_ev_diff
    if(myid == 0) print *, 'Maximum error in eigenvectors     :', glob_max_z_diff
Pavel Kus's avatar
Pavel Kus committed
147
    status = 0
148
149
150
151
152
153
154
    if (nev .gt. 2) then
      if (max_ev_diff .gt. 5e-14_rk8 .or. max_ev_diff .eq. ZERO) status = 1
      if (glob_max_z_diff .gt. 6e-11_rk8 .or. glob_max_z_diff .eq. ZERO) status = 1
    else
      if (max_ev_diff .gt. 5e-14_rk8) status = 1
      if (glob_max_z_diff .gt. 6e-11_rk8) status = 1
    endif
Andreas Marek's avatar
Andreas Marek committed
155
  end function
Pavel Kus's avatar
Pavel Kus committed
156
157
158
159

  function decompose(num, decomposition) result(possible)
    implicit none
    integer(kind=ik), intent(in)   :: num
160
    integer(kind=ik), intent(out)  :: decomposition(num_primes)
Pavel Kus's avatar
Pavel Kus committed
161
    logical                        :: possible
162
    integer(kind=ik)               :: reminder, prime, prime_id
Andreas Marek's avatar
Andreas Marek committed
163

Pavel Kus's avatar
Pavel Kus committed
164
    decomposition = 0
Andreas Marek's avatar
Andreas Marek committed
165
    possible = .true.
Pavel Kus's avatar
Pavel Kus committed
166
    reminder = num
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    do prime_id = 1, num_primes
      prime = primes(prime_id)
      do while (MOD(reminder, prime) == 0)
        decomposition(prime_id) = decomposition(prime_id) + 1
        reminder = reminder / prime
      end do
    end do
    if(reminder > 1) then
      possible = .false.
    end if
  end function

  function compose(decomposition) result(num)
    implicit none
    integer(kind=ik), intent(in)   :: decomposition(num_primes)
    integer(kind=ik)               :: num, prime_id

    num = 1;
    do prime_id = 1, num_primes
      num = num * primes(prime_id) ** decomposition(prime_id)
Andreas Marek's avatar
Andreas Marek committed
187
    end do
Pavel Kus's avatar
Pavel Kus committed
188
  end function
Andreas Marek's avatar
Andreas Marek committed
189

Pavel Kus's avatar
Pavel Kus committed
190
191
192
#define ANALYTIC_MATRIX 0
#define ANALYTIC_EIGENVECTORS 1
#define ANALYTIC_EIGENVALUES 2
Andreas Marek's avatar
Andreas Marek committed
193

194
  function analytic_matrix(na, i, j) result(element)
Pavel Kus's avatar
Pavel Kus committed
195
    implicit none
196
    integer(kind=ik), intent(in) :: na, i, j
Pavel Kus's avatar
Pavel Kus committed
197
198
    real(kind=rk8)               :: element

199
    element = analytic(na, i, j, ANALYTIC_MATRIX)
Pavel Kus's avatar
Pavel Kus committed
200
201
202

  end function

203
  function analytic_eigenvectors(na, i, j) result(element)
Pavel Kus's avatar
Pavel Kus committed
204
    implicit none
205
    integer(kind=ik), intent(in) :: na, i, j
Pavel Kus's avatar
Pavel Kus committed
206
207
    real(kind=rk8)               :: element

208
    element = analytic(na, i, j, ANALYTIC_EIGENVECTORS)
Pavel Kus's avatar
Pavel Kus committed
209
210
211

  end function

212
  function analytic_eigenvalues(na, i) result(element)
Pavel Kus's avatar
Pavel Kus committed
213
    implicit none
214
    integer(kind=ik), intent(in) :: na, i
Pavel Kus's avatar
Pavel Kus committed
215
216
    real(kind=rk8)               :: element

217
    element = analytic(na, i, i, ANALYTIC_EIGENVALUES)
Pavel Kus's avatar
Pavel Kus committed
218
219
220

  end function

Andreas Marek's avatar
Andreas Marek committed
221

Pavel Kus's avatar
Pavel Kus committed
222

223
  function analytic(na, i, j, what) result(element)
Pavel Kus's avatar
Pavel Kus committed
224
    implicit none
225
    integer(kind=ik), intent(in)   :: na, i, j, what
Pavel Kus's avatar
Pavel Kus committed
226
    real(kind=rk8)                 :: element, am
227
228
229
    real(kind=rk8)                 :: a, s, c, mat2x2(2,2), mat(5,5)
    integer(kind=ik)               :: levels(num_primes)
    integer(kind=ik)               :: ii, jj, m, prime_id, prime, total_level, level
Pavel Kus's avatar
Pavel Kus committed
230

231
232
233
234
235
236
237
    real(kind=rk8), parameter      :: largest_ev = 2.0_rk8

    assert(i <= na)
    assert(j <= na)
    assert(i >= 0)
    assert(j >= 0)
    assert(decompose(na, levels))
Pavel Kus's avatar
Pavel Kus committed
238
239
240
    ! go to zero-based indexing
    ii = i - 1
    jj = j - 1
Andreas Marek's avatar
Andreas Marek committed
241
242
243
244
245
    if (na .gt. 2) then
      a = exp(log(largest_ev)/(na-1))
    else
      a = exp(log(largest_ev)/(1))
    endif
Pavel Kus's avatar
Pavel Kus committed
246
247
    s = 0.5_rk8
    c = sqrt(3.0_rk8)/2.0_rk8
248
249
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
    element = ONE
    total_level = 0
    am = a
    do prime_id = 1,num_primes
      prime = primes(prime_id)
      do  level = 1, levels(prime_id)
        total_level = total_level + 1
        if(what == ANALYTIC_MATRIX) then
          mat2x2 = reshape((/ c*c + am**(prime-1) * s*s, (ONE-am**(prime-1)) * s*c,  &
                           (ONE-am**(prime-1)) * s*c, s*s + am**(prime-1) * c*c  /), &
                                      (/2, 2/))
        else if(what == ANALYTIC_EIGENVECTORS) then
          mat2x2 = reshape((/ c, s,  &
                           -s,  c  /), &
                                (/2, 2/))
        else if(what == ANALYTIC_EIGENVALUES) then
          mat2x2 = reshape((/ ONE, ZERO,  &
                           ZERO, am**(prime-1)  /), &
                                 (/2, 2/))
        else
          assert(.false.)
        end if

        mat = ZERO
        if(prime == 2) then
          mat(1:2, 1:2) = mat2x2
        else if(prime == 3) then
          mat((/1,3/),(/1,3/)) = mat2x2
          if(what == ANALYTIC_EIGENVECTORS) then
            mat(2,2) = ONE
          else
            mat(2,2) = am
          end if
        else if(prime == 5) then
          mat((/1,5/),(/1,5/)) = mat2x2
          if(what == ANALYTIC_EIGENVECTORS) then
            mat(2,2) = ONE
            mat(3,3) = ONE
            mat(4,4) = ONE
          else
            mat(2,2) = am
            mat(3,3) = am**2
            mat(4,4) = am**3
          end if
        else
          assert(.false.)
        end if

  !      write(*,*) "calc value, elem: ", element, ", mat: ", mod(ii,2), mod(jj,2),  mat(mod(ii,2), mod(jj,2)), "am ", am
  !      write(*,*) " matrix mat", mat
        element = element * mat(mod(ii,prime) + 1, mod(jj,prime) + 1)
        ii = ii / prime
        jj = jj / prime

        am = am**prime
      end do
Pavel Kus's avatar
Pavel Kus committed
304
305
306
    end do
    !write(*,*) "returning value ", element
  end function
Andreas Marek's avatar
Andreas Marek committed
307

308
  subroutine print_matrix(myid, na, mat, mat_name)
Pavel Kus's avatar
Pavel Kus committed
309
    implicit none
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    integer(kind=ik), intent(in)    :: myid, na
    character(len=*), intent(in)    :: mat_name
    real(kind=rk8)                  :: mat(na, na)
    integer(kind=ik)                :: i
    character(len=20)               :: str

    if(myid .ne. 0) &
      return
    write(*,*) "Matrix: "//trim(mat_name)
    write(str, *) na
    do i = 1, na
      write(*, '('//trim(str)//'f8.3)') mat(i, :)
    end do
    write(*,*)
  end subroutine
Pavel Kus's avatar
Pavel Kus committed
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  subroutine check_matrices(myid, na)
    implicit none
    integer(kind=ik), intent(in)    :: myid, na
    real(kind=rk8)                  :: A(na, na), S(na, na), L(na, na)
    integer(kind=ik)                :: i, j, decomposition(num_primes)

    assert(decompose(na, decomposition))

    do i = 1, na
      do j = 1, na
        A(i,j) = analytic_matrix(na, i, j)
        S(i,j) = analytic_eigenvectors(na, i, j)
        L(i,j) = analytic(na, i, j, ANALYTIC_EIGENVALUES)
      end do
    end do

    call print_matrix(myid, na, A, "A")
    call print_matrix(myid, na, S, "S")
    call print_matrix(myid, na, L, "L")
Pavel Kus's avatar
Pavel Kus committed
345

346
347
348
349
350
351
  end subroutine

  subroutine check_module_sanity(myid)
    implicit none
    integer(kind=ik), intent(in)   :: myid
    integer(kind=ik)               :: decomposition(num_primes)
Pavel Kus's avatar
Pavel Kus committed
352

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    if(myid == 0) print *, "Checking test_analytic module sanity.... "
    assert(decompose(1500, decomposition))
    assert(all(decomposition == (/2,1,3/)))
    assert(decompose(6,decomposition))
    assert(all(decomposition == (/1,1,0/)))

    call check_matrices(myid, 2)
    call check_matrices(myid, 3)
    call check_matrices(myid, 5)
    call check_matrices(myid, 6)
    call check_matrices(myid, 10)

    if(myid == 0) print *, "Checking test_analytic module sanity.... DONE"

  end subroutine
Pavel Kus's avatar
Pavel Kus committed
368
369

end module