Week 11 Reflection SIMD
Published: 2021-12-10 03:13:20

Hello my friend, welcome back to the week 11 blog about SPO600(Software Portability and Optimization). In this blog, I'm going to blog about SIMD.


##Introduction##
[SIMD](https://en.wikipedia.org/wiki/SIMD)(Single instruction multiple data) also called vectorization. The concept of SIMD is a single instruction which does one operation but we could improve it to process multiple data in parallel. It is an ability that many modern processors have. It is also called vectorization which is working with vectors. Vectors are arrays of values, typically one dimensional arrays then apply the same operation to different parts of the array simultaneously. For example, 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrl2smgcd9aspwtd4hl2.png)
In the diagram above, the SIMD unit could calculate multiple values from vector A and vector B with the same operation and then put the result into the vector C in parallel which means it is executing four operations simultaneously. 

##MMX instruction set##
[MMX](https://en.wikipedia.org/wiki/MMX_%28instruction_set%29#:~:text=Pentium%20with%20MMX.%20MMX%20is%20a%20single%20instruction%2C,of%20microprocessors%2C%20designated%20as%20%22Pentium%20with%20MMX%20Technology%22.) defines 8 processor registers, named from MM0 to MM7, and operations that operate on them. Each register is 64 bits wide and can be used to hold either one 64-bit integer or multiple smaller integer: one instruction can be applied to two 32-bit integers, or four 16-bit integers or eight 8-bit integers at once.

##How to check SIMD instruction##
Before using the SIMD instruction set, we need help from the processor and compiler. I used my personal laptop as an example:
Check SIMD instruction sets that are supported by the CPU:
```
cat /proc/cpuinfo
```
Then find the flags, we could find the instruction set that the CPU supports.
```
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse 
```
Check SIMD instruction sets which are supported by GCC
```
gcc -march=native -c -Q --help=target
``` 
We could see the instruction set which is enabled or disabled.
For example:
```
The following options are target specific:
  -m128bit-long-double                  [enabled]
  -m16                                  [disabled]
  -m32                                  [disabled]
  -m3dnow                               [disabled]
  -m3dnowa                              [disabled]
  -m64                                  [enabled]
  -m80387                               [enabled]
  -m8bit-idiv                           [disabled]
  -m96bit-long-double                   [disabled]
  -mabi=                                sysv
  -mabm                                 [disabled]
  -maccumulate-outgoing-args            [disabled]
  -maddress-mode=                       long
  -madx                                 [disabled]
  -maes                                 [disabled]
  -malign-data=                         compat
  -malign-double                        [disabled]
  -malign-functions=                    0
  -malign-jumps=                        0
  -malign-loops=                        0
  -malign-stringops                     [enabled]
  -mamx-bf16                            [disabled]
  -mamx-int8                            [disabled]
  -mamx-tile                            [disabled]
  -mandroid                             [disabled]
  -march=                               nehalem
```

##Conclusion##
SIMD is a very significant ability that the modern processor should have. It helps to process multiple data simultaneously and saves a lot of time. In the [Lab6(aka project 1)](https://dev.to/qzhang125/spo600-assignment-1-algorithm-selection-for-optimization-4n9o) we explored more and test how much time the SIMD saved for the program. It is fun to learn and actually test how it works.