Debian Codegraph @

src/Futhark/Pass/ExplicitAllocations/GPU.hs

raw
1 {-# LANGUAGE TypeFamilies #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 -- | Facilities for converting a 'GPU' program to 'GPUMem'.
5 module Futhark.Pass.ExplicitAllocations.GPU
6 ( explicitAllocations,
7 explicitAllocationsInStms,
8 )
9 where
10
11 import Control.Monad
12 import Data.Set qualified as S
13 import Futhark.IR.GPU
14 import Futhark.IR.GPUMem
15 import Futhark.IR.Mem.LMAD qualified as LMAD
16 import Futhark.Pass.ExplicitAllocations
17 import Futhark.Pass.ExplicitAllocations.SegOp
18
19 instance SizeSubst (HostOp rep op) where
20 opIsConst (SizeOp GetSize {}) = True
21 opIsConst (SizeOp GetSizeMax {}) = True
22 opIsConst _ = False
23
24 allocAtLevel :: SegLevel -> AllocM GPU GPUMem a -> AllocM GPU GPUMem a
25 allocAtLevel lvl = local $ \env ->
26 env
27 { allocSpace = space,
28 allocInOp = handleHostOp (Just lvl)
29 }
30 where
31 space = case lvl of
32 SegBlock {} -> Space "shared"
33 SegThread {} -> Space "device"
34 SegThreadInBlock {} -> Space "device"
35
36 handleSegOp ::
37 Maybe SegLevel ->
38 SegOp SegLevel GPU ->
39 AllocM GPU GPUMem (SegOp SegLevel GPUMem)
40 handleSegOp outer_lvl op = do
41 num_threads <-
42 case (outer_lvl, segLevel op) of
43 -- This implies we are in the intragroup parallelism situation.
44 -- Just allocate for a single group; memory expansion will
45 -- handle the rest later.
46 (Just (SegBlock _ (Just grid)), _) -> pure $ unCount $ gridBlockSize grid
47 _ ->
48 letSubExp "num_threads"
49 =<< case maybe_grid of
50 Just grid ->
51 pure . BasicOp $
52 BinOp
53 (Mul Int64 OverflowUndef)
54 (unCount (gridNumBlocks grid))
55 (unCount (gridBlockSize grid))
56 Nothing ->
57 foldBinOp
58 (Mul Int64 OverflowUndef)
59 (intConst Int64 1)
60 (segSpaceDims $ segSpace op)
61 allocAtLevel (segLevel op) $ mapSegOpM (mapper num_threads) op
62 where
63 maybe_grid =
64 case (outer_lvl, segLevel op) of
65 (Just (SegThread _ (Just grid)), _) -> Just grid
66 (Just (SegBlock _ (Just grid)), _) -> Just grid
67 (_, SegThread _ (Just grid)) -> Just grid
68 (_, SegBlock _ (Just grid)) -> Just grid
69 _ -> Nothing
70 scope = scopeOfSegSpace $ segSpace op
71 mapper num_threads =
72 identitySegOpMapper
73 { mapOnSegOpBody =
74 localScope scope . local f . allocInKernelBody,
75 mapOnSegOpLambda =
76 local inThread
77 . allocInBinOpLambda num_threads (segSpace op)
78 }
79 f = case segLevel op of
80 SegThread {} -> inThread
81 SegThreadInBlock {} -> inThread
82 SegBlock {} -> inGroup
83 inThread env = env {envExpHints = inThreadExpHints}
84 inGroup env = env {envExpHints = inGroupExpHints}
85
86 handleHostOp ::
87 Maybe SegLevel ->
88 HostOp SOAC GPU ->
89 AllocM GPU GPUMem (MemOp (HostOp NoOp) GPUMem)
90 handleHostOp _ (SizeOp op) =
91 pure $ Inner $ SizeOp op
92 handleHostOp _ (OtherOp op) =
93 error $ "Cannot allocate memory in SOAC: " ++ prettyString op
94 handleHostOp outer_lvl (SegOp op) =
95 Inner . SegOp <$> handleSegOp outer_lvl op
96 handleHostOp _ (GPUBody ts (Body _ stms res)) =
97 fmap (Inner . GPUBody ts) . buildBody_ . allocInStms stms $ pure res
98
99 kernelExpHints :: Exp GPUMem -> AllocM GPU GPUMem [ExpHint]
100 kernelExpHints (BasicOp (Manifest v perm)) = do
101 dims <- arrayDims <$> lookupType v
102 let perm_inv = rearrangeInverse perm
103 dims' = rearrangeShape perm dims
104 lmad = LMAD.permute (LMAD.iota 0 $ map pe64 dims') perm_inv
105 pure [Hint lmad $ Space "device"]
106 kernelExpHints (Op (Inner (SegOp (SegMap lvl@(SegThread _ _) space ts body)))) =
107 zipWithM (mapResultHint lvl space) ts $ kernelBodyResult body
108 kernelExpHints (Op (Inner (SegOp (SegRed lvl@(SegThread _ _) space ts body reds)))) =
109 (map (const NoHint) red_res <>) <$> zipWithM (mapResultHint lvl space) (drop num_reds ts) map_res
110 where
111 num_reds = segBinOpResults reds
112 (red_res, map_res) = splitAt num_reds $ kernelBodyResult body
113 kernelExpHints e = defaultExpHints e
114
115 mapResultHint ::
116 SegLevel ->
117 SegSpace ->
118 Type ->
119 KernelResult ->
120 AllocM GPU GPUMem ExpHint
121 mapResultHint _lvl space = hint
122 where
123 -- Heuristic: do not rearrange for returned arrays that are
124 -- sufficiently small.
125 thresholdBytes = 8
126
127 coalesceReturnOfShape _ [] = False
128 coalesceReturnOfShape bs [Constant (IntValue (Int64Value d))] = bs * d > thresholdBytes
129 coalesceReturnOfShape _ _ = True
130
131 hint t Returns {}
132 | coalesceReturnOfShape (primByteSize (elemType t)) $ arrayDims t = do
133 let space_dims = segSpaceDims space
134 pure $ Hint (innermost space_dims (arrayDims t)) $ Space "device"
135 hint _ _ = pure NoHint
136
137 innermost :: [SubExp] -> [SubExp] -> LMAD
138 innermost space_dims t_dims =
139 let r = length t_dims
140 dims = space_dims ++ t_dims
141 perm =
142 [length space_dims .. length space_dims + r - 1]
143 ++ [0 .. length space_dims - 1]
144 perm_inv = rearrangeInverse perm
145 dims_perm = rearrangeShape perm dims
146 lmad_base = LMAD.iota 0 $ map pe64 dims_perm
147 lmad_rearranged = LMAD.permute lmad_base perm_inv
148 in lmad_rearranged
149
150 semiStatic :: S.Set VName -> SubExp -> Bool
151 semiStatic _ Constant {} = True
152 semiStatic consts (Var v) = v `S.member` consts
153
154 inGroupExpHints :: Exp GPUMem -> AllocM GPU GPUMem [ExpHint]
155 inGroupExpHints (Op (Inner (SegOp (SegMap _ space ts body))))
156 | any private $ kernelBodyResult body = do
157 consts <- asks envConsts
158 pure $ do
159 (t, r) <- zip ts $ kernelBodyResult body
160 pure $
161 if private r && all (semiStatic consts) (arrayDims t)
162 then
163 let seg_dims = map pe64 $ segSpaceDims space
164 dims = seg_dims ++ map pe64 (arrayDims t)
165 nilSlice d = DimSlice 0 d 0
166 in Hint
167 ( LMAD.slice (LMAD.iota 0 dims) $
168 fullSliceNum dims (map nilSlice seg_dims)
169 )
170 $ ScalarSpace (arrayDims t)
171 $ elemType t
172 else NoHint
173 where
174 private (Returns ResultPrivate _ _) = True
175 private _ = False
176 inGroupExpHints e = defaultExpHints e
177
178 inThreadExpHints :: Exp GPUMem -> AllocM GPU GPUMem [ExpHint]
179 inThreadExpHints e = do
180 consts <- asks envConsts
181 mapM (maybePrivate consts) =<< expExtType e
182 where
183 maybePrivate consts t
184 | Just (Array pt shape _) <- hasStaticShape t,
185 all (semiStatic consts) $ shapeDims shape = do
186 let lmad = LMAD.iota 0 $ map pe64 $ shapeDims shape
187 pure $ Hint lmad $ ScalarSpace (shapeDims shape) pt
188 | otherwise =
189 pure NoHint
190
191 -- | The pass from 'GPU' to 'GPUMem'.
192 explicitAllocations :: Pass GPU GPUMem
193 explicitAllocations = explicitAllocationsGeneric (Space "device") (handleHostOp Nothing) kernelExpHints
194
195 -- | Convert some 'GPU' stms to 'GPUMem'.
196 explicitAllocationsInStms ::
197 (MonadFreshNames m, HasScope GPUMem m) =>
198 Stms GPU ->
199 m (Stms GPUMem)
200 explicitAllocationsInStms = explicitAllocationsInStmsGeneric (Space "device") (handleHostOp Nothing) kernelExpHints