Recommended Posts

A few nights ago I discovered my Python Matrix class was a significant bottleneck in my game. I decided to finally dip my hand back in C++ after a two-year hiatus and learn about writing Python extensions. I started looking at Boost Python on Tuesday night, and have spent quite a few hours trying to get it to work, without success. Maybe I'm just forgetting my C++, but the setup process for Boost seems obscenely difficult. Here are the tutorials I've been working with to get it installed: Boost Getting Started Visual C++ Express Boost Setup Notes Here is a step-by-step scenario of what I've done, so far: 1) Downloaded boost_1_33_1.zip from SourceForge and unzipped the boost_1_33_1 folder to C:\Program Files. 2) Installed Visual C++ Express. 3) Downloaded the Platform SDK. 4) Setup Platform SDK with Visual C++ Express 5) Set PYTHON_ROOT, PYTHON_VERSION, and VC80_ROOT environment variables. 6) Built Boost using this command: bjam "-sTOOLS=vc-8_0" install I now have a C:\boost folder, which contains all the Boost header files in the include folder, and a bunch of DLL and LIB files. Here is the list of python library files: boost_python-vc80-mt-1_33_1.dll boost_python-vc80-mt-1_33_1.lib boost_python-vc80-mt.lib boost_python-vc80-mt-gd-1_33_1.dll boost_python-vc80-mt-gd-1_33_1.lib boost_python-vc80-mt-gd.lib 7) Created a new Empty Project in Visual C++ Express. 8) In Project Properties, I set the following for both Debug and Release builds... Configuration Type: "Dynamic Library (.dll)". Additional Include Directories: "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include";"C:\Program Files\Python24\include";"C:\Boost\include\boost-1_33_1\";"C:\Program Files\boost_1_33_1" Additional Library Directories: C:\Boost\lib;"C:\Program Files\Python24\libs" Generate Debug Info: YES 9) I then created a Matrix class and added the following code for Boost Python:
#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(matrix)
{
    class_<Matrix>("Matrix")
        .def("Identity", &Matrix::Identity)
        .def("Translate", &Matrix::Translate)
        /* etc. */
    ;
}




10) Building my project yielded 27 linker errors: *** LINKER ERRORS REMOVED TO REDUCE CRAZY WIDTH OF THREAD *** How can I resolve these errors? The path to the Boost library files is set, so my only guess is that it isn't looking for one of the boost_python_X files that exist there. Any thoughts? [Edited by - doctorsixstring on May 18, 2006 8:39:49 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
In the Project Configuration you have to explicitly add the name of each .lib you want the project to link to.
Actually, if you add boost's location to your library path, the boost header files will automatically add the libraries using #pragma for many compilers (not sure about VS8, but definitely works for VS7 and VS7.1).

Share this post


Link to post
Share on other sites
Quote:
Original post by Extrarius
Actually, if you add boost's location to your library path, the boost header files will automatically add the libraries using #pragma for many compilers (not sure about VS8, but definitely works for VS7 and VS7.1).


It also works in VS8, at least for Boost.filesystem and boost.thread.

Share this post


Link to post
Share on other sites
Quote:
Original post by doctorsixstring
A few nights ago I discovered my Python Matrix class was a significant bottleneck in my game. I decided to finally dip my hand back in C++ after a two-year hiatus and learn about writing Python extensions. I started looking at Boost Python on Tuesday night, and have spent quite a few hours trying to get it to work, without success.


Have you already looked at using Numpy for your matrix operations? Surely it'd be easier than C++, Boost Python or no. For that matter, depending upon the reasons for the bottleneck, you might even be able to get there by just recoding some of your Python in Pyrex.

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
It also works in VS8, at least for Boost.filesystem and boost.thread.


Assuming that this is also true for Boost.Python, what else might be the problem?

Quote:
Original post by sindisil
Have you already looked at using Numpy for your matrix operations? Surely it'd be easier than C++, Boost Python or no. For that matter, depending upon the reasons for the bottleneck, you might even be able to get there by just recoding some of your Python in Pyrex.


PyRex looks pretty sweet, too. I'll give it and NumPy a try tonight.

I'd still like to figure out how to extend Python with C++, since I may want to write other portions of my game in C++, like my classes that make low-level PyOpenGL calls. I still plan on writing everything in Python first, then re-writing the bottlenecks in C++. I don't want to optimize too early, but debugging my game will be a little difficult when it is running at 4 FPS.

Thanks for the replies so far.

- Mike

Share this post


Link to post
Share on other sites
Quote:
Original post by doctorsixstring
PyRex looks pretty sweet, too. I'll give it and NumPy a try tonight.

I'd still like to figure out how to extend Python with C++, since I may want to write other portions of my game in C++, like my classes that make low-level PyOpenGL calls. I still plan on writing everything in Python first, then re-writing the bottlenecks in C++. I don't want to optimize too early, but debugging my game will be a little difficult when it is running at 4 FPS.

You're very wise to put off optimization. Two additional points, though.

First, the whole point of Pyrex is to avoid the need to do the "context switch" into a low-level language, since it is mostly a subset of Python. The idea being, first code in Python. Then, if it's too slow, profile. If you can't figure out a way to remove the hotspot in Python, preferably by using a better algorithm, you can fall back to coding said hotspot in Pyrex incrementally. Intially, the Pyrex code will be little different from the Python code -- just a few cdefs added. You can then incremetally change the Pyrex to be more and more C like until it is fast enough.

Secondly, if you really feel you must use a lower level language, might I suggest an alternative to C++? Instead, drop all the way back to C and use ctypes (which, incidentally, is going to be included in Python 2.5)? ctypes can be used to easily call C libraries from Python, as well. No shim layer necessary.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
In the Project Configuration you have to explicitly add the name of each .lib you want the project to link to.


Adding the Boost.Python LIBs to my project did the trick. After a few more hours of messing around, I was able to create a Python module DLL with my new C++ class.

That being said, I still plan on taking a look at Pyrex and ctypes. It would be trivial to learn C or Pyrex, since I already know C++ and Python, but I'm not sure if there would be a significant benefit over Boost. I haven't looked at building C++ modules for Unix-based systems, so that could be an issue. With Pyrex or ctypes, I would basically be writing pure Python code, without the need to compile a DLL, right? What other benefits do Pyrex or ctypes have over Boost (and each other)?

- Mike

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this