|
1 | 1 | --- |
2 | | -title: "Understanding Memory" |
| 2 | +title: "Understanding Latency" |
3 | 3 | teaching: 30 |
4 | 4 | exercises: 0 |
5 | 5 | --- |
6 | 6 |
|
7 | 7 | :::::::::::::::::::::::::::::::::::::: questions |
8 | 8 |
|
9 | | -- How does a CPU look for a variable it requires? |
10 | | -- What impact do cache lines have on memory accesses? |
11 | 9 | - Why is it faster to read/write a single 100 MB file, than 100 files of 1 MB each? |
| 10 | +- How many orders of magnitude slower are disk accesses than RAM? |
| 11 | +- What's the cost of creating a list? |
12 | 12 |
|
13 | 13 | :::::::::::::::::::::::::::::::::::::::::::::::: |
14 | 14 |
|
15 | 15 | ::::::::::::::::::::::::::::::::::::: objectives |
16 | 16 |
|
17 | | -- Able to explain, at a high-level, how memory accesses occur during computation and how this impacts optimisation considerations. |
18 | 17 | - Able to identify the relationship between different latencies relevant to software. |
| 18 | +- Demonstrate how to implement parallel network requests. |
| 19 | +- Justify the re-use of existing variables over creating new ones. |
19 | 20 |
|
20 | 21 | :::::::::::::::::::::::::::::::::::::::::::::::: |
21 | 22 |
|
22 | | -## Accessing Variables |
23 | | - |
24 | | -The storage and movement of data plays a large role in the performance of executing software. |
25 | | - |
26 | | -<!-- Brief summary of hardware --> |
27 | | -Modern computer's typically have a single processor (CPU), within this processor there are multiple processing cores each capable of executing different code in parallel. |
28 | | - |
29 | | -Data held in memory by running software is exists in RAM, this memory is faster to access than hard drives (and solid-state drives). |
30 | | -But the CPU has much smaller caches on-board, to make accessing the most recent variables even faster. |
31 | | - |
32 | | -{alt="An annotated photo of inside a desktop computer's case. The CPU, RAM, power supply, graphics cards (GPUs) and harddrive are labelled."} |
33 | | - |
34 | | -<!-- Read/operate on variable ram->cpu cache->registers->cpu --> |
35 | | -When reading a variable, to perform an operation with it, the CPU will first look in its registers. These exist per core, they are the location that computation is actually performed. Accessing them is incredibly fast, but there only exists enough storage for around 32 variables (typical number, e.g. 4 bytes). |
36 | | -As the register file is so small, most variables won't be found and the CPU's caches will be searched. |
37 | | -It will first check the current processing core's L1 (Level 1) cache, this small cache (typically 64 KB per physical core) is the smallest and fastest to access cache on a CPU. |
38 | | -If the variable is not found in the L1 cache, the L2 cache that is shared between multiple cores will be checked. This shared cache, is slower to access but larger than L1 (typically 1-3MB per core). |
39 | | -This process then repeats for the L3 cache which may be shared among all cores of the CPU. This cache again has higher latency to access, but increased size (typically slightly larger than the total L2 cache size). |
40 | | -If the variable has not been found in any of the CPU's cache, the CPU will look to the computer's RAM. This is an order of magnitude slower to access, with several orders of magnitude greater capacity (tens to hundreds of GB are now standard). |
41 | | - |
42 | | -Correspondingly, the earlier the CPU finds the variable the faster it will be to access. |
43 | | -However, to fully understand the cache's it's necessary to explain what happens once a variable has been found. |
44 | | - |
45 | | -If a variable is not found in the caches, it must be fetched from RAM. |
46 | | -The full 64 byte cache line containing the variable, will be copied first into the CPU's L3, then L2 and then L1. |
47 | | -Most variables are only 4 or 8 bytes, so many neighbouring variables are also pulled into the caches. |
48 | | -Similarly, adding new data to a cache evicts old data. |
49 | | -This means that reading 16 integers contiguously stored in memory, should be faster than 16 scattered integers |
50 | | - |
51 | | -Therefore, to **optimally** access variables they should be stored contiguously in memory with related data and worked on whilst they remain in caches. |
52 | | -If you add to a variable, perform large amount of unrelated processing, then add to the variable again it will likely have been evicted from caches and need to be reloaded from slower RAM again. |
53 | | - |
54 | | -<!-- Latency/Throughput typically inversely proportional to capacity --> |
55 | | -It's not necessary to remember this full detail of how memory access work within a computer, but the context perhaps helps understand why memory locality is important. |
56 | | - |
57 | | -{alt='An abstract representation of a CPU, RAM and Disk, showing their internal caches and the pathways data can pass.'} |
58 | | - |
59 | | -::::::::::::::::::::::::::::::::::::: callout |
60 | | - |
61 | | -Python as a programming language, does not give you enough control to carefully pack your variables in this manner (every variable is an object, so it's stored as a pointer that redirects to the actual data stored elsewhere). |
62 | | - |
63 | | -However all is not lost, packages such as `numpy` and `pandas` implemented in C/C++ enable Python users to take advantage of efficient memory accesses (when they are used correctly). |
64 | | - |
65 | | -::::::::::::::::::::::::::::::::::::::::::::: |
66 | | - |
67 | | -<!-- TODO python code example |
68 | | -```python |
69 | | -
|
70 | | -```--> |
71 | 23 |
|
72 | 24 | ## Accessing Disk |
73 | 25 |
|
74 | 26 | <!-- Read data from a file it goes disk->disk cache->ram->cpu cache/s->cpu --> |
75 | | -When accessing data on disk (or network), a very similar process is performed to that between CPU and RAM when accessing variables. |
| 27 | +When reading data from a file, it is first transferred from the disk to the disk cache and then to the RAM (the computer's main memory, where variables are stored). |
| 28 | +The latency to access files on disk is another order of magnitude higher than accessing normal variables. |
76 | 29 |
|
77 | | -When reading data from a file, it transferred from the disk, to the disk cache, to the RAM. |
78 | | -The latency to access files on disk is another order of magnitude higher than accessing RAM. |
79 | | - |
80 | | -As such, disk accesses similarly benefit from sequential accesses and reading larger blocks together rather than single variables. |
| 30 | +As such, disk accesses benefit from sequential accesses and reading larger blocks together rather than single variables. |
81 | 31 | Python's `io` package is already buffered, so automatically handles this for you in the background. |
82 | 32 |
|
83 | 33 | However before a file can be read, the file system on the disk must be polled to transform the file path to its address on disk to initiate the transfer (or throw an exception). |
@@ -158,7 +108,7 @@ An even greater overhead would apply. |
158 | 108 |
|
159 | 109 | ## Accessing the Network |
160 | 110 |
|
161 | | -When transfering files over a network, similar effects apply. There is a fixed overhead for every file transfer (no matter how big the file), so downloading many small files will be slower than downloading a single large file of the same total size. |
| 111 | +When transferring files over a network, similar effects apply. There is a fixed overhead for every file transfer (no matter how big the file), so downloading many small files will be slower than downloading a single large file of the same total size. |
162 | 112 |
|
163 | 113 | Because of this overhead, downloading many small files often does not use all the available bandwidth. It may be possible to speed things up by parallelising downloads. |
164 | 114 |
|
@@ -227,7 +177,9 @@ Latency can have a big impact on the speed that a program executes, the below gr |
227 | 177 |
|
228 | 178 | {alt="A horizontal bar chart displaying the relative latencies for L1/L2/L3 cache, RAM, SSD, HDD and a packet being sent from London to California and back. These latencies range from 1 nanosecond to 140 milliseconds and are displayed with a log scale."} |
229 | 179 |
|
230 | | -The lower the latency typically the higher the effective bandwidth (L1 and L2 cache have 1 TB/s, RAM 100 GB/s, SSDs up to 32 GB/s, HDDs up to 150 MB/s), making large memory transactions even slower. |
| 180 | +L1/L2/L3 caches are where your most recently accessed variables are stored inside the CPU, whereas RAM is where most of your variables will be found. |
| 181 | + |
| 182 | +The lower the latency typically the higher the effective bandwidth (L1 and L2 cache have 1 TB/s, RAM 100 GB/s, SSDs up to 32 GB/s, HDDs up to 150 MB/s), making large memory transactions even slower. |
231 | 183 |
|
232 | 184 | ## Memory Allocation is not Free |
233 | 185 |
|
@@ -335,9 +287,8 @@ Line # Hits Time Per Hit % Time Line Contents |
335 | 287 |
|
336 | 288 | ::::::::::::::::::::::::::::::::::::: keypoints |
337 | 289 |
|
338 | | -- Sequential accesses to memory (RAM or disk) will be faster than random or scattered accesses. |
339 | | - - This is not always natively possible in Python without the use of packages such as NumPy and Pandas |
340 | 290 | - One large file is preferable to many small files. |
| 291 | +- Network requests can be parallelised to reduce the impact of fixed overheads. |
341 | 292 | - Memory allocation is not free, avoiding destroying and recreating objects can improve performance. |
342 | 293 |
|
343 | 294 | :::::::::::::::::::::::::::::::::::::::::::::::: |
0 commit comments