{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Futhark.Pass.ExplicitAllocations.SegOp
  ( allocInKernelBody,
    allocInBinOpLambda,
  )
where

import Futhark.IR.GPUMem
import Futhark.IR.Mem.IxFun qualified as IxFun
import Futhark.Pass.ExplicitAllocations

instance SizeSubst (SegOp lvl rep)

allocInKernelBody ::
  Allocable fromrep torep inner =>
  KernelBody fromrep ->
  AllocM fromrep torep (KernelBody torep)
allocInKernelBody :: forall {k} (fromrep :: k) torep inner.
Allocable fromrep torep inner =>
KernelBody fromrep -> AllocM fromrep torep (KernelBody torep)
allocInKernelBody (KernelBody () Stms fromrep
stms [KernelResult]
res) =
  forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall {k} (rep :: k).
BodyDec rep -> Stms rep -> [KernelResult] -> KernelBody rep
KernelBody ()))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
MonadBuilder m =>
m a -> m (a, Stms (Rep m))
collectStms (forall {k} (fromrep :: k) torep inner a.
Allocable fromrep torep inner =>
Stms fromrep -> AllocM fromrep torep a -> AllocM fromrep torep a
allocInStms Stms fromrep
stms (forall (f :: * -> *) a. Applicative f => a -> f a
pure [KernelResult]
res))

allocInLambda ::
  Allocable fromrep torep inner =>
  [LParam torep] ->
  Body fromrep ->
  AllocM fromrep torep (Lambda torep)
allocInLambda :: forall {k} (fromrep :: k) torep inner.
Allocable fromrep torep inner =>
[LParam torep]
-> Body fromrep -> AllocM fromrep torep (Lambda torep)
allocInLambda [LParam torep]
params Body fromrep
body =
  forall (m :: * -> *).
MonadBuilder m =>
[LParam (Rep m)] -> m Result -> m (Lambda (Rep m))
mkLambda [LParam torep]
params forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (fromrep :: k) torep inner a.
Allocable fromrep torep inner =>
Stms fromrep -> AllocM fromrep torep a -> AllocM fromrep torep a
allocInStms (forall {k} (rep :: k). Body rep -> Stms rep
bodyStms Body fromrep
body) forall a b. (a -> b) -> a -> b
$
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
      forall {k} (rep :: k). Body rep -> Result
bodyResult Body fromrep
body

allocInBinOpParams ::
  Allocable fromrep torep inner =>
  SubExp ->
  TPrimExp Int64 VName ->
  TPrimExp Int64 VName ->
  [LParam fromrep] ->
  [LParam fromrep] ->
  AllocM fromrep torep ([LParam torep], [LParam torep])
allocInBinOpParams :: forall {k} (fromrep :: k) torep inner.
Allocable fromrep torep inner =>
SubExp
-> TPrimExp Int64 VName
-> TPrimExp Int64 VName
-> [LParam fromrep]
-> [LParam fromrep]
-> AllocM fromrep torep ([LParam torep], [LParam torep])
allocInBinOpParams SubExp
num_threads TPrimExp Int64 VName
my_id TPrimExp Int64 VName
other_id [LParam fromrep]
xs [LParam fromrep]
ys = forall a b. [(a, b)] -> ([a], [b])
unzip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM Param Type
-> Param Type
-> AllocM fromrep torep (Param LParamMem, Param LParamMem)
alloc [LParam fromrep]
xs [LParam fromrep]
ys
  where
    alloc :: Param Type
-> Param Type
-> AllocM fromrep torep (Param LParamMem, Param LParamMem)
alloc Param Type
x Param Type
y =
      case forall dec. Typed dec => Param dec -> Type
paramType Param Type
x of
        Array PrimType
pt ShapeBase SubExp
shape NoUniqueness
u -> do
          SubExp
twice_num_threads <-
            forall (m :: * -> *).
