dockerfile/examples/omnivore/content-fetch/readabilityjs/test/test-pages/erik-engheim/distiller.html

265 lines
51 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div><img alt="Erik Engheim" src="https://miro.medium.com/fit/c/56/56/0*1Y9ylHZ8csOxgZr7.jpg"/><h4>
<span>Nov 28, 2020<span>·</span>23 min read</span>
</h4><figure><img alt="Image for post" src="https://miro.medium.com/max/5760/1*thz7gKafCYxWoA0h8aGjcg.jpeg" srcset="https://miro.medium.com/max/552/1*thz7gKafCYxWoA0h8aGjcg.jpeg 276w, https://miro.medium.com/max/1104/1*thz7gKafCYxWoA0h8aGjcg.jpeg 552w, https://miro.medium.com/max/1280/1*thz7gKafCYxWoA0h8aGjcg.jpeg 640w, https://miro.medium.com/max/1400/1*thz7gKafCYxWoA0h8aGjcg.jpeg 700w" sizes="700px"/><figcaption>Image: Apple</figcaption></figure><p>
<span>On</span> YouTube, I watched a Mac user who had bought an iMac last year. It was maxed out with 40 GB of RAM costing him about $4,000. He watched in disbelief how his hyperexpensive iMac was being demolished by his new M1 Mac Mini, which he had paid a measly $700 for.
</p><p>
In real-world test after test, the M1 Macs are not merely inching past top-of-the-line Intel Macs, they are destroying them. In disbelief, people have started asking how on earth this is possible?
</p><p>
If you are one o<span><span><span><span><span><span><span><span><span>f</span></span></span></span></span></span></span></span></span> those people, you have come to the right place. Here I plan to break it down into digestible pieces exactly what it is that Apple has done with the M1. Specifically the questions I think a lot of people have are:
</p><ol><li>What are the technical reasons this M1 chip is so fast?</li><li>Has Apple made some really exotic technical choices to make this possible?</li><li>How easy will it be for the competition such as Intel and AMD to pull the same technical tricks?</li></ol><p>
Sure you could try to Google this, but if you try to learn what Apple has done beyond the superficial explanations, you will quickly get buried in highly technical jargon such as M1 using very wide instruction decoders, enormous reorder buffer (ROB), etc. Unless you are a CPU hardware geek, a lot of this will simply be gobbledygook.
</p><p>
To get the most out of this story I advise reading my earlier piece: “<a href="https://medium.com/swlh/what-does-risc-and-cisc-mean-in-2020-7b4d42c9a9de" target="_blank" rel="noopener">What Does RISC and CISC mean in 2020</a>?” There I explain what a microprocessor (CPU) is as well as various important concepts such as:
</p><ul><li>Instruction set architecture (ISA)</li><li>Pipelining</li><li>Load/store architecture</li><li>Microcode vs. micro-operations</li></ul><p>
But if you are impatient, I will do a quick version of the material you need to understand to grasp my explanation of the M1 chip.
</p><h1>
What is a microprocessor (CPU)?
</h1><p>
Normally when speaking of chips from Intel and AMD we talk about central processing units (CPUs) or microprocessors. As you can read more about in my <a href="https://medium.com/swlh/what-does-risc-and-cisc-mean-in-2020-7b4d42c9a9de" target="_blank" rel="noopener">RISC vs. CISC story</a>, these pull in instructions from memory. Then each instruction is typically carried out in sequence.
</p><figure><img alt="A very basic RISC CPU, not the M1" src="https://miro.medium.com/proxy/1*3G7uz4l1GnFacxz6InrLLw.png"/><figcaption>A very basic RISC CPU, not the M1. Instructions are moved from memory along blue arrows into the instruction register. There a decoder figures out what the instruction is and enables different parts of the CPU through the red control lines. The ALU adds and subtracts numbers placed in the registers.</figcaption></figure><p>
A CPU at its most basic level is a device with a number of named memory cells called registers and a number of computational units called arithmetic logic units (ALU). The ALUs perform things like addition, subtraction, and other basic math operations. However, these are only connected to the CPU registers. If you want to add up two numbers, you have to get those two numbers from memory and into two registers in the CPU.
</p><p>
Here are some examples of typical instructions that a RISC CPU as found on the M1 carries out.
</p><pre><span>load r1, 150<br/>load r2, 200<br/>add r1, r2<br/>store r1, 310</span></pre><p>
Here <code>r1</code> and <code>r2</code> are the registers I talked about. Modern RISC CPUs cannot do operations on numbers that are not in a register like this. For example, it cannot add two numbers residing in RAM in two different locations. Instead, it has to pull these two numbers into a separate register. That is what we do in this simple example. We pull in the number at memory location 150 in the RAM and put it into register <code>r1</code> in the CPU. Next, we put the contents of address 200 into register <code>r2</code>. Only then can the numbers be added with the <code>add r1, r2</code> instruction.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/800/1*kDqSqtumOzNFZdpixUW0IQ.jpeg" srcset="https://miro.medium.com/max/552/1*kDqSqtumOzNFZdpixUW0IQ.jpeg 276w, https://miro.medium.com/max/800/1*kDqSqtumOzNFZdpixUW0IQ.jpeg 400w" sizes="400px"/><figcaption>An old mechanical calculator with two registers: the accumulator and input register. Modern CPUs typically have more than a dozen registers, and they are electronic rather than mechanical.</figcaption></figure><p>
The concept of registers is old. For example, on this old mechanical calculator, the <em>register</em> is what holds the numbers you are adding. Likely the origin of the term <em>cash register</em>. The register is where you registered input numbers.
</p><h1>
The M1 is not a CPU!
</h1><p>
But here is a very important thing to understand about the M1:
</p><p>
The M1 is not a CPU, it is a whole system of multiple chips put into one large silicon package. The CPU is just one of these chips.
</p><p>
Basically, the M1 is one whole computer onto a chip. The M1 contains a CPU, graphical processing unit (GPU), memory, input and output controllers, and many more things making up a whole computer. This is what we call a system on a chip (SoC).
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/2400/1*c4EYUAVj4k7n6wWLoWUVdA.png" srcset="https://miro.medium.com/max/552/1*c4EYUAVj4k7n6wWLoWUVdA.png 276w, https://miro.medium.com/max/1104/1*c4EYUAVj4k7n6wWLoWUVdA.png 552w, https://miro.medium.com/max/1280/1*c4EYUAVj4k7n6wWLoWUVdA.png 640w, https://miro.medium.com/max/1400/1*c4EYUAVj4k7n6wWLoWUVdA.png 700w" sizes="700px"/><figcaption>M1 is a system on a chip. Meaning all the parts making up a computer are placed on one silicon chip.</figcaption></figure><p>
Today if you buy a chip — whether from Intel or AMD — you actually get what amounts to <em>multiple</em> microprocessors in one package. In the past computers would have multiple physically separate chips on the motherboard of the computer.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/1200/1*GBrG4D2YCEVYREXAHIiJnQ.png" srcset="https://miro.medium.com/max/552/1*GBrG4D2YCEVYREXAHIiJnQ.png 276w, https://miro.medium.com/max/1104/1*GBrG4D2YCEVYREXAHIiJnQ.png 552w, https://miro.medium.com/max/1200/1*GBrG4D2YCEVYREXAHIiJnQ.png 600w" sizes="600px"/><figcaption>Example of a computer motherboard. Memory, CPU, graphics cards, IO controllers, network cards, and many other components can be attached to the motherboard to communicate with each other.</figcaption></figure><p>
However because we are able to put so many transistors on a silicon die today, companies such as Intel and AMD began putting multiple microprocessors onto one chip. Today we refer to these chips as CPU cores. One core is basically a full independent chip that can read instructions from memory and perform calculations.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/850/1*XZsBJV_v4WybnUFYjJJiPQ.gif" srcset="https://miro.medium.com/max/552/1*XZsBJV_v4WybnUFYjJJiPQ.gif 276w, https://miro.medium.com/max/850/1*XZsBJV_v4WybnUFYjJJiPQ.gif 425w" sizes="425px"/><figcaption>A microchip with multiple CPU cores.</figcaption></figure><p>
This has for a long time been the name of the game in terms of increasing performance: Just add more general-purpose CPU cores. But there is a disturbance in the force. There is one player in the CPU market which is deviating from this trend.
</p><h2>
Apples not so secret heterogeneous computing strategy
</h2><p>
Instead of adding ever more general-purpose CPU cores, Apple has followed another strategy: They have started adding ever more specialized chips doing a few specialized tasks. The benefit of this is that specialized chips tend to be able to perform their tasks significantly faster using much less electric current than a general-purpose CPU core.
</p><p>
This is not entirely new knowledge. For many years already specialized chips such as the graphical processing units (GPUs) have been sitting in Nvidia and AMD graphics cards performing operations related to graphics much faster than general-purpose CPUs.
</p><p>
What Apple has done is simply to take a more radical shift toward this direction. Rather than just having general-purpose cores and memory, the M1 contains a wide variety of specialized chips:
</p><ul><li>Central processing unit (CPU) — the “brains” of the SoC. Runs most of the code of the operating system and your apps.</li><li>Graphics processing unit (GPU) — handles graphics-related tasks, such as visualizing an apps user interface and 2D/3D gaming.</li><li>Image processing unit (ISP) — can be used to speed up common tasks done by image processing applications.</li><li>Digital signal processor (DSP) — handles more mathematically intensive functions than a CPU. Includes decompressing music files.</li><li>Neural processing unit (NPU) — used in high-end smartphones to accelerate machine learning (A.I.) tasks. These include voice recognition and camera processing.</li><li>Video encoder/decoder — handles the power-efficient conversion of video files and formats.</li><li>Secure Enclave — encryption, authentication, and security.</li><li>Unified memory — allows the CPU, GPU, and other cores to quickly exchange information.</li></ul><p>
This is part of the reason why a lot of people working on images and video editing with the M1 Macs are seeing such speed improvements. A lot of the tasks they do can run directly on specialized hardware. That is what allows a cheap M1 Mac Mini to encode a large video file without breaking a sweat while an expensive iMac has all its fans going full blast and still cannot keep up.
</p><h2>
What is Special About Apples Unified Memory Architecture?
</h2><p>
Apples “Unified Memory Architecture” (UMA) is a bit tricky to wrap your head around (I got it wrong first time I wrote it down here).
</p><p>
To explain why, we need to take a few steps back.
</p><p>
For a long time cheap computer systems have had the CPU and GPU integrated into the same chip (same silicon die). These have been famously slow. In the past saying “integrated graphics” was essentially the same as saying “slow graphics.”
</p><p>
These where slow for severals reasons:
</p><p>
Separate areas of this memory got reserved for the CPU and GPU. If the CPU had a chunk of data it wanted the GPU to use, it couldnt say “here have some of my memory.” No, the CPU had to explicitly copy the whole chunk of data over the memory area controlled by the GPU.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/1280/1*7c67HhuDWVFM3Pi-mO5boA.jpeg" srcset="https://miro.medium.com/max/552/1*7c67HhuDWVFM3Pi-mO5boA.jpeg 276w, https://miro.medium.com/max/1104/1*7c67HhuDWVFM3Pi-mO5boA.jpeg 552w, https://miro.medium.com/max/1280/1*7c67HhuDWVFM3Pi-mO5boA.jpeg 640w" sizes="640px"/><figcaption>CPUs dont need a lot of data served, but they want it fast.</figcaption></figure><p>
CPUs and GPUs dont want their memory served the same way. Let us do a silly food analogy: CPUs want their plate of data served very quickly by the waiter, but they are totally cool with small portion sizes. Imagine a fancy French restaurant with waiters on rollerblades to serve you really quickly.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/1100/1*ethYaJsPETw2zxF7Xm0Z0w.jpeg" srcset="https://miro.medium.com/max/552/1*ethYaJsPETw2zxF7Xm0Z0w.jpeg 276w, https://miro.medium.com/max/1100/1*ethYaJsPETw2zxF7Xm0Z0w.jpeg 550w" sizes="550px"/><figcaption>This is how your GPU wants their memory: huge portions. The more the merrier.</figcaption></figure><p>
GPUs in contrast are cool with the waiter being slow to serve the data. But the GPUs want enormous servings. They gobble massive amounts of data because they are massive parallel machines, that can chew through lots of data in parallel. Imagine an American junk food place, where the food takes some time to arrive because they are pushing a whole trolley of food to your seating area.
</p><p>
With such different needs, putting CPUs and GPUs on the same physical chip was not a great idea. The GPUs would sit there starving while given small French servings. The result was that there was no point in putting powerful GPUs on an SoC. The tiny portions of data served up, could easily be chewed up by a weak little GPU.
</p><p>
The second problem was that large GPUs produce a lot of heat and thus you cannot integrate them with the CPU without getting problems ridding yourself of the heat produced. Thus discrete graphics cards tend to look like the one below: Large beasts with massive cooling fans. They have special dedicated memory designed to serve the greedy cards massive amounts of data.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/778/1*gQcbAlaUXNtjJi6OAUzMsg.jpeg" srcset="https://miro.medium.com/max/552/1*gQcbAlaUXNtjJi6OAUzMsg.jpeg 276w, https://miro.medium.com/max/778/1*gQcbAlaUXNtjJi6OAUzMsg.jpeg 389w" sizes="389px"/><figcaption>GeForce RTX 3080</figcaption></figure><p>
That is why these cards have high performance. But they have an achilles heel: Whenever they have to get data from the memory used by the CPU, this happens over a set of copper traces on the computer motherboard called a PCIe bus. Try chugging water through a super thin straw. It may get to your mouth fast, but the throughput is totally inadequate.
</p><p>
Apples <em>Unified Memory Architecture</em> tries to solve all these problems without having the disadvantages of old school shared memory. They achieve this in the following ways:
</p><ol><li>There is no special area reserved just for the CPU or just the GPU. Memory is allocated to both processors. They can both use the same memory. No copying is needed.</li><li>Apple uses memory which serves both large chunks of data and serves it fast. In computer speak that is called low latency and high throughput. Thus the need to be connected to separate types of memory is removed.</li><li>Apple has gotten the watt usage of the GPU down, so that a relatively powerful GPU can be integrated without overheating the SoC. And ARM chips produce less heat, allowing the GPU to have a higher heat budget than a GPU on the same silicon die as an AMD or Intel CPU.</li></ol><p>
Some will say unified memory is not entirely new. It is true that different systems have had it in the past. But then the difference in memory requirements may not have been as large. Secondly what Nvidia calls Unified Memory is not really the same thing. In the Nvidea world Unified Memory simply means that there is software and hardware which takes care of automatically copying data back and forth between the separate CPU and GPU memory. Thus from a programmers perspective Apple and Nvidia Unified Memory may look the same, but it is not the same in a physical sense.
</p><p>
There is of course a tradeoff in this strategy. Getting this high bandwidth memory (big servings) require full integration which means you take away the opportunity from customers to upgrade their memory. But Apple seeks to minimize this problem by making the communication with the SSD disks so fast, that they essentially work like old fashion memory.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/1668/1*IJFHpc1CrblUt09PSzaTyg.png" srcset="https://miro.medium.com/max/552/1*IJFHpc1CrblUt09PSzaTyg.png 276w, https://miro.medium.com/max/1104/1*IJFHpc1CrblUt09PSzaTyg.png 552w, https://miro.medium.com/max/1280/1*IJFHpc1CrblUt09PSzaTyg.png 640w, https://miro.medium.com/max/1400/1*IJFHpc1CrblUt09PSzaTyg.png 700w" sizes="700px"/><figcaption>How Macs used GPUs before unified memory. There was even an option of having graphics cards outside the computer using a Thunderbolt 3 cable. There is some speculation that this may still be possible in the future.</figcaption></figure><h2>
If SoCs Are So Smart, Why Dont Intel and AMD Copy This Strategy?
</h2><p>
If what Apple is doing is so smart, why is not everybody doing it? To some extent they are. Other ARM chip makers are increasingly putting in specialized hardware.
</p><p>
AMD has also started putting stronger GPUs on some of their chips and moving gradually toward some form of SoC with the accelerated processing units (APU) which are basically CPU cores and GPU cores placed on the same silicon die.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/1800/1*vutPH0zsrwSzGgFWomL61A.jpeg" srcset="https://miro.medium.com/max/552/1*vutPH0zsrwSzGgFWomL61A.jpeg 276w, https://miro.medium.com/max/1104/1*vutPH0zsrwSzGgFWomL61A.jpeg 552w, https://miro.medium.com/max/1280/1*vutPH0zsrwSzGgFWomL61A.jpeg 640w, https://miro.medium.com/max/1400/1*vutPH0zsrwSzGgFWomL61A.jpeg 700w" sizes="700px"/><figcaption>AMD Ryzen Accelerated Processing Unit (APU) which combines CPU and GPU (Radeon Vega) on one silicon chip. Does however not contain other co-processors, IO-controllers, or unified memory.</figcaption></figure><p>
Yet there are important reasons why they cannot do this. An SoC is essentially a whole computer on a chip. That makes it a more natural fit for an actual computer-maker, such as HP and Dell. Let me clarify with a silly car analogy: If your business model is to build and sell car engines, it would be an unusual leap to begin manufacturing and selling whole cars.
</p><p>
For ARM, in contrast, this isnt an issue. Computer makers such as Dell or HP could simply license ARM intellectual property and buy IP for other chips, to add whatever specialized hardware they think their SoC should have. Next, they ship the finished design over to a semiconductor foundry such as <a href="https://en.wikipedia.org/wiki/GlobalFoundries" target="_blank" rel="noopener nofollow">GlobalFoundries</a> or <a href="https://www.tsmc.com/english" target="_blank" rel="noopener nofollow">TSMC</a>, which manufactures chips for AMD and Apple today.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/1496/1*d88lw8YMonKgdDWQLC1jXQ.jpeg" srcset="https://miro.medium.com/max/552/1*d88lw8YMonKgdDWQLC1jXQ.jpeg 276w, https://miro.medium.com/max/1104/1*d88lw8YMonKgdDWQLC1jXQ.jpeg 552w, https://miro.medium.com/max/1280/1*d88lw8YMonKgdDWQLC1jXQ.jpeg 640w, https://miro.medium.com/max/1400/1*d88lw8YMonKgdDWQLC1jXQ.jpeg 700w" sizes="700px"/><figcaption>TSMC semiconductor foundry in Taiwan. TSMC manufactures chips for other companies such as AMD, Apple, Nvidia, and Qualcomm.</figcaption></figure><p>
Here we get a big problem with the Intel and AMD business model. Their business models are based on selling general-purpose CPUs, which people just slot onto a large PC motherboard. Thus computer-makers can simply buy motherboards, memory, CPUs, and graphics cards from different vendors and integrate them into one solution.
</p><p>
But we are quickly moving away from that world. In the new SoC world, you dont assemble physical components from different vendors. Instead, you assemble IP (intellectual property) from different vendors. You buy the design for graphics cards, CPUs, modems, IO controllers, and other things from different vendors and use that to design an SoC in-house. Then you get a foundry to manufacture this.
</p><p>
Now you got a big problem, because neither Intel, AMD, or Nvidia are going to license their intellectual property to Dell or HP for them to make an SoC for their machines.
</p><p>
Sure Intel and AMD may simply begin to sell whole finished SoCs. But what are these to contain? PC-makers may have different ideas of what they should contain. You potentially get a conflict between Intel, AMD, Microsoft, and PC-makers about what sort of specialized chips should be included because these will need software support.
</p><p>
For Apple this is simple. They control the whole widget. They give you, for example, the Core ML library for developers to write <a href="https://developer.apple.com/machine-learning/" target="_blank" rel="noopener nofollow">machine learning</a> stuff. Whether Core ML runs on Apples CPU or the Neural Engine is an implementation detail developers dont have to care about.
</p><h2>
The fundamental challenge of making any CPU run fast
</h2><p>
So heterogeneous computing is part of the reason but not the sole reason. The fast general-purpose CPU cores on the M1, called Firestorm, are genuinely fast. This is a major deviation from ARM CPU cores in the past which tended to be very weak compared to AMD and Intel cores.
</p><p>
Firestorm, in contrast, beats most Intel cores and almost beats the fastest AMD Ryzen cores. Conventional wisdom said that was not going to happen.
</p><p>
Before talking about what makes Firestorm fast it helps to understand what the core idea of making a fast CPU is really about.
</p><p>
In principle you accomplish in a combination of two strategies:
</p><ol><li>Perform more instructions in a sequence faster.</li><li>Perform lots of instructions in parallel.</li></ol><p>
Back in the 80s, it was easy. Just increase the clock frequency and the instructions would finish faster. Every clock cycle is when the computer does something. But this <em>something</em> can be quite little. Thus an instruction may require multiple clock cycles to finish because it is made up of several smaller tasks.
</p><p>
However, today increasing the clock frequency is next to impossible. That is the whole “End of Moores Law” that people have been harping on for over a decade now.
</p><p>
Thus it is really about executing as many instructions as possible in parallel.
</p><h2>
Multi-core or Out-of-Order processors?
</h2><p>
There are two approaches to this.
</p><ul><li>Add more CPU cores. Each core works independent and in parallel.</li><li>Make each CPU core execute multiple instructions in parallel.</li></ul><p>
For a software developer, adding cores is like adding <em>threads</em>. Every CPU core is like a hardware thread.
</p><p>
If you dont know what a thread is, then you can think of it as the process of carrying out a task. With two cores, a CPU can carry out two separate tasks concurrently: two threads. The tasks could be described as two separate programs stores in memory or it could actually be the same program performed twice. Each thread needs some bookkeeping, such as <em>where</em> in a sequence of program instructions the thread is currently at. Each thread may store temporary results which should be kept separate.
</p><p>
In principle, a processor can have just one core and run multiple threads. In this case, it simply halts one thread and stores current progress before switching to another. Later it switches back. This doesnt bring much of a performance enhancement unless the thread has to frequently halt to:
</p><ul><li>Wait for input from the user</li><li>Data from a slow network connection, etc.</li></ul><p>
Let us call these software threads. Hardware threads mean you have actual physical CPU cores at your disposal to speed up things.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/1200/1*2mDUgCX9a49EldiCTbL6fA.png" srcset="https://miro.medium.com/max/552/1*2mDUgCX9a49EldiCTbL6fA.png 276w, https://miro.medium.com/max/1104/1*2mDUgCX9a49EldiCTbL6fA.png 552w, https://miro.medium.com/max/1200/1*2mDUgCX9a49EldiCTbL6fA.png 600w" sizes="600px"/></figure><p>
The problem with threads is that software developers have to write so called multi-threaded code. That is often difficult. In the past, this was some of the hardest code to write. However making <em>server software</em> multi-threaded tends to be easy. It is simply a matter of handling each user request on a separate thread. Thus in this case having lots of cores, is an obvious advantage. Especially for cloud services.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/716/1*X5ZTLTHUYBdw0t25TemHcQ.png" srcset="https://miro.medium.com/max/552/1*X5ZTLTHUYBdw0t25TemHcQ.png 276w, https://miro.medium.com/max/716/1*X5ZTLTHUYBdw0t25TemHcQ.png 358w" sizes="358px"/><figcaption>The Ampere Altra Max ARM CPU with 128 cores designed for cloud computing, where a lot of hardware threads is a benefit.</figcaption></figure><p>
That is the reason why you see ARM CPU-makers such as Ampere making CPUs such as the <a href="https://www.networkworld.com/article/3564514/ampere-announces-128-core-arm-server-processor.html" target="_blank" rel="noopener nofollow">Altra Max</a> which has a crazy 128 cores. This chip is specifically made for the cloud. You dont need crazy single-core performance, because in the cloud it is all about having as many threads as possible per watt to handle as many concurrent users as possible.
</p><p>
Read more about ARM CPUs with many cores: <a href="https://erik-engheim.medium.com/are-servers-next-for-apple-7992213d72c9" target="_blank" rel="noopener">Are Servers Next for Apple?</a>
</p><p>
Apple, in contrast, is on the complete opposite end of the spectrum. They make single-user devices. Lots of threads is not an advantage. Their devices are used for gaming, video editing, development, etc. They want desktops with beautiful responsive graphics and animations.
</p><p>
Desktop software is generally not made to utilize lots of cores. For example, computer games will likely benefit from eight cores, but something like 128 cores would be a total waste. Instead, you would want fewer but more powerful cores.
</p><h2>
How Out-of-Order Execution Works
</h2><p>
To make a more powerful core we need it to execute more instructions in parallel. <a href="https://en.wikipedia.org/wiki/Out-of-order_execution" target="_blank" rel="noopener nofollow">Out-of-Order execution</a> (OoOE) is a way to execute more instructions in parallel but without exposing that capability as multiple threads.
</p><p>
For an alternative solution read: <a href="https://erik-engheim.medium.com/very-long-instruction-word-microprocessors-17262def3037" target="_blank" rel="noopener">Very Long Instruction Word Microprocessors</a>
</p><p>
Developers dont have to code their software specifically to take advantage of OoOE. Seen from the developers perspective it just looks like each core runs faster. Please note it is not a direct alternative to hardware threads. You want to use both, depending on the particular problem you are solving.
</p><p>
To understand how OoOE works, you need to understand some things about memory. Asking for data in one particular memory location is slow. But the CPU is capable of getting getting many bytes at the same time. Hence getting 1 specific byte in memory, takes no less time than getting 100 more bytes following that byte in memory.
</p><figure><img alt="Image for post" src="https://miro.medium.com/max/1980/1*IbuGfelqOOtcyX8NwTh-tw.jpeg" srcset="https://miro.medium.com/max/552/1*IbuGfelqOOtcyX8NwTh-tw.jpeg 276w, https://miro.medium.com/max/1104/1*IbuGfelqOOtcyX8NwTh-tw.jpeg 552w, https://miro.medium.com/max/1280/1*IbuGfelqOOtcyX8NwTh-tw.jpeg 640w, https://miro.medium.com/max/1400/1*IbuGfelqOOtcyX8NwTh-tw.jpeg 700w" sizes="700px"/><figcaption>Robot pickers in a Warehouse for Komplett.no, an online store in Norway</figcaption></figure><p>
Here is an analogy: Consider pickers in a warehouse. Could be the little red robots in the picture above. Moving to multiple locations spread all over takes time. But picking up items from slots adjacent to each other is quick. Computer memory is very similar. You can quickly fetch content of memory cells which are adjecent.
</p><p>
Data is sent across what we call a databus. You can think of it as a road or pipe between memory and different parts of the CPU where data gets pushed through. In reality, it is of course just some copper tracks conducting electricity. If the databus is wide enough you can get multiple bytes at the same time.
</p><p>
Thus CPUs get a whole chunk of instructions at a time to execute. But they are written to be executed one after the other. Modern microprocessors do what we call Out-of-Order execution (OoOE).
</p><p>
That means they are able to analyze a buffer of instructions quickly and see which ones depend on which. Look at the simple example below:
</p><pre><span>01: mul r1, r2, r3 // r1 ← r2 × r3<br/>02: add r4, r1, 5 // r4 ← r1 + 5<br/>03: add r6, r2, 1 // r6 ← r2 + 1</span></pre><p>
Multiplication tends to be a slow process. So say it takes multiple clock cycles to perform. The second instruction will simply have to wait because its calculation depends on knowing the result that gets put into the <code>r1</code> register.
</p><p>
However, the third instruction at line <code>03</code> doesnt depend on calculations from previous instructions. Hence an Out-of-Order processor can begin calculating this instruction in parallel.
</p><p>
However more realistically we are talking about hundreds of instructions. The CPU is able to figure out all the dependencies between these instructions.
</p><p>
It analyses the instructions by looking at the inputs to each instruction. Do the inputs depend on output from one or more other instructions? By input and output, we mean registers containing results from previous calculations.
</p><p>
For example, the <code>add r4, r1, 5</code> instruction depends on input from <code>r1</code> which is produced by <code>mul r1, r2, r3</code> . We can chain together these relationships into long elaborate graphs that the CPU can work through. The nodes are the instructions and the edges are the registers connecting them.
</p><p>
The CPU can analyze such a graph of nodes and determine which instructions it can perform in parallel and where it needs to wait for the results from multiple dependent calculations before carrying on.
</p><p>
Many instructions will finish early but we cannot make their results official. We cannot commit them; otherwise, we supply the result in the wrong order. To the rest of the world, it has to look as if the instructions were carried out in the same sequence as they were issued.
</p><p>
Like a stack, the CPU will keep popping done instructions from the top, until hitting an instruction that is not done.
</p><p>
Basically you got two forms of parallelism: One that the developer must deal with explicitly when writing code and one that is entirely transparent. Of course the latter relies on lots of transistors on the CPU dedicated to Out-of-Order Execution magic. This is not a viable solution for small CPUs with few transistors.
</p><p>
It is the superior Out-of-Order execution that is making the Firestorm cores on the M1 kick ass and take names. It is in fact much stronger than anything from Intel or AMD and they may never be able to catch up. To understand why, we need to get into some more technical details.
</p><h2>
ISA Instructions vs Micro-Operations
</h2><p>
Previously I skipped some details on how Out-of-Order Execution (OoOE) works.
</p><p>
Programs loaded into memory are made up of machine code instructions designed for specific Instruction-Set Architectures (ISA) such as x86, ARM, PowerPC, 68K, MIPS, AVR etc.
</p><p>
For instance the x86 instruction to fetch a number from memory location 24 into a register you may write:
</p><pre><span>MOV ax, 24</span></pre><p>
x86 have registers named <code>ax</code>, <code>bx</code>, <code>cx</code> and <code>dx</code>(remember these are the memory cells inside the CPU you perform operations on). However the equivalent ARM instruction would look like this:
</p><pre><span>LDR r0, 24</span></pre><p>
AMD and Intel processors understand the x86 ISA, while Apple Silicon chips, such as M1, understand the ARM Instruction-Set Architecture (ISA).
</p><p>
However internally the CPU works on an entirely different instruction-set invisible to the programmer. We call these micro-operations (micro-ops or μops). These are the instructions the Out-of-Order hardware works with.
</p><p>
But why cant the OoOE hardware work with regular machine code instructions? Because the CPU needs to attach lots of different information to the instructions to be able to run them in parallel.
</p><p>
Thus while a normal ARM instruction may be 32-bit (32 digits of 0 and 1), a micro-op can be much longer. It contains information about its order.
</p><pre><span>01: mul r1, r2, r3 // r1 ← r2 × r3<br/>02: add r4, r1, 5 // r4 ← r1 + 5<br/>03: add r1, r2, 1 // r1 ← r2 + 1</span></pre><p>
Consider if we run instruction <code>01: mul</code> and <code>03: add</code> in parallel. Both store their result in register <code>r1</code> . If we write the result of instruction <code>03: add</code> before <code>01: mul</code>, then instruction <code>02: add</code> will get the wrong input. Hence it is very important to keep track of instruction order. The order is stored with each micro-op. It also stores e.g. that instruction <code>02: add</code> depends on output from <code>01: mul</code>.
</p><p>
That is why we cannot have programs written using micro-ops. They contain lots of details specific to the internals of each microprocessor. Two ARM processors could have very different micro-ops internally.
</p><p>
Read more about CPUs with micro-ops like instructions: <a href="https://erik-engheim.medium.com/very-long-instruction-word-microprocessors-17262def3037" target="_blank" rel="noopener">Very Long Instruction Word Microprocessors.</a>
</p><p>
Also, micro-ops are usually easier to work with for the CPU. Why? Because they each do <em>one</em> simple limited task. Regular ISA instructions can be more complex causing a bunch of stuff to happen and thus frequently translate to multiple micro-ops. Thus the name “micro” comes from the small task they do, not the length of the instruction in memory.
</p><p>
For CISC CPUs there is usually no alternative but to use micro-ops otherwise the large complex CISC instructions would make pipelines and OoOE next to impossible to achieve.
</p><p>
RISC CPUs have a choice. So, for example, smaller ARM CPUs dont use micro-ops at all. But that also means they cannot do things such as OoOE.
</p><h2>
Why is AMD and Intel Out-of-Order execution inferior to M1?
</h2><p>
But you wonder, why does any of this matter? Why is this detail important to know to understand why Apple has the upper hand on AMD and Intel?
</p><p>
It is because the ability to run fast depends on how quickly you can fill up a buffer of micro-operations. If you got a large buffer then the OoOE hardware will have an easier time to locate two or more instructions which it can run in parallel. But there is no point in having a large instruction buffer if you cannot refill it fast enough after instructions get picked and executed.
</p><p>
The ability to refill the instruction buffer quickly relies on the ability to quickly chop machine code instruction into micro-ops. The hardware units that does this are called <em>decoders</em>.
</p><p>
And finally we get to the killer feature of the M1. The biggest and meanest Intel and AMD microprocessor have a total of four decoders busy cutting machine code instructions into micro-ops.
</p><p>
But this is no match for the M1, which has an absolutely unheard of number of decoders: Eight. Significantly more than anybody else in the industry. That means it can fill up the instruction buffer much quicker.
</p><p>
To deal with this the M1 also has an instruction buffer which is 3x times larger than what is normal in the industry.
</p><h2>
Why cant Intel and AMD add more instruction decoders?
</h2><p>
This is where we finally see the revenge of RISC, and where the fact that the M1 Firestorm core has an ARM RISC architecture begins to matter.
</p><p>
You see, an x86 instruction can be anywhere from 115 bytes long. RISC instructions have fixed length. Every ARM instruction is 4 bytes long. Why is that relevant in this case?
</p><p>
Because splitting up a stream of bytes into instructions to feed into eight different decoders in parallel becomes trivial if every instruction has the same length.
</p><p>
However, on an x86 CPU, the decoders have no clue <em>where</em> the next instruction starts. It has to actually analyze each instruction in order to see how long it is.
</p><p>
The brute force way Intel and AMD deal with this is by simply attempting to decode instructions at every possible starting point. That means x86 chips have to deal with lots of wrong guesses and mistakes which has to be discarded. This creates such a convoluted and complicated decoder stage that it is really hard to add more decoders. But for Apple, it is trivial in comparison to keep adding more.
</p><p>
In fact, adding more causes so many other problems that four decoders according to AMD itself is basically an upper limit for them.
</p><p>
<mark>This is what allows the M1 Firestorm cores to essentially process</mark> <mark><strong>twice</strong></mark> <mark>as many instructions as AMD and Intel CPUs at the</mark> <mark><em>same</em></mark> <mark>clock frequency.</mark>
</p><p>
One could argue as a counterpoint that CISC instructions turn into more micro-ops. For instance if every x86 instruction turned into 2 micro-ops while every ARM instruction turned into 1 micro-op, then four x86 decoders would produce the same number of micro-ops per clock cycle as an ARM CPU with 8 decoders.
</p><p>
Except this is not the case in the real world. Highly optimized x86 code rarely uses complex CISC instructions, which would translate into many micro-ops. In fact most will only translate into 1 micro-op.
</p><p>
However all these simple x86 instructions dont help Intel or AMD. Because even if those 15 byte long instructions are rare, the decoders have to be made to handle them. This incurs complexity that blocks AMD and Intel from adding more decoders.
</p><h2>
But AMDs Zen3 cores are still faster right?
</h2><p>
As far as I remember from performance benchmarks, the newest AMD CPU cores, the ones called Zen3 are slightly faster than Firestorm cores. But here is the kicker: That only happens because the Zen3 cores are clocked at 5 GHz. Firestorm cores are clocked at 3.2 GHz. The Zen3 is just barely squeezing past Firestorm despite having almost 60% higher clock frequency.
</p><p>
So why doesnt Apple increase the clock frequency too? Because higher clock frequency makes the chips run hotter. That is one of Apples key selling points. Their computers — unlike Intel and AMD offerings — barely need cooling.
</p><p>
In essence, one could say Firestorm cores really are superior to Zen3 cores. Zen3 only manages to stay in the game by drawing a lot more current and getting a lot hotter. Something Apple simply chooses not to do.
</p><p>
If Apple wants higher performance they are simply going to add more cores. That lets them keep watt usage down while offering more performance.
</p><h2>
The future
</h2><p>
It seems AMD and Intel have painted themselves into a corner on two fronts:
</p><ul><li>They dont have a business model that makes it easy to pursue heterogeneous computing and SoC designs.</li><li>Their legacy x86 CISC instruction set is coming back to haunt them, making it hard to improve OoO performance.</li></ul><p>
It doesnt mean game over. They can increase the clock frequency and use more cooling, throw in more cores, beef up the CPU caches, etc. But they are both at a disadvantage. Intel is in the worst situation, as their cores are already soundly beaten by Firestorm, and they have weak GPUs to integrate with an SoC solution.
</p><p>
The problem with throwing in more cores is that for typical desktop workloads you reach diminishing returns with too many cores. Sure lots of cores are great for servers.
</p><p>
However here companies such as Amazon and Ampere are attacking with monster CPUs with 128 cores. This is like fighting the western and eastern front at the same time.
</p><p>
But fortunately for AMD and Intel, Apple doesnt sell their chips on the market. So PC users will simply have to put up with whatever they are offering. PC users may jump ship, but that is a slow process. You dont leave immediately a platform you are heavily invested in.
</p><p>
But young professionals, with money to burn without too deep investments in any platform, may increasingly turn to Apple in the future, beefing up their hold on the premium market and consequently their share of the total profit in the PC market.
</p></div>