module Futhark.CodeGen.Backends.SequentialPython
     ( compileProg
     ) where

import Control.Monad

import Futhark.Representation.ExplicitMemory
import qualified Futhark.CodeGen.ImpCode.Sequential as Imp
import qualified Futhark.CodeGen.ImpGen.Sequential as ImpGen
import qualified Futhark.CodeGen.Backends.GenericPython as GenericPython
import Futhark.CodeGen.Backends.GenericPython.Definitions
import Futhark.CodeGen.Backends.GenericPython.AST
import Futhark.MonadFreshNames

compileProg :: MonadFreshNames m =>
               Maybe String -> Prog ExplicitMemory -> m String
compileProg :: Maybe String -> Prog ExplicitMemory -> m String
compileProg Maybe String
module_name =
  Prog ExplicitMemory -> m Program
forall (m :: * -> *).
MonadFreshNames m =>
Prog ExplicitMemory -> m Program
ImpGen.compileProg (Prog ExplicitMemory -> m Program)
-> (Program -> m String) -> Prog ExplicitMemory -> m String
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=>
  Maybe String
-> Constructor
-> [PyStmt]
-> [PyStmt]
-> Operations Sequential ()
-> ()
-> [PyStmt]
-> [Option]
-> Program
-> m String
forall (m :: * -> *) op s.
MonadFreshNames m =>
Maybe String
-> Constructor
-> [PyStmt]
-> [PyStmt]
-> Operations op s
-> s
-> [PyStmt]
-> [Option]
-> Definitions op
-> m String
GenericPython.compileProg
  Maybe String
module_name
  Constructor
GenericPython.emptyConstructor
  [PyStmt]
imports
  [PyStmt]
defines
  Operations Sequential ()
operations () [] []
  where imports :: [PyStmt]
imports = [String -> Maybe String -> PyStmt
Import String
"sys" Maybe String
forall a. Maybe a
Nothing,
                   String -> Maybe String -> PyStmt
Import String
"numpy" (Maybe String -> PyStmt) -> Maybe String -> PyStmt
forall a b. (a -> b) -> a -> b
$ String -> Maybe String
forall a. a -> Maybe a
Just String
"np",
                   String -> Maybe String -> PyStmt
Import String
"ctypes" (Maybe String -> PyStmt) -> Maybe String -> PyStmt
forall a b. (a -> b) -> a -> b
$ String -> Maybe String
forall a. a -> Maybe a
Just String
"ct",
                   String -> Maybe String -> PyStmt
Import String
"time" Maybe String
forall a. Maybe a
Nothing]
        defines :: [PyStmt]
defines = [String -> PyStmt
Escape String
pyValues, String -> PyStmt
Escape String
pyFunctions, String -> PyStmt
Escape String
pyPanic, String -> PyStmt
Escape String
pyTuning]
        operations :: GenericPython.Operations Imp.Sequential ()
        operations :: Operations Sequential ()
operations = Operations Sequential ()
forall op s. Operations op s
GenericPython.defaultOperations
                     { opsCompiler :: OpCompiler Sequential ()
GenericPython.opsCompiler = CompilerM Sequential () () -> OpCompiler Sequential ()
forall a b. a -> b -> a
const (CompilerM Sequential () () -> OpCompiler Sequential ())
-> CompilerM Sequential () () -> OpCompiler Sequential ()
forall a b. (a -> b) -> a -> b
$ () -> CompilerM Sequential () ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                     , opsCopy :: Copy Sequential ()
GenericPython.opsCopy = Copy Sequential ()
copySequentialMemory
                     }

copySequentialMemory :: GenericPython.Copy Imp.Sequential ()
copySequentialMemory :: Copy Sequential ()
copySequentialMemory PyExp
destmem PyExp
destidx Space
DefaultSpace PyExp
srcmem PyExp
srcidx Space
DefaultSpace PyExp
nbytes PrimType
_bt =
  PyExp
-> PyExp -> PyExp -> PyExp -> PyExp -> CompilerM Sequential () ()
forall op s.
PyExp -> PyExp -> PyExp -> PyExp -> PyExp -> CompilerM op s ()
GenericPython.copyMemoryDefaultSpace PyExp
destmem PyExp
destidx PyExp
srcmem PyExp
srcidx PyExp
nbytes
copySequentialMemory PyExp
_ PyExp
_ Space
destspace PyExp
_ PyExp
_ Space
srcspace PyExp
_ PrimType
_ =
  String -> CompilerM Sequential () ()
forall a. HasCallStack => String -> a
error (String -> CompilerM Sequential () ())
-> String -> CompilerM Sequential () ()
forall a b. (a -> b) -> a -> b
$ String
"Cannot copy to " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Space -> String
forall a. Show a => a -> String
show Space
destspace String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" from " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Space -> String
forall a. Show a => a -> String
show Space
srcspace