module Futhark.CodeGen.ImpGen.Multicore.SegScan
  ( compileSegScan,
  )
where

import Control.Monad
import Data.List (zip4)
import qualified Futhark.CodeGen.ImpCode.Multicore as Imp
import Futhark.CodeGen.ImpGen
import Futhark.CodeGen.ImpGen.Multicore.Base
import Futhark.IR.MCMem
import Futhark.Util.IntegralExp (quot, rem)
import Prelude hiding (quot, rem)

-- Compile a SegScan construct
compileSegScan ::
  Pat LetDecMem ->
  SegSpace ->
  [SegBinOp MCMem] ->
  KernelBody MCMem ->
  TV Int32 ->
  MulticoreGen Imp.MCCode
compileSegScan :: Pat LetDecMem
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> TV Int32
-> MulticoreGen MCCode
compileSegScan Pat LetDecMem
pat SegSpace
space [SegBinOp MCMem]
reds KernelBody MCMem
kbody TV Int32
nsubtasks
  | [(VName, SubExp)
_] <- SegSpace -> [(VName, SubExp)]
unSegSpace SegSpace
space =
      Pat LetDecMem
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> TV Int32
-> MulticoreGen MCCode
nonsegmentedScan Pat LetDecMem
pat SegSpace
space [SegBinOp MCMem]
reds KernelBody MCMem
kbody TV Int32
nsubtasks
  | Bool
otherwise =
      Pat LetDecMem
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> MulticoreGen MCCode
segmentedScan Pat LetDecMem
pat SegSpace
space [SegBinOp MCMem]
reds KernelBody MCMem
kbody

xParams, yParams :: SegBinOp MCMem -> [LParam MCMem]
xParams :: SegBinOp MCMem -> [LParam MCMem]
xParams SegBinOp MCMem
scan =
  Int -> [Param LetDecMem] -> [Param LetDecMem]
forall a. Int -> [a] -> [a]
take ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan)) (Lambda MCMem -> [LParam MCMem]
forall rep. Lambda rep -> [LParam rep]
lambdaParams (SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp MCMem
scan))
yParams :: SegBinOp MCMem -> [LParam MCMem]
yParams SegBinOp MCMem
scan =
  Int -> [Param LetDecMem] -> [Param LetDecMem]
forall a. Int -> [a] -> [a]
drop ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan)) (Lambda MCMem -> [LParam MCMem]
forall rep. Lambda rep -> [LParam rep]
lambdaParams (SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp MCMem
scan))

lamBody :: SegBinOp MCMem -> Body MCMem
lamBody :: SegBinOp MCMem -> Body MCMem
lamBody = Lambda MCMem -> Body MCMem
forall rep. Lambda rep -> Body rep
lambdaBody (Lambda MCMem -> Body MCMem)
-> (SegBinOp MCMem -> Lambda MCMem) -> SegBinOp MCMem -> Body MCMem
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda

-- Arrays for storing worker results.
resultArrays :: String -> [SegBinOp MCMem] -> MulticoreGen [[VName]]
resultArrays :: String -> [SegBinOp MCMem] -> MulticoreGen [[VName]]
resultArrays String
s [SegBinOp MCMem]
segops =
  [SegBinOp MCMem]
-> (SegBinOp MCMem -> ImpM MCMem HostEnv Multicore [VName])
-> MulticoreGen [[VName]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [SegBinOp MCMem]
segops ((SegBinOp MCMem -> ImpM MCMem HostEnv Multicore [VName])
 -> MulticoreGen [[VName]])
-> (SegBinOp MCMem -> ImpM MCMem HostEnv Multicore [VName])
-> MulticoreGen [[VName]]
forall a b. (a -> b) -> a -> b
$ \(SegBinOp Commutativity
_ Lambda MCMem
lam [SubExp]
_ Shape
shape) ->
    [Type]
-> (Type -> ImpM MCMem HostEnv Multicore VName)
-> ImpM MCMem HostEnv Multicore [VName]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (Lambda MCMem -> [Type]
forall rep. Lambda rep -> [Type]
lambdaReturnType Lambda MCMem
lam) ((Type -> ImpM MCMem HostEnv Multicore VName)
 -> ImpM MCMem HostEnv Multicore [VName])
-> (Type -> ImpM MCMem HostEnv Multicore VName)
-> ImpM MCMem HostEnv Multicore [VName]
forall a b. (a -> b) -> a -> b
$ \Type
t -> do
      let pt :: PrimType
pt = Type -> PrimType
forall shape u. TypeBase shape u -> PrimType
elemType Type
t
          full_shape :: Shape
full_shape = Shape
shape Shape -> Shape -> Shape
forall a. Semigroup a => a -> a -> a
<> Type -> Shape
forall shape u. ArrayShape shape => TypeBase shape u -> shape
arrayShape Type
t
      String
-> PrimType -> Shape -> Space -> ImpM MCMem HostEnv Multicore VName
forall rep r op.
String -> PrimType -> Shape -> Space -> ImpM rep r op VName
sAllocArray String
s PrimType
pt Shape
full_shape Space
DefaultSpace

nonsegmentedScan ::
  Pat LetDecMem ->
  SegSpace ->
  [SegBinOp MCMem] ->
  KernelBody MCMem ->
  TV Int32 ->
  MulticoreGen Imp.MCCode
nonsegmentedScan :: Pat LetDecMem
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> TV Int32
-> MulticoreGen MCCode
nonsegmentedScan Pat LetDecMem
pat SegSpace
space [SegBinOp MCMem]
scan_ops KernelBody MCMem
kbody TV Int32
nsubtasks = do
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ String -> Maybe Exp -> MCCode
forall a. String -> Maybe Exp -> Code a
Imp.DebugPrint String
"nonsegmented segScan" Maybe Exp
forall a. Maybe a
Nothing
  ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
    -- Are we working with nested arrays
    let dims :: [[SubExp]]
dims = (SegBinOp MCMem -> [SubExp]) -> [SegBinOp MCMem] -> [[SubExp]]
forall a b. (a -> b) -> [a] -> [b]
map (Shape -> [SubExp]
forall d. ShapeBase d -> [d]
shapeDims (Shape -> [SubExp])
-> (SegBinOp MCMem -> Shape) -> SegBinOp MCMem -> [SubExp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegBinOp MCMem -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape) [SegBinOp MCMem]
scan_ops
    -- Are we only working on scalars
    let scalars :: Bool
scalars = (SegBinOp MCMem -> Bool) -> [SegBinOp MCMem] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Param LetDecMem -> Bool) -> [Param LetDecMem] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Type -> Bool
forall shape u. TypeBase shape u -> Bool
primType (Type -> Bool)
-> (Param LetDecMem -> Type) -> Param LetDecMem -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LetDecMem -> Type
forall t. Typed t => t -> Type
typeOf (LetDecMem -> Type)
-> (Param LetDecMem -> LetDecMem) -> Param LetDecMem -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Param LetDecMem -> LetDecMem
forall dec. Param dec -> dec
paramDec) ([Param LetDecMem] -> Bool)
-> (SegBinOp MCMem -> [Param LetDecMem]) -> SegBinOp MCMem -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Lambda MCMem -> [Param LetDecMem]
forall rep. Lambda rep -> [LParam rep]
lambdaParams (Lambda MCMem -> [Param LetDecMem])
-> (SegBinOp MCMem -> Lambda MCMem)
-> SegBinOp MCMem
-> [Param LetDecMem]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda)) [SegBinOp MCMem]
scan_ops Bool -> Bool -> Bool
&& ([SubExp] -> Bool) -> [[SubExp]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all [SubExp] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[SubExp]]
dims
    -- Do we have nested vector operations
    let vectorize :: Bool
vectorize = [] [SubExp] -> [[SubExp]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [[SubExp]]
dims

    let param_types :: [Type]
param_types = (SegBinOp MCMem -> [Type]) -> [SegBinOp MCMem] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((Param LetDecMem -> Type) -> [Param LetDecMem] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Param LetDecMem -> Type
forall dec. Typed dec => Param dec -> Type
paramType ([Param LetDecMem] -> [Type])
-> (SegBinOp MCMem -> [Param LetDecMem])
-> SegBinOp MCMem
-> [Type]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Lambda MCMem -> [Param LetDecMem]
forall rep. Lambda rep -> [LParam rep]
lambdaParams (Lambda MCMem -> [Param LetDecMem])
-> (SegBinOp MCMem -> Lambda MCMem)
-> SegBinOp MCMem
-> [Param LetDecMem]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda)) [SegBinOp MCMem]
scan_ops
    let no_array_param :: Bool
no_array_param = (Type -> Bool) -> [Type] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Type -> Bool
forall shape u. TypeBase shape u -> Bool
primType [Type]
param_types

    let (Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage1, Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage3)
          | Bool
scalars = (Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage1Scalar, Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage3Scalar)
          | Bool
vectorize Bool -> Bool -> Bool
&& Bool
no_array_param = (Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage1Nested, Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage3Nested)
          | Bool
otherwise = (Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage1Fallback, Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage3Fallback)

    Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage1 Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops

    let nsubtasks' :: TExp Int32
