Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cold & Conflict misses done

  • Loading branch information...
commit 4691547a3013ee97f8d9a3b0a6fce2ac8d02ddcb 1 parent aebb796
@pksunkara authored
View
16 Access.cpp
@@ -9,7 +9,7 @@ using namespace std;
Access::Access(int cache_size, int block_size, int assoc, int data_numbers) {
this->accesses = 0;
this->memory = new Memory(data_numbers, block_size);
- this->cache = new Cache(cache_size, block_size, assoc, log2(data_numbers*4));
+ this->cache = new Cache(cache_size, block_size, assoc, log2(data_numbers*4), this->memory);
}
void Access::print(char* str) {
@@ -34,20 +34,20 @@ void Access::write(int value) {
int Access::get(int addr) {
this->accesses++;
- //cout << addr;
- /*int misses = this->cache->get_misses();
+
+ //int misses = this->cache->get_misses();
int word = this->cache->get_cached(addr);
- if(word==-1 && this->cache->get_misses()==misses+1) {
- */ Block* buf = this->memory->read(addr);
- // this->cache->insert_block(addr, buf);
+ /*if(word==-1 && this->cache->get_misses()==misses+1) {
+ Block* buf = this->memory->read(addr);
+ this->cache->insert_block(addr, buf);
//cout << " inserted" << endl;
return buf->get_word_by_addr(addr);
- /*} else {
+ } else {
//cout << " cached" << endl;
return word;
}*/
- return 0;
+ return word;
}
Access::~Access(void) {
View
1  Access.h
@@ -11,6 +11,7 @@ class Access {
void write(int value);
int get(int addr);
void print(char* str);
+
void restart_cache(void);
~Access(void);
View
13 Block.cpp
@@ -2,9 +2,12 @@
Block::Block(int index, int counter, int block_size) {
this->index = index;
- this->word_n = block_size/4;
- this->words = new int[this->word_n];
this->counter = counter;
+
+ this->num_words = block_size/4;
+ this->words = new int[this->num_words];
+
+ this->tag = -1;
}
int Block::get_tag() {
@@ -19,10 +22,6 @@ int Block::get_word(int index) {
return words[index];
}
-int Block::get_word_by_addr(int addr) {
- return words[addr % word_n];
-}
-
void Block::set_word(int index, int value) {
this->words[index] = value;
}
@@ -39,7 +38,7 @@ void Block::set_block(int counter, int tag_value, Block* new_block) {
this->counter = counter;
this->tag = tag_value;
- for(int i=0; i < this->word_n; i++) {
+ for(int i=0; i<num_words; i++) {
this->words[i] = new_block->get_word(i);
}
}
View
8 Block.h
@@ -12,7 +12,6 @@ class Block {
int get_tag(void);
int get_index(void);
int get_word(int index);
- int get_word_by_addr(int addr);
int get_counter(void);
void set_counter(int counter);
@@ -20,12 +19,13 @@ class Block {
~Block();
private:
- int tag; // Tag bits of the block
+ int* words; // Array of words (integers)
+
int counter; // Unique counter of the block
int index; // Index in the array.
- int word_n; // Number of words present in the block
- int* words; // Array of words (integers)
+ int tag; // Tag bits of the block
+ int num_words; // Number of words present in the block
};
#endif /* BLOCK_H_ */
View
78 Cache.cpp
@@ -6,39 +6,51 @@
using namespace std;
-Cache::Cache(int cache_size, int block_size, int assoc, int bits_n) {
- this->hits = 0;
- this->assoc = assoc;
- this->cache_size = cache_size; // Cache size is given in KB
- this->block_size = block_size; // Block size is given in B
- this->set_size = this->block_size * assoc;
-
- this->set_n = this->cache_size / this->set_size;
- this->bits_n = bits_n;
-
- this->word_bits = log2(this->block_size/4);
- this->set_bits = log2(set_n);
- this->tag_bits = this->bits_n - (this->set_bits + this->word_bits);
-
- this->set_mul = pow(2, this->word_bits) * (pow(2, set_bits) - 1);
- this->tag_mul = pow(2, this->word_bits + this->set_bits) * (pow(2, tag_bits) - 1);
+Cache::Cache(int cache_size, int block_size, int assoc, int num_bits, Memory* memory) {
+ this->memory = memory;
+
+ this->cache_size = cache_size;
+ this->block_size = block_size;
+
+ if(assoc==0) {
+ this->assoc = cache_size/block_size;
+ this->set_size = cache_size;
+ } else {
+ this->assoc = assoc;
+ this->set_size = this->block_size * assoc;
+ }
+
+ this->num_words = this->block_size/4;
+ this->num_sets = this->cache_size/this->set_size;
+ this->num_bits = num_bits;
+
+ this->word_bits = log2(this->num_words);
+ this->set_bits = log2(this->num_sets);
+ this->tag_bits = this->num_bits - (this->set_bits + this->word_bits);
+
this->word_mul = pow(2, this->word_bits) -1;
+ this->set_mul = pow(2, this->word_bits) * (pow(2, this->set_bits) - 1);
+ this->tag_mul = pow(2, this->word_bits + this->set_bits) * (pow(2, this->tag_bits) - 1);
+ // Initialising Statistics
+ this->hits = 0;
for(int i=0; i<4; i++)
this->misses[i] = 0;
- this->data = new Set*[set_n];
- for( int i=0; i<set_n; i++) {
+
+ // Initialising Cache
+ this->data = new Set*[this->num_sets];
+ for( int i=0; i<num_sets; i++) {
this->data[i] = new Set(this->assoc, this->block_size);
}
}
void Cache::restart(void) {
- for(int i=0; i<this->set_n; i++)
+ for(int i=0; i<num_sets; i++)
delete this->data[i];
delete this->data;
- this->data = new Set*[set_n];
- for(int i=0; i<this->set_n; i++)
+ this->data = new Set*[this->num_sets];
+ for(int i=0; i<num_sets; i++)
this->data[i] = new Set(this->assoc, this->block_size);
this->hits = 0;
@@ -67,12 +79,12 @@ int Cache::get_conflict_misses(void) {
}
int Cache::get_cached(int address) {
- int set_num = this->set_mul & address;
- int tag_num = this->tag_mul & address;
int word_num = this->word_mul & address;
+ int set_num = (this->set_mul & address)/pow(2, this->word_bits);
+ int tag_num = (this->tag_mul & address)/pow(2, this->word_bits + this->set_bits);
Set* cur_set = this->data[set_num];
- for(int i=0; i < this->assoc; i++) {
+ for(int i=0; i<assoc; i++) {
Block* cur_block = cur_set->get_block(i);
if(cur_block->get_tag() == tag_num) {
cur_set->inc_counter(cur_block->get_index());
@@ -82,18 +94,28 @@ int Cache::get_cached(int address) {
}
this->misses[0]++;
- return -1;
+
+ Block* cur_block = this->memory->read(address);
+ this->insert_block(address, cur_block);
+ if(cur_set->get_dirty()==false) {
+ this->misses[1]++;
+ cur_set->set_dirty();
+ } else {
+ this->misses[3]++;
+ }
+
+ return cur_block->get_word(address % this->num_words);
}
void Cache::insert_block(int address, Block* new_block) {
- int set_num = this->set_mul & address;
- int tag_num = this->tag_mul & address;
+ int set_num = (this->set_mul & address)/pow(2, this->word_bits);
+ int tag_num = (this->tag_mul & address)/pow(2, this->word_bits + this->set_bits);
this->data[set_num]->insert_block(tag_num, new_block);
}
Cache::~Cache(void) {
- for(int i=0; i<this->set_n; i++)
+ for(int i=0; i<this->num_sets; i++)
delete this->data[i];
delete this->data;
}
View
9 Cache.h
@@ -2,6 +2,7 @@
#define CACHE_H_
#include "Set.h"
+#include "Memory.h"
/* Class: Cache
*
@@ -23,7 +24,7 @@
class Cache {
public:
- Cache(int cache_size, int block_size, int set_size, int bits_n);
+ Cache(int cache_size, int block_size, int assoc, int bits_n, Memory* memory);
int get_hits(void);
int get_misses(void);
@@ -39,14 +40,16 @@ class Cache {
private:
Set** data;
+ Memory* memory;
int cache_size; // Size of cache in bytes
int block_size; // Size of block in bytes
int set_size; // Size of set in bytes
int assoc; // Associativity
- int set_n; // Number of sets
- int bits_n; // Number of bits in Main memory address
+ int num_words; // Number of words per block
+ int num_sets; // Number of sets
+ int num_bits; // Number of bits in Main memory address
int set_bits; // Number of bits for Set
int tag_bits; // Number of bits for Tag
View
57 Set.cpp
@@ -4,46 +4,55 @@
Set::Set(int assoc, int block_size) {
this->assoc = assoc;
this->block_size = block_size;
-
- this->counter = 0;
- this->lru = 0;
-
- this->blocks = new Block*[assoc];
- for(int i=0; i < this->assoc; i++) {
- this->blocks[i] = new Block(i, this->get_counter(), block_size);
+ this->_counter = 0;
+
+ // Initialising dirty and Blocks
+ this->dirty = new bool[assoc];
+ this->data = new Block*[assoc];
+ for(int i=0; i<assoc; i++) {
+ this->data[i] = new Block(i, this->counter(), block_size);
+ this->dirty[i] = false;
}
}
Block* Set::get_block(int index) {
- return this->blocks[index];
+ return this->data[index];
+}
+
+bool Set::get_dirty(void) {
+ return this->dirty[this->lru];
}
void Set::insert_block(int tag_value, Block* new_block) {
- int least = 0;
+ this->lru = 0;
- for(int i=0; i < this->assoc; i++) {
- Block* temp = this->blocks[i];
- if(temp->get_counter() < (this->blocks[least])->get_counter())
- least = i;
+ for(int i=0; i<assoc; i++) {
+ Block* temp = this->data[i];
+ if(temp->get_counter() < (this->data[lru])->get_counter())
+ this->lru = i;
}
- this->lru = least;
- Block* temp_block = this->blocks[this->lru];
- temp_block->set_block(this->get_counter(), tag_value, new_block);
+ Block* temp_block = this->data[this->lru];
+ temp_block->set_block(this->counter(), tag_value, new_block);
}
-int Set::get_counter() {
- this->counter++;
- return this->counter;
+int Set::counter() {
+ this->_counter++;
+ return this->_counter;
}
void Set::inc_counter(int index) {
- Block* temp_block = this->blocks[index];
- temp_block->set_counter(this->get_counter());
+ Block* temp_block = this->data[index];
+ temp_block->set_counter(this->counter());
+}
+
+void Set::set_dirty(void) {
+ this->dirty[this->lru] = true;
}
Set::~Set() {
- for(int i=0; i < this->assoc; i++)
- delete this->blocks[i];
- delete this->blocks;
+ for(int i=0; i<assoc; i++)
+ delete this->data[i];
+ delete this->data;
+ delete this->dirty;
}
View
16 Set.h
@@ -8,20 +8,24 @@ class Set {
Set(int assoc, int block_size);
Block* get_block(int index);
+ bool get_dirty(void);
+
void insert_block(int tag_value, Block* new_block);
- int get_counter(void);
+ int counter(void);
void inc_counter(int index);
+ void set_dirty(void);
+
~Set();
private:
+ Block** data; // Array of Blocks
+ bool* dirty; // Array of dirty bits
+
int assoc; // Associativity
int block_size; // Size of each block
-
- int counter; // Counter for giving unique index to each block
- int lru; // Index of block having least value of counter
-
- Block** blocks; // Array of Blocks
+ int _counter; // Counter for giving unique index to each block
+ int lru; // Index of block having the least value of counter
};
#endif /* SET_H_ */
View
7 main.cpp
@@ -53,24 +53,25 @@ int main(int argc, char** argv) {
// Cache Unaware matrix multiplication
cache_unaware();
- accessor->print("unaware");
+ accessor->print((char*)"unaware");
// Restart Cache
accessor->restart_cache();
// Cache Aware matrix multiplication
cache_aware(2);
- accessor->print("aware");
+ accessor->print((char*)"aware");
// Restart Cache
accessor->restart_cache();
// Cache Oblivious matrix multiplicatio
cache_oblivious();
- accessor->print("oblivious");
+ accessor->print((char*)"oblivious");
cout << endl;
+ delete accessor;
return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.