embedded
contributed by <Cayonliow
>
tags:
在讀論文之前, 去找了找 Prefetch 的定義
Stride Prefetching
* 在 stride prefetching 裏 有一個表是用來儲存 stride-related local history information
* 儲存 the most recent stride, last address, state infoemation
* 當 prefetch 被觸發, a+s, a+2s,…a+ds 的 address 會被預讀
* a = 最根本的地址
* s = 偵測到的範圍
* d = degree of prefetch (不太懂。。
Markov Prefetching
Distance Prefetching
提到的是 Software Prefetching, Hardware Prefetching, 跟兩者之間一起使用的利弊
圖上表示的是 Sw, Hw,Sw&Hw 用各種 benchmark 的效能的測試結果
這有提到各種形態造成的 index 是 direct 或是 Indirect 的, 這會影響到 Hw 的 Prefetch 效能
在使用 Sw Prefetching 需要手動輸入一些程式碼
#include <mmintrinsics.h>
void _mm_prefetch(char * p , int i );
p = 要做 Prefetch 的資料的地址
i = 上圖的 Hint 的部分
這裏說的是對 Prefetch 的分類 只有Timely 是好的
Software Prefetch Distance
b[i][j][k] = (a[i][j][k] + a[i][j][k+1] + a[i][j][k-1] + a[i][j-1][k] + a[i][j+1][k]+...)/27
Short Streams
Irregular Memory Access
Cache locality Hint
Loop Bounds
Instruction count
Static Insertion
Code Structure change
Handling Multiple Streams
Training
Harmful Software Prefetching
先執行看看結果
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15
sse prefetch: 58170 us
sse: 122664 us
naive: 267798 us
做的是矩陣轉置, 並看到這三種方式的執行時間
然後個別檢查各種方式的 cache miss
Performance counter stats for './main' (100 runs):
428,9651 cache-misses # 78.672 % of all cache refs ( +- 0.05% )
545,2589 cache-references ( +- 0.01% )
848,9913 L1-dcache-load-misses ( +- 0.02% )
<not supported> L1-dcache-store-misses
<not supported> L1-dcache-prefetch-misses
2,9095 L1-icache-load-misses ( +- 0.35% )
0.186183896 seconds time elapsed ( +- 0.09% )
Performance counter stats for './main' (100 runs):
448,4773 cache-misses # 81.924 % of all cache refs ( +- 0.61% )
547,4291 cache-references ( +- 0.06% )
849,2977 L1-dcache-load-misses ( +- 0.03% )
<not supported> L1-dcache-store-misses
<not supported> L1-dcache-prefetch-misses
4,1314 L1-icache-load-misses ( +- 3.31% )
0.253898320 seconds time elapsed ( +- 0.57% )
Performance counter stats for './main' (100 runs):
1678,6696 cache-misses # 93.020 % of all cache refs ( +- 0.09% )
1804,6256 cache-references ( +- 0.01% )
2105,5287 L1-dcache-load-misses ( +- 0.01% )
<not supported> L1-dcache-store-misses
<not supported> L1-dcache-prefetch-misses
3,6499 L1-icache-load-misses ( +- 2.63% )
0.387200088 seconds time elapsed ( +- 0.53% )
很明顯 naive 所造成的 cache-miss 是比較高的
perf stat 的結果有兩個 not supported 分別是 L1-dcache-store-misses 跟 L1-dcache-prefetch-misses 原因還在查詢中
參考 illusion030 的共筆與ierosodin 的共筆 中的 使用 SSE 的指令集
<immintrin.h>
CFLAGS = -msse2 --std gnu99 -O0 -Wall -Wextra -mavx2
Description | |
---|---|
_mm256_loadu_si256 | 從記憶體中讀入 256-bits 的 整數(integer data) 放入 dst. mem_addr (不需要有特定邊界? |
_mm256_unpacklo_epi32 | 將兩個參數的 lower bits 以 32 bits 為單位輪流排序並 return |
_mm256_unpackhi_epi32 | 將兩個參數的 higher bits 以 32 bits 為單位輪流排序並 return |
_mm256_unpacklo_epi64 | 將兩個參數的 lower bits 以 64 bits 為單位輪流排序並 return |
_mm256_unpackhi_epi64 | 將兩個參數的 higher bits 以 64 bits 為單位輪流排序並 return |
_mm256_permute2x128_si256 | 將兩個參數以 128 bits 爲單位進行隨機洗牌,然後存入 dst |
_mm256_storeu_si256 | 將整數數據以 256 bits 爲單位存入記憶體 |
執行100次, 其中的5次執行時間:
avx: 58198 us
avx: 57160 us
avx: 59003 us
avx: 56443 us
avx: 56071 us
cache miss:
Performance counter stats for './cache-miss-avx' (100 runs):
331,0269 cache-misses # 74.376 % of all cache refs ( +- 0.31% )
445,0747 cache-references ( +- 0.32% )
803,6992 L1-dcache-load-misses ( +- 0.25% )
<not supported> L1-dcache-store-misses
<not supported> L1-dcache-prefetch-misses
3,4753 L1-icache-load-misses ( +- 2.62% )
0.200664836 seconds time elapsed ( +- 1.17% )
不低, 可是已經比錢三個版本來的低