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

Replace std::map by std::unordered_map #43

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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: 4 additions & 4 deletions multiphenicsx/cpp/multiphenicsx/fem/DofMapRestriction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ DofMapRestriction::DofMapRestriction(
//-----------------------------------------------------------------------------
void DofMapRestriction::_compute_cell_dofs(std::shared_ptr<const DofMap> dofmap)
{
// Fill in cell dofs first into a temporary std::map
std::map<int, std::vector<std::int32_t>> restricted_cell_dofs;
// Fill in cell dofs first into a temporary std::unordered_map
std::unordered_map<int, std::vector<std::int32_t>> restricted_cell_dofs;
std::size_t restricted_cell_dofs_total_size = 0;
auto unrestricted_cell_dofs = dofmap->map();
const int num_cells = unrestricted_cell_dofs.extent(0);
Expand Down Expand Up @@ -82,8 +82,8 @@ void DofMapRestriction::_compute_cell_dofs(std::shared_ptr<const DofMap> dofmap)
}
}

// Flatten std::map into the std::vector dof_array, and store start/end
// indices associated to each cell in cell_bounds
// Flatten std::unordered_map into the std::vector dof_array, and store
// start/end indices associated to each cell in cell_bounds
_dof_array.reserve(restricted_cell_dofs_total_size);
_cell_bounds.reserve(num_cells + 1);
std::size_t current_cell_bound = 0;
Expand Down
12 changes: 7 additions & 5 deletions multiphenicsx/cpp/multiphenicsx/fem/DofMapRestriction.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include <dolfinx/common/IndexMap.h>
#include <dolfinx/fem/DofMap.h>
#include <map>
#include <memory>
#include <unordered_map>

