2013年10月14日 星期一

llvmpy = JIT python via llvm keynotes

  • llvmpy
    • a jit python interpreter via llvm
    • a  wrapper interface between llvm c/c++ function calls and python interface
  • flow
    • wrapper c/c++ code to shared lib, such as pass, target, VM...
    • explore the shared lib to python import path
    • import it
  • example
  • original c/c++ code
    #include "dpi.h"
    
    /* c_add */
    int
    c_add(int a, int b) {
      return a+b;
    }
    
    wrapper it via c/c++ python extend
    #include <Python/Python.h>
    #include <numpy/arrayobject.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "dpi.h"
    
    /* wrapper add, ref count definition */
    static PyObject *
    dpi_add_wrapper(PyObject *self, PyObject *args)
    {
      int a, b, c;
    
      // parse arguments
      if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
        return NULL;
      }
    
      // run the actual function
      c = c_add(a, b);
    
      // build the result to a Python object.
      return Py_BuildValue("i", c);
    }
    
    /* register methods */
    static PyMethodDef DPIMethods[] =
    {
          {"dpi_add", dpi_add_wrapper, METH_VARARGS, "Calculate the sum of two integers."},
          {NULL, NULL, 0, NULL}
    };
    
    
    register to python search path
    from distutils.core import setup, Extension
    
    
    # ref : http://docs.python.org/2/extending/building.html
    # the c++ extension module
    extension_mod = Extension("dpi", ["moduledpi.c", "dpi.c"], include_dirs=['/var/root/anaconda/lib/python2.7/site-packages/numpy/core/include'],
    library_dirs=['/var/root/anaconda/lib/python2.7/site-packages/numpy/core/lib', '/var/root/anaconda/lib/python2.7/site-packages/numpy/lib'])
    
    setup(name = "dpi", ext_modules=[extension_mod])
    
    run it
    >>> python
    >>> import dpi
    >>> dpi.dpi_add(1,2)
    

2013年10月6日 星期日

how many ways to import c/c++ to python


  • import your c/c++ to python
  • way
    • cython
    • pyobject python c api (python extension with C)
    • swing
    • ctypes
  • ref
    • http://realmike.org/blog/2012/07/05/supercharging-c-code-with-embedded-python/
    • https://intermediate-and-advanced-software-carpentry.readthedocs.org/en/latest/c++-wrapping.html
    • http://realmike.org/blog/2012/07/08/embedding-python-tutorial-part-1/
    • http://www.tutorialspoint.com/python/python_further_extensions.htm
    • https://mail.python.org/pipermail/capi-sig/2009-May/000256.html
    • http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html
    • http://dan.iel.fm/posts/python-c-extensions/
    • http://web.mit.edu/course/6/6.863/OldFiles/python/old/numpy-1.0.1/numpy/core/src/arraymethods.c
    • http://scipy-lectures.github.io/advanced/interfacing_with_c/interfacing_with_c.html#introduction