コード例 #1
0
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))
コード例 #2
0
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))
コード例 #3
0
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))
コード例 #4
0
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, :]))
コード例 #5
0
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)
コード例 #6
0
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))
コード例 #7
0
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))
コード例 #8
0
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))
コード例 #9
0
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))
コード例 #10
0
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]))
コード例 #11
0
ファイル: example.py プロジェクト: qsimulate/elpa-fork
#!/usr/bin/env python
import numpy as np
from pyelpa import DistributedMatrix
import sys

# set some parameters for matrix layout
na = 1000
nev = 200
nblk = 16

# create distributed matrix
a = DistributedMatrix.from_comm_world(na, nev, nblk)

# set matrix a by looping over indices
# this is the easiest but also slowest way
for global_row, global_col in a.global_indices():
    a.set_data_for_global_index(global_row, global_col,
                                global_row * global_col)

print("Call ELPA eigenvectors")
sys.stdout.flush()

# now compute nev of na eigenvectors and eigenvalues
data = a.compute_eigenvectors()
eigenvalues = data['eigenvalues']
eigenvectors = data['eigenvectors']

print("Done")

# now eigenvectors.data contains the local part of the eigenvector matrix
# which is stored in a block-cyclic distributed layout and eigenvalues contains