Linuxメモリ帯域幅の計測方法(STREAMの使用方法 )
メモリ帯域幅を計測する機会があったので記録として残しておきます。簡単にインストール出来るので試してみてくださいね。
記事のベンチマーク STREAM の配布元は、2026年11月5日の時点でも同じ場所から取得できました。
$ curl -sI https://www.cs.virginia.edu/stream/FTP/Code/stream.c | head -3
HTTP/1.1 200 OK
Date: Thu, 05 Nov 2026 02:32:53 GMT
Server: Apache/2.4.52 (Ubuntu)
| 項目 | この記事(2014年) | Debian 13(2026年11月時点) |
|---|---|---|
| コンパイラ | gcc(Debian 7 の 4.7 系) | gcc-14(14.2.0) |
| 入れ方 | apt-get install gcc | apt install build-essential |
| stream.c の配布元 | www.cs.virginia.edu/stream/ | 同じ(stream.c の最終更新は 2021年11月) |
版は Debian のパッケージ一覧(madison) で 2026年11月5日に確認しました。今の CPU はキャッシュが大きいので、STREAM の配列の大きさ(-DSTREAM_ARRAY_SIZE)は、配列 1 本が最上位のキャッシュの 4倍以上になるようにして測ります(stream.c の冒頭の説明のとおり)。小さいままだと、メモリではなくキャッシュの速さを測ってしまいます。
$ gcc -O3 -fopenmp -DSTREAM_ARRAY_SIZE=80000000 stream.c -o stream
$ OMP_NUM_THREADS=1 ./stream <- 1スレッド
$ ./stream <- 全コア
| 関数 | 中身 | 1回で動くデータ量 |
|---|---|---|
| Copy | a[i] = b[i] | 16バイト(読み 8 + 書き 8) |
| Scale | a[i] = q*b[i] | 16バイト |
| Add | a[i] = b[i]+c[i] | 24バイト |
| Triad | a[i] = b[i]+q*c[i] | 24バイト |
今回使うツールは、「STREAM」です。STREAMはシングル・マルチスレッドに対応しています。同一マシーンで何回も使う事は希なので、使ったら削除出来るように /tmp に STREAM用のディレクトリを作ります。
STREAMはコンパイルが必要ですが簡単に出来るので、コンパイルからSTREAMの実行・削除まで記述しますね。
gccコンパイラーをインストール
STREAMをコンパイルするには、gccコンパイラーが必要なのでインストールします。イントール済みの方はここは飛ばしてください。
// インストール
# apt-get install gcc
STREAMをインストール
メモリの限界を極めたいならマルチスレッドでコンパイルした方が良いですが、通常はシングルスレッドでも十分です。シングル・マルチスレッドの両方を計測したい場合は、シングル・マルチの両方をそのままコンパイルすれば対応できます。下記の5つの手順をそのまま実行するだけ両方対応です!
// STREAM用のディレクトリ作成
# mkdir /tmp/stream
// 移動
# cd /tmp/stream
// STREAMのソースを取得
# wget http://www.cs.virginia.edu/stream/FTP/Code/stream.c
// コンパイル(シングルスレッドの場合)
# gcc -O stream.c -o stream
// コンパイル(マルチスレッドの場合)
# gcc -O -fopenmp stream.c -o stream_openmp
STREAMの実行方法
// シングルスレッドでの実行
# ./stream
// マルチスレッドでの実行(OpenMP)
# ./stream_openmp
// マルチスレッドのスレッド数を指定したい場合(例:3スレッド)
# export OMP_NUM_THREADS=3
# ./stream_openmp
シングルスレッドの実行例
// シングルスレッドでの実行
# ./stream
-------------------------------------------------------------
STREAM version $Revision: 5.10 $
-------------------------------------------------------------
This system uses 8 bytes per array element.
-------------------------------------------------------------
Array size = 10000000 (elements), Offset = 0 (elements)
Memory per array = 76.3 MiB (= 0.1 GiB).
Total memory required = 228.9 MiB (= 0.2 GiB).
Each kernel will be executed 10 times.
The *best* time for each kernel (excluding the first iteration)
will be used to compute the reported bandwidth.
-------------------------------------------------------------
Your clock granularity/precision appears to be 1 microseconds.
Each test below will take on the order of 47430 microseconds.
(= 47430 clock ticks)
Increase the size of the arrays if this shows that
you are not getting at least 20 clock ticks per test.
-------------------------------------------------------------
WARNING -- The above is only a rough guideline.
For best results, please be sure you know the
precision of your system timer.
-------------------------------------------------------------
Function Best Rate MB/s Avg time Min time Max time
Copy: 3828.5 0.042689 0.041792 0.043653
Scale: 3195.6 0.051129 0.050068 0.052513
Add: 4014.4 0.062034 0.059785 0.073144
Triad: 3408.9 0.071900 0.070404 0.074448
-------------------------------------------------------------
Solution Validates: avg error less than 1.000000e-13 on all three arrays
-------------------------------------------------------------
黄色くマークしてある部分がメモリの計測結果です。「Best Rate MB/s」の値に注目。
某VPSでの結果ですが、ハッキリ言って遅い部類です・・・。
STREAM の削除
gccコンパイラーは今後も使う機会があると思うので残しておきます。STREAM だけ削除します。STREAM のディレクトリを削除するだけなので簡単です!
// 別のディレクトリに移動
# cd /
// STREAMの削除
# rm -rf /tmp/stream