Code Monkey home page Code Monkey logo

capi_example's Introduction

Cython
======

* pyx
  - in the head
```python
#cython: boundscheck=False
#cython: wraparound=False
#cython: overflowcheck.fold=False
```
  - memoryview vs numpy.ndarray

* compiling. see example setup_cythonenergy.py

* c++ binding
  - for pyx
    cimport libcpp
    vector[T]
  - setup.py
    Extension(language='c++')

* interface layer for external libraries


ctypes
======

* passing pointer (with numpy array)
  ctypes.byref
  numpy.ndarray.ctypes.data_as(ctypes.POINTER(ctypes.c_double))

* call by symbol

* passing function pointer
  - callback function

* defined struct
```python
class RGB(ctypes.Structure):
    _field_ = [('r', ctypes.c_int),
               ('g', ctypes.c_int),
               ('b', ctypes.c_int),]
```

cons
1. calling overhead 
2. python object lifetime. Two examples of memory leak
  - nparray.copy().ctypes
  - def f(alist): return numpy.array(alist).ctypes
3. nparray data contiguous
  - use function numpy.ascontiguousarray
4. str in python3 is unicode string object. A python single char string
  cannot be used with ctypes.c_char. see
  https://docs.python.org/2/library/ctypes.html
5. The dynamic loader may mess up the libraries if two dynamic libraries have
  the same name and one was already loaded in memory


f2py
====

F2py is a tool of numpy.

* callback function

others
======
* Swig
* numba (llvm required)
* weave (out of date, but can be an example of python-cpp interface code)

capi_example's People

Contributors

sunqm avatar

Watchers

 avatar  avatar  avatar

Forkers

jjren zhcui pyscf

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.