module Data.Array.Repa.Repr.Unboxed
( U, U.Unbox, Array (..)
, computeUnboxedS, computeUnboxedP
, fromListUnboxed
, fromUnboxed, toUnboxed
, zip, zip3, zip4, zip5, zip6
, unzip, unzip3, unzip4, unzip5, unzip6)
where
import Data.Array.Repa.Shape as R
import Data.Array.Repa.Base as R
import Data.Array.Repa.Eval as R
import Data.Array.Repa.Repr.Delayed as R
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import Control.Monad
import Prelude hiding (zip, zip3, unzip, unzip3)
data U
instance U.Unbox a => Source U a where
data Array U sh a
= AUnboxed !sh !(U.Vector a)
linearIndex (AUnboxed _ vec) ix
= vec U.! ix
unsafeLinearIndex (AUnboxed _ vec) ix
= vec `U.unsafeIndex` ix
extent (AUnboxed sh _)
= sh
deepSeqArray (AUnboxed sh vec) x
= sh `deepSeq` vec `seq` x
deriving instance (Show sh, Show e, U.Unbox e)
=> Show (Array U sh e)
deriving instance (Read sh, Read e, U.Unbox e)
=> Read (Array U sh e)
instance U.Unbox e => Target U e where
data MVec U e
= UMVec (UM.IOVector e)
newMVec n
= liftM UMVec (UM.new n)
unsafeWriteMVec (UMVec v) ix
= UM.unsafeWrite v ix
unsafeFreezeMVec sh (UMVec mvec)
= do vec <- U.unsafeFreeze mvec
return $ AUnboxed sh vec
deepSeqMVec (UMVec vec) x
= vec `seq` x
touchMVec _
= return ()
computeUnboxedS
:: ( Shape sh
, Load r1 sh e, U.Unbox e)
=> Array r1 sh e -> Array U sh e
computeUnboxedS = computeS
computeUnboxedP
:: ( Shape sh
, Load r1 sh e, Monad m, U.Unbox e)
=> Array r1 sh e -> m (Array U sh e)
computeUnboxedP = computeP
fromListUnboxed
:: (Shape sh, U.Unbox a)
=> sh -> [a] -> Array U sh a
fromListUnboxed = R.fromList
fromUnboxed
:: (Shape sh, U.Unbox e)
=> sh -> U.Vector e -> Array U sh e
fromUnboxed sh vec
= AUnboxed sh vec
toUnboxed
:: U.Unbox e
=> Array U sh e -> U.Vector e
toUnboxed (AUnboxed _ vec)
= vec
zip :: (Shape sh, U.Unbox a, U.Unbox b)
=> Array U sh a -> Array U sh b
-> Array U sh (a, b)
zip (AUnboxed sh1 vec1) (AUnboxed sh2 vec2)
| sh1 /= sh2 = error "Repa: zip array shapes not identical"
| otherwise = AUnboxed sh1 (U.zip vec1 vec2)
zip3 :: (Shape sh, U.Unbox a, U.Unbox b, U.Unbox c)
=> Array U sh a -> Array U sh b -> Array U sh c
-> Array U sh (a, b, c)
zip3 (AUnboxed sh1 vec1) (AUnboxed sh2 vec2) (AUnboxed sh3 vec3)
| sh1 /= sh2 || sh1 /= sh3
= error "Repa: zip array shapes not identical"
| otherwise = AUnboxed sh1 (U.zip3 vec1 vec2 vec3)
zip4 :: (Shape sh, U.Unbox a, U.Unbox b, U.Unbox c, U.Unbox d)
=> Array U sh a -> Array U sh b -> Array U sh c -> Array U sh d
-> Array U sh (a, b, c, d)
zip4 (AUnboxed sh1 vec1) (AUnboxed sh2 vec2) (AUnboxed sh3 vec3) (AUnboxed sh4 vec4)
| sh1 /= sh2 || sh1 /= sh3 || sh1 /= sh4
= error "Repa: zip array shapes not identical"
| otherwise = AUnboxed sh1 (U.zip4 vec1 vec2 vec3 vec4)
zip5 :: (Shape sh, U.Unbox a, U.Unbox b, U.Unbox c, U.Unbox d, U.Unbox e)
=> Array U sh a -> Array U sh b -> Array U sh c -> Array U sh d -> Array U sh e
-> Array U sh (a, b, c, d, e)
zip5 (AUnboxed sh1 vec1) (AUnboxed sh2 vec2) (AUnboxed sh3 vec3) (AUnboxed sh4 vec4) (AUnboxed sh5 vec5)
| sh1 /= sh2 || sh1 /= sh3 || sh1 /= sh4 || sh1 /= sh5
= error "Repa: zip array shapes not identical"
| otherwise = AUnboxed sh1 (U.zip5 vec1 vec2 vec3 vec4 vec5)
zip6 :: (Shape sh, U.Unbox a, U.Unbox b, U.Unbox c, U.Unbox d, U.Unbox e, U.Unbox f)
=> Array U sh a -> Array U sh b -> Array U sh c -> Array U sh d -> Array U sh e -> Array U sh f
-> Array U sh (a, b, c, d, e, f)
zip6 (AUnboxed sh1 vec1) (AUnboxed sh2 vec2) (AUnboxed sh3 vec3) (AUnboxed sh4 vec4) (AUnboxed sh5 vec5) (AUnboxed sh6 vec6)
| sh1 /= sh2 || sh1 /= sh3 || sh1 /= sh4 || sh1 /= sh5 || sh1 /= sh6
= error "Repa: zip array shapes not identical"
| otherwise = AUnboxed sh1 (U.zip6 vec1 vec2 vec3 vec4 vec5 vec6)
unzip :: (U.Unbox a, U.Unbox b)
=> Array U sh (a, b)
-> (Array U sh a, Array U sh b)
unzip (AUnboxed sh vec)
= let (as, bs) = U.unzip vec
in (AUnboxed sh as, AUnboxed sh bs)
unzip3 :: (U.Unbox a, U.Unbox b, U.Unbox c)
=> Array U sh (a, b, c)
-> (Array U sh a, Array U sh b, Array U sh c)
unzip3 (AUnboxed sh vec)
= let (as, bs, cs) = U.unzip3 vec
in (AUnboxed sh as, AUnboxed sh bs, AUnboxed sh cs)
unzip4 :: (U.Unbox a, U.Unbox b, U.Unbox c, U.Unbox d)
=> Array U sh (a, b, c, d)
-> (Array U sh a, Array U sh b, Array U sh c, Array U sh d)
unzip4 (AUnboxed sh vec)
= let (as, bs, cs, ds) = U.unzip4 vec
in (AUnboxed sh as, AUnboxed sh bs, AUnboxed sh cs, AUnboxed sh ds)
unzip5 :: (U.Unbox a, U.Unbox b, U.Unbox c, U.Unbox d, U.Unbox e)
=> Array U sh (a, b, c, d, e)
-> (Array U sh a, Array U sh b, Array U sh c, Array U sh d, Array U sh e)
unzip5 (AUnboxed sh vec)
= let (as, bs, cs, ds, es) = U.unzip5 vec
in (AUnboxed sh as, AUnboxed sh bs, AUnboxed sh cs, AUnboxed sh ds, AUnboxed sh es)
unzip6 :: (U.Unbox a, U.Unbox b, U.Unbox c, U.Unbox d, U.Unbox e, U.Unbox f)
=> Array U sh (a, b, c, d, e, f)
-> (Array U sh a, Array U sh b, Array U sh c, Array U sh d, Array U sh e, Array U sh f)
unzip6 (AUnboxed sh vec)
= let (as, bs, cs, ds, es, fs) = U.unzip6 vec
in (AUnboxed sh as, AUnboxed sh bs, AUnboxed sh cs, AUnboxed sh ds, AUnboxed sh es, AUnboxed sh fs)