Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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: 8 additions & 0 deletions cpp/dolfinx/fem/DirichletBC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ find_local_entity_index(const mesh::Topology& topology,

std::vector<std::pair<std::int32_t, int>> entity_indices;
entity_indices.reserve(entities.size());
std::int32_t num_entities_local = e_to_c->num_nodes();
for (std::int32_t e : entities)
{
// Get first attached cell
if (num_entities_local <= e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (num_entities_local <= e)
if (num_entities_local <= e) [[unlikely]]

Maybe good to add here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it will yield an optimization here, as there is no else-case, and there is no performance critical operation happening within the if test.

At least I get the same machine code for both GCC and clang with:

#include<vector>
#include<cstdint>
#include<stdexcept>

int test (std::int32_t max_size)
{
    std::vector<std::int32_t> a;
    for (auto ai: a)
    {
    if (ai >=max_size) [[likely]]
    // if (ai >=max_size) [[unlikely]]
    {
        throw std::runtime_error("error");
    }
    auto z = ai + 1;
    }
    return 0;

}

{
throw std::runtime_error(
"Input entity " + std::to_string(e)
+ " is bigger than the number of entities on this process ("
+ std::to_string(num_entities_local) + ").");
}
assert(e_to_c->num_links(e) > 0);
const int cell = e_to_c->links(e).front();

Expand Down
13 changes: 13 additions & 0 deletions python/test/unit/fem/test_bcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,16 @@ def test_blocked_dof_ownership(shape):
owned_sub_dofs = boundary_dofs_V[boundary_dofs_V < num_owned_blocked * bs]
assert len(owned_sub_dofs) == num_owned_sub
assert len(unrolled_dofs_sub) == len(boundary_dofs_V)


def test_bc_index_out_of_range():
mesh = create_unit_square(MPI.COMM_WORLD, 4, 4)
mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim)
facet_map = mesh.topology.index_map(mesh.topology.dim - 1)
num_facets_on_proc = facet_map.size_local + facet_map.num_ghosts
facets = np.arange(num_facets_on_proc + 1, dtype=np.int32)
V = functionspace(mesh, ("Lagrange", 1))
with pytest.raises(
RuntimeError, match=r".*is bigger than the number of entities on this process.*"
):
locate_dofs_topological(V, mesh.topology.dim - 1, facets)
Loading