<commit_before>package benchmark

import java.io.File
import java.util.concurrent.TimeUnit

import benchmark.jni.NativeMethods
import org.openjdk.jmh.annotations._

import scalan.compilation.GraphVizConfig
import scalan.compilation.lms.cxx.{CommunityCXXLmsBackend, LmsCompilerCXX}
import scalan.compilation.lms.scalac.LmsCompilerScala
import scalan.compilation.lms.{CommunityBridgeScala, CommunityLmsBackend}
import scalan.linalgebra.LinearAlgebraExamples
import scalan.util.FileUtil
import scalan.{ScalanCommunityDslExp, ScalanCommunityDslSeq}

object MvmBenchmark {
  @State(Scope.Benchmark)
  @volatile
  class MvmStateBase {
    // all declared vars to make them volatile
    var height = 10000
    var width = 10000
    var matSparse = 0.9
    var vecSparse = 0.1
    var max = 10

    def genRandVec(len: Int, sp: Double) = {
      val dense = (for (i <- 0 until len) yield   {
        val e = scala.util.Random.nextDouble()
        if (e >= sp) e*max else 0.0
      }).toArray
      //dense
      val (spInd, spVals) = ((0 until len).toArray zip dense).filter( x => x._2 != 0).unzip
      (dense, (spInd.toArray, (spVals.toArray, width)))
    }

    def genRandMat(h: Int, w: Int, sp: Double) = {
      (for (i <- 0 until h) yield genRandVec(w, sp)).toArray
      val (dmat, smat) = (for (i <- 0 until h) yield genRandVec(w, sp)).unzip
      (dmat.toArray, smat.toArray)
    }

    def printSparseVec[T]( sv: (Array[Int], Array[T] ), prefix: String = "") = {
      println(prefix + (sv._1 zip sv._2).mkString(" "))
    }
    def printDenseVec[T](dv: Array[T], prefix: String = "") = {
      println(prefix + dv.mkString(" "))
    }
    def printDenseMat[T](m: Array[Array[T]], prefix: String = "") = {
      println(prefix)
      for (i <- m) printDenseVec(i)
      println("")
    }
    def printSparseMat[T](m: Array[(Array[Int], Array[T])], prefix: String = "") = {
      println(prefix)
      for (i <- m) printSparseVec(i)
      println("")
    }


    var (dvec, svec) = genRandVec(width, vecSparse)
    var (dmat, smat) = genRandMat(height, width, matSparse)
    var fmat = (dmat.flatten, width)

    // calculate correct result
    val right:Array[Double] = scala.Array.tabulate(dmat.length)(_=>0.0)
    for(i <- 0 until dmat.length) {
      for(j <- 0 until dmat(i).length) {
        right(i) += dmat(i)(j) * dvec(j)
      }
    }

    var res: Array[Double] = Array.empty;

    @TearDown
    def check(): Unit = {
      require(res.sameElements(right), "res.sameElements(right)")
    }

    //printDenseVec(dvec, "Dense Vector:")
    //printSparseVec(svec, "Sparse Vector:")
    //printDenseMat(dmat, "Dense Matrix:")
    //printSparseMat(smat, "Sparse Matrix:")

    //val dmdv_res = (new dmdvm())(dmat, dvec)
    //printDenseVec(dmdv_res, "Dense x Dense: ")

    //val dmsv_res = (new dmsvm())(dmat, svec)
    //printDenseVec(dmsv_res, "Dense x Sparse: ")

    //val smdv_res = (new smdvm())(smat, dvec)
    //printDenseVec(smdv_res, "Sparse x Dense: ")

