Extra Credit 4

CMSC 411 / Olano, Spring 2012

Consider the following 3x3 matrix multiply

float r[3], v[3], m[3][3];
for(int j=0; j<3; ++j) {
  r[j] = 0.0;
  for(int i=0; i<3; ++i) {
    r[j] = r[j] + m[j][i]*v[i];
  }
}

Write a simulation of a direct mapped write-back cache. Use this struct to represent a cache line:

struct CacheLine {
  unsigned int dirtyBit;
  unsigned int validBit;
  unsigned int tag;
  unsigned char data[BLOCKSIZE];
}

Your cache should be an array of these structs. This struct is bigger than the actual cache, since the two bit fields and tag are all represented with full ints, but can tell you the exact number of hits or misses of a real cache with matching parameters. Write two functions, cache_read(address) and cache_write(address) that print "read hit (address)", "read miss (address)", "write hit (address)", "write miss (address)", and "write back (address)".

The following modification, embedded into a C program harness, uses &r[j], &m[j][i], and &v[i], to provide a trace of the exact memory locations accessed

int main() {
    int i, j;
    float r[3]={0}, v[3]={0}, m[3][3]={0};
    for(j=0; j<3; ++j) {
        cache_write(&r[j]);
        r[j] = 0.0;
        for(i=0; i<3; ++i) {
            cache_read(&m[j][i]);
            cache_read(&v[i]);
            cache_write(&r[j]);
            r[j] = r[j] + m[j][i]*v[i];
        }
    }
    return 0;
}

Produce a trace using eight 4-byte cache blocks and using four 8-byte cache blocks.

Create a directory called ec4 in your class cvs directory, and submit your code there.