Skip to content

Commit

Permalink
Fix DofMap::collapse() on rank with no dofs (#3369)
Browse files Browse the repository at this point in the history
* Fix collapse on empty rank

* Add test that segfaults on main
  • Loading branch information
jorgensd authored Aug 27, 2024
1 parent e93606b commit fbab1dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 2 additions & 3 deletions cpp/dolfinx/fem/DofMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ fem::DofMap build_collapsed_dofmap(const DofMap& dofmap_view,
}

// Create a map from old dofs to new dofs
std::vector<std::int32_t> old_to_new(dofs_view.back() + bs_view, -1);
std::size_t array_size = dofs_view.empty() ? 0 : dofs_view.back() + bs_view;
std::vector<std::int32_t> old_to_new(array_size, -1);
for (std::size_t new_idx = 0; new_idx < sub_imap_to_imap.size(); ++new_idx)
{
for (int k = 0; k < bs_view; ++k)
Expand Down Expand Up @@ -215,7 +216,6 @@ std::pair<DofMap, std::vector<std::int32_t>> DofMap::collapse(
reorder_fn = [](const graph::AdjacencyList<std::int32_t>& g)
{ return graph::reorder_gps(g); };
}

// Create new dofmap
auto create_subdofmap = [](MPI_Comm comm, auto index_map_bs, auto& layout,
auto& topology, auto& reorder_fn, auto& dmap)
Expand All @@ -227,7 +227,6 @@ std::pair<DofMap, std::vector<std::int32_t>> DofMap::collapse(

// Create new element dof layout and reset parent
ElementDofLayout collapsed_dof_layout = layout.copy();

auto [_index_map, bs, dofmaps] = build_dofmap_data(
comm, topology, {collapsed_dof_layout}, reorder_fn);
auto index_map
Expand Down
23 changes: 23 additions & 0 deletions python/test/unit/fem/test_dofmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,26 @@ def test_transpose_dofmap():
dofmap = np.array([[0, 2, 1], [3, 2, 1], [4, 3, 1]], dtype=np.int32)
transpose = dolfinx.fem.transpose_dofmap(dofmap, 3)
assert np.array_equal(transpose.array, [0, 2, 5, 8, 1, 4, 3, 7, 6])


def test_empty_rank_collapse():
"""Test that dofmap with no dofs on a rank can be collapsed"""
if MPI.COMM_WORLD.rank == 0:
nodes = np.array([[0.0], [1.0], [2.0]], dtype=np.float64)
cells = np.array([[0, 1], [1, 2]], dtype=np.int64)
else:
nodes = np.empty((0, 1), dtype=np.float64)
cells = np.empty((0, 2), dtype=np.int64)
c_el = element("Lagrange", "interval", 1, shape=(1,))

def self_partitioner(comm: MPI.Intracomm, n, m, topo):
dests = np.full(len(topo[0]) // 2, comm.rank, dtype=np.int32)
offsets = np.arange(len(topo[0]) // 2 + 1, dtype=np.int32)
return dolfinx.graph.adjacencylist(dests, offsets)

mesh = create_mesh(MPI.COMM_WORLD, cells, nodes, c_el, partitioner=self_partitioner)

el = element("Lagrange", "interval", 1, shape=(2,))
V = functionspace(mesh, el)
V_0, _ = V.sub(0).collapse()
assert V.dofmap.index_map.size_local == V_0.dofmap.index_map.size_local

0 comments on commit fbab1dd

Please sign in to comment.