Cython 0.16 C++ Demo - Gists · GitHub

Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@npinto npinto/.gitignore Created August 1, 2012 23:40 Show Gist options
  • Star (0) You must be signed in to star a gist
  • Fork (0) You must be signed in to fork a gist
  • Embed Select an option
    • Embed Embed this gist in your website.
    • Share Copy sharable link for this gist.
    • Clone via HTTPS Clone using the web URL.

    No results found

    Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/npinto/3231603.js"></script>
  • Save npinto/3231603 to your computer and use it in GitHub Desktop.
Code Revisions 3 Embed Select an option
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/npinto/3231603.js"></script> Save npinto/3231603 to your computer and use it in GitHub Desktop. Download ZIP Cython 0.16 C++ Demo Raw .gitignore This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
*.so
_demo.cpp
build
Raw _demo.pyx This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
# distutils: language = c++
# distutils: sources = demo.cpp
cdef extern from "demo.h" namespace "demo":
cdef int test(float*, int)
import numpy as np
cimport numpy as cnp
def func(cnp.ndarray[cnp.float32_t, ndim=1] arr,
size=1,
):
assert arr.dtype == 'float32'
ptr = arr.data
return test(<float*>arr.data, <Py_ssize_t>size)
Raw demo.cpp This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
#include <iostream>
#include "demo.h"
using namespace std;
int demo::test(float* arr, int size)
{
int i;
for(i=0; i < size; ++i)
cout << i << ":" << ((float*)arr)[i] << endl;
return i;
}
Raw demo.h This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
namespace demo {
int test(float* arr, int size);
}
Raw setup.py This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np
setup(
ext_modules=[
Extension("_demo",
sources=["_demo.pyx", "demo.cpp"],
language="c++",
include_dirs=[np.get_include()],
),
],
cmdclass={'build_ext': build_ext}
)
import _demo
arr = np.random.randn(10).astype('f')
print _demo.func(arr, size=len(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Từ khóa » Cython C++ Github