Thinking Hybrid - Python/C++ Integration

40
Python Extending/Integrating A Real World Example Tips Summary Python where we can, C ++ where we must Source: http://xkcd.com/413/ Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 1/33

description

Talk given at a Computer Science/Information Technology seminar at the IIMS institute @ Massey University, Albany. Very similar to the one given at the NZPUG in January, just a bit updated and polished. Outline: Talk on integrating native C++ sensibly into Python for ease of use of the code base. Inheriting from C++ classes, overriding functionality, automatically generating the bindings using Py++ and SCons. Code demonstrated in the presentation can be found here: http://www.kloss-familie.de/moin/TalksPresentations

Transcript of Thinking Hybrid - Python/C++ Integration

Page 1: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Python where we can,C++ where we must

Source:http://xkcd.com/413/

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 1/33

Page 2: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Thinking Hybrid –Python/C++ Integration

(Python where we can, C++ where we must∗)

Guy K. Kloss

Computer ScienceMassey University, Albany

Computer Science/Information Technology SeminarAuckland, 23 April 2008

∗ Quote: Alex Martelli, Senior Google DeveloperGuy K. Kloss | Thinking Hybrid – Python/C++ Integration 2/33

Page 3: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Source:http://xkcd.com/413/

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 3/33

Page 4: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Outline

1 Python

2 Extending/Integrating

3 A Real World Example

4 Tips

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 4/33

Page 5: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Outline

1 Python

2 Extending/Integrating

3 A Real World Example

4 Tips

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 5/33

Page 6: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Why Python?

Python C/C++• dynamic • static• 50% less code, 300% pro-ductivity

• high performance

• extendable with C/C++ • extendable with Python• platform independent • close to hardware• garbage collector • small memory footprint• huge standard library• many other bindings

While others are still debugging, you can publish your results!

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 6/33

Page 7: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Why Python?

Source: http://xkcd.com/371/

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 7/33

Page 8: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

What is Python used for?

System UtilitiesSystem admin tools, portable shell scriptsInternet ScriptingCGI scripts, parse HTML, process XML, email toolsUser Interfaces (UIs) & rapid prototypingComponent GlueScripting for apps, COM scriptingDistributed ProgrammingWeb Services, COM, CORBA, XML–RPCDatabase ProgrammingScientific ComputingPyODE, NumPy, SciPy, PyMol, . . .Image ProcessingPython Image Library, OpenCVOpenGL Programming, Writing GamesPyOpenGL, Panda3D, PyOgre, Py3d, VisualPythonArtifical Intelligence

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 8/33

Page 9: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Outline

1 Python

2 Extending/Integrating

3 A Real World Example

4 Tips

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 9/33

Page 10: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Extending/Integrating

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 10/33

Page 11: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

What if I could . . .

Use this code more effectively . . . ?

[NaSt2D demonstration (native executable)]

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 11/33

Page 12: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Extending/Integration

The Contestants:The “classic way” . . .Extending and Embedding the Python InterpreterThe “new way” . . .Python ctypes

SWIGBoost.PythonOthers:

SIPPythonizerSILOON(Pyrex)

Extensions also for Java, C#, Fortran, . . . availableGuy K. Kloss | Thinking Hybrid – Python/C++ Integration 12/33

Page 13: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Extending/Integration

The Contestants:The “classic way” . . .Extending and Embedding the Python InterpreterThe “new way” . . .Python ctypes

SWIGBoost.PythonOthers:

SIPPythonizerSILOON(Pyrex)

Extensions also for Java, C#, Fortran, . . . availableGuy K. Kloss | Thinking Hybrid – Python/C++ Integration 12/33

Page 14: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Extending/Integration

The Contestants:The “classic way” . . .Extending and Embedding the Python InterpreterThe “new way” . . .Python ctypes

SWIGBoost.PythonOthers:

SIPPythonizerSILOON(Pyrex)

Extensions also for Java, C#, Fortran, . . . availableGuy K. Kloss | Thinking Hybrid – Python/C++ Integration 12/33

Page 15: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Extending/Integration

The Contestants:The “classic way” . . .Extending and Embedding the Python InterpreterThe “new way” . . .Python ctypes

SWIGBoost.PythonOthers:

SIPPythonizerSILOON(Pyrex)

Extensions also for Java, C#, Fortran, . . . availableGuy K. Kloss | Thinking Hybrid – Python/C++ Integration 12/33

Page 16: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Extending/Integration

The Contestants:The “classic way” . . .Extending and Embedding the Python InterpreterThe “new way” . . .Python ctypes

SWIGBoost.PythonOthers:

SIPPythonizerSILOON(Pyrex)

Extensions also for Java, C#, Fortran, . . . availableGuy K. Kloss | Thinking Hybrid – Python/C++ Integration 12/33

Page 17: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Boost.Python

Thinking Hybrid with Boost.PythonBottom up and . . .Top down possibleDevelop quicklyResolve bottle necksDonald Knuth’s:“Premature optimisation is the root of all evil.”or: Don’t Optimise Now!

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 13/33

