1-DAV-202 Data Management 2024/25

Materials · Introduction · Rules · Contact
· Please fill in the following survey


Difference between revisions of "Lcpp"

From MAD
Jump to navigation Jump to search
(Created page with "<!-- NOTEX --> HWweb <!-- /NOTEX -->")
 
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
<!-- NOTEX -->
 
<!-- NOTEX -->
[[HWweb]]
+
[[HWcpp]]
 
<!-- /NOTEX -->
 
<!-- /NOTEX -->
 +
 +
Python is a very flexible language with many libraries, but programs in Python can be slow and take a lot of memory compared with programs written for example in C++. In this lecture we will see how to combine C++ and Python so that you write core algorithmic parts in C++ but use Python to do other less time-sensitive computations, such as parsing inputs. We will be using [https://pybind11.readthedocs.io/en/stable/basics.html#creating-bindings-for-a-simple-function pybind11 library] to do that.
 +
 +
Here is an example C++ code, which can be converted as an extension and used in Python:
 +
 +
<syntaxhighlight lang="C++">
 +
#include <pybind11/pybind11.h>
 +
#include <pybind11/stl.h>
 +
#include <vector>
 +
#include <string>
 +
 +
using std::vector;
 +
using std::string;
 +
 +
 +
// Here is your class code
 +
struct MyClass {
 +
    MyClass() {}
 +
    int f(vector<string> arr) {
 +
        int ss = 0;
 +
        for (auto &s: arr) {
 +
            ss += s.size();
 +
        }
 +
        return ss;
 +
    }
 +
};
 +
 +
 +
// This is the binding code, every function you want to export, must be declared here
 +
PYBIND11_MODULE(example, m) {
 +
    m.doc() = "pybind11 example plugin"; // optional module docstring
 +
 +
    pybind11::class_<MyClass>(m, "MyClass")
 +
        .def(pybind11::init())
 +
        .def("f", &MyClass::f);
 +
}
 +
</syntaxhighlight>
 +
 +
We can then compile this code to be used as Python module:
 +
<syntaxhighlight lang="bash">
 +
c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cc -o example$(python3-config --extension-suffix)
 +
</syntaxhighlight>
 +
 +
And then we can import it in Python (we need to be in same directory as compiled output):
 +
 +
<syntaxhighlight lang="Python">
 +
import example
 +
 +
c = example.MyClass()
 +
print(c.f(["aaa", "bb"]))
 +
print(c.f([]))
 +
</syntaxhighlight>

Latest revision as of 13:17, 30 April 2024

HWcpp

Python is a very flexible language with many libraries, but programs in Python can be slow and take a lot of memory compared with programs written for example in C++. In this lecture we will see how to combine C++ and Python so that you write core algorithmic parts in C++ but use Python to do other less time-sensitive computations, such as parsing inputs. We will be using pybind11 library to do that.

Here is an example C++ code, which can be converted as an extension and used in Python:

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
#include <string>

using std::vector;
using std::string;


// Here is your class code
struct MyClass {
    MyClass() {}
    int f(vector<string> arr) {
        int ss = 0;
        for (auto &s: arr) {
            ss += s.size();
        }
        return ss;
    }
};


// This is the binding code, every function you want to export, must be declared here
PYBIND11_MODULE(example, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring

    pybind11::class_<MyClass>(m, "MyClass")
        .def(pybind11::init())
        .def("f", &MyClass::f);
}

We can then compile this code to be used as Python module:

c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cc -o example$(python3-config --extension-suffix)

And then we can import it in Python (we need to be in same directory as compiled output):

import example

c = example.MyClass()
print(c.f(["aaa", "bb"]))
print(c.f([]))