Run Scala code on your GPU with ScalaCL in 1 minute !
ScalaCL is still very young but you can already play with it in a few seconds… Here’s how :
Prequisites
- You must have an OpenCL implementation installed on your computer : it’s automatically the case under MacOS X 10.6 (Snow Leopard) or later, you’ll need to install one from NVIDIA, ATI or Intel otherwise (see ScalaCL’s wiki for more information)
- You need to install Scala 2.8.1. If you haven’t yet, I suggest running the IzPack Installer (on Windows 64 bits, make sure to install it in `c:\Program Files`, not `c:\Program Files (x86)`)
Start your chronometer
-
Install ScalaCL’s compiler plugin & collections using sbaz (sbaz is part of the Scala distribution) :
sbaz update sbaz install scalacl
-
Open a Scala console in command line :
scala
-
Add some imports :
import scalacl._ ; import scala.math._
-
Create an OpenCL context (has to be ‘implicit’) :
implicit val context = Context.best // prefer CPUs ? Context.best(CPU)
-
Create an OpenCL collection :
val a = (0 until 100000).cl // this gives a CLRange
-
Run some maps and filters on your GPU :
val result = a.map(x => cos(x / 100.0f).toFloat).zipWithIndex map { case (c, i) => c * 10 + i } filter { v => (v.toInt % 2) == 1 }
-
Need to get back an Array[Float] ? CLArray[T], CLRange and other CLIndexedSeq[T] collections behave as expected :
result.toArray
That’s it !
You’ve just run some Scala code on your graphic card !!!
Wanna get juicy details on what’s happening under the hood ? Set the `SCALACL_VERBOSE` environment variable to `1` before launching the Scala console (or the Scala compiler) :
- On Windows : set SCALACL_VERBOSE=1
- With a bash shell (MacOS X and Linux default) : SCALACL_VERBOSE=1 scala
- With a tcsh shell : env SCALACL_VERBOSE=1 scala
Reactions ? Join NativeLibs4Java’s Google Group !
Wanna learn more ? Read ScalaCL’s FAQ.