/* tests/eigen_tensor.cpp -- automatic conversion of Eigen Tensor All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ #include #include "pybind11_tests.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE) PYBIND11_WARNING_DISABLE_MSVC(4127) template void reset_tensor(M &x) { for (int i = 0; i < x.dimension(0); i++) { for (int j = 0; j < x.dimension(1); j++) { for (int k = 0; k < x.dimension(2); k++) { x(i, j, k) = i * (5 * 2) + j * 2 + k; } } } } template bool check_tensor(M &x) { for (int i = 0; i < x.dimension(0); i++) { for (int j = 0; j < x.dimension(1); j++) { for (int k = 0; k < x.dimension(2); k++) { if (x(i, j, k) != (i * (5 * 2) + j * 2 + k)) { return false; } } } } return true; } template Eigen::Tensor &get_tensor() { static Eigen::Tensor *x; if (!x) { x = new Eigen::Tensor(3, 5, 2); reset_tensor(*x); } return *x; } template Eigen::TensorMap> &get_tensor_map() { static Eigen::TensorMap> *x; if (!x) { x = new Eigen::TensorMap>(get_tensor()); } return *x; } template Eigen::TensorFixedSize, Options> &get_fixed_tensor() { static Eigen::TensorFixedSize, Options> *x; if (!x) { Eigen::aligned_allocator, Options>> allocator; x = new (allocator.allocate(1)) Eigen::TensorFixedSize, Options>(); reset_tensor(*x); } return *x; } template const Eigen::Tensor &get_const_tensor() { return get_tensor(); } template struct CustomExample { CustomExample() : member(get_tensor()), view_member(member) {} Eigen::Tensor member; Eigen::TensorMap> view_member; }; template void init_tensor_module(pybind11::module &m) { const char *needed_options = ""; if (Options == Eigen::ColMajor) { needed_options = "F"; } else { needed_options = "C"; } m.attr("needed_options") = needed_options; m.def("setup", []() { reset_tensor(get_tensor()); reset_tensor(get_fixed_tensor()); }); m.def("is_ok", []() { return check_tensor(get_tensor()) && check_tensor(get_fixed_tensor()); }); py::class_>(m, "CustomExample") .def(py::init<>()) .def_readonly( "member", &CustomExample::member, py::return_value_policy::reference_internal) .def_readonly("member_view", &CustomExample::view_member, py::return_value_policy::reference_internal); m.def( "copy_fixed_tensor", []() { return &get_fixed_tensor(); }, py::return_value_policy::copy); m.def( "copy_tensor", []() { return &get_tensor(); }, py::return_value_policy::copy); m.def( "copy_const_tensor", []() { return &get_const_tensor(); }, py::return_value_policy::copy); m.def( "move_fixed_tensor_copy", []() -> Eigen::TensorFixedSize, Options> { return get_fixed_tensor(); }, py::return_value_policy::move); m.def( "move_tensor_copy", []() -> Eigen::Tensor { return get_tensor(); }, py::return_value_policy::move); m.def( "move_const_tensor", []() -> const Eigen::Tensor & { return get_const_tensor(); }, py::return_value_policy::move); m.def( "take_fixed_tensor", []() { Eigen::aligned_allocator< Eigen::TensorFixedSize, Options>> allocator; return new (allocator.allocate(1)) Eigen::TensorFixedSize, Options>( get_fixed_tensor()); }, py::return_value_policy::take_ownership); m.def( "take_tensor", []() { return new Eigen::Tensor(get_tensor()); }, py::return_value_policy::take_ownership); m.def( "take_const_tensor", []() -> const Eigen::Tensor * { return new Eigen::Tensor(get_tensor()); }, py::return_value_policy::take_ownership); m.def( "take_view_tensor", []() -> const Eigen::TensorMap> * { return new Eigen::TensorMap>(get_tensor()); }, py::return_value_policy::take_ownership); m.def( "reference_tensor", []() { return &get_tensor(); }, py::return_value_policy::reference); m.def( "reference_tensor_v2", []() -> Eigen::Tensor & { return get_tensor(); }, py::return_value_policy::reference); m.def( "reference_tensor_internal", []() { return &get_tensor(); }, py::return_value_policy::reference_internal); m.def( "reference_fixed_tensor", []() { return &get_tensor(); }, py::return_value_policy::reference); m.def( "reference_const_tensor", []() { return &get_const_tensor(); }, py::return_value_policy::reference); m.def( "reference_const_tensor_v2", []() -> const Eigen::Tensor & { return get_const_tensor(); }, py::return_value_policy::reference); m.def( "reference_view_of_tensor", []() -> Eigen::TensorMap> { return get_tensor_map(); }, py::return_value_policy::reference); m.def( "reference_view_of_tensor_v2", // NOLINTNEXTLINE(readability-const-return-type) []() -> const Eigen::TensorMap> { return get_tensor_map(); // NOLINT(readability-const-return-type) }, // NOLINT(readability-const-return-type) py::return_value_policy::reference); m.def( "reference_view_of_tensor_v3", []() -> Eigen::TensorMap> * { return &get_tensor_map(); }, py::return_value_policy::reference); m.def( "reference_view_of_tensor_v4", []() -> const Eigen::TensorMap> * { return &get_tensor_map(); }, py::return_value_policy::reference); m.def( "reference_view_of_tensor_v5", []() -> Eigen::TensorMap> & { return get_tensor_map(); }, py::return_value_policy::reference); m.def( "reference_view_of_tensor_v6", []() -> const Eigen::TensorMap> & { return get_tensor_map(); }, py::return_value_policy::reference); m.def( "reference_view_of_fixed_tensor", []() { return Eigen::TensorMap< Eigen::TensorFixedSize, Options>>( get_fixed_tensor()); }, py::return_value_policy::reference); m.def("round_trip_tensor", [](const Eigen::Tensor &tensor) { return tensor; }); m.def( "round_trip_tensor_noconvert", [](const Eigen::Tensor &tensor) { return tensor; }, py::arg("tensor").noconvert()); m.def("round_trip_tensor2", [](const Eigen::Tensor &tensor) { return tensor; }); m.def("round_trip_fixed_tensor", [](const Eigen::TensorFixedSize, Options> &tensor) { return tensor; }); m.def( "round_trip_view_tensor", [](Eigen::TensorMap> view) { return view; }, py::return_value_policy::reference); m.def( "round_trip_view_tensor_ref", [](Eigen::TensorMap> &view) { return view; }, py::return_value_policy::reference); m.def( "round_trip_view_tensor_ptr", [](Eigen::TensorMap> *view) { return view; }, py::return_value_policy::reference); m.def( "round_trip_aligned_view_tensor", [](Eigen::TensorMap, Eigen::Aligned> view) { return view; }, py::return_value_policy::reference); m.def( "round_trip_const_view_tensor", [](Eigen::TensorMap> view) { return Eigen::Tensor(view); }, py::return_value_policy::move); m.def( "round_trip_rank_0", [](const Eigen::Tensor &tensor) { return tensor; }, py::return_value_policy::move); m.def( "round_trip_rank_0_noconvert", [](const Eigen::Tensor &tensor) { return tensor; }, py::arg("tensor").noconvert(), py::return_value_policy::move); m.def( "round_trip_rank_0_view", [](Eigen::TensorMap> &tensor) { return tensor; }, py::return_value_policy::reference); } void test_module(py::module_ &); test_initializer name(test_eigen_tensor_module_name, test_module); void test_module(py::module_ &m) { auto f_style = m.def_submodule("f_style"); auto c_style = m.def_submodule("c_style"); init_tensor_module(f_style); init_tensor_module(c_style); } PYBIND11_NAMESPACE_END(PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE)