Instances.hs 3.96 KB
Newer Older
1
2
module Data.Array.Parallel.Lifted.Instances (
  dPA_Int,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
3
  dPA_0, dPA_2, dPA_3
4
) where
5
6

import Data.Array.Parallel.Lifted.PArray
7
import Data.Array.Parallel.Unlifted ( UArr, replicateU )
8

9
10
11
12
import GHC.Exts    (Int#, Int(..))

data instance PArray Int = PInt Int# !(UArr Int)

13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)
27
28

data instance PArray () = PUnit Int# ()
29

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
30
31
32
33
34
dPA_0 :: PA ()
{-# INLINE dPA_0 #-}
dPA_0 = PA {
             lengthPA    = lengthPA_0
           , replicatePA = replicatePA_0
35
36
           }

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
37
38
39
lengthPA_0 :: PArray () -> Int#
{-# INLINE lengthPA_0 #-}
lengthPA_0 (PUnit n# _) = n#
40

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
41
42
43
replicatePA_0 :: Int# -> () -> PArray ()
{-# INLINE replicatePA_0 #-}
replicatePA_0 = PUnit
44
45
46

-- Tuples
--
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- We can use one of the following two representations
--
-- data instance PArray (a1,...,an) = PTup<n> !Int (STup<n> (PArray a1)
--                                                          ...
--                                                          (PArray an))
--
-- where STup<n> are strict n-ary tuples or
--
-- data instance PArray (a1,...,an) = PTup<n> !Int (PArray a1) ... (PArray an)
--
-- Consider the following two terms:
--
--   xs = replicateP n (_|_, _|_)
--   ys = replicateP n (_|_ :: (t,u))
--
-- These have to be represented differently; in particular, we have
--
--   xs !: 0 = (_|_,_|_)
--   ys !: 0 = _|_
--
-- but
--
--   lengthP xs = lengthP ys = n
--
-- With he first representation, we have
--
--   xs = PTup2 n (STup2 (replicateP n _|_) (replicateP n _|_))
--   ys = PTup2 n _|_
--
-- With
-- 
--   PTup2 n (STup2 xs ys) !: i = (xs !: i, ys !: i)
--   lengthP (PTup2 n _)        = n
--
-- this gives use the desired result. With the second representation we might
-- use:
--
--   replicateP n p = PArray n (p `seq` replicateP n x)
--                             (p `seq` replicateP n y)
--     where
--       (x,y) = p
--
-- which gives us
--
--   xs = PTup2 n (replicateP n _|_) (replicateP n _|_)
--   ys = PTup2 n _|_ _|_
--
-- We'd then have to use
--
--   PTup2 n xs ys !: i  = xs `seq` ys `seq` (xs !: i, ys !: i)
--   lengthP (PTup2 n _) = n
--
-- Not sure which is better (the first seems slightly cleaner) but we'll go
-- with the second repr for now as it makes closure environments slightly
-- simpler to construct and to take apart.

{-
104
105
106
107
data STup2 a b = STup2 !a !b
data STup3 a b c = STup3 !a !b !c
data STup4 a b c d = STup4 !a !b !c !d
data STup5 a b c d e = STup5 !a !b !c !d !e
108
-}
109

110
data instance PArray (a,b) = PTup2 Int# (PArray a) (PArray b)
111

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
128

129
data instance PArray (a,b,c) = PTup3 Int# (PArray a)
130
131
                                          (PArray b)
                                          (PArray c)
132

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
153