Giter Site home page Giter Site logo

antonshbk / example_cpp_python_wrapper Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 135 KB

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: MIT License

CMake 29.35% C++ 23.14% C 0.78% Python 42.42% Dockerfile 4.32%
cmake cpp omp openmp pybind11 python wrapper example

example_cpp_python_wrapper's Introduction

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)

example_cpp_python_wrapper's People

Contributors

antonshbk avatar

Stargazers

 avatar Jerdik avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.