Giter Site home page Giter Site logo

pbnp_demo's Introduction

pybind11 / numpy Demo

Introduction

pybind11 is really cool. I can write explicitly typed and performant C++ code that is accessible from python. Usually this means doing a bunch of heavy lifting in C++ and then providing a python interface to get data, check status, etc.

In some cases, I require that I be able to pass large amounts of structured data quickly - for example, when collecting data from a high-throughput sensor. In this case, it really matters that my communication of these data from C++ to python is performant.

pybind11 has a fairly easy way to pass a std::vector<T> and some other built-in STL containers, which is the natural way to accomplish this, at least to my lizard brain. For my case, T would be a struct that just holds some data I want to pass around. More info can be found here .

This approach ends up being slow, since the vector's underlying data is copied when passed into python. You'll typically then perform another copy when converting to a numpy array. This is slow - we'd rather get a numpy.ndarray directly, and ideally, in a way that it knows to copy the reference to the underlying data, and not the data itself.

This repo demonstrates how to build the numpy structured array in C++ and then pass it directly into python. This avoids the copies and results in very good performance. A single function call and then you've got a numpy object, ready to go. Great!

The test code is written to generate a sizeable chunk of data and benchmark passing it from C++ to python via the two different mechanisms.

There's also a Gadget showing how to pass multidimensional numpy arrays both ways, from python to C++, and vice-versa.

Instructions

Install with pip install --user . from the base of this repo. Test with python scripts/test_generator.py or python scripts/test_gadget.py.

pbnp_demo's People

Contributors

micahfolsom avatar

Stargazers

Aaron Nowack 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.