From a0e2799e8f0cdd6453b74b0cfd76f5ec1b232b6b Mon Sep 17 00:00:00 2001 From: HUANG SIZHE Date: Fri, 28 Oct 2016 20:29:59 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E7=94=A8ctypes=E5=92=8Cc=E4=BA=A4=E4=BA=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用ctypes和c交互 --- ...344\276\213\346\226\271\346\263\225.ipynb" | 2 +- .../C\346\211\251\345\261\225/.DS_Store" | Bin 0 -> 6148 bytes ...\344\272\244\344\272\222-checkpoint.ipynb" | 349 ++++++++++++++++++ .../libvec.so" | Bin 0 -> 4176 bytes .../v.c" | 37 ++ .../v.so" | Bin 0 -> 8692 bytes .../vector.c" | 13 + .../vector.h" | 12 + .../vector.so" | Bin 0 -> 8564 bytes .../vectorTest" | Bin 0 -> 8480 bytes .../vectorTest.c" | 16 + .../vtest" | Bin 0 -> 8756 bytes ...45\222\214c\344\272\244\344\272\222.ipynb" | 349 ++++++++++++++++++ 13 files changed, 777 insertions(+), 1 deletion(-) create mode 100644 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/.DS_Store" create mode 100644 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222-checkpoint.ipynb" create mode 100755 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/libvec.so" create mode 100644 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/v.c" create mode 100755 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/v.so" create mode 100644 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.c" create mode 100644 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.h" create mode 100755 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.so" create mode 100755 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vectorTest" create mode 100644 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vectorTest.c" create mode 100755 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vtest" create mode 100644 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222.ipynb" diff --git "a/Python_Advance/\351\235\242\345\220\221\345\257\271\350\261\241\347\274\226\347\250\213/\351\235\231\346\200\201\346\226\271\346\263\225,\347\261\273\346\226\271\346\263\225\345\222\214\345\256\236\344\276\213\346\226\271\346\263\225.ipynb" "b/Python_Advance/\351\235\242\345\220\221\345\257\271\350\261\241\347\274\226\347\250\213/\351\235\231\346\200\201\346\226\271\346\263\225,\347\261\273\346\226\271\346\263\225\345\222\214\345\256\236\344\276\213\346\226\271\346\263\225.ipynb" index 4e87d18..b6859c0 100755 --- "a/Python_Advance/\351\235\242\345\220\221\345\257\271\350\261\241\347\274\226\347\250\213/\351\235\231\346\200\201\346\226\271\346\263\225,\347\261\273\346\226\271\346\263\225\345\222\214\345\256\236\344\276\213\346\226\271\346\263\225.ipynb" +++ "b/Python_Advance/\351\235\242\345\220\221\345\257\271\350\261\241\347\274\226\347\250\213/\351\235\231\346\200\201\346\226\271\346\263\225,\347\261\273\346\226\271\346\263\225\345\222\214\345\256\236\344\276\213\346\226\271\346\263\225.ipynb" @@ -575,7 +575,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.5.1" } }, "nbformat": 4, diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/.DS_Store" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/.DS_Store" new file mode 100644 index 0000000000000000000000000000000000000000..750ca99ecc11fcb8631094c929dd9d52b9adb254 GIT binary patch literal 6148 zcmeHKJ5B>J5S@V(k5tHuGp%W)hfUI-0bW6MIXS!Uyj!g6y zyJYXP?wo4c(Q@5(>QY`)2C?+;e*Z6w@K zAQT7%LV-}=#|q%i7MsotqYec^fl%N}0XZKMiePpuhI(|M(i8wF&uBIHl1oTTa?Fm! z5F;?QP@sjfrxXWzi9PvXU->h6VO<^bC#4f-!>B`nP++LQsWs(tj2af^-DDVpkd;r%FH@W}- literal 0 HcmV?d00001 diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222-checkpoint.ipynb" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222-checkpoint.ipynb" new file mode 100644 index 0000000..6b670ec --- /dev/null +++ "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222-checkpoint.ipynb" @@ -0,0 +1,349 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 用ctypes和c交互" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[ctypes](https://docs.python.org/2/library/ctypes.html)是原版python和pypy都支持的一种与c交互的方式,他的原理其实是利用python调用c/c++的动态链接库(就是.so或者.dll那个文件),也就是说c还是那个c,python还是那个python,不用再学其他的了,这也是最推荐的一种与c直接交互的方式." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> 例子:一个二维向量运算的定义" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### c代码:\n", + "\n", + "v.c\n", + "\n", + "```c\n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "//向量结构体\n", + "typedef struct{\n", + " float x,y;\n", + " char repr[50];\n", + "}Vector;\n", + "\n", + "Vector add(Vector a,Vector b){\n", + " Vector c;\n", + " c.x = a.x+b.x;\n", + " c.y = a.y+b.y;\n", + " char tempx[10];\n", + " char tempy[10];\n", + " //char x = itoa(c.x,&tempx,10);\n", + " //char y = itoa(c.y,&tempy,10);\n", + " int len = sprintf(c.repr, \"\\n\",c.x,c.y);\n", + " //c.repr = \"\";\n", + " return c;\n", + "}\n", + "\n", + "int main(void){\n", + " Vector a,b,c;\n", + " a.x = 10.0;\n", + " a.y = 20.0;\n", + " strcpy(a.repr,\"\");\n", + "\n", + " b.x = 1.0;\n", + " b.y = 2.0;\n", + " strcpy(b.repr,\"\");\n", + " c = add(a,b);\n", + " printf(\"x %f y %f\\n\",c.x,c.y);\n", + " printf(\"%s\\n\",c.repr);\n", + " return 0;\n", + "}\n", + "```\n", + "\n", + "编译运行下:\n", + "\n", + "```bash\n", + "gcc v.c -o vtest\n", + "./vtest\n", + "```\n", + "\n", + "结果正常" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 修改成动态链接库(把哪个显示结果的省了)\n", + "\n", + "vector.h\n", + "\n", + "```c\n", + "#ifndef VECTOR_HEAD_\n", + "#define VECTOR_HEAD_\n", + "\n", + "//向量结构体\n", + "typedef struct{\n", + " float x,y;\n", + "}Vector;\n", + "\n", + "//向量加法\n", + "Vector add(Vector a,Vector b);\n", + "\n", + "#endif\n", + "```\n", + "\n", + "vector.c\n", + "\n", + "```c\n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "#include \"vector.h\"\n", + "\n", + "//向量结构体\n", + "Vector add(Vector a,Vector b){\n", + " Vector c;\n", + " c.x = a.x+b.x;\n", + " c.y = a.y+b.y;\n", + " return c;\n", + "}\n", + "```\n", + "编译成动态链接库\n", + "\n", + "```bash\n", + "gcc -shared -fPIC -o libvec.so vector.c\n", + "```\n", + "\n", + "这样就生成一个v.so文件,我们先来用c语言试试看调用\n", + "\n", + "vectorTest.c\n", + "\n", + "```c\n", + "#include \n", + "#include \n", + "\n", + "#include \"vector.h\"\n", + "\n", + "int main(void){\n", + " Vector a,b,c;\n", + " a.x = 10.0;\n", + " a.y = 20.0;\n", + "\n", + " b.x = 1.0;\n", + " b.y = 2.0;\n", + " c = add(a,b);\n", + " printf(\"x %f y %f\\n\",c.x,c.y);\n", + " return 0;\n", + "}\n", + "```\n", + "\n", + "编译:\n", + "\n", + "```bash\n", + "gcc vectorTest.c -o vectorTest -L ./ -lvec\n", + "./vectorTest\n", + "```\n", + "\n", + "结果正常" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 用ctypes调用这个动态链接库\n", + "\n", + "到正题了,我们用这个ctypes调用下动态库试试" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from ctypes import CDLL,c_float,c_char_p,Structure,POINTER" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Vector(Structure):\n", + " _fields_ = [(\"x\",c_float),(\"y\",c_float)]\n", + " def __str__(self):\n", + " return \"Vector:<{this.x},{this.y}>\".format(this=self)\n", + " def __repr__(self):\n", + " return self.__str__()\n", + " def __add__(self,that):\n", + " dll = CDLL(\"libvec.so\")\n", + " dll.add.restype = Vector\n", + " return dll.add(self,that)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "v1 = Vector(1,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Vector:<1.0,2.0>" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v1" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "v2 = Vector(10,20)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Vector:<10.0,20.0>" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v2" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Vector:<11.0,22.0>" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v1+v2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ctypes内置数据类型映射:\n", + "\n", + "\n", + "ctypes type|c type|Python type\n", + "---|---|---\n", + "c_char|char\t|1-character string\n", + "c_wchar\t|wchar_t\t|1-character unicode string\n", + "c_byte\t|char\t|int/long\n", + "c_ubyte\t|unsigned char\t|int/long\n", + "c_short\t|short\t|int/long\n", + "c_ushort\t|unsigned short\t|int/long\n", + "c_int\t|int\t|int/long\n", + "c_uint\t|unsigned int\t|int/long\n", + "c_long\t|long\t|int/long\n", + "c_ulong\t|unsigned long\t|int/long\n", + "c_longlong\t|__int64 or long long\t|int/long\n", + "c_ulonglong\t|unsigned __int64 or unsigned long long\t|int/long\n", + "c_float\t|float\t|float\n", + "c_double\t|double\t|float\n", + "c_char_p\t|char * (NUL terminated)\t|string or None\n", + "c_wchar_p\t|wchar_t * |(NUL terminated)\t|unicode or None\n", + "c_void_p\t|void *|int/long or None" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.12" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/libvec.so" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/libvec.so" new file mode 100755 index 0000000000000000000000000000000000000000..32a0209050d172803ea6f4c279a31f75487be384 GIT binary patch literal 4176 zcmeHKze`(D82w&jTS+YM6_o)FNqn53F5P#i)L z;=dv;ZY}N9#nmx`f`f$=9n_)poVb9nvx?|l&`B_j8L zA)s+bWLrE$A|t>lpwyCISYoBm;)UOs+BiO9f8M^H7wApQ&0UgnOw4z(SE)Dog*pFq-n`Ykug@>)jeMu0 zW4Z^x6CevD>uaUr=jcPO!n@KQf#&CNc=L*Lc{K0NnOP)^r{S4rfJGo_Jl7yuzfC-f zCeGb$92AbOUu=E(GxGCl=H1RDYP^SJ(p@|=IcqTfZ>9n6<^7!WFZ_ItOw}qIQ+Pb_ zwqB`5>$%xnSVx8?*kgcmJuHLpPj$|BTC)sT1}p=X0n318z%pPNunbrRECc^D1It14 z#2xlK?(qESx$F5IEQ|`@&)tW9o23hu&P?ez`0BTVrhmB_xaDsvKU%F8wcVH|A?c~R zsGDEH6MI?%Gp4>Df*$jFsx+3Tj(Q)4GMJLiI9l`?Yd9AjMwe@y8}C8igmm{o$%U#? Mtrb-fVm#WAe+R;3<^TWy literal 0 HcmV?d00001 diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/v.c" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/v.c" new file mode 100644 index 0000000..22c8824 --- /dev/null +++ "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/v.c" @@ -0,0 +1,37 @@ +#include +#include +#include + +//向量结构体 +typedef struct{ + float x,y; + char repr[50]; +}Vector; + +Vector add(Vector a,Vector b){ + Vector c; + c.x = a.x+b.x; + c.y = a.y+b.y; + char tempx[10]; + char tempy[10]; + //char x = itoa(c.x,&tempx,10); + //char y = itoa(c.y,&tempy,10); + int len = sprintf(c.repr, "\n",c.x,c.y); + //c.repr = ""; + return c; +} + +int main(void){ + Vector a,b,c; + a.x = 10.0; + a.y = 20.0; + strcpy(a.repr,""); + + b.x = 1.0; + b.y = 2.0; + strcpy(b.repr,""); + c = add(a,b); + printf("x %f y %f\n",c.x,c.y); + printf("%s\n",c.repr); + return 0; +} diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/v.so" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/v.so" new file mode 100755 index 0000000000000000000000000000000000000000..18cd84391383cc68e95313572e52ba5807b2e8b6 GIT binary patch literal 8692 zcmeHNQEU`N7@jKyk3zXSVq+E2gIKKwOCg#xQfROCbckE1frh3^hHI}!8`|s9yK0+2 zQUaHBvRTs?6CR8qKJj4UJwS2~g%lx5h*Sr9G8o<^-LV@!y{(ayulwftg- zE%~$!PPP`6IH?Gc=^f43ZQEYXSHIEj`w91=LN?M^D&=f^$soRTW~4j4=yK<`DPVU! zrz6PrEk-lu*L+L)5ufPm9Z2;KBhJmYL-Vz04zhKswr53tJwwU#(uCc7Db44|NA@zl zl3b}NL{B<1+@CzI{cgT-&9_7MJGM4ga0@Y#{IEY67yZe^km6sy9-nKzc1=L`YWdMV zX@I~&v_=j`oVx;@2|fmCpU~c;N=1m{LrbbXp3iTGD}HAzxoACU-#JBy5N z7)~lqVqs;m2YM3|Pw9T9&n<;iyZj&2rD=KDcE-l*sS&pC?*}TId?*cJ#*P8c1M#EQ znz*K6tOuUk##jot0ks{qw9vkwN;y5Qv|~Kw=q_8}p!;n8^t1QBS!nt_d%NnRa|dp8 zz5rj4f;UvC`if(d4~4!i8c=C0-P>1j3ttyLwsRysymO$xo9dylbf$N(uB9$M1`9zb zFQO7}K5XmovbJfSUhx$06z~-A6z~-A6nL}(9bEo$h@bgm0^M9LaI3AITOGk&e@F7W z;fl&@hE??vo_GxFXpWm(KS7h5Tev(rHy;R=%?CPt^MOz_2cxANRs@$&^wR#b*ZuQ> zF7kJ^UE=ceq#^Ga@|GdL<1#vtD=WnCTb`Sh2KcTv;!g4}Zk^7_=y}6zo5X-|L(Xt> zWSmQDv{)<_-c<3b&CL+s(=ivwZla{k=mbA=gYtjx==DMnVTnYdF7W(0co?JkVS0Cb zh_qfmfnGH)`6XtWN78GF7g*V(`1ejJXhH%DUuPdYEEb6bQ=;Hdbc&z7=I3&z@P*B{ z1<|%pt;DFZBhvfmCeoi3hO|GFJ%yQQ3NEt+&=-0YleK_0gwJ#9GEKMDjOMufwN<`7 z3Oz0w@?Z*h0oVsT4?IRX0Xu;cz;?q_w=v8PNOy$8)eM$7P&6Q<0J|PI3#@zWUZH)McYS5#UuJ4@L~>#l;2?Q?!M~N+DRAUip@q84$|_RL zT+jEkEy!#hU4bk;$?lrQR-m=UPN1j!Vof=WV$C{r^{Z=zx^BePu(tk)pva+O&28Xh zA1>^-`^c!TidsKV>s4xfPpt^GZc^(YwXUKCWULvF;!TWQKz}&WIQm9SV#gRR`zjcW zUG;VKFn0s?vC(i%B0Ppm1*=I{;OUmJXNdu&6tSt)-^;H7sRs1lZq&xd1<%T5>`C>M z%zU%3QG7o7S*0{;j<+lVKN4BGx}Z{#xxH?n#X~vGwo`_71{B5e=|xE}NhE1kQ2GV(~cJJQ(Xw zvZvOtTs2;2Og=-mM|)Pizo2&?0koX|AD}ZxvV;1tkpQNOMN_>uDYffd{SlXa&}AQS z*}`R0KJ?DT`QHc12M3eRnQ!x>`om1nJcXJkEklW@%T`6nxy)m)lX5L;>!|AJxncqT E1MwO}7ytkO literal 0 HcmV?d00001 diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.c" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.c" new file mode 100644 index 0000000..bb5c14e --- /dev/null +++ "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.c" @@ -0,0 +1,13 @@ +#include +#include +#include + +#include "vector.h" + +//向量结构体 +Vector add(Vector a,Vector b){ + Vector c; + c.x = a.x+b.x; + c.y = a.y+b.y; + return c; +} diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.h" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.h" new file mode 100644 index 0000000..f8185a6 --- /dev/null +++ "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.h" @@ -0,0 +1,12 @@ +#ifndef VECTOR_HEAD_ +#define VECTOR_HEAD_ + +//向量结构体 +typedef struct{ + float x,y; +}Vector; + +//向量加法 +Vector add(Vector a,Vector b); + +#endif diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.so" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vector.so" new file mode 100755 index 0000000000000000000000000000000000000000..0494fd9fb0324be3794218f978d182e4566d286b GIT binary patch literal 8564 zcmeHNQD|FL82)e9Y3w$WC>xcnkjXl#Xy??2u?!*W6Hz~#*(Mzn~TSQV$r)Q^CsK4=sZO1g` znzUCdI;C_9s}4L*!u2}Od-3mdKQK>YTL!7Q4N$M<)aPqkp>Vx?)a!adj{xRnY)M4d z!w>!G91-xmXF@COhiIbfE~6S%8E2sE7N`7b#2Gf1BU1t~aa` zfP?k0UjH7!5by?$rL-sCdpA8X>Hj7Y9E9k4lu(Eqebb?ic2x)VeT)^?4aTn)a!#c> zUc(+kUM6b30VhF(HAmQW6b-=QKDJ4`&ciq2E}3k4e}N2 zhyOG|^jtW|_%JK3Gx6a!qsQ)^{PAx0#Y(33@$q9EQxxnQ;)@Q^t3GB6+0TmkBNubE zhVJvv+@cfpTJ=P!I1kMlH!H5U39A`z{PQv5i6^*e@BZdLPfMLKeU>GnA z7zPXj|6c}Xg!^+w-1u#cmmu7yqCIs%v}a-`e@}h!CWq4QS9bg0Blhg}TvxP4Y<_?h zt;52dd%PKq4Qxhdt<7jW-DPlYkWq@8czS*EqkECf=taaYPOS^~ddGHuvfcZ(`%t*) zweG;i2e@GCz@KcsJj;#yM6|DW-Slm{HPzvSrtLlut$I_q?bTkdxAB%5uYA#pi!(Ei zqc`ymD_ZF_apP;u|I*yO4LpPQMn6++SGAvWi|LQJY*LxtXoGEM3Bg}~;mS(~;r#&b zb$H*hcXsvk=)!9aZrGHFkl!k3>XFs1BQYBECcbQ!x>C2o|HHO&*m=6 z+|p&aSkG4T^!}AtC&`C!d3D$>=lT4Z567Du{6#{E)uAiHUgip)bvp~lB@QM+MQl6-5sodhp;uB}#=By(nV*zR7O0Nz}VIkIawny*F>)d@~E1nU~)` z|6C~&Y7{9nh)4uwZkfn+vCtFQgc3(lswZ~D>32?@ptf2<)$q57apvHtRG)LEui8bA z=YxadVdN&uy+X56Zfe|RuPk-(!k7$9dEWsT)2YZZDO3RC<=wHwl6PHxt{1P_8{}g@ zMw1tBg{LUTQ^TpuXetL?ZGHtW-VV>7n7l7ZU4DakH1UYShjBF`gfW8 zm%oRtnt1|KEIoM<#__sZza8({UhN>#gto~N*{Ch$$^ynuY*h5)oM;~h=6moKt&NuJ z-GdSo`mzM)5wx@a#1irMYS6Y804Cu+?-GnA7zPXjhJpW= zfz$EH_wj4R@1S`#(@@@p;Rgj^ArE$8}!Dl=#Vvm3ZK{^D7VlD5^VK1G`_L-L(jth zsTB74f1nv7BYHHom5xS%Y6|1{jA^e8m`v9&U>GnA7zPXjh5^HXVZbn87%&VN1`Gp+ zfq#^NmhF*KOug0SWuLwbR|(t?8wO~FwrQ5ZJxj1p*kGWH;4MXVS~o3DrX!xhu~3!mNwJ? l`j~?X-sc5z`7uj(wziKg+$7iMrgUz4UjB^grq8E{`~qhB=)?d3 literal 0 HcmV?d00001 diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vectorTest.c" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vectorTest.c" new file mode 100644 index 0000000..7136776 --- /dev/null +++ "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vectorTest.c" @@ -0,0 +1,16 @@ +#include +#include + +#include "vector.h" + +int main(void){ + Vector a,b,c; + a.x = 10.0; + a.y = 20.0; + + b.x = 1.0; + b.y = 2.0; + c = add(a,b); + printf("x %f y %f\n",c.x,c.y); + return 0; +} diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vtest" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/vtest" new file mode 100755 index 0000000000000000000000000000000000000000..c31a8fde589cabb961787ef8b2feab10be1f1f84 GIT binary patch literal 8756 zcmeHNUuauZ7(ZFNnbmb^z)E#avtef)v(3hY;<~M=jXT3z8{LYfGS1TcSwhpaBq^q_ zLB|aA5H9pZ7~;boeHfUB75dQ4&50WWZS`pn>Wbiupg8Q1D!BOj&b_xyZnr%v+>f5~ zo$ou}`JM0l&Pj5U(}ll(d+-Eft~$o1HZaCKD4~sv6_^h##-2gpC_;n+M}kAaclya& zE3R7ep%&q^VFDqJ2M3SWhN$sPwLWIr$c-T#w#n=kA{Wc&)?)2={w-Jxx__X>K*+8) znnqpErm9ULvbl-j?5fL-w-tmC-t%UJWm^N>Cf-_pgeMZQ)Oaj|AUob36R*bvAlppU z@T|)(GM3J+E!d7XZsJ+=kzIplC2DFRBH3IfnLf2@*Y3xZiMPk>C%ang*g{OC-%qBa zBAJelA<&-RMH4S%f{<;;sK%#r(h0(a=nWhXRP|tiv=7#~uxmOeIK!$?m=LGN)>wNq zo|}S6fKTHsq5Y(DXNeH$l*mqw4v(e8crLRpKew45loGP-c-9%A03lN0$Kkb@cx?t8 zBfA=p*1MWJ+H0>j)6@cB^ia%LS9*-S?& zIouJQOhsXnY(t?unwt|h9;EkuHv8A_{r9(OAH4`4s6#@?4rRAxlMe!Zr)XT0ZBm+t z)<=D`Mpwfvyp6cl=EnTTCbPNNXnRjP@<=3Dd`{Jm`2#E&Bm0}cZY0}cZY z1OHbB2Dtp`7(e?fea3US!u7ro*9V&R{u!9t2Uk#D)AZ(-@F}n9CrZ4y{bMwFaT}K> z?k#(oHY|GvUCSPCumq#09##OAH+bpjx$Ev_&k*^C`Yv&KW=4~DG z@hQxUjT-Q-bYUj>2iMP(WbnLJ?3=-WDNQc$;=~k}dcIPrlwVizs?CdDeqi99r$7%8 zycnG3XTPBQ-#c-=+=Q@rJl^hk;RARWqxE5Vi!Ma!ua2NstxJB6mChmQjf4x`cv#^d zoLA~F5?HxWxc{J1ArLHyg1y0ce(su^%Z2i%2HrMA8$|UIqRNgy@1mPPe^lCQ`n`qI zScwV2WuXlCa!euVD`H_W&~4DSp?%Pc&@Iq6p_`!RF#amWyEWQs6ah9 z8+sQ-6jcNc1wH)%?T%8$XhrzfvM>*f5-aymSFy2~SSxPk2l`fIVGdoM0zIMby^5ni zdySJoPv@1^dKi_~O{#XQxC7P-A9kpJe*0hq6*0^sHRRe3y zHsI-#vF8W@Qmt{Q)c4%|&{RD5jzvOkdfwiGC)lUdQ!H~W!G^e8^!FI)GQYRIVd{{n zhfEzc^koW*zj) zM;$A*pqhD>(n5Pl{jNbh9+$fLQ@@)$t!(3W5(=RELJt~r???6y6liKH@uRM`du%q5 zR^1=5*&{Za{==X;o_?3Xg`)bu2J0@D?wdEGK)N5+@58O}dOig4Arv39g;g6;v!iM& WYCOgsE7$72SUY0v#k$R> 例子:一个二维向量运算的定义" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### c代码:\n", + "\n", + "v.c\n", + "\n", + "```c\n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "//向量结构体\n", + "typedef struct{\n", + " float x,y;\n", + " char repr[50];\n", + "}Vector;\n", + "\n", + "Vector add(Vector a,Vector b){\n", + " Vector c;\n", + " c.x = a.x+b.x;\n", + " c.y = a.y+b.y;\n", + " char tempx[10];\n", + " char tempy[10];\n", + " //char x = itoa(c.x,&tempx,10);\n", + " //char y = itoa(c.y,&tempy,10);\n", + " int len = sprintf(c.repr, \"\\n\",c.x,c.y);\n", + " //c.repr = \"\";\n", + " return c;\n", + "}\n", + "\n", + "int main(void){\n", + " Vector a,b,c;\n", + " a.x = 10.0;\n", + " a.y = 20.0;\n", + " strcpy(a.repr,\"\");\n", + "\n", + " b.x = 1.0;\n", + " b.y = 2.0;\n", + " strcpy(b.repr,\"\");\n", + " c = add(a,b);\n", + " printf(\"x %f y %f\\n\",c.x,c.y);\n", + " printf(\"%s\\n\",c.repr);\n", + " return 0;\n", + "}\n", + "```\n", + "\n", + "编译运行下:\n", + "\n", + "```bash\n", + "gcc v.c -o vtest\n", + "./vtest\n", + "```\n", + "\n", + "结果正常" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 修改成动态链接库(把哪个显示结果的省了)\n", + "\n", + "vector.h\n", + "\n", + "```c\n", + "#ifndef VECTOR_HEAD_\n", + "#define VECTOR_HEAD_\n", + "\n", + "//向量结构体\n", + "typedef struct{\n", + " float x,y;\n", + "}Vector;\n", + "\n", + "//向量加法\n", + "Vector add(Vector a,Vector b);\n", + "\n", + "#endif\n", + "```\n", + "\n", + "vector.c\n", + "\n", + "```c\n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "#include \"vector.h\"\n", + "\n", + "//向量结构体\n", + "Vector add(Vector a,Vector b){\n", + " Vector c;\n", + " c.x = a.x+b.x;\n", + " c.y = a.y+b.y;\n", + " return c;\n", + "}\n", + "```\n", + "编译成动态链接库\n", + "\n", + "```bash\n", + "gcc -shared -fPIC -o libvec.so vector.c\n", + "```\n", + "\n", + "这样就生成一个v.so文件,我们先来用c语言试试看调用\n", + "\n", + "vectorTest.c\n", + "\n", + "```c\n", + "#include \n", + "#include \n", + "\n", + "#include \"vector.h\"\n", + "\n", + "int main(void){\n", + " Vector a,b,c;\n", + " a.x = 10.0;\n", + " a.y = 20.0;\n", + "\n", + " b.x = 1.0;\n", + " b.y = 2.0;\n", + " c = add(a,b);\n", + " printf(\"x %f y %f\\n\",c.x,c.y);\n", + " return 0;\n", + "}\n", + "```\n", + "\n", + "编译:\n", + "\n", + "```bash\n", + "gcc vectorTest.c -o vectorTest -L ./ -lvec\n", + "./vectorTest\n", + "```\n", + "\n", + "结果正常" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 用ctypes调用这个动态链接库\n", + "\n", + "到正题了,我们用这个ctypes调用下动态库试试" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from ctypes import CDLL,c_float,c_char_p,Structure,POINTER" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Vector(Structure):\n", + " _fields_ = [(\"x\",c_float),(\"y\",c_float)]\n", + " def __str__(self):\n", + " return \"Vector:<{this.x},{this.y}>\".format(this=self)\n", + " def __repr__(self):\n", + " return self.__str__()\n", + " def __add__(self,that):\n", + " dll = CDLL(\"libvec.so\")\n", + " dll.add.restype = Vector\n", + " return dll.add(self,that)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "v1 = Vector(1,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Vector:<1.0,2.0>" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v1" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "v2 = Vector(10,20)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Vector:<10.0,20.0>" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v2" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Vector:<11.0,22.0>" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v1+v2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ctypes内置数据类型映射:\n", + "\n", + "\n", + "ctypes type|c type|Python type\n", + "---|---|---\n", + "c_char|char\t|1-character string\n", + "c_wchar\t|wchar_t\t|1-character unicode string\n", + "c_byte\t|char\t|int/long\n", + "c_ubyte\t|unsigned char\t|int/long\n", + "c_short\t|short\t|int/long\n", + "c_ushort\t|unsigned short\t|int/long\n", + "c_int\t|int\t|int/long\n", + "c_uint\t|unsigned int\t|int/long\n", + "c_long\t|long\t|int/long\n", + "c_ulong\t|unsigned long\t|int/long\n", + "c_longlong\t|__int64 or long long\t|int/long\n", + "c_ulonglong\t|unsigned __int64 or unsigned long long\t|int/long\n", + "c_float\t|float\t|float\n", + "c_double\t|double\t|float\n", + "c_char_p\t|char * (NUL terminated)\t|string or None\n", + "c_wchar_p\t|wchar_t * |(NUL terminated)\t|unicode or None\n", + "c_void_p\t|void *|int/long or None" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.12" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From 076d1a41a8953b05289a66cb0a64e4a0e7cb2158 Mon Sep 17 00:00:00 2001 From: HUANG SIZHE Date: Fri, 28 Oct 2016 21:41:30 +0800 Subject: [PATCH 2/4] update 1 --- ...\344\272\244\344\272\222-checkpoint.ipynb" | 332 +++++++++++++++++- ...345\272\223\344\272\244\344\272\222.ipynb" | 332 +++++++++++++++++- 2 files changed, 626 insertions(+), 38 deletions(-) rename "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222-checkpoint.ipynb" => "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\344\270\216\345\212\250\346\200\201\351\223\276\346\216\245\345\272\223\344\272\244\344\272\222-checkpoint.ipynb" (50%) rename "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222.ipynb" => "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\344\270\216\345\212\250\346\200\201\351\223\276\346\216\245\345\272\223\344\272\244\344\272\222.ipynb" (50%) diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222-checkpoint.ipynb" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\344\270\216\345\212\250\346\200\201\351\223\276\346\216\245\345\272\223\344\272\244\344\272\222-checkpoint.ipynb" similarity index 50% rename from "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222-checkpoint.ipynb" rename to "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\344\270\216\345\212\250\346\200\201\351\223\276\346\216\245\345\272\223\344\272\244\344\272\222-checkpoint.ipynb" index 6b670ec..11af4d0 100644 --- "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222-checkpoint.ipynb" +++ "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/.ipynb_checkpoints/\347\224\250ctypes\344\270\216\345\212\250\346\200\201\351\223\276\346\216\245\345\272\223\344\272\244\344\272\222-checkpoint.ipynb" @@ -4,21 +4,36 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 用ctypes和c交互" + "# 用types与动态链接库交互" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "[ctypes](https://docs.python.org/2/library/ctypes.html)是原版python和pypy都支持的一种与c交互的方式,他的原理其实是利用python调用c/c++的动态链接库(就是.so或者.dll那个文件),也就是说c还是那个c,python还是那个python,不用再学其他的了,这也是最推荐的一种与c直接交互的方式." + "[ctypes](https://docs.python.org/2/library/ctypes.html)是原版python和pypy都支持的一种与动态链接库(就是.so或者.dll那个文件)交互的方式,因此它也是最方便的一种与c语言混合编程的方式.c还是那个c,python还是那个python,不用再学其他的了,这也是简单情况下最推荐的一种与c直接交互的方式.\n", + "\n", + "ctypes 有以下优点:\n", + "\n", + "+ Python内建,不需要单独安装\n", + "+ 可以直接调用二进制的动态链接库 \n", + "+ 对基本类型的相互映射有良好的支持\n", + "\n", + "ctypes 有以下缺点:\n", + "\n", + "+ 平台兼容性差\n", + "+ 不能够直接调用动态链接库中未经导出的函数或变量\n", + "+ 对C++的支持差\n", + "\n", + "\n", + "**事先申明:**本文写在mac osx平台,使用的编译工具是clang,其他平台都没试过,有兴趣的小朋友可以自己摸索下" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "> 例子:一个二维向量运算的定义" + "> 例子:一个二维向量运算" ] }, { @@ -83,7 +98,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### 修改成动态链接库(把哪个显示结果的省了)\n", + "### 修改成动态链接库(把那个显示结果的省了)\n", "\n", "vector.h\n", "\n", @@ -175,12 +190,12 @@ }, "outputs": [], "source": [ - "from ctypes import CDLL,c_float,c_char_p,Structure,POINTER" + "from ctypes import CDLL,c_float,c_char_p,Structure,POINTER,c_int,pointer" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -194,13 +209,14 @@ " return self.__str__()\n", " def __add__(self,that):\n", " dll = CDLL(\"libvec.so\")\n", + " dll.add.argtypes = (Vector,Vector)\n", " dll.add.restype = Vector\n", " return dll.add(self,that)" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -211,7 +227,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -222,7 +238,7 @@ "Vector:<1.0,2.0>" ] }, - "execution_count": 11, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -233,7 +249,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -244,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -255,7 +271,7 @@ "Vector:<10.0,20.0>" ] }, - "execution_count": 13, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -266,10 +282,21 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "metadata": { "collapsed": false, - "scrolled": true + "scrolled": false + }, + "outputs": [], + "source": [ + "v3 = v1+v2" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false }, "outputs": [ { @@ -278,13 +305,20 @@ "Vector:<11.0,22.0>" ] }, - "execution_count": 14, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "v1+v2" + "v3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可喜可贺~~" ] }, { @@ -311,18 +345,278 @@ "c_float\t|float\t|float\n", "c_double\t|double\t|float\n", "c_char_p\t|char * (NUL terminated)\t|string or None\n", - "c_wchar_p\t|wchar_t * |(NUL terminated)\t|unicode or None\n", + "c_wchar_p\t|wchar_t * (NUL terminated)\t|unicode or None\n", "c_void_p\t|void *|int/long or None" ] }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "需要注意的是:\n", + "+ 关于指定映射类型,如果指定c中的返回值类型,则是`dll.FUNCNAME.restype = TYPE`,如果函数的返回值是`void`那么你可以赋值为 `None`,如果不设定,则默认是`int`类型;\n", + "+ 如果是传入参数类型指定,则使用`dll.FUNCNAME.argtypes = (TYPE1, TYPE2...)`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ctypes 和 指针" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "如何创建一个 ctypes 的指针呢?这里有三个跟指针有个的 ctypes 里的函数,\n", + "\n", + "函数\t|说明\n", + "---|---\n", + "byref(x [, offset])\t|返回 x 的地址,x 必须为 ctypes 类型的一个实例。相当于 c 的 &x 。 offset 表示偏移量。\n", + "pointer(x)\t|创建并返回一个指向 x 的指针实例, x 是一个实例对象。\n", + "POINTER(type)\t|返回一个类型,这个类型是指向 type 类型的指针类型, type 是 ctypes 的一个类型。\n", + "\n", + "\n", + "\n", + "byref 很好理解,传递参数的时候就用这个,用 pointer 创建一个指针变量也行,不过 byref 更快。\n", + "\n", + "而 pointer 和 POINTER 的区别是,pointer 返回一个实例,POINTER 返回一个类型。甚至你可以用 POINTER 来做 pointer 的工作:" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = c_int(6) # 创建一个 c_int 实例" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "c_int(6)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "b = pointer(a) # 创建指针" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.LP_c_int at 0x103d618c0>" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], - "source": [] + "source": [ + "c = POINTER(c_int)(a) # 创建指针" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.LP_c_int at 0x103d70050>" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "c_int(6)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b.contents #输出 a 的值" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "c_int(6)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c.contents # 输出 a 的值" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.LP_Vector at 0x103d704d0>" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pointer(v3)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.LP_Vector at 0x103d70560>" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "POINTER(Vector)(v3)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Vector:<11.0,22.0>" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "POINTER(Vector)(v3).contents" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "11.0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "POINTER(Vector)(v3).contents.x" + ] } ], "metadata": { diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222.ipynb" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\344\270\216\345\212\250\346\200\201\351\223\276\346\216\245\345\272\223\344\272\244\344\272\222.ipynb" similarity index 50% rename from "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222.ipynb" rename to "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\344\270\216\345\212\250\346\200\201\351\223\276\346\216\245\345\272\223\344\272\244\344\272\222.ipynb" index 6b670ec..11af4d0 100644 --- "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\345\222\214c\344\272\244\344\272\222.ipynb" +++ "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/C\346\211\251\345\261\225/\347\224\250ctypes\345\222\214C\344\272\244\344\272\222/\347\224\250ctypes\344\270\216\345\212\250\346\200\201\351\223\276\346\216\245\345\272\223\344\272\244\344\272\222.ipynb" @@ -4,21 +4,36 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 用ctypes和c交互" + "# 用types与动态链接库交互" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "[ctypes](https://docs.python.org/2/library/ctypes.html)是原版python和pypy都支持的一种与c交互的方式,他的原理其实是利用python调用c/c++的动态链接库(就是.so或者.dll那个文件),也就是说c还是那个c,python还是那个python,不用再学其他的了,这也是最推荐的一种与c直接交互的方式." + "[ctypes](https://docs.python.org/2/library/ctypes.html)是原版python和pypy都支持的一种与动态链接库(就是.so或者.dll那个文件)交互的方式,因此它也是最方便的一种与c语言混合编程的方式.c还是那个c,python还是那个python,不用再学其他的了,这也是简单情况下最推荐的一种与c直接交互的方式.\n", + "\n", + "ctypes 有以下优点:\n", + "\n", + "+ Python内建,不需要单独安装\n", + "+ 可以直接调用二进制的动态链接库 \n", + "+ 对基本类型的相互映射有良好的支持\n", + "\n", + "ctypes 有以下缺点:\n", + "\n", + "+ 平台兼容性差\n", + "+ 不能够直接调用动态链接库中未经导出的函数或变量\n", + "+ 对C++的支持差\n", + "\n", + "\n", + "**事先申明:**本文写在mac osx平台,使用的编译工具是clang,其他平台都没试过,有兴趣的小朋友可以自己摸索下" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "> 例子:一个二维向量运算的定义" + "> 例子:一个二维向量运算" ] }, { @@ -83,7 +98,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### 修改成动态链接库(把哪个显示结果的省了)\n", + "### 修改成动态链接库(把那个显示结果的省了)\n", "\n", "vector.h\n", "\n", @@ -175,12 +190,12 @@ }, "outputs": [], "source": [ - "from ctypes import CDLL,c_float,c_char_p,Structure,POINTER" + "from ctypes import CDLL,c_float,c_char_p,Structure,POINTER,c_int,pointer" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -194,13 +209,14 @@ " return self.__str__()\n", " def __add__(self,that):\n", " dll = CDLL(\"libvec.so\")\n", + " dll.add.argtypes = (Vector,Vector)\n", " dll.add.restype = Vector\n", " return dll.add(self,that)" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -211,7 +227,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -222,7 +238,7 @@ "Vector:<1.0,2.0>" ] }, - "execution_count": 11, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -233,7 +249,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -244,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -255,7 +271,7 @@ "Vector:<10.0,20.0>" ] }, - "execution_count": 13, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -266,10 +282,21 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "metadata": { "collapsed": false, - "scrolled": true + "scrolled": false + }, + "outputs": [], + "source": [ + "v3 = v1+v2" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false }, "outputs": [ { @@ -278,13 +305,20 @@ "Vector:<11.0,22.0>" ] }, - "execution_count": 14, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "v1+v2" + "v3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可喜可贺~~" ] }, { @@ -311,18 +345,278 @@ "c_float\t|float\t|float\n", "c_double\t|double\t|float\n", "c_char_p\t|char * (NUL terminated)\t|string or None\n", - "c_wchar_p\t|wchar_t * |(NUL terminated)\t|unicode or None\n", + "c_wchar_p\t|wchar_t * (NUL terminated)\t|unicode or None\n", "c_void_p\t|void *|int/long or None" ] }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "需要注意的是:\n", + "+ 关于指定映射类型,如果指定c中的返回值类型,则是`dll.FUNCNAME.restype = TYPE`,如果函数的返回值是`void`那么你可以赋值为 `None`,如果不设定,则默认是`int`类型;\n", + "+ 如果是传入参数类型指定,则使用`dll.FUNCNAME.argtypes = (TYPE1, TYPE2...)`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ctypes 和 指针" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "如何创建一个 ctypes 的指针呢?这里有三个跟指针有个的 ctypes 里的函数,\n", + "\n", + "函数\t|说明\n", + "---|---\n", + "byref(x [, offset])\t|返回 x 的地址,x 必须为 ctypes 类型的一个实例。相当于 c 的 &x 。 offset 表示偏移量。\n", + "pointer(x)\t|创建并返回一个指向 x 的指针实例, x 是一个实例对象。\n", + "POINTER(type)\t|返回一个类型,这个类型是指向 type 类型的指针类型, type 是 ctypes 的一个类型。\n", + "\n", + "\n", + "\n", + "byref 很好理解,传递参数的时候就用这个,用 pointer 创建一个指针变量也行,不过 byref 更快。\n", + "\n", + "而 pointer 和 POINTER 的区别是,pointer 返回一个实例,POINTER 返回一个类型。甚至你可以用 POINTER 来做 pointer 的工作:" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = c_int(6) # 创建一个 c_int 实例" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "c_int(6)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "b = pointer(a) # 创建指针" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.LP_c_int at 0x103d618c0>" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], - "source": [] + "source": [ + "c = POINTER(c_int)(a) # 创建指针" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.LP_c_int at 0x103d70050>" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "c_int(6)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b.contents #输出 a 的值" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "c_int(6)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c.contents # 输出 a 的值" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.LP_Vector at 0x103d704d0>" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pointer(v3)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.LP_Vector at 0x103d70560>" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "POINTER(Vector)(v3)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Vector:<11.0,22.0>" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "POINTER(Vector)(v3).contents" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "11.0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "POINTER(Vector)(v3).contents.x" + ] } ], "metadata": { From b56dc2655c239970a516efa8d38fce3d13f8a8de Mon Sep 17 00:00:00 2001 From: HUANG SIZHE Date: Mon, 31 Oct 2016 20:11:26 +0800 Subject: [PATCH 3/4] 123 123 --- .../mpi4py.ipynb" | 60 -------------- .../mpi4py-checkpoint.ipynb" | 0 .../hl.py" | 0 .../mpi4py.ipynb" | 78 +++++++++++++++++++ 4 files changed, 78 insertions(+), 60 deletions(-) delete mode 100755 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/mpi4py.ipynb" rename "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/.ipynb_checkpoints/mpi4py-checkpoint.ipynb" => "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/.ipynb_checkpoints/mpi4py-checkpoint.ipynb" (100%) rename "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/hl.py" => "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/hl.py" (100%) create mode 100755 "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/mpi4py.ipynb" diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/mpi4py.ipynb" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/mpi4py.ipynb" deleted file mode 100755 index ba4a384..0000000 --- "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/mpi4py.ipynb" +++ /dev/null @@ -1,60 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "mpi是c,c++,以及fortran下的并行框架,而[mpi4py](http://mpi4py.readthedocs.org/en/stable/)是它的python接口,pypy也可以使用呦\n", - "\n", - "安装它先要安装openmpi,然后安装numpy和cython" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "> 例 " - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Overwriting hl.py\n" - ] - } - ], - "source": [ - "%%writefile hl.py\n", - "from mpi4py import MPI\n", - "comm = MPI.COMM_WORLD\n", - "rank = comm.Get_rank()\n", - "print(\"hello world\")\n", - "print(\"my rank is:\",rank)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " mpirun –np 3 python h1.py" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Pypy", - "language": "python", - "name": "pypy" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/.ipynb_checkpoints/mpi4py-checkpoint.ipynb" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/.ipynb_checkpoints/mpi4py-checkpoint.ipynb" similarity index 100% rename from "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/.ipynb_checkpoints/mpi4py-checkpoint.ipynb" rename to "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/.ipynb_checkpoints/mpi4py-checkpoint.ipynb" diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/hl.py" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/hl.py" similarity index 100% rename from "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/mpi\346\211\251\345\261\225(mpi4py)/hl.py" rename to "Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/hl.py" diff --git "a/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/mpi4py.ipynb" "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/mpi4py.ipynb" new file mode 100755 index 0000000..7d43351 --- /dev/null +++ "b/Python_Other_Topics/\346\211\251\345\261\225\344\270\216\346\200\247\350\203\275\344\274\230\345\214\226/\345\244\232\350\277\233\347\250\213\345\267\245\345\205\267/mpi\346\211\251\345\261\225(mpi4py)/mpi4py.ipynb" @@ -0,0 +1,78 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "mpi(Message Passing Interface的简称),也就是消息传递,是c,c++,以及fortran下的并行框架,而[mpi4py](http://mpi4py.readthedocs.org/en/stable/)是它的python接口,pypy也可以使用呦\n", + "\n", + "安装它先要安装openmpi,然后安装numpy和cython\n", + "\n", + "## MPI的工作方式:\n", + "\n", + "MPI就是启动一组MPI进程,每个进程都是执行同样的代码,然后每个进程都有一个ID,也就是rank来标记自己。假设一个CPU是你请的一个工人,共有10个工人。你有100块砖头要搬,然后很公平,让每个工人搬10块。这时候,你把任务写到一个任务卡里面,让10个工人都执行这个任务卡中的任务,也就是搬砖!这个任务卡中的“搬砖”就是你写的代码。然后10个CPU执行同一段代码。需要注意的是,代码里面的所有变量都是每个进程独有的,虽然名字相同。简单的理解就是工作室魔兽多开刷金..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> 例: helloworld" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false + }, + "source": [ + "h1.py\n", + "\n", + "```python\n", + "from mpi4py import MPI\n", + "comm = MPI.COMM_WORLD\n", + "rank = comm.Get_rank()\n", + "print(\"hello world\")\n", + "print(\"my rank is:\",rank)\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " mpirun –np 3 python h1.py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Pypy", + "language": "python", + "name": "pypy" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.10" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From fd047093ba355d65cfd9b18f1c7fb5d4b21dcc98 Mon Sep 17 00:00:00 2001 From: HUANG SIZHE Date: Mon, 31 Oct 2016 21:43:05 +0800 Subject: [PATCH 4/4] delete 1 --- ...72\277\347\250\213\346\250\241\345\235\227(threading).ipynb" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/Python_STD_LIB/\345\271\266\350\241\214\345\244\204\347\220\206/\345\244\232\347\272\277\347\250\213\346\250\241\345\235\227(threading).ipynb" "b/Python_STD_LIB/\345\271\266\350\241\214\345\244\204\347\220\206/\345\244\232\347\272\277\347\250\213\346\250\241\345\235\227(threading).ipynb" index 366ab81..67c5517 100755 --- "a/Python_STD_LIB/\345\271\266\350\241\214\345\244\204\347\220\206/\345\244\232\347\272\277\347\250\213\346\250\241\345\235\227(threading).ipynb" +++ "b/Python_STD_LIB/\345\271\266\350\241\214\345\244\204\347\220\206/\345\244\232\347\272\277\347\250\213\346\250\241\345\235\227(threading).ipynb" @@ -571,7 +571,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.11" + "version": "2.7.12" } }, "nbformat": 4,