<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="Module.2004-02-09.0329">
  <name>Lab 5: Optimization Theory</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2006/08/02 11:13:20.004 GMT-5</md:created>
  <md:revised>2006/08/20 22:35:35.896 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="tbshen">
      <md:firstname>Thomas</md:firstname>
      
      <md:surname>Shen</md:surname>
      <md:email>tbshen@uiuc.edu</md:email>
    </md:author>
      <md:author id="dljones">
      <md:firstname>Douglas</md:firstname>
      <md:othername>L.</md:othername>
      <md:surname>Jones</md:surname>
      <md:email>dl-jones@uiuc.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="tbshen">
      <md:firstname>Thomas</md:firstname>
      
      <md:surname>Shen</md:surname>
      <md:email>tbshen@uiuc.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>algorithm optimization</md:keyword>
    <md:keyword>assembly language</md:keyword>
    <md:keyword>autocorrelation</md:keyword>
    <md:keyword>C language</md:keyword>
    <md:keyword>code optimization</md:keyword>
    <md:keyword>compiler</md:keyword>
    <md:keyword>digital signal processing</md:keyword>
    <md:keyword>fast algorithms</md:keyword>
    <md:keyword>FFT</md:keyword>
    <md:keyword>library routines</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>optimization</md:keyword>
    <md:keyword>optimizing compiler</md:keyword>
    <md:keyword>profiler</md:keyword>
    <md:keyword>simulation</md:keyword>
  </md:keywordlist>

  <md:abstract>An overview of DSP optimization techniques, from the design stage, through high-level implementations, to assembly implementations.</md:abstract>
