7f732de4d63680cc0d0e73eb45c233c2facf820e
[nofib.git] / parallel / OLD / coins / ParForce.hs
1 {- -------------------------------------------------------------------------
2 $Id: ParForce.hs,v 1.2 1996/07/25 21:20:04 partain Exp $
3 Time-stamp: <Sat Apr 27 1996 22:58:48 Stardate: [-31]7419.57 hwloidl>
4
5 This is revision: $Revision: 1.2 $
6
7 Module for forcing parallelism using par and friends.
8
9 It contains three classes of functions:
10 1) For each _<par-annotation>_ that's hard-wired into the compiler, there is
11 a function <par-annotation> in this module, doing the same thing as the
12 annotation but taking boxed values in the annotation positions.
13 2) Forcing functions like forcelist that cause the evaluation of every
14 component of the data-structure. The clean way to handle this would be
15 to define a class Forcable with a force method for each datatype of
16 interest.
17 3) Parallel versions of some higher-order functions such as map, filter, zip
18 etc. Currently par_map is the most often used way to parallelise a program.
19 The different versions force the argument list in different ways, and have
20 different ways of expressing the annotation information.
21
22 Currently two PP variables are used to decide which versions of the functions
23 to take:
24 SEQ ... creates dummy definitions for all annotations and makes the parallel
25 higher order functions to aliases of their sequential counterparts
26 GRAN_TNG ... uses the versions of the annotations that provide
27 sparkname, gran-info, size-info, parallelism-info
28 different versions of the parallel h.o. fcts pass this info in
29 different ways
30 [in the current versions the h.o. fcts just insert dummy values
31 for the size and parallelism info]
32
33 If no PP variable is defined, the parallel version is used, which only
34 supplies spark names but no gran-, size- or par-info.
35
36 Changelog (including older versions not in the correwsponding RCS file):
37 $Log: ParForce.hs,v $
38 Revision 1.2 1996/07/25 21:20:04 partain
39 Bulk of final changes for 2.01
40
41 --# Revision 1.3 1994/11/23 01:07:23 hwloidl
42 --# Version for testing fwd mapping and hom sol.
43 --#
44 --# Revision 1.1 1994/11/19 02:00:05 hwloidl
45 --# Initial revision
46 --#
47 ---------------------------------------------------------------------- -}
48
49 module ParForce (parGlobal, parLocal, parAt, parAtForNow, seq, par,
50 forcelist, forcelist1,
51 parmap, parmap0, parmap1, pariterate,
52 parzip, parzipWith, parfilter,
53 par_map, par_map0, par_map1, par_iterate,
54 par_zip, par_zipWith, par_filter ) where
55
56 -- import Parallel
57 -- Basic parallelism annotations
58
59 #if defined(SEQ)
60
61 parGlobal :: Int -> Int -> Int -> Int -> a -> b -> b
62 parGlobal _ _ _ _ x y = y
63
64 parLocal :: Int -> Int -> Int -> Int -> a -> b -> b
65 parLocal _ _ _ _ x y = y
66
67 parAt :: Int -> Int -> Int -> Int -> a -> b -> c -> c
68 parAt _ _ _ _ w x y = y
69
70 parAtForNow :: Int -> Int -> Int -> Int -> a -> b -> c -> c
71 parAtForNow _ _ _ _ w x y = y
72
73 seq :: a -> b -> b
74 seq x y = y
75
76 par :: a -> b -> b
77 par x y = y
78
79 #elif defined(GRAN)
80
81 parGlobal :: Int -> Int -> Int -> Int -> a -> b -> b
82 parGlobal (I# n) (I# g) (I# s) (I# p) = _parGlobal_ n g s p
83 {-
84 where n' = case (n) of (I# n') -> n'
85 g' = case (g) of (I# g') -> g'
86 s' = case (s) of (I# s') -> s'
87 p' = case (p) of (I# p') -> p'
88 -}
89
90 parLocal :: Int -> Int -> Int -> Int -> a -> b -> b
91 parLocal (I# n) (I# g) (I# s) (I# p) = _parLocal_ n g s p
92
93 parAt :: Int -> Int -> Int -> Int -> a -> b -> c -> c
94 parAt (I# n) (I# g) (I# s) (I# p) = _parAt_ n g s p
95
96 parAtForNow :: Int -> Int -> Int -> Int -> a -> b -> c -> c
97 parAtForNow (I# n) (I# g) (I# s) (I# p) = _parAtForNow_ n g s p
98
99 {-# INLINE seq #-}
100 seq :: a -> b -> b
101 seq = _seq_
102
103 {-# INLINE par #-}
104 par :: a -> b -> b
105 par = _par_
106
107 #elif defined(PAR) /* i.e.GUM */
108
109 import Parallel -- includes definitions of par and seq
110
111 parGlobal :: Int -> Int -> Int -> Int -> a -> b -> b
112 parGlobal _ _ _ _ = par
113
114 parLocal :: Int -> Int -> Int -> Int -> a -> b -> b
115 parLocal _ _ _ _ = par
116
117 --parAt :: Int -> Int -> Int -> Int -> a -> b -> c -> c
118 parAt _ _ _ _ w = par
119
120 --parAtForNow :: Int -> Int -> Int -> Int -> a -> b -> c -> c
121 parAtForNow _ _ _ _ w = par
122
123 #else /* e.g. HBCPP */
124
125 import Parallel -- renaming (par to hbcpp_par, seq to hbcpp_seq)
126
127 {-
128 --seq :: a -> b -> b
129 seq = hbcpp_seq
130
131 --par :: a -> b -> b
132 par = hbcpp_par "par"
133
134 --parGlobal :: Int -> Int -> Int -> Int -> a -> b -> b
135 parGlobal n _ _ _ = hbcpp_par (show n)
136
137 --parLocal :: Int -> Int -> Int -> Int -> a -> b -> b
138 parLocal n _ _ _ = hbcpp_par (show n)
139
140 --parAt :: Int -> Int -> Int -> Int -> a -> b -> c -> c
141 parAt n _ _ _ w = hbcpp_par (show n)
142
143 --parAtForNow :: Int -> Int -> Int -> Int -> a -> b -> c -> c
144 parAtForNow n _ _ _ w = hbcpp_par (show n)
145 -}
146
147 --parGlobal :: Int -> Int -> Int -> Int -> a -> b -> b
148 parGlobal n _ _ _ x y = par x y
149
150 --parLocal :: Int -> Int -> Int -> Int -> a -> b -> b
151 parLocal n _ _ _ x y = par x y
152
153 --parAt :: Int -> Int -> Int -> Int -> a -> b -> c -> c
154 parAt n _ _ _ w x y = par x y
155
156 --parAtForNow :: Int -> Int -> Int -> Int -> a -> b -> c -> c
157 parAtForNow n _ _ _ w x y = par x y
158
159 #endif
160
161 forcelist [] = ()
162 forcelist (x:xs) = seq x (forcelist xs)
163
164 forcelist1 0 (x:xs) = ()
165 forcelist1 n (x:xs) = seq x (forcelist1 (n-1) xs)
166
167 #if 0 /* defined(SEQ) */
168
169 par_map :: Int -> (a -> b) -> [a] -> [b]
170 par_map _ = map
171
172 par_map0 :: Int -> (a -> b) -> [a] -> [b]
173 par_map0 _ = map
174
175 par_map1 :: (Int -> Int) -> (a -> b) -> [a] -> [b]
176 par_map1 _ = map
177
178 par_iterate :: Int -> (a -> a) -> a -> [a]
179 par_iterate _ = iterate
180
181 par_zipWith :: Int -> (a -> b -> c) -> [a] -> [b] -> [c]
182 par_zipWith _ = zipWith
183
184 par_zip :: Int -> [a] -> [b] -> [(a,b)]
185 par_zip _ = zip
186
187 par_filter :: Int -> (a -> Bool) -> [a] -> [a]
188 par_filter _ = filter
189
190 #else {- !SEQ -}
191
192 parmap0 :: Int -> Int -> Int -> (a -> b) -> [a] -> [b]
193 parmap0 g s p f [] = []
194 parmap0 g s p f (x:xs) = parGlobal g g s p fx
195 ( parGlobal g g s p (forcelist pmxs)
196 (fx:pmxs) )
197 where fx = f x
198 pmxs = parmap0 g s p f xs
199
200
201 par_map0 :: Int -> (a -> b) -> [a] -> [b]
202 par_map0 g = parmap0 g 0 0
203
204 parmap :: Int -> Int -> Int -> (a -> b) -> [a] -> [b]
205 parmap g s p f [] = []
206 parmap g s p f (x:xs) = parGlobal g g s p fx
207 ( parGlobal g g s p (forcelist pmxs)
208 (fx:pmxs) )
209 where fx = f x
210 pmxs = parmap g s p f xs
211
212 par_map :: Int -> (a -> b) -> [a] -> [b]
213 par_map g = parmap g 0 0
214
215 parmap1 :: (Int -> Int) -> (Int -> Int) -> (Int -> Int) -> (a -> b) -> [a] -> [b]
216 parmap1 g s p f l = parmap' 0 l
217 where parmap' n [] = []
218 parmap' n (x:xs) = parGlobal gn gn sn pn
219 pmxs
220 (parGlobal gn gn sn pn
221 fx
222 (fx:pmxs) )
223 where fx = f x
224 pmxs = parmap' (n+1) xs
225 gn = g n
226 sn = s n
227 pn = p n
228
229 par_map1 :: (Int -> Int) -> (a -> b) -> [a] -> [b]
230 par_map1 g = parmap1 g ignore ignore
231 where ignore _ = 0
232
233 pariterate :: Int -> Int -> Int -> (a -> a) -> a -> [a]
234 pariterate g s p f x = parGlobal g g s p fx
235 {- parGlobal 14# 0# (forcelist rest) -}
236 (fx : rest)
237 where fx = f x
238 rest = pariterate g s p f (f x)
239
240 par_iterate :: Int -> (a -> a) -> a -> [a]
241 par_iterate g = pariterate g 0 0
242
243 parzipWith :: Int -> Int -> Int -> (a -> b -> c) -> [a] -> [b] -> [c]
244 parzipWith g s p z (a:as) (b:bs) = parGlobal g g s p zab
245 (parGlobal g g s p (forcelist rest)
246 (zab : rest)
247 )
248 where zab = z a b
249 rest = parzipWith g s p z as bs
250 parzipWith _ _ _ _ _ _ = []
251
252 par_zipWith :: Int -> (a -> b -> c) -> [a] -> [b] -> [c]
253 par_zipWith g = parzipWith g 0 0
254
255 parzip :: Int -> Int -> Int -> [a] -> [b] -> [(a,b)]
256 parzip g s p = parzipWith g s p (\ a b -> (a,b))
257
258 par_zip :: Int -> [a] -> [b] -> [(a,b)]
259 par_zip g = parzip g 0 0
260
261 -- This version is taken from Roe's thesis (p. 38)
262 parfilter :: Int -> Int -> Int -> (a -> Bool) -> [a] -> [a]
263 parfilter _ _ _ pred [] = []
264 parfilter g s p pred (x:xs) = parGlobal g g s p rest l
265 where rest = parfilter g s p pred xs
266 l = if pred x then x:rest
267 else rest
268
269 par_filter :: Int -> (a -> Bool) -> [a] -> [a]
270 par_filter g = parfilter g 0 0
271
272 #endif {- SEQ -}
273
274 -- ---------------------------------------------------------------------------
275 -- General way how to force data-stuctures
276 -- ToDo: Use strategies
277 -- ---------------------------------------------------------------------------
278
279 #if 0
280
281 class Forcable a where
282 force :: a -> ()
283
284 instance Forcable [a] where
285 force = forcelist
286
287 #endif
288