Skip to content

The project also uses the OpenMP library, this project can serve as an example of use. It is important that the Pybind11 build project has an import of the OpenMP library

License

Notifications You must be signed in to change notification settings

AntonSHBK/example_cpp_python_wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example of wrapping the Cpp library for Python using Pybind11 and OpenMP

The project provides an example of a library written in C++ with the CMake build system, as well as a code wrapping module for working in Python. The C++ library can be used separately, and you can also use a wrapper for Python.

Cpp logotype Python logotype
Pybind11 logotype OpenMP logotype

Interesting materials

Dependencies

  • cmake
  • pybind11

Using C++ library only

git clone https://github.com/AntonSHBK/simple_cpp_python_porting.git

For use in a project in conjunction with Cmake:

if (NOT TARGET cpp_code)
    add_subdirectory(cpp)
endif() 
target_link_libraries(${PROJECT_NAME} cpp_code)

The project also uses the OpenMP library, this project can serve as an example of use. It is important that the Pybind11 build project has an import of the OpenMP library

find_package(OpenMP)
if(OpenMP_CXX_FOUND)
    target_link_libraries( ${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
endif()

Building project

mkdir build && cd build
# unix
cmake ..
make
# win (my machine)
cmake .. -G "MinGW Makefiles"
make

Testing

Testing the library's performance. For test i use CTest. For verbose output you will use:

cd build
ctest --verbose

Using Python library

The deployment was tested on a Linux system, for use on Windows, you need to look at the official documentation.

pip3 install pybind11
pip3 install . --break-system-packages

Run testing library while in the working directory:

python3 python_test.py
#include <omp.h>
#include <pybind11/pybind11.h>

#include <my_lib/my_include_file.h>
#include "python_header.h"

namespace py = pybind11;

int add(const int &a, const int &b) {
    return a + b;
}

PYBIND11_MODULE(CppToPython, m) {
    m.def("add", &add); // добавили функцию в модуль
    m.def("bob", &talker::bob); // добавили функцию в модуль

    py::class_<talker::SomeTalker>(m, "SomeTalker")
        .def(py::init<std::string>())
        .def("get_omp_max_treads", &talker::SomeTalker::get_omp_max_treads)
        .def("get_text_parallel", &talker::SomeTalker::get_text_parallel);
};

There is a Docker container for testing. Running container:

cd /docker
docker-compose up --build

Result:

import CppToPython

print(CppToPython.add(40, 10))
print(CppToPython.bob(40, 10))

print(CppToPython.sum_thread_ids())
print(CppToPython.get_max_threads())

this_talker = CppToPython.SomeTalker('Hello world!')

print(this_talker.get_omp_max_treads())
this_talker.get_text_parallel(4)

About

The project also uses the OpenMP library, this project can serve as an example of use. It is important that the Pybind11 build project has an import of the OpenMP library

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published