mirror of
https://github.com/Steffo99/unimore-hpc-assignments.git
synced 2024-11-25 01:24:22 +00:00
HLS lab 1
This commit is contained in:
parent
32d3b1ebe0
commit
3c3ddf4bec
10 changed files with 302 additions and 0 deletions
17
hls/lab1/exercise_0_solution.cpp
Executable file
17
hls/lab1/exercise_0_solution.cpp
Executable file
|
@ -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];
|
||||||
|
}
|
||||||
|
}
|
19
hls/lab1/exercise_1.cpp
Executable file
19
hls/lab1/exercise_1.cpp
Executable file
|
@ -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];
|
||||||
|
}
|
||||||
|
}
|
54
hls/lab1/exercise_2.cpp
Executable file
54
hls/lab1/exercise_2.cpp
Executable file
|
@ -0,0 +1,54 @@
|
||||||
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#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];
|
||||||
|
}
|
||||||
|
}
|
59
hls/lab1/exercise_2_solution.cpp
Executable file
59
hls/lab1/exercise_2_solution.cpp
Executable file
|
@ -0,0 +1,59 @@
|
||||||
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#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];
|
||||||
|
}
|
||||||
|
}
|
29
hls/lab1/exercise_3.cpp
Executable file
29
hls/lab1/exercise_3.cpp
Executable file
|
@ -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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
hls/lab1/exercise_4.cpp
Executable file
33
hls/lab1/exercise_4.cpp
Executable file
|
@ -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
|
||||||
|
}
|
36
hls/lab1/exercise_5.cpp
Executable file
36
hls/lab1/exercise_5.cpp
Executable file
|
@ -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
|
||||||
|
}
|
21
hls/lab1/exercise_6.cpp
Executable file
21
hls/lab1/exercise_6.cpp
Executable file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
34
hls/lab1/exercise_7.cpp
Executable file
34
hls/lab1/exercise_7.cpp
Executable file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
0
hls/lab2/TODO
Normal file
0
hls/lab2/TODO
Normal file
Loading…
Reference in a new issue