MonadBuilder m =>
String -> Exp (Rep m) -> m SubExp
letSubExp String
"twice_num_threads" forall a b. (a -> b) -> a -> b
$
              forall {k} (rep :: k). BasicOp -> Exp rep
BasicOp forall a b. (a -> b) -> a -> b
$
                BinOp -> SubExp -> SubExp -> BasicOp
BinOp (IntType -> Overflow -> BinOp
Mul IntType
Int64 Overflow
OverflowUndef) SubExp
num_threads forall a b. (a -> b) -> a -> b
$
                  IntType -> Integer -> SubExp
intConst IntType
Int64 Integer
2
          let t :: Type
t = forall dec. Typed dec => Param dec -> Type
paramType Param Type
x forall d.
ArrayShape (ShapeBase d) =>
TypeBase (ShapeBase d) NoUniqueness
-> d -> TypeBase (ShapeBase d) NoUniqueness
`arrayOfRow` SubExp
twice_num_threads
          VName
mem <- forall {k} (fromrep :: k) torep inner.
Allocable fromrep torep inner =>
Type -> Space -> AllocM fromrep torep VName
allocForArray Type
t Space
DefaultSpace
          -- XXX: this iota ixfun is a bit inefficient; leading to
          -- uncoalesced access.
          let base_dims :: [TPrimExp Int64 VName]
base_dims = forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TPrimExp Int64 VName
pe64 forall a b. (a -> b) -> a -> b
$ forall u. TypeBase (ShapeBase SubExp) u -> [SubExp]
arrayDims Type
t
              ixfun_base :: IxFun (TPrimExp Int64 VName)
ixfun_base = forall num. IntegralExp num => Shape num -> IxFun num
IxFun.iota [TPrimExp Int64 VName]
base_dims
              ixfun_x :: IxFun (TPrimExp Int64 VName)
ixfun_x =
                forall num.
(Eq num, IntegralExp num) =>
IxFun num -> Slice num -> IxFun num
IxFun.slice IxFun (TPrimExp Int64 VName)
ixfun_base forall a b. (a -> b) -> a -> b
$
                  forall d. Num d => [d] -> [DimIndex d] -> Slice d
fullSliceNum [TPrimExp Int64 VName]
base_dims [forall d. d -> DimIndex d
DimFix TPrimExp Int64 VName
my_id]
              ixfun_y :: IxFun (TPrimExp Int64 VName)
ixfun_y =
                forall num.
(Eq num, IntegralExp num) =>
IxFun num -> Slice num -> IxFun num
IxFun.slice IxFun (TPrimExp Int64 VName)
ixfun_base forall a b. (a -> b) -> a -> b
$
                  forall d. Num d => [d] -> [DimIndex d] -> Slice d
fullSliceNum [TPrimExp Int64 VName]
base_dims [forall d. d -> DimIndex d
DimFix TPrimExp Int64 VName
other_id]
          forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( Param Type
x {paramDec :: LParamMem
paramDec = forall d u ret.
PrimType -> ShapeBase d -> u -> ret -> MemInfo d u ret
MemArray PrimType
pt ShapeBase SubExp
shape NoUniqueness
u forall a b. (a -> b) -> a -> b
$ VName -> IxFun (TPrimExp Int64 VName) -> MemBind
ArrayIn VName
mem IxFun (TPrimExp Int64 VName)
ixfun_x},
              Param Type
y {paramDec :: LParamMem
paramDec = forall d u ret.
PrimType -> ShapeBase d -> u -> ret -> MemInfo d u ret
MemArray PrimType
pt ShapeBase SubExp
shape NoUniqueness
u forall a b. (a -> b) -> a -> b
$ VName -> IxFun (TPrimExp Int64 VName) -> MemBind
ArrayIn VName
mem IxFun (TPrimExp Int64 VName)
ixfun_y}
            )
        Prim PrimType
