I am working in C++ inside Vitis HLS from Xilinx. I am writing a simple buffer to perform the well know overlap and add algorithm (https://www.eetimes.com/fft-convolution-and-the-overlap-add-method/)
My code creates a static object (my buffer) and then works with a "sample-wise" approach.
The code I am using is the following (header + function to be synthesized + testbench):
Overlapper_HLS.hpp:
#include "ap_axi_sdata.h"
#include "hls_stream.h"
#include <ap_fixed.h>
#define IFFT_LENGTH 16
#define INPUT_WINDOW_LENGTH 8
#define BUFFER IFFT_LENGTH - INPUT_WINDOW_LENGTH
typedef ap_fixed<16,1> ap_fixed_data_type;
typedef struct {
ap_fixed_data_type real_part;
ap_fixed_data_type imaginary_part;
} my_data_struct;
typedef hls::axis<my_data_struct,0,0,0> pkt_t;
typedef hls::axis<my_data_struct,0,0,0> pkt_t_out;
class Overlapper {
private:
ap_fixed_data_type IFFT_output_real[IFFT_LENGTH];
ap_fixed_data_type IFFT_output_imaginary[IFFT_LENGTH];
ap_fixed_data_type output_chunk_real[INPUT_WINDOW_LENGTH];
ap_fixed_data_type output_chunk_imaginary[INPUT_WINDOW_LENGTH];
ap_fixed_data_type buffer_real[BUFFER];
ap_fixed_data_type buffer_imaginary[BUFFER];
int counter;
public:
Overlapper() {
for(int i = 0; i<IFFT_LENGTH; i++){
IFFT_output_real[i]= 0;
IFFT_output_imaginary[i] = 0;
}
for(int i = 0; i<INPUT_WINDOW_LENGTH; i++){
output_chunk_real[i]= 0;
output_chunk_imaginary[i] = 0;
}
for(int i = 0; i<BUFFER; i++){
buffer_real[i]= 0;
buffer_imaginary[i] = 0;
}
counter = 0;
}
void top_function(
hls::stream<pkt_t> &input_signal_stream,
hls::stream<pkt_t> &output_signal_stream) {
#pragma HLS PIPELINE
pkt_t pkt_input_signal;
pkt_t pkt_output_signal;
float tmp =0;
int i = 0;
if(counter < IFFT_LENGTH){
i = counter;
input_signal_stream.read(pkt_input_signal);
IFFT_output_real[i] = pkt_input_signal.data.real_part;
IFFT_output_imaginary[i] = pkt_input_signal.data.imaginary_part;
counter++;
printf("printing counter: %d \n", counter);
printf("printing i: %d \n", i);
printf("printing input value: %f \n", pkt_input_signal.data.real_part.to_float());
printf("printing value of IFFT_real: %f \n", IFFT_output_real[i].to_float());
}
else{
//Buffer does not contain the previously stored values anymore, it is always 0.
for(int i=0; i<BUFFER; i++){
printf("printing content of buffer: %f \n", buffer_real[i].to_float());
}
for(int i=0; i<BUFFER; i++){
IFFT_output_real[i] += buffer_real[i];
IFFT_output_imaginary[i] += buffer_imaginary[i];
printf("checking buffer value before sum: %f \n", buffer_real[i].to_float());
printf("checking sum result real: %f \n", IFFT_output_real[i].to_float());
printf("checking sum result imaginary: %f \n", IFFT_output_imaginary[i].to_float());
}
for(int k=INPUT_WINDOW_LENGTH; k<IFFT_LENGTH; k++){
buffer_real[k-BUFFER] = IFFT_output_real[k];
buffer_imaginary[k-BUFFER] = IFFT_output_imaginary[k];
printf("Printing value of buffer: %f \n", buffer_real[k-BUFFER].to_float());
}
//Here it seems that the values in Buffer are stored correctly
for(int i=0; i<BUFFER; i++){
printf("printing content of buffer: %f \n", buffer_real[i].to_float());
}
for(int j=0; j<INPUT_WINDOW_LENGTH; j++){
pkt_output_signal.data.real_part = IFFT_output_real[j];
pkt_output_signal.data.imaginary_part = IFFT_output_imaginary[j];
printf("printing output signal: %f \n", pkt_output_signal.data.real_part.to_float());
output_signal_stream.write(pkt_output_signal);
}
counter = 0;
}
}
};
void overlapper(hls::stream<pkt_t> &input_signal_stream, hls::stream<pkt_t> &output_signal_stream);
Overlapper_HLS.cpp:
#include <overlapper_HLS.hpp>
void overlapper(hls::stream<pkt_t> &input_signal_stream, hls::stream<pkt_t> &output_signal_stream){
#pragma HLS INTERFACE axis register port=input_stream
#pragma HLS INTERFACE axis register port=output_stream
#pragma HLS INTERFACE ap_ctrl_none port=return
static Overlapper bb;
bb.top_function(input_signal_stream, output_signal_stream);
}
testbench.cpp:
#include <overlapper_HLS.hpp>
int main(){
hls::stream<pkt_t> input_stream;
hls::stream<pkt_t> output_stream;
pkt_t input_data;
pkt_t temp_output;
float int_output, input_check;
// pkt_t input_package_check
input_data.data.real_part = 0.1;
input_data.data.imaginary_part = 0.1;
for(int i=0; i<32; i++){
input_stream.write(input_data);
}
for(int i=0; i<34; i++){
overlapper(input_stream, output_stream);
}
for(int i=0; i<16; i++){
output_stream.read(temp_output);
//printf("output_value: %f", temp_output.data.real_part.to_float());
printf("output_value: %f \n", temp_output.data.real_part.to_float());
}
return 0;
}
My code works as expected, the only problem I have is that inside the top_function()
of my Overlapper
class, the buffer_real
and buffer_imaginary
arrays do not store values between different calls of the function.
In particular the problem is in the following lines of code:
else{
//Here unfortunately the stored value is always 0! It seems like the values are "reset" between different calls of the function.
for(int i=0; i<BUFFER; i++){
printf("printing content of buffer: %f \n", buffer_real[i]);
}
for(int i=0; i<BUFFER; i++){
IFFT_output_real[i] += buffer_real[i];
IFFT_output_imaginary[i] += buffer_imaginary[i];
printf("checking buffer value before sum: %f \n", buffer_real[i].to_float());
printf("checking sum result real: %f \n", IFFT_output_real[i].to_float());
printf("checking sum result imaginary: %f \n", IFFT_output_imaginary[i].to_float());
}
for(int k=INPUT_WINDOW_LENGTH; k<IFFT_LENGTH; k++){
//Here I am saving the values I need inside my buffer
buffer_real[k-BUFFER] = IFFT_output_real[k];
buffer_imaginary[k-BUFFER] = IFFT_output_imaginary[k];
printf("Printing value of buffer: %f \n", buffer_real[k-BUFFER].to_float());
}
//Once I assign the values in the previous for cycle, they seem to be stored correctly!
for(int i=0; i<BUFFER; i++){
printf("printing content of buffer: %f \n", buffer_real[i]);
}
for(int j=0; j<INPUT_WINDOW_LENGTH; j++){
pkt_output_signal.data.real_part = IFFT_output_real[j];
pkt_output_signal.data.imaginary_part = IFFT_output_imaginary[j];
printf("printing output signal: %f \n", pkt_output_signal.data.real_part.to_float());
output_signal_stream.write(pkt_output_signal);
}
counter = 0;
}
I can not figure out why my buffer_real
and buffer_imaginary
seem to be reset between different calls, even if the object I created is static (in fact the variable counter is preserved between different calls of my top_function()
)
EDIT : I am adding my output log
#### FIRST ROUND ######
printing counter: 1
printing i: 0
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 2
printing i: 1
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 3
printing i: 2
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 4
printing i: 3
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 5
printing i: 4
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 6
printing i: 5
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 7
printing i: 6
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 8
printing i: 7
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 9
printing i: 8
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 10
printing i: 9
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 11
printing i: 10
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 12
printing i: 11
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 13
printing i: 12
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 14
printing i: 13
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 15
printing i: 14
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 16
printing i: 15
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
### SECOND ROUND ###
printing counter: 1
printing i: 0
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 2
printing i: 1
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 3
printing i: 2
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 4
printing i: 3
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 5
printing i: 4
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 6
printing i: 5
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 7
printing i: 6
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 8
printing i: 7
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 9
printing i: 8
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 10
printing i: 9
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 11
printing i: 10
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 12
printing i: 11
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 13
printing i: 12
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 14
printing i: 13
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 15
printing i: 14
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing counter: 16
printing i: 15
printing input value: 0.099976
printing value of IFFT_real: 0.099976
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
printing first content of buffer: 0.000000
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
checking buffer value before sum: 0.000000
checking sum result real: 0.099976
checking sum result imaginary: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
Printing value of buffer after saving values: 0.099976
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing second content of buffer: 0.000000
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
printing output signal: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
output_value: 0.099976
As you might notice my buffer has always 0 value (printing second content of buffer: 0.000000
), however in the previous cycle where I save the values, it seems that they're stored correctly (Printing value of buffer after saving values: 0.099976
).
Basically in the second "round" of printing the content of my buffer, it should be 0.099976
instead of 0
The main problem was the usage of my #define BUFFER IFFT_LENGTH - INPUT_WINDOW_LENGTH
inside the indexing of the following code snippet:
for(int k=INPUT_WINDOW_LENGTH; k<IFFT_LENGTH; k++){
//Here I am saving the values I need inside my buffer
buffer_real[k-BUFFER] = IFFT_output_real[k];
buffer_imaginary[k-BUFFER] = IFFT_output_imaginary[k];
printf("Printing value of buffer: %f \n", buffer_real[k-BUFFER].to_float());
}
It messes up the indexing because is substitutes BUFFER
with 16 - 8
as it is, so my indexing becomes 8 - 16 + 8
, which is still 0 as intended, but probably it creates some problems because the number becomes negative if added sequentially.
Changing that line of code to buffer_real[k-(BUFFER)] = IFFT_output_real[k];
solved my problem.
A better solution is using constexpr
instead of a define:
#define IFFT_LENGTH 16
#define INPUT_WINDOW_LENGTH 8
constexpr int BUFFER=IFFT_LENGTH - INPUT_WINDOW_LENGTH;