Transcript
Page 1: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 1

Making ComputationsExecute Very Quickly

Dr Russel Winder

email: [email protected]: @russel_winder

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

Page 2: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 2

Python is slow…

Page 3: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 3

…at computation.

Page 4: Making Computations Execute Very Quickly

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.

Page 5: Making Computations Execute Very Quickly

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.

Page 6: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 6

Page 7: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 7

Page 8: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 8

Page 9: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 9

What is the value of ?

Page 10: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 10

Well that's easy, it's…

Page 11: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 11

Page 12: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 12

Exactly.

Page 13: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 13

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

Page 14: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 14

Albeit irrational.

Page 15: Making Computations Execute Very Quickly

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

Page 16: Making Computations Execute Very Quickly

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.

Page 17: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 17

Code!

Page 18: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 18

C++D

Chapel

Page 19: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 19

Because addition is commutative andassociative, expression can be

decomposed into sums of partial sums.

Page 20: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 20

a + b + c + d + e + f

=

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

Page 21: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 21

Scatter Gather—

map reduce

data parallel

Page 22: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 22

Code!

Page 23: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 23

C++D

Chapel

Page 24: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 24

The Python data modeland its GIL make Python

unsuitable for parallel computation.

Page 25: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 25

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

Page 26: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 26

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

Python-based system.

Page 27: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 27

And then there isOpenCL and OpenGL,

soon to be replaced by Vulkan.

Page 28: Making Computations Execute Very Quickly

Copyright © 2015 Russel Winder 28

Making ComputationsExecute Very Quickly

Dr Russel Winder

email: [email protected]: @russel_winder

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