| 1 |
{-# LANGUAGE TypeFamilies #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
module Futhark.Pass.ExplicitAllocations.SegOp |
| 5 |
( allocInKernelBody, |
| 6 |
allocInBinOpLambda, |
| 7 |
) |
| 8 |
where |
| 9 |
|
| 10 |
import Control.Monad |
| 11 |
import Futhark.IR.GPUMem |
| 12 |
import Futhark.IR.Mem.LMAD qualified as LMAD |
| 13 |
import Futhark.Pass.ExplicitAllocations |
| 14 |
|
| 15 |
instance SizeSubst (SegOp lvl rep) |
| 16 |
|
| 17 |
allocInKernelBody :: |
| 18 |
(Allocable fromrep torep inner) => |
| 19 |
KernelBody fromrep -> |
| 20 |
AllocM fromrep torep (KernelBody torep) |
| 21 |
allocInKernelBody (KernelBody () stms res) = |
| 22 |
uncurry (flip (KernelBody ())) |
| 23 |
<$> collectStms (allocInStms stms (pure res)) |
| 24 |
|
| 25 |
allocInLambda :: |
| 26 |
(Allocable fromrep torep inner) => |
| 27 |
[LParam torep] -> |
| 28 |
Body fromrep -> |
| 29 |
AllocM fromrep torep (Lambda torep) |
| 30 |
allocInLambda params body = |
| 31 |
mkLambda params . allocInStms (bodyStms body) $ |
| 32 |
pure $ |
| 33 |
bodyResult body |
| 34 |
|
| 35 |
allocInBinOpParams :: |
| 36 |
(Allocable fromrep torep inner) => |
| 37 |
SubExp -> |
| 38 |
TPrimExp Int64 VName -> |
| 39 |
TPrimExp Int64 VName -> |
| 40 |
[LParam fromrep] -> |
| 41 |
[LParam fromrep] -> |
| 42 |
AllocM fromrep torep ([LParam torep], [LParam torep]) |
| 43 |
allocInBinOpParams num_threads my_id other_id xs ys = unzip <$> zipWithM alloc xs ys |
| 44 |
where |
| 45 |
alloc x y = |
| 46 |
case paramType x of |
| 47 |
Array pt shape u -> do |
| 48 |
let name = maybe "num_threads" baseString (subExpVar num_threads) |
| 49 |
twice_num_threads <- |
| 50 |
letSubExp ("twice_" <> name) . BasicOp $ |
| 51 |
BinOp (Mul Int64 OverflowUndef) num_threads (intConst Int64 2) |
| 52 |
let t = paramType x `arrayOfRow` twice_num_threads |
| 53 |
mem <- allocForArray t =<< askDefaultSpace |
| 54 |
-- XXX: this iota lmad is a bit inefficient; leading to |
| 55 |
-- uncoalesced access. |
| 56 |
let base_dims = map pe64 $ arrayDims t |
| 57 |
lmad_base = LMAD.iota 0 base_dims |
| 58 |
lmad_x = |
| 59 |
LMAD.slice lmad_base $ |
| 60 |
fullSliceNum base_dims [DimFix my_id] |
| 61 |
lmad_y = |
| 62 |
LMAD.slice lmad_base $ |
| 63 |
fullSliceNum base_dims [DimFix other_id] |
| 64 |
pure |
| 65 |
( x {paramDec = MemArray pt shape u $ ArrayIn mem lmad_x}, |
| 66 |
y {paramDec = MemArray pt shape u $ ArrayIn mem lmad_y} |
| 67 |
) |
| 68 |
Prim bt -> |
| 69 |
pure |
| 70 |
( x {paramDec = MemPrim bt}, |
| 71 |
y {paramDec = MemPrim bt} |
| 72 |
) |
| 73 |
Mem space -> |
| 74 |
pure |
| 75 |
( x {paramDec = MemMem space}, |
| 76 |
y {paramDec = MemMem space} |
| 77 |
) |
| 78 |
-- This next case will never happen. |
| 79 |
Acc acc ispace ts u -> |
| 80 |
pure |
| 81 |
( x {paramDec = MemAcc acc ispace ts u}, |
| 82 |
y {paramDec = MemAcc acc ispace ts u} |
| 83 |
) |
| 84 |
|
| 85 |
allocInBinOpLambda :: |
| 86 |
(Allocable fromrep torep inner) => |
| 87 |
SubExp -> |
| 88 |
SegSpace -> |
| 89 |
Lambda fromrep -> |
| 90 |
AllocM fromrep torep (Lambda torep) |
| 91 |
allocInBinOpLambda num_threads (SegSpace flat _) lam = do |
| 92 |
let (acc_params, arr_params) = |
| 93 |
splitAt (length (lambdaParams lam) `div` 2) $ lambdaParams lam |
| 94 |
index_x = TPrimExp $ LeafExp flat int64 |
| 95 |
index_y = index_x + pe64 num_threads |
| 96 |
(acc_params', arr_params') <- |
| 97 |
allocInBinOpParams num_threads index_x index_y acc_params arr_params |
| 98 |
|
| 99 |
allocInLambda (acc_params' ++ arr_params') (lambdaBody lam) |