{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}

-- | Code generation for segmented and non-segmented scans.  Uses a
-- fairly inefficient two-pass algorithm, but can handle anything.
module Futhark.CodeGen.ImpGen.Kernels.SegScan.TwoPass (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)

-- Aggressively try to reuse memory for different SegBinOps, because
-- we will run them sequentially after another.
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 Int64 VName) -> IxFun
forall num. IntegralExp num => Shape num -> IxFun num
IxFun.iota (Shape (TPrimExp Int64 VName) -> IxFun)
-> Shape (TPrimExp Int64 VName) -> IxFun
forall a b. (a -> b) -> a -> b
$ (SubExp -> TPrimExp Int64 VName)
-> [SubExp] -> Shape (TPrimExp Int64 VName)
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TPrimExp Int64 VName
pe64 ([SubExp] -> Shape (TPrimExp Int64 VName))
-> [SubExp] -> Shape (TPrimExp Int64 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 Int64 -> Imp.TExp Int64 -> Imp.TExp Bool)

localArrayIndex :: KernelConstants -> Type -> Imp.TExp Int64
localArrayIndex :: KernelConstants
-> TypeBase Shape NoUniqueness -> TPrimExp Int64 ExpLeaf
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 TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int32 ExpLeaf
kernelLocalThreadId KernelConstants
constants)
    else TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int32 ExpLeaf
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM lore r op ()
copyDWIMFix
        (PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
        ((VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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 Int64] ->
  [PatElem KernelsMem] ->
  SegBinOp KernelsMem ->
  InKernelGen ()
readToScanValues :: [TPrimExp Int64 ExpLeaf]
-> [PatElem KernelsMem]
-> SegBinOp KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
readToScanValues [TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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)) [TPrimExp Int64 ExpLeaf]
is
  | Bool
otherwise =
    () -> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

readCarries ::
  Imp.TExp Int64 ->
  [Imp.TExp Int64] ->
  [Imp.TExp Int64] ->
  [PatElem KernelsMem] ->
  SegBinOp KernelsMem ->
  InKernelGen ()
readCarries :: TPrimExp Int64 ExpLeaf
-> [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf]
-> [PatElem KernelsMem]
-> SegBinOp KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
readCarries TPrimExp Int64 ExpLeaf
chunk_offset [TPrimExp Int64 ExpLeaf]
dims' [TPrimExp Int64 ExpLeaf]
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
    TPrimExp Int32 ExpLeaf
ltid <- KernelConstants -> TPrimExp Int32 ExpLeaf
kernelLocalThreadId (KernelConstants -> TPrimExp Int32 ExpLeaf)
-> (KernelEnv -> KernelConstants)
-> KernelEnv
-> TPrimExp Int32 ExpLeaf
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KernelEnv -> KernelConstants
kernelConstants (KernelEnv -> TPrimExp Int32 ExpLeaf)
-> ImpM KernelsMem KernelEnv KernelOp KernelEnv
-> ImpM KernelsMem KernelEnv KernelOp (TPrimExp Int32 ExpLeaf)
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
    -- We may have to reload the carries from the output of the
    -- previous chunk.
    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
      (TPrimExp Int64 ExpLeaf
chunk_offset TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.>. TPrimExp Int64 ExpLeaf
0 TExp Bool -> TExp Bool -> TExp Bool
forall v. TPrimExp Bool v -> TPrimExp Bool v -> TPrimExp Bool v
.&&. TPrimExp Int32 ExpLeaf
ltid TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. TPrimExp Int32 ExpLeaf
0)
      ( do
          let is :: [TPrimExp Int64 ExpLeaf]
is = [TPrimExp Int64 ExpLeaf]
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TPrimExp Int64 ExpLeaf]
dims' (TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf])
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> a -> b
$ TPrimExp Int64 ExpLeaf
chunk_offset TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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)) ([TPrimExp Int64 ExpLeaf]
is [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf] -> [TPrimExp Int64 ExpLeaf]
forall a. [a] -> [a] -> [a]
++ [TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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 ()

-- | Produce partially scanned intervals; one per workgroup.
scanStage1 ::
  Pattern KernelsMem ->
  Count NumGroups SubExp ->
  Count GroupSize SubExp ->
  SegSpace ->
  [SegBinOp KernelsMem] ->
  KernelBody KernelsMem ->
  CallKernelGen (TV Int32, Imp.TExp Int64, CrossesSegment)
scanStage1 :: Pattern KernelsMem
-> Count NumGroups SubExp
-> Count GroupSize SubExp
-> SegSpace
-> [SegBinOp KernelsMem]
-> KernelBody KernelsMem
-> CallKernelGen (TV Int32, TPrimExp Int64 ExpLeaf, 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 (TPrimExp Int64 ExpLeaf)
num_groups' = (SubExp -> TPrimExp Int64 ExpLeaf)
-> Count NumGroups SubExp
-> Count NumGroups (TPrimExp Int64 ExpLeaf)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp Count NumGroups SubExp
num_groups
      group_size' :: Count GroupSize (TPrimExp Int64 ExpLeaf)
group_size' = (SubExp -> TPrimExp Int64 ExpLeaf)
-> Count GroupSize SubExp
-> Count GroupSize (TPrimExp Int64 ExpLeaf)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp Count GroupSize SubExp
group_size
  TV Int32
num_threads <- SpaceId
-> TPrimExp Int32 ExpLeaf
-> ImpM KernelsMem HostEnv HostOp (TV Int32)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"num_threads" (TPrimExp Int32 ExpLeaf
 -> ImpM KernelsMem HostEnv HostOp (TV Int32))
-> TPrimExp Int32 ExpLeaf
-> ImpM KernelsMem HostEnv HostOp (TV Int32)
forall a b. (a -> b) -> a -> b
$ TPrimExp Int64 ExpLeaf -> TPrimExp Int32 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int32 v
sExt32 (TPrimExp Int64 ExpLeaf -> TPrimExp Int32 ExpLeaf)
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int32 ExpLeaf
forall a b. (a -> b) -> a -> b
$ Count NumGroups (TPrimExp Int64 ExpLeaf) -> TPrimExp Int64 ExpLeaf
forall u e. Count u e -> e
unCount Count NumGroups (TPrimExp Int64 ExpLeaf)
num_groups' TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* Count GroupSize (TPrimExp Int64 ExpLeaf) -> TPrimExp Int64 ExpLeaf
forall u e. Count u e -> e
unCount Count GroupSize (TPrimExp Int64 ExpLeaf)
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' :: [TPrimExp Int64 ExpLeaf]
dims' = (SubExp -> TPrimExp Int64 ExpLeaf)
-> [SubExp] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp [SubExp]
dims
  let num_elements :: TPrimExp Int64 ExpLeaf
num_elements = [TPrimExp Int64 ExpLeaf] -> TPrimExp Int64 ExpLeaf
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [TPrimExp Int64 ExpLeaf]
dims'
      elems_per_thread :: TPrimExp Int64 ExpLeaf
elems_per_thread = TPrimExp Int64 ExpLeaf
num_elements TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall e. IntegralExp e => e -> e -> e
`divUp` TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (TV Int32 -> TPrimExp Int32 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int32
num_threads)
      elems_per_group :: TPrimExp Int64 ExpLeaf
elems_per_group = Count GroupSize (TPrimExp Int64 ExpLeaf) -> TPrimExp Int64 ExpLeaf
forall u e. Count u e -> e
unCount Count GroupSize (TPrimExp Int64 ExpLeaf)
group_size' TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf
elems_per_thread

  let crossesSegment :: CrossesSegment
crossesSegment =
        case [TPrimExp Int64 ExpLeaf] -> [TPrimExp Int64 ExpLeaf]
forall a. [a] -> [a]
reverse [TPrimExp Int64 ExpLeaf]
dims' of
          TPrimExp Int64 ExpLeaf
segment_size : TPrimExp Int64 ExpLeaf
_ : [TPrimExp Int64 ExpLeaf]
_ -> (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool)
-> CrossesSegment
forall a. a -> Maybe a
Just ((TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool)
 -> CrossesSegment)
-> (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool)
-> CrossesSegment
forall a b. (a -> b) -> a -> b
$ \TPrimExp Int64 ExpLeaf
from TPrimExp Int64 ExpLeaf
to ->
            (TPrimExp Int64 ExpLeaf
to TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
from) TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.>. (TPrimExp Int64 ExpLeaf
to TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall e. IntegralExp e => e -> e -> e
`rem` TPrimExp Int64 ExpLeaf
segment_size)
          [TPrimExp Int64 ExpLeaf]
_ -> CrossesSegment
forall a. Maybe a
Nothing

  SpaceId
-> Count NumGroups (TPrimExp Int64 ExpLeaf)
-> Count GroupSize (TPrimExp Int64 ExpLeaf)
-> VName
-> ImpM KernelsMem KernelEnv KernelOp ()
-> CallKernelGen ()
sKernelThread SpaceId
"scan_stage1" Count NumGroups (TPrimExp Int64 ExpLeaf)
num_groups' Count GroupSize (TPrimExp Int64 ExpLeaf)
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

    -- The variables from scan_op will be used for the carry and such
    -- in the big chunking loop.
    [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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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
-> TPrimExp Int64 ExpLeaf
-> (TPrimExp Int64 ExpLeaf
    -> 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" TPrimExp Int64 ExpLeaf
elems_per_thread ((TPrimExp Int64 ExpLeaf -> ImpM KernelsMem KernelEnv KernelOp ())
 -> ImpM KernelsMem KernelEnv KernelOp ())
-> (TPrimExp Int64 ExpLeaf
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \TPrimExp Int64 ExpLeaf
j -> do
      TV Int64
chunk_offset <-
        SpaceId
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"chunk_offset" (TPrimExp Int64 ExpLeaf
 -> ImpM KernelsMem KernelEnv KernelOp (TV Int64))
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall a b. (a -> b) -> a -> b
$
          TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int64 ExpLeaf
kernelGroupSize KernelConstants
constants) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf
j
            TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int32 ExpLeaf
kernelGroupId KernelConstants
constants) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf
elems_per_group
      TV Int64
flat_idx <-
        SpaceId
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"flat_idx" (TPrimExp Int64 ExpLeaf
 -> ImpM KernelsMem KernelEnv KernelOp (TV Int64))
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall a b. (a -> b) -> a -> b
$
          TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
chunk_offset TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int32 ExpLeaf
kernelLocalThreadId KernelConstants
constants)
      -- Construct segment indices.
      (VName
 -> TPrimExp Int64 ExpLeaf -> ImpM KernelsMem KernelEnv KernelOp ())
-> [VName]
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName
-> TPrimExp Int64 ExpLeaf -> ImpM KernelsMem KernelEnv KernelOp ()
forall t lore r op. VName -> TExp t -> ImpM lore r op ()
dPrimV_ [VName]
gtids ([TPrimExp Int64 ExpLeaf] -> ImpM KernelsMem KernelEnv KernelOp ())
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ [TPrimExp Int64 ExpLeaf]
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TPrimExp Int64 ExpLeaf]
dims' (TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf])
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> a -> b
$ TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
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
$ (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool)
-> [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf]
-> [TExp Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
(.<.) ((VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [VName]
gtids) [TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM lore r op ()
copyDWIMFix
                  (PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
                  ((VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [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
-> ([TPrimExp Int64 ExpLeaf]
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Shape
-> ([TPrimExp Int64 ExpLeaf] -> ImpM lore r op ())
-> ImpM lore r op ()
sLoopNest Shape
vec_shape (([TPrimExp Int64 ExpLeaf]
  -> ImpM KernelsMem KernelEnv KernelOp ())
 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ([TPrimExp Int64 ExpLeaf]
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \[TPrimExp Int64 ExpLeaf]
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
                      [TPrimExp Int64 ExpLeaf]
-> [PatElem KernelsMem]
-> SegBinOp KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
readToScanValues ((VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [VName]
gtids [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf] -> [TPrimExp Int64 ExpLeaf]
forall a. [a] -> [a] -> [a]
++ [TPrimExp Int64 ExpLeaf]
vec_is) [PatElem KernelsMem]
[PatElemT (MemInfo SubExp NoUniqueness MemBind)]
pes SegBinOp KernelsMem
scan
                      TPrimExp Int64 ExpLeaf
-> [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf]
-> [PatElem KernelsMem]
-> SegBinOp KernelsMem
-> ImpM KernelsMem KernelEnv KernelOp ()
readCarries (TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
chunk_offset) [TPrimExp Int64 ExpLeaf]
dims' [TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM lore r op ()
copyDWIMFix VName
arr [KernelConstants
-> TypeBase Shape NoUniqueness -> TPrimExp Int64 ExpLeaf
localArrayIndex KernelConstants
constants TypeBase Shape NoUniqueness
t] SubExp
se []

              let crossesSegment' :: Maybe
  (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
crossesSegment' = do
                    TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
f <- CrossesSegment
crossesSegment
                    (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
-> Maybe
     (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
forall a. a -> Maybe a
Just ((TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
 -> Maybe
      (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool))
-> (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
-> Maybe
     (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
forall a b. (a -> b) -> a -> b
$ \TPrimExp Int32 ExpLeaf
from TPrimExp Int32 ExpLeaf
to ->
                      let from' :: TPrimExp Int64 ExpLeaf
from' = TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TPrimExp Int32 ExpLeaf
from TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
chunk_offset
                          to' :: TPrimExp Int64 ExpLeaf
to' = TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TPrimExp Int32 ExpLeaf
to TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
chunk_offset
                       in TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
f TPrimExp Int64 ExpLeaf
from' TPrimExp Int64 ExpLeaf
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

              -- We need to avoid parameter name clashes.
              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
              Maybe
  (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
-> TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf
-> Lambda KernelsMem
-> [VName]
-> ImpM KernelsMem KernelEnv KernelOp ()
groupScan
                Maybe
  (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
crossesSegment'
                (TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf)
-> TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a b. (a -> b) -> a -> b
$ TV Int32 -> TPrimExp Int32 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int32
num_threads)
                (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf)
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a b. (a -> b) -> a -> b
$ KernelConstants -> TPrimExp Int64 ExpLeaf
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM lore r op ()
copyDWIMFix
                      (PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
                      ((VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [VName]
gtids [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf] -> [TPrimExp Int64 ExpLeaf]
forall a. [a] -> [a] -> [a]
++ [TPrimExp Int64 ExpLeaf]
vec_is)
                      (VName -> SubExp
Var VName
arr)
                      [KernelConstants
-> TypeBase Shape NoUniqueness -> TPrimExp Int64 ExpLeaf
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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 TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int64 ExpLeaf
kernelGroupSize KernelConstants
constants) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
1
                            else
                              (TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int32 ExpLeaf
kernelGroupId KernelConstants
constants) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int64 ExpLeaf
1)
                                TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int64 ExpLeaf
kernelGroupSize KernelConstants
constants) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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 TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
f ->
                      TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
f
                        ( TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
chunk_offset
                            TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int64 ExpLeaf
kernelGroupSize KernelConstants
constants) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
-TPrimExp Int64 ExpLeaf
1
                        )
                        ( TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
chunk_offset
                            TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int64 ExpLeaf
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 -> TPrimExp Int32 ExpLeaf
kernelLocalThreadId KernelConstants
constants TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. TPrimExp Int32 ExpLeaf
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, TPrimExp Int64 ExpLeaf, CrossesSegment)
-> CallKernelGen (TV Int32, TPrimExp Int64 ExpLeaf, CrossesSegment)
forall (m :: * -> *) a. Monad m => a -> m a
return (TV Int32
num_threads, TPrimExp Int64 ExpLeaf
elems_per_group, CrossesSegment
crossesSegment)

scanStage2 ::
  Pattern KernelsMem ->
  TV Int32 ->
  Imp.TExp Int64 ->
  Count NumGroups SubExp ->
  CrossesSegment ->
  SegSpace ->
  [SegBinOp KernelsMem] ->
  CallKernelGen ()
scanStage2 :: Pattern KernelsMem
-> TV Int32
-> TPrimExp Int64 ExpLeaf
-> Count NumGroups SubExp
-> CrossesSegment
-> SegSpace
-> [SegBinOp KernelsMem]
-> CallKernelGen ()
scanStage2 (Pattern [PatElem KernelsMem]
_ [PatElem KernelsMem]
all_pes) TV Int32
stage1_num_threads TPrimExp Int64 ExpLeaf
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' :: [TPrimExp Int64 ExpLeaf]
dims' = (SubExp -> TPrimExp Int64 ExpLeaf)
-> [SubExp] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp [SubExp]
dims

  -- Our group size is the number of groups for the stage 1 kernel.
  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 (TPrimExp Int64 ExpLeaf)
group_size' = (SubExp -> TPrimExp Int64 ExpLeaf)
-> Count GroupSize SubExp
-> Count GroupSize (TPrimExp Int64 ExpLeaf)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp Count GroupSize SubExp
group_size

  let crossesSegment' :: Maybe
  (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
crossesSegment' = do
        TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
f <- CrossesSegment
crossesSegment
        (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
-> Maybe
     (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
forall a. a -> Maybe a
Just ((TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
 -> Maybe
      (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool))
-> (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
-> Maybe
     (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
forall a b. (a -> b) -> a -> b
$ \TPrimExp Int32 ExpLeaf
from TPrimExp Int32 ExpLeaf
to ->
          TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
f
            ((TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TPrimExp Int32 ExpLeaf
from TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int64 ExpLeaf
1) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf
elems_per_group TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
1)
            ((TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TPrimExp Int32 ExpLeaf
to TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int64 ExpLeaf
1) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf
elems_per_group TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
1)

  SpaceId
-> Count NumGroups (TPrimExp Int64 ExpLeaf)
-> Count GroupSize (TPrimExp Int64 ExpLeaf)
-> VName
-> ImpM KernelsMem KernelEnv KernelOp ()
-> CallKernelGen ()
sKernelThread SpaceId
"scan_stage2" Count NumGroups (TPrimExp Int64 ExpLeaf)
1 Count GroupSize (TPrimExp Int64 ExpLeaf)
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 Int64
flat_idx <-
      SpaceId
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"flat_idx" (TPrimExp Int64 ExpLeaf
 -> ImpM KernelsMem KernelEnv KernelOp (TV Int64))
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall a b. (a -> b) -> a -> b
$
        (TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int32 ExpLeaf
kernelLocalThreadId KernelConstants
constants) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int64 ExpLeaf
1) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf
elems_per_group TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
1
    -- Construct segment indices.
    (VName
 -> TPrimExp Int64 ExpLeaf -> ImpM KernelsMem KernelEnv KernelOp ())
-> [VName]
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName
-> TPrimExp Int64 ExpLeaf -> ImpM KernelsMem KernelEnv KernelOp ()
forall t lore r op. VName -> TExp t -> ImpM lore r op ()
dPrimV_ [VName]
gtids ([TPrimExp Int64 ExpLeaf] -> ImpM KernelsMem KernelEnv KernelOp ())
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ [TPrimExp Int64 ExpLeaf]
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TPrimExp Int64 ExpLeaf]
dims' (TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf])
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> a -> b
$ TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
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
-> ([TPrimExp Int64 ExpLeaf]
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Shape
-> ([TPrimExp Int64 ExpLeaf] -> ImpM lore r op ())
-> ImpM lore r op ()
sLoopNest Shape
vec_shape (([TPrimExp Int64 ExpLeaf]
  -> ImpM KernelsMem KernelEnv KernelOp ())
 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ([TPrimExp Int64 ExpLeaf]
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \[TPrimExp Int64 ExpLeaf]
vec_is -> do
          let glob_is :: [TPrimExp Int64 ExpLeaf]
glob_is = (VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [VName]
gtids [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf] -> [TPrimExp Int64 ExpLeaf]
forall a. [a] -> [a] -> [a]
++ [TPrimExp Int64 ExpLeaf]
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
$ (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool)
-> [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf]
-> [TExp Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
(.<.) ((VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [VName]
gtids) [TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM lore r op ()
copyDWIMFix
                  VName
arr
                  [KernelConstants
-> TypeBase Shape NoUniqueness -> TPrimExp Int64 ExpLeaf
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)
                  [TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM lore r op ()
copyDWIMFix VName
arr [KernelConstants
-> TypeBase Shape NoUniqueness -> TPrimExp Int64 ExpLeaf
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

          Maybe
  (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
-> TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf
-> Lambda KernelsMem
-> [VName]
-> ImpM KernelsMem KernelEnv KernelOp ()
groupScan
            Maybe
  (TPrimExp Int32 ExpLeaf -> TPrimExp Int32 ExpLeaf -> TExp Bool)
crossesSegment'
            (TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf)
-> TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a b. (a -> b) -> a -> b
$ TV Int32 -> TPrimExp Int32 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int32
stage1_num_threads)
            (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf)
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a b. (a -> b) -> a -> b
$ KernelConstants -> TPrimExp Int64 ExpLeaf
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM lore r op ()
copyDWIMFix
                  (PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
                  [TPrimExp Int64 ExpLeaf]
glob_is
                  (VName -> SubExp
Var VName
arr)
                  [KernelConstants
-> TypeBase Shape NoUniqueness -> TPrimExp Int64 ExpLeaf
localArrayIndex KernelConstants
constants TypeBase Shape NoUniqueness
t]

scanStage3 ::
  Pattern KernelsMem ->
  Count NumGroups SubExp ->
  Count GroupSize SubExp ->
  Imp.TExp Int64 ->
  CrossesSegment ->
  SegSpace ->
  [SegBinOp KernelsMem] ->
  CallKernelGen ()
scanStage3 :: Pattern KernelsMem
-> Count NumGroups SubExp
-> Count GroupSize SubExp
-> TPrimExp Int64 ExpLeaf
-> CrossesSegment
-> SegSpace
-> [SegBinOp KernelsMem]
-> CallKernelGen ()
scanStage3 (Pattern [PatElem KernelsMem]
_ [PatElem KernelsMem]
all_pes) Count NumGroups SubExp
num_groups Count GroupSize SubExp
group_size TPrimExp Int64 ExpLeaf
elems_per_group CrossesSegment
crossesSegment SegSpace
space [SegBinOp KernelsMem]
scans = do
  let num_groups' :: Count NumGroups (TPrimExp Int64 ExpLeaf)
num_groups' = (SubExp -> TPrimExp Int64 ExpLeaf)
-> Count NumGroups SubExp
-> Count NumGroups (TPrimExp Int64 ExpLeaf)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp Count NumGroups SubExp
num_groups
      group_size' :: Count GroupSize (TPrimExp Int64 ExpLeaf)
group_size' = (SubExp -> TPrimExp Int64 ExpLeaf)
-> Count GroupSize SubExp
-> Count GroupSize (TPrimExp Int64 ExpLeaf)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp 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' :: [TPrimExp Int64 ExpLeaf]
dims' = (SubExp -> TPrimExp Int64 ExpLeaf)
-> [SubExp] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp [SubExp]
dims
  TPrimExp Int32 ExpLeaf
required_groups <-
    SpaceId
-> TPrimExp Int32 ExpLeaf
-> ImpM KernelsMem HostEnv HostOp (TPrimExp Int32 ExpLeaf)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TExp t)
dPrimVE SpaceId
"required_groups" (TPrimExp Int32 ExpLeaf
 -> ImpM KernelsMem HostEnv HostOp (TPrimExp Int32 ExpLeaf))
-> TPrimExp Int32 ExpLeaf
-> ImpM KernelsMem HostEnv HostOp (TPrimExp Int32 ExpLeaf)
forall a b. (a -> b) -> a -> b
$
      TPrimExp Int64 ExpLeaf -> TPrimExp Int32 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int32 v
sExt32 (TPrimExp Int64 ExpLeaf -> TPrimExp Int32 ExpLeaf)
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int32 ExpLeaf
forall a b. (a -> b) -> a -> b
$ [TPrimExp Int64 ExpLeaf] -> TPrimExp Int64 ExpLeaf
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [TPrimExp Int64 ExpLeaf]
dims' TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall e. IntegralExp e => e -> e -> e
`divUp` TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (Count GroupSize (TPrimExp Int64 ExpLeaf) -> TPrimExp Int64 ExpLeaf
forall u e. Count u e -> e
unCount Count GroupSize (TPrimExp Int64 ExpLeaf)
group_size')

  SpaceId
-> Count NumGroups (TPrimExp Int64 ExpLeaf)
-> Count GroupSize (TPrimExp Int64 ExpLeaf)
-> VName
-> ImpM KernelsMem KernelEnv KernelOp ()
-> CallKernelGen ()
sKernelThread SpaceId
"scan_stage3" Count NumGroups (TPrimExp Int64 ExpLeaf)
num_groups' Count GroupSize (TPrimExp Int64 ExpLeaf)
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
-> TPrimExp Int32 ExpLeaf
-> (TPrimExp Int32 ExpLeaf
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
virtualiseGroups SegVirt
SegVirt TPrimExp Int32 ExpLeaf
required_groups ((TPrimExp Int32 ExpLeaf -> ImpM KernelsMem KernelEnv KernelOp ())
 -> ImpM KernelsMem KernelEnv KernelOp ())
-> (TPrimExp Int32 ExpLeaf
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \TPrimExp Int32 ExpLeaf
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

      -- Compute our logical index.
      TPrimExp Int64 ExpLeaf
flat_idx <-
        SpaceId
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TPrimExp Int64 ExpLeaf)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TExp t)
dPrimVE SpaceId
"flat_idx" (TPrimExp Int64 ExpLeaf
 -> ImpM KernelsMem KernelEnv KernelOp (TPrimExp Int64 ExpLeaf))
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TPrimExp Int64 ExpLeaf)
forall a b. (a -> b) -> a -> b
$
          TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 TPrimExp Int32 ExpLeaf
virt_group_id TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (Count GroupSize (TPrimExp Int64 ExpLeaf) -> TPrimExp Int64 ExpLeaf
forall u e. Count u e -> e
unCount Count GroupSize (TPrimExp Int64 ExpLeaf)
group_size')
            TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int32 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall t v. IntExp t => TPrimExp t v -> TPrimExp Int64 v
sExt64 (KernelConstants -> TPrimExp Int32 ExpLeaf
kernelLocalThreadId KernelConstants
constants)
      (VName
 -> TPrimExp Int64 ExpLeaf -> ImpM KernelsMem KernelEnv KernelOp ())
-> [VName]
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ VName
-> TPrimExp Int64 ExpLeaf -> ImpM KernelsMem KernelEnv KernelOp ()
forall t lore r op. VName -> TExp t -> ImpM lore r op ()
dPrimV_ [VName]
gtids ([TPrimExp Int64 ExpLeaf] -> ImpM KernelsMem KernelEnv KernelOp ())
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ [TPrimExp Int64 ExpLeaf]
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TPrimExp Int64 ExpLeaf]
dims' TPrimExp Int64 ExpLeaf
flat_idx

      -- Figure out which group this element was originally in.
      TV Int64
orig_group <- SpaceId
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"orig_group" (TPrimExp Int64 ExpLeaf
 -> ImpM KernelsMem KernelEnv KernelOp (TV Int64))
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall a b. (a -> b) -> a -> b
$ TPrimExp Int64 ExpLeaf
flat_idx TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall e. IntegralExp e => e -> e -> e
`quot` TPrimExp Int64 ExpLeaf
elems_per_group
      -- Then the index of the carry-in of the preceding group.
      TV Int64
carry_in_flat_idx <-
        SpaceId
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"carry_in_flat_idx" (TPrimExp Int64 ExpLeaf
 -> ImpM KernelsMem KernelEnv KernelOp (TV Int64))
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem KernelEnv KernelOp (TV Int64)
forall a b. (a -> b) -> a -> b
$
          TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
orig_group TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf
elems_per_group TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
1
      -- Figure out the logical index of the carry-in.
      let carry_in_idx :: [TPrimExp Int64 ExpLeaf]
carry_in_idx = [TPrimExp Int64 ExpLeaf]
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall num. IntegralExp num => [num] -> num -> [num]
unflattenIndex [TPrimExp Int64 ExpLeaf]
dims' (TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf])
-> TPrimExp Int64 ExpLeaf -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> a -> b
$ TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
carry_in_flat_idx

      -- Apply the carry if we are not in the scan results for the first
      -- group, and are not the last element in such a group (because
      -- then the carry was updated in stage 2), and we are not crossing
      -- a segment boundary.
      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
$ (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool)
-> [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf]
-> [TExp Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
(.<.) ((VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [VName]
gtids) [TPrimExp Int64 ExpLeaf]
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 (TPrimExp Int64 ExpLeaf)
-> Maybe (TPrimExp Int64 ExpLeaf -> TExp Bool)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TPrimExp Int64 ExpLeaf -> Maybe (TPrimExp Int64 ExpLeaf)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
carry_in_flat_idx)
                Maybe (TPrimExp Int64 ExpLeaf -> TExp Bool)
-> Maybe (TPrimExp Int64 ExpLeaf) -> Maybe (TExp Bool)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TPrimExp Int64 ExpLeaf -> Maybe (TPrimExp Int64 ExpLeaf)
forall (f :: * -> *) a. Applicative f => a -> f a
pure TPrimExp Int64 ExpLeaf
flat_idx
          is_a_carry :: TExp Bool
is_a_carry = TPrimExp Int64 ExpLeaf
flat_idx TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. (TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
orig_group TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
+ TPrimExp Int64 ExpLeaf
1) TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
* TPrimExp Int64 ExpLeaf
elems_per_group TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a. Num a => a -> a -> a
- TPrimExp Int64 ExpLeaf
1
          no_carry_in :: TExp Bool
no_carry_in = TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
orig_group TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf -> TExp Bool
forall t v. TPrimExp t v -> TPrimExp t v -> TPrimExp Bool v
.==. TPrimExp Int64 ExpLeaf
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
-> ([TPrimExp Int64 ExpLeaf]
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
Shape
-> ([TPrimExp Int64 ExpLeaf] -> ImpM lore r op ())
-> ImpM lore r op ()
sLoopNest Shape
vec_shape (([TPrimExp Int64 ExpLeaf]
  -> ImpM KernelsMem KernelEnv KernelOp ())
 -> ImpM KernelsMem KernelEnv KernelOp ())
-> ([TPrimExp Int64 ExpLeaf]
    -> ImpM KernelsMem KernelEnv KernelOp ())
-> ImpM KernelsMem KernelEnv KernelOp ()
forall a b. (a -> b) -> a -> b
$ \[TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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)
                    ([TPrimExp Int64 ExpLeaf]
carry_in_idx [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf] -> [TPrimExp Int64 ExpLeaf]
forall a. [a] -> [a] -> [a]
++ [TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> 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 -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [VName]
gtids [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf] -> [TPrimExp Int64 ExpLeaf]
forall a. [a] -> [a] -> [a]
++ [TPrimExp Int64 ExpLeaf]
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
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM KernelsMem KernelEnv KernelOp ()
forall lore r op.
VName
-> [TPrimExp Int64 ExpLeaf]
-> SubExp
-> [TPrimExp Int64 ExpLeaf]
-> ImpM lore r op ()
copyDWIMFix
                    (PatElemT (MemInfo SubExp NoUniqueness MemBind) -> VName
forall dec. PatElemT dec -> VName
patElemName PatElemT (MemInfo SubExp NoUniqueness MemBind)
pe)
                    ((VName -> TPrimExp Int64 ExpLeaf)
-> [VName] -> [TPrimExp Int64 ExpLeaf]
forall a b. (a -> b) -> [a] -> [b]
map VName -> TPrimExp Int64 ExpLeaf
Imp.vi64 [VName]
gtids [TPrimExp Int64 ExpLeaf]
-> [TPrimExp Int64 ExpLeaf] -> [TPrimExp Int64 ExpLeaf]
forall a. [a] -> [a] -> [a]
++ [TPrimExp Int64 ExpLeaf]
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)
                    []

-- | Compile 'SegScan' instance to host-level code with calls to
-- various kernels.
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 = do
  -- Since stage 2 involves a group size equal to the number of groups
  -- used for stage 1, we have to cap this number to the maximum group
  -- size.
  TV Int64
stage1_max_num_groups <- SpaceId -> PrimType -> ImpM KernelsMem HostEnv HostOp (TV Int64)
forall lore r op t. SpaceId -> PrimType -> ImpM lore r op (TV t)
dPrim SpaceId
"stage1_max_num_groups" PrimType
int64
  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 Int64 -> VName
forall t. TV t -> VName
tvVar TV Int64
stage1_max_num_groups) SizeClass
SizeGroup

  Count NumGroups SubExp
stage1_num_groups <-
    (TV Int64 -> Count NumGroups SubExp)
-> ImpM KernelsMem HostEnv HostOp (TV Int64)
-> 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 Int64 -> SubExp) -> TV Int64 -> Count NumGroups SubExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TV Int64 -> SubExp
forall t. TV t -> SubExp
tvSize) (ImpM KernelsMem HostEnv HostOp (TV Int64)
 -> ImpM KernelsMem HostEnv HostOp (Count NumGroups SubExp))
-> ImpM KernelsMem HostEnv HostOp (TV Int64)
-> ImpM KernelsMem HostEnv HostOp (Count NumGroups SubExp)
forall a b. (a -> b) -> a -> b
$
      SpaceId
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem HostEnv HostOp (TV Int64)
forall t lore r op. SpaceId -> TExp t -> ImpM lore r op (TV t)
dPrimV SpaceId
"stage1_num_groups" (TPrimExp Int64 ExpLeaf
 -> ImpM KernelsMem HostEnv HostOp (TV Int64))
-> TPrimExp Int64 ExpLeaf
-> ImpM KernelsMem HostEnv HostOp (TV Int64)
forall a b. (a -> b) -> a -> b
$
        TPrimExp Int64 ExpLeaf
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall v. TPrimExp Int64 v -> TPrimExp Int64 v -> TPrimExp Int64 v
sMin64 (TV Int64 -> TPrimExp Int64 ExpLeaf
forall t. TV t -> TExp t
tvExp TV Int64
stage1_max_num_groups) (TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf)
-> TPrimExp Int64 ExpLeaf -> TPrimExp Int64 ExpLeaf
forall a b. (a -> b) -> a -> b
$
          SubExp -> TPrimExp Int64 ExpLeaf
forall a. ToExp a => a -> TPrimExp Int64 ExpLeaf
toInt64Exp (SubExp -> TPrimExp Int64 ExpLeaf)
-> SubExp -> TPrimExp Int64 ExpLeaf
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, TPrimExp Int64 ExpLeaf
elems_per_group, CrossesSegment
crossesSegment) <-
    Pattern KernelsMem
-> Count NumGroups SubExp
-> Count GroupSize SubExp
-> SegSpace
-> [SegBinOp KernelsMem]
-> KernelBody KernelsMem
-> CallKernelGen (TV Int32, TPrimExp Int64 ExpLeaf, 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
$ TPrimExp Int64 ExpLeaf -> Exp
forall t v. TPrimExp t v -> PrimExp v
untyped TPrimExp Int64 ExpLeaf
elems_per_group

  Pattern KernelsMem
-> TV Int32
-> TPrimExp Int64 ExpLeaf
-> Count NumGroups SubExp
-> CrossesSegment
-> SegSpace
-> [SegBinOp KernelsMem]
-> CallKernelGen ()
scanStage2 Pattern KernelsMem
pat TV Int32
stage1_num_threads TPrimExp Int64 ExpLeaf
elems_per_group Count NumGroups SubExp
stage1_num_groups CrossesSegment
crossesSegment SegSpace
space [SegBinOp KernelsMem]
scans
  Pattern KernelsMem
-> Count NumGroups SubExp
-> Count GroupSize SubExp
-> TPrimExp Int64 ExpLeaf
-> CrossesSegment
-> SegSpace
-> [SegBinOp KernelsMem]
-> CallKernelGen ()
scanStage3 Pattern KernelsMem
pat (SegLevel -> Count NumGroups SubExp
segNumGroups SegLevel
lvl) (SegLevel -> Count GroupSize SubExp
segGroupSize SegLevel
lvl) TPrimExp Int64 ExpLeaf
elems_per_group CrossesSegment
crossesSegment SegSpace
space [SegBinOp KernelsMem]
scans