Skip to main content

Vectorization and SIMD (Lab 5)

Our task was to create a small program that:

1) Creates two 1000-element integer arrays and fills them with random numbers in the range -1000 to +1000

2) Sums those two arrays element-by-element to a third array

3) Sums the third array and prints the result

The source code for the program is here. The best way I found to get this to compile vectorized on our aarch64 machine was:

gcc -03 lab5.c

Before this, I tried:

gcc  -ftree-vectorize lab5.c //already done in -03, leaves a messy main that is hard to understand.
gcc -ftree-vectorizer-verbose=2 lab5.c//supposed to show how the program was vectorized but has been retired for -fopt-info-
gcc -O2 -ftree-vectorize -fopt-info-vec-all lab5.c //shows vectorization process, useful for making sure your code structure allows vectorization. Interestingly, this helped me understand having one for loop allows for no vectorization. 

A picture demonstrating the breakdown of the code is here. I thought this task was really practical for real world applications because if it is coded in a way that can be vectorized, the compiler can do the speed up instead of the programmer. Going line by line commenting was rather simple but time consuming, however very useful in seeing the vectorization process in assembly.

In case anyone needs help, this guide is really useful in decoding aarch64 assembly and gave me a better understanding on how everything works in the big picture.  

Comments

Popular posts from this blog

Inline Assembler (Lab 7)

Part 1 After given an Inline assembler version of the volume program I made in the last lab, I got some results that shocked me. After running it with the same 500,000,000 sample size It took only 1.2 seconds of computing time, which is better than even the best variant (bit-shifting) of the program I had made by over 50%. I answered some questions below to further my understanding: 1. What is another way of defining variables instead of the (type name register) format? This can be done using normal type variables as the compiler will automatically put values into registers. 2. For the line vol_int = (int16_t) (0.5 * 32767.0); should 32767 or 32768 be used? 32767 should be used because the int will round the value and 32768 is not in the int16_t range. 3. What does __asm__("dup v1.8h,w22"); do? The duplicate simply means copy the int value into a new vector register . This is for SIMD instructions. 4. What happens if we remove : "=r"(in_cursor) ...

Final Project Part 01 - Making and Installing Dependancies for clib

To preface making clib I will first explain what it is. clib is a package manger, with the one major difference being it contains it's own dependancies and cannot be used to install anything else. The default clib repository is on git here . If you notice by default it has no hash or checksum function. To do this you must import it through clib. To make clib I used these steps: mkdir test cd test git clone https://github.com/clibs/clib.git make clib-install sha1 clib is now succesfully made and I have the sha1 function available to test. Inside the sha1 development repository here I notice it already had an extensive test file for function. I presumed getting this to work would be much easier than it actually was. The first thing to note, is the sha1 folder that gets pulled into the deps (dependancies folder) is much different than the development version. The published version has 3 files: package.json, sha1.c, and sha1.h. The development version has 7: .travis.yml, Makef...

Algorithim Selection (Lab 6)

When attempting to alter large amounts of data by a single value, the approach taken can have a significant impact on performance. In this lab example, we had the task of simulating sound and reducing the volume to 75% of the original. Limited by the variable type of int16_t means that the range must be between -32,768 to +32,767. To simulate sound the random function was used albeit with a fixed seed to predict the same results. The basic program had to work something like this: //make a 500 million digit array with random int16_t numbers //scale the values by 0.75 and see how long it takes //sum the result and display it Part 1 This part involves simply multiplying each value in the array by 0.75 and placing it back in the array. The code I made for that is here . This approach took the most amount of time, coming in at approximately 8 seconds. Part 2 This part was completed using a precalculated array of values scaled to 0.75 volume. The calculations during the ti...