Commit a6d9be5d authored by Claudiu Mihali's avatar Claudiu Mihali
Browse files

Added matrix-vector multiplication HLS code

parent 08370a57
#include "MatrixVectorMultiplication.h"
void MatrixVectorMultiplication(IN in, OUT& out) {
#pragma HLS INTERFACE ap_ctrl_none port=return
#pragma HLS DATA_PACK variable=in
#pragma HLS INTERFACE axis register both port=in
#pragma HLS DATA_PACK variable=out
#pragma HLS INTERFACE axis register both port=out
for (int i = 0; i < DIM; i++) {
ENTRY_TYPE acc = 0;
for (int j = 0; j < DIM; j++) {
#pragma HLS UNROLL
acc += in.mat.vals[i][j] * in.vec.vals[j];
}
out.res.vals[i] = acc;
}
}
#ifndef MATRIX_VECTOR_MULTIPLICATION_H
#define MATRIX_VECTOR_MULTIPLICATION_H
const unsigned DIM = 3;
typedef double ENTRY_TYPE;
typedef struct _MATRIX {
ENTRY_TYPE vals[DIM][DIM];
} MATRIX;
typedef struct _VECTOR {
ENTRY_TYPE vals[DIM];
} VECTOR;
typedef struct _IN {
MATRIX mat;
VECTOR vec;
} IN;
typedef struct _OUT {
VECTOR res;
} OUT;
void MatrixVectorMultiplication(IN in, OUT& out);
#endif
#include "MatrixVectorMultiplication.h"
#define INPUTS_NO 4
int main() {
IN ins[INPUTS_NO] = {
{{0.1, 0.2, 0.3, 0.1, 0.2, 0.3, 0.1, 0.2, 0.3}, {0.0, -1.0, 0.0}},
{{0.1, 0.2, 0.3, 0.1, 0.2, 0.3, 0.1, 0.2, 0.3}, {1.0, 339.0, 0.0}},
{{0.1, 0.2, 0.3, 0.1, 0.2, 0.3, 0.1, 0.2, 0.3}, {0.0, -1.0, 0.0}},
{{0.1, 0.2, 0.3, 0.1, 0.2, 0.3, 0.1, 0.2, 0.3}, {1.0, 339.0, 0.0}},
};
OUT outs[INPUTS_NO];
for (int k = 0; k < INPUTS_NO; k++) {
MatrixVectorMultiplication(ins[k], outs[k]);
}
for (int k = 0; k < INPUTS_NO; k++) {
for (int i = 0; i < DIM; i++) {
ENTRY_TYPE acc = 0;
for (int j = 0; j < DIM; j++) {
acc += ins[k].mat.vals[i][j] * ins[k].vec.vals[j];
}
if (acc != outs[k].res.vals[i]) {
return 1;
}
}
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment