|
|
Example of vectors in a plane (2 dimension) --- high school material:
Effect of adding 2 vectors:
The SISD computer algorithm used to add 2 vectors A and B is using multiple + instructions:
Input: float A[N] // vector 1 in array A
float B[N] // vector 2 in array B
Output: float C[N] // Output vector
Vector addition algorithm:
// The SISD computer performs
// N addition operations
for (i = 0; i < N; i++)
C[i] = A[i] + B[i];
|
Each + instruction adds 1 pair of numbers
|
The SIMD computer can add 2 vectors A and B using a single instruction:
Input: float A[N] // vector 1 in array A
float B[N] // vector 2 in array B
Output: float C[N] // Output vector
Vector addition algorithm:
// The SIMD computer performs
// N addition operations at once:
C[0..N] = A[0..N] + B[0..N]
|
The + instruction can add N pairs of numbers
|
Review: how to multiply 2 matrices (= row colum sum)
+- -+ +- -+
|A11 A12 A13| |B11 B12 B13|
A = |A21 A22 A23| B = |B21 B22 B23|
|A31 A32 A33| |B31 B32 B33|
+- -+ +- -+
Then:
+- -+
|C11 C12 C13|
C = A*B = |C21 C22 C23|
|C31 C32 C33|
+- -+
where:
Cij = Ai1*B1j + Ai2*B2j + Ai3*B3j
(for i = 1, 2, 3 and j = 1, 2, 3)
|
Consider the following matrix multiplication:
I will show you how the matrix multiplication can be performed using vector operations
We first initialize the output to the ZERO matrix:
We now use vector multiplications and vector additions to compute the row-column sum !
Multiply a11 with the 1st row vector in B:
This is one constant × vector vector operation in a SIMD computer !!!
Add the resulting vector to the row in the output matrix:
This is one vector + vector vector operation in a SIMD computer !!!
Multiply a12 with the 2nd row vector in B:
This is one constant × vector vector operation in a SIMD computer !!!
Add the resulting vector to the row in the output matrix:
This is one vector + vector vector operation in a SIMD computer !!!
Multiply a13 with the 3rd row vector in B:
This is one constant × vector vector operation in a SIMD computer !!!
Add the resulting vector to the row in the output matrix:
This is one vector + vector vector operation in a SIMD computer !!! 1 row is done !!
Multiply a21 with the 1st row vector in B:
This is one constant × vector vector operation in a SIMD computer !!!
Add the resulting vector to the row in the output matrix:
This is one vector + vector vector operation in a SIMD computer !!! And so on !!
|
|