@@ -42,30 +42,40 @@ resolvers += Resolver.sonatypeRepo("snapshots")
42
42
43
43
# Examples
44
44
45
- The following will eventually work :
45
+ The following example currently works :
46
46
47
47
``` scala
48
48
import scalacl ._
49
49
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 ) =
52
57
this (new CLArray [Float ](rows * columns), rows, columns)
53
- def this (n : Int ) =
58
+
59
+ def this (n : Int )
60
+ (implicit context : Context ) =
54
61
this (n, n)
55
-
62
+
56
63
def putProduct (a : Matrix , b : Matrix ): Unit = {
57
64
assert(a.columns == b.rows)
58
65
assert(a.rows == rows)
59
66
assert(b.columns == columns)
60
67
61
68
kernel {
62
69
// 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
+ }
69
79
}
70
80
71
81
def putSum (a : Matrix , b : Matrix ): Unit = {
@@ -75,12 +85,12 @@ case class Matrix(data: CLArray[Float], rows: Int, columns: Int)(implicit contex
75
85
kernel {
76
86
for (i <- 0 until rows; j <- 0 until columns) {
77
87
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
+ }
81
91
}
82
92
}
83
-
93
+
84
94
implicit val context = Context .best
85
95
86
96
val n = 10
0 commit comments