Wind back INLINEs to make nbody compile
[packages/dph.git] / dph-lifted-vseg / Data / Array / Parallel / PArray / PData / Tuple2.hs
1 {-# OPTIONS_HADDOCK hide #-}
2 #include "fusion-phases.h"
3
4 -- | PR instance for tuples.
5 module Data.Array.Parallel.PArray.PData.Tuple2
6 ( PData(..), PDatas(..)
7 , zipPD
8 , ziplPR
9 , unzipPD
10 , unziplPD)
11 where
12 import Data.Array.Parallel.Pretty
13 import Data.Array.Parallel.PArray.PData.Base
14 import Data.Array.Parallel.PArray.PData.Nested
15 import GHC.Exts
16 import Prelude hiding (zip, unzip)
17 import qualified Data.Vector as V
18 import qualified Prelude as P
19 import qualified Data.Array.Parallel.Unlifted as U
20
21 -------------------------------------------------------------------------------
22 data instance PData (a, b)
23 = PTuple2 (PData a) (PData b)
24
25 data instance PDatas (a, b)
26 = PTuple2s (PDatas a) (PDatas b)
27
28
29 -- PR -------------------------------------------------------------------------
30 instance (PR a, PR b) => PR (a, b) where
31
32 {-# NOINLINE validPR #-}
33 validPR (PTuple2 xs ys)
34 = validPR xs && validPR ys
35
36
37 {-# NOINLINE nfPR #-}
38 nfPR (PTuple2 arr1 arr2)
39 = nfPR arr1 `seq` nfPR arr2 `seq` ()
40
41
42 {-# NOINLINE similarPR #-}
43 similarPR (x1, y1) (x2, y2)
44 = similarPR x1 x2
45 && similarPR y1 y2
46
47
48 {-# NOINLINE coversPR #-}
49 coversPR weak (PTuple2 arr1 arr2) ix
50 = coversPR weak arr1 ix
51 && coversPR weak arr2 ix
52
53 {-# NOINLINE pprpPR #-}
54 pprpPR (x, y)
55 = text "Tuple2 " <> vcat [pprpPR x, pprpPR y]
56
57
58 {-# NOINLINE pprpDataPR #-}
59 pprpDataPR (PTuple2 xs ys)
60 = text "PTuple2 " <> vcat [pprpDataPR xs, pprpDataPR ys]
61
62 -- Constructors -------------------------------
63 {-# INLINE_PDATA emptyPR #-}
64 emptyPR
65 = PTuple2 emptyPR emptyPR
66
67
68 {-# INLINE_PDATA replicatePR #-}
69 replicatePR len (x, y)
70 = PTuple2 (replicatePR len x)
71 (replicatePR len y)
72
73
74 {-# INLINE_PDATA replicatesPR #-}
75 replicatesPR lens (PTuple2 arr1 arr2)
76 = PTuple2 (replicatesPR lens arr1)
77 (replicatesPR lens arr2)
78
79
80 {-# INLINE_PDATA appendPR #-}
81 appendPR (PTuple2 arr11 arr12) (PTuple2 arr21 arr22)
82 = PTuple2 (arr11 `appendPR` arr21)
83 (arr12 `appendPR` arr22)
84
85
86 {-# INLINE_PDATA appendsPR #-}
87 appendsPR segdResult segd1 (PTuple2 arrs11 arrs12) segd2 (PTuple2 arrs21 arrs22)
88 = PTuple2 (appendsPR segdResult segd1 arrs11 segd2 arrs21)
89 (appendsPR segdResult segd1 arrs12 segd2 arrs22)
90
91
92 -- Projections ---------------------------------
93 {-# INLINE_PDATA lengthPR #-}
94 lengthPR (PTuple2 arr1 _)
95 = lengthPR arr1
96
97 {-# INLINE_PDATA indexPR #-}
98 indexPR (PTuple2 arr1 arr2) ix
99 = (indexPR arr1 ix, indexPR arr2 ix)
100
101 {-# INLINE_PDATA indexsPR #-}
102 indexsPR (PTuple2s xs ys) srcs ixs
103 = PTuple2 (indexsPR xs srcs ixs)
104 (indexsPR ys srcs ixs)
105
106 {-# NOINLINE extractPR #-}
107 extractPR (PTuple2 arr1 arr2) start len
108 = PTuple2 (extractPR arr1 start len)
109 (extractPR arr2 start len)
110
111 {-# NOINLINE extractsPR #-}
112 extractsPR (PTuple2s xs ys) ussegd
113 = PTuple2 (extractsPR xs ussegd)
114 (extractsPR ys ussegd)
115
116
117 -- Pack and Combine ---------------------------
118 {-# NOINLINE packByTagPR #-}
119 packByTagPR (PTuple2 arr1 arr2) tags tag
120 = PTuple2 (packByTagPR arr1 tags tag)
121 (packByTagPR arr2 tags tag)
122
123 {-# NOINLINE combine2PR #-}
124 combine2PR sel (PTuple2 xs1 ys1) (PTuple2 xs2 ys2)
125 = PTuple2 (combine2PR sel xs1 xs2)
126 (combine2PR sel ys1 ys2)
127
128
129 -- Conversions --------------------------------
130 {-# NOINLINE fromVectorPR #-}
131 fromVectorPR vec
132 = let (xs, ys) = V.unzip vec
133 in PTuple2 (fromVectorPR xs)
134 (fromVectorPR ys)
135
136 {-# NOINLINE toVectorPR #-}
137 toVectorPR (PTuple2 xs ys)
138 = V.zip (toVectorPR xs)
139 (toVectorPR ys)
140
141
142 -- PData --------------------------------------
143 {-# INLINE_PDATA emptydPR #-}
144 emptydPR
145 = PTuple2s emptydPR
146 emptydPR
147
148
149 {-# INLINE_PDATA singletondPR #-}
150 singletondPR (PTuple2 x y)
151 = PTuple2s (singletondPR x)
152 (singletondPR y)
153
154
155 {-# INLINE_PDATA lengthdPR #-}
156 lengthdPR (PTuple2s xs _)
157 = lengthdPR xs
158
159
160 {-# INLINE_PDATA indexdPR #-}
161 indexdPR (PTuple2s xs ys) i
162 = PTuple2 (indexdPR xs i)
163 (indexdPR ys i)
164
165
166 {-# INLINE_PDATA appenddPR #-}
167 appenddPR (PTuple2s xs1 ys1) (PTuple2s xs2 ys2)
168 = PTuple2s (appenddPR xs1 xs2)
169 (appenddPR ys1 ys2)
170
171
172 {-# NOINLINE fromVectordPR #-}
173 fromVectordPR vec
174 = let (xss, yss) = V.unzip $ V.map (\(PTuple2 xs ys) -> (xs, ys)) vec
175 in PTuple2s (fromVectordPR xss)
176 (fromVectordPR yss)
177
178
179 {-# NOINLINE toVectordPR #-}
180 toVectordPR (PTuple2s pdatas1 pdatas2)
181 = V.zipWith PTuple2
182 (toVectordPR pdatas1)
183 (toVectordPR pdatas2)
184
185
186 -- PD Functions ---------------------------------------------------------------
187 -- These work on PData arrays of tuples, but don't need a PA or PR dictionary
188
189 -- | O(1). Zip a pair of arrays into an array of pairs.
190 zipPD :: PData a -> PData b -> PData (a, b)
191 zipPD = PTuple2
192 {-# INLINE_PA zipPD #-}
193
194
195 -- | Lifted zip.
196 ziplPR :: (PR a, PR b) => PData (PArray a) -> PData (PArray b) -> PData (PArray (a, b))
197 ziplPR arr1 arr2
198 = let -- We need to flatten the data here because we can't guarantee
199 -- that the vsegds of both arrays have the same form.
200 -- One of the arrays may have been created with replicate, and
201 -- thus has internal sharing, while the other does not.
202 (segd1, pdata1) = flattenPR arr1
203 (_, pdata2) = flattenPR arr2
204
205 in PNested (U.promoteSegdToVSegd segd1)
206 (PTuple2s (singletondPR pdata1) (singletondPR pdata2))
207
208 {-# INLINE_PA ziplPR #-}
209
210
211 -- | O(1). Unzip an array of pairs into a pair of arrays.
212 unzipPD :: PData (a, b) -> (PData a, PData b)
213 unzipPD (PTuple2 xs ys) = (xs, ys)
214 {-# INLINE_PA unzipPD #-}
215
216
217 -- | Lifted unzip.
218 unziplPD :: PData (PArray (a, b)) -> PData (PArray a, PArray b)
219 unziplPD (PNested uvsegd (PTuple2s xsdata ysdata))
220 = PTuple2 (PNested uvsegd xsdata)
221 (PNested uvsegd ysdata)
222 {-# INLINE_PA unziplPD #-}
223
224
225 -- Show -----------------------------------------------------------------------
226 deriving instance (Show (PData a), Show (PData b)) => Show (PData (a, b))
227 deriving instance (Show (PDatas a), Show (PDatas b)) => Show (PDatas (a, b))
228
229
230 instance ( PR a, PR b, Show a, Show b
231 , PprVirtual (PData a), PprVirtual (PData b))
232 => PprVirtual (PData (a, b)) where
233 pprv (PTuple2 xs ys)
234 = text $ show
235 $ P.zip (V.toList $ toVectorPR xs)
236 (V.toList $ toVectorPR ys)
237