|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE PolyKinds #-} |
| 3 | +{-# LANGUAGE TemplateHaskell #-} |
| 4 | +{-# LANGUAGE QuasiQuotes #-} |
| 5 | +{-# LANGUAGE OverloadedStrings #-} |
| 6 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 7 | +{-# LANGUAGE TypeApplications #-} |
| 8 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 9 | +{-# LANGUAGE FlexibleInstances #-} |
| 10 | + |
| 11 | +module GpuCpp where |
| 12 | + |
| 13 | +import qualified Language.C.Inline.Cpp as C |
| 14 | +import qualified Language.C.Inline.Cpp.Unsafe as C |
| 15 | +import qualified Language.C.Inline.Context as C |
| 16 | +import Foreign.C.String |
| 17 | +import Foreign.C.Types |
| 18 | +import GHC.Int |
| 19 | +import GHC.ForeignPtr(mallocPlainForeignPtrBytes) |
| 20 | +import Foreign |
| 21 | +import Control.Monad (forM_) |
| 22 | +import GpuCpp.Types |
| 23 | +import Control.Exception.Safe (bracket) |
| 24 | +import qualified Data.Vector.Storable as V |
| 25 | + |
| 26 | +C.context $ C.cppCtx <> mempty { C.ctxTypesTable = typeTable } |
| 27 | + |
| 28 | +C.include "<gpu.hpp>" |
| 29 | +C.include "<future>" |
| 30 | +C.include "<vector>" |
| 31 | + |
| 32 | +[C.emitBlock| |
| 33 | +struct GpuAsync { |
| 34 | + std::promise<void> promise; |
| 35 | + std::future<void> future; |
| 36 | + GpuAsync(): future(promise.get_future()){ |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +gpu::Shape vector_to_shape(const std::vector<int64_t> &dims) { |
| 41 | + switch(dims.size()){ |
| 42 | + case 1: |
| 43 | + return gpu::Shape{(unsigned long)dims[0]}; |
| 44 | + break; |
| 45 | + case 2: |
| 46 | + return gpu::Shape{(unsigned long)dims[0],(unsigned long)dims[1]}; |
| 47 | + break; |
| 48 | + case 3: |
| 49 | + return gpu::Shape{(unsigned long)dims[0],(unsigned long)dims[1],(unsigned long)dims[2]}; |
| 50 | + break; |
| 51 | + case 4: |
| 52 | + return gpu::Shape{(unsigned long)dims[0],(unsigned long)dims[1],(unsigned long)dims[2],(unsigned long)dims[3]}; |
| 53 | + break; |
| 54 | + case 5: |
| 55 | + return gpu::Shape{(unsigned long)dims[0],(unsigned long)dims[1],(unsigned long)dims[2],(unsigned long)dims[3],(unsigned long)dims[4]}; |
| 56 | + break; |
| 57 | + } |
| 58 | + return gpu::Shape{0}; |
| 59 | +} |
| 60 | +|] |
| 61 | + |
| 62 | +kf32 :: CInt |
| 63 | +kf32 = [C.pure| int { (int)gpu::kf32 } |] |
| 64 | + |
| 65 | +createContext :: IO (ForeignPtr Context) |
| 66 | +createContext = |
| 67 | + [C.throwBlock| gpu::Context* { return new gpu::Context(gpu::createContext()); }|] >>= |
| 68 | + newForeignPtr |
| 69 | + [C.funPtr| void deleteContext(gpu::Context* ptr) { delete ptr; }|] |
| 70 | + |
| 71 | + |
| 72 | +createKernelCode :: String -> CInt -> CInt -> IO (ForeignPtr KernelCode) |
| 73 | +createKernelCode kernelString workgroupSize precision = |
| 74 | + withCString kernelString $ \pData -> |
| 75 | + [C.throwBlock| gpu::KernelCode* { return new gpu::KernelCode($(char* pData), $(int workgroupSize), (gpu::NumType)$(int precision)); }|] >>= |
| 76 | + newForeignPtr |
| 77 | + [C.funPtr| void deleteKernelCode(gpu::KernelCode* ptr) { delete ptr; }|] |
| 78 | + |
| 79 | + |
| 80 | +dispatchKernel :: ForeignPtr Context -> ForeignPtr Kernel -> IO (ForeignPtr GpuAsync) |
| 81 | +dispatchKernel context kernel = |
| 82 | + withForeignPtr context $ \c -> |
| 83 | + withForeignPtr kernel $ \k -> |
| 84 | + [C.throwBlock| GpuAsync* { |
| 85 | + auto async = new GpuAsync(); |
| 86 | + gpu::dispatchKernel(*$(gpu::Context* c), *$(gpu::Kernel* k), async->promise); |
| 87 | + return async; }|] >>= |
| 88 | + newForeignPtr |
| 89 | + [C.funPtr| void deleteGpuAsync(GpuAsync* ptr) { delete ptr; }|] |
| 90 | + |
| 91 | +wait :: ForeignPtr Context -> ForeignPtr GpuAsync -> IO () |
| 92 | +wait context async = |
| 93 | + withForeignPtr context $ \c -> |
| 94 | + withForeignPtr async $ \a -> |
| 95 | + [C.throwBlock| void { |
| 96 | + gpu::wait(*$(gpu::Context* c), $(GpuAsync* a)->future); |
| 97 | + }|] |
| 98 | + |
| 99 | +instance WithVector CInt Int64 where |
| 100 | + withVector shape func = |
| 101 | + bracket |
| 102 | + (do |
| 103 | + let len = fromIntegral $ length shape |
| 104 | + vec <- [C.throwBlock| std::vector<int64_t>* { |
| 105 | + return new std::vector<int64_t>($(int len)); |
| 106 | + }|] |
| 107 | + ptr <- [C.throwBlock| int64_t* { |
| 108 | + return $(std::vector<int64_t>* vec)->data(); |
| 109 | + }|] |
| 110 | + pokeArray ptr (map fromIntegral shape) |
| 111 | + return vec |
| 112 | + ) |
| 113 | + (\vec -> [C.block| void { delete $(std::vector<int64_t>* vec); }|]) |
| 114 | + (\vec -> func vec) |
| 115 | + |
| 116 | +instance WithVector CInt CSize where |
| 117 | + withVector shape func = |
| 118 | + bracket |
| 119 | + (do |
| 120 | + let len = fromIntegral $ length shape |
| 121 | + vec <- [C.throwBlock| std::vector<size_t>* { |
| 122 | + return new std::vector<size_t>($(int len)); |
| 123 | + }|] |
| 124 | + ptr <- [C.throwBlock| size_t* { |
| 125 | + return $(std::vector<size_t>* vec)->data(); |
| 126 | + }|] |
| 127 | + pokeArray ptr (map fromIntegral shape) |
| 128 | + return vec |
| 129 | + ) |
| 130 | + (\vec -> [C.block| void { delete $(std::vector<size_t>* vec); }|]) |
| 131 | + (\vec -> func vec) |
| 132 | + |
| 133 | +instance WithVector (Ptr Tensor) Tensor where |
| 134 | + withVector ptrs func = |
| 135 | + bracket (do |
| 136 | + vec <- [C.throwBlock| std::vector<gpu::Tensor>* { return new std::vector<gpu::Tensor>(); }|] |
| 137 | + forM_ ptrs $ do |
| 138 | + \ptr -> [C.throwBlock| void { $(std::vector<gpu::Tensor>* vec)->push_back(*$(gpu::Tensor* ptr)); }|] |
| 139 | + return vec |
| 140 | + ) |
| 141 | + (\vec -> [C.block| void { delete $(std::vector<gpu::Tensor>* vec); }|]) |
| 142 | + (\vec -> func vec) |
| 143 | + |
| 144 | +withForeignPtrs :: [ForeignPtr a] -> ([Ptr a] -> IO b) -> IO b |
| 145 | +withForeignPtrs [] func = func [] |
| 146 | +withForeignPtrs (x:xs) func = |
| 147 | + withForeignPtr x $ \x' -> |
| 148 | + withForeignPtrs xs $ \xs' -> |
| 149 | + func (x':xs') |
| 150 | + |
| 151 | +createKernel :: ForeignPtr Context -> ForeignPtr KernelCode -> [ForeignPtr Tensor] -> [Int] -> [Int] -> IO (ForeignPtr Kernel) |
| 152 | +createKernel context kernelCode dataBindings viewOffsets totalWorkgroups = |
| 153 | + withForeignPtr context $ \c -> |
| 154 | + withForeignPtr kernelCode $ \k -> |
| 155 | + withForeignPtrs dataBindings $ \b -> |
| 156 | + withVector b $ \b' -> |
| 157 | + withVector @CInt (map fromIntegral viewOffsets) $ \v -> |
| 158 | + withVector @CInt (map fromIntegral totalWorkgroups) $ \w -> |
| 159 | + [C.throwBlock| gpu::Kernel* { |
| 160 | + return new gpu::Kernel(gpu::createKernel( |
| 161 | + *$(gpu::Context* c), |
| 162 | + *$(gpu::KernelCode* k), |
| 163 | + $(std::vector<gpu::Tensor>* b')->data(), |
| 164 | + $(std::vector<gpu::Tensor>* b')->size(), |
| 165 | + $(std::vector<size_t>* v)->data(), |
| 166 | + vector_to_shape(*$(std::vector<int64_t>* w)))); |
| 167 | + }|] >>= |
| 168 | + newForeignPtr |
| 169 | + [C.funPtr| void deleteKernel(gpu::Kernel* ptr) { delete ptr; }|] |
| 170 | + |
| 171 | +createTensor :: ForeignPtr Context -> [CInt] -> CInt -> IO (ForeignPtr Tensor) |
| 172 | +createTensor context shape dtype = |
| 173 | + withVector shape $ \s -> |
| 174 | + withForeignPtr context $ \c -> |
| 175 | + [C.throwBlock| gpu::Tensor* { |
| 176 | + return new gpu::Tensor(gpu::createTensor(*$(gpu::Context* c), vector_to_shape(*$(std::vector<int64_t>* s)), (gpu::NumType)$(int dtype))); |
| 177 | + }|] >>= |
| 178 | + newForeignPtr |
| 179 | + [C.funPtr| void deleteTensor(gpu::Tensor* ptr) { delete ptr; }|] |
| 180 | + |
| 181 | +createVector :: forall a. Storable a => Int -> IO (V.Vector a) |
| 182 | +createVector n = do |
| 183 | + ptr <- mallocPlainForeignPtrBytes (n * sizeOf (undefined :: a)) |
| 184 | + return $ V.unsafeFromForeignPtr ptr 0 n |
| 185 | + |
| 186 | +instance GpuStorable CFloat where |
| 187 | + toGpu context array tensor = |
| 188 | + withForeignPtr context $ \c -> |
| 189 | + withForeignPtr tensor $ \t -> |
| 190 | + V.unsafeWith array $ \ptr -> |
| 191 | + [C.throwBlock| void { |
| 192 | + gpu::toGPU(*$(gpu::Context* c), $(float* ptr), *$(gpu::Tensor* t)); |
| 193 | + }|] |
| 194 | + toCpu context tensor = |
| 195 | + withForeignPtr context $ \c -> |
| 196 | + withForeignPtr tensor $ \t -> do |
| 197 | + (size :: CInt) <- [C.block| int { |
| 198 | + size_t u = sizeof(float); |
| 199 | + size_t len = $(gpu::Tensor* t)->data.size; |
| 200 | + return len/u; |
| 201 | + }|] |
| 202 | + array <- createVector (fromIntegral size) |
| 203 | + V.unsafeWith array $ \ptr -> |
| 204 | + [C.throwBlock| void { |
| 205 | + gpu::toCPU(*$(gpu::Context* c), *$(gpu::Tensor* t), $(float* ptr), $(int size) * sizeof(float)); |
| 206 | + }|] |
| 207 | + return array |
0 commit comments