You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improve clarity and fix typos in need_for_speed lecture (#523)
Fix typos (oursives, stray "for", missing period, "in" vs "is") and
rewrite the parallelization and hardware accelerator sections for
precision: sharpen multiprocessing vs multithreading distinction, add
concrete examples, explain what a "core" is, and streamline GPU coverage.
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@@ -79,11 +79,11 @@ We need Python's scientific libraries for two reasons:
79
79
1. Python is small
80
80
2. Python is slow
81
81
82
-
**Python in small**
82
+
**Python is small**
83
83
84
84
Core python is small by design -- this helps with optimization, accessibility, and maintenance
85
85
86
-
Scientific libraries provide the routines we don't want to -- and probably shouldn't -- write oursives
86
+
Scientific libraries provide the routines we don't want to -- and probably shouldn't -- write ourselves
87
87
88
88
* numerical integration, interpolation, linear algebra, root finding, etc.
89
89
@@ -127,37 +127,17 @@ Here's how they fit together:
127
127
We will discuss all of these libraries at length in this lecture series.
128
128
129
129
130
-
## Pure Python is slow
130
+
## Why is Pure Python Slow?
131
131
132
-
As mentioned above, one major attraction of the scientific libraries is greater execution speeds.
132
+
As mentioned above, numerical code written in pure Python is relatively slow.
133
133
134
-
We will discuss how scientific libraries can help us accelerate code.
134
+
Let's try to understand what's driving slow execution speeds.
135
135
136
-
For this topic, it will be helpful if we understand what's driving slow execution speeds.
136
+
### Type Checking
137
137
138
+
One source of overhead in pure Python operations is type checking.
138
139
139
-
### High vs low level code
140
-
141
-
Higher-level languages like Python are optimized for humans.
142
-
143
-
This means that the programmer can leave many details to the runtime environment
144
-
145
-
* specifying variable types
146
-
* memory allocation/deallocation
147
-
* etc.
148
-
149
-
In addition, pure Python is run by an [interpreter](https://en.wikipedia.org/wiki/Interpreter_(computing)), which executes code statement-by-statement.
150
-
151
-
This makes Python flexible, interactive, easy to write, easy to read, and relatively easy to debug.
152
-
153
-
On the other hand, the standard implementation of Python (called CPython) cannot
154
-
match the speed of compiled languages such as C or Fortran.
155
-
156
-
157
-
### Where are the bottlenecks?
158
-
159
-
Why is this the case?
160
-
140
+
Let's try to understand the issues.
161
141
162
142
#### Dynamic typing
163
143
@@ -245,7 +225,7 @@ To illustrate, let's consider the problem of summing some data --- say, a collec
245
225
In C or Fortran, an array of integers is stored in a single contiguous block of memory
246
226
247
227
* For example, a 64 bit integer is stored in 8 bytes of memory.
248
-
* An array of $n$ such integers occupies $8n$ *consecutive* memory slots.
228
+
* An array of $n$ such integers occupies $8n$ consecutive bytes.
249
229
250
230
Moreover, the data type is known at compile time.
251
231
@@ -336,7 +316,7 @@ The idea of vectorization dates back to MATLAB, which uses vectorization extensi
336
316
NumPy uses a similar model, inspired by MATLAB
337
317
338
318
339
-
### Vectorization vs for pure Python loops
319
+
### Vectorization vs pure Python loops
340
320
341
321
Let's try a quick speed comparison to illustrate how vectorization can
342
322
accelerate code.
@@ -431,61 +411,45 @@ Let's review the two main kinds of CPU-based parallelization commonly used in
431
411
scientific computing and discuss their pros and cons.
432
412
433
413
434
-
#### Multiprocessing
435
-
436
-
Multiprocessing means concurrent execution of multiple threads of logic using more than one processor.
437
-
438
-
Multiprocessing can be carried out on one machine with multiple CPUs or on a
439
-
cluster of machines connected by a network.
440
-
441
-
With multiprocessing, *each process has its own memory space*, although the physical memory chip might be shared.
414
+
#### Multithreading
442
415
416
+
Multithreading means running multiple threads of execution within a single process.
443
417
444
-
#### Multithreading
418
+
All threads share the same memory space, so they can read from and write to the same arrays without copying data.
445
419
446
-
Multithreading is similar to multiprocessing, except that, during execution, the
447
-
threads all *share the same memory space*.
420
+
For example, when a numerical operation on a large array runs on a modern laptop, the workload can be split across the machine's multiple CPU cores, with each core handling a portion of the array.
448
421
422
+
```{note}
449
423
Native Python struggles to implement multithreading due to some [legacy design
0 commit comments