# Difference between revisions of "SPO600 Algorithm Selection Lab"

Purpose of this Lab
In this lab, you will investigate the impact of different algorithms which produce the same effect. You will test and select one of three algorithms for adjusting the volume of PCM audio samples based on benchmarking.

## Lab 5

### Background

• Digital sound is typically represented, uncompressed, as signed 16-bit integer signal samples. There is one stream of samples for the left and right stereo channels, at typical sample rates of 44.1 or 48 thousand samples per second, for a total of 88.2 or 96 thousand samples per second (kHz). Since there are 16 bits (2 bytes) per sample, the data rate is 88.2 * 1000 * 2 = 176,400 bytes/second (~172 KiB/sec) or 96 * 1000 * 2 = 192,000 bytes/second (~187.5 KiB/sec).
• To change the volume of sound, each sample can be scaled by a volume factor, in the range of 0.00 (silence) to 1.00 (full volume).
• On a mobile device, the amount of processing required to scale sound will affect battery life.

### Basic Sound Scale Program

Get the files for this lab on one of the ARMv8 AArch64 SPO600 Servers -- but you can perform the lab wherever you want.

1. Unpack the archive `/public/spo600-20181-algorithm-selection-lab.tgz`
2. Examine the `vol1.c` source code. This program:
1. Creates 500,000 random "sound samples" in an input array (the number of samples is set in the `vol.h` file).
2. Scales those samples by the volume factor 0.75 and stores them in an output array.
3. Sums the output array and prints the sum.
3. Build and test this file.
• Does it produce the same output each time?
4. Test the performance of this program. Adjust the number of samples as necessary.
• How long does it take to run?
• How much time is spent scaling the sound samples?
• Do multiple runs take the same time?

### Alternate Approaches

The sample program uses the most basic, obvious algorithm for the problem. Let's call this "Algorithm 0", or the "Naive Algorithm".

Try these alternate algorithms for scaling the sound samples by modifying copies of `vol1.c`. Edit the `Makefile` to build your modified programs as well as the original. Test each approach to see the performance impact:

1. Pre-calculate a lookup table (array) of all possible sample values multiplied by the volume factor, and look up each sample in that table to get the scaled values.
2. Convert the volume factor 0.75 to a fix-point integer by multiplying by a binary number representing a fixed-point value "1". For example, you could use 0b100000000 (= 256 in decimal) to represent 1.00. Shift the result to the right the required number of bits after the multiplication (>>8 if you're using 256 as the multiplier).

### Deliverables

Important! -- explain what you're doing so that a reader coming across your blog post understands the context (in other words, don't just jump into a discussion of optimization results -- give your post some context).

Optional - Recommended: Compare results across several implementations of AArch64 and x86_64 systems. Note that on different implementations, the relative performance of different algorithms will vary; for example, table lookup may outperform other algorithms on a system with a fast memory system (cache), but not on a system with a slower memory system.

• For AArch64, you could compare the performance of Cortex-A57 octa-core CPU (on aarchie) against the APM XGene-1 octa-core CPUs (on bbetty or ccharlie), or against Cortex-A53 cores (e.g., on a Raspberry Pi 3, or on ddouglas).
• For x86_64, you could compare the performance of different processors, such as xerxes, your own laptop or desktop, and Seneca systems such as Matrix, Zenit, or lap desktops.

### Things to consider

• Most solutions for a problem of this type involve generating a large amount of data in an array, processing that array using the function being evaluated, and then storing that data back into an array. Make sure that you measure the time taken in the test function only -- you need to be able to remove the rest of the processing time from your evaluation.
• You may need to run a very large amount of sample data through the function to be able to detect its performance. Feel free to edit the sample count in `vol.h` as necessary.
• If you do not use the output from your calculation (e.g., do something with the output array), the compiler may recognize that, and remove the code you're trying to test. Be sure to process the results in some way so that the optimizer preserves the code you want to test. It is a good idea to calculate some sort of verification value to ensure that both approaches generate the same results.
• Be aware of what other tasks the system is handling during your test run.

#### Analyzing Results

• What is the impact of various optimization levels on the software performance? (For example, -O0 / -O1 / -O2 / -O3)
• Does the distribution of data matter? (e.g., is there any difference if there are no absolute large numbers, or no negative numbers?)
• If samples are fed at CD rate (44100 samples per second x 2 channels x 2 bytes per sample), can each of the algorithms keep up?
• What is the memory footprint of each approach?
• What is the performance of each approach?
• What is the energy consumption of each approach? (What information do you need to calculate this?)
• Various machines, such as Aarchie, Bbetty, DDouglas, and a Raspberry Pi 3, have different performance profiles -- so it's not reasonable to compare performance between the machines, but it is reasonable to compare the relative performance of the two algorithms in each context. Do you get similar results?
• What other optimizations can be applied to this problem?

### Tips

SOX
If you want to try this with actual sound samples, you can convert a sound file of your choice to raw 16-bit signed integer PCM data using the sox utility present on most Linux systems and available for a wide range of platforms.
Stack Limit
Fixed-size, non-static arrays will be placed in the stack space. The size of the stack space is controlled by per-process limits, inherited from the shell, and adjustable with the `ulimit` command. Allocating an array larger than the stack size limit will cause a segmentation fault, usually on the first write. To see the current stack limit, use `ulimit -s` (displayed value is in KB; default is usually 8192 KB or 8 MB). To set the current stack limit, place a new size in KB or the keyword `unlimited`after the `-s` argument.

Alternate (and preferred) approach, as used in the provided sample code: allocate the array space with `malloc()` or `calloc()`.
stdint.h
The `stdint.h` header provides definitions for many specialized integer size types. Use `int16_t` for 16-bit signed integers.