diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2011-01-17 15:18:06 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2011-01-17 15:18:06 +0000 |
commit | 16e7ad360d1a902d6bd5c845642dbe14bcecdb9d (patch) | |
tree | a189e624a75dbb47a689a821208b41b0141e2ee0 /test/files/specialized/spec-matrix.scala | |
parent | be2778d50f1a73a941c6ffe6355f0ed401d6d8eb (diff) | |
download | scala-16e7ad360d1a902d6bd5c845642dbe14bcecdb9d.tar.gz scala-16e7ad360d1a902d6bd5c845642dbe14bcecdb9d.tar.bz2 scala-16e7ad360d1a902d6bd5c845642dbe14bcecdb9d.zip |
Adapted specialization tests to track number of...
Adapted specialization tests to track number of boxings.
Review by dragos
Diffstat (limited to 'test/files/specialized/spec-matrix.scala')
-rw-r--r-- | test/files/specialized/spec-matrix.scala | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/test/files/specialized/spec-matrix.scala b/test/files/specialized/spec-matrix.scala new file mode 100644 index 0000000000..52bd3077d7 --- /dev/null +++ b/test/files/specialized/spec-matrix.scala @@ -0,0 +1,72 @@ +/** Test matrix multiplication with specialization. + */ + +class Matrix[@specialized A: ClassManifest](val rows: Int, val cols: Int) { + private val arr: Array[Array[A]] = new Array[Array[A]](rows, cols) + + def apply(i: Int, j: Int): A = { + if (i < 0 || i >= rows || j < 0 || j >= cols) + throw new NoSuchElementException("Indexes out of bounds: " + (i, j)) + + arr(i)(j) + } + + def update(i: Int, j: Int, e: A) { + arr(i)(j) = e + } + + def rowsIterator: Iterator[Array[A]] = new Iterator[Array[A]] { + var idx = 0; + def hasNext = idx < rows + def next = { + idx += 1 + arr(idx - 1) + } + } +} + +object Test { + def main(args: Array[String]) { + val m = randomMatrix(200, 100) + val n = randomMatrix(100, 200) + + mult(m, n) + println("*") + + println(runtime.BoxesRunTime.integerBoxCount) + } + + def randomMatrix(n: Int, m: Int) = { + val r = new util.Random(10) + val x = new Matrix[Int](n, m) + for (i <- 0 until n; j <- 0 until m) + x(i, j) = r.nextInt + x + } + + + def multManifest[@specialized(Int) T](m: Matrix[T], n: Matrix[T])(implicit cm: ClassManifest[T], num: Numeric[T]) { + val p = new Matrix[T](m.rows, n.cols) + import num._ + + for (i <- 0 until m.rows) + for (j <- 0 until n.cols) { + var sum = num.zero + for (k <- 0 until n.rows) + sum += m(i, k) * n(k, j) + p(i, j) = sum + } + } + + def mult(m: Matrix[Int], n: Matrix[Int]) { + val p = new Matrix[Int](m.rows, n.cols) + + for (i <- 0 until m.rows) + for (j <- 0 until n.cols) { + var sum = 0 + for (k <- 0 until n.rows) + sum += m(i, k) * n(k, j) + p(i, j) = sum + } + } +} |