Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

dph-common-global: finish enough for smvm example

  • Loading branch information...
commit a983730358b1f5176919315a3b46b1045dcbfe4f 1 parent ffeb4c7
Ben Lippmeier benl23x5 authored
29 dph-common-global/Data/Array/Parallel/Lifted/Combinators.hs
@@ -74,35 +74,10 @@ mapPA_l n arrClo arr2
74 74 PNestedS segd xs ->
75 75 PNestedS segd
76 76 (liftedApply (U.elementsSegd segd)
77   - (replicatelPJ segd arrClo)
  77 + (restrictsPJ segd arrClo)
78 78 xs )
79 79
80 80
81   --- not sure how to represent the result of a nested repeat.
82   --- maybe we want to stash extra information in the mode.
83   --- so the mode type says what sort of segment descriptor to use
84   -
85   --- result of repeatlPE has form:
86   --- a a a b b c c c c c
87   --- Want to represent this without physically copying the data.
88   --- Roman says maybe only need to slice, replicate, slice.. but not replicate again.
89   ---
90   --- Really want:
91   --- . . .
92   --- . . c
93   --- a . c
94   --- a . c
95   --- a b c
96   --- Don't need to actually store run lengths.
97   --- lifted apply just applies whole elem arrays
98   ---
99   -
100   -{-
101   -repeatlPE :: PJ m1 a
102   - => U.Segd -> PData m1 a -> PData m2 a
103   -repeatlPE = error "repeatlPE: undefined"
104   --}
105   -
106 81 -- index ----------------------------------------------------------------------
107 82 indexPP :: PA a => PArray a :-> Int :-> a
108 83 indexPP = closure2 indexPA indexPA_l
@@ -115,7 +90,6 @@ indexPA_l n src ixs
115 90 PArray _ d -> d
116 91
117 92 -- Tuple ======================================================================
118   -
119 93 -- unzip ----------------------------------------------------------------------
120 94 unzipPP :: PArray (a, b) :-> (PArray a, PArray b)
121 95 unzipPP = closure1 unzipPA unzipPA_l
@@ -124,7 +98,6 @@ unzipPP = closure1 unzipPA unzipPA_l
124 98
125 99
126 100 -- Scalar =====================================================================
127   -
128 101 -- sum ------------------------------------------------------------------------
129 102 sumPP_double :: PArray Double :-> Double
130 103 sumPP_double = closure1 sumPA sumPA_l
31 dph-common-global/Data/Array/Parallel/PArray/PData/Base.hs
@@ -36,11 +36,12 @@ lengthPA (PArray n _) = n
36 36 unpackPA :: PArray a -> PData Sized a
37 37 unpackPA (PArray _ d) = d
38 38
  39 +
39 40 -- PData ----------------------------------------------------------------------
40 41 -- | Parallel array data.
41 42 -- As opposed to finite PArrays, a PData can represent a finite or infinite
42 43 -- number of array elements, depending on the mode. The infinite case simply
43   --- means that all array indices map to some element value.
  44 +-- means that all possible array indices map to some element value.
44 45 --
45 46 data family PData mode a
46 47
@@ -94,21 +95,33 @@ class PS a where
94 95 class PS a => PJ m a where
95 96 -- | Restrict an array to be a particular size.
96 97 -- For pre-Sized arrays, instances should simply check that the source
97   - -- has the required length, and `error` if it does not. For global arrays,
98   - -- we take a finite slice, copying the single element.
99   - -- We then rely on fusion in the back-end to eliminate the copying.
100   - restrictPJ :: Int -> PData m a -> PData Sized a
  98 + -- has the required length, and `error` if it does not.
  99 + -- For global arrays, we take a finite slice, copying the single element.
  100 + -- Work and Space is O(n) in the size of the result array.
  101 + restrictPJ :: Int -> PData m a -> PData Sized a
  102 +
  103 + -- | Segmented restrict.
  104 + -- Restrict the outer layer of a nested array to be a particular size.
  105 + -- Only defined for a = PArray b.
  106 + ---
  107 + -- eg: restrictsPJ [___ __ _] [[x0 x1] [x2] [x3 x4 x5]]
  108 + -- => [[x0 x1] [x0 x1] [x0 x1] [x2] [x2] [x3 x4 x5]]
  109 + ---
  110 + -- This must be implemented in a copy-free way. The segment descriptor
  111 + -- in the result should point to the same data for each replicated
  112 + -- segment.
  113 + --
  114 + -- Work and Space must be O(n) in the number of segments in the result,
  115 + -- NOT the total number of elements in the flat array.
  116 + restrictsPJ :: U.Segd -> PData m a -> PData Sized a
