Interfacing Python and C using Ctypes
Giuseppe Piero Brandino and Jimmy Aguilar Mena March 9, 2016
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 1 / 12
Interfacing Python and C using Ctypes Giuseppe Piero Brandino and - - PowerPoint PPT Presentation
Interfacing Python and C using Ctypes Giuseppe Piero Brandino and Jimmy Aguilar Mena March 9, 2016 Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 1 / 12 Motivation When do we want to
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 1 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 2 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 2 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 2 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 2 / 12
#i n c l u d e <s t d i o . h> i n t main ( i n t argc , char ∗∗argv ){ i n t i , j , t o t a l ; double avg ; t o t a l = 10000000; f o r ( i = 0; i < 10; i ++){ avg = 0; f o r ( j = 0 ; j < t o t a l ; j++){ avg += j ; } avg = avg/ t o t a l ; } p r i n t f ( ” Average i s %f \n” , avg ) ; r e t u r n 0 ; }
t o t a l = 10000000 f o r i i n xrange ( 1 0 ) : avg = 0.0 f o r j i n xrange ( t o t a l ) : avg += j avg = avg/ t o t a l p r i n t ” Average i s {0}” . format ( avg )
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 3 / 12
#i n c l u d e <s t d i o . h> i n t main ( i n t argc , char ∗∗argv ){ i n t i , j , t o t a l ; double avg ; t o t a l = 10000000; f o r ( i = 0; i < 10; i ++){ avg = 0; f o r ( j = 0 ; j < t o t a l ; j++){ avg += j ; } avg = avg/ t o t a l ; } p r i n t f ( ” Average i s %f \n” , avg ) ; r e t u r n 0 ; }
t o t a l = 10000000 f o r i i n xrange ( 1 0 ) : avg = 0.0 f o r j i n xrange ( t o t a l ) : avg += j avg = avg/ t o t a l p r i n t ” Average i s {0}” . format ( avg )
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 3 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 4 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 4 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 5 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 6 / 12
f l o a t a d d f l o a t ( f l o a t a , f l o a t b){ r e t u r n a + b ;} i n t a d d i n t ( i n t a , i n t b){ r e t u r n a + b ;} i n t a d d f l o a t r e f ( f l o a t ∗a , f l o a t ∗b , f l o a t ∗c ){ ∗c = ∗a + ∗b ; r e t u r n 0; }
i n t a d d i n t a r r a y ( i n t ∗a , i n t ∗b , i n t ∗c , i n t n){ i n t i ; f o r ( i = 0; i < n ; i ++) { c [ i ] = a [ i ] + b [ i ] ; } r e t u r n 0; } f l o a t dot product ( f l o a t ∗a , f l o a t ∗b , i n t n ) { f l o a t r e s =0; i n t i ; f o r ( i = 0; i < n ; i ++) { r e s = r e s + a [ i ] ∗ b [ i ] ; } r e t u r n r e s ; }
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 7 / 12
f l o a t a d d f l o a t ( f l o a t a , f l o a t b){ r e t u r n a + b ;} i n t a d d i n t ( i n t a , i n t b){ r e t u r n a + b ;} i n t a d d f l o a t r e f ( f l o a t ∗a , f l o a t ∗b , f l o a t ∗c ){ ∗c = ∗a + ∗b ; r e t u r n 0; }
i n t a d d i n t a r r a y ( i n t ∗a , i n t ∗b , i n t ∗c , i n t n){ i n t i ; f o r ( i = 0; i < n ; i ++) { c [ i ] = a [ i ] + b [ i ] ; } r e t u r n 0; } f l o a t dot product ( f l o a t ∗a , f l o a t ∗b , i n t n ) { f l o a t r e s =0; i n t i ; f o r ( i = 0; i < n ; i ++) { r e s = r e s + a [ i ] ∗ b [ i ] ; } r e t u r n r e s ; }
import c t y p e s math= c t y p e s . CDLL( ” libmymath . so ” ) math . a d d i n t (4 ,5) Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 7 / 12
f l o a t a d d f l o a t ( f l o a t a , f l o a t b){ r e t u r n a + b ;} i n t a d d i n t ( i n t a , i n t b){ r e t u r n a + b ;} i n t a d d f l o a t r e f ( f l o a t ∗a , f l o a t ∗b , f l o a t ∗c ){ ∗c = ∗a + ∗b ; r e t u r n 0; }
i n t a d d i n t a r r a y ( i n t ∗a , i n t ∗b , i n t ∗c , i n t n){ i n t i ; f o r ( i = 0; i < n ; i ++) { c [ i ] = a [ i ] + b [ i ] ; } r e t u r n 0; } f l o a t dot product ( f l o a t ∗a , f l o a t ∗b , i n t n ) { f l o a t r e s =0; i n t i ; f o r ( i = 0; i < n ; i ++) { r e s = r e s + a [ i ] ∗ b [ i ] ; } r e t u r n r e s ; }
import c t y p e s math= c t y p e s . CDLL( ” libmymath . so ” ) math . a d d i n t (4 ,5) Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 7 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 8 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 8 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 8 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 8 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 9 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 9 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 9 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 10 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 10 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 11 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 11 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 11 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 12 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 12 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 12 / 12
Giuseppe Piero Brandino and Jimmy Aguilar Mena Interfacing Python and C using Ctypes March 9, 2016 12 / 12