Download presentation
Presentation is loading. Please wait.
1
ECE 353 Lab 1: Cache Simulation
2
Purpose Introduce C programming by means of a simple example Reinforce your knowledge of set associative caches
3
C Many of the constructs are very similar to those found in Java In many other respects, C is more primitive: –Not object-oriented –Lacks inbuilt garbage collection, some checks (e.g. array bounds) –C carries out implicit type conversions when appropriate
4
Some C Constructs Relational, logical –, >=, ==, != –&&, ||, ! Arithmetic –+, -, *, /, %
5
C Constructs (contd.) Some control constructs: –while( ) { }; –if ( ) { }; –for (i=0; i<100; i++) { };
6
Data Types int char float double long short
7
Arrays x[1000] is a linear array of 1000 elements Y[100][100] is a two-dimensional 100x100 array Variable names (like other things in C) are type-sensitive: Y is not the same as y.
8
Example #include #define SIZE 100 main(){ int iter; int a[SIZE], b[SIZE], c[SIZE]; for (iter=0; iter<SIZE; iter++){ c[iter] = iter*3; b[iter] = iter/2; a[iter] = b[iter]+c[iter]; } printf(“My first C program\n”); for (iter=0; iter<SIZE; iter++) printf(“a[%d] = %d; b[%d]= %d; c[%d]= %d\n”, iter, a[iter], iter, b[iter], iter, c[iter]); }
9
Pointers A pointer is a variable which contains the address of some other variable. –int *i is the declaration of an integer whose address is stored in i. –int j is the declaration of another integer; its address is obtained by using the address operator, &. That is, the location of j is given by &j.
10
Opening Files: Example #include main(){ int x, y; FILE *ifp; //Pointer to a file is declared ifp = fopen(“trace.txt”, “r”); // ifp points to file // trace.txt, opened for // reading while (!feof(ifp) { // exit if end-of-file reached fscanf(ifp, “%d %x”, &x, &y); // read next line printf(“Address read = %x\n”, y);// print as required } fclose(ifp); // Close file } If individual file lines consist of an integer followed by a hex quantity, and we want to print out the hex quantity:
11
Cache Simulation Parameters: –Cache size, in bytes –Block or line size –Set associativity, i.e., number of lines per set Hit rate is the fraction of memory accesses that can be served by the cache
12
Cache Simulation (contd.) Given the following: –Cache parameters –Memory access trace Obtain: –Cache hit rate
13
Set Associative Organization © 2004 Morgan-Kaufman Publishers
14
Use split caches because there is more spatial locality in code: Typical Performance © 2004 Morgan-Kaufman Publishers
15
Simulation Maintain the cache directory (as an array) and LRU status of the lines within each set When an access is made, update LRU status. –If a hit, record it as such –If a miss, update the contents of the directory
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.