Instances.hs 3.05 KB
Newer Older
1
module Data.Array.Parallel.Lifted.Instances
2
3
4
where

import Data.Array.Parallel.Lifted.PArray
5
import Data.Array.Parallel.Unlifted ( UArr, replicateU )
6

7
8
9
10
11
12
13
14
15
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)
16
17

data instance PArray () = PUnit Int# ()
18
19
20
21
22
23
24
25
26

instance PA () where
  {-# INLINE lengthPA #-}
  lengthPA    (PUnit n _) = n
  {-# INLINE replicatePA #-}
  replicatePA n x         = PUnit n x

-- Tuples
--
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
-- 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.

{-
84
85
86
87
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
88
-}
89

90
data instance PArray (a,b) = PTup2 Int# (PArray a) (PArray b)
91
92
93

instance (PA a, PA b) => PA (a,b) where
  {-# INLINE lengthPA #-}
94
  lengthPA (PTup2 n _ _) = n
95
  {-# INLINE replicatePA #-}
96
97
98
99
  replicatePA n p = PTup2 n (p `seq` replicatePA n a)
                            (p `seq` replicatePA n b)
    where
      (a,b) = p
100

101
data instance PArray (a,b,c) = PTup3 Int# (PArray a)
102
103
                                          (PArray b)
                                          (PArray c)
104
105
106

instance (PA a, PA b, PA c) => PA (a,b,c) where
  {-# INLINE lengthPA #-}
107
  lengthPA (PTup3 n _ _ _) = n
108
  {-# INLINE replicatePA #-}
109
110
111
112
113
  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
114