CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Time Measurement Topics Time scales Interval counting Cycle counters K-best measurement scheme time.ppt CS 105 Tour of Black Holes of Computing.
For(int i = 1; i
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Winter 2006CISC121 - Prof. McLeod1 Stuff Assn 5 is available and due Friday, 7pm. Winding down! Possible Remaining topics: –Shell and Radix sorts (not.
1 CSC241: Object Oriented Programming Lecture No 21.
Introduction to Assembly language
SPRING 2015 QtSpim Demo & Tutorial. 2 By DGP Outline How to write your own MIPS assembly language program How to use QtSpim simulator.
SPARC Architecture & Assembly Language
Java Planning our Programs Flowcharts Arithmetic Operators.
CSCE 3121 Computer Organization Lecture 1. CSCE 3122 Course Overview Topics: –Theme –Five great realities of computer systems –Computer System Overview.
Time Measurement October 25, 2001 Topics Time scales Interval counting Cycle counters K-best measurement scheme class18.ppt.
FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.
Time Measurement CS 201 Gerson Robboy Portland State University Topics Time scales Interval counting Cycle counters K-best measurement scheme.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
Introduction to Computer Systems Topics: Theme Five great realities of computer systems How this fits within CS curriculum CS 213 F ’03 class01a.ppt
Time Measurement Topics Time scales Interval counting Cycle counters K-best measurement scheme class18.ppt.
Time Measurement December 9, 2004 Topics Time scales Interval counting Cycle counters K-best measurement scheme Related tools & ideas class29.ppt
MAT 150 Algebra Class #2 Today’s Topics: Point Plotting Method Align Inputs and scale outputs to model data.
Shorthand operators.
1 CS/COE0447 Computer Organization & Assembly Language Pre-Chapter 2.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
computer
CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX Bogdan Simion.
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
1 Operators and Expressions Instructor: Mainak Chaudhuri
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
CSC 107 – Programming For Science. The Week’s Goal.
Lecture 20: Parallelism & Concurrency CS 62 Spring 2013 Kim Bruce & Kevin Coogan CS 62 Spring 2013 Kim Bruce & Kevin Coogan Some slides based on those.
Exercise 1 : ex1.java class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread.
CMSC 150 PROGRAM EXECUTION CS 150: Wed 1 Feb 2012.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  Using typedef to make meaningful names.
Assembly Language Review Being able to repeat on the Blackfin the things we were able to do on the MIPS 2/26/2016 Review of 50% OF ENCM369 in 50 minutes1.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 CSC241: Object Oriented Programming Lecture No 08.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
ASCII, Binary, and Data Storage A MATLAB Tutorial.
Methods CSC 171 FALL 2001 LECTURE 3. History The abacus.
Octave Tutorial Basic operations Machine Learning.
User Interaction and Variables
Programming in Java: lecture 10
Data Structures and Algorithms I
ASSIGNMENT NO.-2.
Something about Java Introduction to Problem Solving and Programming 1.
HI !.
OPERATORS (1) CSC 111.
Computing Adjusted Quiz Total Score
Introduction to Computer Systems
Assembler (assembly language)
CS/COE0447 Computer Organization & Assembly Language
Introduction to Computer Systems
Chapter Three: Expressions and Equations
EDLC(Embedded system Development Life Cycle ).
class PrintOnetoTen { public static void main(String args[]) {
Why did the programmer quit his job?
CSC1401 Input and Output (with Files)
The Von Neumann Machine
Exercise 1 : ex1.java Thread Creation and Execution
Displaying Memory/Files
CSE Module 1 A Programming Primer
Quiz 11 February 13, 2019.
Presentation transcript:

CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX Bogdan Simion

Today’s tutorial Three things you’ll need for the first assignment Inline assembly for cycle counters (get data) Using Gnuplot (plot data) LaTeX (report)

Measuring cycles Idea: Get current value of cycle counter Compute something Get new value of cycle counter Get elapsed time (in cycles) by subtraction Measuring the cycles elapsed can be done by following these steps: First, …

Measurement Code We need inline assembly to implement access_counter. static u_int64_t start = 0; void access_counter(unsigned* hi, unsigned* low); void start_counter() { unsigned hi, lo; access_counter(&hi, &lo); start = ((u_int64_t)hi << 32) | lo; } u_int64_t get_counter() unsigned ncyc_hi, ncyc_lo; access_counter(&ncyc_hi, &ncyc_lo); return (((u_int64_t)ncyc_hi << 32) | ncyc_lo) - start; We need inline assembly to implement access_counter. In this code, we have a function that starts a counter and a function that retrieves the elapsed time Both of them need to retrieve the current value of the counter. Describe functions … To implement access_counter, we need inline assembly.

Inline Assembly Needed for accessing cycle counters Key point: there is no magic asm() directive tells GCC “emit this assembly code here” You give it a template Same idea as printf() format strings Using the ‘asm’ directive, you can specify GCC to add assembly code in your program. In extended assembler, you can now specify the operands of the instruction using C expressions. You use templates much like the printf format strings.

Accessing the Cycle Counter void access_counter(unsigned *hi, unsigned *lo) { asm volatile ("rdtsc; movl %%edx, %0; movl %%eax, %1" /* Format string */ : "=r" (*hi), "=r" (*lo) /* Output list */ : /* No inputs */ : "%edx", "%eax"); /* Clobber list */ } Code only works on an x86 machine compiling with GCC Emits rdtsc and two movl instructions GCC automatically adds instructions to move symbolic register value %0 into *hi and %1 into *lo. GCC also adds instructions to save and restore the registers that rdtsc clobbers. Careful with newer processors and out-of-order execution: see cpuid, rdtscp instructions. The Time Stamp Counter (TSC) is a 64-bit register present on all x86 processors since the Pentium. It counts the number of cycles since reset. The instruction RDTSC returns the TSC in EDX:EAX. - If our assembly statement must execute where we put it (i.e. must not be reordered by the compiler and put elsewhere), put the keyword “volatile” after “asm” and before the ()’s. asm volatile ( instr : outputs : inputs : clobber_list); - “=r” means output in any regular register (f for floating point register, a-d are eax-edx, S=esi, D=edi) - Clobber list is the list of registers that get clobbered so that the compiler knows about this. - Wiki(rdtsc): Starting with the Pentium Pro, Intel processors have supported out-of-order execution, where instructions are not necessarily performed in the order they appear in the executable. This can cause RDTSC to be executed later than expected, producing a misleading cycle count. This problem can be solved by executing a serializing instruction, such as CPUID, to force every preceding instruction to complete before allowing the program to continue, or by using the RDTSCP instruction, which is a serializing variant of the RDTSC instruction (starting from Intel Core i7 and AMD Athlon64 X2 CPUs).

For more information More information about inline assembly available online: http://www.cs.virginia.edu/~clc5q/gcc-inline-asm.pdf http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html#ss5.3

Timing with Cycle Counter Need to convert cycles into time determine clock rate of processor count number of cycles required for some fixed number of seconds Naïve version: This is a bit too simple Assumes sleep() actually sleeps for 10 seconds May be less (if interrupted) or more (if heavy load) See “mhz: Anatomy of a micro-benchmark”, Staelin & McVoy, Usenix Technical Conference 1998 double MHz; int sleep_time = 10; start_counter(); sleep(sleep_time); MHz = get_counter() / (sleep_time * 1e6); Sleep return value: Zero if the requested time has elapsed, OR the number of seconds left to sleep if the call was interrupted by a signal handler. The sleep() function suspends execution of the calling thread until either seconds seconds have elapsed or a signal is delivered to the thread and its action is to invoke a signal-catching function or to terminate the thread or process. System activity may lengthen the sleep by an indeterminate amount.

Gnuplot A graphing tool Advantages Scriptable Makes pretty pictures Easy to learn by example Good documentation: http://www.gnuplot.info/ Unlike Excel, it does not use a GUI, but rather declarative statements to create everything you need on a plot/graph. It’s used by the scientific community due to its power to represent some things that are not easy to do in other graphing programs.

Using Gnuplot The way to operate with gnuplot …

Gnuplot example #!/bin/sh gnuplot << ---EOF--- set title "Activity periods, load = 2" set xlabel "Time (ms)" set nokey set noytics set term postscript eps 10 set size 0.45,0.35 set output "bars.eps" set object 1 rect from 0, 1 to 8.76, 2 fs empty set object 2 rect from 8.76, 1 to 11, 2 fc rgb "black" fs solid set object 3 rect from 11, 1 to 13, 2 fs empty set object 4 rect from 13, 1 to 14, 2 fc rgb "black" fs solid set object 5 rect from 14, 1 to 19, 2 fs empty set object 6 rect from 19, 1 to 31, 2 fc rgb "black" fs solid set object 7 rect from 31, 1 to 33, 2 fs empty set object 8 rect from 33, 1 to 34, 2 fc rgb "black" fs solid plot [0:40] [0:3] 0 ---EOF--- “plot” range – [0:40] on x axis, [0:3] on y axis; “splot” for surface – specify x,y,z ranges Either input file or function to be plotted; Using lines, bars, dots, etc e.g., plot [0:100] "filename" using 1:2 with lines plot [-3:5] asin(x), acos(x) splot [0:10][0:10][0:5] "input" using 1:2:3 with dots

Title, Key, etc. title - the main title set title “Activity periods, load = 2” set xlabel “Time (ms)” set nokey set noytics title - the main title xlabel - the label of the x-axis nokey - no legend noytics - don’t print numbers on the y-axis, since we aren’t using it

Output Produce a postscript file called “bars.eps” set term postscript eps 10 set size 0.45, 0.35 set output “bars.eps” Produce a postscript file called “bars.eps” size - width and height of chart More options: landscape/portrait, color, font, etc.

Drawing Rectangles Solid rectangle, <fill> is: set object N rect from x0, y0 to x1, y1 <fill> Solid rectangle, <fill> is: fc rgb “black” fs solid Empty rectangle, <fill> is: fs empty N = object ID -> can add properties later with another “set object N …” instruction Rect = type (rectangle)

So … Write your program with cycle counters Print active and inactive times to a log file Write a script to read your logs Scale the time durations Output a sequence of rectangles Write the rectangles into boilerplate Gnuplot script

Gnuplot example http://www.cdf.toronto.edu/~csc469h/winter/tutorials/t2/t2.tgz A contrived example of automating running an experiment, parsing the output, and generating a plot. See README.txt for details.

LaTeX A markup language, like HTML Easiest to learn by example There’s lots of online documentation: http://www.ctan.org/ http://tug.ctan.org/info/lshort/english/lshort.pdf http://www.cdf.toronto.edu/~csc469h/winter/tutorials/t2/latex_template.tgz

LaTeX – General Structure \documentclass[options]{class} \usepackage[pkgs] definitions \begin{document} text \end{document}

LaTeX - Figure \begin{figure} \centering command optional required \includegraphics[scale=1.25]{random.eps} \caption{Random periods (red).} \label{fig:random} \end{figure} Includegraphics = command [scale=1.25] = optional {random.eps} = required

LaTeX - Compiling Commonly: TWICE ! $ latex report.tex $ dvips -o report.ps report.dvi $ ps2pdf report.ps TWICE ! ANYTHING at all that involves a reference – \ref (figures, sections, subsections, etc), page numbers even, all require double compilation, as the correct references are added in a second pass.

LaTeX: Compiling BibTex for bibliography: $ latex report.tex $ bibtex report 1. LaTeX: finds all bib references (\cite), figure entries, sections, etc. => aux file 2. BibTeX: creates an initial set of text for the bib entries => bbl 3. LaTeX: populates the document with bib refs + update aux 4. LaTeX: knows what the correct labels are now and includes them in the final document. At the first latex run, all \cite{...} arguments are written in the file document.aux. At the bibtex run, this information is taken by bibtex and the relevant entries are put into the .bbl file. At the next run of latex, the .bbl file is included and correct labels for \cite{...} commands are written in .aux file Only at the last run, latex knows what the correct labels are and includes them in the document.

LaTeX: Compiling OR directly using pdflatex: $ pdflatex report.tex $ bibtex report

GUI Alternatives LyX (Cross-platform): TexnicCenter (Windows) http://www.lyx.org/ TexnicCenter (Windows) http://www.texniccenter.org/ needs MikTex or TexLive distributions of Latex: http://miktex.org/ http://www.tug.org/texlive/

Questions?