making python computations fast

21
Copyright © 2015 Russel Winder 1 Making Python computations fast Dr Russel Winder email: [email protected] twitter: @russel_winder Web: http://www.russel.org.uk

Upload: russel-winder

Post on 14-Apr-2017

651 views

Category:

Technology


5 download

TRANSCRIPT

Copyright © 2015 Russel Winder 1

Making Python computations fast

Dr Russel Winder

email: [email protected]: @russel_winder

Web: http://www.russel.org.uk

Copyright © 2015 Russel Winder 2

Presentation slots arevery short…

Copyright © 2015 Russel Winder 3

Structure

BeginningMiddleEnd

Copyright © 2015 Russel Winder 4

What is the value of ? π

Copyright © 2015 Russel Winder 5

π

Copyright © 2015 Russel Winder 6

It's simples. Александр Орлов 2009

Copyright © 2007–2015 Russel Winder 7

The Maths

The equation:

Leads to the approximation formula:

∑i=0

n 4

(1+xi2)Δ x≈π

∫0

1 4

(1+x2)dx=π ¿

Copyright © 2007–2015 Russel Winder 8

The ConceptionCalculate an approximation to the area undera conic section by filling the area with a knownnumber of rectangles of known size and thensum the areas of the rectangles.

The narrower the rectangles, the more accurate the approximation.

This is a data parallel problem andembarrassingly parallel.

Copyright © 2007–2015 Russel Winder 9

The Parallelism● Addition is commutative and associative so we can partition the overall summation in any way we want:

a + b + c + d + e + f

( a + b ) + ( c + d ) + ( e + f )

Can partition the overall sum into bits tobe done on different processors.This should make things faster.

Copyright © 2007–2015 Russel Winder 10

Adding the Parallelism● Instead of doing all the calculations sequentially, partition the problem into a number of chunks.

problem

subproblem

result

subproblem

subproblem

subproblem

.

.

.

scatter gather

fork join

reduce

Data parallel

map

Copyright © 2007–2015 Russel Winder 11

Some Code● Sequential.● Use threads to obtain parallelism…not.● Use process to obtain parallelism.

Friends don't let friends use Python 2 instead of Python 3.

Copyright © 2007–2015 Russel Winder 12

It is all just too slow.

Copyright © 2007–2015 Russel Winder 13

More Code● Use Cython.● Use Numba.

cf. lightning talk at PyConUK 2014.

Copyright © 2007–2015 Russel Winder 14

A lot faster but… iscontrolling parallelism inPython the thing to do…

Copyright © 2007–2015 Russel Winder 15

Even More Code● Use NumPy.● Use C.● Use C++/Boost.● Use D/PyD.● Use Chapel/PyChapel.

Offload the whole computation,Leaving Python code as thecoordination code.

https://github.com/russel/Pi_Quadrature/tree/master/Python

http://dlang.org/ http://pyd.readthedocs.org/

http://chapel.cray.com/

http://pychapel.readthedocs.org/

Copyright © 2007–2015 Russel Winder 16

Parallelism and computationis not Python's forte.

(Even though Python is a great language.)

Copyright © 2007–2015 Russel Winder 17

So do not do it.

Copyright © 2007–2015 Russel Winder 18

NUMA

PGAS

Copyright © 2007–2015 Russel Winder 19

Employ a multilingual,polyglot programming mentality.

Copyright © 2015 Russel Winder 20

Making Python computations fast

Dr Russel Winder

email: [email protected]: @russel_winder

Web: http://www.russel.org.uk

Copyright © 2015 Russel Winder 21

John, you were a truly Pythonic hero,I miss your physical presence here.