深入理解計算機系統家庭作業第六章
/*
***6.23
*/
等價於求xr(1 - x)的最大值,由代數知識得x=0.5的時候取得最大。
/*
***6.24
*/
0.5 * 60 / 12000 * 1000 + 60 / 12000 * 1000 /500 + 3 = 5.51ms
/*
***6.25
*/
定位時間 = 3 + 2.5 = 5.5ms
A. 3072 / 500 * 5 + 5.5 = 36.22ms
B. 5.5 * 3072 = 16896ms
6.26,6.27 根據公式C = B * E * S即可推得,此處略之
/*
***6.28
*/
A. 0x1238, 0x1239, 0x123a, 0x123b
B. 0x8a4, 0x8a5, 0x8a6, 0x8a7 0x704, 0x705, 0x706, 0x707
/*
***6.29
*/
A. 0x1bdc, 0x1bdd, 0x1bde, 0x1bdf
B. 0xe34, 0xe35, 0xe36, 0xe37
C. 0x18f0, 0x18f1, 0x18f2, 0x18f3 0xb0, 0xb1, 0xb2, 0xb3
D. 不會命中
/*
***6.30
*/
B. 1.不命中,有效位為0
2.命中。在1中已載入
3.命中。值為D0
/*
***6.31
*/
A. C= E*B*S = 128
B. CO 最後兩位
CI 除去最後兩位的後三位
CT 前8位
/*
***6.32
*/
A. 0011100011000
B. CO 0x0
CI 0x6
CT 0x38
沒有命中
/*
***6.33
*/
A. 1011011101100
B. CO 0x0
CI 0x3
CT 0xB7
沒有命中
/*
***6.34
*/
0x1314, 0x1315, 0x1316, 0x1317
0x1794, 0x1795, 0x1796, 0x1797
/*
***6.35
*/
快取一共有兩個塊,src[0],src[2],dst[0],dst[2]訪問快取第一個塊,src[1],src[3],dst[1],dst[3]訪問快取第二個塊。
dst陣列
m h m h
m m h m
m h m h
m m h m
src陣列
m m m m
m m m m
m m m m
m m m m
/*
***6.36
*/
當總大小為128時,能容納下src與dst陣列中的所有元素
dst陣列
m h h h
m h h h
m h h h
m h h h
src陣列
m h h h
m h h h
m h h h
m h h h
/*
***6.37
*/
A. x[0][i]和x[1][i]是同一個快取條目。不命中率為100%,屬於交叉不命中
B. 快取大小可容納陣列所有內容。不命中率為1/8。
C. x[0][i]和x[1][i]載入到不同行。不命中率為1/8.
D. 不能,存在冷不命中。
E. 能。加大塊大小能減小冷不命中概率。
/*
***6.38
*/
N=64時:
sumA: 1/4
sumB: 1
sumC: 1/2
N= 60時:
sumA ,sumB,sumC的快取不命中率均為 1/4
比較難判斷的是N = 60時sumB的快取不命中率(sumC與sumB是一樣的),我寫了一個函式返回不命中次數,將形參n賦值60即可。
//快取記憶體命中率函式,返回不命中次數
int noHitPercentage(int n)
{
//不命中的次數
int result = 0;
//總共要迴圈的次數
int count;
//儲存塊的標記位
int a[256];
for(int i =0;i < 256;i++)
{
a[i] = -1;
}
for(int j = 0;j < n;j++)
for(int i = 0;i < n;i++)
{
//求出這個數的相對索引
count = i * n + j;
//求這個索引對應的塊號
int blockNo = (count/4) % 256;
//求出標記t
int t = (count/4)/256;
//如果標記位不相等則不明中
if(t != a[blockNo])
{
a[blockNo] = t;
result++;
}
}
return result;
}
/*
***6.39
*/
A. 16 * 16 * 4 = 1024
B. 64
C. 1/16
/*
***6.40
*/
A. 1024
B. 256
C. 1/4
/*
***6.41
*/
A. 1024
B. 64 + 64 = 128
C. 1/8
/*
***6.42
*/
25%
/*
***6.43
*/
25%
/*
***6.44
*/
100%
/*
***6.46
*/
void betterTranspose(int *dst,int *src,int dim)
{
<span style="white-space:pre"> </span>int i, j;
<span style="white-space:pre"> </span>int iCount,jCount;
<span style="white-space:pre"> </span>//以4 * 4 的方陣為單位依次計算,增加了寫的快取命中率,多個元素一起讀寫還減少了迴圈開銷
<span style="white-space:pre"> </span>for(i = 0;i < dim - 3;i += 4)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>iCount = i * dim;
<span style="white-space:pre"> </span>for(j = 0;j < dim - 3;j += 4)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>jCount = j * dim;
<span style="white-space:pre"> </span>dst[jCount + i] = src[iCount + j]; //dst[j][i] = src[i][j]
<span style="white-space:pre"> </span>dst[jCount + i + 1] = src[iCount + dim + j]; //dst[j][i + 1] = src[i + 1][j]
<span style="white-space:pre"> </span>dst[jCount + i + 2] = src[iCount + dim * 2 + j]; //dst[j][i + 2] = src[i + 2][j]
<span style="white-space:pre"> </span>dst[jCount + i + 3] = src[iCount + dim * 3 + j]; //dst[j][i + 3] = src[i + 3][j]
<span style="white-space:pre"> </span>dst[jCount + dim + i] = src[iCount + j + 1]; //dst[j + 1][i] = src[i][j + 1]
<span style="white-space:pre"> </span>dst[jCount + dim + i + 1] = src[iCount + dim + j + 1]; //dst[j + 1][i + 1] = src[i + 1][j + 1]
<span style="white-space:pre"> </span>dst[jCount + dim + i + 2] = src[iCount + dim * 2 + j + 1]; //dst[j + 1][i + 2] = src[i + 2][j + 1]
<span style="white-space:pre"> </span>dst[jCount + dim + i + 3] = src[iCount + dim * 3 + j + 1]; //dst[j + 1][i + 3] = src[i + 3][j + 1]
<span style="white-space:pre"> </span>dst[jCount + dim * 2 + i] = src[iCount + j + 2]; //dst[j + 2][i] = src[i][j + 2]
<span style="white-space:pre"> </span>dst[jCount + dim * 2 + i + 1] = src[iCount + dim + j + 2]; //dst[j + 2][i + 1] = src[i + 1][j + 2]
<span style="white-space:pre"> </span>dst[jCount + dim * 2 + i + 2] = src[iCount + dim * 2 + j + 2]; //dst[j + 2][i + 2] = src[i + 2][j + 2]
<span style="white-space:pre"> </span>dst[jCount + dim * 2+ i + 3] = src[iCount + dim * 3 + j + 2]; //dst[j + 2][i + 3] = src[i + 3][j + 2]
<span style="white-space:pre"> </span>dst[jCount + dim * 3 + i] = src[iCount + j + 3]; //dst[j + 3][i] = src[i][j + 3]
<span style="white-space:pre"> </span>dst[jCount + dim * 3 + i + 1] = src[iCount + dim + j + 3]; //dst[j + 3][i + 1] = src[i + 1][j + 3]
<span style="white-space:pre"> </span>dst[jCount + dim * 3 + i + 2] = src[iCount + dim * 2 + j + 3]; //dst[j + 3][i + 2] = src[i + 2][j + 3]
<span style="white-space:pre"> </span>dst[jCount + dim * 3 + i + 3] = src[iCount + dim * 3 + j + 3]; //dst[j + 3][i + 3] = src[i + 3][j + 3]
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>//記錄當前行和列的索引,以便執行完剩餘的項
<span style="white-space:pre"> </span>int curIndex = i;
<span style="white-space:pre"> </span>//處理剩餘項,簡單的交換處理
<span style="white-space:pre"> </span>for(i = 0;i < curIndex;i++)
<span style="white-space:pre"> </span>for(j = curIndex;j < dim;j++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>dst[j * dim + i] = src[i * dim + j];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>for(i = curIndex;i < dim;i++)
<span style="white-space:pre"> </span>for(j = 0;j < dim;j++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>dst[j * dim + i] = src[i * dim + j];
<span style="white-space:pre"> </span>}
}
/*
***6.47
*/
void better_col_convert(int *G,int dim)
{
int i, j;
int iCount,jCount;
//以4 * 4 的方陣為單位依次計算,增加了寫的快取命中率,多個元素一起讀寫還減少了迴圈開銷
for(i = 0;i < dim - 3;i += 4)
{
iCount = i * dim;
for(j = 0;j < dim - 3;j += 4)
{
jCount = j * dim;
G[jCount + i] = G[iCount + j] || G[jCount + i]; //G[j][i] = G[i][j] || G[j][i]
G[jCount + i + 1] = G[iCount + dim + j] || G[jCount + i + 1]; //G[j][i + 1] = G[i + 1][j] || G[j][i + 1]
G[jCount + i + 2] = G[iCount + dim * 2 + j] || G[jCount + i + 2]; //G[j][i + 2] = G[i + 2][j] || G[j][i + 2]
G[jCount + i + 3] = G[iCount + dim * 3 + j] || G[jCount + i + 3]; //G[j][i + 3] = G[i + 3][j] || G[j][i + 3]
G[jCount + dim + i] = G[iCount + j + 1] || G[jCount + dim + i]; //G[j + 1][i] = G[i][j + 1] || G[j + 1][i]
G[jCount + dim + i + 1] = G[iCount + dim + j + 1] || G[jCount + dim + i + 1]; //G[j + 1][i + 1] = G[i + 1][j + 1] || G[j +1][i + 1]
G[jCount + dim + i + 2] = G[iCount + dim * 2 + j + 1] || G[jCount + dim + i + 2]; //G[j + 1][i + 2] = G[i + 2][j + 1] || G[j +1][i + 2]
G[jCount + dim + i + 3] = G[iCount + dim * 3 + j + 1] || G[jCount + dim + i + 3]; //G[j + 1][i + 3] = G[i + 3][j + 1] || G[j + 1][i + 3]
G[jCount + dim * 2 + i] = G[iCount + j + 2] || G[jCount + dim * 2 + i]; //G[j + 2][i] = G[i][j + 2] || G[j +2][i]
G[jCount + dim * 2 + i + 1] = G[iCount + dim + j + 2] || G[jCount + dim * 2 + i +1]; //G[j + 2][i + 1] = G[i + 1][j + 2] || G[j +2][i + 1]
G[jCount + dim * 2 + i + 2] = G[iCount + dim * 2 + j + 2] || G[jCount + dim * 2 + i + 2]; //G[j + 2][i + 2] = G[i + 2][j + 2] || G[j +2][i + 2]
G[jCount + dim * 2+ i + 3] = G[iCount + dim * 3 + j + 2] || G[jCount + dim * 2 + i + 3]; //G[j + 2][i + 3] = G[i + 3][j + 2] || G[j + 2][i + 3]
G[jCount + dim * 3 + i] = G[iCount + j + 3] || G[jCount + dim * 3 + i]; //G[j + 3][i] = G[i][j + 3] || G[j +3][i]
G[jCount + dim * 3 + i + 1] = G[iCount + dim + j + 3] || G[jCount + dim * 3 + i + 1]; //G[j + 3][i + 1] = G[i + 1][j + 3] || G[j +3][i + 1]
G[jCount + dim * 3 + i + 2] = G[iCount + dim * 2 + j + 3] || G[jCount + dim * 3 + i + 2]; //G[j + 3][i + 2] = G[i + 2][j + 3] || G[j + 3][i + 2]
G[jCount + dim * 3 + i + 3] = G[iCount + dim * 3 + j + 3] || G[jCount + dim * 3 + i + 3]; //G[j + 3][i + 3] = G[i + 3][j + 3] || G[j + 3][i + 3]
}
}
//記錄當前行和列的索引,以便執行完剩餘的項
int curIndex = i;
//處理剩餘項,簡單的交換處理
for(i = 0;i < curIndex;i++)
for(j = curIndex;j < dim;j++)
{
G[j * dim + i] = G[i * dim + j] || G[j * dim + i];
}
for(i = curIndex;i < dim;i++)
for(j = 0;j < dim;j++)
{
G[j * dim + i] = G[i * dim + j] || G[j * dim + i];
}
}