Slide 1

Slide 1 text

PyStokes: A case study of accelerating Python using Cython Rajesh Singh Jan 31, 2015 1

Slide 2

Slide 2 text

Contributors 2

Slide 3

Slide 3 text

Outline • Stokes law • Rigid body motion of active colloids 3

Slide 4

Slide 4 text

Outline • Stokes law • Rigid body motion of active colloids • Python 3

Slide 5

Slide 5 text

Outline • Stokes law • Rigid body motion of active colloids • Python • Cython 3

Slide 6

Slide 6 text

Outline • Stokes law • Rigid body motion of active colloids • Python • Cython • Python and Cython 3

Slide 7

Slide 7 text

Outline • Stokes law • Rigid body motion of active colloids • Python • Cython • Python and Cython • PyStokes 3

Slide 8

Slide 8 text

Outline • Stokes law • Rigid body motion of active colloids • Python • Cython • Python and Cython • PyStokes • Benchmarks 3

Slide 9

Slide 9 text

Stokes law 4

Slide 10

Slide 10 text

Stokes law Vn = Fn 6πηa 4

Slide 11

Slide 11 text

Stokes law Vn = Fn 6πηa Ωn = Tn 8πηa3 4

Slide 12

Slide 12 text

Rigid body motion of active colloids Vn = N m=1 µTT nm · FB m + µTR nm · TB m + lσ, m π(T, lσ) nm · V(lσ) m , Ωn = N m=1 µRT nm · FB m + µRR nm · TB m + lσ, m π(R, lσ) nm · V(lσ) m . J. Stat. Mech. (2015) P06017 5

Slide 13

Slide 13 text

Python 1 Free and open source 2 High level & interpreted 3 Interactive environment 4 Object-oriented 6

Slide 14

Slide 14 text

Python 1 Free and open source 2 High level & interpreted 3 Interactive environment 4 Object-oriented 5 Speed 6 Dictionary lookups 7 Function calling overheads 8 GIL - global interpreter lock 6

Slide 15

Slide 15 text

Cython • Attempt to make a superset of python 7

Slide 16

Slide 16 text

Cython • Attempt to make a superset of python • High level coolness of python along with the speed of C 7

Slide 17

Slide 17 text

Cython • Attempt to make a superset of python • High level coolness of python along with the speed of C • Compiled 7

Slide 18

Slide 18 text

Cython • Attempt to make a superset of python • High level coolness of python along with the speed of C • Compiled • Cdef variables, attributes, functions 7

Slide 19

Slide 19 text

Cython • Attempt to make a superset of python • High level coolness of python along with the speed of C • Compiled • Cdef variables, attributes, functions • Supports parallelism (openMP) by opening GIL 7

Slide 20

Slide 20 text

Cython • Attempt to make a superset of python • High level coolness of python along with the speed of C • Compiled • Cdef variables, attributes, functions • Supports parallelism (openMP) by opening GIL • Memoryviews allow efficient access to memory buffers like numpy arrays without python overhead 7

Slide 21

Slide 21 text

Cython • Attempt to make a superset of python • High level coolness of python along with the speed of C • Compiled • Cdef variables, attributes, functions • Supports parallelism (openMP) by opening GIL • Memoryviews allow efficient access to memory buffers like numpy arrays without python overhead • Steps involves in building a cython code 1 A .pyx file is compiled by cython to .c 7

Slide 22

Slide 22 text

Cython • Attempt to make a superset of python • High level coolness of python along with the speed of C • Compiled • Cdef variables, attributes, functions • Supports parallelism (openMP) by opening GIL • Memoryviews allow efficient access to memory buffers like numpy arrays without python overhead • Steps involves in building a cython code 1 A .pyx file is compiled by cython to .c 2 The .c file is then compiled by the C compiler 7

Slide 23

Slide 23 text

Cython • Attempt to make a superset of python • High level coolness of python along with the speed of C • Compiled • Cdef variables, attributes, functions • Supports parallelism (openMP) by opening GIL • Memoryviews allow efficient access to memory buffers like numpy arrays without python overhead • Steps involves in building a cython code 1 A .pyx file is compiled by cython to .c 2 The .c file is then compiled by the C compiler 3 Building can be done in a single step using a setup.py 7

Slide 24

Slide 24 text

Python and Cython 8

Slide 25

Slide 25 text

Python and Cython 9

Slide 26

Slide 26 text

Python and Cython 10

Slide 27

Slide 27 text

Python and Cython • Cython is three orders of magnitude FASTER than Python! 10

Slide 28

Slide 28 text

Python and Cython • Cython is three orders of magnitude FASTER than Python! • The Cython code is as fast as C code!! 10

Slide 29

Slide 29 text

PyStokes: README.md • Cython library for computing Stokes flows produced by spheres • The library computes flow and RBM • Geometries supported • unbounded • wall-bounded • periodic • Free and open source • Planned developments • linear solvers • fast multipole accelerations • data layout • References • R. Singh, A. Laskar, and R. Adhikari. PyStokes: Hampi, Nov 2014. • S. Ghose and R. Adhikari. Phys. Rev. Lett., 112(11):118102, 2014. • R. Singh, S. Ghose and R. Adhikari. J. Stat. Mech, P06017, 2015 11

Slide 30

Slide 30 text

PyStokes: Usage import pystokes , p y f o r c e s import numpy as np a , Np = 1 , 100 L , dim = 128 , 3 dt , T = 0.01 , 100 v = np . zeros ( dim∗Np ) ; r = v ; F = v ; pRbm = pystokes . p e r i o d i c .Rbm(a , Np, L) f f = p y f o r c e s . f o r c e F i e l d s . Forces (Np) for t t in range (T) : f f . sedimentation (F , g=−10) pRbm. s t o k e s l e t V ( v , r , F) r = ( r + (F/(0.75∗ a ) + v )∗ dt)%L 12

Slide 31

Slide 31 text

PyStokes: Example Figure: Crowley instability in a sedimenting lattice. 13

Slide 32

Slide 32 text

PyStokes: Benchmarks Figure: Propulsion matrix calculation using the PyStokes library Present implementation scales • linearly with # cores • quadratic with # particles 14

Slide 33

Slide 33 text

Summary • Free and open source library • Efficient and fast evaluation of Stokes flow • A python front end for the user • Present implementation scales • linearly with # cores • quadratic with # particles 15