Unit 2 · From the Abacus to C++¶
1. Setting the scene¶
Scenario: The first sensor program
Before SensorGrid can be parallelised, it needs a fast sequential baseline. The team decides to rewrite the core of the Python script in C++, a language that sits close to the hardware. The first task is modest: simulate one temperature sensor, collect ten readings, and print their average.
A new team member asks: "Why C++? Why not stay in Python?"
Guiding questions
- How did computing evolve from mechanical calculators to programmable machines?
- What happens, level by level, between a line of C++ and the transistors?
- How does the letter "A" become electrical signals?
2. Intended learning outcomes¶
| # | Outcome | Bloom | CLO |
|---|---|---|---|
| ILO 2.1 | Describe the three stages and the generations of computing. | UNDERSTAND | 1 |
| ILO 2.2 | Explain the computer level hierarchy from digital logic to high level languages. | UNDERSTAND | 1 |
| ILO 2.3 | Trace how a character is encoded in ASCII and binary. | APPLY | 1 |
| ILO 2.4 | Write simple C++ programs with Boolean logic, input/output and stack arrays. | APPLY | 1 |
| ILO 2.5 | Argue why parallelism is the cost effective path to performance. | EVALUATE | 1 |
3. Three stages of computing¶
flowchart TB
S1["<b>Stage 1 · Mechanised arithmetic</b><br/>abacus · Pascal's adder · Leibniz's calculator"]
S2["<b>Stage 2 · Stored programs</b><br/>automatic looms (1801) · punched cards"]
S3["<b>Stage 3 · Both together</b><br/>Babbage's programmable computer"]
S1 --> S3
S2 --> S3
flowchart LR
subgraph M["Mechanical era"]
direction TB
a["1642 · Pascal's adder"] --> b["1673 · Leibniz's<br/>stepped reckoner"] --> c["1801 · Jacquard's<br/>punched card loom"] --> d["1837 · Babbage's<br/>Analytical Engine"]
end
subgraph E["Electromechanical and electronic era"]
direction TB
e["1890 · Hollerith tabulates<br/>the US census"] --> f["1944 · Harvard Mark I"] --> g["1946 · ENIAC"] --> h["1951 · UNIVAC"]
end
M --> E
Why the 1890 census matters
Hollerith's punched card tabulators saved the US Census Office about two years of labour and an estimated 5 million dollars compared with the 1880 census; in 1924 his company became IBM. Data processing at scale has driven computing from the very beginning.
Generations of hardware¶
| Generation | Technology | Period |
|---|---|---|
| First | Vacuum tubes | 1940s to 1950s |
| Second | Transistors | 1959 to 1965 |
| Third | Integrated circuits | 1965 to 1970s |
| Fourth | Very Large Scale Integration (VLSI): millions to billions of transistors on one chip, microprocessors | 1975 onward |
| Fifth? | Japan's 1980s plan: AI ideas (LISP) and massive parallelism | "with us, but not as envisioned" |
The fifth generation's ingredients, AI and massive parallelism, are exactly what dominate computing today, only through GPUs and clusters rather than through special AI machines.
4. The computer level hierarchy¶
A computer is best understood as a stack of virtual machines. Each level hides the details of the one below.
flowchart TB
L6["<b>Level 6 · User</b><br/>executable programs"]
L5["<b>Level 5 · High level language</b><br/>C++, Python, Java"]
L4["<b>Level 4 · Assembly language</b><br/>mnemonics such as ld, add, st"]
L3["<b>Level 3 · System software</b><br/>operating system, libraries"]
L2["<b>Level 2 · Machine</b><br/>instruction set architecture (ISA)"]
L1["<b>Level 1 · Control</b><br/>microcode or hardwired control"]
L0["<b>Level 0 · Digital logic</b><br/>circuits, gates, transistors"]
L6 --> L5 --> L4 --> L3 --> L2 --> L1 --> L0
C++ lives at level 5 but maps very directly to levels 4 and 2. That is why it is the language of choice for OpenMP and CUDA: the programmer can reason about registers, memory layout and caches.
5. How the CPU reads the letter "A"¶
flowchart LR
K["Key press<br/>'A'"] --> E["ASCII code<br/>65"] --> B["Binary<br/>01000001"] --> T["Transistors<br/>off on off off off off off on"]
Each bit controls whether current flows through a transistor acting as a switch: ON for 1, OFF for 0. Every value a program touches, every sensor reading and every model weight, ends up as such patterns.
Try it
Which character has the binary code 01000010? Answer: 66, the letter "B".
6. Talking to the machine in C++¶
Boolean logic¶
In C++, && computes \(a \wedge b\) (AND), || computes \(a \vee b\) (OR), and ^ computes \(a \oplus b\) (XOR).
| \(a\) | \(b\) | \(a \wedge b\) | \(a \vee b\) | \(a \oplus b\) |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
#include <iostream>
using namespace std;
int main() {
bool a, b;
cout << "Please enter a boolean (0,1): ";
if (!(cin >> a)) { cerr << "Invalid input\n"; return 1; }
cout << "Please enter a boolean (0,1): ";
if (!(cin >> b)) { cerr << "Invalid input\n"; return 1; }
cout << "\nThe result for a AND b is: " << (a && b) << '\n'
<< "The result for a OR b is: " << (a || b) << '\n'
<< "The result for a XOR b is: " << (a ^ b) << '\n';
return 0;
}
Can you find the bug?
The lecture version reads cin >> a; without checking the result. What happens if the user types 2 or yes?
Answer
The extraction into a bool fails, cin enters a fail state, and every later read is skipped. b is then used uninitialised, which is undefined behaviour: the program may print anything. The fixed version above checks every read. Lesson: the machine does exactly what we say, not what we mean.
Practice: a smart home access control system¶
flowchart TB
S(["Person at the door"]) --> O{"Owner fingerprint?"}
O -->|"yes"| U["Door unlocked"]
O -->|"no"| K{"Keycard AND correct PIN?"}
K -->|"no"| D["Access denied"]
K -->|"yes"| A{"Alarm active?"}
A -->|"yes"| D
A -->|"no"| U
#include <iostream>
int main() {
bool hasKeycard, knowsPin, isOwner, alarmActive;
std::cout << "Keycard (0/1)? "; std::cin >> hasKeycard;
std::cout << "Correct PIN (0/1)? "; std::cin >> knowsPin;
std::cout << "Owner fingerprint (0/1)? "; std::cin >> isOwner;
std::cout << "Alarm active (0/1)? "; std::cin >> alarmActive;
bool accessGranted = isOwner || (hasKeycard && knowsPin && !alarmActive);
std::cout << (accessGranted ? "Door unlocked\n" : "Access denied\n");
return 0;
}
Lab: the first SensorGrid program (stack memory)¶
Task 1: basic sensor simulation
Simulate one temperature sensor, collect 10 random readings, store them in a stack array float readings[10], and print the average.
#include <cstdlib>
#include <ctime>
#include <iostream>
int main() {
std::srand(static_cast<unsigned>(std::time(nullptr)));
float readings[10]; // stack array: size fixed at compile time
float sum = 0.0f;
for (int i = 0; i < 10; ++i) {
readings[i] = 15.0f + (std::rand() % 1000) / 100.0f; // 15.00 to 24.99 °C
sum += readings[i];
std::cout << "reading " << i << ": " << readings[i] << " C\n";
}
std::cout << "average: " << sum / 10 << " C\n";
return 0;
}
Unit 3 explains where readings lives (the stack) and why that matters; Unit 9 computes the same average over millions of readings with several threads.
7. The economic argument for parallelism¶
- It is harder and harder to increase clock speeds.
- Sustainable gains in speed come only from better software, better use of hardware, and parallelism.
- Parallelism is cost effective: multiplying ordinary hardware is cheap; making one component extremely fast is expensive.
- It also helps memory: adding memory modules is cheap, building one huge fast memory is expensive.
quadrantChart
title Ways to get more performance
x-axis Low cost --> High cost
y-axis Low scalability --> High scalability
quadrant-1 Powerful but expensive
quadrant-2 Sweet spot
quadrant-3 Dead end
quadrant-4 Diminishing returns
More commodity cores: [0.25, 0.85]
Cluster of cheap nodes: [0.35, 0.9]
Higher clock frequency: [0.8, 0.2]
Deeper single core ILP: [0.75, 0.3]
Specialised supercomputer: [0.9, 0.75]
8. Check your understanding¶
Which two ideas did Babbage merge?
Mechanised arithmetic and stored programs.
At which level of the hierarchy does the instruction set architecture live, and why does it matter to programmers?
Level 2 (machine). It is the contract between software and hardware: compilers target it, and its features (vector instructions, atomic operations) determine what parallel code can do efficiently.
What is 01001000 in decimal and ASCII?
\(64 + 8 = 72\), the letter "H".
9. Key takeaways¶
- Computing merged mechanised arithmetic and stored programs; data processing (the census) drove it from the start.
- A computer is a hierarchy of levels; C++ lets us reason close to the machine.
- Characters and numbers become bits, and bits become transistor states.
- Parallelism is the cost effective path to performance.
10. Link to assessment¶
The portfolio requires a sequential C/C++ baseline for every parallel experiment. Speedup is always measured against your own best sequential version.
Further reading¶
- L. Null, J. Lobur, The Essentials of Computer Organization and Architecture, ch. 1.
- K. Yelick, CS267 Applications of Parallel Computers, UC Berkeley, 2007.