diff --git a/Data/Array/Accelerate/Repa.hs b/Data/Array/Accelerate/Repa.hs index 1a99dc8..91cd890 100644 --- a/Data/Array/Accelerate/Repa.hs +++ b/Data/Array/Accelerate/Repa.hs @@ -11,6 +11,7 @@ module Data.Array.Accelerate.Repa ( Arrays , run + , compileToFile ) where @@ -22,10 +23,11 @@ import Data.Array.Accelerate.Repa.Stencil (stencilDoc) import Text.PrettyPrint --- | Used to compile and run an embedded array program using the Repa backend ---run :: Arrays a => Smart.Acc a -> String ---run = evalAcc . Smart.convertAcc +import GHC -- For compiling and running using GHC API +import GHC.Paths (libdir) -- simplifies use of GHC API +import System.IO -- For writing source to a file +-- | Used to compile and run an embedded array program using the Repa backend run :: Arrays a => Smart.Acc a -> String run acc = show $ headS $$ (nest 1 (evalAcc $ Smart.convertAcc acc)) @@ -33,12 +35,38 @@ run acc = show $ $+$ text " " $$ stencilDoc +compileToFile :: Arrays a => Maybe String -> Smart.Acc a -> IO () +compileToFile targetFile acc = do + let f = case targetFile of + Just s -> s + Nothing -> defaultFile + -- writes source to file as currently can't compile from String + writeFile f $ run acc + -- using GHC API from here + r <- loadAndCompile f + case r of + Just err -> error err + Nothing -> return () + +loadAndCompile :: String -> IO (Maybe String) +loadAndCompile targetFile = runGhc (Just libdir) $ do + dflags <- getSessionDynFlags + setSessionDynFlags (dflags{ + optLevel = 2 + }) + target <- guessTarget targetFile Nothing + addTarget target + r <- load LoadAllTargets + return $ case r of + Failed -> Just "Error in module loading" + Succeeded -> Nothing + headS :: Doc {-# INLINE headS #-} headS = text "{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, TypeOperators #-}" $+$ text "{-# LANGUAGE FlexibleContexts #-}" $+$ - text "module AccRepa where" $+$ + text "module" <+> text modName <+> text "where" $+$ text "import Data.Array.Repa as Repa" $+$ text "import Data.Bits -- required for Prim ops" $+$ text "import Data.Char -- required for Prim ops" $+$ @@ -51,3 +79,11 @@ headS = tailS :: Doc {-# INLINE tailS #-} tailS = empty + +modName :: String +{-# INLINE modName #-} +modName = "Main" + +defaultFile :: String +{-# INLINE defaultFile #-} +defaultFile = "AccRepa.hs" diff --git a/accelerate-repa.cabal b/accelerate-repa.cabal index fe644b8..6a4ae68 100644 --- a/accelerate-repa.cabal +++ b/accelerate-repa.cabal @@ -18,7 +18,9 @@ Library Build-depends: repa == 2.*, accelerate == 0.9.*, base, - pretty == 1.1.* + pretty == 1.1.*, + ghc-paths == 0.1.*, + ghc == 7.2.* Exposed-modules: Data.Array.Accelerate.Repa Other-modules: Data.Array.Accelerate.Repa.Evaluations