1 / 33

CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX

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:

renee
Télécharger la présentation

CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC469 Tutorial2Inline assembly, cycle counters, Gnuplot, LaTeX Bogdan Simion

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

  3. Measuring cycles • Idea: • Get current value of cycle counter • Compute something • Get new value of cycle counter • Get elapsed time (in cycles) by subtraction

  4. Measurement Code static uint64_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.

  5. 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

  6. Accessing the Cycle Counter void access_counter(unsigned *hi, unsigned *lo) { asmvolatile ("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.

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

  8. 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);

  9. Gnuplot • A graphing tool • Advantages • Scriptable • Makes pretty pictures • Easy to learn by example • Good documentation: • http://www.gnuplot.info/

  10. Using Gnuplot

  11. 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---

  12. Title, Key, etc. 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

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

  14. Drawing Rectangles • Solid rectangle, <fill> is: fc rgb “black” fs solid • Empty rectangle, <fill> is: fs empty set object N rect from x0, y0 to x1, y1 <fill>

  15. 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

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

  17. A few more examples • Line graphs • Bar charts • Surfaces, 3D volumes • Many more …

  18. set title "Student behaviour“ set xlabel "Weeks into semester“ plot 'curves.dat‘ using 2:xticlabels(1) title 'Learning curve‘ with lines, 'curves.dat‘ using 3:xticlabels(1) title 'Stress Level‘ with lines curves.dat -------------- 0 0 50 2 1 47 4 5 43 6 10 35 8 25 22 10 50 0

  19. set xlabel "Race Track" 0,-1.2 set ylabel 'LapTime (minutes) - logscale‘ set logscale y plot 'speeds.dat' using 2:xticlabels(1) title 'Ferrari' with histograms fill solid 0.1,\ 'speeds.dat' using 3:xticlabels(1) title 'Porsche' with histograms fill solid 0.9,\ 'speeds.dat' using 4:xticlabels(1) title 'My Cat' with histograms fill solid 0.4 speeds.dat --------------------------- Indianapols 1.4 1.3 45 Monaco 1.3 1.5 30 MyBackyard 5 5.7 0.8

  20. set nokey set parametric set hidden3d set view 60 set isosamples 30,20 set xrange[-2 : 2] set yrange[-2 : 2] set zrange[-1 : 1] splot [-pi:pi][-pi/2:pi/2] cos(u)*cos(v), sin(u)*cos(v), sin(v) [http://soukoreff.com/gnuplot/]

  21. set nokey set noborder set noxtics set noytics set noztics set parametric set hidden3d set view 90,330,1,1.3 set isosamples 300,20 splot [0:13*pi][-pi:pi] u*cos(u)*(cos(v)+1), u*sin(u)*(cos(v)+1),\ u*sin(v) - ((u+3)/8*pi)**2 – 20 [http://soukoreff.com/gnuplot/]

  22. 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/fall/tutorials/t2/latex_template.tgz

  23. LaTeX – General Structure \documentclass[options]{class} e.g.,[10pt,twoside,a4paper]{article} \usepackage[options]{package} e.g., {amsmath,epsfig} [tight]{subfigure} [pdftex]{graphicx} \begin{document} \title{...} \author{...} \maketitle abstract,chapters,sections,references,etc. \end{document}

  24. Example Package - strikethrough \usepackage{ulem} Yesterday, I \sout{ate 20 cookies} ran a marathon. Output: Yesterday, I ate 20 cookies ran a marathon.

  25. Sections, subsections, etc .. \section{Methodology} \label{sec:method} In this document, we describe our methods for designing cool stuff. \section{Evaluation} \subsection{Experimental results} We analyze the performance of our cool stuff described in section \ref{sec:method}.

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

  27. Latex - multifigure • Why people call their • own art “abstract” b) Languages I can speak English It really is abstract art French It sucks Sarcasm Figure1. Funny graphs Images: http://www.boredpanda.com

  28. LaTeX - multifigure \begin{figure*}[t] \centering \subfigure[Why people call their own art abstract]{ \includegraphics[width=0.4\linewidth]{AbstractArtGraph.pdf} \label{fig:art}} \subfigure[Languages I speak]{ \includegraphics[width=0.4\linewidth]{LanguagesGraph.pdf} \label{fig:languages}} \caption{Funny graphs} \label{fig:graphs} \end{figure*}

  29. LaTeX - Compiling • Commonly: $ latex report.tex $ latex report.tex $ dvips-o report.ps report.dvi $ ps2pdf report.ps TWICE !

  30. LaTeX: Compiling • BibTex for bibliography: $ latex report.tex $ bibtex report $ latex report.tex $ latex report.tex 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.

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

  32. GUI Alternatives • LyX (Cross-platform): • 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/

  33. Questions?

More Related