Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

dph-common-global: finish enough for smvm example

  • Loading branch information...
commit a983730358b1f5176919315a3b46b1045dcbfe4f 1 parent ffeb4c7
@benl23x5 benl23x5 authored
View
29 dph-common-global/Data/Array/Parallel/Lifted/Combinators.hs
@@ -74,35 +74,10 @@ mapPA_l n arrClo arr2
PNestedS segd xs ->
PNestedS segd
(liftedApply (U.elementsSegd segd)
- (replicatelPJ segd arrClo)
+ (restrictsPJ segd arrClo)
xs )
--- not sure how to represent the result of a nested repeat.
--- maybe we want to stash extra information in the mode.
--- so the mode type says what sort of segment descriptor to use
-
--- result of repeatlPE has form:
--- a a a b b c c c c c
--- Want to represent this without physically copying the data.
--- Roman says maybe only need to slice, replicate, slice.. but not replicate again.
---
--- Really want:
--- . . .
--- . . c
--- a . c
--- a . c
--- a b c
--- Don't need to actually store run lengths.
--- lifted apply just applies whole elem arrays
---
-
-{-
-repeatlPE :: PJ m1 a
- => U.Segd -> PData m1 a -> PData m2 a
-repeatlPE = error "repeatlPE: undefined"
--}
-
-- index ----------------------------------------------------------------------
indexPP :: PA a => PArray a :-> Int :-> a
indexPP = closure2 indexPA indexPA_l
@@ -115,7 +90,6 @@ indexPA_l n src ixs
PArray _ d -> d
-- Tuple ======================================================================
-
-- unzip ----------------------------------------------------------------------
unzipPP :: PArray (a, b) :-> (PArray a, PArray b)
unzipPP = closure1 unzipPA unzipPA_l
@@ -124,7 +98,6 @@ unzipPP = closure1 unzipPA unzipPA_l
-- Scalar =====================================================================
-
-- sum ------------------------------------------------------------------------
sumPP_double :: PArray Double :-> Double
sumPP_double = closure1 sumPA sumPA_l
View
31 dph-common-global/Data/Array/Parallel/PArray/PData/Base.hs
@@ -36,11 +36,12 @@ lengthPA (PArray n _) = n
unpackPA :: PArray a -> PData Sized a
unpackPA (PArray _ d) = d
+
-- PData ----------------------------------------------------------------------
-- | Parallel array data.
-- As opposed to finite PArrays, a PData can represent a finite or infinite
-- number of array elements, depending on the mode. The infinite case simply
--- means that all array indices map to some element value.
+-- means that all possible array indices map to some element value.
--
data family PData mode a
@@ -94,21 +95,33 @@ class PS a where
class PS a => PJ m a where
-- | Restrict an array to be a particular size.
-- For pre-Sized arrays, instances should simply check that the source
- -- has the required length, and `error` if it does not. For global arrays,
- -- we take a finite slice, copying the single element.
- -- We then rely on fusion in the back-end to eliminate the copying.
- restrictPJ :: Int -> PData m a -> PData Sized a
+ -- has the required length, and `error` if it does not.
+ -- For global arrays, we take a finite slice, copying the single element.
+ -- Work and Space is O(n) in the size of the result array.
+ restrictPJ :: Int -> PData m a -> PData Sized a
+
+ -- | Segmented restrict.
+ -- Restrict the outer layer of a nested array to be a particular size.
+ -- Only defined for a = PArray b.
+ ---
+ -- eg: restrictsPJ [___ __ _] [[x0 x1] [x2] [x3 x4 x5]]
+ -- => [[x0 x1] [x0 x1] [x0 x1] [x2] [x2] [x3 x4 x5]]
+ ---
+ -- This must be implemented in a copy-free way. The segment descriptor
+ -- in the result should point to the same data for each replicated
+ -- segment.
+ --
+ -- Work and Space must be O(n) in the number of segments in the result,
+ -- NOT the total number of elements in the flat array.
+ restrictsPJ :: U.Segd -> PData m a -> PData Sized a
-- | Lookup a single element from the source array.
indexPJ :: PData m a -> Int -> a
-- | Lifted indexing, look up each indexed element from the corresponding array.
- -- NOTE: This needs to be in the PJ class because that's the only one stored
- -- in the closure. Only defined for the case where a = PArray b.
+ -- Only defined for a = PArray b
indexlPJ :: Int -> PData m a -> PData Sized Int -> a
- replicatelPJ :: U.Segd -> PData m a -> PData Sized a
-
-- PE Dictionary (Expansion) --------------------------------------------------
-- | Contains expansion operators.
View
5 dph-common-global/Data/Array/Parallel/PArray/PData/Closure.hs
@@ -23,11 +23,12 @@ instance PJ m (a :-> b) where
restrictPJ n (AClo fv fl env)
= AClo fv fl (restrictPJ n env)
+ restrictsPJ segd (AClo fv fl env)
+ = AClo fv fl (restrictsPJ segd env)
+
indexPJ (AClo fv fl env) ix
= Clo fv fl (indexPJ env ix)
- replicatelPJ segd (AClo fv fl env)
- = AClo fv fl (replicatelPJ segd env)
instance PE (a :-> b) where
repeatPE (Clo fv fl env)
View
78 dph-common-global/Data/Array/Parallel/PArray/PData/Nested.hs
@@ -20,16 +20,6 @@ data instance PData Sized (PArray a)
-- A sized array of sized data.
= PNestedS U.Segd (PData Sized a)
- -- A sized array of global data
- -- Represents an array of repeated arrays
- -- repeatl [[x1 x2 x3], [x4], [x5 x6]] (logical)
- -- [x1 x2 x3 x4 x5 x6] (physical data)
- -- -------- -- ----- (physical segd)
- --
- -- => [ [[x1 x2 x3] ... ], [[x4] ...], [[x5 x6] ...]]
- --
--- | PNestedSG U.Segd (PData Sized a)
-
data instance PData Global (PArray a)
= PNestedG (PArray a)
@@ -53,29 +43,64 @@ instance PJ Sized a => PJ Sized (PArray a) where
restrictPJ n arr@(PNestedS segd d1)
= arr
- indexlPJ n (PNestedS segd d1) d2
- = let d1s = restrictPJ n d1
- d2s = restrictPJ n d2
- in error "indexlPJ@PArray sized undefined"
+ -- Lifted replicate.
+ -- logical restrictsPJ :: [Int] -> [[a]] -> [[[a]]]
+ -- eg: restrictsPJ [2 3 1] [ [x0 x1] [x2] [x3 x4 x5]]
+ -- => [ [[x0 x1] [x0 x1]] [[x2] [x2] [x2]] [[x3 x4 x5]]
+
+ -- physical restrictsPJ :: U.Segd -> PData Sized (PArray a) -> PData Sized (PArray a)
+ -- restrictsPJ [2 3 1] [ x0 x1 x2 x3 x4 x5 ] (data)
+ -- [ 2 1 3 ] (lengths)
+ -- [ 0 2 3 ] (indices)
+
+ -- => [ x0 x1 x2 x3 x4 x5 ] (data)
+ -- [ 2 2 1 1 1 3 ] (lengths)
+ -- [ 0 0 2 2 2 3 ] (indices)
+ restrictsPJ segd1 (PNestedS segd2 d2)
+ = let segd' = U.mkSegd (U.replicate_s segd1 (U.lengthsSegd segd2))
+ (U.replicate_s segd1 (U.indicesSegd segd2))
+ (error "replicatelPS@Sized PArray: no size for flat array")
+ in PNestedS segd' d2
+
+ indexlPJ n (PNestedS segd d1) arrIxs
+ = let -- Ensure the indices array has the correct size.
+ PIntS ixs = restrictPJ n arrIxs
+
+ -- Compute the indices of all the elements we want from the flat source array.
+ ixsFlat = U.zipWith (+) (U.indicesSegd segd) ixs
+
+ -- Lookup all the elements we want from the flat array.
+ in PArray (error "indexlPJ@Sized PArray: fake segd should not be touched by caller")
+ $ constructPS (indexPJ d1) ixsFlat
+
- -- TODO: replicate segment descriptor
- replicatelPJ segd1 (PNestedS segd2 d2)
- = PNestedS (error "replicatelPS@PArray sized undefined") d2
instance PR a => PJ Global (PArray a) where
- -- TODO: make all elems of segment descriptor point to start of the data array
restrictPJ n1 arr@(PNestedG (PArray n2 d2))
- = PNestedS (error "repliatePJ@PArray global undefined") d2
+ = let segd' = U.mkSegd (U.replicate n1 n2)
+ (U.replicate n1 0)
+ (error "restrictPJ@Global PArray: no size for flat array")
+ in PNestedS segd' d2
+
+ -- When restricting a global array, all the segments in the result
+ -- share the same source data.
+ restrictsPJ segd1 (PNestedG (PArray n d2))
+ = let segd' = U.mkSegd (U.replicate_s segd1 (U.replicate (U.lengthSegd segd1) n))
+ (U.replicate_s segd1 (U.replicate (U.lengthSegd segd1) 0))
+ (error "replicatelPJ@Global PArray: no size for flat array")
+ in PNestedS segd' d2
- indexlPJ n (PNestedG (PArray _ d1)) d2
- = let PIntS vec2 = restrictPJ n d2
- in PArray (error "indexlPJ@PArray: fake segd should not be touched by caller")
- $ constructPS (indexPJ d1) vec2
- -- TODO: make all elems of segment descriptor point to the start of the data array
- replicatelPJ segd1 (PNestedG (PArray n d2))
- = PNestedS (error "replicatePJ@PArray global undefined") d2
+ indexlPJ n (PNestedG (PArray _ d1)) arrIxs
+ = let -- Ensure the indices array has the correct size.
+ PIntS ixs = restrictPJ n arrIxs
+
+ -- When indexing into a global nested array, all the elements of the source
+ -- are identical so we can use the original indices without offsetting them.
+ in PArray (error "indexlPJ@Global PArray: fake segd should not be touched by caller")
+ $ constructPS (indexPJ d1) ixs
+
instance PE a => PE (PArray a) where
repeatPE x = PNestedG x
@@ -83,4 +108,3 @@ instance PE a => PE (PArray a) where
instance PR a => PR (PArray a)
-
View
9 dph-common-global/Main.hs
@@ -33,15 +33,11 @@ ex_plus_l
ex_index_l
= mapPP $: (indexPP $: arr10) $: arr5
-
-
-
-
-- smvm -----------------------------------------------------------------------
matrix :: PArray (PArray (Int, Double))
-matrix = fromListPA [ fromListPA [(0, 1), (10, 5), (100, 20)]
- , fromListPA [(0, 2), (20, 6), (90, 30)] ]
+matrix = fromListPA [ fromListPA [(1, 1), (10, 5), (80, 20)]
+ , fromListPA [(1, 2), (20, 6), (90, 30)] ]
vector :: PArray Double
vector = fromListPA [0..99 :: Double]
@@ -51,6 +47,7 @@ smvm :: PArray (PArray (Int, Double)) -> PArray Double -> PArray Double
smvm m v
= v_smvm $: m $: v
+ex_smvm = smvm matrix vector
lap = liftedApply
Please sign in to comment.
Something went wrong with that request. Please try again.