Commit 08d671b8 authored by rl@cse.unsw.edu.au's avatar rl@cse.unsw.edu.au
Browse files

Make PA an explicit record instead of a typeclass

parent 01e30d9b
module Data.Array.Parallel.Lifted (
module Data.Array.Parallel.Lifted.PArray,
(:->), ($:), ($:^)
(:->), ($:), ($:^),
dPA_Int
) where
import Data.Array.Parallel.Lifted.PArray
......
module Data.Array.Parallel.Lifted.Closure (
(:->)(..), PArray(..),
mkClosure, mkClosureP, ($:), ($:^)
mkClosure, mkClosureP, ($:), ($:^),
dPA_Clo
) where
import Data.Array.Parallel.Lifted.PArray
......@@ -10,14 +11,15 @@ infixr 0 $:, $:^
-- |The type of closures
--
data a :-> b = forall e. PA e => Clo !(e -> a -> b)
!(PArray e -> PArray a -> PArray b)
e
data a :-> b = forall e. Clo (PA e)
!(e -> a -> b)
!(PArray e -> PArray a -> PArray b)
e
-- |Closure construction
--
mkClosure :: forall a b e.
PA e => (e -> a -> b)
PA e -> (e -> a -> b)
-> (PArray e -> PArray a -> PArray b)
-> e -> (a :-> b)
{-# INLINE mkClosure #-}
......@@ -27,19 +29,20 @@ mkClosure = Clo
--
($:) :: forall a b. (a :-> b) -> a -> b
{-# INLINE ($:) #-}
Clo f _ e $: a = f e a
Clo _ f _ e $: a = f e a
-- |Arrays of closures (aka array closures)
--
data instance PArray (a :-> b)
= forall e. PA e => AClo !(e -> a -> b)
!(PArray e -> PArray a -> PArray b)
!(PArray e)
= forall e. AClo (PA e)
!(e -> a -> b)
!(PArray e -> PArray a -> PArray b)
!(PArray e)
-- |Lifted closure construction
--
mkClosureP :: forall a b e.
PA e => (e -> a -> b)
PA e -> (e -> a -> b)
-> (PArray e -> PArray a -> PArray b)
-> PArray e -> PArray (a :-> b)
{-# INLINE mkClosureP #-}
......@@ -49,11 +52,18 @@ mkClosureP = AClo
--
($:^) :: forall a b. PArray (a :-> b) -> PArray a -> PArray b
{-# INLINE ($:^) #-}
AClo _ f es $:^ as = f es as
AClo _ _ f es $:^ as = f es as
instance (PA a, PA b) => PA (a :-> b) where
{-# INLINE lengthPA #-}
lengthPA (AClo _ _ es) = lengthPA es
{-# INLINE replicatePA #-}
replicatePA n# (Clo f f' e) = AClo f f' (replicatePA n# e)
dPA_Clo :: PA a -> PA b -> PA (a :-> b)
{-# INLINE dPA_Clo #-}
dPA_Clo _ _ = PA {
lengthPA = lengthPA_Clo
, replicatePA = replicatePA_Clo
}
{-# INLINE lengthPA_Clo #-}
lengthPA_Clo (AClo pa f f' es) = lengthPA pa es
{-# INLINE replicatePA_Clo #-}
replicatePA_Clo n# (Clo pa f f' e) = AClo pa f f' (replicatePA pa n# e)
module Data.Array.Parallel.Lifted.Instances
where
module Data.Array.Parallel.Lifted.Instances (
dPA_Int,
dPA_Unit, dPA_2, dPA_3
) where
import Data.Array.Parallel.Lifted.PArray
import Data.Array.Parallel.Unlifted ( UArr, replicateU )
......@@ -8,19 +10,37 @@ import GHC.Exts (Int#, Int(..))
data instance PArray Int = PInt Int# !(UArr Int)
instance PA Int where
{-# INLINE lengthPA #-}
lengthPA (PInt n _) = n
{-# INLINE replicatePA #-}
replicatePA n i = PInt n (replicateU (I# n) i)
dPA_Int :: PA Int
{-# INLINE dPA_Int #-}
dPA_Int = PA {
lengthPA = lengthPA_Int
, replicatePA = replicatePA_Int
}
lengthPA_Int :: PArray Int -> Int#
{-# INLINE lengthPA_Int #-}
lengthPA_Int (PInt n _) = n
replicatePA_Int :: Int# -> Int -> PArray Int
{-# INLINE replicatePA_Int #-}
replicatePA_Int n i = PInt n (replicateU (I# n) i)
data instance PArray () = PUnit Int# ()
instance PA () where
{-# INLINE lengthPA #-}
lengthPA (PUnit n _) = n
{-# INLINE replicatePA #-}
replicatePA n x = PUnit n x
dPA_Unit :: PA ()
{-# INLINE dPA_Unit #-}
dPA_Unit = PA {
lengthPA = lengthPA_Unit
, replicatePA = replicatePA_Unit
}
lengthPA_Unit :: PArray () -> Int#
{-# INLINE lengthPA_Unit #-}
lengthPA_Unit (PUnit n# _) = n#
replicatePA_Unit :: Int# -> () -> PArray ()
{-# INLINE replicatePA_Unit #-}
replicatePA_Unit = PUnit
-- Tuples
--
......@@ -89,26 +109,45 @@ data STup5 a b c d e = STup5 !a !b !c !d !e
data instance PArray (a,b) = PTup2 Int# (PArray a) (PArray b)
instance (PA a, PA b) => PA (a,b) where
{-# INLINE lengthPA #-}
lengthPA (PTup2 n _ _) = n
{-# INLINE replicatePA #-}
replicatePA n p = PTup2 n (p `seq` replicatePA n a)
(p `seq` replicatePA n b)
where
(a,b) = p
dPA_2 :: PA a -> PA b -> PA (a,b)
{-# INLINE dPA_2 #-}
dPA_2 pa pb = PA {
lengthPA = lengthPA_2
, replicatePA = replicatePA_2 pa pb
}
lengthPA_2 :: PArray (a,b) -> Int#
{-# INLINE lengthPA_2 #-}
lengthPA_2 (PTup2 n# _ _) = n#
replicatePA_2 :: PA a -> PA b -> Int# -> (a,b) -> PArray (a,b)
{-# INLINE replicatePA_2 #-}
replicatePA_2 pa pb n# p = PTup2 n# (p `seq` replicatePA pa n# a)
(p `seq` replicatePA pb n# b)
where (a,b) = p
data instance PArray (a,b,c) = PTup3 Int# (PArray a)
(PArray b)
(PArray c)
instance (PA a, PA b, PA c) => PA (a,b,c) where
{-# INLINE lengthPA #-}
lengthPA (PTup3 n _ _ _) = n
{-# INLINE replicatePA #-}
replicatePA n p = PTup3 n (p `seq` replicatePA n a)
(p `seq` replicatePA n b)
(p `seq` replicatePA n c)
where
(a,b,c) = p
dPA_3 :: PA a -> PA b -> PA c -> PA (a,b,c)
{-# INLINE dPA_3 #-}
dPA_3 pa pb pc
= PA {
lengthPA = lengthPA_3
, replicatePA = replicatePA_3 pa pb pc
}
lengthPA_3 :: PArray (a,b,c) -> Int#
{-# INLINE lengthPA_3 #-}
lengthPA_3 (PTup3 n# _ _ _) = n#
replicatePA_3 :: PA a -> PA b -> PA c
-> Int# -> (a,b,c) -> PArray (a,b,c)
{-# INLINE replicatePA_3 #-}
replicatePA_3 pa pb pc n# p
= PTup3 n# (p `seq` replicatePA pa n# a)
(p `seq` replicatePA pb n# b)
(p `seq` replicatePA pc n# c)
where (a,b,c) = p
......@@ -10,10 +10,11 @@ data family PArray a
-- |Dictionaries
--
class PA a where
lengthPA :: PArray a -> Int#
replicatePA :: Int# -> a -> PArray a
data PA a = PA {
lengthPA :: PArray a -> Int#
, replicatePA :: Int# -> a -> PArray a
}
emptyPA :: PA a => PArray a
emptyPA = replicatePA 0# (error "PArray.emptyPA: empty")
emptyPA :: PA a -> PArray a
emptyPA pa = replicatePA pa 0# (error "PArray.emptyPA: empty")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment