Chisel是否支持矩阵运算,例如加法,乘法,换位等?如果没有,实施它们的最佳方法是什么?
载体怎么样?
答案 0 :(得分:1)
Chisel不支持矩阵运算。它是用于编写实现此类操作的硬件生成器的DSL。有关专用数学硬件生成器的示例,请参阅:Hwacha: A hardware vector unit和DspTools: a set of math tools
答案 1 :(得分:0)
是的,你可以借助矢量在Chisel中进行矩阵运算。我想出的代码如下(输入矩阵的大小为3x5和5x2)。
package matrixmult
import chisel3._
import chisel3.iotesters.{PeekPokeTester, Driver}
import scala.collection.mutable.ArrayBuffer
class MatMult extends Module {
val io = IO(new Bundle {
val matA = Input(Vec(15, UInt(32.W)))
val matB = Input(Vec(10, UInt(32.W)))
val load = Input(Bool())
val matC = Output(Vec(6, UInt(32.W)))
val valid = Output(Bool())
})
var sum = UInt(32.W)
val matC = new ArrayBuffer[UInt]()
for(i <- 0 until 6) {
matC += 0.asUInt(32.W)
}
when (io.load) {
for(i <- 0 until 3) {
for(j <- 0 until 2) {
sum = 0.asUInt(32.W)
for(k <- 0 until 5)
{
sum = sum + matA(i*5+k)*io.matB(k*2+j)
}
matC(i*2 + j) = sum
}
}
io.valid := true.B
} .otherwise {
io.valid := false.B
}
val outputMat = Vec(matC)
io.matC := outputMat
}