ECE 353 Lab 1: Cache Simulation. Purpose Introduce C programming by means of a simple example Reinforce your knowledge of set associative caches.

Slides:



Advertisements
Similar presentations
Memory.
Advertisements

Cache Here we focus on cache improvements to support at least 1 instruction fetch and at least 1 data access per cycle – With a superscalar, we might need.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
ECE 353 Lab 1: Cache Simulation. Purpose Introduce C programming by means of a simple example Reinforce your knowledge of set associative caches.
The Memory Hierarchy CPSC 321 Andreas Klappenecker.
Memory Management (II)
1 COMP 206: Computer Architecture and Implementation Montek Singh Mon, Oct 31, 2005 Topic: Memory Hierarchy Design (HP3 Ch. 5) (Caches, Main Memory and.
Memory Management.
Memory Organization.
1  1998 Morgan Kaufmann Publishers Chapter Seven Large and Fast: Exploiting Memory Hierarchy.
1  Caches load multiple bytes per block to take advantage of spatial locality  If cache block size = 2 n bytes, conceptually split memory into 2 n -byte.
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
Chapter 91 Translation Lookaside Buffer (described later with virtual memory) Frame.
An Intelligent Cache System with Hardware Prefetching for High Performance Jung-Hoon Lee; Seh-woong Jeong; Shin-Dug Kim; Weems, C.C. IEEE Transactions.
Pointers Applications
Cache Memories Effectiveness of cache is based on a property of computer programs called locality of reference Most of programs time is spent in loops.
COEN 180 Main Memory Cache Architectures. Basics Speed difference between cache and memory is small. Therefore:  Cache algorithms need to be implemented.
Memory Hierarchy and Cache Design The following sources are used for preparing these slides: Lecture 14 from the course Computer architecture ECE 201 by.
Memory Management Last Update: July 31, 2014 Memory Management1.
Memory Allocation CS Introduction to Operating Systems.
Cache memory October 16, 2007 By: Tatsiana Gomova.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
CMPE 421 Parallel Computer Architecture
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
How to Build a CPU Cache COMP25212 – Lecture 2. Learning Objectives To understand: –how cache is logically structured –how cache operates CPU reads CPU.
10/18: Lecture topics Memory Hierarchy –Why it works: Locality –Levels in the hierarchy Cache access –Mapping strategies Cache performance Replacement.
Computer Architecture Memory organization. Types of Memory Cache Memory Serves as a buffer for frequently accessed data Small  High Cost RAM (Main Memory)
Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Malloc & VM By sseshadr. Agenda Administration – Process lab code will be inked by Thursday (pick up in ECE hub) – Malloc due soon (Thursday, November.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
COMP SYSTEM ARCHITECTURE HOW TO BUILD A CACHE Antoniu Pop COMP25212 – Lecture 2Jan/Feb 2015.
Nov. 15, 2000Systems Architecture II1 Machine Organization (CS 570) Lecture 8: Memory Hierarchy Design * Jeremy R. Johnson Wed. Nov. 15, 2000 *This lecture.
Caching Chapter 7.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
1 Appendix C. Review of Memory Hierarchy Introduction Cache ABCs Cache Performance Write policy Virtual Memory and TLB.
LECTURE 12 Virtual Memory. VIRTUAL MEMORY Just as a cache can provide fast, easy access to recently-used code and data, main memory acts as a “cache”
Cache Organization 1 Computer Organization II © CS:APP & McQuain Cache Memory and Performance Many of the following slides are taken with.
Introduction to C Programming CE Lecture 6 Functions, Parameters and Arguments.
CACHE MEMORY CS 147 October 2, 2008 Sampriya Chandra.
Operating Systems A Biswas, Dept. of Information Technology.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
COSC2410: LAB 19 INTRODUCTION TO MEMORY/CACHE DIRECT MAPPING 1.
LINKED LISTS.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSCI206 - Computer Organization & Programming
CMSC 611: Advanced Computer Architecture
Memory Hierarchy Ideal memory is fast, large, and inexpensive
CSE 351 Section 9 3/1/12.
Data Structures Interview / VIVA Questions and Answers
Evolution in Memory Management Techniques
Systems Architecture II
Module IV Memory Organization.
Morgan Kaufmann Publishers Memory Hierarchy: Cache Basics
Cache - Optimization.
Cache Memory and Performance
CSCE 206 Lab Structured Programming in C
Presentation transcript:

ECE 353 Lab 1: Cache Simulation

Purpose Introduce C programming by means of a simple example Reinforce your knowledge of set associative caches

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

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

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

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.

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

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.

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

Set Associative Organization © 2004 Morgan-Kaufman Publishers

Typical Performance Use split caches because there is more spatial locality in code: © 2004 Morgan-Kaufman Publishers

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

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

Some C Constructs Relational, logical , >=, ==, !=  &&, ||, ! Arithmetic  +, -, *, /, %

C Constructs (contd.) Some control constructs:  while( ) { };  if ( ) { };  for (i=0; i<100; i++) { };

Data Types int char float double long short

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.

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]); }

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]

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:

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) }

Cache Simulation (contd.) Given the following:  Cache parameters  Memory access trace Obtain:  Cache hit rate

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

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

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))

Example of malloc int *dir_ptr; int k; //value of k can change dir_ptr = (int *) malloc(k * sizeof(int));

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)