nsubtasks' = TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
nsubtasks
    TExp Bool
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. TExp Bool -> ImpM rep r op () -> ImpM rep r op ()
sWhen (TExp Int32
nsubtasks' TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.>. TExp Int32
1) (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ do
      [SegBinOp MCMem]
scan_ops2 <- [SegBinOp MCMem] -> MulticoreGen [SegBinOp MCMem]
renameSegBinOp [SegBinOp MCMem]
scan_ops
      Pat LetDecMem
-> TV Int32
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> ImpM MCMem HostEnv Multicore ()
scanStage2 Pat LetDecMem
pat TV Int32
nsubtasks SegSpace
space [SegBinOp MCMem]
scan_ops2 KernelBody MCMem
kbody
      [SegBinOp MCMem]
scan_ops3 <- [SegBinOp MCMem] -> MulticoreGen [SegBinOp MCMem]
renameSegBinOp [SegBinOp MCMem]
scan_ops
      Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage3 Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops3

-- Different ways to generate code for a scan loop
data ScanLoopType
  = ScanSeq -- Fully sequential
  | ScanNested -- Nested vectorized map
  | ScanScalar -- Vectorized scan over scalars

-- Given a scan type, return a function to inject into the loop body
getScanLoop ::
  ScanLoopType ->
  (Imp.TExp Int64 -> MulticoreGen ()) ->
  MulticoreGen ()
getScanLoop :: ScanLoopType
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
getScanLoop ScanLoopType
ScanScalar = (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
generateUniformizeLoop
getScanLoop ScanLoopType
_ = \TExp Int64 -> ImpM MCMem HostEnv Multicore ()
body -> TExp Int64 -> ImpM MCMem HostEnv Multicore ()
body TExp Int64
0

-- Given a scan type, return a function to extract a scalar from a vector
getExtract :: ScanLoopType -> Imp.TExp Int64 -> MulticoreGen Imp.MCCode -> MulticoreGen ()
getExtract :: ScanLoopType
-> TExp Int64
-> MulticoreGen MCCode
-> ImpM MCMem HostEnv Multicore ()
getExtract ScanLoopType
ScanSeq = \TExp Int64
_ MulticoreGen MCCode
body -> MulticoreGen MCCode
body MulticoreGen MCCode
-> (MCCode -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit
getExtract ScanLoopType
_ = TExp Int64
-> MulticoreGen MCCode -> ImpM MCMem HostEnv Multicore ()
extractVectorLane

genBinOpParams :: [SegBinOp MCMem] -> MulticoreGen ()
genBinOpParams :: [SegBinOp MCMem] -> ImpM MCMem HostEnv Multicore ()
genBinOpParams [SegBinOp MCMem]
scan_ops = Maybe (Exp MCMem) -> Scope MCMem -> ImpM MCMem HostEnv Multicore ()
forall rep inner r op.
Mem rep inner =>
Maybe (Exp rep) -> Scope rep -> ImpM rep r op ()
dScope Maybe (Exp MCMem)
forall a. Maybe a
Nothing (Scope MCMem -> ImpM MCMem HostEnv Multicore ())
-> Scope MCMem -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ [Param LetDecMem] -> Scope MCMem
forall rep dec. (LParamInfo rep ~ dec) => [Param dec] -> Scope rep
scopeOfLParams ([Param LetDecMem] -> Scope MCMem)
-> [Param LetDecMem] -> Scope MCMem
forall a b. (a -> b) -> a -> b
$ (SegBinOp MCMem -> [Param LetDecMem])
-> [SegBinOp MCMem] -> [Param LetDecMem]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Lambda MCMem -> [Param LetDecMem]
forall rep. Lambda rep -> [LParam rep]
lambdaParams (Lambda MCMem -> [Param LetDecMem])
-> (SegBinOp MCMem -> Lambda MCMem)
-> SegBinOp MCMem
-> [Param LetDecMem]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda) [SegBinOp MCMem]
scan_ops

genLocalAccsStage1 :: [SegBinOp MCMem] -> MulticoreGen [[VName]]
genLocalAccsStage1 :: [SegBinOp MCMem] -> MulticoreGen [[VName]]
genLocalAccsStage1 [SegBinOp MCMem]
scan_ops = do
  [SegBinOp MCMem]
-> (SegBinOp MCMem -> ImpM MCMem HostEnv Multicore [VName])
-> MulticoreGen [[VName]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [SegBinOp MCMem]
scan_ops ((SegBinOp MCMem -> ImpM MCMem HostEnv Multicore [VName])
 -> MulticoreGen [[VName]])
-> (SegBinOp MCMem -> ImpM MCMem HostEnv Multicore [VName])
-> MulticoreGen [[VName]]
forall a b. (a -> b) -> a -> b
$ \SegBinOp MCMem
scan_op -> do
    let shape :: Shape
shape = SegBinOp MCMem -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape SegBinOp MCMem
scan_op
        ts :: [Type]
ts = Lambda MCMem -> [Type]
forall rep. Lambda rep -> [Type]
lambdaReturnType (Lambda MCMem -> [Type]) -> Lambda MCMem -> [Type]
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp MCMem
scan_op
    [(Param LetDecMem, SubExp, Type)]
-> ((Param LetDecMem, SubExp, Type)
    -> ImpM MCMem HostEnv Multicore VName)
-> ImpM MCMem HostEnv Multicore [VName]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM ([Param LetDecMem]
-> [SubExp] -> [Type] -> [(Param LetDecMem, SubExp, Type)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 (SegBinOp MCMem -> [LParam MCMem]
xParams SegBinOp MCMem
scan_op) (SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan_op) [Type]
ts) (((Param LetDecMem, SubExp, Type)
  -> ImpM MCMem HostEnv Multicore VName)
 -> ImpM MCMem HostEnv Multicore [VName])
-> ((Param LetDecMem, SubExp, Type)
    -> ImpM MCMem HostEnv Multicore VName)
-> ImpM MCMem HostEnv Multicore [VName]
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, SubExp
ne, Type
t) -> do
      VName
acc <- -- update accumulator to have type decoration
        case Shape -> [SubExp]
forall d. ShapeBase d -> [d]
shapeDims Shape
shape of
          [] -> VName -> ImpM MCMem HostEnv Multicore VName
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VName -> ImpM MCMem HostEnv Multicore VName)
-> VName -> ImpM MCMem HostEnv Multicore VName
forall a b. (a -> b) -> a -> b
$ Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p
          [SubExp]
_ -> do
            let pt :: PrimType
pt = Type -> PrimType
forall shape u. TypeBase shape u -> PrimType
elemType Type
t
            String
-> PrimType -> Shape -> Space -> ImpM MCMem HostEnv Multicore VName
forall rep r op.
String -> PrimType -> Shape -> Space -> ImpM rep r op VName
sAllocArray String
"local_acc" PrimType
pt (Shape
shape Shape -> Shape -> Shape
forall a. Semigroup a => a -> a -> a
<> Type -> Shape
forall shape u. ArrayShape shape => TypeBase shape u -> shape
arrayShape Type
t) Space
DefaultSpace

      -- Now neutral-initialise the accumulator.
      Shape
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Shape -> ([TExp Int64] -> ImpM rep r op ()) -> ImpM rep r op ()
sLoopNest (SegBinOp MCMem -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape SegBinOp MCMem
scan_op) (([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \[TExp Int64]
vec_is ->
        VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix VName
acc [TExp Int64]
vec_is SubExp
ne []

      VName -> ImpM MCMem HostEnv Multicore VName
forall (f :: * -> *) a. Applicative f => a -> f a
pure VName
acc

getNestLoop ::
  ScanLoopType ->
  Shape ->
  ([Imp.TExp Int64] -> MulticoreGen ()) ->
  MulticoreGen ()
getNestLoop :: ScanLoopType
-> Shape
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
getNestLoop ScanLoopType
ScanNested = Shape
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
sLoopNestVectorized
getNestLoop ScanLoopType
_ = Shape
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Shape -> ([TExp Int64] -> ImpM rep r op ()) -> ImpM rep r op ()
sLoopNest

-- Generate a loop which performs a potentially vectorized scan.
genScanLoop ::
  ScanLoopType ->
  Pat LetDecMem ->
  SegSpace ->
  KernelBody MCMem ->
  [SegBinOp MCMem] ->
  [[VName]] ->
  Imp.TExp Int64 ->
  ImpM MCMem HostEnv Imp.Multicore ()
genScanLoop :: ScanLoopType
-> Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> [[VName]]
-> TExp Int64
-> ImpM MCMem HostEnv Multicore ()
genScanLoop ScanLoopType
typ Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops [[VName]]
local_accs TExp Int64
i = do
  let ([KernelResult]
all_scan_res, [KernelResult]
map_res) = Int -> [KernelResult] -> ([KernelResult], [KernelResult])
forall a. Int -> [a] -> ([a], [a])
splitAt ([SegBinOp MCMem] -> Int
forall rep. [SegBinOp rep] -> Int
segBinOpResults [SegBinOp MCMem]
scan_ops) ([KernelResult] -> ([KernelResult], [KernelResult]))
-> [KernelResult] -> ([KernelResult], [KernelResult])
forall a b. (a -> b) -> a -> b
$ KernelBody MCMem -> [KernelResult]
forall rep. KernelBody rep -> [KernelResult]
kernelBodyResult KernelBody MCMem
kbody
      per_scan_res :: [[KernelResult]]
per_scan_res = [SegBinOp MCMem] -> [KernelResult] -> [[KernelResult]]
forall rep a. [SegBinOp rep] -> [a] -> [[a]]
segBinOpChunks [SegBinOp MCMem]
scan_ops [KernelResult]
all_scan_res
      per_scan_pes :: [[PatElem LetDecMem]]
per_scan_pes = [SegBinOp MCMem] -> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall rep a. [SegBinOp rep] -> [a] -> [[a]]
segBinOpChunks [SegBinOp MCMem]
scan_ops ([PatElem LetDecMem] -> [[PatElem LetDecMem]])
-> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall a b. (a -> b) -> a -> b
$ Pat LetDecMem -> [PatElem LetDecMem]
forall dec. Pat dec -> [PatElem dec]
patElems Pat LetDecMem
pat
  let ([VName]
is, [SubExp]
ns) = [(VName, SubExp)] -> ([VName], [SubExp])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(VName, SubExp)] -> ([VName], [SubExp]))
-> [(VName, SubExp)] -> ([VName], [SubExp])
forall a b. (a -> b) -> a -> b
$ SegSpace -> [(VName, SubExp)]
unSegSpace SegSpace
space
      ns' :: [TExp Int64]
ns' = (SubExp -> TExp Int64) -> [SubExp] -> [TExp Int64]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TExp Int64
forall a. ToExp a => a -> TExp Int64
toInt64Exp [SubExp]
ns

  (VName -> TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> [VName] -> [TExp Int64] -> ImpM MCMem HostEnv Multicore ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName -> TExp Int64 -> ImpM MCMem HostEnv Multicore ()
forall t rep r op. VName -> TExp t -> ImpM rep r op ()
dPrimV_ [VName]
is ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> [TExp Int64] -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ [TExp Int64] -> TExp Int64 -> [TExp Int64]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TExp Int64]
ns' TExp Int64
i
  Names
-> Stms MCMem
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op ()
compileStms Names
forall a. Monoid a => a
mempty (KernelBody MCMem -> Stms MCMem
forall rep. KernelBody rep -> Stms rep
kernelBodyStms KernelBody MCMem
kbody) (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ do
    -- Potential vector load and then do sequential scan
    ScanLoopType
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
getScanLoop ScanLoopType
typ ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \TExp Int64
j -> do
      String
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. String -> ImpM rep r op () -> ImpM rep r op ()
sComment String
"write mapped values results to memory" (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ do
        let map_arrs :: [PatElem LetDecMem]
map_arrs = Int -> [PatElem LetDecMem] -> [PatElem LetDecMem]
forall a. Int -> [a] -> [a]
drop ([SegBinOp MCMem] -> Int
forall rep. [SegBinOp rep] -> Int
segBinOpResults [SegBinOp MCMem]
scan_ops) ([PatElem LetDecMem] -> [PatElem LetDecMem])
-> [PatElem LetDecMem] -> [PatElem LetDecMem]
forall a b. (a -> b) -> a -> b
$ Pat LetDecMem -> [PatElem LetDecMem]
forall dec. Pat dec -> [PatElem dec]
patElems Pat LetDecMem
pat
        (PatElem LetDecMem
 -> KernelResult -> ImpM MCMem HostEnv Multicore ())
-> [PatElem LetDecMem]
-> [KernelResult]
-> ImpM MCMem HostEnv Multicore ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ (SegSpace
-> PatElem LetDecMem
-> KernelResult
-> ImpM MCMem HostEnv Multicore ()
compileThreadResult SegSpace
space) [PatElem LetDecMem]
map_arrs [KernelResult]
map_res
      [([PatElem LetDecMem], SegBinOp MCMem, [KernelResult], [VName])]
-> (([PatElem LetDecMem], SegBinOp MCMem, [KernelResult], [VName])
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([[PatElem LetDecMem]]
-> [SegBinOp MCMem]
-> [[KernelResult]]
-> [[VName]]
-> [([PatElem LetDecMem], SegBinOp MCMem, [KernelResult], [VName])]
forall a b c d. [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)]
zip4 [[PatElem LetDecMem]]
per_scan_pes [SegBinOp MCMem]
scan_ops [[KernelResult]]
per_scan_res [[VName]]
local_accs) ((([PatElem LetDecMem], SegBinOp MCMem, [KernelResult], [VName])
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (([PatElem LetDecMem], SegBinOp MCMem, [KernelResult], [VName])
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \([PatElem LetDecMem]
pes, SegBinOp MCMem
scan_op, [KernelResult]
scan_res, [VName]
acc) ->
        ScanLoopType
-> Shape
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
getNestLoop ScanLoopType
typ (SegBinOp MCMem -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape SegBinOp MCMem
scan_op) (([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \[TExp Int64]
vec_is -> do
          -- Read accum value
          [(Param LetDecMem, VName)]
-> ((Param LetDecMem, VName) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param LetDecMem] -> [VName] -> [(Param LetDecMem, VName)]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp MCMem -> [LParam MCMem]
xParams SegBinOp MCMem
scan_op) [VName]
acc) (((Param LetDecMem, VName) -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((Param LetDecMem, VName) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, VName
acc') -> do
            VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p) [] (VName -> SubExp
Var VName
acc') [TExp Int64]
vec_is
          -- Read next value
          String
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. String -> ImpM rep r op () -> ImpM rep r op ()
sComment String
"Read next values" (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
            [(Param LetDecMem, KernelResult)]
-> ((Param LetDecMem, KernelResult)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param LetDecMem]
-> [KernelResult] -> [(Param LetDecMem, KernelResult)]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp MCMem -> [LParam MCMem]
yParams SegBinOp MCMem
scan_op) [KernelResult]
scan_res) (((Param LetDecMem, KernelResult)
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((Param LetDecMem, KernelResult)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, KernelResult
se) ->
              ScanLoopType
-> TExp Int64
-> MulticoreGen MCCode
-> ImpM MCMem HostEnv Multicore ()
getExtract ScanLoopType
typ TExp Int64
j (MulticoreGen MCCode -> ImpM MCMem HostEnv Multicore ())
-> MulticoreGen MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
                ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$
                  VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p) [] (KernelResult -> SubExp
kernelResultSubExp KernelResult
se) [TExp Int64]
vec_is
          -- Scan body
          String
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. String -> ImpM rep r op () -> ImpM rep r op ()
sComment String
"Scan body" (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
            Names
-> Stms MCMem
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op ()
compileStms Names
forall a. Monoid a => a
mempty (Body MCMem -> Stms MCMem
forall rep. Body rep -> Stms rep
bodyStms (Body MCMem -> Stms MCMem) -> Body MCMem -> Stms MCMem
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Body MCMem
lamBody SegBinOp MCMem
scan_op) (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
              [(VName, PatElem LetDecMem, SubExp)]
-> ((VName, PatElem LetDecMem, SubExp)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([VName]
-> [PatElem LetDecMem]
-> [SubExp]
-> [(VName, PatElem LetDecMem, SubExp)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [VName]
acc [PatElem LetDecMem]
pes ([SubExp] -> [(VName, PatElem LetDecMem, SubExp)])
-> [SubExp] -> [(VName, PatElem LetDecMem, SubExp)]
forall a b. (a -> b) -> a -> b
$ (SubExpRes -> SubExp) -> [SubExpRes] -> [SubExp]
forall a b. (a -> b) -> [a] -> [b]
map SubExpRes -> SubExp
resSubExp ([SubExpRes] -> [SubExp]) -> [SubExpRes] -> [SubExp]
forall a b. (a -> b) -> a -> b
$ Body MCMem -> [SubExpRes]
forall rep. Body rep -> [SubExpRes]
bodyResult (Body MCMem -> [SubExpRes]) -> Body MCMem -> [SubExpRes]
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Body MCMem
lamBody SegBinOp MCMem
scan_op) (((VName, PatElem LetDecMem, SubExp)
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((VName, PatElem LetDecMem, SubExp)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
                \(VName
acc', PatElem LetDecMem
pe, SubExp
se) -> do
                  VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (PatElem LetDecMem -> VName
forall dec. PatElem dec -> VName
patElemName PatElem LetDecMem
pe) ((VName -> TExp Int64) -> [VName] -> [TExp Int64]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int64
forall a. a -> TPrimExp Int64 a
Imp.le64 [VName]
is [TExp Int64] -> [TExp Int64] -> [TExp Int64]
forall a. [a] -> [a] -> [a]
++ [TExp Int64]
vec_is) SubExp
se []
                  VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix VName
acc' [TExp Int64]
vec_is SubExp
se []

scanStage1Scalar ::
  Pat LetDecMem ->
  SegSpace ->
  KernelBody MCMem ->
  [SegBinOp MCMem] ->
  MulticoreGen ()
scanStage1Scalar :: Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage1Scalar Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops = do
  MCCode
fbody <- ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
    VName -> PrimType -> ImpM MCMem HostEnv Multicore ()
forall rep r op. VName -> PrimType -> ImpM rep r op ()
dPrim_ (SegSpace -> VName
segFlat SegSpace
space) PrimType
int64
    Multicore -> ImpM MCMem HostEnv Multicore ()
forall op rep r. op -> ImpM rep r op ()
sOp (Multicore -> ImpM MCMem HostEnv Multicore ())
-> Multicore -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ VName -> Multicore
Imp.GetTaskId (SegSpace -> VName
segFlat SegSpace
space)

    [SegBinOp MCMem] -> ImpM MCMem HostEnv Multicore ()
genBinOpParams [SegBinOp MCMem]
scan_ops
    [[VName]]
local_accs <- [SegBinOp MCMem] -> MulticoreGen [[VName]]
genLocalAccsStage1 [SegBinOp MCMem]
scan_ops
    ImpM MCMem HostEnv Multicore () -> ImpM MCMem HostEnv Multicore ()
inISPC (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
      String
-> ChunkLoopVectorization
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
generateChunkLoop String
"SegScan" ChunkLoopVectorization
Vectorized ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
        ScanLoopType
-> Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> [[VName]]
-> TExp Int64
-> ImpM MCMem HostEnv Multicore ()
genScanLoop ScanLoopType
ScanScalar Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops [[VName]]
local_accs
  [Param]
free_params <- MCCode -> MulticoreGen [Param]
forall a. FreeIn a => a -> MulticoreGen [Param]
freeParams MCCode
fbody
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ Multicore -> MCCode
forall a. a -> Code a
Imp.Op (Multicore -> MCCode) -> Multicore -> MCCode
forall a b. (a -> b) -> a -> b
$ String -> MCCode -> [Param] -> Multicore
Imp.ParLoop String
"scan_stage_1" MCCode
fbody [Param]
free_params

scanStage1Nested ::
  Pat LetDecMem ->
  SegSpace ->
  KernelBody MCMem ->
  [SegBinOp MCMem] ->
  MulticoreGen ()
scanStage1Nested :: Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage1Nested Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops = do
  MCCode
fbody <- ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
    VName -> PrimType -> ImpM MCMem HostEnv Multicore ()
forall rep r op. VName -> PrimType -> ImpM rep r op ()
dPrim_ (SegSpace -> VName
segFlat SegSpace
space) PrimType
int64
    Multicore -> ImpM MCMem HostEnv Multicore ()
forall op rep r. op -> ImpM rep r op ()
sOp (Multicore -> ImpM MCMem HostEnv Multicore ())
-> Multicore -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ VName -> Multicore
Imp.GetTaskId (SegSpace -> VName
segFlat SegSpace
space)

    MCCode
lparams <- ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ [SegBinOp MCMem] -> ImpM MCMem HostEnv Multicore ()
genBinOpParams [SegBinOp MCMem]
scan_ops
    [[VName]]
local_accs <- [SegBinOp MCMem] -> MulticoreGen [[VName]]
genLocalAccsStage1 [SegBinOp MCMem]
scan_ops

    ImpM MCMem HostEnv Multicore () -> ImpM MCMem HostEnv Multicore ()
inISPC (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ do
      MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit MCCode
lparams
      String
-> ChunkLoopVectorization
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
generateChunkLoop String
"SegScan" ChunkLoopVectorization
Scalar ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \TExp Int64
i -> do
        ScanLoopType
-> Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> [[VName]]
-> TExp Int64
-> ImpM MCMem HostEnv Multicore ()
genScanLoop ScanLoopType
ScanNested Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops [[VName]]
local_accs TExp Int64
i

  [Param]
free_params <- MCCode -> MulticoreGen [Param]
forall a. FreeIn a => a -> MulticoreGen [Param]
freeParams MCCode
fbody
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ Multicore -> MCCode
forall a. a -> Code a
Imp.Op (Multicore -> MCCode) -> Multicore -> MCCode
forall a b. (a -> b) -> a -> b
$ String -> MCCode -> [Param] -> Multicore
Imp.ParLoop String
"scan_stage_1" MCCode
fbody [Param]
free_params

scanStage1Fallback ::
  Pat LetDecMem ->
  SegSpace ->
  KernelBody MCMem ->
  [SegBinOp MCMem] ->
  MulticoreGen ()
scanStage1Fallback :: Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage1Fallback Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops = do
  -- Stage 1 : each thread partially scans a chunk of the input
  -- Writes directly to the resulting array
  MCCode
fbody <- ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
    VName -> PrimType -> ImpM MCMem HostEnv Multicore ()
forall rep r op. VName -> PrimType -> ImpM rep r op ()
dPrim_ (SegSpace -> VName
segFlat SegSpace
space) PrimType
int64
    Multicore -> ImpM MCMem HostEnv Multicore ()
forall op rep r. op -> ImpM rep r op ()
sOp (Multicore -> ImpM MCMem HostEnv Multicore ())
-> Multicore -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ VName -> Multicore
Imp.GetTaskId (SegSpace -> VName
segFlat SegSpace
space)

    [SegBinOp MCMem] -> ImpM MCMem HostEnv Multicore ()
genBinOpParams [SegBinOp MCMem]
scan_ops
    [[VName]]
local_accs <- [SegBinOp MCMem] -> MulticoreGen [[VName]]
genLocalAccsStage1 [SegBinOp MCMem]
scan_ops

    String
-> ChunkLoopVectorization
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
generateChunkLoop String
"SegScan" ChunkLoopVectorization
Scalar ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
      ScanLoopType
-> Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> [[VName]]
-> TExp Int64
-> ImpM MCMem HostEnv Multicore ()
genScanLoop ScanLoopType
ScanSeq Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops [[VName]]
local_accs
  [Param]
free_params <- MCCode -> MulticoreGen [Param]
forall a. FreeIn a => a -> MulticoreGen [Param]
freeParams MCCode
fbody
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ Multicore -> MCCode
forall a. a -> Code a
Imp.Op (Multicore -> MCCode) -> Multicore -> MCCode
forall a b. (a -> b) -> a -> b
$ String -> MCCode -> [Param] -> Multicore
Imp.ParLoop String
"scan_stage_1" MCCode
fbody [Param]
free_params

scanStage2 ::
  Pat LetDecMem ->
  TV Int32 ->
  SegSpace ->
  [SegBinOp MCMem] ->
  KernelBody MCMem ->
  MulticoreGen ()
scanStage2 :: Pat LetDecMem
-> TV Int32
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> ImpM MCMem HostEnv Multicore ()
scanStage2 Pat LetDecMem
pat TV Int32
nsubtasks SegSpace
space [SegBinOp MCMem]
scan_ops KernelBody MCMem
kbody = do
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ String -> Maybe Exp -> MCCode
forall a. String -> Maybe Exp -> Code a
Imp.DebugPrint String
"nonsegmentedScan stage 2" Maybe Exp
forall a. Maybe a
Nothing
  let ([VName]
is, [SubExp]
ns) = [(VName, SubExp)] -> ([VName], [SubExp])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(VName, SubExp)] -> ([VName], [SubExp]))
-> [(VName, SubExp)] -> ([VName], [SubExp])
forall a b. (a -> b) -> a -> b
$ SegSpace -> [(VName, SubExp)]
unSegSpace SegSpace
space
      ns_64 :: [TExp Int64]
ns_64 = (SubExp -> TExp Int64) -> [SubExp] -> [TExp Int64]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TExp Int64
forall a. ToExp a => a -> TExp Int64
toInt64Exp [SubExp]
ns
      per_scan_pes :: [[PatElem LetDecMem]]
per_scan_pes = [SegBinOp MCMem] -> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall rep a. [SegBinOp rep] -> [a] -> [[a]]
segBinOpChunks [SegBinOp MCMem]
scan_ops ([PatElem LetDecMem] -> [[PatElem LetDecMem]])
-> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall a b. (a -> b) -> a -> b
$ Pat LetDecMem -> [PatElem LetDecMem]
forall dec. Pat dec -> [PatElem dec]
patElems Pat LetDecMem
pat
      nsubtasks' :: TExp Int32
nsubtasks' = TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
nsubtasks

  Maybe (Exp MCMem) -> Scope MCMem -> ImpM MCMem HostEnv Multicore ()
forall rep inner r op.
Mem rep inner =>
Maybe (Exp rep) -> Scope rep -> ImpM rep r op ()
dScope Maybe (Exp MCMem)
forall a. Maybe a
Nothing (Scope MCMem -> ImpM MCMem HostEnv Multicore ())
-> Scope MCMem -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ [Param LetDecMem] -> Scope MCMem
forall rep dec. (LParamInfo rep ~ dec) => [Param dec] -> Scope rep
scopeOfLParams ([Param LetDecMem] -> Scope MCMem)
-> [Param LetDecMem] -> Scope MCMem
forall a b. (a -> b) -> a -> b
$ (SegBinOp MCMem -> [Param LetDecMem])
-> [SegBinOp MCMem] -> [Param LetDecMem]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Lambda MCMem -> [Param LetDecMem]
forall rep. Lambda rep -> [LParam rep]
lambdaParams (Lambda MCMem -> [Param LetDecMem])
-> (SegBinOp MCMem -> Lambda MCMem)
-> SegBinOp MCMem
-> [Param LetDecMem]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda) [SegBinOp MCMem]
scan_ops
  TV Int64
offset <- String -> TExp Int64 -> ImpM MCMem HostEnv Multicore (TV Int64)
forall t rep r op. String -> TExp t -> ImpM rep r op (TV t)
dPrimV String
"offset" (TExp Int64
0 :: Imp.TExp Int64)
  let offset' :: TExp Int64
offset' = TV Int64 -> TExp Int64
forall t. TV t -> TExp t
tvExp TV Int64
offset
  TV Int64
offset_index <- String -> TExp Int64 -> ImpM MCMem HostEnv Multicore (TV Int64)
forall t rep r op. String -> TExp t -> ImpM rep r op (TV t)
dPrimV String
"offset_index" (TExp Int64
0 :: Imp.TExp Int64)
  let offset_index' :: TExp Int64
offset_index' = TV Int64 -> TExp Int64
forall t. TV t -> TExp t
tvExp TV Int64
offset_index

  -- Parameters used to find the chunk sizes
  -- Perhaps get this information from ``scheduling information``
  -- instead of computing it manually here.
  let iter_pr_subtask :: TExp Int64
iter_pr_subtask = [TExp Int64] -> TExp Int64
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [TExp Int64]
ns_64 TExp Int64 -> TExp Int64 -> TExp Int64
forall e. IntegralExp e => e -> e -> e
`quot` TExp Int32 -> TExp Int64
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TExp Int32
nsubtasks'
      remainder :: TExp Int64
remainder = [TExp Int64] -> TExp Int64
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [TExp Int64]
ns_64 TExp Int64 -> TExp Int64 -> TExp Int64
forall e. IntegralExp e => e -> e -> e
`rem` TExp Int32 -> TExp Int64
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TExp Int32
nsubtasks'

  [[VName]]
accs <- String -> [SegBinOp MCMem] -> MulticoreGen [[VName]]
resultArrays String
"scan_stage_2_accum" [SegBinOp MCMem]
scan_ops
  [(SegBinOp MCMem, [VName])]
-> ((SegBinOp MCMem, [VName]) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([SegBinOp MCMem] -> [[VName]] -> [(SegBinOp MCMem, [VName])]
forall a b. [a] -> [b] -> [(a, b)]
zip [SegBinOp MCMem]
scan_ops [[VName]]
accs) (((SegBinOp MCMem, [VName]) -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((SegBinOp MCMem, [VName]) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(SegBinOp MCMem
scan_op, [VName]
acc) ->
    Shape
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Shape -> ([TExp Int64] -> ImpM rep r op ()) -> ImpM rep r op ()
sLoopNest (SegBinOp MCMem -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape SegBinOp MCMem
scan_op) (([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \[TExp Int64]
vec_is ->
      [(VName, SubExp)]
-> ((VName, SubExp) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([VName] -> [SubExp] -> [(VName, SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip [VName]
acc ([SubExp] -> [(VName, SubExp)]) -> [SubExp] -> [(VName, SubExp)]
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan_op) (((VName, SubExp) -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((VName, SubExp) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(VName
acc', SubExp
ne) ->
        VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix VName
acc' [TExp Int64]
vec_is SubExp
ne []

  -- Perform sequential scan over the last element of each chunk
  String
-> TExp Int32
-> (TExp Int32 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall t rep r op.
String
-> TExp t -> (TExp t -> ImpM rep r op ()) -> ImpM rep r op ()
sFor String
"i" (TExp Int32
nsubtasks' TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1) ((TExp Int32 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int32 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \TExp Int32
i -> do
    TV Int64
offset TV Int64 -> TExp Int64 -> ImpM MCMem HostEnv Multicore ()
forall t rep r op. TV t -> TExp t -> ImpM rep r op ()
<-- TExp Int64
iter_pr_subtask
    TExp Bool
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. TExp Bool -> ImpM rep r op () -> ImpM rep r op ()
sWhen (TExp Int32 -> TExp Int64
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TExp Int32
i TExp Int64 -> TExp Int64 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.<. TExp Int64
remainder) (TV Int64
offset TV Int64 -> TExp Int64 -> ImpM MCMem HostEnv Multicore ()
forall t rep r op. TV t -> TExp t -> ImpM rep r op ()
<-- TExp Int64
offset' TExp Int64 -> TExp Int64 -> TExp Int64
forall a. Num a => a -> a -> a
+ TExp Int64
1)
    TV Int64
offset_index TV Int64 -> TExp Int64 -> ImpM MCMem HostEnv Multicore ()
forall t rep r op. TV t -> TExp t -> ImpM rep r op ()
<-- TExp Int64
offset_index' TExp Int64 -> TExp Int64 -> TExp Int64
forall a. Num a => a -> a -> a
+ TExp Int64
offset'
    (VName -> TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> [VName] -> [TExp Int64] -> ImpM MCMem HostEnv Multicore ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName -> TExp Int64 -> ImpM MCMem HostEnv Multicore ()
forall t rep r op. VName -> TExp t -> ImpM rep r op ()
dPrimV_ [VName]
is ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> [TExp Int64] -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ [TExp Int64] -> TExp Int64 -> [TExp Int64]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TExp Int64]
ns_64 (TExp Int64 -> [TExp Int64]) -> TExp Int64 -> [TExp Int64]
forall a b. (a -> b) -> a -> b
$ TExp Int64 -> TExp Int64
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TExp Int64
offset_index'

    Names
-> Stms MCMem
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op ()
compileStms Names
forall a. Monoid a => a
mempty (KernelBody MCMem -> Stms MCMem
forall rep. KernelBody rep -> Stms rep
kernelBodyStms KernelBody MCMem
kbody) (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
      [([PatElem LetDecMem], SegBinOp MCMem, [VName])]
-> (([PatElem LetDecMem], SegBinOp MCMem, [VName])
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([[PatElem LetDecMem]]
-> [SegBinOp MCMem]
-> [[VName]]
-> [([PatElem LetDecMem], SegBinOp MCMem, [VName])]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [[PatElem LetDecMem]]
per_scan_pes [SegBinOp MCMem]
scan_ops [[VName]]
accs) ((([PatElem LetDecMem], SegBinOp MCMem, [VName])
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (([PatElem LetDecMem], SegBinOp MCMem, [VName])
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \([PatElem LetDecMem]
pes, SegBinOp MCMem
scan_op, [VName]
acc) ->
        Shape
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Shape -> ([TExp Int64] -> ImpM rep r op ()) -> ImpM rep r op ()
sLoopNest (SegBinOp MCMem -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape SegBinOp MCMem
scan_op) (([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \[TExp Int64]
vec_is -> do
          String
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. String -> ImpM rep r op () -> ImpM rep r op ()
sComment String
"Read carry in" (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
            [(Param LetDecMem, VName)]
-> ((Param LetDecMem, VName) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param LetDecMem] -> [VName] -> [(Param LetDecMem, VName)]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp MCMem -> [LParam MCMem]
xParams SegBinOp MCMem
scan_op) [VName]
acc) (((Param LetDecMem, VName) -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((Param LetDecMem, VName) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, VName
acc') ->
              VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p) [] (VName -> SubExp
Var VName
acc') [TExp Int64]
vec_is

          String
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. String -> ImpM rep r op () -> ImpM rep r op ()
sComment String
"Read next values" (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
            [(Param LetDecMem, PatElem LetDecMem)]
-> ((Param LetDecMem, PatElem LetDecMem)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param LetDecMem]
-> [PatElem LetDecMem] -> [(Param LetDecMem, PatElem LetDecMem)]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp MCMem -> [LParam MCMem]
yParams SegBinOp MCMem
scan_op) [PatElem LetDecMem]
pes) (((Param LetDecMem, PatElem LetDecMem)
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((Param LetDecMem, PatElem LetDecMem)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, PatElem LetDecMem
pe) ->
              VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p) [] (VName -> SubExp
Var (VName -> SubExp) -> VName -> SubExp
forall a b. (a -> b) -> a -> b
$ PatElem LetDecMem -> VName
forall dec. PatElem dec -> VName
patElemName PatElem LetDecMem
pe) ((TExp Int64
offset_index' TExp Int64 -> TExp Int64 -> TExp Int64
forall a. Num a => a -> a -> a
- TExp Int64
1) TExp Int64 -> [TExp Int64] -> [TExp Int64]
forall a. a -> [a] -> [a]
: [TExp Int64]
vec_is)

          Names
-> Stms MCMem
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op ()
compileStms Names
forall a. Monoid a => a
mempty (Body MCMem -> Stms MCMem
forall rep. Body rep -> Stms rep
bodyStms (Body MCMem -> Stms MCMem) -> Body MCMem -> Stms MCMem
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Body MCMem
lamBody SegBinOp MCMem
scan_op) (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
            [(VName, PatElem LetDecMem, SubExp)]
-> ((VName, PatElem LetDecMem, SubExp)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([VName]
-> [PatElem LetDecMem]
-> [SubExp]
-> [(VName, PatElem LetDecMem, SubExp)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [VName]
acc [PatElem LetDecMem]
pes ([SubExp] -> [(VName, PatElem LetDecMem, SubExp)])
-> [SubExp] -> [(VName, PatElem LetDecMem, SubExp)]
forall a b. (a -> b) -> a -> b
$ (SubExpRes -> SubExp) -> [SubExpRes] -> [SubExp]
forall a b. (a -> b) -> [a] -> [b]
map SubExpRes -> SubExp
resSubExp ([SubExpRes] -> [SubExp]) -> [SubExpRes] -> [SubExp]
forall a b. (a -> b) -> a -> b
$ Body MCMem -> [SubExpRes]
forall rep. Body rep -> [SubExpRes]
bodyResult (Body MCMem -> [SubExpRes]) -> Body MCMem -> [SubExpRes]
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Body MCMem
lamBody SegBinOp MCMem
scan_op) (((VName, PatElem LetDecMem, SubExp)
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((VName, PatElem LetDecMem, SubExp)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
              \(VName
acc', PatElem LetDecMem
pe, SubExp
se) -> do
                VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (PatElem LetDecMem -> VName
forall dec. PatElem dec -> VName
patElemName PatElem LetDecMem
pe) ((TExp Int64
offset_index' TExp Int64 -> TExp Int64 -> TExp Int64
forall a. Num a => a -> a -> a
- TExp Int64
1) TExp Int64 -> [TExp Int64] -> [TExp Int64]
forall a. a -> [a] -> [a]
: [TExp Int64]
vec_is) SubExp
se []
                VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix VName
acc' [TExp Int64]
vec_is SubExp
se []

genLocalAccsStage3 :: [SegBinOp MCMem] -> [[PatElem LetDecMem]] -> MulticoreGen [[VName]]
genLocalAccsStage3 :: [SegBinOp MCMem] -> [[PatElem LetDecMem]] -> MulticoreGen [[VName]]
genLocalAccsStage3 [SegBinOp MCMem]
scan_ops [[PatElem LetDecMem]]
per_scan_pes =
  [(SegBinOp MCMem, [PatElem LetDecMem])]
-> ((SegBinOp MCMem, [PatElem LetDecMem])
    -> ImpM MCMem HostEnv Multicore [VName])
-> MulticoreGen [[VName]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM ([SegBinOp MCMem]
-> [[PatElem LetDecMem]] -> [(SegBinOp MCMem, [PatElem LetDecMem])]
forall a b. [a] -> [b] -> [(a, b)]
zip [SegBinOp MCMem]
scan_ops [[PatElem LetDecMem]]
per_scan_pes) (((SegBinOp MCMem, [PatElem LetDecMem])
  -> ImpM MCMem HostEnv Multicore [VName])
 -> MulticoreGen [[VName]])
-> ((SegBinOp MCMem, [PatElem LetDecMem])
    -> ImpM MCMem HostEnv Multicore [VName])
-> MulticoreGen [[VName]]
forall a b. (a -> b) -> a -> b
$ \(SegBinOp MCMem
scan_op, [PatElem LetDecMem]
pes) -> do
    let shape :: Shape
shape = SegBinOp MCMem -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape SegBinOp MCMem
scan_op
        ts :: [Type]
ts = Lambda MCMem -> [Type]
forall rep. Lambda rep -> [Type]
lambdaReturnType (Lambda MCMem -> [Type]) -> Lambda MCMem -> [Type]
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp MCMem
scan_op
    [(Param LetDecMem, PatElem LetDecMem, Type, SubExp)]
-> ((Param LetDecMem, PatElem LetDecMem, Type, SubExp)
    -> ImpM MCMem HostEnv Multicore VName)
-> ImpM MCMem HostEnv Multicore [VName]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM ([Param LetDecMem]
-> [PatElem LetDecMem]
-> [Type]
-> [SubExp]
-> [(Param LetDecMem, PatElem LetDecMem, Type, SubExp)]
forall a b c d. [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)]
zip4 (SegBinOp MCMem -> [LParam MCMem]
xParams SegBinOp MCMem
scan_op) [PatElem LetDecMem]
pes [Type]
ts ([SubExp] -> [(Param LetDecMem, PatElem LetDecMem, Type, SubExp)])
-> [SubExp] -> [(Param LetDecMem, PatElem LetDecMem, Type, SubExp)]
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan_op) (((Param LetDecMem, PatElem LetDecMem, Type, SubExp)
  -> ImpM MCMem HostEnv Multicore VName)
 -> ImpM MCMem HostEnv Multicore [VName])
-> ((Param LetDecMem, PatElem LetDecMem, Type, SubExp)
    -> ImpM MCMem HostEnv Multicore VName)
-> ImpM MCMem HostEnv Multicore [VName]
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, PatElem LetDecMem
pe, Type
t, SubExp
ne) -> do
      VName
acc <-
        case Shape -> [SubExp]
forall d. ShapeBase d -> [d]
shapeDims Shape
shape of
          [] -> VName -> ImpM MCMem HostEnv Multicore VName
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VName -> ImpM MCMem HostEnv Multicore VName)
-> VName -> ImpM MCMem HostEnv Multicore VName
forall a b. (a -> b) -> a -> b
$ Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p
          [SubExp]
_ -> do
            let pt :: PrimType
pt = Type -> PrimType
forall shape u. TypeBase shape u -> PrimType
elemType Type
t
            String
-> PrimType -> Shape -> Space -> ImpM MCMem HostEnv Multicore VName
forall rep r op.
String -> PrimType -> Shape -> Space -> ImpM rep r op VName
sAllocArray String
"local_acc" PrimType
pt (Shape
shape Shape -> Shape -> Shape
forall a. Semigroup a => a -> a -> a
<> Type -> Shape
forall shape u. ArrayShape shape => TypeBase shape u -> shape
arrayShape Type
t) Space
DefaultSpace

      -- Initialise the accumulator with neutral from previous chunk.
      -- or read neutral if first ``iter``
      (TExp Int64
start, TExp Int64
_end) <- MulticoreGen (TExp Int64, TExp Int64)
getLoopBounds
      Shape
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Shape -> ([TExp Int64] -> ImpM rep r op ()) -> ImpM rep r op ()
sLoopNest (SegBinOp MCMem -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape SegBinOp MCMem
scan_op) (([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \[TExp Int64]
vec_is -> do
        let read_carry_in :: ImpM rep r op ()
read_carry_in =
              VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix VName
acc [TExp Int64]
vec_is (VName -> SubExp
Var (VName -> SubExp) -> VName -> SubExp
forall a b. (a -> b) -> a -> b
$ PatElem LetDecMem -> VName
forall dec. PatElem dec -> VName
patElemName PatElem LetDecMem
pe) (TExp Int64
start TExp Int64 -> TExp Int64 -> TExp Int64
forall a. Num a => a -> a -> a
- TExp Int64
1 TExp Int64 -> [TExp Int64] -> [TExp Int64]
forall a. a -> [a] -> [a]
: [TExp Int64]
vec_is)
            read_neutral :: ImpM rep r op ()
read_neutral =
              VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix VName
acc [TExp Int64]
vec_is SubExp
ne []
        TExp Bool
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
TExp Bool
-> ImpM rep r op () -> ImpM rep r op () -> ImpM rep r op ()
sIf (TExp Int64
start TExp Int64 -> TExp Int64 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. TExp Int64
0) ImpM MCMem HostEnv Multicore ()
forall rep r op. ImpM rep r op ()
read_neutral ImpM MCMem HostEnv Multicore ()
forall rep r op. ImpM rep r op ()
read_carry_in
      VName -> ImpM MCMem HostEnv Multicore VName
forall (f :: * -> *) a. Applicative f => a -> f a
pure VName
acc

scanStage3Scalar ::
  Pat LetDecMem ->
  SegSpace ->
  KernelBody MCMem ->
  [SegBinOp MCMem] ->
  MulticoreGen ()
scanStage3Scalar :: Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage3Scalar Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops = do
  let per_scan_pes :: [[PatElem LetDecMem]]
per_scan_pes = [SegBinOp MCMem] -> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall rep a. [SegBinOp rep] -> [a] -> [[a]]
segBinOpChunks [SegBinOp MCMem]
scan_ops ([PatElem LetDecMem] -> [[PatElem LetDecMem]])
-> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall a b. (a -> b) -> a -> b
$ Pat LetDecMem -> [PatElem LetDecMem]
forall dec. Pat dec -> [PatElem dec]
patElems Pat LetDecMem
pat
  MCCode
body <- ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
    VName -> PrimType -> ImpM MCMem HostEnv Multicore ()
forall rep r op. VName -> PrimType -> ImpM rep r op ()
dPrim_ (SegSpace -> VName
segFlat SegSpace
space) PrimType
int64
    Multicore -> ImpM MCMem HostEnv Multicore ()
forall op rep r. op -> ImpM rep r op ()
sOp (Multicore -> ImpM MCMem HostEnv Multicore ())
-> Multicore -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ VName -> Multicore
Imp.GetTaskId (SegSpace -> VName
segFlat SegSpace
space)

    [SegBinOp MCMem] -> ImpM MCMem HostEnv Multicore ()
genBinOpParams [SegBinOp MCMem]
scan_ops
    [[VName]]
local_accs <- [SegBinOp MCMem] -> [[PatElem LetDecMem]] -> MulticoreGen [[VName]]
genLocalAccsStage3 [SegBinOp MCMem]
scan_ops [[PatElem LetDecMem]]
per_scan_pes

    ImpM MCMem HostEnv Multicore () -> ImpM MCMem HostEnv Multicore ()
inISPC (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
      String
-> ChunkLoopVectorization
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
generateChunkLoop String
"SegScan" ChunkLoopVectorization
Vectorized ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
        ScanLoopType
-> Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> [[VName]]
-> TExp Int64
-> ImpM MCMem HostEnv Multicore ()
genScanLoop ScanLoopType
ScanScalar Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops [[VName]]
local_accs
  [Param]
free_params <- MCCode -> MulticoreGen [Param]
forall a. FreeIn a => a -> MulticoreGen [Param]
freeParams MCCode
body
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ Multicore -> MCCode
forall a. a -> Code a
Imp.Op (Multicore -> MCCode) -> Multicore -> MCCode
forall a b. (a -> b) -> a -> b
$ String -> MCCode -> [Param] -> Multicore
Imp.ParLoop String
"scan_stage_3" MCCode
body [Param]
free_params

scanStage3Nested ::
  Pat LetDecMem ->
  SegSpace ->
  KernelBody MCMem ->
  [SegBinOp MCMem] ->
  MulticoreGen ()
scanStage3Nested :: Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage3Nested Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops = do
  let per_scan_pes :: [[PatElem LetDecMem]]
per_scan_pes = [SegBinOp MCMem] -> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall rep a. [SegBinOp rep] -> [a] -> [[a]]
segBinOpChunks [SegBinOp MCMem]
scan_ops ([PatElem LetDecMem] -> [[PatElem LetDecMem]])
-> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall a b. (a -> b) -> a -> b
$ Pat LetDecMem -> [PatElem LetDecMem]
forall dec. Pat dec -> [PatElem dec]
patElems Pat LetDecMem
pat
  MCCode
body <- ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
    VName -> PrimType -> ImpM MCMem HostEnv Multicore ()
forall rep r op. VName -> PrimType -> ImpM rep r op ()
dPrim_ (SegSpace -> VName
segFlat SegSpace
space) PrimType
int64
    Multicore -> ImpM MCMem HostEnv Multicore ()
forall op rep r. op -> ImpM rep r op ()
sOp (Multicore -> ImpM MCMem HostEnv Multicore ())
-> Multicore -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ VName -> Multicore
Imp.GetTaskId (SegSpace -> VName
segFlat SegSpace
space)

    MCCode
lparams <- ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ [SegBinOp MCMem] -> ImpM MCMem HostEnv Multicore ()
genBinOpParams [SegBinOp MCMem]
scan_ops
    [[VName]]
local_accs <- [SegBinOp MCMem] -> [[PatElem LetDecMem]] -> MulticoreGen [[VName]]
genLocalAccsStage3 [SegBinOp MCMem]
scan_ops [[PatElem LetDecMem]]
per_scan_pes

    ImpM MCMem HostEnv Multicore () -> ImpM MCMem HostEnv Multicore ()
inISPC (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ do
      MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit MCCode
lparams
      String
-> ChunkLoopVectorization
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
generateChunkLoop String
"SegScan" ChunkLoopVectorization
Scalar ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \TExp Int64
i -> do
        ScanLoopType
-> Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> [[VName]]
-> TExp Int64
-> ImpM MCMem HostEnv Multicore ()
genScanLoop ScanLoopType
ScanNested Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops [[VName]]
local_accs TExp Int64
i

  [Param]
free_params <- MCCode -> MulticoreGen [Param]
forall a. FreeIn a => a -> MulticoreGen [Param]
freeParams MCCode
body
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ Multicore -> MCCode
forall a. a -> Code a
Imp.Op (Multicore -> MCCode) -> Multicore -> MCCode
forall a b. (a -> b) -> a -> b
$ String -> MCCode -> [Param] -> Multicore
Imp.ParLoop String
"scan_stage_3" MCCode
body [Param]
free_params

scanStage3Fallback ::
  Pat LetDecMem ->
  SegSpace ->
  KernelBody MCMem ->
  [SegBinOp MCMem] ->
  MulticoreGen ()
scanStage3Fallback :: Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> ImpM MCMem HostEnv Multicore ()
scanStage3Fallback Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops = do
  let per_scan_pes :: [[PatElem LetDecMem]]
per_scan_pes = [SegBinOp MCMem] -> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall rep a. [SegBinOp rep] -> [a] -> [[a]]
segBinOpChunks [SegBinOp MCMem]
scan_ops ([PatElem LetDecMem] -> [[PatElem LetDecMem]])
-> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall a b. (a -> b) -> a -> b
$ Pat LetDecMem -> [PatElem LetDecMem]
forall dec. Pat dec -> [PatElem dec]
patElems Pat LetDecMem
pat
  MCCode
body <- ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
    VName -> PrimType -> ImpM MCMem HostEnv Multicore ()
forall rep r op. VName -> PrimType -> ImpM rep r op ()
dPrim_ (SegSpace -> VName
segFlat SegSpace
space) PrimType
int64
    Multicore -> ImpM MCMem HostEnv Multicore ()
forall op rep r. op -> ImpM rep r op ()
sOp (Multicore -> ImpM MCMem HostEnv Multicore ())
-> Multicore -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ VName -> Multicore
Imp.GetTaskId (SegSpace -> VName
segFlat SegSpace
space)

    [SegBinOp MCMem] -> ImpM MCMem HostEnv Multicore ()
genBinOpParams [SegBinOp MCMem]
scan_ops
    [[VName]]
local_accs <- [SegBinOp MCMem] -> [[PatElem LetDecMem]] -> MulticoreGen [[VName]]
genLocalAccsStage3 [SegBinOp MCMem]
scan_ops [[PatElem LetDecMem]]
per_scan_pes

    String
-> ChunkLoopVectorization
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
generateChunkLoop String
"SegScan" ChunkLoopVectorization
Scalar ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
      ScanLoopType
-> Pat LetDecMem
-> SegSpace
-> KernelBody MCMem
-> [SegBinOp MCMem]
-> [[VName]]
-> TExp Int64
-> ImpM MCMem HostEnv Multicore ()
genScanLoop ScanLoopType
ScanSeq Pat LetDecMem
pat SegSpace
space KernelBody MCMem
kbody [SegBinOp MCMem]
scan_ops [[VName]]
local_accs
  [Param]
free_params <- MCCode -> MulticoreGen [Param]
forall a. FreeIn a => a -> MulticoreGen [Param]
freeParams MCCode
body
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ Multicore -> MCCode
forall a. a -> Code a
Imp.Op (Multicore -> MCCode) -> Multicore -> MCCode
forall a b. (a -> b) -> a -> b
$ String -> MCCode -> [Param] -> Multicore
Imp.ParLoop String
"scan_stage_3" MCCode
body [Param]
free_params

-- Note: This isn't currently used anywhere.
-- This implementation for a Segmented scan only
-- parallelize over the segments and each segment is
-- scanned sequentially.
segmentedScan ::
  Pat LetDecMem ->
  SegSpace ->
  [SegBinOp MCMem] ->
  KernelBody MCMem ->
  MulticoreGen Imp.MCCode
segmentedScan :: Pat LetDecMem
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> MulticoreGen MCCode
segmentedScan Pat LetDecMem
pat SegSpace
space [SegBinOp MCMem]
scan_ops KernelBody MCMem
kbody = do
  MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ String -> Maybe Exp -> MCCode
forall a. String -> Maybe Exp -> Code a
Imp.DebugPrint String
"segmented segScan" Maybe Exp
forall a. Maybe a
Nothing
  ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
    MCCode
body <- Pat LetDecMem
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> MulticoreGen MCCode
compileSegScanBody Pat LetDecMem
pat SegSpace
space [SegBinOp MCMem]
scan_ops KernelBody MCMem
kbody
    [Param]
free_params <- MCCode -> MulticoreGen [Param]
forall a. FreeIn a => a -> MulticoreGen [Param]
freeParams MCCode
body
    MCCode -> ImpM MCMem HostEnv Multicore ()
forall op rep r. Code op -> ImpM rep r op ()
emit (MCCode -> ImpM MCMem HostEnv Multicore ())
-> MCCode -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ Multicore -> MCCode
forall a. a -> Code a
Imp.Op (Multicore -> MCCode) -> Multicore -> MCCode
forall a b. (a -> b) -> a -> b
$ String -> MCCode -> [Param] -> Multicore
Imp.ParLoop String
"seg_scan" MCCode
body [Param]
free_params

compileSegScanBody ::
  Pat LetDecMem ->
  SegSpace ->
  [SegBinOp MCMem] ->
  KernelBody MCMem ->
  MulticoreGen Imp.MCCode
compileSegScanBody :: Pat LetDecMem
-> SegSpace
-> [SegBinOp MCMem]
-> KernelBody MCMem
-> MulticoreGen MCCode
compileSegScanBody Pat LetDecMem
pat SegSpace
space [SegBinOp MCMem]
scan_ops KernelBody MCMem
kbody = ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall rep r op. ImpM rep r op () -> ImpM rep r op (Code op)
collect (ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode)
-> ImpM MCMem HostEnv Multicore () -> MulticoreGen MCCode
forall a b. (a -> b) -> a -> b
$ do
  let ([VName]
is, [SubExp]
ns) = [(VName, SubExp)] -> ([VName], [SubExp])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(VName, SubExp)] -> ([VName], [SubExp]))
-> [(VName, SubExp)] -> ([VName], [SubExp])
forall a b. (a -> b) -> a -> b
$ SegSpace -> [(VName, SubExp)]
unSegSpace SegSpace
space
      ns_64 :: [TExp Int64]
ns_64 = (SubExp -> TExp Int64) -> [SubExp] -> [TExp Int64]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TExp Int64
forall a. ToExp a => a -> TExp Int64
toInt64Exp [SubExp]
ns

  VName -> PrimType -> ImpM MCMem HostEnv Multicore ()
forall rep r op. VName -> PrimType -> ImpM rep r op ()
dPrim_ (SegSpace -> VName
segFlat SegSpace
space) PrimType
int64
  Multicore -> ImpM MCMem HostEnv Multicore ()
forall op rep r. op -> ImpM rep r op ()
sOp (Multicore -> ImpM MCMem HostEnv Multicore ())
-> Multicore -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ VName -> Multicore
Imp.GetTaskId (SegSpace -> VName
segFlat SegSpace
space)

  let per_scan_pes :: [[PatElem LetDecMem]]
per_scan_pes = [SegBinOp MCMem] -> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall rep a. [SegBinOp rep] -> [a] -> [[a]]
segBinOpChunks [SegBinOp MCMem]
scan_ops ([PatElem LetDecMem] -> [[PatElem LetDecMem]])
-> [PatElem LetDecMem] -> [[PatElem LetDecMem]]
forall a b. (a -> b) -> a -> b
$ Pat LetDecMem -> [PatElem LetDecMem]
forall dec. Pat dec -> [PatElem dec]
patElems Pat LetDecMem
pat
  String
-> ChunkLoopVectorization
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
generateChunkLoop String
"SegScan" ChunkLoopVectorization
Scalar ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \TExp Int64
segment_i -> do
    [(SegBinOp MCMem, [PatElem LetDecMem])]
-> ((SegBinOp MCMem, [PatElem LetDecMem])
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([SegBinOp MCMem]
-> [[PatElem LetDecMem]] -> [(SegBinOp MCMem, [PatElem LetDecMem])]
forall a b. [a] -> [b] -> [(a, b)]
zip [SegBinOp MCMem]
scan_ops [[PatElem LetDecMem]]
per_scan_pes) (((SegBinOp MCMem, [PatElem LetDecMem])
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((SegBinOp MCMem, [PatElem LetDecMem])
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(SegBinOp MCMem
scan_op, [PatElem LetDecMem]
scan_pes) -> do
      Maybe (Exp MCMem) -> Scope MCMem -> ImpM MCMem HostEnv Multicore ()
forall rep inner r op.
Mem rep inner =>
Maybe (Exp rep) -> Scope rep -> ImpM rep r op ()
dScope Maybe (Exp MCMem)
forall a. Maybe a
Nothing (Scope MCMem -> ImpM MCMem HostEnv Multicore ())
-> Scope MCMem -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ [Param LetDecMem] -> Scope MCMem
forall rep dec. (LParamInfo rep ~ dec) => [Param dec] -> Scope rep
scopeOfLParams ([Param LetDecMem] -> Scope MCMem)
-> [Param LetDecMem] -> Scope MCMem
forall a b. (a -> b) -> a -> b
$ Lambda MCMem -> [LParam MCMem]
forall rep. Lambda rep -> [LParam rep]
lambdaParams (Lambda MCMem -> [LParam MCMem]) -> Lambda MCMem -> [LParam MCMem]
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp MCMem
scan_op
      let ([Param LetDecMem]
scan_x_params, [Param LetDecMem]
scan_y_params) = Int -> [Param LetDecMem] -> ([Param LetDecMem], [Param LetDecMem])
forall a. Int -> [a] -> ([a], [a])
splitAt ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([SubExp] -> Int) -> [SubExp] -> Int
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan_op) ([Param LetDecMem] -> ([Param LetDecMem], [Param LetDecMem]))
-> [Param LetDecMem] -> ([Param LetDecMem], [Param LetDecMem])
forall a b. (a -> b) -> a -> b
$ (Lambda MCMem -> [Param LetDecMem]
forall rep. Lambda rep -> [LParam rep]
lambdaParams (Lambda MCMem -> [Param LetDecMem])
-> (SegBinOp MCMem -> Lambda MCMem)
-> SegBinOp MCMem
-> [Param LetDecMem]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda) SegBinOp MCMem
scan_op

      [(Param LetDecMem, SubExp)]
-> ((Param LetDecMem, SubExp) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param LetDecMem] -> [SubExp] -> [(Param LetDecMem, SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param LetDecMem]
scan_x_params ([SubExp] -> [(Param LetDecMem, SubExp)])
-> [SubExp] -> [(Param LetDecMem, SubExp)]
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan_op) (((Param LetDecMem, SubExp) -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((Param LetDecMem, SubExp) -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, SubExp
ne) ->
        VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p) [] SubExp
ne []

      let inner_bound :: TExp Int64
inner_bound = [TExp Int64] -> TExp Int64
forall a. [a] -> a
last [TExp Int64]
ns_64
      -- Perform a sequential scan over the segment ``segment_i``
      String
-> TExp Int64
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall t rep r op.
String
-> TExp t -> (TExp t -> ImpM rep r op ()) -> ImpM rep r op ()
sFor String
"i" TExp Int64
inner_bound ((TExp Int64 -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> (TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \TExp Int64
i -> do
        (VName -> TExp Int64 -> ImpM MCMem HostEnv Multicore ())
-> [VName] -> [TExp Int64] -> ImpM MCMem HostEnv Multicore ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName -> TExp Int64 -> ImpM MCMem HostEnv Multicore ()
forall t rep r op. VName -> TExp t -> ImpM rep r op ()
dPrimV_ ([VName] -> [VName]
forall a. [a] -> [a]
init [VName]
is) ([TExp Int64] -> ImpM MCMem HostEnv Multicore ())
-> [TExp Int64] -> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ [TExp Int64] -> TExp Int64 -> [TExp Int64]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex ([TExp Int64] -> [TExp Int64]
forall a. [a] -> [a]
init [TExp Int64]
ns_64) TExp Int64
segment_i
        VName -> TExp Int64 -> ImpM MCMem HostEnv Multicore ()
forall t rep r op. VName -> TExp t -> ImpM rep r op ()
dPrimV_ ([VName] -> VName
forall a. [a] -> a
last [VName]
is) TExp Int64
i
        Names
-> Stms MCMem
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op ()
compileStms Names
forall a. Monoid a => a
mempty (KernelBody MCMem -> Stms MCMem
forall rep. KernelBody rep -> Stms rep
kernelBodyStms KernelBody MCMem
kbody) (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ do
          let ([KernelResult]
scan_res, [KernelResult]
map_res) = Int -> [KernelResult] -> ([KernelResult], [KernelResult])
forall a. Int -> [a] -> ([a], [a])
splitAt ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([SubExp] -> Int) -> [SubExp] -> Int
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan_op) ([KernelResult] -> ([KernelResult], [KernelResult]))
-> [KernelResult] -> ([KernelResult], [KernelResult])
forall a b. (a -> b) -> a -> b
$ KernelBody MCMem -> [KernelResult]
forall rep. KernelBody rep -> [KernelResult]
kernelBodyResult KernelBody MCMem
kbody
          String
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. String -> ImpM rep r op () -> ImpM rep r op ()
sComment String
"write to-scan values to parameters" (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
            [(Param LetDecMem, KernelResult)]
-> ((Param LetDecMem, KernelResult)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param LetDecMem]
-> [KernelResult] -> [(Param LetDecMem, KernelResult)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param LetDecMem]
scan_y_params [KernelResult]
scan_res) (((Param LetDecMem, KernelResult)
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((Param LetDecMem, KernelResult)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, KernelResult
se) ->
              VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p) [] (KernelResult -> SubExp
kernelResultSubExp KernelResult
se) []

          String
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. String -> ImpM rep r op () -> ImpM rep r op ()
sComment String
"write mapped values results to memory" (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
            [(PatElem LetDecMem, KernelResult)]
-> ((PatElem LetDecMem, KernelResult)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([PatElem LetDecMem]
-> [KernelResult] -> [(PatElem LetDecMem, KernelResult)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Int -> [PatElem LetDecMem] -> [PatElem LetDecMem]
forall a. Int -> [a] -> [a]
drop ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([SubExp] -> Int) -> [SubExp] -> Int
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral SegBinOp MCMem
scan_op) ([PatElem LetDecMem] -> [PatElem LetDecMem])
-> [PatElem LetDecMem] -> [PatElem LetDecMem]
forall a b. (a -> b) -> a -> b
$ Pat LetDecMem -> [PatElem LetDecMem]
forall dec. Pat dec -> [PatElem dec]
patElems Pat LetDecMem
pat) [KernelResult]
map_res) (((PatElem LetDecMem, KernelResult)
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((PatElem LetDecMem, KernelResult)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(PatElem LetDecMem
pe, KernelResult
se) ->
              VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (PatElem LetDecMem -> VName
forall dec. PatElem dec -> VName
patElemName PatElem LetDecMem
pe) ((VName -> TExp Int64) -> [VName] -> [TExp Int64]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int64
forall a. a -> TPrimExp Int64 a
Imp.le64 [VName]
is) (KernelResult -> SubExp
kernelResultSubExp KernelResult
se) []

          String
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op. String -> ImpM rep r op () -> ImpM rep r op ()
sComment String
"combine with carry and write to memory" (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
            Names
-> Stms MCMem
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op ()
compileStms Names
forall a. Monoid a => a
mempty (Body MCMem -> Stms MCMem
forall rep. Body rep -> Stms rep
bodyStms (Body MCMem -> Stms MCMem) -> Body MCMem -> Stms MCMem
forall a b. (a -> b) -> a -> b
$ Lambda MCMem -> Body MCMem
forall rep. Lambda rep -> Body rep
lambdaBody (Lambda MCMem -> Body MCMem) -> Lambda MCMem -> Body MCMem
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp MCMem
scan_op) (ImpM MCMem HostEnv Multicore ()
 -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$
              [(Param LetDecMem, PatElem LetDecMem, SubExp)]
-> ((Param LetDecMem, PatElem LetDecMem, SubExp)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param LetDecMem]
-> [PatElem LetDecMem]
-> [SubExp]
-> [(Param LetDecMem, PatElem LetDecMem, SubExp)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Param LetDecMem]
scan_x_params [PatElem LetDecMem]
scan_pes ([SubExp] -> [(Param LetDecMem, PatElem LetDecMem, SubExp)])
-> [SubExp] -> [(Param LetDecMem, PatElem LetDecMem, SubExp)]
forall a b. (a -> b) -> a -> b
$ (SubExpRes -> SubExp) -> [SubExpRes] -> [SubExp]
forall a b. (a -> b) -> [a] -> [b]
map SubExpRes -> SubExp
resSubExp ([SubExpRes] -> [SubExp]) -> [SubExpRes] -> [SubExp]
forall a b. (a -> b) -> a -> b
$ Body MCMem -> [SubExpRes]
forall rep. Body rep -> [SubExpRes]
bodyResult (Body MCMem -> [SubExpRes]) -> Body MCMem -> [SubExpRes]
forall a b. (a -> b) -> a -> b
$ Lambda MCMem -> Body MCMem
forall rep. Lambda rep -> Body rep
lambdaBody (Lambda MCMem -> Body MCMem) -> Lambda MCMem -> Body MCMem
forall a b. (a -> b) -> a -> b
$ SegBinOp MCMem -> Lambda MCMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp MCMem
scan_op) (((Param LetDecMem, PatElem LetDecMem, SubExp)
  -> ImpM MCMem HostEnv Multicore ())
 -> ImpM MCMem HostEnv Multicore ())
-> ((Param LetDecMem, PatElem LetDecMem, SubExp)
    -> ImpM MCMem HostEnv Multicore ())
-> ImpM MCMem HostEnv Multicore ()
forall a b. (a -> b) -> a -> b
$ \(Param LetDecMem
p, PatElem LetDecMem
pe, SubExp
se) -> do
                VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (PatElem LetDecMem -> VName
forall dec. PatElem dec -> VName
patElemName PatElem LetDecMem
pe) ((VName -> TExp Int64) -> [VName] -> [TExp Int64]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int64
forall a. a -> TPrimExp Int64 a
Imp.le64 [VName]
is) SubExp
se []
                VName
-> [TExp Int64]
-> SubExp
-> [TExp Int64]
-> ImpM MCMem HostEnv Multicore ()
forall rep r op.
VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op ()
copyDWIMFix (Param LetDecMem -> VName
forall dec. Param dec -> VName
paramName Param LetDecMem
p) [] SubExp
se []