Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
{-# LANGUAGE BangPatterns, FlexibleInstances, MultiParamTypeClasses, DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies, CPP, MagicHash, UnboxedTuples #-}
{-# OPTIONS_GHC -O3 -Wall -fforce-recomp #-}
module Main (main) where
import Foreign.C.Types (CSize)
import Foreign.Storable
import Foreign.ForeignPtr
import Foreign.Marshal.Array (advancePtr, copyArray, moveArray)
import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
import qualified GHC.ForeignPtr as FP
import GHC.Prim
import GHC.Exts
import GHC.Base
import Data.Word (Word8)
import qualified Data.Word as W
import Control.Monad.Primitive
import qualified Data.Primitive.Types as PT
import qualified Data.Primitive.ByteArray as BA
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Generic.Mutable as GM
import Data.Vector.Storable.Internal (getPtr, updPtr)
import qualified Data.Vector.Unboxed as UV
import qualified Data.Vector.Storable as SV
import Data.Typeable (Typeable)
data MVector s a = MVector {-# UNPACK #-} !Int
{-# UNPACK #-} !(ForeignPtr a)
deriving (Typeable)
instance Storable a => GM.MVector MVector a where
basicLength (MVector n _) = n
{-# INLINE basicLength #-}
basicUnsafeSlice j m (MVector _ fp) = MVector m (updPtr (`advancePtr` j) fp)
{-# INLINE basicUnsafeSlice #-}
-- FIXME: this relies on non-portable pointer comparisons
basicOverlaps (MVector m fp) (MVector n fq) =
between p q (q `advancePtr` n) || between q p (p `advancePtr` m)
where
between x y z = x >= y && x < z
p = getPtr fp
q = getPtr fq
{-# INLINE basicOverlaps #-}
basicUnsafeNew n = unsafePrimToPrim $ do
fp <- mallocVector n 16
return $ MVector n fp
{-# INLINE basicUnsafeNew #-}
{-# SPECIALIZE basicUnsafeNew ::
(Storable a, PrimMonad m) => Int -> m (MVector (PrimState m) a) #-}
basicUnsafeRead (MVector _ fp) i =
unsafePrimToPrim $ withForeignPtr fp (`peekElemOff` i)
{-# INLINE basicUnsafeRead #-}
basicUnsafeWrite (MVector _ fp) i x =
unsafePrimToPrim $ withForeignPtr fp $ \p -> pokeElemOff p i x
{-# INLINE basicUnsafeWrite #-}
basicUnsafeCopy (MVector n fp) (MVector _ fq) =
unsafePrimToPrim $ withForeignPtr fp $ \p ->
withForeignPtr fq $ \q ->
copyArray p q n
{-# INLINE basicUnsafeCopy #-}
basicUnsafeMove (MVector n fp) (MVector _ fq) =
unsafePrimToPrim $ withForeignPtr fp $ \p ->
withForeignPtr fq $ \q ->
moveArray p q n
{-# INLINE basicUnsafeMove #-}
mallocVectorMine1 :: Storable a => Int -> Int -> IO (ForeignPtr a)
mallocVectorMine1 = doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> Int -> IO (ForeignPtr b)
doMalloc b !l !align = do
!ba <- BA.newAlignedPinnedByteArray bytes align
let !(PT.Addr addr) = BA.mutableByteArrayContents ba
!ptr = Ptr addr
newForeignPtr_ ptr
where
bytes :: Int
!bytes = sizeOf b * l
{-# INLINE bytes #-}
{-# INLINE doMalloc #-}
{-# SPECIALIZE doMalloc :: W.Word8 -> Int -> Int -> IO (ForeignPtr W.Word8) #-}
{-# INLINE mallocVectorMine1 #-}
{-# SPECIALIZE mallocVectorMine1 :: Int -> Int -> IO (ForeignPtr W.Word8) #-}
mallocVectorMine2 :: Storable a => Int -> Int -> IO (ForeignPtr a)
mallocVectorMine2 = doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> Int -> IO (ForeignPtr b)
doMalloc b !(I# len) !(I# align) = IO $ \s0 ->
case newAlignedPinnedByteArray# bytes align s0 of { (# s1, mbarr# #) ->
let ptr = Ptr (byteArrayContents# (unsafeCoerce# mbarr#)) in
let (IO f) = newForeignPtr_ ptr in
case f s1 of { (# s2, fp# #) -> (# s2, fp# #) } }
where
!(I# size) = sizeOf b
{-# INLINE size #-}
!bytes = len *# size
{-# INLINE bytes #-}
{-# INLINE doMalloc #-}
{-# SPECIALIZE doMalloc :: W.Word8 -> Int -> Int -> IO (ForeignPtr W.Word8) #-}
{-# INLINE mallocVectorMine2 #-}
{-# SPECIALIZE mallocVectorMine2 :: Int -> Int -> IO (ForeignPtr W.Word8) #-}
mallocVectorOrig :: Storable a => Int -> Int -> IO (ForeignPtr a)
mallocVectorOrig =
doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> Int -> IO (ForeignPtr b)
doMalloc dummy size _ = mallocPlainForeignPtrBytes (size * sizeOf dummy)
{-# INLINE mallocVectorOrig #-}
#ifdef MALLOC_MINE3
mallocVectorMine3 :: Storable a => Int -> Int -> IO (ForeignPtr a)
mallocVectorMine3 =
doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> Int -> IO (ForeignPtr b)
doMalloc dummy size align =
FP.mallocPlainForeignPtrAlignedBytes (size * sizeOf dummy) align
{-# INLINE doMalloc #-}
{-# SPECIALIZE doMalloc :: W.Word8 -> Int -> Int -> IO (ForeignPtr W.Word8) #-}
{-# INLINE mallocVectorMine3 #-}
{-# SPECIALIZE mallocVectorMine2 :: Int -> Int -> IO (ForeignPtr W.Word8) #-}
#endif
mallocVector :: Storable a => Int -> Int -> IO (ForeignPtr a)
#ifdef MALLOC_ORIG
mallocVector = mallocVectorOrig
#elif defined MALLOC_MINE1
mallocVector = mallocVectorMine1
#elif defined MALLOC_MINE2
mallocVector = mallocVectorMine2
#elif defined MALLOC_MINE3
mallocVector = mallocVectorMine3
#else
# error No malloc define
#endif
{-# INLINE mallocVector #-}
{-# SPECIALIZE mallocVector :: Int -> Int -> IO (ForeignPtr W.Word8) #-}
data Vector a = Vector {-# UNPACK #-} !Int
{-# UNPACK #-} !(ForeignPtr a)
deriving (Typeable)
type instance G.Mutable Vector = MVector
instance Storable a => G.Vector Vector a where
basicUnsafeFreeze (MVector n fp) = return $ Vector n fp
{-# INLINE basicUnsafeFreeze #-}
basicUnsafeThaw (Vector n fp) = return $ MVector n fp
{-# INLINE basicUnsafeThaw #-}
basicLength (Vector n _) = n
{-# INLINE basicLength #-}
basicUnsafeSlice i n (Vector _ fp) = Vector n (updPtr (`advancePtr` i) fp)
{-# INLINE basicUnsafeSlice #-}
basicUnsafeIndexM (Vector _ fp) i = return
. unsafeInlineIO
$ withForeignPtr fp $ \p ->
peekElemOff p i
{-# INLINE basicUnsafeIndexM #-}
basicUnsafeCopy (MVector n fp) (Vector _ fq) =
unsafePrimToPrim $ withForeignPtr fp $ \p ->
withForeignPtr fq $ \q ->
copyArray p q n
{-# INLINE basicUnsafeCopy #-}
elemseq _ = seq
{-# INLINE elemseq #-}
fromListN :: Storable a => Int -> [a] -> Vector a
fromListN = G.fromListN
{-# INLINE fromListN #-}
toList :: Storable a => Vector a -> [a]
toList = G.toList
{-# INLINE toList #-}
main :: IO ()
main = loop 0
where
loop n | n > 8192 = putStrLn "OK!"
| otherwise = do
print n
let uv = gen UV.fromListN n
sv = gen SV.fromListN n
mv = gen fromListN n :: Vector Word8
putStrLn $ "uv: " ++ show (UV.toList uv)
putStrLn $ "mv: " ++ show (toList mv)
if UV.toList uv /= (take n $ cycle [0 :: Word8 .. 255])
then print "Oops"
else return ()
if SV.toList sv /= (take n $ cycle [0 :: Word8 .. 255])
then print "Oop 2"
else return ()
if toList mv /= (take n $ cycle [0 :: Word8 .. 255])
then do
print "Oops3"
print $ zip3 [(0 :: Int) ..] (cycle [(0 :: Word8) .. 255]) (toList mv)
print $ head $ dropWhile (\(_, b, c) -> b == c) $ zip3 [(0 :: Int) ..] (cycle [(0 :: Word8) .. 255]) (toList mv)
else return ()
if (UV.toList uv /= toList mv)
then do
putStrLn $ "Fail at " ++ show n
putStrLn $ "mv: " ++ show (toList mv)
else loop (n + 16)
gen :: (Int -> [Word8] -> v Word8) -> Int -> v Word8
gen f n = a
where
!a = f n $ cycle [0 :: Word8 .. 255]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.