bt ->
          forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( Param Type
x {paramDec :: LParamMem
paramDec = forall d u ret. PrimType -> MemInfo d u ret
MemPrim PrimType
bt},
              Param Type
y {paramDec :: LParamMem
paramDec = forall d u ret. PrimType -> MemInfo d u ret
MemPrim PrimType
bt}
            )
        Mem Space
space ->
          forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( Param Type
x {paramDec :: LParamMem
paramDec = forall d u ret. Space -> MemInfo d u ret
MemMem Space
space},
              Param Type
y {paramDec :: LParamMem
paramDec = forall d u ret. Space -> MemInfo d u ret
MemMem Space
space}
            )
        -- This next case will never happen.
        Acc VName
acc ShapeBase SubExp
ispace [Type]
ts NoUniqueness
u ->
          forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( Param Type
x {paramDec :: LParamMem
paramDec = forall d u ret.
VName -> ShapeBase SubExp -> [Type] -> u -> MemInfo d u ret
MemAcc VName
acc ShapeBase SubExp
ispace [Type]
ts NoUniqueness
u},
              Param Type
y {paramDec :: LParamMem
paramDec = forall d u ret.
VName -> ShapeBase SubExp -> [Type] -> u -> MemInfo d u ret
MemAcc VName
acc ShapeBase SubExp
ispace [Type]
ts NoUniqueness
u}
            )

allocInBinOpLambda ::
  Allocable fromrep torep inner =>
  SubExp ->
  SegSpace ->
  Lambda fromrep ->
  AllocM fromrep torep (Lambda torep)
allocInBinOpLambda :: forall {k} (fromrep :: k) torep inner.
Allocable fromrep torep inner =>
SubExp
-> SegSpace
-> Lambda fromrep
-> AllocM fromrep torep (Lambda torep)
allocInBinOpLambda SubExp
num_threads (SegSpace VName
flat [(VName, SubExp)]
_) Lambda fromrep
lam = do
  let ([Param Type]
acc_params, [Param Type]
arr_params) =
        forall a. Int -> [a] -> ([a], [a])
splitAt (forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall {k} (rep :: k). Lambda rep -> [LParam rep]
lambdaParams Lambda fromrep
lam) forall a. Integral a => a -> a -> a
`div` Int
2) forall a b. (a -> b) -> a -> b
$ forall {k} (rep :: k). Lambda rep -> [LParam rep]
lambdaParams Lambda fromrep
lam
      index_x :: TPrimExp Int64 VName
index_x = forall {k} (t :: k) v. PrimExp v -> TPrimExp t v
TPrimExp forall a b. (a -> b) -> a -> b
$ forall v. v -> PrimType -> PrimExp v
LeafExp VName
flat PrimType
int64
      index_y :: TPrimExp Int64 VName
index_y = TPrimExp Int64 VName
index_x forall a. Num a => a -> a -> a
+ SubExp -> TPrimExp Int64 VName
pe64 SubExp
num_threads
  ([Param LParamMem]
acc_params', [Param LParamMem]
arr_params') <-
    forall {k} (fromrep :: k) torep inner.
Allocable fromrep torep inner =>
SubExp
-> TPrimExp Int64 VName
-> TPrimExp Int64 VName
-> [LParam fromrep]
-> [LParam fromrep]
-> AllocM fromrep torep ([LParam torep], [LParam torep])
allocInBinOpParams SubExp
num_threads TPrimExp Int64 VName
index_x TPrimExp Int64 VName
index_y [Param Type]
acc_params [Param Type]
arr_params

  forall {k} (fromrep :: k) torep inner.
Allocable fromrep torep inner =>
[LParam torep]
-> Body fromrep -> AllocM fromrep torep (Lambda torep)
allocInLambda ([Param LParamMem]
acc_params' forall a. [a] -> [a] -> [a]
++ [Param LParamMem]
arr_params') (forall {k} (rep :: k). Lambda rep -> Body rep
lambdaBody Lambda fromrep
lam)