Permalink
Browse files

Merge branch 'cache'

Conflicts:
	Access.cpp
	Access.h
	Block.cpp
	Block.h
	Set.cpp
	Set.h
	cache.cpp
	cache.h
	main.cpp
  • Loading branch information...
2 parents 217989d + 5649cdc commit 776da8c8cc86280e066b4ba2eb5846f922954d84 @pksunkara committed Aug 21, 2011
Showing with 301 additions and 89 deletions.
  1. +7 −3 access.cpp → Access.cpp
  2. +23 −0 Access.h
  3. +40 −0 Block.cpp
  4. +26 −0 Block.h
  5. +70 −0 Cache.cpp
  6. +58 −0 Cache.h
  7. +1 −1 Makefile
  8. +2 −1 memory.cpp → Memory.cpp
  9. +1 −1 memory.h → Memory.h
  10. +42 −0 Set.cpp
  11. +25 −0 Set.h
  12. +0 −22 access.h
  13. +0 −32 cache.cpp
  14. +0 −25 cache.h
  15. +1 −1 main.cpp
  16. +2 −1 matrix/aware.cpp
  17. +2 −1 matrix/oblivious.cpp
  18. +1 −1 matrix/unaware.cpp
View
10 access.cpp → Access.cpp
@@ -1,12 +1,15 @@
#include <iostream>
#include <cstdlib>
-#include "access.h"
+#include <cmath>
+
+#include "Access.h"
using namespace std;
-Access::Access(int cache_size, int block_size, int set_size, int memory_size) {
+Access::Access(int cache_size, int block_size, int assoc, int memory_size) {
+ this->bits_n = log2(memory_size);
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) {
@@ -23,4 +26,5 @@ int Access::get(int addr) {
Access::~Access(void) {
delete this->memory;
+ delete this->cache;
}
View
23 Access.h
@@ -0,0 +1,23 @@
+#ifndef ACCESS_H
+#define ACCESS_H
+
+#include "Memory.h"
+#include "Cache.h"
+
+class Access {
+public:
+ Access(int cache_size, int block_size, int assoc, int memory_size);
+
+ void write(int value);
+ void set(int addr, int value);
+ int get(int addr);
+
+ ~Access(void);
+
+private:
+ Memory* memory;
+ Cache* cache;
+ int bits_n; // Number of bits in the address
+};
+
+#endif
View
40 Block.cpp
@@ -0,0 +1,40 @@
+#include "Block.h"
+
+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() {
+ delete this->words;
+}
View
26 Block.h
@@ -0,0 +1,26 @@
+#ifndef BLOCK_H_
+#define BLOCK_H_
+
+class Block {
+public:
+ 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
70 Cache.cpp
@@ -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_block(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
2 Makefile
@@ -4,7 +4,7 @@ RM= rm -rf
all: sim
-sim: access.o cache.o memory.o matrix/aware.o matrix/unaware.o matrix/oblivious.o main.cpp
+sim: Access.o Cache.o Memory.o Set.o Block.o matrix/aware.o matrix/unaware.o matrix/oblivious.o main.cpp
$(CC) $(CFLAGS) $^ -o $@
%.o: %.c
View
3 memory.cpp → Memory.cpp
@@ -1,6 +1,7 @@
#include <iostream>
#include <cstdlib>
-#include "memory.h"
+
+#include "Memory.h"
using namespace std;
View
2 memory.h → Memory.h
@@ -14,7 +14,7 @@ class Memory {
private:
int* memory;
int size;
- int writer;
+ int writer; // Current position of writer
};
#endif
View
42 Set.cpp
@@ -0,0 +1,42 @@
+#include <cstdlib>
+#include "Set.h"
+
+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 < 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() {
+ for(int i=0; i < this->assoc; i++)
+ delete this->blocks[i];
+}
View
25 Set.h
@@ -0,0 +1,25 @@
+#ifndef SET_H_
+#define SET_H_
+
+#include "Block.h"
+
+class Set {
+public:
+ 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 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
+};
+
+#endif /* SET_H_ */
View
22 access.h
@@ -1,22 +0,0 @@
-#ifndef ACCESS_H
-#define ACCESS_H
-
-#include "memory.h"
-#include "cache.h"
-
-class Access {
- public:
- Access(int cache_size, int block_size, int set_size, int memory_size);
-
- void write(int value);
- void set(int addr, int value);
- int get(int addr);
-
- ~Access(void);
-
- private:
- Memory* memory;
- Cache* cache;
-};
-
-#endif
View
32 cache.cpp
@@ -1,32 +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
-}
-
-int Cache::get_hits(void) {
- return this->hits;
-}
-
-int Cache::get_misses(void) {
- return this->misses;
-}
-
-Cache::~Cache(void) {
- delete this->memory;
-}
View
25 cache.h
@@ -1,25 +0,0 @@
-#ifndef CACHE_H
-#define CACHE_H
-
-class Cache {
- public:
- Cache(int cache_size, int block_size, int set_size);
-
- int get_hits(void);
- int get_misses(void);
-
- ~Cache(void);
-
- private:
- int cache_size;
- int block_size;
- int set_size;
- int set_n;
-
- int hits;
- int misses;
-
- int* memory;
-};
-
-#endif
View
2 main.cpp
@@ -1,7 +1,7 @@
#include <iostream>
#include <cstdlib>
#include <fstream>
-#include "access.h"
+#include "Access.h"
using namespace std;
View
3 matrix/aware.cpp
@@ -1,6 +1,7 @@
#include <iostream>
#include <cstdlib>
-#include "../access.h"
+
+#include "../Access.h"
using namespace std;
View
3 matrix/oblivious.cpp
@@ -1,6 +1,7 @@
#include <iostream>
#include <cstdlib>
-#include "../access.h"
+
+#include "../Access.h"
using namespace std;
View
2 matrix/unaware.cpp
@@ -1,6 +1,6 @@
#include <iostream>
#include <cstdlib>
-#include "../access.h"
+#include "../Access.h"
using namespace std;

0 comments on commit 776da8c

Please sign in to comment.