Permalink
Browse files

Finished implemeting Cache with Sets and Blocks

  • Loading branch information...
SIddardha G
SIddardha G committed Aug 21, 2011
1 parent fa2404c commit 5649cdc2cde475d6d71e9655cfe98a91fc1e13ba
Showing with 248 additions and 94 deletions.
  1. +8 −3 access.cpp → Access.cpp
  2. +5 −3 access.h → Access.h
  3. +33 −2 Block.cpp
  4. +17 −1 Block.h
  5. +70 −0 Cache.cpp
  6. +58 −0 Cache.h
  7. 0 INPUT
  8. 0 Makefile
  9. +2 −1 memory.cpp → Memory.cpp
  10. 0 memory.h → Memory.h
  11. 0 README.md
  12. +34 −13 Set.cpp
  13. +13 −4 Set.h
  14. +0 −36 cache.cpp
  15. +0 −25 cache.h
  16. +3 −3 main.cpp
  17. +2 −1 matrix/aware.cpp
  18. +2 −1 matrix/oblivious.cpp
  19. +1 −1 matrix/unaware.cpp
View
11 access.cpp → Access.cpp 100755 → 100644
@@ -1,13 +1,17 @@
#include <iostream>
#include <cstdlib>
-#include "access.h"
+#include <cmath>
+
+#include "Access.h"
using namespace std;
-Access::Access(int matrix_n, int cache_size, int block_size, int set_size, int memory_size) {
+Access::Access(int matrix_n, int cache_size, int block_size, int assoc, int memory_size) {
+ this->bits_n = log2(memory_size);
+
this->matrix_n = matrix_n;
this->memory = new Memory(memory_size);
- this->cache = new Cache(cache_size, block_size, set_size);
+ this->cache = new Cache(cache_size, block_size, assoc, this->bits_n);
}
void Access::write(int value) {
@@ -24,4 +28,5 @@ int Access::get(int x, int y) {
Access::~Access(void) {
delete this->memory;
+ delete this->cache;
}
View
8 access.h → Access.h 100755 → 100644
@@ -1,12 +1,12 @@
#ifndef ACCESS_H
#define ACCESS_H
-#include "memory.h"
-#include "cache.h"
+#include "Memory.h"
+#include "Cache.h"
class Access {
public:
- Access(int matrix_n, int cache_size, int block_size, int set_size, int memory_size);
+ Access(int matrix_n, int cache_size, int block_size, int assoc, int memory_size);
void write(int value);
void set(int x, int y, int value);
@@ -19,6 +19,8 @@ class Access {
Memory* memory;
Cache* cache;
+ int bits_n; // Number of bits in the address
+
};
#endif
View
35 Block.cpp 100755 → 100644
@@ -1,9 +1,40 @@
#include "Block.h"
-Block::Block(int block_size) {
+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;
+}
+
+int Block::get_tag() {
+ return this->tag;
+}
+
+int Block::get_index() {
+ return this->index;
+}
+
+int Block::get_word(int index) {
+ return words[index];
+}
+
+int Block::get_counter() {
+ return this->counter;
+}
+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++) {
+ this->words[i] = new_block->get_word(i);
+ }
+}
+void Block::set_tag(int tag) {
+ this->tag = tag;
}
Block::~Block() {
- // TODO Auto-generated destructor stub
+ delete this->words;
}
View
18 Block.h 100755 → 100644
@@ -3,8 +3,24 @@
class Block {
public:
- Block(int block_size);
+ Block(int index, int counter, int block_size);
~Block();
+
+ void set_block(int counter, int tag, Block* new_block);
+ void set_tag(int tag);
+
+ int get_tag();
+ int get_index();
+ int get_word(int index);
+ int get_counter();
+
+private:
+ int tag; // Tag bits of the block
+ 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)
};
#endif /* BLOCK_H_ */
View
@@ -0,0 +1,70 @@
+#include <iostream>
+#include <cstdlib>
+#include <cmath>
+
+#include "Cache.h"
+
+using namespace std;
+
+Cache::Cache(int cache_size, int block_size, int assoc, int bits_n) {
+ this->hits = 0;
+ this->misses = 0;
+
+ this->assoc = assoc;
+ this->cache_size = cache_size*1024; // 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 = 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 = pow10(this->word_bits) * (pow(2, set_bits) - 1);
+ this->tag_mul = pow10(this->word_bits + this->set_bits) * (pow(2, tag_bits) - 1);
+ this->word_mul = pow(2, this->word_bits) -1;
+
+ for( int i=0; i<set_n; i++) {
+ this->memory[i] = new Set(this->assoc, this->block_size);
+ }
+}
+
+int Cache::get_hits(void) {
+ return this->hits;
+}
+
+int Cache::get_misses(void) {
+ return this->misses;
+}
+
+int Cache::get_memory(int address) {
+ int set_num = this->set_mul & address;
+ int tag_num = this->tag_mul & address;
+ int word_num = this->word_mul & address;
+
+ Set* cur_set = this->memory[set_num];
+ for(int i=0; i < this->assoc; i++) {
+ Block* cur_block = cur_set->get_block(i);
+ if(cur_block->get_tag() == tag_num) {
+ this->hits++;
+ return cur_block->get_word(word_num);
+ }
+ }
+
+ this->misses++;
+ return -1;
+}
+
+void Cache::insert_block(int address, Block* new_block) {
+ int set_num = this->set_mul & address;
+ int tag_num = this->tag_mul & address;
+
+ Set* cur_set = memory[set_num];
+ cur_set->insert(tag_num, new_block);
+}
+
+Cache::~Cache(void) {
+ delete this->memory;
+}
View
58 Cache.h
@@ -0,0 +1,58 @@
+#ifndef CACHE_H
+#define CACHE_H
+
+#include "Set.h"
+
+/* Class: Cache
+ *
+ * We have implemented Set-Associative Mapping i.e. a combination
+ * of direct and associative-mapping techniques.
+ *
+ * Blocks of cache are grouped into sets, and the mapping allows a
+ * block of the main memory to reside in any block of a specific set.
+ *
+ * Main memory Address is parsed to the following format
+ * -----------------------------------
+ * | Tag Bits | Set Bits | Word Bits |
+ * -----------------------------------
+ * The number of bits for tag, set and word are calculated from
+ * the block size, cache size and associativity.
+ */
+
+
+class Cache {
+ public:
+ Cache(int cache_size, int block_size, int set_size, int bits_n);
+
+ int get_hits(void);
+ int get_misses(void);
+ int get_memory(int address);
+
+ void insert_block(int address, Block* new_block);
+
+ ~Cache(void);
+
+ private:
+ 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 set_bits; // Number of bits for Set
+ int tag_bits; // Number of bits for Tag
+ int word_bits; // Number of bits for Word
+
+ int set_mul; // Number to be bitwise-multiplied with location to get set index
+ int tag_mul; // Number to be bitwise-multiplied with location to get tag index
+ int word_mul; // Number to be bitwise-multiplied with location to get word index
+
+ int hits; // Number of hits
+ int misses; // Number of misses
+
+ Set** memory; // Cache memory - Has set_n sets
+};
+
+#endif /* CACHE_H_ */
View
0 INPUT 100755 → 100644
No changes.
View
0 Makefile 100755 → 100644
No changes.
View
3 memory.cpp → Memory.cpp 100755 → 100644
@@ -1,6 +1,7 @@
#include <iostream>
#include <cstdlib>
-#include "memory.h"
+
+#include "Memory.h"
using namespace std;
View
0 memory.h → Memory.h 100755 → 100644
File renamed without changes.
View
0 README.md 100755 → 100644
No changes.
View
47 Set.cpp 100755 → 100644
@@ -1,21 +1,42 @@
-/*
- * Set.cpp
- *
- * Created on: 21-Aug-2011
- * Author: gsiddardha
- */
-
+#include <cstdlib>
#include "Set.h"
-Set::Set(int set_size, int block_size) {
- this->set_size = set_size;
- this->block_size = block_size;
+Set::Set(int assoc, int block_size) {
+ this->assoc = assoc;
+ this->block_size = block_size;
+
+ this->counter = 0;
+ this->lru = 0;
- for(int i=0; i<set_size; i++) {
- this->block[i] = new Block(block_size);
+ for(int i=0; i < this->assoc; i++) {
+ this->blocks[i] = new Block(i, this->get_counter(), block_size);
}
}
+Block* Set::get_block(int index) {
+ return this->blocks[index];
+}
+
+void Set::insert_block(int tag_value, Block* new_block) {
+ int least = 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;
+ }
+ this->lru = least;
+
+ Block* temp_block = this->blocks[this->lru];
+ temp_block->set_block(this->get_counter(), tag_value, new_block);
+}
+
+int Set::get_counter() {
+ this->counter++;
+ return this->counter;
+}
+
Set::~Set() {
- // TODO Auto-generated destructor stub
+ for(int i=0; i < this->assoc; i++)
+ delete this->blocks[i];
}
View
17 Set.h 100755 → 100644
@@ -1,16 +1,25 @@
#ifndef SET_H_
#define SET_H_
+#include "Block.h"
+
class Set {
public:
- Set();
+ Set(int assoc, int block_size);
~Set();
+ Block* get_block(int index);
+ void insert_block(int tag_value, Block* new_block);
+ int get_counter();
+
private:
- int set_size;
- int block_size;
+ 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;
+ Block** blocks; // Array of Blocks
};
#endif /* SET_H_ */
View
@@ -1,36 +0,0 @@
-#include <iostream>
-#include <cstdlib>
-#include "cache.h"
-
-using namespace std;
-
-Cache::Cache(int cache_size, int block_size, int set_size) {
- this->hits = 0;
- this->misses = 0;
-
- // Cache size is given in KB
- this->cache_size = cache_size*1024;
-
- // Block size is given in B
- this->block_size = block_size;
- this->set_size = set_size;
-
- this->set_n = this->cache_size / (this->set_size * this->block_size);
- //TODO: Introduce blocks later
- for( int i=0; i<set_n; i++) {
- this->memory[i] = new Set(this->set_size, this->block_size);
- }
-
-}
-
-int Cache::get_hits(void) {
- return this->hits;
-}
-
-int Cache::get_misses(void) {
- return this->misses;
-}
-
-Cache::~Cache(void) {
- delete this->memory;
-}
Oops, something went wrong.

0 comments on commit 5649cdc

Please sign in to comment.