Chisel 中的矩阵运算
Matrix Operations in Chisel
Chisel是否支持加法、乘法、转置等矩阵运算?如果没有,实现它们的最佳方法是什么?
向量呢?
Chisel 不支持矩阵运算。它是一种 DSL,用于编写实现此类操作的硬件生成器。有关专用数学硬件生成器的示例,请参阅:Hwacha: A hardware vector unit and DspTools: a set of math tools
是的,您可以借助向量在 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
}
Chisel是否支持加法、乘法、转置等矩阵运算?如果没有,实现它们的最佳方法是什么?
向量呢?
Chisel 不支持矩阵运算。它是一种 DSL,用于编写实现此类操作的硬件生成器。有关专用数学硬件生成器的示例,请参阅:Hwacha: A hardware vector unit and DspTools: a set of math tools
是的,您可以借助向量在 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
}