計算機システムの基本
informationsystemsundergraduatelecture
data/lecture/information/systems/computer-systems-portal.lecture.n.md
1導入
この講義では、CPU、主記憶、入出力が協調してプログラムを実行する過程を説明する。命令の実行、データの保持、外部装置とのデータ交換を区別することにより、プログラムの振る舞いと計算機の構成を対応づける。
2格納プログラム方式
格納プログラム方式では、プログラムを構成する命令と、その命令が処理するデータを、いずれも主記憶に配置する。CPUは主記憶から次に実行する命令を取得し、その命令が指定する演算を実行する。
主記憶は番地によって識別される記憶場所の集合として扱える。プログラムの変数は、実行時には値を格納する記憶場所またはその場所を参照する仕組みに対応する。ただし、コンパイラによる最適化では、値がCPU内部のレジスタだけに保持されることもあるため、すべての変数が常に主記憶の固定番地を占有するわけではない。
3CPUと命令サイクル
CPUは、演算を実行する演算装置、命令実行を制御する制御装置、少量のデータを高速に保持するレジスタなどから構成される。
命令の処理は、概念的には次の命令サイクルとして整理できる。
- 取り出し: 次に実行する命令を主記憶から取得する。
- 解読: 命令が要求する演算と対象を判定する。
- 実行: 演算、データ転送、または分岐を実行する。
- 書き戻し: 必要に応じて結果をレジスタまたは主記憶へ保存する。
たとえば、二つの値をレジスタへ読み込んだ後に加算する処理では、加算命令がALUに演算を指示し、結果をレジスタへ保存する。どの命令がメモリを直接参照できるかは、命令セットによって異なる。条件分岐は、条件に応じて次に取得する命令の番地を変更する。
4記憶階層
記憶装置の容量、速度、単位容量あたりの費用の間にはトレードオフがある。このため、計算機は一般に、レジスタ、キャッシュ、主記憶、補助記憶という記憶階層を構成する。
\text{レジスタ}\longrightarrow\text{キャッシュ}\longrightarrow\text{主記憶}\longrightarrow\text{補助記憶}
左側ほどCPUに近く、高速かつ小容量である。プログラムによるメモリ参照には、短い時間に同一の記憶場所またはその近傍へ参照が集中する傾向がある。この傾向を参照の局所性という。記憶階層は、この局所性を利用して、頻繁に参照するデータをCPUに近い階層で再利用する。
5入出力
入出力は、ディスプレイ、キーボード、補助記憶装置、ネットワークインタフェースなどとデータを交換する機能である。CPUと入出力装置には動作速度の差があるため、CPUが完了を待機し続ける方式だけではCPU時間を有効に利用できない。割込みは装置の状態変化や処理完了をCPUへ通知する。DMAは、CPUがデータを一語ずつ転送することなく、装置と主記憶の間でデータを転送する。これらは、CPUと入出力の並行動作を支援する代表的な仕組みである。
ネットワーク通信も入出力の一種である。後続のネットワーク講義では、データを複数の計算機の間で転送する仕組みを詳述する。
6性能を区別して評価する
プログラムの所要時間を分析するときは、少なくとも次の要因を区別する。
- CPUが命令を実行する時間
- 記憶階層からデータを取得する時間
- 入出力の完了を待つ時間
アルゴリズムの計算量は、入力規模に対して必要な演算回数や記憶量がどのように増加するかを抽象的に表す。実測時間は、それに加えてCPU、記憶階層、入出力装置、実装などの影響を受ける。したがって、計算量と特定の計算機における実測時間は同一ではない。
7要点
- 格納プログラム方式では、命令とデータを主記憶に配置し、CPUが命令サイクルに従って処理する。
- 記憶階層は、速度、容量、費用の相違を利用してデータ参照を効率化する。
- 実行時間の分析では、CPU処理、メモリ参照、入出力待機を区別する。
8次に進む講義
data/lecture/information/networks/computer-networks-portal.lecture.n.md
Computer Systems Fundamentals
data/lecture/information/systems/computer-systems-portal.lecture.n.md
1Introduction
This lecture explains how the CPU, main memory, and input cooperate to execute a program. By distinguishing instruction execution, data storage, and exchange with external devices, it connects program behavior to computer organization.
2Stored-Program Architecture
In a stored-program architecture, both the instructions that constitute a program and the data processed by those instructions reside in main memory. The CPU retrieves instructions from main memory and performs the operations that they specify.
Main memory can be treated as a collection of storage locations identified by addresses. At run time, a program variable corresponds to a location that stores a value or to a mechanism for referring to that location. Compiler optimizations may keep a value solely in a CPU register, so not every variable permanently occupies a fixed address in main memory.
3The CPU and the Instruction Cycle
A CPU includes an arithmetic and logic unit that performs operations, a control unit that directs instruction execution, and registers that provide a small amount of high-speed storage.
Instruction processing can be organized conceptually as the following instruction cycle.
- Fetch: Retrieve the next instruction from main memory.
- Decode: Determine the operation and operands specified by the instruction.
- Execute: Perform an operation, data transfer, or branch.
- Write-back: Store the result in a register or main memory when required.
For example, in a computation that loads two values into registers and then adds them, an addition instruction directs the ALU to perform the operation and stores the result in a register. Whether an instruction can refer to memory directly depends on the instruction set. A conditional branch changes the address of the next instruction according to a condition.
4Memory Hierarchy
Storage technologies involve tradeoffs among capacity, speed, and cost per unit of capacity. Computers therefore generally organize storage as a memory hierarchy consisting of registers, caches, main memory, and secondary storage.
\text{registers}\longrightarrow\text{caches}\longrightarrow\text{main memory}\longrightarrow\text{secondary storage}
Components farther to the left are closer to the CPU, faster, and smaller. A program tends to concentrate its memory references on the same storage locations or nearby locations over a short period. This tendency is called locality of reference. A memory hierarchy exploits locality by reusing frequently accessed data at levels close to the CPU.
5Input/Output
Input/output exchanges data with displays, keyboards, secondary storage devices, network interfaces, and other devices. Because CPUs and I/O devices operate at different speeds, a design in which the CPU merely waits for each operation to finish cannot use CPU time efficiently. An interrupt notifies the CPU of a device state change or operation completion. DMA transfers data between a device and main memory without requiring the CPU to move each word. These mechanisms help CPU execution and input/output proceed concurrently.
Network communication is also a form of input/output. The following network lectures explain how data is transferred among multiple computers.
6Distinguishing Sources of Performance
An analysis of program execution time should distinguish at least the following factors:
- time spent executing instructions on the CPU;
- time spent obtaining data from the memory hierarchy; and
- time spent waiting for input/output to complete.
Algorithmic complexity abstracts how the required number of operations or amount of storage grows with input size. Measured execution time additionally depends on the CPU, memory hierarchy, I/O devices, implementation, and other factors. Algorithmic complexity and measured time on a particular computer are therefore not identical.
7Key Points
- In a stored-program architecture, instructions and data reside in main memory, and the CPU processes instructions through an instruction cycle.
- A memory hierarchy exploits differences in speed, capacity, and cost to make data access efficient.
- Performance analysis distinguishes CPU processing, memory access, and input/output waiting time.
8Next Lecture
data/lecture/information/networks/computer-networks-portal.lecture.n.md