Revert rename of dph-common for now
[packages/dph.git] / dph-common / Data / Array / Parallel / PArray / Base.hs
1 {-# LANGUAGE CPP, FlexibleContexts #-}
2
3 #include "fusion-phases.h"
4
5 -- | Definition of the PArray type, and functions that work on it. The PArray
6 -- type is a PData with an array length. The functions we export from this
7 -- module are just wrappers for the PD functions from Data.Array.Parallel.PArray.PRepr.
8 --
9 -- TODO: Check inconsistent use of INLINE pragmas.
10 -- Most have INLINE_PA, but bpermutePD and nfPD have plain INLINE
11 --
12 module Data.Array.Parallel.PArray.Base (
13 PArray(..),
14 lengthPA#,
15 dataPA#,
16
17 -- These functions have corresponding members in the PR class
18 -- from Data.Array.Parallel.PArray.PData.
19 emptyPA,
20 replicatePA#,
21 replicatelPA#,
22 repeatPA#,
23 indexPA#,
24 extractPA#,
25 bpermutePA#,
26 appPA#,
27 applPA#,
28 packByTagPA#,
29 combine2PA#,
30 updatePA#,
31 fromListPA#, fromListPA,
32 nfPA,
33 )
34 where
35 import Data.Array.Parallel.Lifted.Unboxed (elementsSegd#)
36 import Data.Array.Parallel.PArray.PData
37 import Data.Array.Parallel.PArray.PRepr
38 import Data.Array.Parallel.Base (Tag)
39 import qualified Data.Array.Parallel.Unlifted as U
40 import GHC.Exts (Int#, Int(..), (+#), (*#))
41 import SpecConstr
42
43
44 -- | Lifted\/bulk parallel arrays
45 -- This contains the array length, along with the element data.
46 --
47 {-# ANN type PArray NoSpecConstr #-}
48 data PArray a = PArray Int# (PData a)
49
50
51 -- | Take the length field of a PArray.
52 lengthPA# :: PArray a -> Int#
53 {-# INLINE_PA lengthPA# #-}
54 lengthPA# (PArray n# _) = n#
55
56 -- | Take the data field of a PArray.
57 dataPA# :: PArray a -> PData a
58 {-# INLINE_PA dataPA# #-}
59 dataPA# (PArray _ d) = d
60
61
62 -- PA Wrappers ----------------------------------------------------------------
63 -- These wrappers work on PArrays. As the PArray contains a PData, we can
64 -- can just pass this to the corresponding PD function from
65 -- Data.Array.Parallel.PArray.PRepr. However, as a PData doesn't contain
66 -- the array length, we need to do the length calculations here.
67 --
68 -- Note: There are some more operator# functions that work on PArrays in
69 -- "Data.Array.Parallel.PArray.DataInstances". The ones there have
70 -- a similar shape but need to know about the underlying representation
71 -- constructors.
72 --
73 emptyPA :: PA a => PArray a
74 {-# INLINE_PA emptyPA #-}
75 emptyPA
76 = PArray 0# emptyPD
77
78 replicatePA# :: PA a => Int# -> a -> PArray a
79 {-# INLINE_PA replicatePA# #-}
80 replicatePA# n# x
81 = PArray n# (replicatePD n# x)
82
83 replicatelPA# :: PA a => U.Segd -> PArray a -> PArray a
84 {-# INLINE_PA replicatelPA# #-}
85 replicatelPA# segd (PArray _ xs)
86 = PArray (elementsSegd# segd) (replicatelPD segd xs)
87
88 repeatPA# :: PA a => Int# -> PArray a -> PArray a
89 {-# INLINE_PA repeatPA# #-}
90 repeatPA# m# (PArray n# xs)
91 = PArray (m# *# n#) (repeatPD m# n# xs)
92
93 indexPA# :: PA a => PArray a -> Int# -> a
94 {-# INLINE_PA indexPA# #-}
95 indexPA# (PArray _ xs) i#
96 = indexPD xs i#
97
98 extractPA# :: PA a => PArray a -> Int# -> Int# -> PArray a
99 {-# INLINE_PA extractPA# #-}
100 extractPA# (PArray _ xs) i# n#
101 = PArray n# (extractPD xs i# n#)
102
103 bpermutePA# :: PA a => PArray a -> Int# -> U.Array Int -> PArray a
104 {-# INLINE bpermutePA# #-}
105 bpermutePA# (PArray _ xs) n# is
106 = PArray n# (bpermutePD xs n# is)
107
108 appPA# :: PA a => PArray a -> PArray a -> PArray a
109 {-# INLINE_PA appPA# #-}
110 appPA# (PArray m# xs) (PArray n# ys)
111 = PArray (m# +# n#) (appPD xs ys)
112
113 applPA# :: PA a => U.Segd -> U.Segd -> PArray a -> U.Segd -> PArray a -> PArray a
114 {-# INLINE_PA applPA# #-}
115 applPA# segd is (PArray m# xs) js (PArray n# ys)
116 = PArray (m# +# n#) (applPD segd is xs js ys)
117
118 packByTagPA# :: PA a => PArray a -> Int# -> U.Array Tag -> Int# -> PArray a
119 {-# INLINE_PA packByTagPA# #-}
120 packByTagPA# (PArray _ xs) n# tags t#
121 = PArray n# (packByTagPD xs n# tags t#)
122
123 combine2PA# :: PA a => Int# -> U.Sel2 -> PArray a -> PArray a -> PArray a
124 {-# INLINE_PA combine2PA# #-}
125 combine2PA# n# sel (PArray _ as) (PArray _ bs)
126 = PArray n# (combine2PD n# sel as bs)
127
128 updatePA# :: PA a => PArray a -> U.Array Int -> PArray a -> PArray a
129 {-# INLINE_PA updatePA# #-}
130 updatePA# (PArray n# xs) is (PArray _ ys)
131 = PArray n# (updatePD xs is ys)
132
133 fromListPA# :: PA a => Int# -> [a] -> PArray a
134 {-# INLINE_PA fromListPA# #-}
135 fromListPA# n# xs
136 = PArray n# (fromListPD n# xs)
137
138 fromListPA :: PA a => [a] -> PArray a
139 {-# INLINE fromListPA #-}
140 fromListPA xs
141 = case length xs of
142 I# n# -> fromListPA# n# xs
143
144 nfPA :: PA a => PArray a -> ()
145 {-# INLINE nfPA #-}
146 nfPA (PArray _ xs)
147 = nfPD xs
148