【问题标题】:How to add elements in Vec like a binary tree's leaf nodes?如何在 Vec 中添加元素,如二叉树的叶节点?
【发布时间】:2021-09-02 08:22:14
【问题描述】:

假设 Vec 中有 n = 2^x 个元素,

在 Vec 中使用添加元素

vec.reduce(_ +& _)

最终会得到元素宽度 = w + 2^x - 1

如果像树一样为每两个元素添加它们,宽度将为w + x

好像

vec.par.reduce(_ +& _)

适用于 4 个元素,但不适用于 8 或 16 个元素。

有什么解决方法吗?

【问题讨论】:

    标签: chisel


    【解决方案1】:

    我建议你看看 chisel3 中的实验性Interval 类型。有一些有限的文档here。间隔就是为这种情况而设计的。您甚至不必更改所有内容,只需将 Vec 的元素映射到间隔,然后减少它们。

    【讨论】:

      【解决方案2】:

      chisel3.Vec 有一个 reduceTree 方法可以做你想做的事:

      class Example extends MultiIOModule {
        val in = IO(Input(Vec(8, UInt(8.W))))
        val out = IO(Output(UInt())) // Infers width to be 11
        
        out := in.reduceTree(_ +& _)
      }
      

      Scastie 链接:https://scastie.scala-lang.org/rhm4je13T0ykEtRS7P7mfQ

      另请参阅 Vec API 文档:https://www.chisel-lang.org/api/latest/chisel3/Vec.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 2019-12-25
        相关资源
        最近更新 更多