Skip to content

Commit

Permalink
Include pyAMReX.H & ODR Fix
Browse files Browse the repository at this point in the history
We provide a single helper include now for all pyAMReX compilation
units (TUs) / cpp files.

This ensures that optional pybind11 includes, e.g., for stl, numpy,
functions and operators, are consistently present in all TUs. This is
required to avoid C++ one-definition-rule (ODR) violations, which are
undefined behavior and thus could cause instabilities down the road.
  • Loading branch information
ax3l committed Jul 19, 2023
1 parent 9cdc6ce commit b881a08
Show file tree
Hide file tree
Showing 31 changed files with 179 additions and 217 deletions.
13 changes: 5 additions & 8 deletions src/AmrCore/AmrMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_Config.H>
#include <AMReX_AmrMesh.H>
#include "pyAMReX.H"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <AMReX_AmrMesh.H>

#include <sstream>

namespace py = pybind11;
using namespace amrex;

void init_AmrMesh(py::module &m)
{
using namespace amrex;

void init_AmrMesh(py::module &m) {
py::class_< AmrInfo >(m, "AmrInfo")
.def("__repr__",
[](AmrInfo const & amr_info) {
Expand Down Expand Up @@ -75,6 +73,5 @@ void init_AmrMesh(py::module &m) {
.def("finest_level", &AmrMesh::finestLevel)
.def("ref_ratio", py::overload_cast< >(&AmrMesh::refRatio, py::const_))
.def("ref_ratio", py::overload_cast< int >(&AmrMesh::refRatio, py::const_))

;
}
12 changes: 5 additions & 7 deletions src/Base/AMReX.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
#include <AMReX_Config.H>
#include "pyAMReX.H"

#include <AMReX.H>
#include <AMReX_Vector.H>
#include <AMReX_ParmParse.H>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <string>

namespace py = pybind11;
using namespace amrex;

namespace amrex {
struct Config {};
}

void init_AMReX(py::module& m)
{
using namespace amrex;

py::class_<AMReX>(m, "AMReX")
.def_static("empty", &AMReX::empty)
.def_static("size", &AMReX::size)
.def_static("erase", &AMReX::erase)
.def_static("top", &AMReX::top,
py::return_value_policy::reference)
;
;

py::class_<Config>(m, "Config")
.def_property_readonly_static(
Expand Down
11 changes: 4 additions & 7 deletions src/Base/Arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_Arena.H>

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include "pyAMReX.H"

namespace py = pybind11;
using namespace amrex;
#include <AMReX_Arena.H>


void init_Arena(py::module &m) {
using namespace amrex;

py::class_< Arena >(m, "Arena");

m.def("The_Arena", &The_Arena, py::return_value_policy::reference)
Expand Down
13 changes: 6 additions & 7 deletions src/Base/Array4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include "pyAMReX.H"

#include <AMReX_Array4.H>
#include <AMReX_BLassert.H>
#include <AMReX_IntVect.H>

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

#include <cstdint>
#include <sstream>
#include <type_traits>

namespace py = pybind11;
using namespace amrex;


namespace
{
using namespace amrex;

/** CPU: __array_interface__ v3
*
* https://numpy.org/doc/stable/reference/arrays.interface.html
Expand Down Expand Up @@ -77,6 +74,8 @@ namespace
template< typename T >
void make_Array4(py::module &m, std::string typestr)
{
using namespace amrex;

// dispatch simpler via: py::format_descriptor<T>::format() naming
auto const array_name = std::string("Array4_").append(typestr);
py::class_< Array4<T> >(m, array_name.c_str())
Expand Down
11 changes: 6 additions & 5 deletions src/Base/BaseFab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_FArrayBox.H>
#include "pyAMReX.H"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <AMReX_FArrayBox.H>

#include <istream>

namespace py = pybind11;
using namespace amrex;

namespace
{
using namespace amrex;

template< typename T >
void init_bf(py::module &m, std::string typestr) {
auto const bf_name = std::string("BaseFab_").append(typestr);
Expand Down Expand Up @@ -122,5 +121,7 @@ namespace
}

void init_BaseFab(py::module &m) {
using namespace amrex;

init_bf<Real>(m, "Real");
}
14 changes: 6 additions & 8 deletions src/Base/Box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_Config.H>
#include "pyAMReX.H"

#include <AMReX_Box.H>
#include <AMReX_IntVect.H>

#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>

#include <sstream>
#include <optional>

namespace py = pybind11;
using namespace amrex;

namespace
{
using namespace amrex;

/** A little Wrapper class to iterate an amrex::Box via
* amrex::Box::next().
*/
Expand Down Expand Up @@ -66,8 +63,9 @@ namespace
}

void init_Box(py::module &m) {
py::class_< Direction >(m, "Direction");
using namespace amrex;

py::class_< Direction >(m, "Direction");


py::class_< Box >(m, "Box")
Expand Down
11 changes: 4 additions & 7 deletions src/Base/BoxArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_Config.H>
#include "pyAMReX.H"

#include <AMReX_BoxArray.H>
#include <AMReX_IntVect.H>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <sstream>

namespace py = pybind11;
using namespace amrex;


void init_BoxArray(py::module &m) {
using namespace amrex;

/* A collection of Boxes stored in an Array. It is a
* reference-counted concrete class, not a polymorphic one; i.e. you
* cannot use any of the List member functions with a BoxList
Expand Down
12 changes: 4 additions & 8 deletions src/Base/CoordSys.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#include <AMReX_Config.H>
#include <AMReX_CoordSys.H>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "pyAMReX.H"

#include <sstream>
#include <AMReX_CoordSys.H>

namespace py = pybind11;
using namespace amrex;

void init_CoordSys(py::module& m)
{
using namespace amrex;

py::class_<CoordSys> coord_sys(m, "CoordSys");
coord_sys.def("__repr__",
[](const CoordSys&) {
Expand Down
10 changes: 4 additions & 6 deletions src/Base/Dim3.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include <AMReX_Config.H>
#include <AMReX_Dim3.H>
#include "pyAMReX.H"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <AMReX_Dim3.H>

#include <sstream>

namespace py = pybind11;
using namespace amrex;

void init_Dim3(py::module& m)
{
using namespace amrex;

py::class_<Dim3>(m, "Dim3")
.def("__repr__",
[](const Dim3& d) {
Expand Down
11 changes: 4 additions & 7 deletions src/Base/DistributionMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_Config.H>
#include "pyAMReX.H"

#include <AMReX_BoxArray.H>
#include <AMReX_DistributionMapping.H>
#include <AMReX_Vector.H>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <sstream>

namespace py = pybind11;
using namespace amrex;


void init_DistributionMapping(py::module &m) {
using namespace amrex;

py::class_< DistributionMapping >(m, "DistributionMapping")
.def("__repr__",
[](DistributionMapping const & dm) {
Expand Down
10 changes: 4 additions & 6 deletions src/Base/FArrayBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
* Authors: Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_FArrayBox.H>
#include "pyAMReX.H"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <AMReX_FArrayBox.H>

#include <istream>
#include <ostream>
#include <string>

namespace py = pybind11;
using namespace amrex;


void init_FArrayBox(py::module &m) {
using namespace amrex;

py::class_< FArrayBox, BaseFab<Real> >(m, "FArrayBox")
.def("__repr__",
[](FArrayBox const & /* fab */) {
Expand Down
11 changes: 4 additions & 7 deletions src/Base/Geometry.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#include <AMReX_Config.H>
#include "pyAMReX.H"

#include <AMReX_Geometry.H>
#include <AMReX_CoordSys.H>
#include <AMReX_MultiFab.H>
#include <AMReX_Periodicity.H>

#include <pybind11/pybind11.h>
// #include <pybind11/operators.h>
#include <pybind11/stl.h>

#include <sstream>
#include <string>
#include <stdexcept>

namespace py = pybind11;
using namespace amrex;

void init_Geometry(py::module& m)
{
using namespace amrex;

py::class_<GeometryData>(m, "GeometryData")
.def("__repr__",
[](const GeometryData&) {
Expand Down
11 changes: 4 additions & 7 deletions src/Base/IndexType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
* Authors: David Grote
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_Config.H>
#include "pyAMReX.H"

#include <AMReX_Dim3.H>
#include <AMReX_IntVect.H>
#include <AMReX_IndexType.H>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

#include <array>
#include <sstream>
#include <string>

namespace py = pybind11;
using namespace amrex;

namespace {
int check_index(const int i)
Expand All @@ -30,6 +25,8 @@ namespace {
}

void init_IndexType(py::module &m) {
using namespace amrex;

py::class_< IndexType > index_type(m, "IndexType");
index_type.def("__repr__",
[](py::object& obj) {
Expand Down
Loading

0 comments on commit b881a08

Please sign in to comment.