-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpydefix.hpp
More file actions
227 lines (186 loc) · 7.21 KB
/
pydefix.hpp
File metadata and controls
227 lines (186 loc) · 7.21 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// ***********************************************************************************
// Idefix MHD astrophysical code
// Copyright(C) Geoffroy R. J. Lesur <geoffroy.lesur@univ-grenoble-alpes.fr>
// and other code contributors
// Licensed under CeCILL 2.1 License, see COPYING for more information
// ***********************************************************************************
#ifndef PYDEFIX_HPP_
#define PYDEFIX_HPP_
#define PYBIND11_DETAILED_ERROR_MESSAGES
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/numpy.h>
#include <string>
#include <vector>
#include "idefix.hpp"
#include "input.hpp"
namespace py = pybind11;
class DataBlock;
class DataBlockHost;
class Pydefix {
public:
explicit Pydefix(Input&);
~Pydefix();
void Output(DataBlock &, int);
void InitFlow(DataBlock &);
void ShowConfig();
bool isActive{false};
bool haveOutput{false};
bool haveInitflow{false};
private:
template<typename... Ts>
void CallScript(std::string, std::string, Ts...);
static int ninstance;
std::string scriptFilename;
std::string outputFunctionName;
std::string initflowFunctionName;
};
namespace pybind11 { namespace detail {
// Caster for IdefixArray4D<T>
template <typename T> struct type_caster<IdefixHostArray4D<T>> {
public:
PYBIND11_TYPE_CASTER(IdefixHostArray4D<T>, _("IdefixHostArray4D<T>"));
// Conversion part 1 (Python -> C++)
bool load(py::handle src, bool convert) {
if ( !convert && !py::array_t<T>::check_(src) )
return false;
auto array = py::array_t<T, py::array::c_style | py::array::forcecast>::ensure(src);
if ( !array )
return false;
auto dims = array.ndim();
if ( dims != 4 )
return false;
auto buf = array.request();
value = Kokkos::View<T****,
Kokkos::LayoutRight,
Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>> (reinterpret_cast<T*>(buf.ptr),
array.shape()[0],
array.shape()[1],
array.shape()[2],
array.shape()[3]);
return true;
}
//Conversion part 2 (C++ -> Python)
static py::handle cast(const IdefixHostArray4D<T>& src,
py::return_value_policy policy,
py::handle parent) {
py::none dummyDataOwner;
py::array_t<real, py::array::c_style> a({src.extent(0),
src.extent(1),
src.extent(2),
src.extent(3)},
src.data(), dummyDataOwner);
return a.release();
}
};
// Caster for IdefixArray3D<T>
template <typename T> struct type_caster<IdefixHostArray3D<T>> {
public:
PYBIND11_TYPE_CASTER(IdefixHostArray3D<T>, _("IdefixHostArray3D<T>"));
// Conversion part 1 (Python -> C++)
bool load(py::handle src, bool convert) {
idfx::pushRegion("Pydefix::TypeCaster3D Python->C");
if ( !convert && !py::array_t<T>::check_(src) )
return false;
auto array = py::array_t<T, py::array::c_style | py::array::forcecast>::ensure(src);
if ( !array )
return false;
auto dims = array.ndim();
if ( dims != 3 )
return false;
auto buf = array.request();
value = Kokkos::View<T***,
Kokkos::LayoutRight,
Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>> (reinterpret_cast<T*>(buf.ptr),
array.shape()[0],
array.shape()[1],
array.shape()[2]);
idfx::popRegion();
return true;
}
//Conversion part 2 (C++ -> Python)
static py::handle cast(const IdefixHostArray3D<T>& src,
py::return_value_policy policy,
py::handle parent) {
idfx::pushRegion("Pydefix::TypeCaster3D C->Python");
// Keep a local reference
auto arr = src;
py::none dummyDataOwner;
py::array_t<real, py::array::c_style> a({src.extent(0),
src.extent(1),
src.extent(2)},
src.data(),dummyDataOwner);
idfx::popRegion();
return a.release();
}
};
// Caster for IdefixArray2D<T>
template <typename T> struct type_caster<IdefixHostArray2D<T>> {
public:
PYBIND11_TYPE_CASTER(IdefixHostArray2D<T>, _("IdefixHostArray2D<T>"));
// Conversion part 1 (Python -> C++)
bool load(py::handle src, bool convert) {
if ( !convert && !py::array_t<T>::check_(src) )
return false;
auto array = py::array_t<T, py::array::c_style | py::array::forcecast>::ensure(src);
if ( !array )
return false;
auto dims = array.ndim();
if ( dims != 2 )
return false;
auto buf = array.request();
value = Kokkos::View<T**,
Kokkos::LayoutRight,
Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>> (reinterpret_cast<T*>(buf.ptr),
array.shape()[0],
array.shape()[1]);
idfx::popRegion();
return true;
}
//Conversion part 2 (C++ -> Python)
static py::handle cast(const IdefixHostArray2D<T>& src,
py::return_value_policy policy,
py::handle parent) {
py::none dummyOwner;
py::array_t<real, py::array::c_style> a({src.extent(0),src.extent(1)},src.data(),dummyOwner);
return a.release();
}
};
// Caster for IdefixArray1D<T>
template <typename T> struct type_caster<IdefixHostArray1D<T>> {
public:
PYBIND11_TYPE_CASTER(IdefixHostArray1D<T>, _("IdefixHostArray1D<T>"));
// Conversion part 1 (Python -> C++)
bool load(py::handle src, bool convert) {
if ( !convert && !py::array_t<T>::check_(src) )
return false;
auto array = py::array_t<T, py::array::c_style | py::array::forcecast>::ensure(src);
if ( !array )
return false;
auto dims = array.ndim();
if ( dims != 1 )
return false;
auto buf = array.request();
value = Kokkos::View<T*,
Kokkos::LayoutRight,
Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>> (reinterpret_cast<T*>(buf.ptr),
array.shape()[0]);
idfx::popRegion();
return true;
}
//Conversion part 2 (C++ -> Python)
static py::handle cast(const IdefixHostArray1D<T>& src,
py::return_value_policy policy,
py::handle parent) {
py::none dummyDataOwner;
py::array_t<real, py::array::c_style> a(src.extent(0),src.data(),dummyDataOwner);
return a.release();
}
};
} // namespace detail
} // namespace pybind11
#endif // PYDEFIX_HPP_