# 2017q1 Homework5 (matrix)
contributed by < `Chihsiang` >
作業說明: [B10: matrix](https://hackmd.io/s/rkrrpHm2e)
github: [chihsiang/matrix_oo](https://github.com/ChiHsiang/matrix_oo)
## 改善方向
* 重新封裝 Matrix
* 使用 SIMD 改善效能
* 支援不同大小的 Matrix
## 封裝 Matrix
* 定義 Matrix
```c=
typedef struct __MATRIX_IMPL_T {
unsigned int row;
unsigned int column;
int *values;
/* operations */
bool (*ftpr_equal)(struct __MATRIX_IMPL_T *, struct __MATRIX_IMPL_T *);
struct __MATRIX_IMPL_T *(*ftpr_mul)(struct __MATRIX_IMPL_T *, struct __MATRIX_IMPL_T *);
} Matrix_t;
typedef Matrix_t *ptrMatrix_t;
```
跟原本的結構只差在將存放值得部分變成可動態分配的,以及存入 row & column
* 宣告 function
```c=
ptrMatrix_t matrix_init(rows, columns, bool);
bool equal(ptrMatrix_t, ptrMatrix_t);
ptrMatrix_t mul(ptrMatrix_t, ptrMatrix_t);
void matrix_free(ptrMatrix_t, bool);
void print_matrix(ptrMatrix_t m);
```
唯一差別只有初始化的 function
## SIMD 修改
目標放在乘法的部分
* NAIVE
```c=
ptrMatrix_t mul(ptrMatrix_t m, ptrMatrix_t n)
{
int x, y, k;
ptrMatrix_t matrix;
matrix = matrix_init(m->row, n->column, false);
for (y = 0; y < m->row; y++)
¦ for (x = 0; x < n->column; x++)
¦ ¦ for (k = 0; k < m->column; k++)
¦ ¦ ¦ *(matrix->values + y * n->column + x) +=
¦ ¦ ¦ ¦ *(m->values + m->column * y + k) * *(n->values + n->column * k + x);
return matrix;
}
```
- perf record
```shell=
Samples: 30K of event 'cache-misses', Event count (approx.): 32191400
Overhead Command Shared Object Symbol
99.60% test test [.] mul
```
數值馬上顯示 mul 效能極差,主要在隨著 Matrix 大小,執行迴圈次數呈現倍數成長(row * column * k)
* SSE
## 記錄目前情況
由於這次作業以及 prefetch 作業一直碰到 Matrix,這次在寫 matrix mul and matrix add 也遇到許多數學問題,因此決定重新認識矩陣,學習中教學如下,預期能把矩陣掌握度提高,先做數學分析接著再做程式分析
* [Lec01 矩陣分析](https://www.youtube.com/watch?v=Py9yunCRUA4)