| 1 |
{-# LANGUAGE TypeFamilies #-} |
| 2 |
|
| 3 |
module Futhark.Pass.ExtractKernels.ToGPU |
| 4 |
( getSize, |
| 5 |
segThread, |
| 6 |
soacsLambdaToGPU, |
| 7 |
soacsStmToGPU, |
| 8 |
scopeForGPU, |
| 9 |
scopeForSOACs, |
| 10 |
injectSOACS, |
| 11 |
) |
| 12 |
where |
| 13 |
|
| 14 |
import Control.Monad.Identity |
| 15 |
import Data.List () |
| 16 |
import Futhark.IR |
| 17 |
import Futhark.IR.GPU |
| 18 |
import Futhark.IR.SOACS (SOACS) |
| 19 |
import Futhark.IR.SOACS.SOAC qualified as SOAC |
| 20 |
import Futhark.Tools |
| 21 |
|
| 22 |
getSize :: |
| 23 |
(MonadBuilder m, Op (Rep m) ~ HostOp inner (Rep m)) => |
| 24 |
String -> |
| 25 |
SizeClass -> |
| 26 |
m SubExp |
| 27 |
getSize desc size_class = do |
| 28 |
size_key <- nameFromString . prettyString <$> newVName desc |
| 29 |
letSubExp desc $ Op $ SizeOp $ GetSize size_key size_class |
| 30 |
|
| 31 |
segThread :: |
| 32 |
(MonadBuilder m, Op (Rep m) ~ HostOp inner (Rep m)) => |
| 33 |
String -> |
| 34 |
m SegLevel |
| 35 |
segThread desc = |
| 36 |
SegThread SegVirt <$> (Just <$> kernelGrid) |
| 37 |
where |
| 38 |
kernelGrid = |
| 39 |
KernelGrid |
| 40 |
<$> (Count <$> getSize (desc ++ "_num_tblocks") SizeGrid) |
| 41 |
<*> (Count <$> getSize (desc ++ "_tblock_size") SizeThreadBlock) |
| 42 |
|
| 43 |
injectSOACS :: |
| 44 |
( Monad m, |
| 45 |
SameScope from to, |
| 46 |
ExpDec from ~ ExpDec to, |
| 47 |
BodyDec from ~ BodyDec to, |
| 48 |
RetType from ~ RetType to, |
| 49 |
BranchType from ~ BranchType to, |
| 50 |
Op from ~ SOAC from |
| 51 |
) => |
| 52 |
(SOAC to -> Op to) -> |
| 53 |
Rephraser m from to |
| 54 |
injectSOACS f = |
| 55 |
Rephraser |
| 56 |
{ rephraseExpDec = pure, |
| 57 |
rephraseBodyDec = pure, |
| 58 |
rephraseLetBoundDec = pure, |
| 59 |
rephraseFParamDec = pure, |
| 60 |
rephraseLParamDec = pure, |
| 61 |
rephraseOp = fmap f . onSOAC, |
| 62 |
rephraseRetType = pure, |
| 63 |
rephraseBranchType = pure |
| 64 |
} |
| 65 |
where |
| 66 |
onSOAC = SOAC.mapSOACM mapper |
| 67 |
mapper = |
| 68 |
SOAC.SOACMapper |
| 69 |
{ SOAC.mapOnSOACSubExp = pure, |
| 70 |
SOAC.mapOnSOACVName = pure, |
| 71 |
SOAC.mapOnSOACLambda = rephraseLambda $ injectSOACS f |
| 72 |
} |
| 73 |
|
| 74 |
soacsStmToGPU :: Stm SOACS -> Stm GPU |
| 75 |
soacsStmToGPU = runIdentity . rephraseStm (injectSOACS OtherOp) |
| 76 |
|
| 77 |
soacsLambdaToGPU :: Lambda SOACS -> Lambda GPU |
| 78 |
soacsLambdaToGPU = runIdentity . rephraseLambda (injectSOACS OtherOp) |
| 79 |
|
| 80 |
scopeForSOACs :: Scope GPU -> Scope SOACS |
| 81 |
scopeForSOACs = castScope |
| 82 |
|
| 83 |
scopeForGPU :: Scope SOACS -> Scope GPU |
| 84 |
scopeForGPU = castScope |