Does Chisel support matrix operations such as addition, multiplication, transposition, etc.? If not, what is the best way to implement them?
How about vectors?
Does Chisel support matrix operations such as addition, multiplication, transposition, etc.? If not, what is the best way to implement them?
How about vectors?
Chisel does not support matrix operations. It is a DSL for writing hardware generators that implement of such operations. For examples of specialized math hardware generators see: Hwacha: A hardware vector unit and DspTools: a set of math tools
Yes, you can do matrix operations in Chisel with the help of vectors. The code I came up with is below (Input matrices are of sizes 3x5 and 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
}