Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Many changes which are listed below

  * Memory size is not needed from now
  * Using rand() instead of input
  * Comment out caching
  * Change the printing data
  * Correct matrix algorithms
  * Change some variable names
  • Loading branch information...
commit 8a940544105483ac994e583beae42207bf89f217 1 parent 72bf494
@pksunkara authored
View
30 Access.cpp
@@ -6,18 +6,20 @@
using namespace std;
-Access::Access(int cache_size, int block_size, int assoc, int memory_size) {
+Access::Access(int cache_size, int block_size, int assoc, int data_numbers) {
this->accesses = 0;
- this->memory = new Memory(memory_size, block_size);
- this->cache = new Cache(cache_size, block_size, assoc, log2(memory_size*1024));
+ this->memory = new Memory(data_numbers, block_size);
+ this->cache = new Cache(cache_size, block_size, assoc, log2(data_numbers*4));
}
-void Access::print(void) {
- cout << endl << "\tCache Statistics:" << endl;
- cout << "\t\tAccesses:" << this->accesses << endl;
- cout << "\t\tHits: " << this->cache->get_hits() << endl;
- cout << "\t\tMisses: " << this->cache->get_misses() << endl;
- cout << "\t\tCold Misses: " << this->cache->get_cold_misses() << endl;
+void Access::print(char* str) {
+ cout << "Cache " << str << " Statistics:" << endl;
+ cout << "\tAccesses: " << this->accesses << endl;
+ cout << "\tHits: " << this->cache->get_hits() << endl;
+ cout << "\tMisses: " << this->cache->get_misses() << endl;
+ cout << "\tCold Misses: " << this->cache->get_cold_misses() << endl;
+ cout << "\tCapacity Misses: " << this->cache->get_capacity_misses() << endl;
+ cout << "\tConflict Misses: " << this->cache->get_conflict_misses() << endl;
cout << endl << endl;
}
@@ -33,17 +35,17 @@ 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);
+ */ 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;
}
View
4 Access.h
@@ -6,11 +6,11 @@
class Access {
public:
- Access(int cache_size, int block_size, int assoc, int memory_size);
+ Access(int cache_size, int block_size, int assoc, int data_numbers);
void write(int value);
int get(int addr);
- void print(void);
+ void print(char* str);
void restart_cache(void);
~Access(void);
View
22 Cache.cpp
@@ -9,7 +9,7 @@ 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*1024; // Cache size is given in KB
+ 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;
@@ -26,20 +26,20 @@ Cache::Cache(int cache_size, int block_size, int assoc, int bits_n) {
for(int i=0; i<4; i++)
this->misses[i] = 0;
- this->memory = new Set*[set_n];
+ this->data = new Set*[set_n];
for( int i=0; i<set_n; i++) {
- this->memory[i] = new Set(this->assoc, this->block_size);
+ this->data[i] = new Set(this->assoc, this->block_size);
}
}
void Cache::restart(void) {
for(int i=0; i<this->set_n; i++)
- delete this->memory[i];
- delete this->memory;
+ delete this->data[i];
+ delete this->data;
- this->memory = new Set*[set_n];
+ this->data = new Set*[set_n];
for(int i=0; i<this->set_n; i++)
- this->memory[i] = new Set(this->assoc, this->block_size);
+ this->data[i] = new Set(this->assoc, this->block_size);
this->hits = 0;
for(int i=0; i<4; i++)
@@ -71,7 +71,7 @@ int Cache::get_cached(int address) {
int tag_num = this->tag_mul & address;
int word_num = this->word_mul & address;
- Set* cur_set = this->memory[set_num];
+ Set* cur_set = this->data[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) {
@@ -89,11 +89,11 @@ void Cache::insert_block(int address, Block* new_block) {
int set_num = this->set_mul & address;
int tag_num = this->tag_mul & address;
- this->memory[set_num]->insert_block(tag_num, new_block);
+ this->data[set_num]->insert_block(tag_num, new_block);
}
Cache::~Cache(void) {
for(int i=0; i<this->set_n; i++)
- delete this->memory[i];
- delete this->memory;
+ delete this->data[i];
+ delete this->data;
}
View
2  Cache.h
@@ -38,7 +38,7 @@ class Cache {
~Cache(void);
private:
- Set** memory;
+ Set** data;
int cache_size; // Size of cache in bytes
int block_size; // Size of block in bytes
View
13 INPUT
@@ -1,12 +1 @@
-5 4 6
-
-0 1 2 3
-4 5 6 7
-8 9 0 1
-2 3 4 5
-6 7 8 9
-
-1 2 3 4 5 6
-7 8 9 0 1 2
-3 4 5 6 7 8
-9 0 1 2 3 4
+10 10 16
View
32 Memory.cpp
@@ -5,36 +5,36 @@
using namespace std;
-Memory::Memory(int memory_size, int block_size) {
- this->blocks_n = memory_size*1024/block_size;
- this->words_n = block_size/4;
+Memory::Memory(int data_numbers, int block_size) {
+ this->num_words = block_size/4;
+ this->num_blocks = data_numbers/this->num_words + 1;
this->writer = 0;
- this->memory = new Block*[this->blocks_n];
- for(int i=0; i<blocks_n; i++)
- this->memory[i] = new Block(i, i, block_size);
+ this->data = new Block*[this->num_blocks];
+ for(int i=0; i<num_blocks; i++)
+ this->data[i] = new Block(i, i, block_size);
}
void Memory::write(int value) {
- int block_num = writer/words_n;
- this->memory[block_num]->set_word(writer % words_n, value);
+ int block_num = writer/num_words;
+ this->data[block_num]->set_word(writer % num_words, value);
this->writer++;
}
Block* Memory::read(int address) {
- int block_num = address / words_n;
- return this->memory[block_num];
+ int block_num = address/num_words;
+ return this->data[block_num];
}
void Memory::print(void) {
- for(int i=0; i<blocks_n; i++) {
- for(int j=0; j<words_n; j++)
- cout << "\t" << this->memory[i]->get_word(j);
+ for(int i=0; i<num_blocks; i++) {
+ for(int j=0; j<num_words; j++)
+ cout << "\t" << this->data[i]->get_word(j);
cout << endl;
}
}
Memory::~Memory(void) {
- for(int i=0; i<blocks_n; i++)
- delete this->memory[i];
- delete this->memory;
+ for(int i=0; i<num_blocks; i++)
+ delete this->data[i];
+ delete this->data;
}
View
8 Memory.h
@@ -5,7 +5,7 @@
class Memory {
public:
- Memory(int memory_size, int block_size);
+ Memory(int data_numbers, int block_size);
Block* read(int address);
void write(int value);
@@ -14,10 +14,10 @@ class Memory {
~Memory(void);
private:
- Block** memory;
+ Block** data;
- int blocks_n;
- int words_n;
+ int num_blocks;
+ int num_words;
int writer;
};
View
29 main.cpp
@@ -20,13 +20,13 @@ int A(int x, int y);
int B(int x, int y);
int main(int argc, char** argv) {
- if(argc!=5) {
- cout << "Usage: " << argv[0] << " <cache_size_inKB> <block_size_inB> <associativity> <memory_size_inKB>\n";
+ if(argc!=4) {
+ cout << "Usage: " << argv[0] << " <cache_size_inKB> <block_size_inB> <associativity>\n";
return 1;
}
// Declaring variables
- int i, j, buf;
+ int i, j;
ifstream inFile;
// Opening files
@@ -38,44 +38,39 @@ int main(int argc, char** argv) {
return 1;
}
- // Initialise accessor
- accessor = new Access(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
-
// Reading matrix lengths
inFile >> matrix_n >> matrix_m >> matrix_p;
+ // Initialise accessor
+ accessor = new Access(atoi(argv[1])*1024, atoi(argv[2]), atoi(argv[3]), (matrix_n+matrix_p)*matrix_m);
+
// Reading matrices A & B
for(i=0; i<matrix_n; i++)
for(j=0; j<matrix_m; j++) {
- inFile >> buf;
- accessor->write(buf);
+ accessor->write(rand());
}
for(i=0; i<matrix_m; i++)
for(j=0; j<matrix_p; j++) {
- inFile >> buf;
- accessor->write(buf);
+ accessor->write(rand());
}
// Cache Unaware matrix multiplication
- cout << "Cache Unaware:" << endl;
cache_unaware();
- accessor->print();
+ accessor->print("unaware");
// Restart Cache
accessor->restart_cache();
// Cache Aware matrix multiplication
- cout << "Cache Aware:" << endl;
cache_aware(2);
- accessor->print();
+ accessor->print("aware");
// Restart Cache
accessor->restart_cache();
- // Cache Oblivious matrix multiplication
- cout << "Cache Oblivious:" << endl;
+ // Cache Oblivious matrix multiplicatio
cache_oblivious();
- accessor->print();
+ accessor->print("oblivious");
// Closing files
inFile.close();
View
26 matrix/aware.cpp
@@ -10,24 +10,18 @@ extern int matrix_n, matrix_m, matrix_p;
extern int A(int, int);
extern int B(int, int);
-void cache_aware(int s);
-void matrix_ord(int n, int m, int p, int i, int j, int k, int s);
-
+void ord_matrix_multi(int, int, int, int);
void cache_aware(int s){
- int i, j, k;
-
- for(i = 0; i < matrix_n/s; i++)
- for(j = 0; j < matrix_m/s; j++)
- for(k = 0 ; k < matrix_p/s; k++)
- matrix_ord(matrix_n, matrix_m, matrix_p, i, k, j, s);
+ for(int i=0; i<=matrix_n/s; i++)
+ for(int j=0; j<=matrix_m/s; j++)
+ for(int k=0; k<=matrix_p/s; k++)
+ ord_matrix_multi(i, j, k, s);
}
-void matrix_ord(int n, int m, int p, int i, int j, int k, int s){
- int w, x, y;
-
- for(w=i*s; w < min(n, (i+1)*s); w++)
- for(x=j*s; x < min(m, (j+1)*s); x++)
- for(y=k*s; y < min(p, (k+1)*s); y++)
- A(w,y)*B(y,x);
+void ord_matrix_multi(int i, int j, int k, int s){
+ for(int x=i*s; x < min(matrix_n, (i+1)*s); x++)
+ for(int y=j*s; y < min(matrix_m, (j+1)*s); y++)
+ for(int z=k*s; z < min(matrix_p, (k+1)*s); z++)
+ A(x,y)*B(y,z);
}
View
1  matrix/oblivious.cpp
@@ -13,7 +13,6 @@ extern int B(int, int);
void recur_func(int, int, int, int, int, int, int);
void cache_oblivious(void) {
- int i, j, k;
recur_func(matrix_n, matrix_m, matrix_p, 0, 0, 0, 0);
}
View
19 matrix/unaware.cpp
@@ -10,19 +10,8 @@ extern int A(int, int);
extern int B(int, int);
void cache_unaware(void) {
- int i, j, k;
-
- int** C = new int*[matrix_n];
- for(i=0; i<matrix_n; i++) {
- C[i] = new int[matrix_p];
- }
-
- for(i=0; i<matrix_n; i++) {
- for(j=0; j<matrix_p; j++) {
- for(k=0; k<matrix_m; k++)
- C[i][j] += A(i,k)*B(k,j);
- cout << "\t" << C[i][j];
- }
- cout << endl;
- }
+ for(int i=0; i<matrix_n; i++)
+ for(int j=0; j<matrix_p; j++)
+ for(int k=0; k<matrix_m; k++)
+ A(i,k)*B(k,j);
}
Please sign in to comment.
Something went wrong with that request. Please try again.