Wednesday 15 March 2017

Hello All,

I have moved my blogs to new website. Also , new blogs will be on following address.

Enigmalink

Why I am moving from Blog spot. I wanted to setup my own server and run WordPress. It is a good experience of maintaining  own server. So,Please go to link below .

Friday 10 February 2017

Sound Sampling Algorithms

Hello there. In this blog, I will show you few approaches to do sound sampling in C language.
I will not be taking WAV, MP3, or any other file formats, instead I will generate numbers randomly and use it as a sound input. Reason because I am not using WAV or any other format because the main purpose of this blog is to see how we can process big chunk of data in just few seconds with System efficiency. By system efficiency I mean, using resource of a System like Memory and CPU as low as possible. So, Let’s Begin with some basics. 

Basics 

Above is the Header information of the Wav file. Until the byte 44, all the information is related to specification of the Wav file. Actual samples start after byte 45.
For more information on the Wav format, here is the link Link

This is the actual WAV file


What Exactly I am going to do?
I am going through few approaches to build different algorithms. But question is what exactly I am going to do? I will code three different approaches adjusting the volume of a sequence of sound samples. Here, I will have 20,00,00,000 length of 16bit signed integer array which will have values between -32768 to 32767 as my sound samples. I will have to calculate those sound samples by the Volume . This is like user is adjusting volume on a device. 

Simple Approach

Code:

Compiler Argument: gcc -O1 -o output program.c
This code is simple approach and following is the result.

Optimization
Time Elapsed (mSecond)
Memory Usage (MB)
0 741391.996 
1261392.160 
2 268392.160
3 229392.160


Above table shows when I use optimization level 3 , program’s elapsed time decreased by 30% and Memory usage fairly stays the same.

Compiler Argument to see Memory and CPU usage :command time -v ./output “.


Below is the screen shout of how output looks like.
 Note: Package “command” may not be installed by default on your server. you can get it by apt-get on most of the Ubuntu distribution.

Table Look Up

In table look up approach, I pre-calculated the samples from 0 to 32768 by Volume.

This is the code:


For this approach the elapsed time and memory usage were significantly changed. This algorithm take more time and memory than simple approach.

Optimization
Time Elapsed (Second)
Memory Usage (MB)
0 2.121782.284
11.180782.760 
2 1.117782.760
3 1.117782.856


Memoization

In this approach, main purpose is to eliminate repeating calculations. Once we calculate a sample by Volume factor, we store it in the array and next time use it instead of Multiplying.

Below is how code looks like. 


Optimization
Time Elapsed (Second)
Memory Usage (MB)
0 2.847782.692
10.679782.764 
2 0.547782.736
3 0.545782.760

This approach cost us less time compare to the table lookup. In average, out of 20,00,00,000 total samples only 41,905 time calculation happen and all the other values just got copied from an array . So, this is the fairly improvement in terms of CPU stress. 

Friday 3 February 2017

SPO600 Lab04 Compiler Options

In this Lab I have learn about "gcc" compiler options to optimize C code.

To begging lets use following 3 compile options.
-g is to enable debugging information.More Information.

-O0 to tell compiler which level of optimization to apply on Code. (Capital letter and then digit Zero).More Information

-fno-builtin is to tell compiler to not use built in function optimization. We will look in a bit.

We will be adding and removing the compile option to compare some factors like code size, sections in assembly .

1. Lets compile the following basic Hello World code.


Compile Argument : gcc -g -O0 -fno-builtin hello.c 

From above compile , the executable file size was 11K. 
To see the compiled object assembly code, I used objdump -d | less. This command will dump executable file in less so, we can see the output . 







Above snippet is how main function looks like in assembly . 

2. Second step is to add the -static option in compile option. 

Compile Argument : gcc -g -O0 -fno-builtin -static hello.c


Q:What happens when we use static option while compile?
A: It tells the compiler to load assembly for the whole library used by the program.
So when program runs, it doesn't have to go through link table to find the function in library.
This can improve performance but executable size will increase a lot. 
On this compile file size jumped to 895K.
From the snippet below , there are two new lines added at the end of the main section. 
These are padding so that program instructions can end at a particular memory boundary.













3. Third step , compile without the -fno-builtin

Compile Argument : gcc -g -O0 -static hello.c

-fno-builtin asks the compiler not to do any optimization for built in functions, in our case printf.
If we remove -fno-builtin from compile, then compiler will optimize the printf .
After the objdump , if we look at the function call for printing in the section main, it is changed to <_IO_puts>




4. Removing the compiler option -g .

Compile Argument : gcc -O0 -fno-builtin hello.c 

Now lets compare the object with the  gcc -g -O0 -fno-builtin hello.c ( First step ).

1. Code size reduced by 20% (9K)
2. From objdump ,sections like .debug_line, .debug_str, .debug_abbrev , .debug_aranges,.debug_info are removed.

Debugging Section Headers.

1. debug_info : describes the program's debug information for ensuring simple data types, variables and function are being used correctly.

2. debug_types : ensuring complex data types, like arrays, list, struct are being uses correctly 

3. debug_str section: contains strings that are used more than once. 

5. Some Optimization level
Option Optimization Level execution time code size memory usage compile time
-O0 optimization for compilation time (default) + + - -
-O1 or -O optimization for code size and execution time - - + +
-O2 optimization more for code size and execution time -- + ++
-O3 optimization more for code size and execution time --- + +++
-Os optimization for code size -- ++
-Ofast O3 with fast none accurate math calculations --- + +++

+increase, ++increase more, +++increase even more. -reduce ,--reduce more, ---reduce even more

7. Calling printf  within a method. 

This section , we will create the function output() with printf inside. And, call output function from main(). Code looks like following snippet.












Compile Argument : gcc -O0 -fno-builtin hello.c

Now,objdump that executable and one can see new section is being created for the output function.





















So, new section will be created for each method.


We have seen , optimization , assembly section and function call. There are lot more options for gcc compiler if you look at the man page. Some option may improve performance of program but the executable size would be bigger and for other options is completely opposite. These days, programmers dont have to think about writing code in optimized way because compiler can do it for them . However, Algorithm , approach and some other factors which still depends on programmer.


Thursday 12 January 2017