101 117
102 118 -- | Lookup a single element from the source array.
103 119 indexPJ :: PData m a -> Int -> a
104 120
105 121 -- | Lifted indexing, look up each indexed element from the corresponding array.
106   - -- NOTE: This needs to be in the PJ class because that's the only one stored
107   - -- in the closure. Only defined for the case where a = PArray b.
  122 + -- Only defined for a = PArray b
108 123 indexlPJ :: Int -> PData m a -> PData Sized Int -> a
109 124
110   - replicatelPJ :: U.Segd -> PData m a -> PData Sized a
111   -
112 125
113 126 -- PE Dictionary (Expansion) --------------------------------------------------
114 127 -- | Contains expansion operators.
5 dph-common-global/Data/Array/Parallel/PArray/PData/Closure.hs
@@ -23,11 +23,12 @@ instance PJ m (a :-> b) where
23 23 restrictPJ n (AClo fv fl env)
24 24 = AClo fv fl (restrictPJ n env)
25 25
  26 + restrictsPJ segd (AClo fv fl env)
  27 + = AClo fv fl (restrictsPJ segd env)
  28 +
26 29 indexPJ (AClo fv fl env) ix
27 30 = Clo fv fl (indexPJ env ix)
28 31
29   - replicatelPJ segd (AClo fv fl env)
30   - = AClo fv fl (replicatelPJ segd env)
31 32
32 33 instance PE (a :-> b) where
33 34 repeatPE (Clo fv fl env)
78 dph-common-global/Data/Array/Parallel/PArray/PData/Nested.hs
@@ -20,16 +20,6 @@ data instance PData Sized (PArray a)
20 20 -- A sized array of sized data.
21 21 = PNestedS U.Segd (PData Sized a)
22 22
23   - -- A sized array of global data
24   - -- Represents an array of repeated arrays
25   - -- repeatl [[x1 x2 x3], [x4], [x5 x6]] (logical)
26   - -- [x1 x2 x3 x4 x5 x6] (physical data)
27   - -- -------- -- ----- (physical segd)
28   - --
29   - -- => [ [[x1 x2 x3] ... ], [[x4] ...], [[x5 x6] ...]]
30   - --
31   --- | PNestedSG U.Segd (PData Sized a)
32   -
33 23 data instance PData Global (PArray a)
34 24 = PNestedG (PArray a)
35 25
@@ -53,29 +43,64 @@ instance PJ Sized a => PJ Sized (PArray a) where
53 43 restrictPJ n arr@(PNestedS segd d1)
54 44 = arr
55 45
56   - indexlPJ n (PNestedS segd d1) d2
57   - = let d1s = restrictPJ n d1
58   - d2s = restrictPJ n d2
59   - in error "indexlPJ@PArray sized undefined"
  46 + -- Lifted replicate.
  47 + -- logical restrictsPJ :: [Int] -> [[a]] -> [[[a]]]
  48 + -- eg: restrictsPJ [2 3 1] [ [x0 x1] [x2] [x3 x4 x5]]
  49 + -- => [ [[x0 x1] [x0 x1]] [[x2] [x2] [x2]] [[x3 x4 x5]]
  50 +
  51 + -- physical restrictsPJ :: U.Segd -> PData Sized (PArray a) -> PData Sized (PArray a)
  52 + -- restrictsPJ [2 3 1] [ x0 x1 x2 x3 x4 x5 ] (data)
  53 + -- [ 2 1 3 ] (lengths)
  54 + -- [ 0 2 3 ] (indices)
  55 +
  56 + -- => [ x0 x1 x2 x3 x4 x5 ] (data)
  57 + -- [ 2 2 1 1 1 3 ] (lengths)
  58 + -- [ 0 0 2 2 2 3 ] (indices)
  59 + restrictsPJ segd1 (PNestedS segd2 d2)
  60 + = let segd' = U.mkSegd (U.replicate_s segd1 (U.lengthsSegd segd2))
  61 + (U.replicate_s segd1 (U.indicesSegd segd2))
  62 + (error "replicatelPS@Sized PArray: no size for flat array")
  63 + in PNestedS segd' d2
  64 +
  65 + indexlPJ n (PNestedS segd d1) arrIxs
  66 + = let -- Ensure the indices array has the correct size.
  67 + PIntS ixs = restrictPJ n arrIxs
  68 +
  69 + -- Compute the indices of all the elements we want from the flat source array.
  70 + ixsFlat = U.zipWith (+) (U.indicesSegd segd) ixs
  71 +
  72 + -- Lookup all the elements we want from the flat array.
  73 + in PArray (error "indexlPJ@Sized PArray: fake segd should not be touched by caller")
  74 + $ constructPS (indexPJ d1) ixsFlat
  75 +
60 76
61   - -- TODO: replicate segment descriptor
62   - replicatelPJ segd1 (PNestedS segd2 d2)
63   - = PNestedS (error "replicatelPS@PArray sized undefined") d2
64 77
65 78 instance PR a => PJ Global (PArray a) where
66 79
67   - -- TODO: make all elems of segment descriptor point to start of the data array
68 80 restrictPJ n1 arr@(PNestedG (PArray n2 d2))
69   - = PNestedS (error "repliatePJ@PArray global undefined") d2
  81 + = let segd' = U.mkSegd (U.replicate n1 n2)
  82 + (U.replicate n1 0)
  83 + (error "restrictPJ@Global PArray: no size for flat array")
  84 + in PNestedS segd' d2
  85 +
  86 + -- When restricting a global array, all the segments in the result
  87 + -- share the same source data.
  88 + restrictsPJ segd1 (PNestedG (PArray n d2))
  89 + = let segd' = U.mkSegd (U.replicate_s segd1 (U.replicate (U.lengthSegd segd1) n))
  90 + (U.replicate_s segd1 (U.replicate (U.lengthSegd segd1) 0))
  91 + (error "replicatelPJ@Global PArray: no size for flat array")
  92 + in PNestedS segd' d2
70 93
71   - indexlPJ n (PNestedG (PArray _ d1)) d2
72   - = let PIntS vec2 = restrictPJ n d2
73   - in PArray (error "indexlPJ@PArray: fake segd should not be touched by caller")
74   - $ constructPS (indexPJ d1) vec2
75 94
76   - -- TODO: make all elems of segment descriptor point to the start of the data array
77   - replicatelPJ segd1 (PNestedG (PArray n d2))
78   - = PNestedS (error "replicatePJ@PArray global undefined") d2
  95 + indexlPJ n (PNestedG (PArray _ d1)) arrIxs
  96 + = let -- Ensure the indices array has the correct size.
  97 + PIntS ixs = restrictPJ n arrIxs
  98 +
  99 + -- When indexing into a global nested array, all the elements of the source
  100 + -- are identical so we can use the original indices without offsetting them.
  101 + in PArray (error "indexlPJ@Global PArray: fake segd should not be touched by caller")
  102 + $ constructPS (indexPJ d1) ixs
  103 +
79 104
80 105 instance PE a => PE (PArray a) where
81 106 repeatPE x = PNestedG x
@@ -83,4 +108,3 @@ instance PE a => PE (PArray a) where
83 108
84 109 instance PR a => PR (PArray a)
85 110
86   -
9 dph-common-global/Main.hs
@@ -33,15 +33,11 @@ ex_plus_l
33 33 ex_index_l
34 34 = mapPP $: (indexPP $: arr10) $: arr5
35 35
36   -
37   -
38   -
39   -
40 36
41 37 -- smvm -----------------------------------------------------------------------
42 38 matrix :: PArray (PArray (Int, Double))
43   -matrix = fromListPA [ fromListPA [(0, 1), (10, 5), (100, 20)]
44   - , fromListPA [(0, 2), (20, 6), (90, 30)] ]
  39 +matrix = fromListPA [ fromListPA [(1, 1), (10, 5), (80, 20)]
  40 + , fromListPA [(1, 2), (20, 6), (90, 30)] ]
45 41
46 42 vector :: PArray Double
47 43 vector = fromListPA [0..99 :: Double]
@@ -51,6 +47,7 @@ smvm :: PArray (PArray (Int, Double)) -> PArray Double -> PArray Double
51 47 smvm m v
52 48 = v_smvm $: m $: v
53 49
  50 +ex_smvm = smvm matrix vector
54 51
55 52 lap = liftedApply
56 53

0 comments on commit a983730

Please sign in to comment.
Something went wrong with that request. Please try again.