C++静态对象不保存数组属性值

如何解决C++静态对象不保存数组属性值

我在 Xilinx 的 Vitis HLS 中使用 C++。我正在编写一个简单的缓冲区来执行众所周知的重叠和添加算法 (https://www.eetimes.com/fft-convolution-and-the-overlap-add-method/)

我的代码创建了一个静态对象(我的缓冲区),然后使用“采样”方法。

我使用的代码如下(header +要合成的函数+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> pkt_t;

typedef hls::axis<my_data_struct,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;

}

我的代码按预期工作,唯一的问题是在我的 top_function() 类的 Overlapper 中,buffer_realbuffer_imaginary 数组不在函数的不同调用。

特别是问题出在以下几行代码中:

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;
    
            }

我不明白为什么我的 buffer_realbuffer_imaginary 似乎在不同的调用之间被重置,即使我创建的对象是静态的(实际上变量计数器在我的不同调用之间保留top_function())

编辑:我正在添加我的输出日志

#### 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 

您可能会注意到我的缓冲区始终为 0 值 (printing second content of buffer: 0.000000),但是在我保存这些值的前一个周期中,它们似乎已正确存储 (Printing value of buffer after saving values: 0.099976)。>

基本上在打印缓冲区内容的第二“轮”中,它应该是 0.099976 而不是 0

解决方法

主要问题是在以下代码片段的索引中使用了我的 #define BUFFER IFFT_LENGTH - INPUT_WINDOW_LENGTH

 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());
    
                }

它弄乱了索引,因为它用 BUFFER 代替了 16 - 8,所以我的索引变成了 8 - 16 + 8,它仍然是 0,但可能会产生一些问题,因为如果按顺序添加,则数字变为负数。

将该行代码更改为 buffer_real[k-(BUFFER)] = IFFT_output_real[k]; 解决了我的问题。

更好的解决方案是使用 constexpr 而不是定义:

#define IFFT_LENGTH 16
#define INPUT_WINDOW_LENGTH 8
constexpr int BUFFER=IFFT_LENGTH - INPUT_WINDOW_LENGTH;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res