Matrix multiplication.

Write a program that multiplies two matrices with one another.

The output of the program is a matrix \( C \), which is the result of the operation \( C = A \times B \), where \( A \in R^{n \times m} \), \( B \in R^{m \times p} \) and \( C \in R^{n \times p} \).

The input matrix dimensions are located at addresses matrix_a_rows, matrix_a_cols, matrix_b_rows and matrix_b_cols.

The data of matrix A is located at the address matrix_a_start, and the data of matrix B is located at the address matrix_b_start. The matrix data is stored in row-major order (i.e. the first row is stored first, then the second row, and so on).

Calculate the matrix \( C \) and store the result at the address matrix_c_start. Store the dimensions of the matrix \( C \) at the addresses matrix_c_rows and matrix_c_cols.

If the dimensions of the input matrices are not compatible, set the output dimensions to \( -1 \).

Note

Matrix multiplication is defined as: \[ c_{ij} = \sum_{k=1}^{m} a_{ik} \cdot b_{kj} \] where \( c_{ij} \) is the element at the \( i \)-th row and \( j \)-th column of the matrix \( C \).

This can be graphically represented as:

$$ \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n}\\ a_{21} & a_{22} & \cdots & a_{2n}\\ \vdots & \vdots & \ddots & \vdots\\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} \times \begin{bmatrix} b_{11} & b_{12} & \cdots & b_{1p}\\ b_{21} & b_{22} & \cdots & b_{2p}\\ \vdots & \vdots & \ddots & \vdots\\ b_{n1} & b_{n2} & \cdots & b_{np} \end{bmatrix} = $$ $$ \begin{bmatrix} a_{11} \cdot b_{11} + \cdots + a_{1n} \cdot b_{n1} & \cdots & a_{11} \cdot b_{1p} + \cdots + a_{1n} \cdot b_{np}\\ a_{21} \cdot b_{11} + \cdots + a_{2n} \cdot b_{n1} & \cdots & a_{21} \cdot b_{1p} + \cdots + a_{2n} \cdot b_{np}\\ \vdots & \ddots & \vdots\\ a_{m1} \cdot b_{11} + \cdots + a_{mn} \cdot b_{n1} & \cdots & a_{m1} \cdot b_{1p} + \cdots + a_{mn} \cdot b_{np} \end{bmatrix} $$

This can be represented by the following code:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < p; j++) {
        for (int k = 0; k < m; k++) {
            C[i][j] += A[i][k] * B[k][j];
        }
    }
}

In python, you can test matrix multiplication with the following code:

import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[1, 0], [0, 1], [1, 1]])
C = A @ B

This task was not yet properly tested, it may not work as expected. If you find any issues, please report them.

Input: Matrix multiplication
In: Two matrices A and B at addresses matrix_a_start and matrix_b_start, with dimensions at addresses matrix_a_rows, matrix_a_cols, matrix_b_rows and matrix_b_cols.
Out: Matrix C at address matrix_c_start, with dimensions at addresses matrix_c_rows and matrix_c_cols.

Your program will be run with the following arguments: --dump-cycles --cycle-limit 200000 --asm submission.S
Your program will be scored by this following metric: Runtime of the program in cycles.

For interactive solution, you can use QtRvSim. The web version of the simulator is located here.
Top Scores (cycles)