From 3c3ddf4bec0d225938c1f51473da4c23fe97bd89 Mon Sep 17 00:00:00 2001 From: Gianluca Brilli Date: Thu, 13 May 2021 21:40:40 +0200 Subject: [PATCH] HLS lab 1 --- hls/lab1/exercise_0_solution.cpp | 17 +++++++++ hls/lab1/exercise_1.cpp | 19 ++++++++++ hls/lab1/exercise_2.cpp | 54 +++++++++++++++++++++++++++++ hls/lab1/exercise_2_solution.cpp | 59 ++++++++++++++++++++++++++++++++ hls/lab1/exercise_3.cpp | 29 ++++++++++++++++ hls/lab1/exercise_4.cpp | 33 ++++++++++++++++++ hls/lab1/exercise_5.cpp | 36 +++++++++++++++++++ hls/lab1/exercise_6.cpp | 21 ++++++++++++ hls/lab1/exercise_7.cpp | 34 ++++++++++++++++++ hls/lab2/TODO | 0 10 files changed, 302 insertions(+) create mode 100755 hls/lab1/exercise_0_solution.cpp create mode 100755 hls/lab1/exercise_1.cpp create mode 100755 hls/lab1/exercise_2.cpp create mode 100755 hls/lab1/exercise_2_solution.cpp create mode 100755 hls/lab1/exercise_3.cpp create mode 100755 hls/lab1/exercise_4.cpp create mode 100755 hls/lab1/exercise_5.cpp create mode 100755 hls/lab1/exercise_6.cpp create mode 100755 hls/lab1/exercise_7.cpp create mode 100644 hls/lab2/TODO diff --git a/hls/lab1/exercise_0_solution.cpp b/hls/lab1/exercise_0_solution.cpp new file mode 100755 index 0000000..f1739ee --- /dev/null +++ b/hls/lab1/exercise_0_solution.cpp @@ -0,0 +1,17 @@ +#define TEST_DATA_SIZE 4194304 // 2^22 + +const unsigned int c_dim = TEST_DATA_SIZE; + +void vadd(int *a, int *b, int *c, const int len) +{ + #pragma HLS INTERFACE m_axi port=a offset=slave bundle=mem + #pragma HLS INTERFACE m_axi port=b offset=slave bundle=mem + #pragma HLS INTERFACE m_axi port=c offset=slave bundle=mem + #pragma HLS INTERFACE s_axilite port=len bundle=params + #pragma HLS INTERFACE s_axilite port=return bundle=params + + loop: for(int i = 0; i < len; i++) { + #pragma HLS LOOP_TRIPCOUNT min=c_dim max=c_dim + c[i] = a[i] + b[i]; + } +} diff --git a/hls/lab1/exercise_1.cpp b/hls/lab1/exercise_1.cpp new file mode 100755 index 0000000..5f379c6 --- /dev/null +++ b/hls/lab1/exercise_1.cpp @@ -0,0 +1,19 @@ +#define TEST_DATA_SIZE 4194304 // 2^22 + +const unsigned int c_dim = TEST_DATA_SIZE; + +void vadd(int *a, int *b, int *c, const int len) +{ + //TODO: split bundles on three different AXI4 bus + #pragma HLS INTERFACE m_axi port=a offset=slave bundle=mem + #pragma HLS INTERFACE m_axi port=b offset=slave bundle=mem + #pragma HLS INTERFACE m_axi port=c offset=slave bundle=mem + #pragma HLS INTERFACE s_axilite port=len bundle=params + #pragma HLS INTERFACE s_axilite port=return bundle=params + + loop: for(int i = 0; i < len; i++) { + //TODO: insert pipeline directive + #pragma HLS LOOP_TRIPCOUNT min=c_dim max=c_dim + c[i] = a[i] + b[i]; + } +} diff --git a/hls/lab1/exercise_2.cpp b/hls/lab1/exercise_2.cpp new file mode 100755 index 0000000..ff47d0f --- /dev/null +++ b/hls/lab1/exercise_2.cpp @@ -0,0 +1,54 @@ +#include +#include + +#define NUM_DIMS 5 +#define NUM_POINTS 512 + +const unsigned int num_dims = NUM_DIMS; +const unsigned int num_points = NUM_POINTS; + +void nearest_neighbor(int *out, const int *points, + const int *search_point, const int len, + const int dim){ + +#pragma HLS INTERFACE m_axi port=out offset=slave bundle=out_mem +#pragma HLS INTERFACE m_axi port=points offset=slave bundle=points_mem +#pragma HLS INTERFACE m_axi port=search_point offset=slave bundle=search_point_mem + +#pragma HLS INTERFACE s_axilite port=len bundle=param +#pragma HLS INTERFACE s_axilite port=dim bundle=param +#pragma HLS INTERFACE s_axilite port=return bundle=param + + int best_i = 0; + int best_dist = INT_MAX; + int s_point[NUM_DIMS]; + + memcpy(s_point, search_point, NUM_DIMS*sizeof(int)); + + // TODO: merge upper_loop and lower loop + // TODO: insert pipeline directive + upper_loop:for(int p = 0 ; p < len; ++p){ + + #pragma HLS LOOP_TRIPCOUNT max=num_points min=num_points + + int dist = 0; + + lower_loop:for(int c = 0 ; c < dim ; c++){ + + #pragma HLS LOOP_TRIPCOUNT max=num_dims min=num_dims + + int dx = points[dim*p + c] - s_point[c]; + dist += dx * dx; + } + + if (dist < best_dist){ + best_i = p; + best_dist = dist; + } + } + //TODO: insert pipeline directive + write_best: for (int c = 0; c < dim; ++c) { + #pragma HLS LOOP_TRIPCOUNT max=num_dims min=num_dims + out[c] = points[best_i * dim + c]; + } +} diff --git a/hls/lab1/exercise_2_solution.cpp b/hls/lab1/exercise_2_solution.cpp new file mode 100755 index 0000000..83db945 --- /dev/null +++ b/hls/lab1/exercise_2_solution.cpp @@ -0,0 +1,59 @@ +#include +#include + +#define NUM_DIMS 5 +#define NUM_POINTS 512 + +const unsigned int num_dims = NUM_DIMS; +const unsigned int num_points = NUM_POINTS; +const unsigned int max_iterations = num_dims * num_points; + +void nearest_neighbor(int *out, const int *points, + const int *search_point, const int len, + const int dim){ + + #pragma HLS INTERFACE m_axi port=out offset=slave bundle=out_mem + #pragma HLS INTERFACE m_axi port=points offset=slave bundle=points_mem + #pragma HLS INTERFACE m_axi port=search_point offset=slave bundle=search_point_mem + + #pragma HLS INTERFACE s_axilite port=len bundle=param + #pragma HLS INTERFACE s_axilite port=dim bundle=param + #pragma HLS INTERFACE s_axilite port=return bundle=param + + int best_i = 0; + int best_dist = INT_MAX; + int s_point[NUM_DIMS]; + + memcpy(s_point, search_point, NUM_DIMS*sizeof(int)); + + int dist = 0; + int iterations = len * dim; + + find_best: for (int p = 0, c = 0, itr = 0; itr < iterations; itr++) { + + #pragma HLS PIPELINE + #pragma HLS LOOP_TRIPCOUNT max=max_iterations min=max_iterations + + int dx = points[dim * p + c] - s_point[c]; + dist += dx * dx; + if (c == dim - 1) { + if (dist < best_dist) { + best_i = p; + best_dist = dist; + } + c = 0; + dist = 0; + p++; + } else { + c++; + } + } + write_best: + for (int c = 0; c < dim; ++c) { + + #pragma HLS PIPELINE + #pragma HLS LOOP_TRIPCOUNT max=num_dims min=num_dims + + out[c] = points[best_i * dim + c]; + } +} diff --git a/hls/lab1/exercise_3.cpp b/hls/lab1/exercise_3.cpp new file mode 100755 index 0000000..9b794f1 --- /dev/null +++ b/hls/lab1/exercise_3.cpp @@ -0,0 +1,29 @@ +#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 + + for (int i = 0; i < dim; i++){ + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + for (int j = 0; j < dim; j++){ + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + for (int k = 0; k < dim; k++){ + //TODO: insert pipeline directive + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + out[i * dim + j] += in1[i * dim + k] * in2[k * dim + j]; + } + } + } +} diff --git a/hls/lab1/exercise_4.cpp b/hls/lab1/exercise_4.cpp new file mode 100755 index 0000000..11f642c --- /dev/null +++ b/hls/lab1/exercise_4.cpp @@ -0,0 +1,33 @@ +#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 + + //TODO: create three Blocked RAM + //TODO: copy data from DRAM to BRAM + + for (int i = 0; i < dim; i++){ + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + for (int j = 0; j < dim; j++){ + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + for (int k = 0; k < dim; k++){ + //TODO: insert pipeline directive + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + out[i * dim + j] += in1[i * dim + k] * in2[k * dim + j]; + } + } + } + //TODO: copy data back from BRAM to DRAM +} diff --git a/hls/lab1/exercise_5.cpp b/hls/lab1/exercise_5.cpp new file mode 100755 index 0000000..74d0ec5 --- /dev/null +++ b/hls/lab1/exercise_5.cpp @@ -0,0 +1,36 @@ +#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 + + //TODO: create three Blocked RAM + //TODO: copy data from DRAM to BRAM + + //TODO: insert array partition directives + + for (int i = 0; i < dim; i++){ + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + for (int j = 0; j < dim; j++){ + //TODO: insert pipeline directive + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + for (int k = 0; k < dim; k++){ + //TODO: insert loop unrolling directive + #pragma HLS LOOP_TRIPCOUNT max=max_size min=max_size + out[i * dim + j] += in1[i * dim + k] * in2[k * dim + j]; + } + } + } + //TODO: copy data back from BRAM to DRAM +} diff --git a/hls/lab1/exercise_6.cpp b/hls/lab1/exercise_6.cpp new file mode 100755 index 0000000..e747e1f --- /dev/null +++ b/hls/lab1/exercise_6.cpp @@ -0,0 +1,21 @@ +#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}; + + for (int n = 0; n < SIZE; n++) { + int acc = 0; + for (int i= 0; i< N; i++ ) { + if (n - i >= 0) + acc += coeff[i] * input[n-i]; + } + output[n] = acc; + } +} diff --git a/hls/lab1/exercise_7.cpp b/hls/lab1/exercise_7.cpp new file mode 100755 index 0000000..97ab267 --- /dev/null +++ b/hls/lab1/exercise_7.cpp @@ -0,0 +1,34 @@ +#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 + + for (int n = 0; n < SIZE; n++) { + //TODO: insert pipeline directive + + int acc = 0; + + for(int j = N-1; j > 0; j--) { + //TODO: insert unroll directive + shift_reg[j] = shift_reg[j-1]; + } + + shift_reg[0] = input[n]; + + for (int j= 0; j< N; j++ ) { + //TODO: insert unroll directive + acc += shift_reg[j]*coeff[j]; + } + output[n] = acc; + } +} diff --git a/hls/lab2/TODO b/hls/lab2/TODO new file mode 100644 index 0000000..e69de29