    //val smsv_res = (new smsvm())(smat, svec)
    //printDenseVec(smsv_res, " Sparse x Sparse: ")
  }<commit_msg>use @Param in MvmBenchmark<commit_after>package benchmark

import java.io.File
import java.util.concurrent.TimeUnit

import benchmark.jni.NativeMethods
import org.openjdk.jmh.annotations._

import scalan.compilation.GraphVizConfig
import scalan.compilation.lms.cxx.{CommunityCXXLmsBackend, LmsCompilerCXX}
import scalan.compilation.lms.scalac.LmsCompilerScala
import scalan.compilation.lms.{CommunityBridgeScala, CommunityLmsBackend}
import scalan.linalgebra.LinearAlgebraExamples
import scalan.util.FileUtil
import scalan.{ScalanCommunityDslExp, ScalanCommunityDslSeq}

object MvmBenchmark {
  @State(Scope.Benchmark)
  @volatile
  class MvmStateBase {
    // all declared vars to make them volatile
    @Param(Array("10000"))
    var height:Int = _
    @Param(Array("10000"))
    var width:Int = _
    @Param(Array("0.9"))
    var matSparse:Double = _
    @Param(Array("0.1"))
    var vecSparse:Double = _

    type DVec[T] = Array[T]
    type SVec[T] = (Array[Int], (Array[T], Int))
    var dvec: DVec[Double] = _
    var svec: SVec[Double] = _
    var dmat: Array[DVec[Double]] = _
    var smat: Array[SVec[Double]] = _
    var fmat: (DVec[Double], Int) = _

    var right: DVec[Double] = _
    var res: DVec[Double] = Array.empty;

    @Setup
    def prepare(): Unit = {
      val p = genRandVec(width, vecSparse)
      dvec = p._1
      svec = p._2

      val p1 = genRandMat(height, width, matSparse)
      dmat = p1._1
      smat = p1._2

      fmat = (dmat.flatten, width)

      // calculate correct result
      right = scala.Array.tabulate(dmat.length)(_=>0.0)
      for(i <- 0 until dmat.length) {
        for(j <- 0 until dmat(i).length) {
          right(i) += dmat(i)(j) * dvec(j)
        }
      }
    }

    var max = 10

    def genRandVec(len: Int, sp: Double) = {
      val dense = (for (i <- 0 until len) yield   {
        val e = scala.util.Random.nextDouble()
        if (e >= sp) e*max else 0.0
      }).toArray
      //dense
      val (spInd, spVals) = ((0 until len).toArray zip dense).filter( x => x._2 != 0).unzip
      (dense, (spInd.toArray, (spVals.toArray, width)))
    }

    def genRandMat(h: Int, w: Int, sp: Double) = {
      (for (i <- 0 until h) yield genRandVec(w, sp)).toArray
      val (dmat, smat) = (for (i <- 0 until h) yield genRandVec(w, sp)).unzip
      (dmat.toArray, smat.toArray)
    }

    def printSparseVec[T]( sv: (Array[Int], Array[T] ), prefix: String = "") = {
      println(prefix + (sv._1 zip sv._2).mkString(" "))
    }
    def printDenseVec[T](dv: Array[T], prefix: String = "") = {
      println(prefix + dv.mkString(" "))
    }
    def printDenseMat[T](m: Array[Array[T]], prefix: String = "") = {
      println(prefix)
      for (i <- m) printDenseVec(i)
      println("")
    }
    def printSparseMat[T](m: Array[(Array[Int], Array[T])], prefix: String = "") = {
      println(prefix)
      for (i <- m) printSparseVec(i)
      println("")
    }

    @TearDown
    def check(): Unit = {
      require(res.sameElements(right), "res.sameElements(right)")
    }

    //printDenseVec(dvec, "Dense Vector:")
    //printSparseVec(svec, "Sparse Vector:")
    //printDenseMat(dmat, "Dense Matrix:")
    //printSparseMat(smat, "Sparse Matrix:")

    //val dmdv_res = (new dmdvm())(dmat, dvec)
    //printDenseVec(dmdv_res, "Dense x Dense: ")

    //val dmsv_res = (new dmsvm())(dmat, svec)
    //printDenseVec(dmsv_res, "Dense x Sparse: ")

    //val smdv_res = (new smdvm())(smat, dvec)
    //printDenseVec(smdv_res, "Sparse x Dense: ")

    //val smsv_res = (new smsvm())(smat, svec)
    //printDenseVec(smsv_res, " Sparse x Sparse: ")
  }
{'fraction_non_alphanumeric': 0.15285671552497757, 'fraction_numerical': 0.010020939276099311, 'mean_word_length': 2.9544648137196923, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2763396', 'n_tokens_mistral': 2598, 'n_tokens_neox': 2457, 'n_words': 544}