Split out Repr types into their own module and add documentation.
[packages/dph.git] / dph-common / Data / Array / Parallel / PArray / PRepr.hs
1 {-# LANGUAGE CPP, FlexibleContexts #-}
2
3 #include "fusion-phases.h"
4
5 -- | Defines the family of types that can be represented generically,
6 -- and the functions to convert two and from the generic representation.
7 --
8 -- TODO: Check inconsistent use of INLINE pragmas.
9 -- Most have INLINE_PA, but bpermutePD and nfPD have plain INLINE
10 --
11 module Data.Array.Parallel.PArray.PRepr (
12 PRepr,
13 PA(..),
14
15 -- These functions have corresponding members in the PR class
16 -- from Data.Array.Parallel.PArray.PData.
17 emptyPD,
18 replicatePD,
19 replicatelPD,
20 repeatPD,
21 indexPD,
22 extractPD,
23 bpermutePD,
24 appPD,
25 applPD,
26 packByTagPD,
27 combine2PD,
28 updatePD,
29 fromListPD,
30 nfPD
31 )
32 where
33 import Data.Array.Parallel.PArray.PData
34
35
36 -- | Representable types.
37 --
38 -- The family of types that we know how to represent generically.
39 -- PRepr takes an arbitrary type and produces the generic type we use to
40 -- represent it.
41 --
42 -- Instances for simple types are defined in Data.Array.Parallel.Lifted.Instances.
43 -- For algebraic types, it's up to the vectoriser/client module to create
44 -- a suitable instance.
45 --
46 type family PRepr a
47
48
49 -- | A PA dictionary contains the functions that we use to convert a
50 -- representable type to and from its generic representation.
51 -- The conversion methods should all be O(1).
52 --
53 class PR (PRepr a) => PA a where
54 toPRepr :: a -> PRepr a
55 fromPRepr :: PRepr a -> a
56 toArrPRepr :: PData a -> PData (PRepr a)
57 fromArrPRepr :: PData (PRepr a) -> PData a
58
59
60 -- PD Wrappers ----------------------------------------------------------------
61 -- These wrappers work on (PData a) arrays when we know the element type 'a'
62 -- is representable. For most of them we can just convert the PData to the
63 -- underlying representation type, and use the corresponding operator from
64 -- the PR dictionary.
65 --
66 emptyPD :: PA a => T_emptyPR a
67 {-# INLINE_PA emptyPD #-}
68 emptyPD
69 = fromArrPRepr emptyPR
70
71 replicatePD :: PA a => T_replicatePR a
72 {-# INLINE_PA replicatePD #-}
73 replicatePD n# x
74 = fromArrPRepr
75 . replicatePR n#
76 $ toPRepr x
77
78 replicatelPD :: PA a => T_replicatelPR a
79 {-# INLINE_PA replicatelPD #-}
80 replicatelPD segd xs
81 = fromArrPRepr
82 . replicatelPR segd
83 $ toArrPRepr xs
84
85 repeatPD :: PA a => T_repeatPR a
86 {-# INLINE_PA repeatPD #-}
87 repeatPD n# len# xs
88 = fromArrPRepr
89 . repeatPR n# len#
90 $ toArrPRepr xs
91
92 indexPD :: PA a => T_indexPR a
93 {-# INLINE_PA indexPD #-}
94 indexPD xs i#
95 = fromPRepr
96 $ indexPR (toArrPRepr xs) i#
97
98 extractPD :: PA a => T_extractPR a
99 {-# INLINE_PA extractPD #-}
100 extractPD xs i# m#
101 = fromArrPRepr
102 $ extractPR (toArrPRepr xs) i# m#
103
104 bpermutePD :: PA a => T_bpermutePR a
105 {-# INLINE bpermutePD #-}
106 bpermutePD xs n# is
107 = fromArrPRepr
108 $ bpermutePR (toArrPRepr xs) n# is
109
110 appPD :: PA a => T_appPR a
111 {-# INLINE_PA appPD #-}
112 appPD xs ys
113 = fromArrPRepr
114 $ appPR (toArrPRepr xs) (toArrPRepr ys)
115
116 applPD :: PA a => T_applPR a
117 {-# INLINE_PA applPD #-}
118 applPD segd is xs js ys
119 = fromArrPRepr
120 $ applPR segd is (toArrPRepr xs) js (toArrPRepr ys)
121
122 packByTagPD :: PA a => T_packByTagPR a
123 {-# INLINE_PA packByTagPD #-}
124 packByTagPD xs n# tags t#
125 = fromArrPRepr
126 $ packByTagPR (toArrPRepr xs) n# tags t#
127
128 combine2PD :: PA a => T_combine2PR a
129 {-# INLINE_PA combine2PD #-}
130 combine2PD n# sel as bs
131 = fromArrPRepr
132 $ combine2PR n# sel (toArrPRepr as) (toArrPRepr bs)
133
134 updatePD :: PA a => T_updatePR a
135 {-# INLINE_PA updatePD #-}
136 updatePD xs is ys
137 = fromArrPRepr
138 $ updatePR (toArrPRepr xs) is (toArrPRepr ys)
139
140 fromListPD :: PA a => T_fromListPR a
141 {-# INLINE_PA fromListPD #-}
142 fromListPD n# xs
143 = fromArrPRepr
144 $ fromListPR n# (map toPRepr xs)
145
146 nfPD :: PA a => T_nfPR a
147 {-# INLINE nfPD #-}
148 nfPD xs = nfPR (toArrPRepr xs)
149
150