Making Computations Execute Very Quickly

Post on 06-Aug-2015

286 views 2 download

Tags:

transcript

Copyright © 2015 Russel Winder 1

Making ComputationsExecute Very Quickly

Dr Russel Winder

email: russel@winder.org.uktwitter: @russel_winder

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

Copyright © 2015 Russel Winder 2

Python is slow…

Copyright © 2015 Russel Winder 3

…at computation.

Copyright © 2015 Russel Winder 4

CPU Bound vs I/O Bound 1/2—

● Python is entirely fine for essentially I/O bound activity:

● Managing user interfaces via native code widgets (Qt, GTK, Wx, )…

● Managing networking activity.

Common theme here, the use of an event loop.

Copyright © 2015 Russel Winder 5

CPU Bound vs I/O Bound 2/2—

● Python uses hardware floating point, but via the Python heap.

● Python uses hardware integers for small integer values, but via the Python heap.

Result: non-trivial numerical activity is slow.

Copyright © 2015 Russel Winder 6

Copyright © 2015 Russel Winder 7

Copyright © 2015 Russel Winder 8

Copyright © 2015 Russel Winder 9

What is the value of ?

Copyright © 2015 Russel Winder 10

Well that's easy, it's…

Copyright © 2015 Russel Winder 11

Copyright © 2015 Russel Winder 12

Exactly.

Copyright © 2015 Russel Winder 13

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

Copyright © 2015 Russel Winder 14

Albeit irrational.

Copyright © 2015 Russel Winder 15

Approximating

● What is it's value represented as a floating point number?

● We can only obtain an approximation.

● A plethora of possible algorithms to choose from, a popular one is to employ the following integral equation.

π4=∫0

1 1

1+ x2dx

Copyright © 2015 Russel Winder 16

One possible algorithm

● Use quadrature to estimate the value of the integral which is the area under the curve.–

π=4n∑i=1

n 1

1+(i−0.5n

)2

With n = 3 not much to do, but potentially lots of error. Use n = 107 or n = 109?

Embarrassingly parallel.

Copyright © 2015 Russel Winder 17

Code!

Copyright © 2015 Russel Winder 18

C++D

Chapel

Copyright © 2015 Russel Winder 19

Because addition is commutative andassociative, expression can be

decomposed into sums of partial sums.

Copyright © 2015 Russel Winder 20

a + b + c + d + e + f

=

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

Copyright © 2015 Russel Winder 21

Scatter Gather—

map reduce

data parallel

Copyright © 2015 Russel Winder 22

Code!

Copyright © 2015 Russel Winder 23

C++D

Chapel

Copyright © 2015 Russel Winder 24

The Python data modeland its GIL make Python

unsuitable for parallel computation.

Copyright © 2015 Russel Winder 25

PyPy and NumPy do not help,nor does Cython, Numba, etc., as much as they perhaps should.

Copyright © 2015 Russel Winder 26

Native code, e.g. C++, D, Chapel,are the way forward forCPU-bound components of a

Python-based system.

Copyright © 2015 Russel Winder 27

And then there isOpenCL and OpenGL,

soon to be replaced by Vulkan.

Copyright © 2015 Russel Winder 28

Making ComputationsExecute Very Quickly

Dr Russel Winder

email: russel@winder.org.uktwitter: @russel_winder

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