@@ -42,30 +42,40 @@ resolvers += Resolver.sonatypeRepo("snapshots")
4242
4343# Examples
4444
45- The following will eventually work :
45+ The following example currently works :
4646
4747``` scala
4848import scalacl ._
4949
50- case class Matrix (data : CLArray [Float ], rows : Int , columns : Int )(implicit context : Context ) {
51- def this (rows : Int , columns : Int ) =
50+ case class Matrix (data : CLArray [Float ],
51+ rows : Int ,
52+ columns : Int )
53+ (implicit context : Context )
54+ {
55+ def this (rows : Int , columns : Int )
56+ (implicit context : Context ) =
5257 this (new CLArray [Float ](rows * columns), rows, columns)
53- def this (n : Int ) =
58+
59+ def this (n : Int )
60+ (implicit context : Context ) =
5461 this (n, n)
55-
62+
5663 def putProduct (a : Matrix , b : Matrix ): Unit = {
5764 assert(a.columns == b.rows)
5865 assert(a.rows == rows)
5966 assert(b.columns == columns)
6067
6168 kernel {
6269 // This block will either be converted to an OpenCL kernel or cause compilation error
63- for (i <- 0 until rows; j <- 0 until columns) {
64- data(i * columns + j) = (0 until a.columns).map(k => {
65- a.data(i * a.columns + k) * b.data(k * b.columns + j)
66- }).sum
67- }
68- }
70+ for (i <- 0 until rows;
71+ j <- 0 until columns) {
72+ // c(i, j) = sum(k, a(i, k) * b(k, j))
73+ data(i * columns + j) = (
74+ for (k <- 0 until a.columns) yield
75+ a.data(i * a.columns + k) * b.data(k * b.columns + j)
76+ ).sum
77+ }
78+ }
6979 }
7080
7181 def putSum (a : Matrix , b : Matrix ): Unit = {
@@ -75,12 +85,12 @@ case class Matrix(data: CLArray[Float], rows: Int, columns: Int)(implicit contex
7585 kernel {
7686 for (i <- 0 until rows; j <- 0 until columns) {
7787 val offset = i * columns + j
78- data(offset) = a.data(offset) + b.data(offset)
79- }
80- }
88+ data(offset) = a.data(offset) + b.data(offset)
89+ }
90+ }
8191 }
8292}
83-
93+
8494implicit val context = Context .best
8595
8696val n = 10
0 commit comments