-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpy_mmopt.cpp
109 lines (87 loc) · 2.62 KB
/
py_mmopt.cpp
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
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#define EXPORT_TENSORND_TO_PYBIND11
#include "include/tensorND.hpp"
#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
int add(int i, int j)
{
return i + j;
}
namespace py = pybind11;
PYBIND11_MODULE(mmopt, m)
{
m.doc() = R"pbdoc(
Pybind11 example plugin
-----------------------
.. currentmodule:: mmopt
.. autosummary::
:toctree: _generate
add
subtract
)pbdoc";
m.def("add", &add, R"pbdoc(
Add two numbers
Some other explanation about the add function.
)pbdoc");
m.def(
"subtract", [](int i, int j)
{ return i - j; },
R"pbdoc(
Subtract two numbers
Some other explanation about the subtract function.
)pbdoc");
tensorND<float>::bind2py(m);
m.def("h", [](tensorND<float> & t, float x) {
t.for_each([&](size_t idx, int * coord){
t(coord) = x;
return true;
});
});
m.def("g", []() {
tensorND<float> t({4,5}, false);
t.for_each([&](size_t idx, int * coord){
t(coord) = idx;
return true;
});
return t;
});
m.def("g2", []() {
tensorND<float> t({4,5}, false);
t.for_each([&](size_t idx, int * coord){
t(coord) = idx;
return true;
});
return t.to_array();
});
m.def("f", []() {
// Allocate and initialize some data; make this big so
// we can see the impact on the process memory use:
constexpr size_t size = 100*1000*1000;
double *foo = new double[size];
for (size_t i = 0; i < size; i++) {
foo[i] = (double) i;
}
std::cerr << "created memory @ " << reinterpret_cast<void*>(foo) << "\n";
// Create a Python object that will free the allocated
// memory when destroyed:
py::capsule free_when_done(foo, [](void *f) {
double *foo = reinterpret_cast<double *>(f);
std::cerr << "Element [0] = " << foo[0] << "\n";
std::cerr << "freeing memory @ " << f << "\n";
delete[] foo;
});
return py::array_t<double>(
{100, 1000, 1000}, // shape
{1000*1000*8, 1000*8, 8}, // C-style contiguous strides for double
foo, // the data pointer
free_when_done); // numpy array references this parent
});
#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
}