Skip to content

Commit e85b95b

Browse files
committed
misc
1 parent c400f8e commit e85b95b

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

lectures/need_for_speed.md

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ premature optimization is the root of all evil." -- Donald Knuth
2727

2828
## Overview
2929

30-
Python is extremely popular for scientific computing, due to such factors as
30+
Python is popular for scientific computing due to factors such as
3131

3232
* the accessible and expressive nature of the language itself,
33-
* its vast range of high quality scientific libraries,
33+
* the huge range of high quality scientific libraries,
3434
* the fact that the language and libraries are open source,
3535
* the popular [Anaconda Python distribution](https://www.anaconda.com/download), which simplifies installation and management of scientific libraries, and
3636
* the key role that Python plays in data science, machine learning and artificial intelligence.
@@ -70,7 +70,9 @@ One reason we use scientific libraries is because they implement routines we wan
7070

7171
For example, it's almost always better to use an existing routine for root finding than to write a new one from scratch.
7272

73-
(For standard algorithms, efficiency is maximized if the community can coordinate on a common set of implementations, written by experts and tuned by users to be as fast and robust as possible.)
73+
(For standard algorithms, efficiency is maximized if the community can
74+
coordinate on a common set of implementations, written by experts and tuned by
75+
users to be as fast and robust as possible.)
7476

7577
But this is not the only reason that we use Python's scientific libraries.
7678

@@ -93,9 +95,9 @@ At QuantEcon, the scientific libraries we use most often are
9395
* [NumPy](https://numpy.org/)
9496
* [SciPy](https://scipy.org/)
9597
* [Matplotlib](https://matplotlib.org/)
98+
* [JAX](https://github.com/jax-ml/jax)
9699
* [Pandas](https://pandas.pydata.org/)
97100
* [Numba](https://numba.pydata.org/) and
98-
* [JAX](https://github.com/jax-ml/jax)
99101

100102
Here's how they fit together:
101103

@@ -104,14 +106,11 @@ Here's how they fit together:
104106
multiplication).
105107
* SciPy builds on NumPy by adding numerical methods routinely used in science (interpolation, optimization, root finding, etc.).
106108
* Matplotlib is used to generate figures, with a focus on plotting data stored in NumPy arrays.
107-
* Pandas provides types and functions for manipulating data.
108-
* Numba provides a just-in-time compiler that integrates well with NumPy and
109-
helps accelerate Python code.
110109
* JAX includes array processing operations similar to NumPy, automatic
111110
differentiation, a parallelization-centric just-in-time compiler, and automated integration with hardware accelerators such as
112111
GPUs.
113-
114-
112+
* Pandas provides types and functions for manipulating data.
113+
* Numba provides a just-in-time compiler that plays well with NumPy and helps accelerate Python code.
115114

116115

117116
## The Need for Speed
@@ -133,25 +132,27 @@ Indeed, the standard implementation of Python (called CPython) cannot match the
133132

134133
Does that mean that we should just switch to C or Fortran for everything?
135134

136-
The answer is: No, no, and one hundred times no!
137-
138-
(This is what you should say to your professor when they insist that your model needs to be rewritten in Fortran or C++.)
135+
The answer is: No!
139136

140-
There are two reasons why:
137+
There are three reasons why:
141138

142139
First, for any given program, relatively few lines are ever going to be time-critical.
143140

144141
Hence it is far more efficient to write most of our code in a high productivity language like Python.
145142

146143
Second, even for those lines of code that *are* time-critical, we can now achieve the same speed as C or Fortran using Python's scientific libraries.
147144

148-
In fact we can often do better, because some scientific libraries are so
149-
effective at accelerating and parallelizing our code.
145+
Third, in the last few years, accelerating code has become essentially
146+
synonymous with parallelizing execution, and this task is best left to
147+
specialized compilers.
148+
149+
Certain Python libraries have outstanding capabilities for parallelizing
150+
scientific code -- we'll discuss this more as we go along.
150151

151152

152153
### Where are the Bottlenecks?
153154

154-
Before we learn how to do this, let's try to understand why plain vanilla Python is slower than C or Fortran.
155+
Before we do so, let's try to understand why plain vanilla Python is slower than C or Fortran.
155156

156157
This will, in turn, help us figure out how to speed things up.
157158

@@ -275,17 +276,22 @@ Let's look at some ways around these problems.
275276
```{index} single: Python; Vectorization
276277
```
277278

278-
There is a clever method called **vectorization** that can be
279-
used to speed up high level languages in numerical applications.
279+
One method for avoiding memory traffic and type checking is [array programming](https://en.wikipedia.org/wiki/Array_programming).
280+
281+
Economists usually refer to array programming as ``vectorization.''
282+
283+
(In computer science, this term has [a slightly different meaning](https://en.wikipedia.org/wiki/Automatic_vectorization).)
280284

281285
The key idea is to send array processing operations in batch to pre-compiled
282286
and efficient native machine code.
283287

284288
The machine code itself is typically compiled from carefully optimized C or Fortran.
285289

286-
For example, when working in a high level language, the operation of inverting a large matrix can be subcontracted to efficient machine code that is pre-compiled for this purpose and supplied to users as part of a package.
290+
For example, when working in a high level language, the operation of inverting a
291+
large matrix can be subcontracted to efficient machine code that is pre-compiled
292+
for this purpose and supplied to users as part of a package.
287293

288-
This clever idea dates back to MATLAB, which uses vectorization extensively.
294+
This idea dates back to MATLAB, which uses vectorization extensively.
289295

290296

291297
```{figure} /_static/lecture_specific/need_for_speed/matlab.png
@@ -297,30 +303,20 @@ in later lectures.
297303
(numba-p_c_vectorization)=
298304
## Beyond Vectorization
299305

300-
At its best, vectorization yields fast, simple code.
306+
At best, vectorization yields fast, simple code.
301307

302308
However, it's not without disadvantages.
303309

304310
One issue is that it can be highly memory-intensive.
305311

306-
For example, the vectorized maximization routine above is far more memory
307-
intensive than the non-vectorized version that preceded it.
308-
309312
This is because vectorization tends to create many intermediate arrays before
310313
producing the final calculation.
311314

312315
Another issue is that not all algorithms can be vectorized.
313316

314-
In these kinds of settings, we need to go back to loops.
315-
316-
Fortunately, there are alternative ways to speed up Python loops that work in
317-
almost any setting.
318-
319-
For example, [Numba](http://numba.pydata.org/) solves the main problems with
320-
vectorization listed above.
321-
322-
It does so through something called **just in time (JIT) compilation**,
323-
which can generate extremely fast and efficient code.
317+
Because of these issues, most high performance computing is moving away from
318+
traditional vectorization and towards the use of [just-in-time compilers](https://en.wikipedia.org/wiki/Just-in-time_compilation).
324319

325-
{doc}`Later <numba>` we'll learn how to use Numba to accelerate Python code.
320+
In later lectures in this series, we will learn about how modern Python libraries exploit
321+
just-in-time compilers to generate fast, efficient, parallelized machine code.
326322

0 commit comments

Comments
 (0)