完全关联缓存实现

时间:2014-11-29 05:27:26

标签: caching associative

我不明白为什么我的完全关联缓存的代码与我给出的跟踪文件不匹配。

参数是每个缓存行为32个字节,总缓存大小为16KB。

我对2,4,8和16的组关联缓存的实现都完美地工作(使用最近最少使用的替换策略)。但是对于完全关联,它也可以被描述为32的组关联,非常接近跟踪文件但不完全。坦率地说,我不知道如何调试这个,因为有大量的步骤(至少我这样做)

这是我的代码的相关部分(原谅低效率)

//Fully Associative
    int **fullyAssoc;

    fullyAssoc = new int*[64]; //where fullyAssoc[0][index] is way 0, fullyAssoc[2][index] is way 1 etc..

    int **LRU32;
    LRU32 = new int*[32];

    for (int i = 0; i < 64; ++i){ //Initialize all entries in fullyAssoc to 0
            fullyAssoc[i] = new int[16 * CACHE_LINE / 32];
    }
    for (int i = 0; i < 16; i++){ //Initialize LRU array
            LRU32[0][i] = 0;
            LRU32[1][i] = 1;
            LRU32[2][i] = 2;
            LRU32[3][i] = 3;
            LRU32[4][i] = 4;
            LRU32[5][i] = 5;
            LRU32[6][i] = 6;
            LRU32[7][i] = 7;
            LRU32[8][i] = 8;
            LRU32[9][i] = 9;
            LRU32[10][i] = 10;
            LRU32[11][i] = 11;
            LRU32[12][i] = 12;
            LRU32[13][i] = 13;
            LRU32[14][i] = 14;
            LRU32[15][i] = 15;
            LRU32[16][i] = 16;
            LRU32[17][i] = 17;
            LRU32[18][i] = 18;
            LRU32[19][i] = 19;
            LRU32[20][i] = 20;
            LRU32[21][i] = 21;
            LRU32[22][i] = 22;
            LRU32[23][i] = 23;
            LRU32[24][i] = 24;
            LRU32[25][i] = 25;
            LRU32[26][i] = 26;
            LRU32[27][i] = 27;
            LRU32[28][i] = 28;
            LRU32[29][i] = 29;
            LRU32[30][i] = 30;
            LRU32[31][i] = 31;
    }
    int fullyAssocLRU = 0;
    int memCount = 0;

    while(getline(fileIn, line)){
            stringstream s(line);
            s >> instruction >> hex >> address;

            int indexFull;
            int tagFull;
            unsigned long long address, addressFull;
            address = address >> 5; //Byte offset
            addressFull = address;

            indexFull = addressFull % 16;
            tagFull = addressFull >> 4;

    if (assocCache(fullyAssoc, indexFull, 32, tagFull, LRU32) == 1){
                    fullyAssocLRU++;
    }

}

void LRU_update(int **lru, int index, int way, int ways){
    int temp = 0;
    int temp2[ways];
    int temp_index = 0;
    int i = 0;
    while(i < ways){
            if (lru[i][index] == way/2){
                    temp = lru[i][index];
                    i++;
                    continue;
            }
            else{
                    temp2[temp_index] = lru[i][index];
                    temp_index++;
            }
            i++;
    }
    for (int j = 0; j < ways - 1; j++){
            lru[j][index] = temp2[j];
    }
    lru[ways - 1][index] = temp;

}

bool assocCache(int **block, int index, int ways, int tag, int **lru){
    bool retVal = false;
    for(int i = 0; i < 2*ways; i = i + 2){
            if (block[i][index] == 0){
                    block[i][index] = 1;
                    block[i+1][index] = tag;
                    LRU_update(lru, index, i, ways);
                    return retVal;
            }
            else{
                    if (block[i+1][index] == tag){
                            retVal = true;
                            LRU_update(lru, index, i, ways);
                            return retVal;
                    }
                    else{
                            continue;
                    }
            }

    }
    int head = 2 * lru[0][index];
    block[head][index] = 1;
    block[head+1][index] = tag;
    LRU_update(lru, index, head, ways);

    return retVal;

}

跟踪文件应该是:

837589,1122102; 932528,1122102; 972661,1122102; 1005547,1122102; //For direct mapped
993999,1122102; 999852,1122102; 999315,1122102; 1000092,1122102; //For set associative
1000500,1122102; //For fully associative (LRU)

我的输出是:

837589,1122102; 932528,1122102; 972661,1122102; 1005547,1122102;
939999,1122102; 999852,1122102; 999315,1122102; 1000092,1122102; 
1000228,1122102; 

正如您所看到的,对于完全关联的一个,它只有正确输出的272。当从16种方式切换到32种方式时,为什么会关闭?

1 个答案:

答案 0 :(得分:0)

啊,我错误地认为,对于32行高速缓存,16KB高速缓存大小的完全关联高速缓存是32路,当它实际上是512路时。