11.07.2015 Views

A Compiler for Parallel Exeuction of Numerical Python Programs on ...

A Compiler for Parallel Exeuction of Numerical Python Programs on ...

A Compiler for Parallel Exeuction of Numerical Python Programs on ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1 import numpy2 a r r = numpy . z e r o s ( ( 1 0 0 , 3 0 0 ) )3 v1 = a r r [ 5 : 9 7 : 3 , 3 : 3 0 0 : 2 ]4 a r r [ 5 , 3 ] = 1 . 05 print v1 [ 0 , 0 ]6 v1 [ 1 , 2 ] = 2 . 07 print a r r [ 8 , 7 ]8 print a r r . s t r i d e s9 print v1 . s t r i d e s10 v2 = a r r . swapaxes ( 0 , 1 )11 print v2 . s t r i d e s12 v3 = a r r . reshape (100∗300)13 print v3 [300∗5+3]14 v4 = v3 [ 3 : 1 0 0 ∗ 3 0 0 : 5 ]15 print v4 [ 3 0 0 ]Figure 3.3: Examples <str<strong>on</strong>g>of</str<strong>on</strong>g> array slicing in <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g>.The strides field can be changed directly without going through slicing or method callssuch as reshape. The strides <str<strong>on</strong>g>of</str<strong>on</strong>g> a NumPy array can also be changed through the NumPy CAPI.3.3 <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g>-C API<str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> combines the development flexibility <str<strong>on</strong>g>of</str<strong>on</strong>g> a dynamically-typed language with an API<str<strong>on</strong>g>for</str<strong>on</strong>g> efficient C implementati<strong>on</strong> <str<strong>on</strong>g>of</str<strong>on</strong>g> per<str<strong>on</strong>g>for</str<strong>on</strong>g>mance critical porti<strong>on</strong>s — functi<strong>on</strong>s and classes —<str<strong>on</strong>g>of</str<strong>on</strong>g> an applicati<strong>on</strong>. This <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g>-C API provides many utility functi<strong>on</strong>s <str<strong>on</strong>g>for</str<strong>on</strong>g> passing databack and <str<strong>on</strong>g>for</str<strong>on</strong>g>th between C and <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g>. <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> exposes all <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> objects as a PyObjectdatatype in C. The API provides various c<strong>on</strong>venience functi<strong>on</strong>s and macros to c<strong>on</strong>vertbetween PyObject and C native types such as int, l<strong>on</strong>g and structures. Various other Cdatatypes are also provided <str<strong>on</strong>g>for</str<strong>on</strong>g> <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> types such as list and tuple. However, the base type<str<strong>on</strong>g>of</str<strong>on</strong>g> all these types is PyObject. NumPy’s C-API exposes NumPy arrays as PyArrayObjectdatatype in C and allows direct manipulati<strong>on</strong> <str<strong>on</strong>g>of</str<strong>on</strong>g> all fields <str<strong>on</strong>g>of</str<strong>on</strong>g> the NumPy object.For implementing functi<strong>on</strong>s, the <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g>-C API requires that any functi<strong>on</strong> that is to beused as a <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> functi<strong>on</strong> must have a predefined type signature and must pass and returnpointers to PyObjects because the <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> interpreter <strong>on</strong>ly knows about PyObjects.<str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> modules implemented in C utilizing the <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g>-C API are called <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> extensi<strong>on</strong>modules. To write an extensi<strong>on</strong> module foo in C, a programmer first writes the code<str<strong>on</strong>g>for</str<strong>on</strong>g> the functi<strong>on</strong>s to be implemented in C in <strong>on</strong>e or more C files. The programmer thenwrites a special module initializati<strong>on</strong> functi<strong>on</strong>, which must be named initfoo if the desiredmodule name is foo. The C code is then compiled into a single dynamically loadable library(DLL <strong>on</strong> windows or shared object files <strong>on</strong> linux) called foo.so <strong>on</strong> Linux or foo.dll <strong>on</strong>22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!