A cycle-accurate simulator of a small vector processor, plus a scalar baseline processor, written in plain Python. Both machines run the same four benchmark kernels (SAXPY, vector add, dot product, matrix multiply). There is also a software-pipelined matrix-multiply variant that measures how much latency hiding helps. Every result is checked against numpy, and the cycle counts feed the experiments and graphs for the paper and the presentation.
| Document | What it is for |
|---|---|
README.md (this file) |
Install, run, and how to read the output |
DESIGN.md |
Architecture, timing model, assumptions. Source for the paper's Methodology section |
RESULTS_SUMMARY.md |
Key numbers, findings in plain English, limitations, extensions |
You need Python 3.9 or newer, plus numpy and matplotlib. Nothing else is required (the
tests use the standard-library unittest).
pip install -r requirements.txtFrom inside the vector_sim/ folder:
python run_experiments.py && python plot_results.pyOn Windows PowerShell 5.1, && does not work. Use ; instead:
python run_experiments.py; python plot_results.py
run_experiments.pyruns every experiment (about 20 seconds) and writesresults/exp*.csv. Every simulated result is compared with numpy. If anything is wrong, the script stops with an error, so every number in the CSVs is from a verified run.plot_results.pyreads the CSVs and writes 13 PNG graphs (300 dpi) toresults/.
python -m unittest discover tests -vThere are 38 tests in four groups:
test_isa.py: the assembler parses programs correctly and rejects bad ones.test_scalar_cpu.pyandtest_vector_cpu.py: correct results, plus exact cycle counts for small programs. The expected cycle counts were worked out by hand from the timing model and are explained in each test. This is the best place to learn how the timing works.test_kernels.py: every kernel on both machines, under several configurations (1/4/8/16 lanes, MVL 16 to 128, chaining on and off, memory latency 10 and 100, sizes that are not multiples of MVL), compared with numpy. It also checks sanity properties, such as "chaining never makes things slower" and "software pipelining speeds up the latency-bound matmul". The pipelined matmul is tested for odd and even N, including N = 1 and 2, where its loop is skipped entirely.
simulate.py runs one kernel and prints statistics. With --trace it also prints a
cycle-by-cycle pipeline diagram.
python simulate.py saxpy # n = 1024 on both machines + speedup
python simulate.py saxpy --n 64 --machine vector --trace
python simulate.py saxpy --n 64 --machine vector --trace --no-chaining # compare!
python simulate.py dot --n 256 --lanes 16 --mem-latency 50
python simulate.py matmul --n 16 --machine scalar
python simulate.py matmul_sp --n 32 --lanes 8 # software-pipelined matmul
python simulate.py vadd --n 8 --machine vector --trace --trace-style cycles
python simulate.py saxpy --listing # print the assembly program
python isa.py # print the instruction set tableOptions: --lanes, --mvl, --no-chaining, --mem-latency, --mem-bandwidth,
--n, --machine scalar|vector|both, --trace, --trace-style pipeline|cycles,
--trace-instrs N, --trace-width W, --listing, --seed.
# instruction stall reason |0123456789012345678901234567890123456789012345678901234
5 VLD v1, (x1) | I----------===============W
6 VLD v2, (x2) memory port busy | SSSSSSSSSSSSSSSI----------===============W
9 VFMA v3, v1, x4, v2 RAW on v2 | SSSSSSSSI-----===============W
10 VST v3, (x2) RAW on v3 | SSSSSI===============W
| Letter | Meaning |
|---|---|
S |
Stalled. The instruction is waiting in the issue stage because a hazard is not resolved yet. The stall reason column says which one. |
I |
Issues. The instruction starts in this cycle. |
- |
In flight. It is inside a functional-unit pipeline or waiting for memory. |
= |
Streaming. Element groups are moving (results being written, or data moving to or from memory). |
W |
Complete. The last result is available. |
Things to point out in the demo:
VLD v2waits for the single memory port, whichVLD v1occupies for 16 cycles.VFMAstarts as soon as the first element ofv2arrives. That is chaining. Run the same command with--no-chainingand it waits for the last element instead.VSTchains behindVFMAthe same way, so load → compute → store overlap like a pipeline of pipelines.
| File | Contents |
|---|---|
config.py |
MachineConfig: every parameter (lanes, MVL, latencies, memory, chaining) in one place |
isa.py |
The 16-instruction ISA table and the two-pass assembler |
scalar_cpu.py |
Scalar baseline: in-order, single-issue, scoreboard timing. Also the base class of the vector CPU |
vector_cpu.py |
Vector CPU = scalar core + lanes, vector units, chaining, VSETVL strip-mining |
tracing.py |
Turns per-instruction timing records into the cycle-by-cycle trace |
kernels.py |
SAXPY, vector add, dot product, matrix multiply and the software-pipelined matrix multiply (matmul_sp) in assembly (scalar and vector versions), with numpy reference answers |
simulate.py |
Command-line tool to run one kernel (demos, traces) |
run_experiments.py |
Runs all experiments and writes CSVs |
plot_results.py |
Makes all graphs from the CSVs |
tests/ |
Unit tests |
results/ |
Output CSVs and PNGs |
| File | Experiment |
|---|---|
exp1_speedup.csv |
Vector vs scalar for every kernel (n = 1024; 16×16 and 32×32 matrices) |
exp2_lanes.csv |
Lanes = 1, 2, 4, 8, 16 |
exp3_chaining.csv |
Chaining off vs on |
exp4_vector_length.csv |
n = 16, 64, 256, 1024, 4096 (1-D kernels only) |
exp5_mem_latency.csv |
Memory latency = 5, 10, 20, 50, 100 cycles |
exp6_mvl.csv |
MVL = 16, 32, 64, 128 (strip-mining overhead) |
exp7_bandwidth.csv |
Extra experiment: memory bandwidth = 1 to 16 elements/cycle with 16 lanes |
exp8_software_pipelining.csv |
Extra experiment: plain vs software-pipelined matmul. A lane sweep (N = 16, 32) and a memory-latency sweep (N = 32). Each variant is compared with the same variant on the scalar machine |
Each experiment changes one parameter. Everything else stays at the defaults: 4 lanes, MVL 64, 8 vector registers, chaining on, memory latency 10 cycles, bandwidth 16 elements/cycle.
Main columns: scalar_cycles, vector_cycles, and speedup (= scalar ÷ vector).
vector_mem_port_util is the fraction of cycles the memory port was busy. Near 100%
means the run is bandwidth-bound. The vector_stall_* columns are stall cycles by
cause. strips is the number of strip-mining loop iterations. Each experiment also
adds one relative column, such as speedup_vs_1_lane. The full list of columns is in
the docstring at the top of run_experiments.py.
| Graph | Shows |
|---|---|
speedup_per_kernel.png |
Headline speedup of each kernel |
speedup_vs_lanes.png |
Speedup vs lanes, one line per kernel |
lane_scaling.png |
The same data relative to 1 lane, against ideal linear scaling |
chaining_on_off.png |
Chaining off vs on (grouped bars) |
cycles_vs_vector_length.png |
Cycles vs n, vector and scalar (log-log) |
speedup_vs_vector_length.png |
Speedup vs n (short vectors cannot amortize start-up) |
cycles_vs_mem_latency.png |
Slowdown vs memory latency, vector and scalar side by side |
speedup_vs_mem_latency.png |
Speedup vs memory latency |
mem_port_util_vs_latency.png |
Where the vector machine changes from bandwidth-bound to latency-bound |
speedup_vs_mvl.png |
Speedup vs MVL (strip-mining overhead) |
speedup_vs_bandwidth.png |
Extra: lanes are useless without memory bandwidth |
software_pipelining.png |
Extra: plain vs pipelined matmul. Cycles per k-step, and speedup, vs lanes |
software_pipelining_latency.png |
Extra: pipelining halves the memory latency each k-step exposes |
-
Machine parameters: change the defaults in
config.py, or passMachineConfig(...)in code, or use thesimulate.pyflags. -
Experiment ranges: edit the lists at the top of
run_experiments.py(LANES,MEM_LATENCIES, ...). -
Adding a kernel:
- Write a function in
kernels.pythat returns aKernelInstance(assembly source, input memory, output location, numpy reference). - Add it to
KERNELSandLABELS. - Add it to
KERNEL_SETinrun_experiments.pyand toKERNEL_ORDER/KERNEL_COLORinplot_results.py.
The tests in
test_kernels.pypick it up if you add it tocases(). - Write a function in