How PyPy can help High Performance Computing
How PyPy can help High Performance Computing
Slide 2
Slide 2 text
Short bio
Short bio
PyPy core dev since 2006
pdb++, CFFI, vmprof, capnpy, ...
@antocuni
https://github.com/antocuni (https://github.com/antocuni)
https://bitbucket.org/antocuni (https://bitbucket.org/antocuni)
Slide 3
Slide 3 text
How many of you use Python?
How many of you use Python?
Slide 4
Slide 4 text
How many have ever had performance problems?
How many have ever had performance problems?
Slide 5
Slide 5 text
Why do you use Python, then?
Why do you use Python, then?
Slide 6
Slide 6 text
Python strong points
Python strong points
Simplicity
Lots of libraries
Ecosystem
Ok, but why?
Slide 7
Slide 7 text
Python
Python REAL strong points
strong points
Expressive & simple APIs
Uniform typesystem (everything is an object)
Powerful abstractions
Slide 8
Slide 8 text
Example: JSON
Example: JSON
Slide 9
Slide 9 text
JSONObject jsonObj = new JSONObject(jsonString);
JSONArray jArray = jsonObj.getJSONArray("data");
int length = jArray.length();
for(int i=0; i Ingredients = new ArrayList<>();
for(int j=0; j
Slide 10
Slide 10 text
obj = json.loads(string)
for item in obj['data']:
id = item['id']
name = item['name']
ingredients = []
for ingr in item["ingredients"]:
ingredients.append(ingr['name'])
Slide 11
Slide 11 text
So far so good, BUT
So far so good, BUT
abstraction
iterators
abstraction
temp objects
abstraction
classes/methods/functions
core of
computation
Slide 12
Slide 12 text
Example of temporary objects
Example of temporary objects
Bound methods
Bound methods
In [ ]: class A(object):
def foo(self):
return 42
a = A()
bound_foo = a.foo
%timeit a.foo()
%timeit bound_foo()
Slide 13
Slide 13 text
Ideally
Ideally
Think of concepts, not implementation details
Think of concepts, not implementation details
Real world
Real world
Details leak to the user
Details leak to the user
Slide 14
Slide 14 text
Python problem
Python problem
Tension between abstractions and performance
Tension between abstractions and performance
Slide 15
Slide 15 text
Classical Python approaches to performance
Classical Python approaches to performance
Slide 16
Slide 16 text
1. Work around in the user code
1. Work around in the user code
e.g. create bound methods beforehand
e.g. create bound methods beforehand
Slide 17
Slide 17 text
2. Work around in the language specs
2. Work around in the language specs
range vs xrange
dict.keys vs .iterkeys
int vs long
array.array vs list
Easier to implement
Harder to use
Clutter the language unnecessarily
More complex to understand
Not really Pythonic
Slide 18
Slide 18 text
3. Stay in C as much as possible
3. Stay in C as much as possible
In [29]:
In [31]:
numbers = range(1000)
% timeit [x*2 for x in numbers]
import numpy as np
numbers = np.arange(1000)
% timeit numbers*2
10000 loops, best of 3: 47.1 µs per loop
The slowest run took 17.59 times longer than the fastest. This could mean that
an intermediate result is being cached.
1000000 loops, best of 3: 1.48 µs per loop
Slide 19
Slide 19 text
4. Rewrite in C
4. Rewrite in C
#include "Python.h"
Cython
CFFI
Slide 20
Slide 20 text
"Rewrite in C" approach
"Rewrite in C" approach
aka, 90/10 rule
aka, 90/10 rule
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
No content
Slide 24
Slide 24 text
Abstractions cost
Code quality => poor performance
Python parts become relevant
Slide 25
Slide 25 text
Python in the HPC world
Python in the HPC world
Python as a glue-only language
Python as a glue-only language
Tradeo between speed and code quality
Tradeo between speed and code quality
Slide 26
Slide 26 text
PyPy
PyPy
Alternative Python implementation
Ideally: no visible difference to the user
JIT compiler
http://pypy.org (http://pypy.org)
Slide 27
Slide 27 text
How fast is PyPy?
How fast is PyPy?
Wrong question
Wrong question
Up to 80x faster in extreme cases
10x faster in good cases
2x faster on "random" code
sometime it's just slower
Slide 28
Slide 28 text
PyPy aws
PyPy aws
Far from being perfect
it leaks other implementation details than CPython
e.g. JIT warmup, GC pecularities
Slide 29
Slide 29 text
PyPy qualities
PyPy qualities
Slide 30
Slide 30 text
Make pythonic, idiomatic code fast
Make pythonic, idiomatic code fast
Slide 31
Slide 31 text
Abstractions are (almost) free
Abstractions are (almost) free
Slide 32
Slide 32 text
The better the code, the biggest the speedup
The better the code, the biggest the speedup
Slide 33
Slide 33 text
No content
Slide 34
Slide 34 text
No content
Slide 35
Slide 35 text
Python as a rst class language
Python as a rst class language
No longer "just glue"
No longer "just glue"
Slide 36
Slide 36 text
Example: Sobel lter
Example: Sobel lter
Extendend version
"The Joy of PyPy: Abstractions for Free", EP 2017
https://speakerdeck.com/antocuni/the-joy-of-pypy-jit-abstractions-for-free
(https://speakerdeck.com/antocuni/the-joy-of-pypy-jit-abstractions-for-free)
https://www.youtube.com/watch?v=NQfpHQII2cU
(https://www.youtube.com/watch?v=NQfpHQII2cU)
Slide 37
Slide 37 text
The
The BIG problem: C extensions
problem: C extensions
CPython
CPython
Slide 38
Slide 38 text
PyPy (cpyext)
PyPy (cpyext)
Slide 39
Slide 39 text
No content
Slide 40
Slide 40 text
cpyext
cpyext
PyPy version of Python.h
Compatibility layer
Most C extensions just work: numpy, scipy, pandas, etc.
Slow :(
Use CFFI whenever it's possible
Slide 41
Slide 41 text
We are working on it
We are working on it
Future status (hopefully)
Future status (hopefully)
All C extensions will just work
C code as fast as today, Python code super-fast
The best of both worlds
PyPy as the default choice for HPC
My personal estimate: 6 months of work and we have a fast cpyext
(let's talk about money :))