Tomographer  v5.2
Tomographer C++ Framework Documentation
common.h
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #ifndef TOMOPY_COMMON_H
29 #define TOMOPY_COMMON_H
30 
31 #include <cstdio>
32 #include <string>
33 
34 #include <pybind11/pybind11.h>
35 
36 namespace py = pybind11;
37 
38 
39 #ifdef _WIN32
40 # ifdef _tomographer_cxx_EXPORTS
41 # define TOMOGRAPHER_EXPORT __declspec(dllexport)
42 # else
43 # define TOMOGRAPHER_EXPORT __declspec(dllimport)
44 # endif
45 #else
46 # define TOMOGRAPHER_EXPORT __attribute__((visibility("default")))
47 #endif
48 
49 
50 // DEBUGGING ONLY: set TOMOGRAPHERPY_DEBUG_EIGEN_ASSERT_CAUSES_ABORT to cause eigen_assert() failures to abort() and dump core
51 #ifndef TOMOGRAPHERPY_DEBUG_EIGEN_ASSERT_CAUSES_ABORT
52 # define TOMOGRAPHER_EIGEN_ASSERT_EXCEPTION
53 #endif
55 
56 // include this AFTER eigen_assert_exception
57 #pragma GCC visibility push(default)
58 #include <pybind11/eigen.h>
59 
60 #ifdef EIGEN_NO_DEBUG
61 # error "TomographerPy requires enabled Eigen assertions, otherwise `TomographerCxxError` won't be raised as documented."
62 #endif
63 
64 #include <Eigen/Core>
65 #pragma GCC visibility pop
66 
67 #include <tomographer/tomographer_version.h>
70 
71 #include <tomographerpy/pylogger.h>
72 
73 
74 // now already provided in <tomographer/tools/cxxutil.h> :
75 //
76 // // get a demangle() function from Boost, either with boost::core::demangle() (boost >=
77 // // 1.56) or boost::units::detail::demangle() (boost before that)
78 // #include <boost/version.hpp>
79 // #if BOOST_VERSION >= 105600
80 // #include <boost/core/demangle.hpp>
81 // #else
82 // #include <boost/units/detail/utility.hpp>
83 // namespace boost { namespace core { using boost::units::detail::demangle; } }
84 // #endif
85 
86 
87 namespace tpy {
88 
90 typedef double RealType;
92 typedef long CountIntType;
93 
101 typedef Eigen::Matrix<std::complex<RealType>, Eigen::Dynamic, Eigen::Dynamic> CplxMatrixType;
102 
105 
119 inline py::module import_tomographer()
120 {
121  auto tomographer_module = py::module::import("tomographer");
122  if (PyErr_Occurred() != NULL) {
123  throw py::error_already_set();
124  }
125  const std::string module_tomographer_version =
126  tomographer_module.attr("__version__").cast<std::string>();
127  if (module_tomographer_version != TOMOGRAPHER_VERSION) {
128  throw std::runtime_error(
129  "Error: Version of compiled tomographer python module ("+module_tomographer_version +
130  ") does not match version used to compile the current module (" + std::string(TOMOGRAPHER_VERSION)
131  + "). If you updated tomographer, please recompile all dependent modules."
132  ) ;
133  }
134  const std::string this_pybind11_ver =
135  std::to_string(PYBIND11_VERSION_MAJOR) + std::string(".") +
136  std::to_string(PYBIND11_VERSION_MINOR) + std::string(".") +
137  std::to_string(PYBIND11_VERSION_PATCH) ;
138  const std::string module_tomographer_pybind11_ver =
139  tomographer_module.attr("version").attr("compile_info").attr("get")("pybind11", "").cast<std::string>();
140  if (module_tomographer_pybind11_ver != this_pybind11_ver) {
141  throw std::runtime_error(
142  "Error: Compiled tomographer's version of PyBind11 (" + module_tomographer_pybind11_ver +
143  ") does not match version used to compile the current module (" + this_pybind11_ver
144  + "). Please recompile all modules using the same PyBind11 version."
145  ) ;
146  }
147  return tomographer_module;
148 }
149 
150 
151 
152 } // namespace tpy
153 
154 
155 
156 #endif
T to_string(T... args)
double RealType
Real type for template arguments (double)
Definition: common.h:90
STL class.
Eigen::Matrix< std::complex< RealType >, Eigen::Dynamic, 1 > CplxVectorType
Shorthand, a 1-D Eigen::Matrix of std::complex<RealType>&#39;s.
Definition: common.h:99
Eigen::Matrix< RealType, Eigen::Dynamic, Eigen::Dynamic > RealMatrixType
Shorthand, a 2-D Eigen::Matrix of RealType&#39;s.
Definition: common.h:97
Some C++ utilities, with a tad of C++11 tricks.
Eigen::Matrix< RealType, Eigen::Dynamic, 1 > RealVectorType
Shorthand, a 1-D Eigen::Matrix of RealType&#39;s.
Definition: common.h:95
Define tools for Eigen&#39;s eigen_assert() to throw an exception instead of assert&#39;ing.
Eigen::Matrix< CountIntType, Eigen::Dynamic, 1 > CountIntVectorType
Shorthand, a 1-D Eigen::Matrix of CountIntType&#39;s.
Definition: common.h:104
long CountIntType
Integer type for template arguments (`long&#39; in case of long random walks)
Definition: common.h:92
Eigen::Matrix< std::complex< RealType >, Eigen::Dynamic, Eigen::Dynamic > CplxMatrixType
Shorthand, a 2-D Eigen::Matrix of std::complex<RealType>&#39;s.
Definition: common.h:101
C++ Classes and Utilities for Python Modules.
Definition: common.h:87
py::module import_tomographer()
Import tomographer definitions into other Python modules.
Definition: common.h:119
Utilities for logging messages.