Download presentation
Presentation is loading. Please wait.
Published byHector Maximillian Quinn Modified over 9 years ago
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
Caches Motivation: The speed differential between main memory and processors is increasing. Main memory may take over a hundred CPU cycles to access. Aim: Achieve the speed of high-speed memory, while keeping the cost per bit close to that of low-speed memory Exploits locality: Temporal locality Spatial locality Many machines have two levels of cache
4
Units of Movement The contents of Main memory: Organized into pages. Cache: Organized into blocks or lines. Material brought into main memory is moved in these units
5
Cache Blocks or Lines Each memory address belongs to a cache block. The cache block address is the byte address with the rightmost lg B bits removed, where B is the size of the block
6
Parts of a Cache Directory: Where the tags associated with the cache contents are stored. Each directory entry corresponds to a matching line in the cache When looking for an item in cache, we seek to match the tag of the desired item against the contents of the directory. In addition to the tag, we also have a valid bit. Only return values from the cache if the tag matches and the valid bit indicates the line is valid. Data Array: Where the contents of the cache are stored.
7
Fully Associative Cache Any block can be placed anywhere in the cache This flexibility may reduce the miss rate When looking for an item, we need to search through the entire directory looking for a tag match This parallel search can increase hardware complexity and the time taken to execute the match
8
Direct-Mapped Cache Each cache set consists of exactly one line This reduces flexibility, which may increase the miss rate To check if the desired item is in the cache, Identify the set that the item maps to Check the contents of the single directory entry corresponding to that set. Only one match needs to be done: can be done faster.
9
K-way Caches Each set consists of K lines When looking for an item in cache, Identify the set to which it belongs Do a parallel search among all the K tags in the directory corresponding to the K lines in that set
10
Set Associative Organization © 2004 Morgan-Kaufman Publishers
11
Typical Performance Use split caches because there is more spatial locality in code: © 2004 Morgan-Kaufman Publishers
12
Cache Simulation Used to determine the miss rate for a given trace and cache configuration. The trace is a sequence of memory addresses The cache configuration is Data capacity of the cache Number of lines (or blocks) per set Number of bytes per line
13
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
14
Some C Constructs Relational, logical , >=, ==, != &&, ||, ! Arithmetic +, -, *, /, %
15
C Constructs (contd.) Some control constructs: while( ) { }; if ( ) { }; for (i=0; i<100; i++) { };
16
Data Types int char float double long short
17
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 case- sensitive: Y is not the same as y.
18
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]); }
19
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. The pointer to an array is just the name of the array. Example: For some array a[100], *a is a[0] and *(a+i) is a[i]
20
Opening and Reading 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:
21
Opening and Writing into Files: Example #include main(){ int a[100], i; FILE *output; for (i=0; i<100; i++) a[i]=i; output=fopen(“example.txt”,”w”); for (i=0; i<100; i++) fprintf(output, “%5d\n”, a[i]); fclose(output) }
22
Cache Simulation (contd.) Given the following: Cache parameters Memory access trace Obtain: Cache hit rate
23
Simulation Maintain the cache directory 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
24
Cache Directory Implementation 1: Implement the directory as an array, with array entries corresponding to directory entries. Advantage: Simpler to program if you don’t have much C experience Disadvantage: Array sizes are static. The array would have to be large enough to accommodate the largest sized directory that the simulator is designed for
25
Cache Directory (contd.) Implementation 2: Use pointers and malloc Advantage: Is flexible and assigns memory dynamically. Disadvantage: If this is your first non-trivial C program, assigning and freeing memory dynamically can be difficult. malloc: Dynamically assigns memory. The call malloc(b) Allocates memory that is sufficient to hold b bytes. Returns a pointer to the starting address of that chunk of memory To allocate memory sufficient to hold b integers, use malloc(b*sizeof(int))
26
Example of malloc int *dir_ptr; int k; //value of k can change dir_ptr = (int *) malloc(k * sizeof(int));
27
Freeing Memory Memory that is allocated using malloc must be freed once it is no longer needed. To free the memory that was allocated in the previous example, set dir_ptr to point to the starting point of the allocated chunk and do the call free(dir_ptr)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.