File: test_with_mpi.py

package info (click to toggle)
elpa 2022.11.001-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,636 kB
  • sloc: f90: 61,114; ansic: 39,478; sh: 4,795; cpp: 4,291; makefile: 923; asm: 834; python: 635; perl: 141
file content (438 lines) | stat: -rw-r--r-- 16,427 bytes parent folder | download | duplicates (4)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
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
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
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
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
import pytest

# combinations of na, nev, nblk to run all the tests with
parameter_list = [
    (200, 20, 16),
    (200, 200, 16),
    (200, 20, 64),
    (200, 200, 64),
    (200, 20, 4),
    (200, 200, 4),
    (50, 20, 16),
    (100, 20, 16),
]

def get_random_vector(size):
    """generate random vector with given size that is equal on all cores"""
    import numpy as np
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    am_i_root = comm.Get_rank() == 0
    vector = np.empty(size)
    if am_i_root:
        vector[:] = np.random.rand(size)
    comm.Bcast(vector)
    return vector


def test_processor_layout():
    from pyelpa import ProcessorLayout
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)
    assert(comm.Get_size() == layout_p.np_cols*layout_p.np_rows)
    assert(layout_p.my_prow >= 0)
    assert(layout_p.my_pcol >= 0)
    assert(layout_p.my_prow <= comm.Get_size())
    assert(layout_p.my_pcol <= comm.Get_size())


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_distributed_matrix_from_processor_layout(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.float32, np.complex64, np.complex128]:
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        assert(a.data.dtype == dtype)
        assert(a.data.shape == (a.na_rows, a.na_cols))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_distributed_matrix_from_communicator(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix
    from mpi4py import MPI
    comm = MPI.COMM_WORLD

    for dtype in [np.float64, np.float32, np.complex64, np.complex128]:
        a = DistributedMatrix.from_communicator(comm, na, nev, nblk,
                                                dtype=dtype)
        assert(a.data.dtype == dtype)
        assert(a.data.shape == (a.na_rows, a.na_cols))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_distributed_matrix_from_world(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.float32, np.complex64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        assert(a.data.dtype == dtype)
        assert(a.data.shape == (a.na_rows, a.na_cols))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_distributed_matrix_like_other_matrix(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.float32, np.complex64, np.complex128]:
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        b = DistributedMatrix.like(a)
        assert(a.na == b.na)
        assert(a.nev == b.nev)
        assert(a.nblk == b.nblk)
        assert(a.data.dtype == b.data.dtype)
        assert(a.data.shape == b.data.shape)


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_call_eigenvectors(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix, Elpa
    from mpi4py import MPI

    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.complex128]:
        # create arrays
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        a.data[:, :] = np.random.rand(a.na_rows, a.na_cols).astype(dtype)
        q = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        ev = np.zeros(na, dtype=np.float64)

        e = Elpa.from_distributed_matrix(a)
        e.eigenvectors(a.data, ev, q.data)


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_call_eigenvalues(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix, Elpa
    from mpi4py import MPI

    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.complex128]:
        # create arrays
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        a.data[:, :] = np.random.rand(a.na_rows, a.na_cols).astype(dtype)
        ev = np.zeros(na, dtype=np.float64)

        e = Elpa.from_distributed_matrix(a)
        e.eigenvalues(a.data, ev)


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_compare_eigenvalues_to_those_from_eigenvectors(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix, Elpa
    from mpi4py import MPI

    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.complex128]:
        # create arrays
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        random_matrix = np.random.rand(a.na_rows, a.na_cols).astype(dtype)
        a.data[:, :] = random_matrix
        q = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        ev = np.zeros(na, dtype=np.float64)
        ev2 = np.zeros(na, dtype=np.float64)

        e = Elpa.from_distributed_matrix(a)
        e.eigenvectors(a.data, ev, q.data)

        a.data[:, :] = random_matrix
        e.eigenvalues(a.data, ev2)

        assert(np.allclose(ev, ev2))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_compare_eigenvalues_to_those_from_eigenvectors_self_functions(
        na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        # create arrays
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        random_matrix = np.random.rand(a.na_rows, a.na_cols).astype(dtype)
        a.data[:, :] = random_matrix
        data = a.compute_eigenvectors()

        a.data[:, :] = random_matrix
        eigenvalues = a.compute_eigenvalues()

        assert(np.allclose(data['eigenvalues'], eigenvalues))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_distributed_matrix_global_index(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        for local_row in range(a.na_rows):
            for local_col in range(a.na_cols):
                global_row, global_col = a.get_global_index(local_row,
                                                            local_col)
                l_row, l_col = a.get_local_index(global_row, global_col)
                assert(global_row >= 0 and global_row < a.na)
                assert(global_col >= 0 and global_col < a.na)
                assert(local_row == l_row and local_col == l_col)


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_distributed_matrix_local_index(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        for global_row in range(a.na):
            for global_col in range(a.na):
                if not a.is_local_index(global_row, global_col):
                    continue
                local_row, local_col = a.get_local_index(global_row,
                                                         global_col)
                g_row, g_col = a.get_global_index(local_row, local_col)
                assert(local_row >= 0 and local_row < a.na_rows)
                assert(local_col >= 0 and local_col < a.na_cols)
                assert(global_row == g_row and global_col == g_col)


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_distributed_matrix_indexing_loop(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        for local_row in range(a.na_rows):
            for local_col in range(a.na_cols):
                global_row, global_col = a.get_global_index(local_row,
                                                            local_col)
                a.data[local_row, local_col] = global_row*10 + global_col

        for global_row in range(a.na):
            for global_col in range(a.na):
                if not a.is_local_index(global_row, global_col):
                    continue
                local_row, local_col = a.get_local_index(global_row,
                                                         global_col)
                assert(a.data[local_row, local_col] ==
                       global_row*10 + global_col)


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_setting_global_matrix(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    layout_p = ProcessorLayout(comm)

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix(layout_p, na, nev, nblk, dtype=dtype)
        # get global matrix that is equal on all cores
        matrix = get_random_vector(na*na).reshape(na, na).astype(dtype)
        a.set_data_from_global_matrix(matrix)

        # check data
        for global_row in range(a.na):
            for global_col in range(a.na):
                if not a.is_local_index(global_row, global_col):
                    continue
                local_row, local_col = a.get_local_index(global_row,
                                                         global_col)
                assert(a.data[local_row, local_col] ==
                       matrix[global_row, global_col])


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_dot_product(na, nev, nblk):
    import numpy as np
    from pyelpa import ProcessorLayout, DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        # get global matrix and vector that is equal on all cores
        matrix = get_random_vector(na*na).reshape(na, na).astype(dtype)
        vector = get_random_vector(na).astype(dtype)

        a.set_data_from_global_matrix(matrix)

        product_distributed = a.dot(vector)
        product_naive = a._dot_naive(vector)
        product_serial = np.dot(matrix, vector)

        assert(np.allclose(product_distributed, product_serial))
        assert(np.allclose(product_distributed, product_naive))

@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_dot_product_incompatible_size(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        # get global matrix and vector that is equal on all cores
        matrix = get_random_vector(na*na).reshape(na, na).astype(dtype)
        vector = get_random_vector(na*2).astype(dtype)

        a.set_data_from_global_matrix(matrix)

        with pytest.raises(ValueError):
            product_distributed = a.dot(vector)


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_validate_eigenvectors(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        # get a symmetric/hermitian matrix
        matrix = get_random_vector(na*na).reshape(na, na).astype(dtype)
        matrix = 0.5*(matrix + np.conj(matrix.T))
        a.set_data_from_global_matrix(matrix)

        data = a.compute_eigenvectors()
        eigenvalues = data['eigenvalues']
        eigenvectors = data['eigenvectors']
        # reset data of a
        a.set_data_from_global_matrix(matrix)
        for index in range(a.nev):
            eigenvector = eigenvectors.get_column(index)
            scaled_eigenvector = eigenvalues[index]*eigenvector
            # test solution
            assert(np.allclose(a.dot(eigenvector),
                               scaled_eigenvector))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_validate_eigenvectors_to_numpy(na, nev, nblk):
    import numpy as np
    from numpy import linalg
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        # get a symmetric/hermitian matrix
        matrix = get_random_vector(na*na).reshape(na, na).astype(dtype)
        matrix = 0.5*(matrix + np.conj(matrix.T))
        a.set_data_from_global_matrix(matrix)

        data = a.compute_eigenvectors()
        eigenvalues = data['eigenvalues']
        eigenvectors = data['eigenvectors']

        # get numpy solution
        eigenvalues_np, eigenvectors_np = linalg.eigh(matrix)

        assert(np.allclose(eigenvalues, eigenvalues_np))
        for index in range(a.nev):
            eigenvector = eigenvectors.get_column(index)
            assert(np.allclose(eigenvector, eigenvectors_np[:, index]) or
                   np.allclose(eigenvector, -eigenvectors_np[:, index]))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_accessing_matrix(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        matrix = get_random_vector(na*na).reshape(na, na).astype(dtype)
        a.set_data_from_global_matrix(matrix)

        for index in range(a.na):
            column = a.get_column(index)
            assert(np.allclose(column, matrix[:, index]))
            row = a.get_row(index)
            assert(np.allclose(row, matrix[index, :]))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_global_index_iterator(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        for i, j in a.global_indices():
            assert(a.is_local_index(i, j))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_global_index_access(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        for i, j in a.global_indices():
            x = dtype(i*j)
            a.set_data_for_global_index(i, j, x)
        for i, j in a.global_indices():
            x = a.get_data_for_global_index(i, j)
            assert(np.isclose(x, i*j))


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_global_block_iterator(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        for i, j, blk_i, blk_j in a.global_block_indices():
            assert(a.is_local_index(i, j))
            assert(blk_i <= nblk)
            assert(blk_j <= nblk)
            assert(i+blk_i <= na)
            assert(j+blk_j <= na)


@pytest.mark.parametrize("na,nev,nblk", parameter_list)
def test_global_block_access(na, nev, nblk):
    import numpy as np
    from pyelpa import DistributedMatrix

    for dtype in [np.float64, np.complex128]:
        a = DistributedMatrix.from_comm_world(na, nev, nblk, dtype=dtype)
        for i, j, blk_i, blk_j in a.global_block_indices():
            x = np.arange(i, i+blk_i)[:, None] * np.arange(j, j+blk_j)[None, :]
            a.set_block_for_global_index(i, j, blk_i, blk_j, x)
        for i, j, blk_i, blk_j in a.global_block_indices():
            original = np.arange(i, i+blk_i)[:, None] * np.arange(j, j+blk_j)[None, :]
            x = a.get_block_for_global_index(i, j, blk_i, blk_j)
            assert(np.allclose(x, original))
        for i, j in a.global_indices():
            x = a.get_data_for_global_index(i, j)
            assert(np.isclose(x, i*j))