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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.5.1 Type annotati<strong>on</strong>sType declarati<strong>on</strong>s enable the compiler to generate efficient C code. <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> is a dynamicallytyped language and has no type declarati<strong>on</strong> syntax. For this purpose, an extensi<strong>on</strong> wasintroduced to <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> to declare the type signature <str<strong>on</strong>g>of</str<strong>on</strong>g> a functi<strong>on</strong>.In this extensi<strong>on</strong>, the programmer creates a special decorator to annotate each functi<strong>on</strong>to be compiled. This decorator declares the types <str<strong>on</strong>g>of</str<strong>on</strong>g> the functi<strong>on</strong>’s parameters and <str<strong>on</strong>g>of</str<strong>on</strong>g> itsreturn value. Decorators are annotati<strong>on</strong>s added just be<str<strong>on</strong>g>for</str<strong>on</strong>g>e a functi<strong>on</strong> definiti<strong>on</strong> and area standard <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> syntax feature.Opti<strong>on</strong>ally, the types <str<strong>on</strong>g>of</str<strong>on</strong>g> local variables may also bedeclared in a similar fashi<strong>on</strong>. If no such declarati<strong>on</strong>s are provided, the compiler infers thetypes <str<strong>on</strong>g>of</str<strong>on</strong>g> a local variable based <strong>on</strong> the type <str<strong>on</strong>g>of</str<strong>on</strong>g> the first value assigned to the variable in thefuncti<strong>on</strong>. In this framework, a variable cannot change its type within a functi<strong>on</strong>.Figure 3.5 shows an example <str<strong>on</strong>g>of</str<strong>on</strong>g> type-declarati<strong>on</strong> syntax. The decorator unpyth<strong>on</strong>.typeis added to the functi<strong>on</strong> declarati<strong>on</strong>. Types are passed to the decorator as arguments. Ifthe functi<strong>on</strong> takes x number <str<strong>on</strong>g>of</str<strong>on</strong>g> parameters, then x + 1 types are required. The first x typesare <str<strong>on</strong>g>for</str<strong>on</strong>g> the parameters and the last type specified is <str<strong>on</strong>g>for</str<strong>on</strong>g> the return type <str<strong>on</strong>g>of</str<strong>on</strong>g> the functi<strong>on</strong>. Inthe example, the first type is <str<strong>on</strong>g>for</str<strong>on</strong>g> the parameter n and the sec<strong>on</strong>d type is <str<strong>on</strong>g>for</str<strong>on</strong>g> the return type.The types <str<strong>on</strong>g>of</str<strong>on</strong>g> sum and i are automatically inferred by the compiler to be int64 which is thedefault integer type. These types can be <str<strong>on</strong>g>for</str<strong>on</strong>g>ced to be int32 by adding type declarati<strong>on</strong>s <str<strong>on</strong>g>for</str<strong>on</strong>g>the local variables.1 @unpyth<strong>on</strong> . type ( ’ i n t 6 4 ’ , ’ i n t 6 4 ’ )2 def f ( n ) :3 sum = 04 <str<strong>on</strong>g>for</str<strong>on</strong>g> i in range ( n ) :5 sum += i6 return sumFigure 3.5: Example <str<strong>on</strong>g>of</str<strong>on</strong>g> decorator used as a type declarator.3.5.2 <str<strong>on</strong>g>Parallel</str<strong>on</strong>g> annotati<strong>on</strong>sThe <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> interpreter does not provide any support <str<strong>on</strong>g>for</str<strong>on</strong>g> parallel programming. One wayto take advantage <str<strong>on</strong>g>of</str<strong>on</strong>g> multiple processor cores is to utilize process-based parallelism wheremultiple instances <str<strong>on</strong>g>of</str<strong>on</strong>g> the <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> interpreter may be launched. Process-based parallelism isnot suitable <str<strong>on</strong>g>for</str<strong>on</strong>g> all types <str<strong>on</strong>g>of</str<strong>on</strong>g> problems and is not c<strong>on</strong>sidered in this thesis. The <strong>on</strong>ly wayto take advantage <str<strong>on</strong>g>of</str<strong>on</strong>g> multiple processor cores within a single <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> process is to writea multithreaded functi<strong>on</strong> in C using an API such as pthreads or OpenMP. However, thecurrent <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g> interpreter is not multithreaded and calling <str<strong>on</strong>g>Pyth<strong>on</strong></str<strong>on</strong>g>-C API functi<strong>on</strong>s fromtwo different C threads is unsafe. There<str<strong>on</strong>g>for</str<strong>on</strong>g>e, the programmer must be careful about calling25

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

Saved successfully!

Ooh no, something went wrong!