Coder Social home page Coder Social logo

npcuda-example's Introduction

npcuda-example

This is an example of a simple Python C++ extension which uses CUDA and is compiled via nvcc. The idea is to use this coda as an example or template from which to build your own CUDA-accelerated Python extensions.

The extension is a single C++ class which manages the GPU memory and provides methods to call operations on the GPU data. This C++ class is wrapped via swig or cython -- effectively exporting this class into python land.

swig vs cython

swig

Swig is a widely used code generator for exposing C and C++ libraries in high level dynamically typed languages. In principle, it involves minimal code rewriting. You just have to write swig interface files that instruct swig on how to do the translation.

In practice, swig and numpy don't work together that well. The numpy interface relies on a bunch of magical macros that are extremely difficult to debug.

!!cython!!

Cython is sweet. It's basically python, with optional static type declarations and the ability to call c functions directly. Take a look at wrapper.pyx. It looks like python, but it gets translated into C, and then compiled into a shared object file which you import from python (look at the test.py file)

Cython is the way to go.

difference from PyCUDA

The point of this project is not to enable you to access the CUDA API in python, to write cuda code in strings and have them be dynamically compiled, or anything like that.

Instead, the goal is to demonstrate some of the biolerplate and tricks needed to make a CPython extension module that uses CUDA compiled with setuptools/distutils just like your standard C exension modules.

authors

  • Robert McGibbon
  • Yutong Zhao

installation

Requirements:

  • python
  • python setuptools, numpy
  • nvcc. I'm using version 4.2
  • nose (for testing)
  • swig for the swig wrapping method. I've tested with version 1.3.40
  • cython for the cython wrapping method. I've tested with version 0.16

To install, cd into your directory of choice -- either swig or cython. Then, just run $ python setup.py install. To see if everything is working, run $ nosetests

Silence is golden!

npcuda-example's People

Contributors

bwohlberg avatar devmessias avatar proteneer avatar rmcgibbo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

npcuda-example's Issues

Cython setup on Windows

When I setup on Windows there was a error occurred:

' default_compiler_so = self.compiler_so'

Would you like to make test on Windows?

PIP install fails

python setup.py sdist && cd dist && ls && pip install gpuadder-0.1.tar.gz
Building wheels for collected packages: gpuadder
  Building wheel for gpuadder (setup.py) ... error
  ERROR: Failed building wheel for gpuadder

Should it be possible to create a pip package from your source code?

Why use "super" as temporary variable?

Hi Robert, very appreciated for sharing this tricky yet effective example code! But there is just one thing that I can't figure out. In cython/setup.py, line 88-89, it looks like this:

default_compiler_so = self.compiler_so
super = self._compile

Can you explain the reason why you use "super" as temporary variable to store the function _compile? Thanks a lot!

Linking with the cublas library

Hi,
first of all, thank you for this introduction to cuda with python, this repository currently helps me a lot with my master thesis!
I decided to use cython to make some cuda kernels available in a python module. My code relies on the cublas library and I know that I have to link my code with the cublas library. However, I'm struggling to do this with a setup.py file.
Could you give me a hint how to change the setup.py file to do so?

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.