</metadata>

  <content>

    <section id="s1">
      <name>Introduction to Code Optimization</name>
      <para id="s1p2">
        Most practical DSP applications have a clock-cycle and/or memory
        budget. Initial implementations typically don't meet these budgets;
        therefore, the code must be optimized. Code development usually
        follows six steps:
          <list id="list1" type="enumerated">
            <item>Develop algorithm on paper</item>
            <item>Simulate in MATLAB</item>
            <item>Develop and simulate more efficient implementations</item>
            <item>Implement algorithm in C</item>
            <item>Use library routines when available</item>
            <item>Use optimizing compiler</item>
            <item>Manually write assembly routines for key routines</item>
          </list>
      </para>
    </section>

    <section id="s2">
      <name>Develop algorithm on paper</name>
      <para id="s2p1">
        The algorithm to be implemented should first be designed on paper.
        In addition to equations describing the algorithm, its design should
        include inputs and outputs of each routine and
        a flow chart of the operation of the complete program.
        <emphasis>Never design an algorithm or a program at a
        computer.</emphasis> While it may be possible to implement some basic
        algorithms and programs this way, compilers cannot overcome any design 
        flaws that are likely to be introduced.
      </para>
    </section>

    <section id="s3">
      <name>Simulate in Matlab</name>
      <para id="s3p1">
        Before any C code is written, the algorithm should be developed and
        simulated in MATLAB since problems with the algorithm design can be
        found more easily. This is done by applying the algorithm to
        test vectors and inspecting and/or plotting the results. Once the
        algorithm is completely defined, C implementation can begin. Recall
        that in each of the previous labs a MATLAB simulation step was given
        before assembly implementation.
      </para>
    </section>      

    <section id="s4">
      <name>Develop and simulate more efficient implementations</name>
      <para id="s4p1">
        An efficient algorithm used as few multiplications and additions as
        possible. Applying DSP theory to "simplify" the algorithm is a common
        way of doing this. The autocorrelation function is a simple example:
        a simple-minded way of implementing this is to
        compute sums for each lag, but careful inspection of the autocorrelation
        function reveals that it is actually a function of the
        <emphasis>absolute value</emphasis> of the lag. Therefore each value
        at a negative lag is identical to the value at the corresponding
        positive lag. Approximately half the apparent multiplications and
        additions are
        therefore required. If the entire autocorrelation sequence is needed,
        the autocorrelation can be optimized even further
        if it is treated as a convolution of two sequences. An FFT and an
        inverse FFT can then be used to compute the autocorrelation.
      </para>
    </section>

    <section id="s5">
      <name>C Implementation</name>
      <para id="s5p1">
        After the algorithm has been designed and optimized, an initial
        implentation in C is done. Tips on writing code with efficiency in
        mind can be found 
        <cite src="http://www.abarnett.demon.co.uk/tutorial.html">here</cite>.
        It is important, however, to get a working implementation of
        the algorithm in a reasonable amount of time as some optimizations 
        cannot be anticipated by the programmer.
        Some of these coding techniques in the URL reference can be applied
        later on when it is clear which routines need the most optimization.
        This implementation can serve as a reference implementation to compare
        the correctness and speed of optimized versions.
      </para>
    </section>

    <section id="s6">
      <name>Library routines</name>
      <para id="s6p1">
        If the algorithm uses common mathematical operations, such as the
        cosine and FFT operations, it is usually
        wise to use existing library routines instead of "reinventing the
        wheel." As many library routines are readily available from DSP
        manufacturers and over the internet, the first factor to consider in
        using a library is its license: do you have permission to use it in
        your application? Libraries can often be used freely for educational
        and research purposes, but any other use requires inspection
        of the library license.
      </para>
      <para id="s6p2">
        The second factor to consider is the design goal of the library: was
        it designed for speed or low memory usage? Typically speed can be
        bought with more memory and vice-versa, so when selecting a library
        it is important to decide
        which budget (speed or memory) is more important with respect to the
        routine.
      </para>
    </section>

    <section id="s7">
      <name>Compiler Optimization</name>
      <para id="s7p1">
        Recall that the basic operation of a C compiler is to translate C
        source code into assembly instructions and then into an executable.
        <quote type="block">"Compiler optimization is used to improve the
        efficiency (in terms of
        running time or resource usage) of the executables output by a
        compiler. These techniques allow programmers to write source code
        in a straightforward manner, expressing their intentions clearly,
        while allowing the computer to make choices about implementation
        details that lead to efficient execution. Contrary to what the term
        might imply, this rarely results in executables that are perfectly
        "optimal" by any measure, only executables that are much improved
        compared to direct translation of the programmer's original source."
        <cite src="#wikipedia"/></quote>
      </para>
      <para id="s7p3">
        An optimizing compiler traditionally groups optimizations into
        <emphasis>phases</emphasis>. Each phase contains a series of
        optimizations (or transformations) that are performed in a fixed order.
        These phases
        are usually turned on with command-line flags such as <code>-O1</code>,
        <code>-O2</code>, etc. Each flag indicates an optimization "level"
        where the level includes all of the lower levels. At higher
        optimization levels bugs in the code are sometimes introduced, so it
        is important to check the behavior of a compiler-optimized program
        against the reference implementation. Keep the highest optimization
        level that produces accurate code.
      </para>
      <para id="s7p4">
        At this point the compiled code should be checked against the budgetary
        constraints. Is it fast enough? Does it fit in available memory?
        Total memory usage is placed in a file produced by the compiler
        (sometimes a command-line flag is needed for this). Speed can be
        measured in a couple of ways. The most common method is the use of a
        <term>profiler</term>. A profiler tracks the performance of the program,
        providing data on how many times each function is called, as well as
        how much time each function takes in terms of cycles and percentages
        of total program cycles. A simulator also allows clock cycles to be
        measured, typically by allowing the user to place breakpoints around
        sections of code to be measured. If the speed and memory properties of 
        the compiled code fit the budget, optimization is finished. If not,
        some of the routines must be hand-written in assembly.
      </para>
    </section>

    <section id="s8">
      <name>Write key assembly routines manually</name>
      <para id="s8p1">
        Finally, if the budget cannot be met with the other optimization
        techniques some routines must be written in assembly. Manually-written
        assembly code is usually the most efficient, but it is
        labor-intensive and it is not portable to other architectures.
        Therefore it is done as a last resort and only on routines that
        absolutely require it for budget constraints to be met. This is done
        by rewriting the routine that consumes the largest portion of the
        budget, followed by the next largest budget-consuming routine and so
        on until the budget is met. It should be noted
        that this step is almost always required in embedded applications as
        current state-of-the-art C compilers and optimizers do not produce
        sufficiently fast and/or small code.
      </para>
      <para id="s8p2">
        If meeting the budget is unexpectedly difficult, remember
        that <emphasis> no compiler optimization or assembler can effectively
        overcome a poor algorithm design or implementation. </emphasis>
        If you are confident that your implentation is fast and accurate, then
        the budget may be too tight for the application. Either some parts of
        the application must be removed (extra "features", for example) or
        an architecture with more resources must be used.
      </para>
    </section>

  </content>

<bib:file>
  <bib:entry id="wikipedia">
    <bib:article>
      <bib:author>Various authors</bib:author>
      <bib:title>Compiler optimization</bib:title>
      <bib:journal>Wikipedia</bib:journal>
      <bib:year>2002-Present</bib:year>
      <bib:note>http://en.wikipedia.org/wiki/Compiler_optimization
      </bib:note>
    </bib:article>
  </bib:entry>
</bib:file>

</document>
