1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
// Copyright (C) 2017 Chris Richardson, Garth N. Wells and Tormod Landet
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include "MPICommWrapper.h"
#include <mpi4py/mpi4py.h>
#include <nanobind/nanobind.h>
namespace nanobind::detail
{
template <>
class type_caster<dolfinx_wrappers::MPICommWrapper>
{
public:
// Define this->value of type MPICommWrapper
NB_TYPE_CASTER(dolfinx_wrappers::MPICommWrapper, const_name("MPICommWrapper"))
// Python -> C++
bool from_python(handle src, uint8_t /*flags*/,
cleanup_list* /*cleanup*/) noexcept
{
if (!PyMPIComm_Get)
{
if (import_mpi4py() != 0)
return false;
}
if (PyObject_TypeCheck(src.ptr(), &PyMPIComm_Type))
{
value = dolfinx_wrappers::MPICommWrapper(*PyMPIComm_Get(src.ptr()));
return true;
}
else
return false;
}
// C++ -> Python
static handle from_cpp(const dolfinx_wrappers::MPICommWrapper& src,
rv_policy policy, cleanup_list* /*cleanup*/) noexcept
{
if (policy == rv_policy::automatic
or policy == rv_policy::automatic_reference
or policy == rv_policy::reference_internal)
{
if (!PyMPIComm_New)
{
if (import_mpi4py() != 0)
return {};
}
PyObject* c = PyMPIComm_New(src.get());
return nanobind::handle(c);
}
else
return {};
}
operator dolfinx_wrappers::MPICommWrapper() { return this->value; }
};
} // namespace nanobind::detail
|