{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
module Futhark.CodeGen.ImpGen.Kernels.SegScan (compileSegScan) where
import Control.Monad.Except
import Control.Monad.State
import Data.List (delete, find, foldl', zip4)
import Data.Maybe
import qualified Futhark.CodeGen.ImpCode.Kernels as Imp
import Futhark.CodeGen.ImpGen
import Futhark.CodeGen.ImpGen.Kernels.Base
import Futhark.IR.KernelsMem
import qualified Futhark.IR.Mem.IxFun as IxFun
import Futhark.Transform.Rename
import Futhark.Util (takeLast)
import Futhark.Util.IntegralExp (divUp, quot, rem)
import Prelude hiding (quot, rem)
makeLocalArrays ::
Count GroupSize SubExp ->
SubExp ->
[SegBinOp KernelsMem] ->
InKernelGen [[VName]]
makeLocalArrays :: Count GroupSize SubExp
-> SubExp -> [SegBinOp KernelsMem] -> InKernelGen [[VName]]
makeLocalArrays (Count SubExp
group_size) SubExp
num_threads [SegBinOp KernelsMem]
scans = do
([[VName]]
arrs, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
mems_and_sizes) <- StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[[VName]]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> ImpM
KernelsMem
KernelEnv
KernelOp
([[VName]], [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT ((SegBinOp KernelsMem
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[VName])
-> [SegBinOp KernelsMem]
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[[VName]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM SegBinOp KernelsMem
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[VName]
onScan [SegBinOp KernelsMem]
scans) [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
forall a. Monoid a => a
mempty
let maxSize :: [Count u (TPrimExp Int64 v)] -> Count Bytes (TPrimExp Int64 v)
maxSize [Count u (TPrimExp Int64 v)]
sizes = TPrimExp Int64 v -> Count Bytes (TPrimExp Int64 v)
forall a. a -> Count Bytes a
Imp.bytes (TPrimExp Int64 v -> Count Bytes (TPrimExp Int64 v))
-> TPrimExp Int64 v -> Count Bytes (TPrimExp Int64 v)
forall a b. (a -> b) -> a -> b
$ (TPrimExp Int64 v -> TPrimExp Int64 v -> TPrimExp Int64 v)
-> TPrimExp Int64 v -> [TPrimExp Int64 v] -> TPrimExp Int64 v
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' TPrimExp Int64 v -> TPrimExp Int64 v -> TPrimExp Int64 v
forall v. TPrimExp Int64 v -> TPrimExp Int64 v -> TPrimExp Int64 v
sMax64 TPrimExp Int64 v
1 ([TPrimExp Int64 v] -> TPrimExp Int64 v)
-> [TPrimExp Int64 v] -> TPrimExp Int64 v
forall a b. (a -> b) -> a -> b
$ (Count u (TPrimExp Int64 v) -> TPrimExp Int64 v)
-> [Count u (TPrimExp Int64 v)] -> [TPrimExp Int64 v]
forall a b. (a -> b) -> [a] -> [b]
map Count u (TPrimExp Int64 v) -> TPrimExp Int64 v
forall u e. Count u e -> e
Imp.unCount [Count u (TPrimExp Int64 v)]
sizes
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> (([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
mems_and_sizes ((([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> (([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \([Count Bytes (TPrimExp Int64 ExpLeaf)]
sizes, VName
mem) ->
VName
-> Count Bytes (TPrimExp Int64 ExpLeaf)
-> Space
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> Count Bytes (TPrimExp Int64 ExpLeaf)
-> Space
-> ImpM lore r op ()
sAlloc_ VName
mem ([Count Bytes (TPrimExp Int64 ExpLeaf)]
-> Count Bytes (TPrimExp Int64 ExpLeaf)
forall v u.
Pretty v =>
[Count u (TPrimExp Int64 v)] -> Count Bytes (TPrimExp Int64 v)
maxSize [Count Bytes (TPrimExp Int64 ExpLeaf)]
sizes) (SpaceId -> Space
Space SpaceId
"local")
[[VName]] -> InKernelGen [[VName]]
forall (m :: * -> *) a. Monad m => a -> m a
return [[VName]]
arrs
where
onScan :: SegBinOp KernelsMem
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[VName]
onScan (SegBinOp Commutativity
_ Lambda KernelsMem
scan_op [SubExp]
nes Shape
_) = do
let ([Param (MemInfo SubExp NoUniqueness MemBind)]
scan_x_params, [Param (MemInfo SubExp NoUniqueness MemBind)]
_scan_y_params) =
Int
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> ([Param (MemInfo SubExp NoUniqueness MemBind)],
[Param (MemInfo SubExp NoUniqueness MemBind)])
forall a. Int -> [a] -> ([a], [a])
splitAt ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SubExp]
nes) ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> ([Param (MemInfo SubExp NoUniqueness MemBind)],
[Param (MemInfo SubExp NoUniqueness MemBind)]))
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> ([Param (MemInfo SubExp NoUniqueness MemBind)],
[Param (MemInfo SubExp NoUniqueness MemBind)])
forall a b. (a -> b) -> a -> b
$ Lambda KernelsMem -> [LParam KernelsMem]
forall lore. LambdaT lore -> [LParam lore]
lambdaParams Lambda KernelsMem
scan_op
([VName]
arrs, [[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]]
used_mems) <- ([(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])]
-> ([VName], [[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]]))
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])]
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
([VName], [[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])]
-> ([VName], [[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]])
forall a b. [(a, b)] -> ([a], [b])
unzip (StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])]
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
([VName], [[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]]))
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])]
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
([VName], [[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]])
forall a b. (a -> b) -> a -> b
$
[Param (MemInfo SubExp NoUniqueness MemBind)]
-> (Param (MemInfo SubExp NoUniqueness MemBind)
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]))
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Param (MemInfo SubExp NoUniqueness MemBind)]
scan_x_params ((Param (MemInfo SubExp NoUniqueness MemBind)
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]))
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])])
-> (Param (MemInfo SubExp NoUniqueness MemBind)
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]))
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])]
forall a b. (a -> b) -> a -> b
$ \Param (MemInfo SubExp NoUniqueness MemBind)
p ->
case Param (MemInfo SubExp NoUniqueness MemBind)
-> MemInfo SubExp NoUniqueness MemBind
forall dec. Param dec -> dec
paramDec Param (MemInfo SubExp NoUniqueness MemBind)
p of
MemArray PrimType
pt Shape
shape NoUniqueness
_ (ArrayIn VName
mem IxFun
_) -> do
let shape' :: Shape
shape' = [SubExp] -> Shape
forall d. [d] -> ShapeBase d
Shape [SubExp
num_threads] Shape -> Shape -> Shape
forall a. Semigroup a => a -> a -> a
<> Shape
shape
VName
arr <-
ImpM KernelsMem KernelEnv KernelOp VName
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
VName
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ImpM KernelsMem KernelEnv KernelOp VName
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
VName)
-> ImpM KernelsMem KernelEnv KernelOp VName
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
VName
forall a b. (a -> b) -> a -> b
$
SpaceId
-> PrimType
-> Shape
-> MemBind
-> ImpM KernelsMem KernelEnv KernelOp VName
forall lore r op.
SpaceId -> PrimType -> Shape -> MemBind -> ImpM lore r op VName
sArray SpaceId
"scan_arr" PrimType
pt Shape
shape' (MemBind -> ImpM KernelsMem KernelEnv KernelOp VName)
-> MemBind -> ImpM KernelsMem KernelEnv KernelOp VName
forall a b. (a -> b) -> a -> b
$
VName -> IxFun -> MemBind
ArrayIn VName
mem (IxFun -> MemBind) -> IxFun -> MemBind
forall a b. (a -> b) -> a -> b
$ Shape (TPrimExp Int32 VName) -> IxFun
forall num. IntegralExp num => Shape num -> IxFun num
IxFun.iota (Shape (TPrimExp Int32 VName) -> IxFun)
-> Shape (TPrimExp Int32 VName) -> IxFun
forall a b. (a -> b) -> a -> b
$ (SubExp -> TPrimExp Int32 VName)
-> [SubExp] -> Shape (TPrimExp Int32 VName)
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TPrimExp Int32 VName
pe32 ([SubExp] -> Shape (TPrimExp Int32 VName))
-> [SubExp] -> Shape (TPrimExp Int32 VName)
forall a b. (a -> b) -> a -> b
$ Shape -> [SubExp]
forall d. ShapeBase d -> [d]
shapeDims Shape
shape'
(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
forall (m :: * -> *) a. Monad m => a -> m a
return (VName
arr, [])
MemInfo SubExp NoUniqueness MemBind
_ -> do
let pt :: PrimType
pt = TypeBase Shape NoUniqueness -> PrimType
forall shape u. TypeBase shape u -> PrimType
elemType (TypeBase Shape NoUniqueness -> PrimType)
-> TypeBase Shape NoUniqueness -> PrimType
forall a b. (a -> b) -> a -> b
$ Param (MemInfo SubExp NoUniqueness MemBind)
-> TypeBase Shape NoUniqueness
forall dec. Typed dec => Param dec -> TypeBase Shape NoUniqueness
paramType Param (MemInfo SubExp NoUniqueness MemBind)
p
shape :: Shape
shape = [SubExp] -> Shape
forall d. [d] -> ShapeBase d
Shape [SubExp
group_size]
([Count Bytes (TPrimExp Int64 ExpLeaf)]
sizes, VName
mem') <- PrimType
-> Shape
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
forall (t :: (* -> *) -> * -> *) lore r op.
(MonadState
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(t (ImpM lore r op)),
MonadTrans t) =>
PrimType
-> Shape
-> t (ImpM lore r op)
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
getMem PrimType
pt Shape
shape
VName
arr <- ImpM KernelsMem KernelEnv KernelOp VName
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
VName
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ImpM KernelsMem KernelEnv KernelOp VName
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
VName)
-> ImpM KernelsMem KernelEnv KernelOp VName
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
VName
forall a b. (a -> b) -> a -> b
$ SpaceId
-> PrimType
-> Shape
-> VName
-> ImpM KernelsMem KernelEnv KernelOp VName
forall lore r op.
SpaceId -> PrimType -> Shape -> VName -> ImpM lore r op VName
sArrayInMem SpaceId
"scan_arr" PrimType
pt Shape
shape VName
mem'
(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
(VName, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
forall (m :: * -> *) a. Monad m => a -> m a
return (VName
arr, [([Count Bytes (TPrimExp Int64 ExpLeaf)]
sizes, VName
mem')])
([([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ([([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
forall a. Semigroup a => a -> a -> a
<> [[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]]
used_mems)
[VName]
-> StateT
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
(ImpM KernelsMem KernelEnv KernelOp)
[VName]
forall (m :: * -> *) a. Monad m => a -> m a
return [VName]
arrs
getMem :: PrimType
-> Shape
-> t (ImpM lore r op)
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
getMem PrimType
pt Shape
shape = do
let size :: Count Bytes (TPrimExp Int64 ExpLeaf)
size = TypeBase Shape NoUniqueness -> Count Bytes (TPrimExp Int64 ExpLeaf)
typeSize (TypeBase Shape NoUniqueness
-> Count Bytes (TPrimExp Int64 ExpLeaf))
-> TypeBase Shape NoUniqueness
-> Count Bytes (TPrimExp Int64 ExpLeaf)
forall a b. (a -> b) -> a -> b
$ PrimType -> Shape -> NoUniqueness -> TypeBase Shape NoUniqueness
forall shape u. PrimType -> shape -> u -> TypeBase shape u
Array PrimType
pt Shape
shape NoUniqueness
NoUniqueness
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
mems <- t (ImpM lore r op)
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
forall s (m :: * -> *). MonadState s m => m s
get
case ((([Count Bytes (TPrimExp Int64 ExpLeaf)], VName) -> Bool)
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> Maybe ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Count Bytes (TPrimExp Int64 ExpLeaf)
size Count Bytes (TPrimExp Int64 ExpLeaf)
-> [Count Bytes (TPrimExp Int64 ExpLeaf)] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem`) ([Count Bytes (TPrimExp Int64 ExpLeaf)] -> Bool)
-> (([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> [Count Bytes (TPrimExp Int64 ExpLeaf)])
-> ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> [Count Bytes (TPrimExp Int64 ExpLeaf)]
forall a b. (a, b) -> a
fst) [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
mems, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
mems) of
(Just ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
mem, [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
_) -> do
([([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
-> t (ImpM lore r op) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (([([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
-> t (ImpM lore r op) ())
-> ([([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)])
-> t (ImpM lore r op) ()
forall a b. (a -> b) -> a -> b
$ ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
forall a. Eq a => a -> [a] -> [a]
delete ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
mem
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> t (ImpM lore r op)
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
mem
(Maybe ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
Nothing, ([Count Bytes (TPrimExp Int64 ExpLeaf)]
size', VName
mem) : [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
mems') -> do
[([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
-> t (ImpM lore r op) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put [([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)]
mems'
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> t (ImpM lore r op)
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
forall (m :: * -> *) a. Monad m => a -> m a
return (Count Bytes (TPrimExp Int64 ExpLeaf)
size Count Bytes (TPrimExp Int64 ExpLeaf)
-> [Count Bytes (TPrimExp Int64 ExpLeaf)]
-> [Count Bytes (TPrimExp Int64 ExpLeaf)]
forall a. a -> [a] -> [a]
: [Count Bytes (TPrimExp Int64 ExpLeaf)]
size', VName
mem)
(Maybe ([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
Nothing, []) -> do
VName
mem <- ImpM lore r op VName -> t (ImpM lore r op) VName
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ImpM lore r op VName -> t (ImpM lore r op) VName)
-> ImpM lore r op VName -> t (ImpM lore r op) VName
forall a b. (a -> b) -> a -> b
$ SpaceId -> Space -> ImpM lore r op VName
forall lore r op. SpaceId -> Space -> ImpM lore r op VName
sDeclareMem SpaceId
"scan_arr_mem" (Space -> ImpM lore r op VName) -> Space -> ImpM lore r op VName
forall a b. (a -> b) -> a -> b
$ SpaceId -> Space
Space SpaceId
"local"
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
-> t (ImpM lore r op)
([Count Bytes (TPrimExp Int64 ExpLeaf)], VName)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Count Bytes (TPrimExp Int64 ExpLeaf)
size], VName
mem)
type CrossesSegment = Maybe (Imp.TExp Int32 -> Imp.TExp Int32 -> Imp.TExp Bool)
localArrayIndex :: KernelConstants -> Type -> Imp.TExp Int32
localArrayIndex :: KernelConstants -> TypeBase Shape NoUniqueness -> TExp Int32
localArrayIndex KernelConstants
constants TypeBase Shape NoUniqueness
t =
if TypeBase Shape NoUniqueness -> Bool
forall shape u. TypeBase shape u -> Bool
primType TypeBase Shape NoUniqueness
t
then KernelConstants -> TExp Int32
kernelLocalThreadId KernelConstants
constants
else KernelConstants -> TExp Int32
kernelGlobalThreadId KernelConstants
constants
barrierFor :: Lambda KernelsMem -> (Bool, Imp.Fence, InKernelGen ())
barrierFor :: Lambda KernelsMem
-> (Bool, Fence, ImpM KernelsMem KernelEnv KernelOp ())
barrierFor Lambda KernelsMem
scan_op = (Bool
array_scan, Fence
fence, KernelOp -> ImpM KernelsMem KernelEnv KernelOp ()
forall op lore r. op -> ImpM lore r op ()
sOp (KernelOp -> ImpM KernelsMem KernelEnv KernelOp ())
-> KernelOp -> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ Fence -> KernelOp
Imp.Barrier Fence
fence)
where
array_scan :: Bool
array_scan = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (TypeBase Shape NoUniqueness -> Bool)
-> [TypeBase Shape NoUniqueness] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all TypeBase Shape NoUniqueness -> Bool
forall shape u. TypeBase shape u -> Bool
primType ([TypeBase Shape NoUniqueness] -> Bool)
-> [TypeBase Shape NoUniqueness] -> Bool
forall a b. (a -> b) -> a -> b
$ Lambda KernelsMem -> [TypeBase Shape NoUniqueness]
forall lore. LambdaT lore -> [TypeBase Shape NoUniqueness]
lambdaReturnType Lambda KernelsMem
scan_op
fence :: Fence
fence
| Bool
array_scan = Fence
Imp.FenceGlobal
| Bool
otherwise = Fence
Imp.FenceLocal
xParams, yParams :: SegBinOp KernelsMem -> [LParam KernelsMem]
xParams :: SegBinOp KernelsMem -> [LParam KernelsMem]
xParams SegBinOp KernelsMem
scan =
Int
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
forall a. Int -> [a] -> [a]
take ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (SegBinOp KernelsMem -> [SubExp]
forall lore. SegBinOp lore -> [SubExp]
segBinOpNeutral SegBinOp KernelsMem
scan)) (Lambda KernelsMem -> [LParam KernelsMem]
forall lore. LambdaT lore -> [LParam lore]
lambdaParams (SegBinOp KernelsMem -> Lambda KernelsMem
forall lore. SegBinOp lore -> Lambda lore
segBinOpLambda SegBinOp KernelsMem
scan))
yParams :: SegBinOp KernelsMem -> [LParam KernelsMem]
yParams SegBinOp KernelsMem
scan =
Int
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
forall a. Int -> [a] -> [a]
drop ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (SegBinOp KernelsMem -> [SubExp]
forall lore. SegBinOp lore -> [SubExp]
segBinOpNeutral SegBinOp KernelsMem
scan)) (Lambda KernelsMem -> [LParam KernelsMem]
forall lore. LambdaT lore -> [LParam lore]
lambdaParams (SegBinOp KernelsMem -> Lambda KernelsMem
forall lore. SegBinOp lore -> Lambda lore
segBinOpLambda SegBinOp KernelsMem
scan))
writeToScanValues ::
[VName] ->
([PatElem KernelsMem], SegBinOp KernelsMem, [KernelResult]) ->
InKernelGen ()
writeToScanValues :: [VName]
-> ([PatElem KernelsMem], SegBinOp KernelsMem, [KernelResult])
-> ImpM KernelsMem KernelEnv KernelOp ()
writeToScanValues [VName]
gtids ([PatElem KernelsMem]
pes, SegBinOp KernelsMem
scan, [KernelResult]
scan_res)
| Shape -> Int
forall a. ArrayShape a => a -> Int
shapeRank (SegBinOp KernelsMem -> Shape
forall lore. SegBinOp lore -> Shape
segBinOpShape SegBinOp KernelsMem
scan) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 =
[(PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)]
-> ((PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [KernelResult]
-> [(PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)]
forall a b. [a] -> [b] -> [(a, b)]
zip [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes [KernelResult]
scan_res) (((PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe, KernelResult
res) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
(PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids)
(KernelResult -> SubExp
kernelResultSubExp KernelResult
res)
[]
| Bool
otherwise =
[(Param (MemInfo SubExp NoUniqueness MemBind), KernelResult)]
-> ((Param (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [KernelResult]
-> [(Param (MemInfo SubExp NoUniqueness MemBind), KernelResult)]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp KernelsMem -> [LParam KernelsMem]
yParams SegBinOp KernelsMem
scan) [KernelResult]
scan_res) (((Param (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, KernelResult
res) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix (Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p) [] (KernelResult -> SubExp
kernelResultSubExp KernelResult
res) []
readToScanValues ::
[Imp.TExp Int32] ->
[PatElem KernelsMem] ->
SegBinOp KernelsMem ->
InKernelGen ()
readToScanValues :: [TExp Int32]
-> [PatElem KernelsMem]
-> SegBinOp KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
readToScanValues [TExp Int32]
is [PatElem KernelsMem]
pes SegBinOp KernelsMem
scan
| Shape -> Int
forall a. ArrayShape a => a -> Int
shapeRank (SegBinOp KernelsMem -> Shape
forall lore. SegBinOp lore -> Shape
segBinOpShape SegBinOp KernelsMem
scan) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 =
[(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp KernelsMem -> [LParam KernelsMem]
yParams SegBinOp KernelsMem
scan) [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes) (((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix (Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p) [] (VName -> SubExp
Var (PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)) [TExp Int32]
is
| Bool
otherwise =
() -> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
readCarries ::
Imp.TExp Int32 ->
[Imp.TExp Int32] ->
[Imp.TExp Int32] ->
[PatElem KernelsMem] ->
SegBinOp KernelsMem ->
InKernelGen ()
readCarries :: TExp Int32
-> [TExp Int32]
-> [TExp Int32]
-> [PatElem KernelsMem]
-> SegBinOp KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
readCarries TExp Int32
chunk_offset [TExp Int32]
dims' [TExp Int32]
vec_is [PatElem KernelsMem]
pes SegBinOp KernelsMem
scan
| Shape -> Int
forall a. ArrayShape a => a -> Int
shapeRank (SegBinOp KernelsMem -> Shape
forall lore. SegBinOp lore -> Shape
segBinOpShape SegBinOp KernelsMem
scan) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = do
TExp Int32
ltid <- KernelConstants -> TExp Int32
kernelLocalThreadId (KernelConstants -> TExp Int32)
-> (KernelEnv -> KernelConstants) -> KernelEnv -> TExp Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KernelEnv -> KernelConstants
kernelConstants (KernelEnv -> TExp Int32)
-> ImpM KernelsMem KernelEnv KernelOp KernelEnv
-> ImpM KernelsMem KernelEnv KernelOp (TExp Int32)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ImpM KernelsMem KernelEnv KernelOp KernelEnv
forall lore r op. ImpM lore r op r
askEnv
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool
-> ImpM lore r op () -> ImpM lore r op () -> ImpM lore r op ()
sIf
(TExp Int32
chunk_offset TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.>. TExp Int32
0 TExp Bool -> TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
.&&. TExp Int32
ltid TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. TExp Int32
0)
( do
let is :: [TExp Int32]
is = [TExp Int32] -> TExp Int32 -> [TExp Int32]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TExp Int32]
dims' (TExp Int32 -> [TExp Int32]) -> TExp Int32 -> [TExp Int32]
forall a b. (a -> b) -> a -> b
$ TExp Int32
chunk_offset TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1
[(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp KernelsMem -> [LParam KernelsMem]
xParams SegBinOp KernelsMem
scan) [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes) (((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix (Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p) [] (VName -> SubExp
Var (PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)) ([TExp Int32]
is [TExp Int32] -> [TExp Int32] -> [TExp Int32]
forall a. [a] -> [a] -> [a]
++ [TExp Int32]
vec_is)
)
( [(Param (MemInfo SubExp NoUniqueness MemBind), SubExp)]
-> ((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [SubExp]
-> [(Param (MemInfo SubExp NoUniqueness MemBind), SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp KernelsMem -> [LParam KernelsMem]
xParams SegBinOp KernelsMem
scan) (SegBinOp KernelsMem -> [SubExp]
forall lore. SegBinOp lore -> [SubExp]
segBinOpNeutral SegBinOp KernelsMem
scan)) (((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, SubExp
ne) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix (Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p) [] SubExp
ne []
)
| Bool
otherwise =
() -> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
scanStage1 ::
Pattern KernelsMem ->
Count NumGroups SubExp ->
Count GroupSize SubExp ->
SegSpace ->
[SegBinOp KernelsMem] ->
KernelBody KernelsMem ->
CallKernelGen (TV Int32, Imp.TExp Int32, CrossesSegment)
scanStage1 :: Pattern KernelsMem
-> Count NumGroups SubExp
-> Count GroupSize SubExp
-> SegSpace
-> [SegBinOp KernelsMem]
-> KernelBody KernelsMem
-> CallKernelGen (TV Int32, TExp Int32, CrossesSegment)
scanStage1 (Pattern [PatElem KernelsMem]
_ [PatElem KernelsMem]
all_pes) Count NumGroups SubExp
num_groups Count GroupSize SubExp
group_size SegSpace
space [SegBinOp KernelsMem]
scans KernelBody KernelsMem
kbody = do
let num_groups' :: Count NumGroups (TExp Int32)
num_groups' = (SubExp -> TExp Int32)
-> Count NumGroups SubExp -> Count NumGroups (TExp Int32)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp Count NumGroups SubExp
num_groups
group_size' :: Count GroupSize (TExp Int32)
group_size' = (SubExp -> TExp Int32)
-> Count GroupSize SubExp -> Count GroupSize (TExp Int32)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp Count GroupSize SubExp
group_size
TV Int32
num_threads <- SpaceId -> TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TV Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"num_threads" (TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TV Int32))
-> TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TV Int32)
forall a b. (a -> b) -> a -> b
$ Count NumGroups (TExp Int32) -> TExp Int32
forall u e. Count u e -> e
unCount Count NumGroups (TExp Int32)
num_groups' TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* Count GroupSize (TExp Int32) -> TExp Int32
forall u e. Count u e -> e
unCount Count GroupSize (TExp Int32)
group_size'
let ([VName]
gtids, [SubExp]
dims) = [(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
dims' :: [TExp Int32]
dims' = (SubExp -> TExp Int32) -> [SubExp] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp [SubExp]
dims
let num_elements :: TExp Int32
num_elements = [TExp Int32] -> TExp Int32
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [TExp Int32]
dims'
elems_per_thread :: TExp Int32
elems_per_thread = TExp Int32
num_elements TExp Int32 -> TExp Int32 -> TExp Int32
forall e. IntegralExp e => e -> e -> e
`divUp` TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
num_threads
elems_per_group :: TExp Int32
elems_per_group = Count GroupSize (TExp Int32) -> TExp Int32
forall u e. Count u e -> e
unCount Count GroupSize (TExp Int32)
group_size' TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* TExp Int32
elems_per_thread
let crossesSegment :: CrossesSegment
crossesSegment =
case [TExp Int32] -> [TExp Int32]
forall a. [a] -> [a]
reverse [TExp Int32]
dims' of
TExp Int32
segment_size : TExp Int32
_ : [TExp Int32]
_ -> (TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment
forall a. a -> Maybe a
Just ((TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment)
-> (TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment
forall a b. (a -> b) -> a -> b
$ \TExp Int32
from TExp Int32
to ->
(TExp Int32
to TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
from) TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.>. (TExp Int32
to TExp Int32 -> TExp Int32 -> TExp Int32
forall e. IntegralExp e => e -> e -> e
`rem` TExp Int32
segment_size)
[TExp Int32]
_ -> CrossesSegment
forall a. Maybe a
Nothing
SpaceId
-> Count NumGroups (TExp Int32)
-> Count GroupSize (TExp Int32)
-> VName
-> ImpM KernelsMem KernelEnv KernelOp ()
-> CallKernelGen ()
sKernelThread SpaceId
"scan_stage1" Count NumGroups (TExp Int32)
num_groups' Count GroupSize (TExp Int32)
group_size' (SegSpace -> VName
segFlat SegSpace
space) (ImpM KernelsMem KernelEnv KernelOp () -> CallKernelGen ())
-> ImpM KernelsMem KernelEnv KernelOp () -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$ do
KernelConstants
constants <- KernelEnv -> KernelConstants
kernelConstants (KernelEnv -> KernelConstants)
-> ImpM KernelsMem KernelEnv KernelOp KernelEnv
-> ImpM KernelsMem KernelEnv KernelOp KernelConstants
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ImpM KernelsMem KernelEnv KernelOp KernelEnv
forall lore r op. ImpM lore r op r
askEnv
[[VName]]
all_local_arrs <- Count GroupSize SubExp
-> SubExp -> [SegBinOp KernelsMem] -> InKernelGen [[VName]]
makeLocalArrays Count GroupSize SubExp
group_size (TV Int32 -> SubExp
forall t. TV t -> SubExp
tvSize TV Int32
num_threads) [SegBinOp KernelsMem]
scans
[SegBinOp KernelsMem]
-> (SegBinOp KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [SegBinOp KernelsMem]
scans ((SegBinOp KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> (SegBinOp KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \SegBinOp KernelsMem
scan -> do
Maybe (Exp KernelsMem)
-> Scope KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Mem lore =>
Maybe (Exp lore) -> Scope lore -> ImpM lore r op ()
dScope Maybe (Exp KernelsMem)
forall a. Maybe a
Nothing (Scope KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ())
-> Scope KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ [Param (MemInfo SubExp NoUniqueness MemBind)] -> Scope KernelsMem
forall lore dec.
(LParamInfo lore ~ dec) =>
[Param dec] -> Scope lore
scopeOfLParams ([Param (MemInfo SubExp NoUniqueness MemBind)] -> Scope KernelsMem)
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> Scope KernelsMem
forall a b. (a -> b) -> a -> b
$ Lambda KernelsMem -> [LParam KernelsMem]
forall lore. LambdaT lore -> [LParam lore]
lambdaParams (Lambda KernelsMem -> [LParam KernelsMem])
-> Lambda KernelsMem -> [LParam KernelsMem]
forall a b. (a -> b) -> a -> b
$ SegBinOp KernelsMem -> Lambda KernelsMem
forall lore. SegBinOp lore -> Lambda lore
segBinOpLambda SegBinOp KernelsMem
scan
[(Param (MemInfo SubExp NoUniqueness MemBind), SubExp)]
-> ((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [SubExp]
-> [(Param (MemInfo SubExp NoUniqueness MemBind), SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp KernelsMem -> [LParam KernelsMem]
xParams SegBinOp KernelsMem
scan) (SegBinOp KernelsMem -> [SubExp]
forall lore. SegBinOp lore -> [SubExp]
segBinOpNeutral SegBinOp KernelsMem
scan)) (((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, SubExp
ne) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix (Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p) [] SubExp
ne []
SpaceId
-> TExp Int32
-> (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall t lore r op.
SpaceId
-> TExp t -> (TExp t -> ImpM lore r op ()) -> ImpM lore r op ()
sFor SpaceId
"j" TExp Int32
elems_per_thread ((TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \TExp Int32
j -> do
TV Int32
chunk_offset <-
SpaceId
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"chunk_offset" (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32))
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall a b. (a -> b) -> a -> b
$
KernelConstants -> TExp Int32
kernelGroupSize KernelConstants
constants TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* TExp Int32
j
TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ KernelConstants -> TExp Int32
kernelGroupId KernelConstants
constants TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* TExp Int32
elems_per_group
TV Int32
flat_idx <-
SpaceId
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"flat_idx" (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32))
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall a b. (a -> b) -> a -> b
$
TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
chunk_offset TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ KernelConstants -> TExp Int32
kernelLocalThreadId KernelConstants
constants
(VName -> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> [VName] -> [TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName -> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ()
forall t lore r op. VName -> TExp t -> ImpM lore r op ()
dPrimV_ [VName]
gtids ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> [TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ [TExp Int32] -> TExp Int32 -> [TExp Int32]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TExp Int32]
dims' (TExp Int32 -> [TExp Int32]) -> TExp Int32 -> [TExp Int32]
forall a b. (a -> b) -> a -> b
$ TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
flat_idx
let per_scan_pes :: [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
per_scan_pes = [SegBinOp KernelsMem]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
forall lore a. [SegBinOp lore] -> [a] -> [[a]]
segBinOpChunks [SegBinOp KernelsMem]
scans [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
all_pes
in_bounds :: TExp Bool
in_bounds =
(TExp Bool -> TExp Bool -> TExp Bool) -> [TExp Bool] -> TExp Bool
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 TExp Bool -> TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
(.&&.) ([TExp Bool] -> TExp Bool) -> [TExp Bool] -> TExp Bool
forall a b. (a -> b) -> a -> b
$ (TExp Int32 -> TExp Int32 -> TExp Bool)
-> [TExp Int32] -> [TExp Int32] -> [TExp Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
(.<.) ((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids) [TExp Int32]
dims'
when_in_bounds :: ImpM KernelsMem KernelEnv KernelOp ()
when_in_bounds = Names
-> Stms KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Names -> Stms lore -> ImpM lore r op () -> ImpM lore r op ()
compileStms Names
forall a. Monoid a => a
mempty (KernelBody KernelsMem -> Stms KernelsMem
forall lore. KernelBody lore -> Stms lore
kernelBodyStms KernelBody KernelsMem
kbody) (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ do
let ([KernelResult]
all_scan_res, [KernelResult]
map_res) =
Int -> [KernelResult] -> ([KernelResult], [KernelResult])
forall a. Int -> [a] -> ([a], [a])
splitAt ([SegBinOp KernelsMem] -> Int
forall lore. [SegBinOp lore] -> Int
segBinOpResults [SegBinOp KernelsMem]
scans) ([KernelResult] -> ([KernelResult], [KernelResult]))
-> [KernelResult] -> ([KernelResult], [KernelResult])
forall a b. (a -> b) -> a -> b
$ KernelBody KernelsMem -> [KernelResult]
forall lore. KernelBody lore -> [KernelResult]
kernelBodyResult KernelBody KernelsMem
kbody
per_scan_res :: [[KernelResult]]
per_scan_res =
[SegBinOp KernelsMem] -> [KernelResult] -> [[KernelResult]]
forall lore a. [SegBinOp lore] -> [a] -> [[a]]
segBinOpChunks [SegBinOp KernelsMem]
scans [KernelResult]
all_scan_res
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"write to-scan values to parameters" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
(([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [KernelResult])
-> ImpM KernelsMem KernelEnv KernelOp ())
-> [([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [KernelResult])]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ([VName]
-> ([PatElem KernelsMem], SegBinOp KernelsMem, [KernelResult])
-> ImpM KernelsMem KernelEnv KernelOp ()
writeToScanValues [VName]
gtids) ([([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [KernelResult])]
-> ImpM KernelsMem KernelEnv KernelOp ())
-> [([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [KernelResult])]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
[[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
-> [SegBinOp KernelsMem]
-> [[KernelResult]]
-> [([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [KernelResult])]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
per_scan_pes [SegBinOp KernelsMem]
scans [[KernelResult]]
per_scan_res
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"write mapped values results to global memory" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
[(PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)]
-> ((PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [KernelResult]
-> [(PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Int
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
forall a. Int -> [a] -> [a]
takeLast ([KernelResult] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [KernelResult]
map_res) [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
all_pes) [KernelResult]
map_res) (((PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((PatElemT (MemInfo SubExp NoUniqueness MemBind), KernelResult)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe, KernelResult
se) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
(PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids)
(KernelResult -> SubExp
kernelResultSubExp KernelResult
se)
[]
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"threads in bounds read input" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool -> ImpM lore r op () -> ImpM lore r op ()
sWhen TExp Bool
in_bounds ImpM KernelsMem KernelEnv KernelOp ()
when_in_bounds
[([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [VName])]
-> (([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [VName])
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
-> [SegBinOp KernelsMem]
-> [[VName]]
-> [([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [VName])]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
per_scan_pes [SegBinOp KernelsMem]
scans [[VName]]
all_local_arrs) ((([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [VName])
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> (([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem, [VName])
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
\([PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes, scan :: SegBinOp KernelsMem
scan@(SegBinOp Commutativity
_ Lambda KernelsMem
scan_op [SubExp]
nes Shape
vec_shape), [VName]
local_arrs) ->
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"do one intra-group scan operation" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ do
let rets :: [TypeBase Shape NoUniqueness]
rets = Lambda KernelsMem -> [TypeBase Shape NoUniqueness]
forall lore. LambdaT lore -> [TypeBase Shape NoUniqueness]
lambdaReturnType Lambda KernelsMem
scan_op
scan_x_params :: [LParam KernelsMem]
scan_x_params = SegBinOp KernelsMem -> [LParam KernelsMem]
xParams SegBinOp KernelsMem
scan
(Bool
array_scan, Fence
fence, ImpM KernelsMem KernelEnv KernelOp ()
barrier) = Lambda KernelsMem
-> (Bool, Fence, ImpM KernelsMem KernelEnv KernelOp ())
barrierFor Lambda KernelsMem
scan_op
Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
array_scan ImpM KernelsMem KernelEnv KernelOp ()
barrier
Shape
-> ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Shape -> ([TExp Int32] -> ImpM lore r op ()) -> ImpM lore r op ()
sLoopNest Shape
vec_shape (([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \[TExp Int32]
vec_is -> do
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"maybe restore some to-scan values to parameters, or read neutral" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool
-> ImpM lore r op () -> ImpM lore r op () -> ImpM lore r op ()
sIf
TExp Bool
in_bounds
( do
[TExp Int32]
-> [PatElem KernelsMem]
-> SegBinOp KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
readToScanValues ((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids [TExp Int32] -> [TExp Int32] -> [TExp Int32]
forall a. [a] -> [a] -> [a]
++ [TExp Int32]
vec_is) [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes SegBinOp KernelsMem
scan
TExp Int32
-> [TExp Int32]
-> [TExp Int32]
-> [PatElem KernelsMem]
-> SegBinOp KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
readCarries (TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
chunk_offset) [TExp Int32]
dims' [TExp Int32]
vec_is [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes SegBinOp KernelsMem
scan
)
( [(Param (MemInfo SubExp NoUniqueness MemBind), SubExp)]
-> ((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [SubExp]
-> [(Param (MemInfo SubExp NoUniqueness MemBind), SubExp)]
forall a b. [a] -> [b] -> [(a, b)]
zip (SegBinOp KernelsMem -> [LParam KernelsMem]
yParams SegBinOp KernelsMem
scan) (SegBinOp KernelsMem -> [SubExp]
forall lore. SegBinOp lore -> [SubExp]
segBinOpNeutral SegBinOp KernelsMem
scan)) (((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind), SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, SubExp
ne) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix (Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p) [] SubExp
ne []
)
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"combine with carry and write to local memory" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
Names
-> Stms KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Names -> Stms lore -> ImpM lore r op () -> ImpM lore r op ()
compileStms Names
forall a. Monoid a => a
mempty (BodyT KernelsMem -> Stms KernelsMem
forall lore. BodyT lore -> Stms lore
bodyStms (BodyT KernelsMem -> Stms KernelsMem)
-> BodyT KernelsMem -> Stms KernelsMem
forall a b. (a -> b) -> a -> b
$ Lambda KernelsMem -> BodyT KernelsMem
forall lore. LambdaT lore -> BodyT lore
lambdaBody Lambda KernelsMem
scan_op) (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
[(TypeBase Shape NoUniqueness, VName, SubExp)]
-> ((TypeBase Shape NoUniqueness, VName, SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([TypeBase Shape NoUniqueness]
-> [VName]
-> [SubExp]
-> [(TypeBase Shape NoUniqueness, VName, SubExp)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [TypeBase Shape NoUniqueness]
rets [VName]
local_arrs (BodyT KernelsMem -> [SubExp]
forall lore. BodyT lore -> [SubExp]
bodyResult (BodyT KernelsMem -> [SubExp]) -> BodyT KernelsMem -> [SubExp]
forall a b. (a -> b) -> a -> b
$ Lambda KernelsMem -> BodyT KernelsMem
forall lore. LambdaT lore -> BodyT lore
lambdaBody Lambda KernelsMem
scan_op)) (((TypeBase Shape NoUniqueness, VName, SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((TypeBase Shape NoUniqueness, VName, SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
\(TypeBase Shape NoUniqueness
t, VName
arr, SubExp
se) -> VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix VName
arr [KernelConstants -> TypeBase Shape NoUniqueness -> TExp Int32
localArrayIndex KernelConstants
constants TypeBase Shape NoUniqueness
t] SubExp
se []
let crossesSegment' :: CrossesSegment
crossesSegment' = do
TExp Int32 -> TExp Int32 -> TExp Bool
f <- CrossesSegment
crossesSegment
(TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment
forall a. a -> Maybe a
Just ((TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment)
-> (TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment
forall a b. (a -> b) -> a -> b
$ \TExp Int32
from TExp Int32
to ->
let from' :: TExp Int32
from' = TExp Int32
from TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
chunk_offset
to' :: TExp Int32
to' = TExp Int32
to TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
chunk_offset
in TExp Int32 -> TExp Int32 -> TExp Bool
f TExp Int32
from' TExp Int32
to'
KernelOp -> ImpM KernelsMem KernelEnv KernelOp ()
forall op lore r. op -> ImpM lore r op ()
sOp (KernelOp -> ImpM KernelsMem KernelEnv KernelOp ())
-> KernelOp -> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ Fence -> KernelOp
Imp.ErrorSync Fence
fence
Lambda KernelsMem
scan_op_renamed <- Lambda KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp (Lambda KernelsMem)
forall lore (m :: * -> *).
(Renameable lore, MonadFreshNames m) =>
Lambda lore -> m (Lambda lore)
renameLambda Lambda KernelsMem
scan_op
CrossesSegment
-> TExp Int32
-> TExp Int32
-> Lambda KernelsMem
-> [VName]
-> ImpM KernelsMem KernelEnv KernelOp ()
groupScan
CrossesSegment
crossesSegment'
(TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
num_threads)
(KernelConstants -> TExp Int32
kernelGroupSize KernelConstants
constants)
Lambda KernelsMem
scan_op_renamed
[VName]
local_arrs
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"threads in bounds write partial scan result" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool -> ImpM lore r op () -> ImpM lore r op ()
sWhen TExp Bool
in_bounds (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
[(TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)]
-> ((TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([TypeBase Shape NoUniqueness]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [VName]
-> [(TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [TypeBase Shape NoUniqueness]
rets [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes [VName]
local_arrs) (((TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(TypeBase Shape NoUniqueness
t, PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe, VName
arr) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
(PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids [TExp Int32] -> [TExp Int32] -> [TExp Int32]
forall a. [a] -> [a] -> [a]
++ [TExp Int32]
vec_is)
(VName -> SubExp
Var VName
arr)
[KernelConstants -> TypeBase Shape NoUniqueness -> TExp Int32
localArrayIndex KernelConstants
constants TypeBase Shape NoUniqueness
t]
ImpM KernelsMem KernelEnv KernelOp ()
barrier
let load_carry :: ImpM KernelsMem KernelEnv KernelOp ()
load_carry =
[(VName, Param (MemInfo SubExp NoUniqueness MemBind))]
-> ((VName, Param (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([VName]
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> [(VName, Param (MemInfo SubExp NoUniqueness MemBind))]
forall a b. [a] -> [b] -> [(a, b)]
zip [VName]
local_arrs [LParam KernelsMem]
[Param (MemInfo SubExp NoUniqueness MemBind)]
scan_x_params) (((VName, Param (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((VName, Param (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(VName
arr, Param (MemInfo SubExp NoUniqueness MemBind)
p) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
(Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p)
[]
(VName -> SubExp
Var VName
arr)
[ if TypeBase Shape NoUniqueness -> Bool
forall shape u. TypeBase shape u -> Bool
primType (TypeBase Shape NoUniqueness -> Bool)
-> TypeBase Shape NoUniqueness -> Bool
forall a b. (a -> b) -> a -> b
$ Param (MemInfo SubExp NoUniqueness MemBind)
-> TypeBase Shape NoUniqueness
forall dec. Typed dec => Param dec -> TypeBase Shape NoUniqueness
paramType Param (MemInfo SubExp NoUniqueness MemBind)
p
then KernelConstants -> TExp Int32
kernelGroupSize KernelConstants
constants TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1
else (KernelConstants -> TExp Int32
kernelGroupId KernelConstants
constants TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ TExp Int32
1) TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* KernelConstants -> TExp Int32
kernelGroupSize KernelConstants
constants TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1
]
load_neutral :: ImpM KernelsMem KernelEnv KernelOp ()
load_neutral =
[(SubExp, Param (MemInfo SubExp NoUniqueness MemBind))]
-> ((SubExp, Param (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([SubExp]
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> [(SubExp, Param (MemInfo SubExp NoUniqueness MemBind))]
forall a b. [a] -> [b] -> [(a, b)]
zip [SubExp]
nes [LParam KernelsMem]
[Param (MemInfo SubExp NoUniqueness MemBind)]
scan_x_params) (((SubExp, Param (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((SubExp, Param (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(SubExp
ne, Param (MemInfo SubExp NoUniqueness MemBind)
p) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix (Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p) [] SubExp
ne []
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"first thread reads last element as carry-in for next iteration" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ do
TExp Bool
crosses_segment <- SpaceId
-> TExp Bool -> ImpM KernelsMem KernelEnv KernelOp (TExp Bool)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TExp t)
dPrimVE SpaceId
"crosses_segment" (TExp Bool -> ImpM KernelsMem KernelEnv KernelOp (TExp Bool))
-> TExp Bool -> ImpM KernelsMem KernelEnv KernelOp (TExp Bool)
forall a b. (a -> b) -> a -> b
$
case CrossesSegment
crossesSegment of
CrossesSegment
Nothing -> TExp Bool
forall v. TPrimExp Bool v
false
Just TExp Int32 -> TExp Int32 -> TExp Bool
f ->
TExp Int32 -> TExp Int32 -> TExp Bool
f
( TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
chunk_offset
TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ KernelConstants -> TExp Int32
kernelGroupSize KernelConstants
constants TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
-TExp Int32
1
)
( TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
chunk_offset
TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ KernelConstants -> TExp Int32
kernelGroupSize KernelConstants
constants
)
TExp Bool
should_load_carry <-
SpaceId
-> TExp Bool -> ImpM KernelsMem KernelEnv KernelOp (TExp Bool)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TExp t)
dPrimVE SpaceId
"should_load_carry" (TExp Bool -> ImpM KernelsMem KernelEnv KernelOp (TExp Bool))
-> TExp Bool -> ImpM KernelsMem KernelEnv KernelOp (TExp Bool)
forall a b. (a -> b) -> a -> b
$
KernelConstants -> TExp Int32
kernelLocalThreadId KernelConstants
constants TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. TExp Int32
0 TExp Bool -> TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
.&&. TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v
bNot TExp Bool
crosses_segment
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool -> ImpM lore r op () -> ImpM lore r op ()
sWhen TExp Bool
should_load_carry ImpM KernelsMem KernelEnv KernelOp ()
load_carry
Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
array_scan ImpM KernelsMem KernelEnv KernelOp ()
barrier
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool -> ImpM lore r op () -> ImpM lore r op ()
sUnless TExp Bool
should_load_carry ImpM KernelsMem KernelEnv KernelOp ()
load_neutral
ImpM KernelsMem KernelEnv KernelOp ()
barrier
(TV Int32, TExp Int32, CrossesSegment)
-> CallKernelGen (TV Int32, TExp Int32, CrossesSegment)
forall (m :: * -> *) a. Monad m => a -> m a
return (TV Int32
num_threads, TExp Int32
elems_per_group, CrossesSegment
crossesSegment)
scanStage2 ::
Pattern KernelsMem ->
TV Int32 ->
Imp.TExp Int32 ->
Count NumGroups SubExp ->
CrossesSegment ->
SegSpace ->
[SegBinOp KernelsMem] ->
CallKernelGen ()
scanStage2 :: Pattern KernelsMem
-> TV Int32
-> TExp Int32
-> Count NumGroups SubExp
-> CrossesSegment
-> SegSpace
-> [SegBinOp KernelsMem]
-> CallKernelGen ()
scanStage2 (Pattern [PatElem KernelsMem]
_ [PatElem KernelsMem]
all_pes) TV Int32
stage1_num_threads TExp Int32
elems_per_group Count NumGroups SubExp
num_groups CrossesSegment
crossesSegment SegSpace
space [SegBinOp KernelsMem]
scans = do
let ([VName]
gtids, [SubExp]
dims) = [(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
dims' :: [TExp Int32]
dims' = (SubExp -> TExp Int32) -> [SubExp] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp [SubExp]
dims
let group_size :: Count GroupSize SubExp
group_size = SubExp -> Count GroupSize SubExp
forall u e. e -> Count u e
Count (SubExp -> Count GroupSize SubExp)
-> SubExp -> Count GroupSize SubExp
forall a b. (a -> b) -> a -> b
$ Count NumGroups SubExp -> SubExp
forall u e. Count u e -> e
unCount Count NumGroups SubExp
num_groups
group_size' :: Count GroupSize (TExp Int32)
group_size' = (SubExp -> TExp Int32)
-> Count GroupSize SubExp -> Count GroupSize (TExp Int32)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp Count GroupSize SubExp
group_size
let crossesSegment' :: CrossesSegment
crossesSegment' = do
TExp Int32 -> TExp Int32 -> TExp Bool
f <- CrossesSegment
crossesSegment
(TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment
forall a. a -> Maybe a
Just ((TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment)
-> (TExp Int32 -> TExp Int32 -> TExp Bool) -> CrossesSegment
forall a b. (a -> b) -> a -> b
$ \TExp Int32
from TExp Int32
to ->
TExp Int32 -> TExp Int32 -> TExp Bool
f ((TExp Int32
from TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ TExp Int32
1) TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* TExp Int32
elems_per_group TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1) ((TExp Int32
to TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ TExp Int32
1) TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* TExp Int32
elems_per_group TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1)
SpaceId
-> Count NumGroups (TExp Int32)
-> Count GroupSize (TExp Int32)
-> VName
-> ImpM KernelsMem KernelEnv KernelOp ()
-> CallKernelGen ()
sKernelThread SpaceId
"scan_stage2" Count NumGroups (TExp Int32)
1 Count GroupSize (TExp Int32)
group_size' (SegSpace -> VName
segFlat SegSpace
space) (ImpM KernelsMem KernelEnv KernelOp () -> CallKernelGen ())
-> ImpM KernelsMem KernelEnv KernelOp () -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$ do
KernelConstants
constants <- KernelEnv -> KernelConstants
kernelConstants (KernelEnv -> KernelConstants)
-> ImpM KernelsMem KernelEnv KernelOp KernelEnv
-> ImpM KernelsMem KernelEnv KernelOp KernelConstants
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ImpM KernelsMem KernelEnv KernelOp KernelEnv
forall lore r op. ImpM lore r op r
askEnv
[[VName]]
per_scan_local_arrs <- Count GroupSize SubExp
-> SubExp -> [SegBinOp KernelsMem] -> InKernelGen [[VName]]
makeLocalArrays Count GroupSize SubExp
group_size (TV Int32 -> SubExp
forall t. TV t -> SubExp
tvSize TV Int32
stage1_num_threads) [SegBinOp KernelsMem]
scans
let per_scan_rets :: [[TypeBase Shape NoUniqueness]]
per_scan_rets = (SegBinOp KernelsMem -> [TypeBase Shape NoUniqueness])
-> [SegBinOp KernelsMem] -> [[TypeBase Shape NoUniqueness]]
forall a b. (a -> b) -> [a] -> [b]
map (Lambda KernelsMem -> [TypeBase Shape NoUniqueness]
forall lore. LambdaT lore -> [TypeBase Shape NoUniqueness]
lambdaReturnType (Lambda KernelsMem -> [TypeBase Shape NoUniqueness])
-> (SegBinOp KernelsMem -> Lambda KernelsMem)
-> SegBinOp KernelsMem
-> [TypeBase Shape NoUniqueness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegBinOp KernelsMem -> Lambda KernelsMem
forall lore. SegBinOp lore -> Lambda lore
segBinOpLambda) [SegBinOp KernelsMem]
scans
per_scan_pes :: [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
per_scan_pes = [SegBinOp KernelsMem]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
forall lore a. [SegBinOp lore] -> [a] -> [[a]]
segBinOpChunks [SegBinOp KernelsMem]
scans [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
all_pes
TV Int32
flat_idx <-
SpaceId
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"flat_idx" (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32))
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall a b. (a -> b) -> a -> b
$
(KernelConstants -> TExp Int32
kernelLocalThreadId KernelConstants
constants TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ TExp Int32
1) TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* TExp Int32
elems_per_group TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1
(VName -> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> [VName] -> [TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName -> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ()
forall t lore r op. VName -> TExp t -> ImpM lore r op ()
dPrimV_ [VName]
gtids ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> [TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ [TExp Int32] -> TExp Int32 -> [TExp Int32]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TExp Int32]
dims' (TExp Int32 -> [TExp Int32]) -> TExp Int32 -> [TExp Int32]
forall a b. (a -> b) -> a -> b
$ TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
flat_idx
[(SegBinOp KernelsMem, [VName], [TypeBase Shape NoUniqueness],
[PatElemT (MemInfo SubExp NoUniqueness MemBind)])]
-> ((SegBinOp KernelsMem, [VName], [TypeBase Shape NoUniqueness],
[PatElemT (MemInfo SubExp NoUniqueness MemBind)])
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([SegBinOp KernelsMem]
-> [[VName]]
-> [[TypeBase Shape NoUniqueness]]
-> [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
-> [(SegBinOp KernelsMem, [VName], [TypeBase Shape NoUniqueness],
[PatElemT (MemInfo SubExp NoUniqueness MemBind)])]
forall a b c d. [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)]
zip4 [SegBinOp KernelsMem]
scans [[VName]]
per_scan_local_arrs [[TypeBase Shape NoUniqueness]]
per_scan_rets [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
per_scan_pes) (((SegBinOp KernelsMem, [VName], [TypeBase Shape NoUniqueness],
[PatElemT (MemInfo SubExp NoUniqueness MemBind)])
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((SegBinOp KernelsMem, [VName], [TypeBase Shape NoUniqueness],
[PatElemT (MemInfo SubExp NoUniqueness MemBind)])
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
\(SegBinOp Commutativity
_ Lambda KernelsMem
scan_op [SubExp]
nes Shape
vec_shape, [VName]
local_arrs, [TypeBase Shape NoUniqueness]
rets, [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes) ->
Shape
-> ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Shape -> ([TExp Int32] -> ImpM lore r op ()) -> ImpM lore r op ()
sLoopNest Shape
vec_shape (([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \[TExp Int32]
vec_is -> do
let glob_is :: [TExp Int32]
glob_is = (VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids [TExp Int32] -> [TExp Int32] -> [TExp Int32]
forall a. [a] -> [a] -> [a]
++ [TExp Int32]
vec_is
in_bounds :: TExp Bool
in_bounds =
(TExp Bool -> TExp Bool -> TExp Bool) -> [TExp Bool] -> TExp Bool
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 TExp Bool -> TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
(.&&.) ([TExp Bool] -> TExp Bool) -> [TExp Bool] -> TExp Bool
forall a b. (a -> b) -> a -> b
$ (TExp Int32 -> TExp Int32 -> TExp Bool)
-> [TExp Int32] -> [TExp Int32] -> [TExp Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
(.<.) ((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids) [TExp Int32]
dims'
when_in_bounds :: ImpM KernelsMem KernelEnv KernelOp ()
when_in_bounds = [(TypeBase Shape NoUniqueness, VName,
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
-> ((TypeBase Shape NoUniqueness, VName,
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([TypeBase Shape NoUniqueness]
-> [VName]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [(TypeBase Shape NoUniqueness, VName,
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [TypeBase Shape NoUniqueness]
rets [VName]
local_arrs [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes) (((TypeBase Shape NoUniqueness, VName,
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((TypeBase Shape NoUniqueness, VName,
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(TypeBase Shape NoUniqueness
t, VName
arr, PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
VName
arr
[KernelConstants -> TypeBase Shape NoUniqueness -> TExp Int32
localArrayIndex KernelConstants
constants TypeBase Shape NoUniqueness
t]
(VName -> SubExp
Var (VName -> SubExp) -> VName -> SubExp
forall a b. (a -> b) -> a -> b
$ PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
[TExp Int32]
glob_is
when_out_of_bounds :: ImpM KernelsMem KernelEnv KernelOp ()
when_out_of_bounds = [(TypeBase Shape NoUniqueness, VName, SubExp)]
-> ((TypeBase Shape NoUniqueness, VName, SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([TypeBase Shape NoUniqueness]
-> [VName]
-> [SubExp]
-> [(TypeBase Shape NoUniqueness, VName, SubExp)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [TypeBase Shape NoUniqueness]
rets [VName]
local_arrs [SubExp]
nes) (((TypeBase Shape NoUniqueness, VName, SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((TypeBase Shape NoUniqueness, VName, SubExp)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(TypeBase Shape NoUniqueness
t, VName
arr, SubExp
ne) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix VName
arr [KernelConstants -> TypeBase Shape NoUniqueness -> TExp Int32
localArrayIndex KernelConstants
constants TypeBase Shape NoUniqueness
t] SubExp
ne []
(Bool
_, Fence
_, ImpM KernelsMem KernelEnv KernelOp ()
barrier) =
Lambda KernelsMem
-> (Bool, Fence, ImpM KernelsMem KernelEnv KernelOp ())
barrierFor Lambda KernelsMem
scan_op
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"threads in bound read carries; others get neutral element" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool
-> ImpM lore r op () -> ImpM lore r op () -> ImpM lore r op ()
sIf TExp Bool
in_bounds ImpM KernelsMem KernelEnv KernelOp ()
when_in_bounds ImpM KernelsMem KernelEnv KernelOp ()
when_out_of_bounds
ImpM KernelsMem KernelEnv KernelOp ()
barrier
CrossesSegment
-> TExp Int32
-> TExp Int32
-> Lambda KernelsMem
-> [VName]
-> ImpM KernelsMem KernelEnv KernelOp ()
groupScan
CrossesSegment
crossesSegment'
(TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
stage1_num_threads)
(KernelConstants -> TExp Int32
kernelGroupSize KernelConstants
constants)
Lambda KernelsMem
scan_op
[VName]
local_arrs
SpaceId
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op. SpaceId -> ImpM lore r op () -> ImpM lore r op ()
sComment SpaceId
"threads in bounds write scanned carries" (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool -> ImpM lore r op () -> ImpM lore r op ()
sWhen TExp Bool
in_bounds (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
[(TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)]
-> ((TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([TypeBase Shape NoUniqueness]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [VName]
-> [(TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [TypeBase Shape NoUniqueness]
rets [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes [VName]
local_arrs) (((TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((TypeBase Shape NoUniqueness,
PatElemT (MemInfo SubExp NoUniqueness MemBind), VName)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(TypeBase Shape NoUniqueness
t, PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe, VName
arr) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
(PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
[TExp Int32]
glob_is
(VName -> SubExp
Var VName
arr)
[KernelConstants -> TypeBase Shape NoUniqueness -> TExp Int32
localArrayIndex KernelConstants
constants TypeBase Shape NoUniqueness
t]
scanStage3 ::
Pattern KernelsMem ->
Count NumGroups SubExp ->
Count GroupSize SubExp ->
Imp.TExp Int32 ->
CrossesSegment ->
SegSpace ->
[SegBinOp KernelsMem] ->
CallKernelGen ()
scanStage3 :: Pattern KernelsMem
-> Count NumGroups SubExp
-> Count GroupSize SubExp
-> TExp Int32
-> CrossesSegment
-> SegSpace
-> [SegBinOp KernelsMem]
-> CallKernelGen ()
scanStage3 (Pattern [PatElem KernelsMem]
_ [PatElem KernelsMem]
all_pes) Count NumGroups SubExp
num_groups Count GroupSize SubExp
group_size TExp Int32
elems_per_group CrossesSegment
crossesSegment SegSpace
space [SegBinOp KernelsMem]
scans = do
let num_groups' :: Count NumGroups (TExp Int32)
num_groups' = (SubExp -> TExp Int32)
-> Count NumGroups SubExp -> Count NumGroups (TExp Int32)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp Count NumGroups SubExp
num_groups
group_size' :: Count GroupSize (TExp Int32)
group_size' = (SubExp -> TExp Int32)
-> Count GroupSize SubExp -> Count GroupSize (TExp Int32)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp Count GroupSize SubExp
group_size
([VName]
gtids, [SubExp]
dims) = [(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
dims' :: [TExp Int32]
dims' = (SubExp -> TExp Int32) -> [SubExp] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp [SubExp]
dims
TExp Int32
required_groups <-
SpaceId
-> TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TExp Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TExp t)
dPrimVE SpaceId
"required_groups" (TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TExp Int32))
-> TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TExp Int32)
forall a b. (a -> b) -> a -> b
$
[TExp Int32] -> TExp Int32
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [TExp Int32]
dims' TExp Int32 -> TExp Int32 -> TExp Int32
forall e. IntegralExp e => e -> e -> e
`divUp` Count GroupSize (TExp Int32) -> TExp Int32
forall u e. Count u e -> e
unCount Count GroupSize (TExp Int32)
group_size'
SpaceId
-> Count NumGroups (TExp Int32)
-> Count GroupSize (TExp Int32)
-> VName
-> ImpM KernelsMem KernelEnv KernelOp ()
-> CallKernelGen ()
sKernelThread SpaceId
"scan_stage3" Count NumGroups (TExp Int32)
num_groups' Count GroupSize (TExp Int32)
group_size' (SegSpace -> VName
segFlat SegSpace
space) (ImpM KernelsMem KernelEnv KernelOp () -> CallKernelGen ())
-> ImpM KernelsMem KernelEnv KernelOp () -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$
SegVirt
-> TExp Int32
-> (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
virtualiseGroups SegVirt
SegVirt TExp Int32
required_groups ((TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \TExp Int32
virt_group_id -> do
KernelConstants
constants <- KernelEnv -> KernelConstants
kernelConstants (KernelEnv -> KernelConstants)
-> ImpM KernelsMem KernelEnv KernelOp KernelEnv
-> ImpM KernelsMem KernelEnv KernelOp KernelConstants
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ImpM KernelsMem KernelEnv KernelOp KernelEnv
forall lore r op. ImpM lore r op r
askEnv
TExp Int32
flat_idx <-
SpaceId
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TExp Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TExp t)
dPrimVE SpaceId
"flat_idx" (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TExp Int32))
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TExp Int32)
forall a b. (a -> b) -> a -> b
$
TExp Int32
virt_group_id TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* Count GroupSize (TExp Int32) -> TExp Int32
forall u e. Count u e -> e
unCount Count GroupSize (TExp Int32)
group_size'
TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ KernelConstants -> TExp Int32
kernelLocalThreadId KernelConstants
constants
(VName -> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ())
-> [VName] -> [TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName -> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp ()
forall t lore r op. VName -> TExp t -> ImpM lore r op ()
dPrimV_ [VName]
gtids ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> [TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ [TExp Int32] -> TExp Int32 -> [TExp Int32]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TExp Int32]
dims' TExp Int32
flat_idx
TV Int32
orig_group <- SpaceId
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"orig_group" (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32))
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall a b. (a -> b) -> a -> b
$ TExp Int32
flat_idx TExp Int32 -> TExp Int32 -> TExp Int32
forall e. IntegralExp e => e -> e -> e
`quot` TExp Int32
elems_per_group
TV Int32
carry_in_flat_idx <-
SpaceId
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"carry_in_flat_idx" (TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32))
-> TExp Int32 -> ImpM KernelsMem KernelEnv KernelOp (TV Int32)
forall a b. (a -> b) -> a -> b
$
TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
orig_group TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* TExp Int32
elems_per_group TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1
let carry_in_idx :: [TExp Int32]
carry_in_idx = [TExp Int32] -> TExp Int32 -> [TExp Int32]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TExp Int32]
dims' (TExp Int32 -> [TExp Int32]) -> TExp Int32 -> [TExp Int32]
forall a b. (a -> b) -> a -> b
$ TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
carry_in_flat_idx
let in_bounds :: TExp Bool
in_bounds =
(TExp Bool -> TExp Bool -> TExp Bool) -> [TExp Bool] -> TExp Bool
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 TExp Bool -> TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
(.&&.) ([TExp Bool] -> TExp Bool) -> [TExp Bool] -> TExp Bool
forall a b. (a -> b) -> a -> b
$ (TExp Int32 -> TExp Int32 -> TExp Bool)
-> [TExp Int32] -> [TExp Int32] -> [TExp Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
(.<.) ((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids) [TExp Int32]
dims'
crosses_segment :: TExp Bool
crosses_segment =
TExp Bool -> Maybe (TExp Bool) -> TExp Bool
forall a. a -> Maybe a -> a
fromMaybe TExp Bool
forall v. TPrimExp Bool v
false (Maybe (TExp Bool) -> TExp Bool) -> Maybe (TExp Bool) -> TExp Bool
forall a b. (a -> b) -> a -> b
$
CrossesSegment
crossesSegment
CrossesSegment
-> Maybe (TExp Int32) -> Maybe (TExp Int32 -> TExp Bool)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TExp Int32 -> Maybe (TExp Int32)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
carry_in_flat_idx)
Maybe (TExp Int32 -> TExp Bool)
-> Maybe (TExp Int32) -> Maybe (TExp Bool)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TExp Int32 -> Maybe (TExp Int32)
forall (f :: * -> *) a. Applicative f => a -> f a
pure TExp Int32
flat_idx
is_a_carry :: TExp Bool
is_a_carry = TExp Int32
flat_idx TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. (TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
orig_group TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
+ TExp Int32
1) TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
* TExp Int32
elems_per_group TExp Int32 -> TExp Int32 -> TExp Int32
forall a. Num a => a -> a -> a
- TExp Int32
1
no_carry_in :: TExp Bool
no_carry_in = TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
orig_group TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. TExp Int32
0 TExp Bool -> TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
.||. TExp Bool
is_a_carry TExp Bool -> TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
.||. TExp Bool
crosses_segment
let per_scan_pes :: [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
per_scan_pes = [SegBinOp KernelsMem]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
forall lore a. [SegBinOp lore] -> [a] -> [[a]]
segBinOpChunks [SegBinOp KernelsMem]
scans [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
all_pes
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool -> ImpM lore r op () -> ImpM lore r op ()
sWhen TExp Bool
in_bounds (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
TExp Bool
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
TExp Bool -> ImpM lore r op () -> ImpM lore r op ()
sUnless TExp Bool
no_carry_in (ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
[([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem)]
-> (([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
-> [SegBinOp KernelsMem]
-> [([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem)]
forall a b. [a] -> [b] -> [(a, b)]
zip [[PatElemT (MemInfo SubExp NoUniqueness MemBind)]]
per_scan_pes [SegBinOp KernelsMem]
scans) ((([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> (([PatElemT (MemInfo SubExp NoUniqueness MemBind)],
SegBinOp KernelsMem)
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$
\([PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes, SegBinOp Commutativity
_ Lambda KernelsMem
scan_op [SubExp]
nes Shape
vec_shape) -> do
Maybe (Exp KernelsMem)
-> Scope KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Mem lore =>
Maybe (Exp lore) -> Scope lore -> ImpM lore r op ()
dScope Maybe (Exp KernelsMem)
forall a. Maybe a
Nothing (Scope KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ())
-> Scope KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ [Param (MemInfo SubExp NoUniqueness MemBind)] -> Scope KernelsMem
forall lore dec.
(LParamInfo lore ~ dec) =>
[Param dec] -> Scope lore
scopeOfLParams ([Param (MemInfo SubExp NoUniqueness MemBind)] -> Scope KernelsMem)
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> Scope KernelsMem
forall a b. (a -> b) -> a -> b
$ Lambda KernelsMem -> [LParam KernelsMem]
forall lore. LambdaT lore -> [LParam lore]
lambdaParams Lambda KernelsMem
scan_op
let ([Param (MemInfo SubExp NoUniqueness MemBind)]
scan_x_params, [Param (MemInfo SubExp NoUniqueness MemBind)]
scan_y_params) =
Int
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> ([Param (MemInfo SubExp NoUniqueness MemBind)],
[Param (MemInfo SubExp NoUniqueness MemBind)])
forall a. Int -> [a] -> ([a], [a])
splitAt ([SubExp] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SubExp]
nes) ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> ([Param (MemInfo SubExp NoUniqueness MemBind)],
[Param (MemInfo SubExp NoUniqueness MemBind)]))
-> [Param (MemInfo SubExp NoUniqueness MemBind)]
-> ([Param (MemInfo SubExp NoUniqueness MemBind)],
[Param (MemInfo SubExp NoUniqueness MemBind)])
forall a b. (a -> b) -> a -> b
$ Lambda KernelsMem -> [LParam KernelsMem]
forall lore. LambdaT lore -> [LParam lore]
lambdaParams Lambda KernelsMem
scan_op
Shape
-> ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Shape -> ([TExp Int32] -> ImpM lore r op ()) -> ImpM lore r op ()
sLoopNest Shape
vec_shape (([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ([TExp Int32] -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \[TExp Int32]
vec_is -> do
[(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param (MemInfo SubExp NoUniqueness MemBind)]
scan_x_params [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes) (((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
(Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p)
[]
(VName -> SubExp
Var (VName -> SubExp) -> VName -> SubExp
forall a b. (a -> b) -> a -> b
$ PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
([TExp Int32]
carry_in_idx [TExp Int32] -> [TExp Int32] -> [TExp Int32]
forall a. [a] -> [a] -> [a]
++ [TExp Int32]
vec_is)
[(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param (MemInfo SubExp NoUniqueness MemBind)]
scan_y_params [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes) (((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
(Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p)
[]
(VName -> SubExp
Var (VName -> SubExp) -> VName -> SubExp
forall a b. (a -> b) -> a -> b
$ PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids [TExp Int32] -> [TExp Int32] -> [TExp Int32]
forall a. [a] -> [a] -> [a]
++ [TExp Int32]
vec_is)
[Param (MemInfo SubExp NoUniqueness MemBind)]
-> BodyT KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ()
forall dec lore r op. [Param dec] -> Body lore -> ImpM lore r op ()
compileBody' [Param (MemInfo SubExp NoUniqueness MemBind)]
scan_x_params (BodyT KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ())
-> BodyT KernelsMem -> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ Lambda KernelsMem -> BodyT KernelsMem
forall lore. LambdaT lore -> BodyT lore
lambdaBody Lambda KernelsMem
scan_op
[(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Param (MemInfo SubExp NoUniqueness MemBind)]
-> [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
-> [(Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Param (MemInfo SubExp NoUniqueness MemBind)]
scan_x_params [PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes) (((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ((Param (MemInfo SubExp NoUniqueness MemBind),
PatElemT (MemInfo SubExp NoUniqueness MemBind))
-> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \(Param (MemInfo SubExp NoUniqueness MemBind)
p, PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe) ->
VName
-> [TExp Int32]
-> SubExp
-> [TExp Int32]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TExp Int32] -> SubExp -> [TExp Int32] -> ImpM lore r op ()
copyDWIMFix
(PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
((VName -> TExp Int32) -> [VName] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TExp Int32
Imp.vi32 [VName]
gtids [TExp Int32] -> [TExp Int32] -> [TExp Int32]
forall a. [a] -> [a] -> [a]
++ [TExp Int32]
vec_is)
(VName -> SubExp
Var (VName -> SubExp) -> VName -> SubExp
forall a b. (a -> b) -> a -> b
$ Param (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. Param dec -> VName
paramName Param (MemInfo SubExp NoUniqueness MemBind)
p)
[]
compileSegScan ::
Pattern KernelsMem ->
SegLevel ->
SegSpace ->
[SegBinOp KernelsMem] ->
KernelBody KernelsMem ->
CallKernelGen ()
compileSegScan :: Pattern KernelsMem
-> SegLevel
-> SegSpace
-> [SegBinOp KernelsMem]
-> KernelBody KernelsMem
-> CallKernelGen ()
compileSegScan Pattern KernelsMem
pat SegLevel
lvl SegSpace
space [SegBinOp KernelsMem]
scans KernelBody KernelsMem
kbody = TExp Bool -> CallKernelGen () -> CallKernelGen ()
forall lore r op.
TExp Bool -> ImpM lore r op () -> ImpM lore r op ()
sWhen (TExp Int32
0 TExp Int32 -> TExp Int32 -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.<. TExp Int32
n) (CallKernelGen () -> CallKernelGen ())
-> CallKernelGen () -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$ do
Code HostOp -> CallKernelGen ()
forall op lore r. Code op -> ImpM lore r op ()
emit (Code HostOp -> CallKernelGen ())
-> Code HostOp -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$ SpaceId -> Maybe Exp -> Code HostOp
forall a. SpaceId -> Maybe Exp -> Code a
Imp.DebugPrint SpaceId
"\n# SegScan" Maybe Exp
forall a. Maybe a
Nothing
TV Int32
stage1_max_num_groups <- SpaceId -> PrimType -> ImpM KernelsMem HostEnv HostOp (TV Int32)
forall lore r op t. SpaceId -> PrimType -> ImpM lore r op (TV t)
dPrim SpaceId
"stage1_max_num_groups" PrimType
int32
HostOp -> CallKernelGen ()
forall op lore r. op -> ImpM lore r op ()
sOp (HostOp -> CallKernelGen ()) -> HostOp -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$ VName -> SizeClass -> HostOp
Imp.GetSizeMax (TV Int32 -> VName
forall t. TV t -> VName
tvVar TV Int32
stage1_max_num_groups) SizeClass
SizeGroup
Count NumGroups SubExp
stage1_num_groups <-
(TV Int32 -> Count NumGroups SubExp)
-> ImpM KernelsMem HostEnv HostOp (TV Int32)
-> ImpM KernelsMem HostEnv HostOp (Count NumGroups SubExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SubExp -> Count NumGroups SubExp
forall u e. e -> Count u e
Imp.Count (SubExp -> Count NumGroups SubExp)
-> (TV Int32 -> SubExp) -> TV Int32 -> Count NumGroups SubExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TV Int32 -> SubExp
forall t. TV t -> SubExp
tvSize) (ImpM KernelsMem HostEnv HostOp (TV Int32)
-> ImpM KernelsMem HostEnv HostOp (Count NumGroups SubExp))
-> ImpM KernelsMem HostEnv HostOp (TV Int32)
-> ImpM KernelsMem HostEnv HostOp (Count NumGroups SubExp)
forall a b. (a -> b) -> a -> b
$
SpaceId -> TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TV Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"stage1_num_groups" (TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TV Int32))
-> TExp Int32 -> ImpM KernelsMem HostEnv HostOp (TV Int32)
forall a b. (a -> b) -> a -> b
$
TExp Int32 -> TExp Int32 -> TExp Int32
forall v. TPrimExp Int32 v -> TPrimExp Int32 v -> TPrimExp Int32 v
sMin32 (TV Int32 -> TExp Int32
forall t. TV t -> TExp t
tvExp TV Int32
stage1_max_num_groups) (TExp Int32 -> TExp Int32) -> TExp Int32 -> TExp Int32
forall a b. (a -> b) -> a -> b
$
SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp (SubExp -> TExp Int32) -> SubExp -> TExp Int32
forall a b. (a -> b) -> a -> b
$ Count NumGroups SubExp -> SubExp
forall u e. Count u e -> e
Imp.unCount (Count NumGroups SubExp -> SubExp)
-> Count NumGroups SubExp -> SubExp
forall a b. (a -> b) -> a -> b
$ SegLevel -> Count NumGroups SubExp
segNumGroups SegLevel
lvl
(TV Int32
stage1_num_threads, TExp Int32
elems_per_group, CrossesSegment
crossesSegment) <-
Pattern KernelsMem
-> Count NumGroups SubExp
-> Count GroupSize SubExp
-> SegSpace
-> [SegBinOp KernelsMem]
-> KernelBody KernelsMem
-> CallKernelGen (TV Int32, TExp Int32, CrossesSegment)
scanStage1 Pattern KernelsMem
pat Count NumGroups SubExp
stage1_num_groups (SegLevel -> Count GroupSize SubExp
segGroupSize SegLevel
lvl) SegSpace
space [SegBinOp KernelsMem]
scans KernelBody KernelsMem
kbody
Code HostOp -> CallKernelGen ()
forall op lore r. Code op -> ImpM lore r op ()
emit (Code HostOp -> CallKernelGen ())
-> Code HostOp -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$ SpaceId -> Maybe Exp -> Code HostOp
forall a. SpaceId -> Maybe Exp -> Code a
Imp.DebugPrint SpaceId
"elems_per_group" (Maybe Exp -> Code HostOp) -> Maybe Exp -> Code HostOp
forall a b. (a -> b) -> a -> b
$ Exp -> Maybe Exp
forall a. a -> Maybe a
Just (Exp -> Maybe Exp) -> Exp -> Maybe Exp
forall a b. (a -> b) -> a -> b
$ TExp Int32 -> Exp
forall t v. TPrimExp t v -> PrimExp v
untyped TExp Int32
elems_per_group
Pattern KernelsMem
-> TV Int32
-> TExp Int32
-> Count NumGroups SubExp
-> CrossesSegment
-> SegSpace
-> [SegBinOp KernelsMem]
-> CallKernelGen ()
scanStage2 Pattern KernelsMem
pat TV Int32
stage1_num_threads TExp Int32
elems_per_group Count NumGroups SubExp
stage1_num_groups CrossesSegment
crossesSegment SegSpace
space [SegBinOp KernelsMem]
scans
Pattern KernelsMem
-> Count NumGroups SubExp
-> Count GroupSize SubExp
-> TExp Int32
-> CrossesSegment
-> SegSpace
-> [SegBinOp KernelsMem]
-> CallKernelGen ()
scanStage3 Pattern KernelsMem
pat (SegLevel -> Count NumGroups SubExp
segNumGroups SegLevel
lvl) (SegLevel -> Count GroupSize SubExp
segGroupSize SegLevel
lvl) TExp Int32
elems_per_group CrossesSegment
crossesSegment SegSpace
space [SegBinOp KernelsMem]
scans
where
n :: TExp Int32
n = [TExp Int32] -> TExp Int32
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product ([TExp Int32] -> TExp Int32) -> [TExp Int32] -> TExp Int32
forall a b. (a -> b) -> a -> b
$ (SubExp -> TExp Int32) -> [SubExp] -> [TExp Int32]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TExp Int32
forall a. ToExp a => a -> TExp Int32
toInt32Exp ([SubExp] -> [TExp Int32]) -> [SubExp] -> [TExp Int32]
forall a b. (a -> b) -> a -> b
$ SegSpace -> [SubExp]
segSpaceDims SegSpace
space