module Futhark.CodeGen.ImpGen.GPU.SegScan (compileSegScan) where
import Futhark.CodeGen.ImpCode.GPU qualified as Imp
import Futhark.CodeGen.ImpGen hiding (compileProg)
import Futhark.CodeGen.ImpGen.GPU.Base
import Futhark.CodeGen.ImpGen.GPU.SegScan.SinglePass qualified as SinglePass
import Futhark.CodeGen.ImpGen.GPU.SegScan.TwoPass qualified as TwoPass
import Futhark.IR.GPUMem
combineScanOps :: [SegBinOp GPUMem] -> SegBinOp GPUMem
combineScanOps :: [SegBinOp GPUMem] -> SegBinOp GPUMem
combineScanOps [SegBinOp GPUMem]
ops =
SegBinOp
{ segBinOpComm :: Commutativity
segBinOpComm = [Commutativity] -> Commutativity
forall a. Monoid a => [a] -> a
mconcat ((SegBinOp GPUMem -> Commutativity)
-> [SegBinOp GPUMem] -> [Commutativity]
forall a b. (a -> b) -> [a] -> [b]
map SegBinOp GPUMem -> Commutativity
forall rep. SegBinOp rep -> Commutativity
segBinOpComm [SegBinOp GPUMem]
ops),
segBinOpLambda :: Lambda GPUMem
segBinOpLambda = Lambda GPUMem
lam',
segBinOpNeutral :: [SubExp]
segBinOpNeutral = (SegBinOp GPUMem -> [SubExp]) -> [SegBinOp GPUMem] -> [SubExp]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap SegBinOp GPUMem -> [SubExp]
forall rep. SegBinOp rep -> [SubExp]
segBinOpNeutral [SegBinOp GPUMem]
ops,
segBinOpShape :: Shape
segBinOpShape = Shape
forall a. Monoid a => a
mempty
}
where
lams :: [Lambda GPUMem]
lams = (SegBinOp GPUMem -> Lambda GPUMem)
-> [SegBinOp GPUMem] -> [Lambda GPUMem]
forall a b. (a -> b) -> [a] -> [b]
map SegBinOp GPUMem -> Lambda GPUMem
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda [SegBinOp GPUMem]
ops
xParams :: GLambda rep t -> [Param (LParamInfo rep)]
xParams GLambda rep t
lam = Int -> [Param (LParamInfo rep)] -> [Param (LParamInfo rep)]
forall a. Int -> [a] -> [a]
take ([t] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (GLambda rep t -> [t]
forall rep t. GLambda rep t -> [t]
lambdaReturnType GLambda rep t
lam)) (GLambda rep t -> [Param (LParamInfo rep)]
forall rep t. GLambda rep t -> [LParam rep]
lambdaParams GLambda rep t
lam)
yParams :: GLambda rep t -> [Param (LParamInfo rep)]
yParams GLambda rep t
lam = Int -> [Param (LParamInfo rep)] -> [Param (LParamInfo rep)]
forall a. Int -> [a] -> [a]
drop ([t] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (GLambda rep t -> [t]
forall rep t. GLambda rep t -> [t]
lambdaReturnType GLambda rep t
lam)) (GLambda rep t -> [Param (LParamInfo rep)]
forall rep t. GLambda rep t -> [LParam rep]
lambdaParams GLambda rep t
lam)
lam' :: Lambda GPUMem
lam' =
Lambda
{ lambdaParams :: [LParam GPUMem]
lambdaParams = (Lambda GPUMem -> [Param LParamMem])
-> [Lambda GPUMem] -> [Param LParamMem]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Lambda GPUMem -> [LParam GPUMem]
Lambda GPUMem -> [Param LParamMem]
forall rep t. GLambda rep t -> [LParam rep]
xParams [Lambda GPUMem]
lams [Param LParamMem] -> [Param LParamMem] -> [Param LParamMem]
forall a. [a] -> [a] -> [a]
++ (Lambda GPUMem -> [Param LParamMem])
-> [Lambda GPUMem] -> [Param LParamMem]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Lambda GPUMem -> [LParam GPUMem]
Lambda GPUMem -> [Param LParamMem]
forall rep t. GLambda rep t -> [LParam rep]
yParams [Lambda GPUMem]
lams,
lambdaReturnType :: [Type]
lambdaReturnType = (Lambda GPUMem -> [Type]) -> [Lambda GPUMem] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Lambda GPUMem -> [Type]
forall rep t. GLambda rep t -> [t]
lambdaReturnType [Lambda GPUMem]
lams,
lambdaBody :: Body GPUMem
lambdaBody =
BodyDec GPUMem -> Stms GPUMem -> [SubExpRes] -> Body GPUMem
forall rep res. BodyDec rep -> Stms rep -> [res] -> GBody rep res
Body
()
([Stms GPUMem] -> Stms GPUMem
forall a. Monoid a => [a] -> a
mconcat ((Lambda GPUMem -> Stms GPUMem) -> [Lambda GPUMem] -> [Stms GPUMem]
forall a b. (a -> b) -> [a] -> [b]
map (Body GPUMem -> Stms GPUMem
forall rep res. GBody rep res -> Stms rep
bodyStms (Body GPUMem -> Stms GPUMem)
-> (Lambda GPUMem -> Body GPUMem) -> Lambda GPUMem -> Stms GPUMem
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lambda GPUMem -> Body GPUMem
forall rep t. GLambda rep t -> Body rep
lambdaBody) [Lambda GPUMem]
lams))
((Lambda GPUMem -> [SubExpRes]) -> [Lambda GPUMem] -> [SubExpRes]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Body GPUMem -> [SubExpRes]
forall rep res. GBody rep res -> [res]
bodyResult (Body GPUMem -> [SubExpRes])
-> (Lambda GPUMem -> Body GPUMem) -> Lambda GPUMem -> [SubExpRes]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lambda GPUMem -> Body GPUMem
forall rep t. GLambda rep t -> Body rep
lambdaBody) [Lambda GPUMem]
lams)
}
canBeSinglePass :: [SegBinOp GPUMem] -> Maybe (SegBinOp GPUMem)
canBeSinglePass :: [SegBinOp GPUMem] -> Maybe (SegBinOp GPUMem)
canBeSinglePass [SegBinOp GPUMem]
scan_ops =
if (SegBinOp GPUMem -> Bool) -> [SegBinOp GPUMem] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all SegBinOp GPUMem -> Bool
forall {rep}. ASTRep rep => SegBinOp rep -> Bool
ok [SegBinOp GPUMem]
scan_ops
then SegBinOp GPUMem -> Maybe (SegBinOp GPUMem)
forall a. a -> Maybe a
Just (SegBinOp GPUMem -> Maybe (SegBinOp GPUMem))
-> SegBinOp GPUMem -> Maybe (SegBinOp GPUMem)
forall a b. (a -> b) -> a -> b
$ [SegBinOp GPUMem] -> SegBinOp GPUMem
combineScanOps [SegBinOp GPUMem]
scan_ops
else Maybe (SegBinOp GPUMem)
forall a. Maybe a
Nothing
where
ok :: SegBinOp rep -> Bool
ok SegBinOp rep
op =
SegBinOp rep -> Shape
forall rep. SegBinOp rep -> Shape
segBinOpShape SegBinOp rep
op Shape -> Shape -> Bool
forall a. Eq a => a -> a -> Bool
== Shape
forall a. Monoid a => a
mempty
Bool -> Bool -> Bool
&& (Type -> Bool) -> [Type] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Type -> Bool
forall shape o. TypeBase shape o -> Bool
primType (GLambda rep Type -> [Type]
forall rep t. GLambda rep t -> [t]
lambdaReturnType (SegBinOp rep -> GLambda rep Type
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp rep
op))
Bool -> Bool -> Bool
&& Bool -> Bool
not (Body rep -> Bool
forall rep. ASTRep rep => Body rep -> Bool
bodyMayFail (GLambda rep Type -> Body rep
forall rep t. GLambda rep t -> Body rep
lambdaBody (SegBinOp rep -> GLambda rep Type
forall rep. SegBinOp rep -> Lambda rep
segBinOpLambda SegBinOp rep
op)))
compileSegScan ::
Pat LetDecMem ->
SegLevel ->
SegSpace ->
[Type] ->
KernelBody GPUMem ->
[SegBinOp GPUMem] ->
SegPostOp GPUMem ->
CallKernelGen ()
compileSegScan :: Pat LParamMem
-> SegLevel
-> SegSpace
-> [Type]
-> KernelBody GPUMem
-> [SegBinOp GPUMem]
-> SegPostOp GPUMem
-> CallKernelGen ()
compileSegScan Pat LParamMem
pat SegLevel
lvl SegSpace
space [Type]
ts KernelBody GPUMem
map_kbody [SegBinOp GPUMem]
scan_ops SegPostOp GPUMem
post_op =
TExp Bool -> CallKernelGen () -> CallKernelGen ()
forall rep r op. TExp Bool -> ImpM rep r op () -> ImpM rep r op ()
sWhen (TPrimExp Int64 VName
0 TPrimExp Int64 VName -> TPrimExp Int64 VName -> TExp Bool
forall {k} v (t :: k).
Eq v =>
TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.<. TPrimExp Int64 VName
n) (CallKernelGen () -> CallKernelGen ())
-> CallKernelGen () -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$ do
Code HostOp -> CallKernelGen ()
forall op rep r. Code op -> ImpM rep r op ()
emit (Code HostOp -> CallKernelGen ())
-> Code HostOp -> CallKernelGen ()
forall a b. (a -> b) -> a -> b
$ String -> Maybe Exp -> Code HostOp
forall a. String -> Maybe Exp -> Code a
Imp.DebugPrint String
"\n# SegScan" Maybe Exp
forall a. Maybe a
Nothing
target <- HostEnv -> Target
hostTarget (HostEnv -> Target)
-> ImpM GPUMem HostEnv HostOp HostEnv
-> ImpM GPUMem HostEnv HostOp Target
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ImpM GPUMem HostEnv HostOp HostEnv
forall rep r op. ImpM rep r op r
askEnv
case (targetSupportsSinglePass target, canBeSinglePass scan_ops) of
(Bool
True, Just SegBinOp GPUMem
scan_ops') ->
Pat LParamMem
-> SegLevel
-> SegSpace
-> [Type]
-> SegBinOp GPUMem
-> KernelBody GPUMem
-> SegPostOp GPUMem
-> CallKernelGen ()
SinglePass.compileSegScan Pat LParamMem
pat SegLevel
lvl SegSpace
space [Type]
ts SegBinOp GPUMem
scan_ops' KernelBody GPUMem
map_kbody SegPostOp GPUMem
post_op
(Bool, Maybe (SegBinOp GPUMem))
_ ->
Pat LParamMem
-> SegLevel
-> SegSpace
-> [Type]
-> [SegBinOp GPUMem]
-> KernelBody GPUMem
-> SegPostOp GPUMem
-> CallKernelGen ()
TwoPass.compileSegScan Pat LParamMem
pat SegLevel
lvl SegSpace
space [Type]
ts [SegBinOp GPUMem]
scan_ops KernelBody GPUMem
map_kbody SegPostOp GPUMem
post_op
emit $ Imp.DebugPrint "" Nothing
where
n :: TPrimExp Int64 VName
n = [TPrimExp Int64 VName] -> TPrimExp Int64 VName
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product ([TPrimExp Int64 VName] -> TPrimExp Int64 VName)
-> [TPrimExp Int64 VName] -> TPrimExp Int64 VName
forall a b. (a -> b) -> a -> b
$ (SubExp -> TPrimExp Int64 VName)
-> [SubExp] -> [TPrimExp Int64 VName]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TPrimExp Int64 VName
pe64 ([SubExp] -> [TPrimExp Int64 VName])
-> [SubExp] -> [TPrimExp Int64 VName]
forall a b. (a -> b) -> a -> b
$ SegSpace -> [SubExp]
segSpaceDims SegSpace
space
targetSupportsSinglePass :: Target -> Bool
targetSupportsSinglePass Target
CUDA = Bool
True
targetSupportsSinglePass Target
HIP = Bool
True
targetSupportsSinglePass Target
_ = Bool
False