Skip to content

Scala using GPUs: First Step

July 26, 2015

Good news: your notebook probably comes with a GPU (e.g. MacBook Pro, Lenovo ThinkPad, etc.) Running some experiments with Scala using your GPU should be easy, but the libraries are evolving fast and documentation doesn’t always keep up. Because the tutorials I found didn’t work for me right off the bat, I decided to post the sequence of steps that worked for me here (on MacOS).

  • install scala 2.9
    brew install scala29
  • make sure you’re running scala 2.9
    scala -version
  • create a file called build.sbt
    scalaVersion := "2.9.2"
    resolvers += "Sonatype OSS Snapshots Repository" at "http://oss.sonatype.org/content/groups/public/"
    resolvers += "NativeLibs4Java Repository" at "http://nativelibs4java.sourceforge.net/maven/"
    libraryDependencies += "com.nativelibs4java" % "javacl" % "1.0.0-RC2" // force latest JavaCL version
    libraryDependencies += "com.nativelibs4java" % "scalacl" % "0.2"
    autoCompilerPlugins := true
    addCompilerPlugin("com.nativelibs4java" % "scalacl-compiler-plugin" % "0.2")
  • Create a test class
    import scalacl._
    import scala.math._
    object Test {
      def main(args: Array[String]): Unit = {
        implicit val context = Context.best // prefer CPUs ? use Context.best(CPU)
        val rng = (100 until 100000).cl // transform the Range into a CLIntRange
        // ops done asynchronously on the GPU (except synchronous sum) :
        val sum = rng.map(_ * 2).zipWithIndex.map(p => p._1 / (p._2 + 1)).sum
        println("sum = " + sum)
      }
    }
  • Run the test class with sbt
    SCALACL_VERBOSE=1 sbt "run-main Test"

Sources:

Advertisements

From → tech-tips

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: