Project stage 2 - Implementation (part 1)
Tags: spo600, autovectorization
Published: 2022-04-05 15:39:39

## Introduction
Hi，this is Tecca, this post is for the purpose of SPO600 project stage 2, to understand it more please check [previous post](https://dev.to/pykedot/111-46d). I decided to add auto-vectorization as SVE2 support in the package libjpeg-turbo of the project.

## Stage 2: Setting up environment

First thing is to set up the package for the Unix environment and compile the project to get everything running as it is suppose to be before making auto-vectorization implementation.

After cloning the repo, let's take a look at all the files first.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3uh9njx7blzs9vfq4d12.png)
 
Within the **BUILDING.md** file, it tells us the building procedure we need to take in order to build the project for our Unix system. I will build it with default flags first to see if everything is working properly.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1i0e8xukt56ondsl8tb.jpg)
 

```
cmake -G"Unix Makefiles"
```
After running the above building procedure command:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpnpy9nvpo5zx1c2lpvw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v75iqgv8r0q8z4m3zpne.png)

Notice the default **Compiler flags** = -03 -DNDEBUG, this tells us that with that optimization level it allows all normal optimization.

Just in case you are curious about the -DNDEBUG, it is actually used to disable assert(), If the macro **NDEBUG** is defined as a macro name at the point in the source code where <cassert> or <assert.h> is included, then assert does nothing. After a bit of research, in general people use it to signal a release/debug switch. 

[Resource](https://en.cppreference.com/w/cpp/error/assert)

Lets run the command `make` to build the project.
```
make -j$((`nproc`+1))
```
`-j` indicates how many jobs it will run in parallel. `nproc` will return the number of cores on your machine. By wrapping it in the ticks, the nproc command will execute first, return a number and that number will be passed into make. In this case, I used core-count + 1 by recommendation for faster compile time also making sure all cores are loaded, you can use define core to use ranging from core-count+1 to core-count*2. so -j$((`nproc`+1))  means it will run number of core I have on this system + 1 jobs in parallel at a time.

[Source](https://unix.stackexchange.com/questions/208568/how-to-determine-the-maximum-number-to-pass-to-make-j-option)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0matc8iv0u360vt9du4t.png)

**Note:** I'm running on a 16 core system.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vekprzaofuwkg4vffgs6.png)
 
Seems like everything is successfully build, lets look at the generated executable files before we move onto the next step.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca03srvsqvqeq5gqp2kj.png)

The files in green are the executables and we can also use the following command to list only executables.

```
find . -executable -type f
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djghbll6nornlu98dtzk.png)
 
Within the usage.txt it has guidance on how to use the executables and what is their purpose. I will go with **djpeg** executable, which decompress a JPEG file back into a conventional image format. 
```
GENERAL USAGE

We provide two programs, cjpeg to compress an image file into JPEG format,
and djpeg to decompress a JPEG file back into a conventional image format.

On most systems, you say:
        cjpeg [switches] [imagefile] >jpegfile
or
        djpeg [switches] [jpegfile]  >imagefile
```
I will also try the **-fast** option they mentioned in the usage.txt.

```
 -fast           Select recommended processing options for fast, low
                        quality output.  (The default options are chosen for
                        highest quality output.)  Currently, this is equivalent
                        to "-dct fast -nosmooth -onepass -dither ordered".
```
I will be using test images in the /testimages directory to test out **djpeg**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0pad2o1ezfyecs3nst9.png)
 
After running the command 
```
./djpeg -fast ./testimages/testimgint.jpg > decompressed.pgm
 
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6ilg2qzgrpal53p3yir.png)

We got a new decompressed pgm file from the testimgint.jpg file!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwluaeawljf2vidfv6dz.png)
 
By comparing the size of the decompressed pgm file and orginal jpg file, we can tell from the increased size that djep was successfully executed.

## Conclusion

In this post, we've successfully set up our environment and executed one of the executable(djpeg) with the option(-fast) in Unix system, I will be moving onto implementing the auto-vectorization in the up coming post soon.