# OpenBLAS Example

## OpenBLAS Overview

`OpenBLAS` is an optimized `BLAS` computation library released under the `BSD` license. `BLAS (Basic Linear Algebra Subprograms)` is an API standard for numerical libraries that provide basic linear-algebra operations such as vector and matrix multiplication. `OpenBLAS` is one concrete implementation of the BLAS standard.

In `K230 RTOS SDK`, a prebuilt `OpenBLAS` library is already included under:

```bash
src/rtsmart/libs/openblas/
```

Users can directly link against this static library when building their own applications.

## Build Examples

This section explains how to build executables by using the prebuilt OpenBLAS static library in the SDK. The SDK provides four example programs under:

```bash
src/rtsmart/examples/3rd-party/openblas/
```

### Code Structure

```text
openblas_cblas_saxpy
openblas_cblas_sgemm
openblas_cblas_sger
openblas_fortran_dgemm
CMakeLists.txt
build_app.sh
cmake/
```

### Firmware Build

If you want the examples to be built into the firmware, run `make menuconfig` from the `K230 RTOS SDK` root and enable:

```text
RT-Smart UserSpace Examples Configuration
-> Enable build 3rd-party examples
-> Enable Build OpenBlas Sample Programs
```

The examples will be placed under `sdcard/app/examples/3rd-party/openblas` in the firmware image.

### Example Build

If you only want to build the OpenBLAS example programs, enter:

```bash
src/rtsmart/examples/3rd-party/openblas
```

and run:

```bash
./build_app.sh
```

After a successful build, the `k230_bin` directory contains the compiled `elf` files and related test files.

## Run Examples

### `openblas_cblas_saxpy`

This sample implements:

```text
y = alpha * x + y
```

Representative result:

```text
This is the result:
4 7 11 14
This is the reference:
4 7 11 14
{Test PASS}.
```

### `openblas_cblas_sgemm`

This sample implements:

```text
C = alpha * A * B + beta * C
```

Representative result:

```text
This is the result:
7 10 15 22
This is the reference:
7 10 15 22
{Test PASS}.
```

### `openblas_cblas_sger`

This sample implements:

```text
A = alpha * x * y^T + beta * A
```

Representative result:

```text
This is the result:
20 40 10 20 30 60
This is the reference:
20 40 10 20 30 60
{Test PASS}.
```

### `openblas_fortran_dgemm`

This sample demonstrates the Fortran-interface `dgemm` flow.

Representative result:

```text
m=2,n=3,k=4,alpha=1.200000,beta=0.001000,sizeofc=6
This is matrix A
...
This is the result:
16.801 18.002 18.003 16.801 15.602 22.803
This is the reference:
16.801 18.002 18.003 16.801 15.602 22.803
{Test PASS}.
```
