Matrix Operations in Chisel

不问归期 提交于 2019-12-13 06:19:16

问题


Does Chisel support matrix operations such as addition, multiplication, transposition, etc.? If not, what is the best way to implement them?

How about vectors?


回答1:


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




回答2:


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   

}


来源:https://stackoverflow.com/questions/41704794/matrix-operations-in-chisel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!