Unformatted text preview: 198:211 Computer Architecture
Topics:
Administrivia Theme Five great realities of computer systems Hexadecimal, Decimal, and Binary class01a.ppt Administrivia
Class website research.rutgers.edu/~ishanic/cs211.htm Visit http://remus.rutgers.edu/newaccount.html to create a new account Programming will be done in C, Java, and assembly language.
If you don't know Java, you may use C or C++ instead, but please learn Java as soon as possible. If you don't know at least one of C, C++, or Java, please reconsider taking this class. 2 Class Theme
Abstraction is good, but don't forget reality! Classes to date emphasize abstraction
Abstract data types Asymptotic analysis These abstractions have limits
Especially in the presence of bugs Need to understand underlying implementations Useful outcomes
Become more effective programmers
Able to find and eliminate bugs efficiently Able to tune program performance Prepare for later "systems" classes in CS: Compilers, Operating Systems, Networks, etc.
3 Great Reality #1
Int's are not Integers, Float's are not Reals Examples
Is x2 0? Float's: Yes! Int's: 40000 * 40000 > 1600000000 50000 * 50000 > ?? Is (x + y) + z = x + (y + z)?
Unsigned & Signed Int's: Yes! Float's: (1e20 + 1e20) + 3.14 > 3.14 1e20 + (1e20 + 3.14) > ?? 4 Computer Arithmetic
Does not generate random values
Arithmetic operations have important mathematical properties Cannot assume "usual" properties
Due to finiteness of representations Integer operations satisfy "ring" properties
Commutativity, associativity, distributivity Floating point operations satisfy "ordering" properties
Monotonicity, values of signs Observation
Need to understand which abstractions apply in which contexts Important issues for compiler writers and serious application programmers 5 Great Reality #2
You've got to know assembly Chances are, you'll never write program in assembly
Compilers are much better & more patient than you are Understanding assembly key to machinelevel execution model
Behavior of programs in presence of bugs
Highlevel language model breaks down Tuning program performance
Understanding sources of program inefficiency Implementing system software
Compiler has machine code as target Operating systems must manage process state
6 Assembly Code Example
Summing integers from 0 through 999 Here's the C code:
#include <stdio.h> int main () { int i, sum = 0; for (i=0; i<1000; i++) { sum += i; } printf ("%d\n", sum); } Call it sum.c and compile with: gcc S O3 fomitframepointer sum.c
7 Assembly Code Example
Here's the assembly language:
main: pushl xorl movl subl andl xorl addl incl cmpl jle movl movl call movl call .string %ebp %edx, %edx %esp, %ebp $8, %esp $16, %esp %eax, %eax %eax, %edx %eax $999, %eax .L6 $.LC0, (%esp) %edx, 4(%esp) printf $0, (%esp) exit "%d\n" .L6: .LC0:
8 Assembly Code Example II
Time Stamp Counter
Special 64bit register in Intelcompatible machines Incremented every clock cycle Read with rdtsc instruction Application
Measure time required by procedure
In units of clock cycles double t; start_counter(); P(); t = get_counter(); printf("P required %f clock cycles\n", t); 9 Code to Read Counter
Write small amount of assembly code using GCC's asm facility Inserts assembly code into machine code generated by compiler
static unsigned cyc_hi = 0; static unsigned cyc_lo = 0; /* Set *hi and *lo to the high and low order bits of the cycle counter. */ void access_counter(unsigned *hi, unsigned *lo) { asm("rdtsc; movl %%edx,%0; movl %%eax,%1" : "=r" (*hi), "=r" (*lo) : : "%edx", "%eax"); } 10 Code to Read Counter
/* Record the current value of the cycle counter. */ void start_counter() { access_counter(&cyc_hi, &cyc_lo); } /* Number of cycles since the last call to start_counter. */ double get_counter() { unsigned ncyc_hi, ncyc_lo; unsigned hi, lo, borrow; /* Get cycle counter */ access_counter(&ncyc_hi, &ncyc_lo); /* Do double precision subtraction */ lo = ncyc_lo  cyc_lo; borrow = lo > ncyc_lo; hi = ncyc_hi  cyc_hi  borrow; return (double) hi * (1 << 30) * 4 + lo; } 11 Measuring Time
Trickier than it Might Look
Many sources of variation Example
Sum integers from 1 to n
n 100 1,000 1,000 10,000 10,000 1,000,000 1,000,000 1,000,000,000 Cycles 961 8,407 8,426 82,861 82,876 8,419,907 8,425,181 8,371,2305,591 Cycles/n 9.61 8.41 8.43 8.29 8.29 8.42 8.43 8.37 12 Great Reality #3
Memory Matters Memory is not unbounded
It must be allocated and managed Many applications are memory dominated Memory referencing bugs especially pernicious
Effects are distant in both time and space Memory performance is not uniform
Cache and virtual memory effects can greatly affect program performance Adapting program to characteristics of memory system can lead to major speed improvements 13 Memory Referencing Bug Example
main () main () { { long int a[2]; long int a[2]; double d = 3.14; double d = 3.14; a[2] = 1073741824; /* Out of bounds reference */ a[2] = 1073741824; /* Out of bounds reference */ printf("d = %.15g\n", d); printf("d = %.15g\n", d); exit(0); exit(0); } } Alpha g O 3.14 MIPS 3.14 Linux 3.14 3.14 5.30498947741318e315 3.1399998664856 (Linux version gives correct result, but implementing as separate function gives segmentation fault.) 14 Memory Referencing Errors
C and C++ do not provide any memory protection
Out of bounds array references Invalid pointer values Abuses of malloc/free Can lead to nasty bugs
Whether or not bug has any effect depends on system and compiler Action at a distance
Corrupted object logically unrelated to one being accessed Effect of bug may be first observed long after it is generated How can I deal with this?
Program in Java, Lisp, or ML Understand what possible interactions may occur Use or develop tools to detect referencing errors 15 Memory Performance Example
Implementations of Matrix Multiplication
Multiple ways to nest loops /* ijk */ /* ijk */ for (i=0; i<n; i++) { for (i=0; i<n; i++) { for (j=0; j<n; j++) { for (j=0; j<n; j++) { sum = 0.0; sum = 0.0; for (k=0; k<n; k++) for (k=0; k<n; k++) sum += a[i][k] * b[k][j]; sum += a[i][k] * b[k][j]; c[i][j] = sum; c[i][j] = sum; } } } } /* jik */ /* jik */ for (j=0; j<n; j++) { for (j=0; j<n; j++) { for (i=0; i<n; i++) { for (i=0; i<n; i++) { sum = 0.0; sum = 0.0; for (k=0; k<n; k++) for (k=0; k<n; k++) sum += a[i][k] * b[k][j]; sum += a[i][k] * b[k][j]; c[i][j] = sum c[i][j] = sum } } } } 16 Matmult Performance (Alpha 21164)
Too big for L1 Cache Too big for L2 Cache
160 140 120 100 80 60 40 20 0 ijk ikj jik jki kij kji matrix size (n) 17 Blocked matmult perf (Alpha 21164)
160 140 120 100 80 60 40 20 0 50 75 100 125 150 175 200 225 250 275 300 325 350 375 400 425 450 475 500 matrix size (n) bijk bikj ijk ikj 18 Great Reality #4
There's more to performance than asymptotic complexity Constant factors matter too!
Easily see 10:1 performance range depending on how code written Must optimize at multiple levels: algorithm, data representations, procedures, and loops Must understand system to optimize performance
How programs compiled and executed How to measure program performance and identify bottlenecks How to improve performance without destroying code modularity and generality 19 Great Reality #5
Computers do more than execute programs They need to get data in and out
I/O system critical to program reliability and performance They communicate with each other over networks
Many systemlevel issues arise in presence of network
Concurrent operations by autonomous processes Coping with unreliable media Cross platform compatibility Complex performance issues 20 Binary and Hexadecimal
Two number systems that are useful in computer architecture.
Decimal  what we use in everyday life  is base 10
0123456789 Binary is base 2
01 Hexadecimal is base 16
0123456789abcdef 21 Conversion to Binary
1. If the number is odd, write 1, otherwise write 0. 2. Divide by two, ignoring the remainder. 3. Continue until you reach 0. 4. Write backwards. E.g. to convert 25 to binary: 25 is odd, so we write 1 25/2 = 12, which is even so we write 0 12 / 2 = 6, write 0 6 / 2 = 3, write 1 3 / 2 = 1, write 1 22 1 / 2 = 0, we're done. the answer is 11001 Conversion From Binary
1. Keep a sum. 2. For each digit from left to right: 3. Multiply the sum by two. 4. Add the digit to the sum. 5. Continue until there are no more digits left. Alternately, add the powers of two corresponding to the 1 digits 23 Conversion to Hexadecimal
1. Write the hex corresponding to the number mod 16. 2. Divide the number by 16. 3. Keep going until you reach 0. 4. Write backwards. Remember: a = 10 b = 11 c = 12 d = 13 e = 14 f = 15 24 Conversion to Hexadecimal
Example: convert 11256099 to hexadecimal. 11256099 % 16 = 3, 11256099 / 16 = 703506 703506 % 16 = 2, 703506 / 16 = 43969 43969 % 16 = 1, 43969 / 16 = 2748 2748 % 16 = 12 (hex a), 2748 / 16 = 171 171 % 16 = 11 (hex b), 171 / 16 = 10 10 % 16 = 10 (hex a), 10 / 16 = 0, we're done the answer is abc123 25 ...
View
Full Document
 Spring '08
 Chakraborty
 Assembly Language, Computer Architecture, Binary numeral system, Great Reality

Click to edit the document details