{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Massiv.Array.Manifest.Internal
( M
, Manifest(..)
, Array(..)
, toManifest
, compute
, computeS
, computeAs
, computeProxy
, computeSource
, computeWithStride
, computeWithStrideAs
, clone
, convert
, convertAs
, convertProxy
, gcastArr
, fromRaggedArrayM
, fromRaggedArray'
, fromRaggedArray
, sizeofArray
, sizeofMutableArray
) where
import Control.Exception (try)
import Control.Monad.ST
import Control.Scheduler
import qualified Data.Foldable as F (Foldable(..))
import Data.Massiv.Array.Delayed.Pull
import Data.Massiv.Array.Mutable
import Data.Massiv.Array.Ops.Fold.Internal
import Data.Massiv.Core.Common
import Data.Massiv.Core.List
import Data.Maybe (fromMaybe)
import Data.Typeable
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV
import GHC.Base hiding (ord)
import System.IO.Unsafe (unsafePerformIO)
#if MIN_VERSION_primitive(0,6,2)
import Data.Primitive.Array (sizeofArray, sizeofMutableArray)
#else
import qualified Data.Primitive.Array as A (Array(..), MutableArray(..))
import GHC.Exts (sizeofArray#, sizeofMutableArray#)
sizeofArray :: A.Array a -> Int
sizeofArray (A.Array a) = I# (sizeofArray# a)
{-# INLINE sizeofArray #-}
sizeofMutableArray :: A.MutableArray s a -> Int
sizeofMutableArray (A.MutableArray ma) = I# (sizeofMutableArray# ma)
{-# INLINE sizeofMutableArray #-}
#endif
data M
data instance Array M ix e = MArray { mComp :: !Comp
, mSize :: !(Sz ix)
, mLinearIndex :: Int -> e }
type instance EltRepr M ix = M
instance (Ragged L ix e, Show e) => Show (Array M ix e) where
showsPrec = showsArrayPrec id
showList = showArrayList
instance (Eq e, Index ix) => Eq (Array M ix e) where
(==) = eq (==)
{-# INLINE (==) #-}
instance (Ord e, Index ix) => Ord (Array M ix e) where
compare = ord compare
{-# INLINE compare #-}
instance Index ix => Construct M ix e where
setComp c arr = arr {mComp = c}
{-# INLINE setComp #-}
makeArrayLinear !comp !sz f =
unsafePerformIO $ do
let !k = totalElem sz
mv <- MV.unsafeNew k
withScheduler_ comp $ \scheduler ->
splitLinearlyWithM_ scheduler k (pure . f) (MV.unsafeWrite mv)
v <- V.unsafeFreeze mv
pure $ MArray comp sz (V.unsafeIndex v)
{-# INLINE makeArrayLinear #-}
toManifest :: Manifest r ix e => Array r ix e -> Array M ix e
toManifest !arr = MArray (getComp arr) (size arr) (unsafeLinearIndexM arr)
{-# INLINE toManifest #-}
instance Index ix => Foldable (Array M ix) where
fold = fold
{-# INLINE fold #-}
foldMap = foldMono
{-# INLINE foldMap #-}
foldl = lazyFoldlS
{-# INLINE foldl #-}
foldl' = foldlS
{-# INLINE foldl' #-}
foldr = foldrFB
{-# INLINE foldr #-}
foldr' = foldrS
{-# INLINE foldr' #-}
null (MArray _ sz _) = totalElem sz == 0
{-# INLINE null #-}
length = totalElem . size
{-# INLINE length #-}
toList arr = build (\ c n -> foldrFB c n arr)
{-# INLINE toList #-}
instance Index ix => Source M ix e where
unsafeLinearIndex = mLinearIndex
{-# INLINE unsafeLinearIndex #-}
instance Index ix => Manifest M ix e where
unsafeLinearIndexM = mLinearIndex
{-# INLINE unsafeLinearIndexM #-}
instance Index ix => Resize M ix where
unsafeResize !sz !arr = arr { mSize = sz }
{-# INLINE unsafeResize #-}
instance Index ix => Extract M ix e where
unsafeExtract !sIx !newSz !arr =
MArray (getComp arr) newSz $ \ i ->
unsafeIndex arr (liftIndex2 (+) (fromLinearIndex newSz i) sIx)
{-# INLINE unsafeExtract #-}
instance {-# OVERLAPPING #-} Slice M Ix1 e where
unsafeSlice arr i _ _ = pure (unsafeLinearIndex arr i)
{-# INLINE unsafeSlice #-}
instance ( Index ix
, Index (Lower ix)
, Elt M ix e ~ Array M (Lower ix) e
) =>
Slice M ix e where
unsafeSlice arr start cutSz dim = do
(_, newSz) <- pullOutSzM cutSz dim
return $ unsafeResize newSz (unsafeExtract start cutSz arr)
{-# INLINE unsafeSlice #-}
instance {-# OVERLAPPING #-} OuterSlice M Ix1 e where
unsafeOuterSlice !arr = unsafeIndex arr
{-# INLINE unsafeOuterSlice #-}
instance (Elt M ix e ~ Array M (Lower ix) e, Index ix, Index (Lower ix)) => OuterSlice M ix e where
unsafeOuterSlice !arr !i =
MArray (getComp arr) (snd (unconsSz (size arr))) (unsafeLinearIndex arr . (+ kStart))
where
!kStart = toLinearIndex (size arr) (consDim i (zeroIndex :: Lower ix))
{-# INLINE unsafeOuterSlice #-}
instance {-# OVERLAPPING #-} InnerSlice M Ix1 e where
unsafeInnerSlice !arr _ = unsafeIndex arr
{-# INLINE unsafeInnerSlice #-}
instance (Elt M ix e ~ Array M (Lower ix) e, Index ix, Index (Lower ix)) => InnerSlice M ix e where
unsafeInnerSlice !arr (szL, m) !i =
MArray (getComp arr) szL (\k -> unsafeLinearIndex arr (k * unSz m + kStart))
where
!kStart = toLinearIndex (size arr) (snocDim (zeroIndex :: Lower ix) i)
{-# INLINE unsafeInnerSlice #-}
instance Index ix => Load M ix e where
size = mSize
{-# INLINE size #-}
getComp = mComp
{-# INLINE getComp #-}
loadArrayM scheduler (MArray _ sz f) = splitLinearlyWith_ scheduler (totalElem sz) f
{-# INLINE loadArrayM #-}
instance Index ix => StrideLoad M ix e
compute :: forall r ix e r' . (Mutable r ix e, Load r' ix e) => Array r' ix e -> Array r ix e
compute !arr = unsafePerformIO $ loadArray arr >>= unsafeFreeze (getComp arr)
{-# INLINE compute #-}
computeS :: forall r ix e r' . (Mutable r ix e, Load r' ix e) => Array r' ix e -> Array r ix e
computeS !arr = runST $ loadArrayS arr >>= unsafeFreeze (getComp arr)
{-# INLINE computeS #-}
computeAs :: (Mutable r ix e, Load r' ix e) => r -> Array r' ix e -> Array r ix e
computeAs _ = compute
{-# INLINE computeAs #-}
computeProxy :: (Mutable r ix e, Load r' ix e) => proxy r -> Array r' ix e -> Array r ix e
computeProxy _ = compute
{-# INLINE computeProxy #-}
computeSource :: forall r ix e r' . (Mutable r ix e, Source r' ix e)
=> Array r' ix e -> Array r ix e
computeSource arr = maybe (compute arr) (\Refl -> arr) (eqT :: Maybe (r' :~: r))
{-# INLINE computeSource #-}
clone :: Mutable r ix e => Array r ix e -> Array r ix e
clone arr = unsafePerformIO $ thaw arr >>= unsafeFreeze (getComp arr)
{-# INLINE clone #-}
gcastArr :: forall r ix e r' . (Typeable r, Typeable r')
=> Array r' ix e -> Maybe (Array r ix e)
gcastArr arr = fmap (\Refl -> arr) (eqT :: Maybe (r :~: r'))
convert :: forall r ix e r' . (Mutable r ix e, Load r' ix e)
=> Array r' ix e -> Array r ix e
convert arr = fromMaybe (compute arr) (gcastArr arr)
{-# INLINE convert #-}
convertAs :: (Mutable r ix e, Load r' ix e)
=> r -> Array r' ix e -> Array r ix e
convertAs _ = convert
{-# INLINE convertAs #-}
convertProxy :: (Mutable r ix e, Load r' ix e)
=> proxy r -> Array r' ix e -> Array r ix e
convertProxy _ = convert
{-# INLINE convertProxy #-}
fromRaggedArray :: (Mutable r ix e, Ragged r' ix e, Load r' ix e) =>
Array r' ix e -> Either ShapeException (Array r ix e)
fromRaggedArray arr =
unsafePerformIO $ do
let !sz = edgeSize arr
!comp = getComp arr
mArr <- unsafeNew sz
try $ do
withScheduler_ comp $ \scheduler ->
loadRagged (scheduleWork scheduler) (unsafeLinearWrite mArr) 0 (totalElem sz) sz arr
unsafeFreeze comp mArr
{-# INLINE fromRaggedArray #-}
{-# DEPRECATED fromRaggedArray "In favor of a more general `fromRaggedArrayM`" #-}
fromRaggedArrayM ::
forall r ix e r' m . (Mutable r ix e, Ragged r' ix e, Load r' ix e, MonadThrow m)
=> Array r' ix e
-> m (Array r ix e)
fromRaggedArrayM arr =
let sz = edgeSize arr
in either (\(e :: ShapeException) -> throwM e) pure $
unsafePerformIO $ do
marr <- unsafeNew sz
traverse (\_ -> unsafeFreeze (getComp arr) marr) =<<
try (withScheduler_ (getComp arr) $ \scheduler ->
loadRagged (scheduleWork scheduler) (unsafeLinearWrite marr) 0 (totalElem sz) sz arr)
{-# INLINE fromRaggedArrayM #-}
fromRaggedArray' ::
forall r ix e r'. (Mutable r ix e, Load r' ix e, Ragged r' ix e)
=> Array r' ix e
-> Array r ix e
fromRaggedArray' arr = either throw id $ fromRaggedArrayM arr
{-# INLINE fromRaggedArray' #-}
computeWithStride ::
forall r ix e r'. (Mutable r ix e, StrideLoad r' ix e)
=> Stride ix
-> Array r' ix e
-> Array r ix e
computeWithStride stride !arr =
unsafePerformIO $ do
let !sz = strideSize stride (size arr)
createArray_ (getComp arr) sz $ \scheduler marr ->
loadArrayWithStrideM scheduler stride sz arr (unsafeLinearWrite marr)
{-# INLINE computeWithStride #-}
computeWithStrideAs ::
(Mutable r ix e, StrideLoad r' ix e) => r -> Stride ix -> Array r' ix e -> Array r ix e
computeWithStrideAs _ = computeWithStride
{-# INLINE computeWithStrideAs #-}