Nektar++
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
NekPyConfig.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekPyConfig.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: NekPy configuration to include boost headers and define
32// commonly-used macros.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#ifndef NEKTAR_LIBRARY_LIBUTILITIES_PYTHON_NEKPYCONFIG_HPP
37#define NEKTAR_LIBRARY_LIBUTILITIES_PYTHON_NEKPYCONFIG_HPP
38
39#define PYBIND11_DETAILED_ERROR_MESSAGES
40
41#include <memory>
42#include <pybind11/numpy.h>
43#include <pybind11/pybind11.h>
44#include <pybind11/stl.h>
45#include <pybind11/stl_bind.h>
46
47namespace py = pybind11;
48
49// Define some common STL opaque types
50PYBIND11_MAKE_OPAQUE(std::vector<unsigned int>)
51
52#define SIZENAME(s) SIZE_##s
53#define NEKPY_WRAP_ENUM(MOD, ENUMNAME, MAPNAME) \
54 { \
55 py::enum_<ENUMNAME> tmp(MOD, #ENUMNAME); \
56 for (int a = 0; a < (int)SIZENAME(ENUMNAME); ++a) \
57 { \
58 tmp.value(MAPNAME[a], (ENUMNAME)a); \
59 } \
60 tmp.export_values(); \
61 }
62#define NEKPY_WRAP_ENUM_STRING(MOD, ENUMNAME, MAPNAME) \
63 { \
64 py::enum_<ENUMNAME> tmp(MOD, #ENUMNAME); \
65 for (int a = 0; a < (int)SIZENAME(ENUMNAME); ++a) \
66 { \
67 tmp.value(MAPNAME[a].c_str(), (ENUMNAME)a); \
68 } \
69 tmp.export_values(); \
70 }
71#define NEKPY_WRAP_ENUM_STRING_DOCS(MOD, ENUMNAME, MAPNAME, DOCSTRING) \
72 { \
73 py::enum_<ENUMNAME> tmp(MOD, #ENUMNAME); \
74 for (int a = 0; a < (int)SIZENAME(ENUMNAME); ++a) \
75 { \
76 tmp.value(MAPNAME[a].c_str(), (ENUMNAME)a); \
77 } \
78 tmp.export_values(); \
79 PyTypeObject *pto = reinterpret_cast<PyTypeObject *>(tmp.ptr()); \
80 PyDict_SetItemString(pto->tp_dict, "__doc__", \
81 PyUnicode_FromString(DOCSTRING)); \
82 }
83
84/**
85 * @brief Helper structure to construct C++ command line `argc` and `argv`
86 * variables from a Python list.
87 */
89{
90 /**
91 * @brief Constructor.
92 *
93 * @param py_argv List of command line arguments from Python.
94 */
95 CppCommandLine(py::list &py_argv) : m_argc(py::len(py_argv))
96 {
97 int i = 0;
98 size_t bufSize = 0;
99 char *p;
100
101 m_argv = new char *[m_argc + 1];
102
103 // Create argc, argv to give to the session reader. Note that this needs
104 // to be a contiguous block in memory, otherwise MPI (specifically
105 // OpenMPI) will likely segfault.
106 for (i = 0; i < m_argc; ++i)
107 {
108 std::string tmp = py::cast<std::string>(py_argv[i]);
109 bufSize += tmp.size() + 1;
110 }
111
112 m_buf.resize(bufSize);
113 for (i = 0, p = &m_buf[0]; i < m_argc; ++i)
114 {
115 std::string tmp = py::cast<std::string>(py_argv[i]);
116 std::copy(tmp.begin(), tmp.end(), p);
117 p[tmp.size()] = '\0';
118 m_argv[i] = p;
119 p += tmp.size() + 1;
120 }
121
122 m_argv[m_argc] = nullptr;
123 }
124
125 /**
126 * @brief Destructor.
127 */
129 {
130 if (m_argv == nullptr)
131 {
132 return;
133 }
134
135 delete m_argv;
136 }
137
138 /**
139 * @brief Returns the constructed `argv`.
140 */
141 char **GetArgv()
142 {
143 return m_argv;
144 }
145
146 /**
147 * @brief Returns the constructed `argc`.
148 */
150 {
151 return m_argc;
152 }
153
154private:
155 /// Pointers for strings `argv`.
156 char **m_argv = nullptr;
157 /// Number of arguments `argc`.
158 int m_argc = 0;
159 /// Buffer for storage of the argument strings.
160 std::vector<char> m_buf;
161};
162
163#endif
PYBIND11_MAKE_OPAQUE(LibUtilities::FieldMetaDataMap)
def copy(self)
Definition: pycml.py:2663
Helper structure to construct C++ command line argc and argv variables from a Python list.
Definition: NekPyConfig.hpp:89
int GetArgc()
Returns the constructed argc.
int m_argc
Number of arguments argc.
std::vector< char > m_buf
Buffer for storage of the argument strings.
char ** m_argv
Pointers for strings argv.
~CppCommandLine()
Destructor.
CppCommandLine(py::list &py_argv)
Constructor.
Definition: NekPyConfig.hpp:95
char ** GetArgv()
Returns the constructed argv.