| 1 |
-- | In the context of this module, a "size" is any kind of tunable |
| 2 |
-- (run-time) constant. |
| 3 |
module Futhark.IR.GPU.Sizes |
| 4 |
( SizeClass (..), |
| 5 |
sizeDefault, |
| 6 |
KernelPath, |
| 7 |
Count (..), |
| 8 |
NumBlocks, |
| 9 |
BlockSize, |
| 10 |
NumThreads, |
| 11 |
) |
| 12 |
where |
| 13 |
|
| 14 |
import Data.Int (Int64) |
| 15 |
import Data.Traversable |
| 16 |
import Futhark.IR.Prop.Names (FreeIn) |
| 17 |
import Futhark.Transform.Substitute |
| 18 |
import Futhark.Util.IntegralExp (IntegralExp) |
| 19 |
import Futhark.Util.Pretty |
| 20 |
import Language.Futhark.Core (Name) |
| 21 |
import Prelude hiding (id, (.)) |
| 22 |
|
| 23 |
-- | An indication of which comparisons have been performed to get to |
| 24 |
-- this point, as well as the result of each comparison. |
| 25 |
type KernelPath = [(Name, Bool)] |
| 26 |
|
| 27 |
-- | The class of some kind of configurable size. Each class may |
| 28 |
-- impose constraints on the valid values. |
| 29 |
data SizeClass |
| 30 |
= -- | A threshold with an optional default. |
| 31 |
SizeThreshold KernelPath (Maybe Int64) |
| 32 |
| SizeThreadBlock |
| 33 |
| -- | The number of thread blocks. |
| 34 |
SizeGrid |
| 35 |
| SizeTile |
| 36 |
| SizeRegTile |
| 37 |
| -- | Likely not useful on its own, but querying the |
| 38 |
-- maximum can be handy. |
| 39 |
SizeSharedMemory |
| 40 |
| -- | A bespoke size with a default. |
| 41 |
SizeBespoke Name Int64 |
| 42 |
| -- | Amount of registers available per threadblock. Mostly |
| 43 |
-- meaningful for querying the maximum. |
| 44 |
SizeRegisters |
| 45 |
| -- | Amount of L2 cache memory, in bytes. Mostly meaningful for |
| 46 |
-- querying the maximum. |
| 47 |
SizeCache |
| 48 |
deriving (Eq, Ord, Show) |
| 49 |
|
| 50 |
instance Pretty SizeClass where |
| 51 |
pretty (SizeThreshold path def) = |
| 52 |
"threshold" <> parens (def' <> comma <+> hsep (map pStep path)) |
| 53 |
where |
| 54 |
pStep (v, True) = pretty v |
| 55 |
pStep (v, False) = "!" <> pretty v |
| 56 |
def' = maybe "def" pretty def |
| 57 |
pretty SizeThreadBlock = "thread_block_size" |
| 58 |
pretty SizeGrid = "grid_size" |
| 59 |
pretty SizeTile = "tile_size" |
| 60 |
pretty SizeRegTile = "reg_tile_size" |
| 61 |
pretty SizeSharedMemory = "shared_memory" |
| 62 |
pretty (SizeBespoke k def) = |
| 63 |
"bespoke" <> parens (pretty k <> comma <+> pretty def) |
| 64 |
pretty SizeRegisters = "registers" |
| 65 |
pretty SizeCache = "cache" |
| 66 |
|
| 67 |
-- | The default value for the size. If 'Nothing', that means the backend gets to decide. |
| 68 |
sizeDefault :: SizeClass -> Maybe Int64 |
| 69 |
sizeDefault (SizeThreshold _ x) = x |
| 70 |
sizeDefault (SizeBespoke _ x) = Just x |
| 71 |
sizeDefault _ = Nothing |
| 72 |
|
| 73 |
-- | A wrapper supporting a phantom type for indicating what we are counting. |
| 74 |
newtype Count u e = Count {unCount :: e} |
| 75 |
deriving (Eq, Ord, Show, Num, IntegralExp, FreeIn, Pretty, Substitute) |
| 76 |
|
| 77 |
instance Functor (Count u) where |
| 78 |
fmap = fmapDefault |
| 79 |
|
| 80 |
instance Foldable (Count u) where |
| 81 |
foldMap = foldMapDefault |
| 82 |
|
| 83 |
instance Traversable (Count u) where |
| 84 |
traverse f (Count x) = Count <$> f x |
| 85 |
|
| 86 |
-- | Phantom type for the number of blocks of some kernel. |
| 87 |
data NumBlocks |
| 88 |
|
| 89 |
-- | Phantom type for the block size of some kernel. |
| 90 |
data BlockSize |
| 91 |
|
| 92 |
-- | Phantom type for number of threads. |
| 93 |
data NumThreads |