namespace multiphenicsx
{
Expand Down Expand Up @@ -59,13 +59,15 @@ class DofMapRestriction
std::shared_ptr<const dolfinx::fem::DofMap> dofmap() const { return _dofmap; }

/// Return map from unrestricted dofs to restricted dofs
const std::map<std::int32_t, std::int32_t>& unrestricted_to_restricted() const
const std::unordered_map<std::int32_t, std::int32_t>&
unrestricted_to_restricted() const
{
return _unrestricted_to_restricted;
}

/// Map from restricted dofs to unrestricted dofs
const std::map<std::int32_t, std::int32_t>& restricted_to_unrestricted() const
const std::unordered_map<std::int32_t, std::int32_t>&
restricted_to_unrestricted() const
{
return _restricted_to_unrestricted;
}
Expand Down Expand Up @@ -94,10 +96,10 @@ class DofMapRestriction
std::shared_ptr<const dolfinx::fem::DofMap> _dofmap;

// Map from unrestricted dofs to restricted dofs
std::map<std::int32_t, std::int32_t> _unrestricted_to_restricted;
std::unordered_map<std::int32_t, std::int32_t> _unrestricted_to_restricted;

// Map from restricted dofs to unrestricted dofs
std::map<std::int32_t, std::int32_t> _restricted_to_unrestricted;
std::unordered_map<std::int32_t, std::int32_t> _restricted_to_unrestricted;

// Cell-local-to-dof map after restriction has been carried out
std::vector<std::int32_t> _dof_array;
Expand Down
8 changes: 5 additions & 3 deletions multiphenicsx/cpp/multiphenicsx/la/petsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ MatSubMatrixWrapper::MatSubMatrixWrapper(Mat A, std::array<IS, 2> index_sets)
MatSubMatrixWrapper::MatSubMatrixWrapper(
Mat A, std::array<IS, 2> unrestricted_index_sets,
std::array<IS, 2> restricted_index_sets,
std::array<std::map<std::int32_t, std::int32_t>, 2>
std::array<std::unordered_map<std::int32_t, std::int32_t>, 2>
unrestricted_to_restricted,
std::array<int, 2> unrestricted_to_restricted_bs)
: MatSubMatrixWrapper(A, restricted_index_sets)
Expand Down Expand Up @@ -348,7 +348,8 @@ VecSubVectorReadWrapper::VecSubVectorReadWrapper(Vec x, IS index_set,
//-----------------------------------------------------------------------------
VecSubVectorReadWrapper::VecSubVectorReadWrapper(
Vec x, IS unrestricted_index_set, IS restricted_index_set,
const std::map<std::int32_t, std::int32_t>& unrestricted_to_restricted,
const std::unordered_map<std::int32_t, std::int32_t>&
unrestricted_to_restricted,
int unrestricted_to_restricted_bs, bool ghosted)
: _ghosted(ghosted)
{
Expand Down Expand Up @@ -446,7 +447,8 @@ VecSubVectorWrapper::VecSubVectorWrapper(Vec x, IS index_set, bool ghosted)
//-----------------------------------------------------------------------------
VecSubVectorWrapper::VecSubVectorWrapper(
Vec x, IS unrestricted_index_set, IS restricted_index_set,
const std::map<std::int32_t, std::int32_t>& unrestricted_to_restricted,
const std::unordered_map<std::int32_t, std::int32_t>&
unrestricted_to_restricted,
int unrestricted_to_restricted_bs, bool ghosted)
: VecSubVectorReadWrapper(x, unrestricted_index_set, restricted_index_set,
unrestricted_to_restricted,
Expand Down
28 changes: 14 additions & 14 deletions multiphenicsx/cpp/multiphenicsx/la/petsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#pragma once

#include <dolfinx/common/IndexMap.h>
#include <map>
#include <petscmat.h>
#include <petscvec.h>
#include <unordered_map>
#include <vector>

namespace multiphenicsx::la
Expand Down Expand Up @@ -63,11 +63,12 @@ class MatSubMatrixWrapper
MatSubMatrixWrapper(Mat A, std::array<IS, 2> index_sets),

/// Constructor (for cases with restriction)
MatSubMatrixWrapper(Mat A, std::array<IS, 2> unrestricted_index_sets,
std::array<IS, 2> restricted_index_sets,
std::array<std::map<std::int32_t, std::int32_t>, 2>
unrestricted_to_restricted,
std::array<int, 2> unrestricted_to_restricted_bs);
MatSubMatrixWrapper(
Mat A, std::array<IS, 2> unrestricted_index_sets,
std::array<IS, 2> restricted_index_sets,
std::array<std::unordered_map<std::int32_t, std::int32_t>, 2>
unrestricted_to_restricted,
std::array<int, 2> unrestricted_to_restricted_bs);

/// Destructor
~MatSubMatrixWrapper();
Expand Down Expand Up @@ -105,12 +106,11 @@ class VecSubVectorReadWrapper
VecSubVectorReadWrapper(Vec x, IS index_set, bool ghosted = true),

/// Constructor (for cases with restriction)
VecSubVectorReadWrapper(Vec x, IS unrestricted_index_set,
IS restricted_index_set,
const std::map<std::int32_t, std::int32_t>&
unrestricted_to_restricted,
int unrestricted_to_restricted_bs,
bool ghosted = true);
VecSubVectorReadWrapper(
Vec x, IS unrestricted_index_set, IS restricted_index_set,
const std::unordered_map<std::int32_t, std::int32_t>&
unrestricted_to_restricted,
int unrestricted_to_restricted_bs, bool ghosted = true);

/// Destructor
~VecSubVectorReadWrapper();
Expand Down Expand Up @@ -146,7 +146,7 @@ class VecSubVectorWrapper : public VecSubVectorReadWrapper
/// Constructor (for cases with restriction)
VecSubVectorWrapper(Vec x, IS unrestricted_index_set,
IS restricted_index_set,
const std::map<std::int32_t, std::int32_t>&
const std::unordered_map<std::int32_t, std::int32_t>&
unrestricted_to_restricted,
int unrestricted_to_restricted_bs,
bool ghosted = true);
Expand All @@ -172,7 +172,7 @@ class VecSubVectorWrapper : public VecSubVectorReadWrapper
private:
Vec _global_vector;
IS _is;
std::map<std::int32_t, std::int32_t> _restricted_to_unrestricted;
std::unordered_map<std::int32_t, std::int32_t> _restricted_to_unrestricted;
};

} // namespace petsc
Expand Down
2 changes: 1 addition & 1 deletion multiphenicsx/cpp/multiphenicsx/wrappers/fem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#include <nanobind/ndarray.h>
#include <nanobind/stl/array.h>
#include <nanobind/stl/complex.h>
#include <nanobind/stl/map.h>
#include <nanobind/stl/pair.h>
#include <nanobind/stl/shared_ptr.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/unordered_map.h>
#include <nanobind/stl/vector.h>
#include <petsc4py/petsc4py.h>
#include <span>
Expand Down
19 changes: 11 additions & 8 deletions multiphenicsx/cpp/multiphenicsx/wrappers/la.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include <nanobind/ndarray.h>
#include <nanobind/stl/array.h>
#include <nanobind/stl/complex.h>
#include <nanobind/stl/map.h>
#include <nanobind/stl/pair.h>
#include <nanobind/stl/unordered_map.h>
#include <nanobind/stl/vector.h>
#include <petsc4py/petsc4py.h>
#include <petscis.h>
Expand All @@ -37,9 +37,10 @@ void la_petsc_module(nb::module_& m)
m, "MatSubMatrixWrapper")
.def(nb::init<Mat, std::array<IS, 2>>(), nb::arg("A"),
nb::arg("index_sets"))
.def(nb::init<Mat, std::array<IS, 2>, std::array<IS, 2>,
std::array<std::map<std::int32_t, std::int32_t>, 2>,
std::array<int, 2>>(),
.def(nb::init<
Mat, std::array<IS, 2>, std::array<IS, 2>,
std::array<std::unordered_map<std::int32_t, std::int32_t>, 2>,
std::array<int, 2>>(),
nb::arg("A"), nb::arg("unrestricted_index_sets"),
nb::arg("restricted_index_sets"),
nb::arg("unrestricted_to_restricted"),
Expand All @@ -57,8 +58,9 @@ void la_petsc_module(nb::module_& m)
m, "VecSubVectorReadWrapper")
.def(nb::init<Vec, IS, bool>(), nb::arg("x"), nb::arg("index_set"),
nb::arg("ghosted") = true)
.def(nb::init<Vec, IS, IS, const std::map<std::int32_t, std::int32_t>&,
int, bool>(),
.def(nb::init<Vec, IS, IS,
const std::unordered_map<std::int32_t, std::int32_t>&, int,
bool>(),
nb::arg("x"), nb::arg("unrestricted_index_set"),
nb::arg("restricted_index_set"),
nb::arg("unrestricted_to_restricted"),
Expand All @@ -78,8 +80,9 @@ void la_petsc_module(nb::module_& m)
m, "VecSubVectorWrapper")
.def(nb::init<Vec, IS, bool>(), nb::arg("x"), nb::arg("index_set"),
nb::arg("ghosted") = true)
.def(nb::init<Vec, IS, IS, const std::map<std::int32_t, std::int32_t>&,
int, bool>(),
.def(nb::init<Vec, IS, IS,
const std::unordered_map<std::int32_t, std::int32_t>&, int,
bool>(),
nb::arg("x"), nb::arg("unrestricted_index_set"),
nb::arg("restricted_index_set"),
nb::arg("unrestricted_to_restricted"),
Expand Down
Loading