Page 18: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Boost.Python

Thinking Hybrid with Boost.PythonBottom up and . . .Top down possibleDevelop quicklyResolve bottle necksDonald Knuth’s:“Premature optimisation is the root of all evil.”or: Don’t Optimise Now!

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 13/33

Page 19: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Boost.Python

Thinking Hybrid with Boost.PythonBottom up and . . .Top down possibleDevelop quicklyResolve bottle necksDonald Knuth’s:“Premature optimisation is the root of all evil.”or: Don’t Optimise Now!

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 13/33

Page 20: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Elegantly withBoost.Python

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 14/33

Page 21: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Hello World

char const* greet(unsigned x) {static char const* const msgs[] = {"hello", "Boost.Python", "world!"};if (x > 2) {

throw std::range error("greet: Index out of range.");}return msgs[x];

}

#include <boost/python.hpp>using namespace boost::python;BOOST PYTHON MODULE(hello){

def("greet", greet, "return one of 3 parts of a greeting");}

And here it is in action:>>> import hello>>> for x in range(3):... print hello.greet(x)...helloBoost.Pythonworld!

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 15/33

Page 22: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Boost.Python

One of a few libraries that make it easyto integrate C++ and Python codeHow does it pull off this trick?Template meta–programming(i. e. Don’t ask!)

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 16/33

Page 23: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Boost.Python

One of a few libraries that make it easyto integrate C++ and Python codeHow does it pull off this trick?Template meta–programming(i. e. Don’t ask!)

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 16/33

Page 24: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

See it Happen

I’m making it work for you now . . .

[“MyClass” demonstration (MyClass.cpp, MyClass.h, mymodule.cpp)]

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 17/33

Page 25: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Outline

1 Python

2 Extending/Integrating

3 A Real World Example

4 Tips

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 18/33

Page 26: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Real–WorldExamples

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 19/33

Page 27: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

A Real World ExampleRe-visiting NaSt2D

Wrapping NaSt2DControl the code

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 20/33

Page 28: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

NaSt2D in Python

This is what I’m going to show you:Code to be wrappedGenerated wrapper codeGenerator scriptSCons (build system)How it works with Python

[Wrapped NaSt2D demonstration (Wrapper.h, nast2dmodule.cpp,

generate bindings.py, SConstruct, demo0.py)]

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 21/33

Page 29: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Extend Wrapper Class

Inheriting from C++ classesInterfacing numerical valuesChange functionalityFollow the Computation

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 22/33

Page 30: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Overriding in Python

This is what I’m going to show you:Overriding a native method in PythonNative method needs to be “virtual”Live data plotting with GNUplot

[NaSt2D with plotting demonstration (demo1.py, demo2.py)]

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 23/33

Page 31: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Do more Computations

Parameter StudyChange input fileCompute several casesPlot results automatically

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 24/33

Page 32: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Automating in Python

This is what I’m going to show you:Using a template input fileBatch–calculating several runsPlotting results with GNUplot

[NaSt2D with parameter variation demonstration (demo3.py, demo4.py)]

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 25/33

Page 33: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Outline

1 Python

2 Extending/Integrating

3 A Real World Example

4 Tips

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 26/33

Page 34: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Some Tips . . .Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 27/33

Page 35: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Tips

To override C++ methods: make them virtualC/C++ pit fall

Call by reference/valueSolution: calling policies

Map to “other/sane” languagesJava: JythonC#: IronPythonFortran: PyFort, Py2F(Native to other: SWIG)

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 28/33

Page 36: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Source:http://xkcd.com/353/

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 29/33

Page 37: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Summary

Why is Python good for you?How can performance bottle necks be resolved?Advantages of “Thinking Hybrid”Python–native wrapping using Boost.PythonAutomated wrapper generationSCons build system

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 30/33

Page 38: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Python and the “Need for Speed”

Cuong Do – Software Architect YouTube.com“Python is fast enough for our site

and allows us to produce maintainable featuresin record times,

with a minimum of developers.”

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 31/33

Page 39: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Some Links

Boost.Pythonhttp://www.boost.org/libs/python/doc/

Py++http://www.language-binding.net/pyplusplus/pyplusplus.html

SConshttp://www.scons.org/

NaSt2Dftp://ftp.lrz-muenchen.de/pub/science/fluiddynamics/cfd/NaSt2Dhttp://people.scs.fsu.edu/~burkardt/cpp_src/nast2d/nast2d.htmlhttp://home.arcor.de/drklaus.bauerfeind/nast/eNaSt2D.html

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 32/33

Page 40: Thinking Hybrid - Python/C++ Integration

Python Extending/Integrating A Real World Example Tips Summary

Questions?

[email protected] and code available here:http://www.kloss-familie.de/moin/TalksPresentations

Guy K. Kloss | Thinking Hybrid – Python/C++ Integration 33/33