Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend to_dense for parallel use cases #3354

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cpp/dolfinx/la/MatrixCSR.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,15 +630,17 @@ std::vector<typename MatrixCSR<U, V, W, X>::value_type>
MatrixCSR<U, V, W, X>::to_dense() const
{
const std::size_t nrows = num_all_rows();
const std::size_t ncols
= _index_maps[1]->size_local() + _index_maps[1]->num_ghosts();
const std::size_t ncols = _index_maps[1]->size_global();
std::vector<value_type> A(nrows * ncols * _bs[0] * _bs[1], 0.0);
for (std::size_t r = 0; r < nrows; ++r)
for (std::int32_t j = _row_ptr[r]; j < _row_ptr[r + 1]; ++j)
for (int i0 = 0; i0 < _bs[0]; ++i0)
for (int i1 = 0; i1 < _bs[1]; ++i1)
{
A[(r * _bs[1] + i0) * ncols * _bs[0] + _cols[j] * _bs[1] + i1]
std::vector<std::int32_t> local_col {_cols[j]};
std::vector<std::int64_t> global_col{0};
_index_maps[1]->local_to_global(local_col, global_col);
schnellerhase marked this conversation as resolved.
Show resolved Hide resolved
A[(r * _bs[1] + i0) * ncols * _bs[0] + global_col[0] * _bs[1] + i1]
= _data[j * _bs[0] * _bs[1] + i0 * _bs[1] + i1];
}

Expand Down
37 changes: 26 additions & 11 deletions cpp/test/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <dolfinx/la/MatrixCSR.h>
#include <dolfinx/la/SparsityPattern.h>
#include <dolfinx/la/Vector.h>
#include <mpi.h>
#include <span>

using namespace dolfinx;

Expand Down Expand Up @@ -199,8 +201,8 @@ la::MatrixCSR<double> create_operator(MPI_Comm comm)

void test_matrix()
{
auto map0 = std::make_shared<common::IndexMap>(MPI_COMM_SELF, 8);
la::SparsityPattern p(MPI_COMM_SELF, {map0, map0}, {1, 1});
auto map0 = std::make_shared<common::IndexMap>(MPI_COMM_WORLD, 8);
la::SparsityPattern p(MPI_COMM_WORLD, {map0, map0}, {1, 1});
p.insert(std::vector{0}, std::vector{0});
p.insert(std::vector{4}, std::vector{5});
p.insert(std::vector{5}, std::vector{4});
Expand All @@ -215,23 +217,36 @@ void test_matrix()

const std::vector Adense0 = A.to_dense();

// Note: we cut off the ghost rows by intent here! But therefore we are not
// able to work with the dimensions of Adense0 to compute indices, these
// contain the ghost rows, which also vary between processes.
MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
const T, MDSPAN_IMPL_STANDARD_NAMESPACE::extents<std::size_t, 8, 8>>
Adense(Adense0.data(), 8, 8);
const T,
MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
std::size_t, 8, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>
Adense(Adense0.data(), 8, A.index_map(1)->size_global());

std::vector<T> Aref_data(8 * 8, 0);
std::vector<T> Aref_data(8 * A.index_map(1)->size_global(), 0);
MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
T, MDSPAN_IMPL_STANDARD_NAMESPACE::extents<std::size_t, 8, 8>>
Aref(Aref_data.data(), 8, 8);
Aref(0, 0) = 1;
Aref(4, 5) = 2.3;
T, MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
std::size_t, 8, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>
Aref(Aref_data.data(), 8, A.index_map(1)->size_global());

auto to_global_col = [&](auto col)
{
std::array<std::int64_t, 1> tmp;
A.index_map(1)->local_to_global(std::vector<std::int32_t>{col}, tmp);
return tmp[0];
};
Aref(0, to_global_col(0)) = 1;
Aref(4, to_global_col(5)) = 2.3;

for (std::size_t i = 0; i < Adense.extent(0); ++i)
for (std::size_t j = 0; j < Adense.extent(1); ++j)
CHECK(Adense(i, j) == Aref(i, j));

Aref(4, 4) = 2.3;
CHECK(Adense(4, 4) != Aref(4, 4));
Aref(4, to_global_col(4)) = 2.3;
CHECK(Adense(4, to_global_col(4)) != Aref(4, to_global_col(4)));
}

} // namespace
Expand Down
8 changes: 4 additions & 4 deletions python/dolfinx/wrappers/la.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ void declare_objects(nb::module_& m, const std::string& type)
{
const std::array<int, 2> bs = self.block_size();
std::size_t nrows = self.num_all_rows() * bs[0];
auto map_col = self.index_map(1);
std::size_t ncols
= (map_col->size_local() + map_col->num_ghosts()) * bs[1];
return dolfinx_wrappers::as_nbarray(self.to_dense(),
std::size_t ncols = self.index_map(1)->size_global() * bs[1];
auto dense = self.to_dense();
assert(nrows*ncols == dense.size());
return dolfinx_wrappers::as_nbarray(std::move(self.to_dense()),
{nrows, ncols});
})
.def_prop_ro(
Expand Down
7 changes: 2 additions & 5 deletions python/test/unit/la/test_matrix_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ def test_create_matrix_csr():
A = la.matrix_csr(pattern, dtype=np.complex128)
assert A.data.dtype == np.complex128

cmap = pattern.column_index_map()
num_cols = cmap.size_local + cmap.num_ghosts
num_rows = bs * (map.size_local + map.num_ghosts)
zero = np.zeros((num_rows, bs * num_cols), dtype=np.complex128)
assert np.allclose(A.to_dense(), zero)
dense = A.to_dense()
assert np.allclose(dense, np.zeros(dense.shape, dtype=np.complex128))


@pytest.mark.parametrize(
Expand Down
Loading