Split out PRepr and PA class into its own module
[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 --
52 class PR (PRepr a) => PA a where
53 toPRepr :: a -> PRepr a
54 fromPRepr :: PRepr a -> a
55 toArrPRepr :: PData a -> PData (PRepr a)
56 fromArrPRepr :: PData (PRepr a) -> PData a
57
58
59 -- PD Wrappers ----------------------------------------------------------------
60 -- These wrappers work on (PData a) arrays when we know the element type 'a'
61 -- is representable. For most of them we can just convert the PData to the
62 -- underlying representation type, and use the corresponding operator from
63 -- the PR dictionary.
64 --
65 emptyPD :: PA a => T_emptyPR a
66 {-# INLINE_PA emptyPD #-}
67 emptyPD
68 = fromArrPRepr emptyPR
69
70 replicatePD :: PA a => T_replicatePR a
71 {-# INLINE_PA replicatePD #-}
72 replicatePD n# x
73 = fromArrPRepr
74 . replicatePR n#
75 $ toPRepr x
76
77 replicatelPD :: PA a => T_replicatelPR a
78 {-# INLINE_PA replicatelPD #-}
79 replicatelPD segd xs
80 = fromArrPRepr
81 . replicatelPR segd
82 $ toArrPRepr xs
83
84 repeatPD :: PA a => T_repeatPR a
85 {-# INLINE_PA repeatPD #-}
86 repeatPD n# len# xs
87 = fromArrPRepr
88 . repeatPR n# len#
89 $ toArrPRepr xs
90
91 indexPD :: PA a => T_indexPR a
92 {-# INLINE_PA indexPD #-}
93 indexPD xs i#
94 = fromPRepr
95 $ indexPR (toArrPRepr xs) i#
96
97 extractPD :: PA a => T_extractPR a
98 {-# INLINE_PA extractPD #-}
99 extractPD xs i# m#
100 = fromArrPRepr
101 $ extractPR (toArrPRepr xs) i# m#
102
103 bpermutePD :: PA a => T_bpermutePR a
104 {-# INLINE bpermutePD #-}
105 bpermutePD xs n# is
106 = fromArrPRepr
107 $ bpermutePR (toArrPRepr xs) n# is
108
109 appPD :: PA a => T_appPR a
110 {-# INLINE_PA appPD #-}
111 appPD xs ys
112 = fromArrPRepr
113 $ appPR (toArrPRepr xs) (toArrPRepr ys)
114
115 applPD :: PA a => T_applPR a
116 {-# INLINE_PA applPD #-}
117 applPD segd is xs js ys
118 = fromArrPRepr
119 $ applPR segd is (toArrPRepr xs) js (toArrPRepr ys)
120
121 packByTagPD :: PA a => T_packByTagPR a
122 {-# INLINE_PA packByTagPD #-}
123 packByTagPD xs n# tags t#
124 = fromArrPRepr
125 $ packByTagPR (toArrPRepr xs) n# tags t#
126
127 combine2PD :: PA a => T_combine2PR a
128 {-# INLINE_PA combine2PD #-}
129 combine2PD n# sel as bs
130 = fromArrPRepr
131 $ combine2PR n# sel (toArrPRepr as) (toArrPRepr bs)
132
133 updatePD :: PA a => T_updatePR a
134 {-# INLINE_PA updatePD #-}
135 updatePD xs is ys
136 = fromArrPRepr
137 $ updatePR (toArrPRepr xs) is (toArrPRepr ys)
138
139 fromListPD :: PA a => T_fromListPR a
140 {-# INLINE_PA fromListPD #-}
141 fromListPD n# xs
142 = fromArrPRepr
143 $ fromListPR n# (map toPRepr xs)
144
145 nfPD :: PA a => T_nfPR a
146 {-# INLINE nfPD #-}
147 nfPD xs = nfPR (toArrPRepr xs)
148
149