Skip to content

Instantly share code, notes, and snippets.

@thesamovar
Created January 31, 2020 18:01
Show Gist options
  • Save thesamovar/750d8b668470af27a41b4bab059513b9 to your computer and use it in GitHub Desktop.
Save thesamovar/750d8b668470af27a41b4bab059513b9 to your computer and use it in GitHub Desktop.
wrapping_with_cython.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import os\nif not os.path.exists('testcython'):\n os.mkdir('testcython')",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "%%writefile testcython/testcython.h\n\n#ifndef TESTCYTHON_H\n#define TESTCYTHON_H\n\ndouble sumit(double* x, int n);\n\n#endif",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "%%writefile testcython/testcython.cpp\n\n#include \"testcython.h\"\n\ndouble sumit(double* x, int n)\n{\n double s = 0.0;\n for(int i=0; i<n; i++)\n {\n s += x[i];\n }\n return s;\n}",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "%%writefile testcython/testcython.pyx\n\n# distutils: sources = testcython.cpp\n# distutils: language = c++\n# cython: language_level=3\n\ncimport numpy as np\nnp.import_array()\n\ncdef extern from \"testcython.cpp\":\n double sumit(double* x, int n)\n \ndef sumit_func(np.ndarray[double, ndim=1, mode=\"c\"] x not None):\n return sumit(<double*> np.PyArray_DATA(x), x.shape[0])",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "%%writefile testcython/setup.py\n\nfrom distutils.core import setup, Extension\nimport numpy\nfrom Cython.Distutils import build_ext\nfrom Cython.Build import cythonize\n\nsetup(\n cmdclass={'build_ext': build_ext},\n ext_modules=[Extension(\"_testcython\",\n sources=[\"testcython.pyx\"],\n include_dirs=[numpy.get_include()])],\n)",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "!! cd testcython && python setup.py build_ext -i",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "['running build_ext',\n 'cythoning testcython.pyx to testcython.c',\n \"building '_testcython' extension\",\n 'C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2017\\\\BuildTools\\\\VC\\\\Tools\\\\MSVC\\\\14.14.26428\\\\bin\\\\HostX86\\\\x64\\\\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\\\\Anaconda3\\\\envs\\\\pybind11\\\\lib\\\\site-packages\\\\numpy\\\\core\\\\include -IC:\\\\Anaconda3\\\\envs\\\\pybind11\\\\include -IC:\\\\Anaconda3\\\\envs\\\\pybind11\\\\include \"-IC:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2017\\\\BuildTools\\\\VC\\\\Tools\\\\MSVC\\\\14.14.26428\\\\include\" \"-IC:\\\\Program Files (x86)\\\\Windows Kits\\\\10\\\\include\\\\10.0.17134.0\\\\ucrt\" \"-IC:\\\\Program Files (x86)\\\\Windows Kits\\\\10\\\\include\\\\10.0.17134.0\\\\shared\" \"-IC:\\\\Program Files (x86)\\\\Windows Kits\\\\10\\\\include\\\\10.0.17134.0\\\\um\" \"-IC:\\\\Program Files (x86)\\\\Windows Kits\\\\10\\\\include\\\\10.0.17134.0\\\\winrt\" \"-IC:\\\\Program Files (x86)\\\\Windows Kits\\\\10\\\\include\\\\10.0.17134.0\\\\cppwinrt\" /Tctestcython.c /Fobuild\\\\temp.win-amd64-3.8\\\\Release\\\\testcython.obj',\n 'testcython.c',\n 'c:\\\\anaconda3\\\\envs\\\\pybind11\\\\lib\\\\site-packages\\\\numpy\\\\core\\\\include\\\\numpy\\\\npy_1_7_deprecated_api.h(14) : Warning Msg: Using deprecated NumPy API, disable it with #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION',\n \"testcython.c(1726): warning C4244: 'function': conversion from 'npy_intp' to 'int', possible loss of data\",\n 'C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2017\\\\BuildTools\\\\VC\\\\Tools\\\\MSVC\\\\14.14.26428\\\\bin\\\\HostX86\\\\x64\\\\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\\\\Anaconda3\\\\envs\\\\pybind11\\\\libs /LIBPATH:C:\\\\Anaconda3\\\\envs\\\\pybind11\\\\PCbuild\\\\amd64 \"/LIBPATH:C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2017\\\\BuildTools\\\\VC\\\\Tools\\\\MSVC\\\\14.14.26428\\\\lib\\\\x64\" \"/LIBPATH:C:\\\\Program Files (x86)\\\\Windows Kits\\\\10\\\\lib\\\\10.0.17134.0\\\\ucrt\\\\x64\" \"/LIBPATH:C:\\\\Program Files (x86)\\\\Windows Kits\\\\10\\\\lib\\\\10.0.17134.0\\\\um\\\\x64\" /EXPORT:PyInit__testcython build\\\\temp.win-amd64-3.8\\\\Release\\\\testcython.obj /OUT:E:\\\\Dan\\\\Dropbox\\\\Programming\\\\IPythonNotebooks\\\\trying_pybind11\\\\testcython\\\\_testcython.cp38-win_amd64.pyd /IMPLIB:build\\\\temp.win-amd64-3.8\\\\Release\\\\_testcython.cp38-win_amd64.lib',\n ' Creating library build\\\\temp.win-amd64-3.8\\\\Release\\\\_testcython.cp38-win_amd64.lib and object build\\\\temp.win-amd64-3.8\\\\Release\\\\_testcython.cp38-win_amd64.exp',\n 'Generating code',\n 'Finished generating code']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import testcython._testcython as T\nimport numpy\nx = numpy.random.rand(10)\nprint(T.sumit_func(x))\nprint(x.sum())",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": "4.319090966340295\n4.319090966340295\n",
"name": "stdout"
}
]
}
],
"metadata": {
"kernelspec": {
"name": "conda-env-pybind11-py",
"display_name": "Python [conda env:pybind11]",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.1",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "wrapping_with_cython.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment