How do I even create a project based on accelerate?

52 views
Skip to first unread message

Nicolau Werneck

unread,
Nov 7, 2017, 10:35:44 PM11/7/17
to Accelerate
Hi. I am sorry if this is a terrible clueless newbie question, but how do I even start a project using accelerate? I am new both to Haskell and to Accelerate.

I already managed to build and run accelerate-examples, and I'm looking forward very much to start a project using it. I started a new Haskell project following basically these instructions https://gist.github.com/androidfred/a2bef54310c847f263343c529d32acd8 and now I just wanted to include the dependency on accelerate in my project, and try to use the simple dotp example in the documentation.

You perhaps don't realize how daunting it may be to a newbie to figure out all the stuff around those two lines of code from the example. I have no clue what to "import" in order to make that work... But no matter, I can just look into the accelerate-examples, right? I tried to `import Data.Array.Accelerate as Acc`, but I just kept having this error:

Could not find module ‘Data.Array.Accelerate’

Now it finally looks like this import is actually importing something from the "lib" direcotry in accelerate-examples. Now I'm completely lost. Could anyone help me understand what are the actual minimal requirements to use this library in a project, and what is a minimal boilerplate code? I just want to run that example code from the documentation. How do I get there?

Thank you,
    ++nic

Trevor McDonell

unread,
Nov 8, 2017, 12:11:36 AM11/8/17
to accelerat...@googlegroups.com

Hey Nic!

This is actually a really good question! You are completely right, I don’t think we have put anywhere how to get a minimal project set up and running, which we really should ):

From the site you linked, I guess you got stack installed ok and are happy to use that (I recommend using stack anyway).

Accelerate is on stackage now, so you’ll be able to get it if you switch to the nightly resolver.

$ stack new --resolver=nightly my-project

This will give you a stack.yaml which looks like this (I just removed the comments for brevity):

resolver: nightly-2017-11-07

packages:
- .
extra-deps: []
flags: {}

In the my-project.cabal file, you need to specify that your project will depend on (a) the Accelerate library, which defines the core language and “standard prelude”; and (b) one (or more) of the backends, which will actually run the code.

For example, in the library stanza of the my-project.cabal file, we can change the build-depends field:

library
  default-language:    Haskell2010
  hs-source-dirs:      src
  exposed-modules:     Lib
  build-depends:
      base >= 4.7 && < 5
    , accelerate
    , accelerate-llvm-native

In the above, substitute accelerate-llvm-native for accelerate-llvm-ptx if you want to run with the CUDA GPU backend instead (if you have the right hardware that is.) You can also install and use both at once, if you like.

Build the project in the usual way:

$ stack setup
$ stack build

Now, in the src/Lib.hs file, you can import Accelerate and start writing code with it:

module Lib where

import Data.Array.Accelerate as A                 -- the standard prelude
import Data.Array.Accelerate.LLVM.Native as CPU   -- this is where `run` comes from

dotp :: Acc (Vector Float) -> Acc (Vector Float) -> Acc (Scalar Float)
dotp xs ys = A.sum (A.zipWith (*) xs ys)

To execute an Accelerate program (generate code for an Acc a expression, compile, and execute it) use the run* functions from one of the backends. Let’s try that out in ghci:

$ stack ghci src/Lib.hs
> let xs = fromList (Z:.10) [0..] :: Vector Float
> let ys = fromList (Z:.10) [1,3..] :: Vector Float
> run $ dotp (use xs) (use ys)
Scalar Z [615.0]

I hope that helps get you started! Feel free to keep asking questions, no matter how newbie they might feel (we all have to start somewhere!) (:

All the best!
-Trev

--
You received this message because you are subscribed to the Google Groups "Accelerate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelerate-hask...@googlegroups.com.
Visit this group at https://groups.google.com/group/accelerate-haskell.
For more options, visit https://groups.google.com/d/optout.

Nicolau Werneck

unread,
Nov 8, 2017, 1:44:08 PM11/8/17
to Accelerate
Thanks a lot! After learning more about Cabal now I feel this was a very silly question. :) Hopefully these detailed instructions can help other newbies...

Cheers,
    +nic
Reply all
Reply to author
Forward
0 new messages