From 41bd48d575089e20518435e043e841bd20a423e4 Mon Sep 17 00:00:00 2001 From: Gianluca Brilli <88740@studenti.unimore.it> Date: Tue, 13 Dec 2022 13:56:12 +0100 Subject: [PATCH] HLS code solutions --- ...pp => SOL_exercise_2_nearest_neighbor.cpp} | 0 hls/lab1/SOL_exercise_3_4_5_matrix_mult.cpp | 47 +++++++++++++++++++ hls/lab1/SOL_exercise_7_fir_filter_sreg.cpp | 38 +++++++++++++++ .../ground_truth.txt | 0 .../main_tb.cpp | 0 .../vadd.cpp | 0 .../vadd.h | 0 ...ercise_1.cpp => exercise_1_vector_add.cpp} | 0 ..._2.cpp => exercise_2_nearest_neighbor.cpp} | 0 ...rcise_3.cpp => exercise_3_matrix_mult.cpp} | 0 ...rcise_4.cpp => exercise_4_matrix_mult.cpp} | 0 ...rcise_5.cpp => exercise_5_matrix_mult.cpp} | 0 ...ercise_6.cpp => exercise_6_fir_filter.cpp} | 0 ...e_7.cpp => exercise_7_fir_filter_sreg.cpp} | 0 14 files changed, 85 insertions(+) rename hls/lab1/{exercise_2_solution.cpp => SOL_exercise_2_nearest_neighbor.cpp} (100%) create mode 100644 hls/lab1/SOL_exercise_3_4_5_matrix_mult.cpp create mode 100644 hls/lab1/SOL_exercise_7_fir_filter_sreg.cpp rename hls/lab1/{exercise_0 => exercise_0_vector_add_simulations}/ground_truth.txt (100%) rename hls/lab1/{exercise_0 => exercise_0_vector_add_simulations}/main_tb.cpp (100%) rename hls/lab1/{exercise_0 => exercise_0_vector_add_simulations}/vadd.cpp (100%) rename hls/lab1/{exercise_0 => exercise_0_vector_add_simulations}/vadd.h (100%) rename hls/lab1/{exercise_1.cpp => exercise_1_vector_add.cpp} (100%) rename hls/lab1/{exercise_2.cpp => exercise_2_nearest_neighbor.cpp} (100%) rename hls/lab1/{exercise_3.cpp => exercise_3_matrix_mult.cpp} (100%) rename hls/lab1/{exercise_4.cpp => exercise_4_matrix_mult.cpp} (100%) rename hls/lab1/{exercise_5.cpp => exercise_5_matrix_mult.cpp} (100%) rename hls/lab1/{exercise_6.cpp => exercise_6_fir_filter.cpp} (100%) rename hls/lab1/{exercise_7.cpp => exercise_7_fir_filter_sreg.cpp} (100%) diff --git a/hls/lab1/exercise_2_solution.cpp b/hls/lab1/SOL_exercise_2_nearest_neighbor.cpp similarity index 100% rename from hls/lab1/exercise_2_solution.cpp rename to hls/lab1/SOL_exercise_2_nearest_neighbor.cpp diff --git a/hls/lab1/SOL_exercise_3_4_5_matrix_mult.cpp b/hls/lab1/SOL_exercise_3_4_5_matrix_mult.cpp new file mode 100644 index 0000000..413f63b --- /dev/null +++ b/hls/lab1/SOL_exercise_3_4_5_matrix_mult.cpp @@ -0,0 +1,47 @@ +#include +#include + +#define MAX_SIZE 64 + +const unsigned int max_size = MAX_SIZE; + +void mmult( int *in1, + int *in2, + int *out, + int dim + ) +{ +#pragma HLS INTERFACE m_axi port=in1 offset=slave bundle=in1_mem +#pragma HLS INTERFACE m_axi port=in2 offset=slave bundle=in2_mem +#pragma HLS INTERFACE m_axi port=out offset=slave bundle=out_mem + +#pragma HLS INTERFACE s_axilite port=dim bundle=params +#pragma HLS INTERFACE s_axilite port=return bundle=params + + int in_1_loc [MAX_SIZE][MAX_SIZE]; + int in_2_loc [MAX_SIZE][MAX_SIZE]; + int out_loc [MAX_SIZE][MAX_SIZE]; + + #pragma HLS ARRAY_PARTITION variable=in_1_loc dim=2 complete + #pragma HLS ARRAY_PARTITION variable=in_2_loc dim=1 complete + + memcpy_1: memcpy(in1, in_1_loc, MAX_SIZE*MAX_SIZE*sizeof(int)); + memcpy_2: memcpy(in2, in_2_loc, MAX_SIZE*MAX_SIZE*sizeof(int)); + + loop_1: for (int i = 0; i < dim; i++){ + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + loop_2: for (int j = 0; j < dim; j++){ + + #pragma HLS PIPELINE + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + + loop_3: for (int k = 0; k < MAX_SIZE; k++){ + + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + + out_loc[i][j] += in_1_loc[i][k] * in_2_loc[k][j]; + } + } + } + memcpy_3: memcpy(out_loc, out, MAX_SIZE*MAX_SIZE*sizeof(int)); +} diff --git a/hls/lab1/SOL_exercise_7_fir_filter_sreg.cpp b/hls/lab1/SOL_exercise_7_fir_filter_sreg.cpp new file mode 100644 index 0000000..9c4b48d --- /dev/null +++ b/hls/lab1/SOL_exercise_7_fir_filter_sreg.cpp @@ -0,0 +1,38 @@ +#define SIZE 128 +#define N 10 + +void fir(int * input, int * output) { + +#pragma HLS INTERFACE m_axi port=input offset=slave bundle=input_mem +#pragma HLS INTERFACE m_axi port=output offset=slave bundle=output_mem + +#pragma HLS INTERFACE s_axilite port=return bundle=params + + int coeff[N] = {13, -2, 9, 11, 26, 18, 95, -43, 6, 74}; + int shift_reg[N] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + //TODO: insert array partition directive + #pragma HLS ARRAY_PARTITION variable=shift_reg dim=0 complete + + loop_1: for (int n = 0; n < SIZE; n++) { + //TODO: insert pipeline directive + #pragma HLS PIPELINE + + int acc = 0; + + loop_2: for(int j = N-1; j > 0; j--) { + //TODO: insert unroll directive + #pragma HLS UNROLL + shift_reg[j] = shift_reg[j-1]; + } + + shift_reg[0] = input[n]; + + loop_3: for (int j= 0; j< N; j++ ) { + //TODO: insert unroll directive + #pragma HLS UNROLL + acc += shift_reg[j]*coeff[j]; + } + output[n] = acc; + } +} diff --git a/hls/lab1/exercise_0/ground_truth.txt b/hls/lab1/exercise_0_vector_add_simulations/ground_truth.txt similarity index 100% rename from hls/lab1/exercise_0/ground_truth.txt rename to hls/lab1/exercise_0_vector_add_simulations/ground_truth.txt diff --git a/hls/lab1/exercise_0/main_tb.cpp b/hls/lab1/exercise_0_vector_add_simulations/main_tb.cpp similarity index 100% rename from hls/lab1/exercise_0/main_tb.cpp rename to hls/lab1/exercise_0_vector_add_simulations/main_tb.cpp diff --git a/hls/lab1/exercise_0/vadd.cpp b/hls/lab1/exercise_0_vector_add_simulations/vadd.cpp similarity index 100% rename from hls/lab1/exercise_0/vadd.cpp rename to hls/lab1/exercise_0_vector_add_simulations/vadd.cpp diff --git a/hls/lab1/exercise_0/vadd.h b/hls/lab1/exercise_0_vector_add_simulations/vadd.h similarity index 100% rename from hls/lab1/exercise_0/vadd.h rename to hls/lab1/exercise_0_vector_add_simulations/vadd.h diff --git a/hls/lab1/exercise_1.cpp b/hls/lab1/exercise_1_vector_add.cpp similarity index 100% rename from hls/lab1/exercise_1.cpp rename to hls/lab1/exercise_1_vector_add.cpp diff --git a/hls/lab1/exercise_2.cpp b/hls/lab1/exercise_2_nearest_neighbor.cpp similarity index 100% rename from hls/lab1/exercise_2.cpp rename to hls/lab1/exercise_2_nearest_neighbor.cpp diff --git a/hls/lab1/exercise_3.cpp b/hls/lab1/exercise_3_matrix_mult.cpp similarity index 100% rename from hls/lab1/exercise_3.cpp rename to hls/lab1/exercise_3_matrix_mult.cpp diff --git a/hls/lab1/exercise_4.cpp b/hls/lab1/exercise_4_matrix_mult.cpp similarity index 100% rename from hls/lab1/exercise_4.cpp rename to hls/lab1/exercise_4_matrix_mult.cpp diff --git a/hls/lab1/exercise_5.cpp b/hls/lab1/exercise_5_matrix_mult.cpp similarity index 100% rename from hls/lab1/exercise_5.cpp rename to hls/lab1/exercise_5_matrix_mult.cpp diff --git a/hls/lab1/exercise_6.cpp b/hls/lab1/exercise_6_fir_filter.cpp similarity index 100% rename from hls/lab1/exercise_6.cpp rename to hls/lab1/exercise_6_fir_filter.cpp diff --git a/hls/lab1/exercise_7.cpp b/hls/lab1/exercise_7_fir_filter_sreg.cpp similarity index 100% rename from hls/lab1/exercise_7.cpp rename to hls/lab1/exercise_7_fir_filter_sreg.cpp