source: Sophya/trunk/SophyaExt/Blitz/blitz/array/reduce.h@ 221

Last change on this file since 221 was 221, checked in by ansari, 26 years ago

Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99

File size: 10.0 KB
Line 
1/***************************************************************************
2 * blitz/array/reduce.h Reductions of an array (or array expression) in a
3 * single rank: sum, mean, min, minIndex, max, maxIndex,
4 * product, count, any, all
5 *
6 * $Id: reduce.h,v 1.1.1.1 1999-04-09 17:59:04 ansari Exp $
7 *
8 * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * Suggestions: blitz-suggest@cybervision.com
21 * Bugs: blitz-bugs@cybervision.com
22 *
23 * For more information, please see the Blitz++ Home Page:
24 * http://seurat.uwaterloo.ca/blitz/
25 *
26 ***************************************************************************
27 * $Log: not supported by cvs2svn $
28 * Revision 1.2 1998/03/14 00:04:47 tveldhui
29 * 0.2-alpha-05
30 *
31 * Revision 1.1 1997/07/16 14:51:20 tveldhui
32 * Update: Alpha release 0.2 (Arrays)
33 *
34 */
35
36#ifndef BZ_ARRAYREDUCE_H
37#define BZ_ARRAYREDUCE_H
38
39#ifndef BZ_ARRAYEXPR_H
40 #error <blitz/array/reduce.h> must be included after <blitz/array/expr.h>
41#endif
42
43#ifndef BZ_REDUCE_H
44 #include <blitz/reduce.h>
45#endif
46
47BZ_NAMESPACE(blitz)
48
49template<class T_expr, int N_index, class T_reduction>
50class _bz_ArrayExprReduce {
51
52public:
53 typedef _bz_typename T_reduction::T_numtype T_numtype;
54 typedef T_expr T_ctorArg1;
55 typedef T_reduction T_ctorArg2;
56
57 enum { numArrayOperands = BZ_ENUM_CAST(T_expr::numArrayOperands),
58 numIndexPlaceholders = BZ_ENUM_CAST(T_expr::numIndexPlaceholders) + 1,
59 rank = BZ_ENUM_CAST(T_expr::rank) - 1 };
60
61 _bz_ArrayExprReduce(const _bz_ArrayExprReduce<T_expr,N_index,T_reduction>&
62 reduce)
63 : reduce_(reduce.reduce_), iter_(reduce.iter_)
64 {
65 }
66
67 _bz_ArrayExprReduce(T_expr expr)
68 : iter_(expr)
69 { }
70
71 _bz_ArrayExprReduce(T_expr expr, T_reduction reduce)
72 : iter_(expr), reduce_(reduce)
73 { }
74
75 int lbound(int rank)
76 { return iter_.lbound(rank); }
77
78 int ubound(int rank)
79 { return iter_.ubound(rank); }
80
81 template<int N_destRank>
82 T_numtype operator()(const TinyVector<int, N_destRank>& destIndex)
83 {
84 BZPRECHECK(N_destRank == N_index,
85 "Array reduction performed over rank " << N_index
86 << " to produce a rank " << N_destRank << " expression." << endl
87 << "You must reduce over rank " << N_destRank << " instead.");
88
89 TinyVector<int, N_destRank + 1> index;
90
91 // This metaprogram copies elements 0..N-1 of destIndex into index
92 _bz_meta_vecAssign<N_index, 0>::assign(index, destIndex,
93 _bz_update<int,int>());
94
95 int lbound = iter_.lbound(N_index);
96 int ubound = iter_.ubound(N_index);
97
98 // NEEDS_WORK: replace with tiny(int()) and huge(int()) once
99 // <limits> widely available
100 BZPRECHECK((lbound != INT_MIN) && (ubound != INT_MAX),
101 "Array reduction performed over rank " << N_index
102 << " is unbounded." << endl
103 << "There must be an array object in the expression being reduced"
104 << endl << "which provides a bound in rank " << N_index << ".");
105
106 reduce_.reset();
107
108 for (index[N_index] = iter_.lbound(N_index);
109 index[N_index] <= ubound; ++index[N_index])
110 {
111 if (!reduce_(iter_(index), index[N_index]))
112 break;
113 }
114
115 return reduce_.result(ubound-lbound+1);
116 }
117
118 // If you have a precondition failure on this routine, it means
119 // you are trying to use stack iteration mode on an expression
120 // which contains an index placeholder. You must use index
121 // iteration mode instead.
122 int operator*()
123 {
124 BZPRECONDITION(0);
125 return 0;
126 }
127
128 // See operator*() note
129 void push(int)
130 {
131 BZPRECONDITION(0);
132 }
133
134 // See operator*() note
135 void pop(int)
136 {
137 BZPRECONDITION(0);
138 }
139
140 // See operator*() note
141 void advance()
142 {
143 BZPRECONDITION(0);
144 }
145
146 // See operator*() note
147 void advance(int)
148 {
149 BZPRECONDITION(0);
150 }
151
152 // See operator*() note
153 void loadStride(int)
154 {
155 BZPRECONDITION(0);
156 }
157
158 _bz_bool isUnitStride(int rank) const
159 {
160 BZPRECONDITION(0);
161 return false;
162 }
163
164 void advanceUnitStride()
165 {
166 BZPRECONDITION(0);
167 }
168
169 _bz_bool canCollapse(int,int) const
170 { BZPRECONDITION(0); return _bz_false; }
171
172 T_numtype operator[](int)
173 {
174 BZPRECONDITION(0);
175 return T_numtype();
176 }
177
178 T_numtype fastRead(int)
179 {
180 BZPRECONDITION(0);
181 return T_numtype();
182 }
183
184 int suggestStride(int) const
185 {
186 BZPRECONDITION(0);
187 return 0;
188 }
189
190 _bz_bool isStride(int,int) const
191 {
192 BZPRECONDITION(0);
193 return _bz_true;
194 }
195
196 template<int N_rank>
197 void moveTo(const TinyVector<int,N_rank>& i)
198 {
199 BZPRECONDITION(0);
200 return;
201 }
202
203 void prettyPrint(std::string& str, prettyPrintFormat& format) const
204 {
205 // NEEDS_WORK-- do real formatting for reductions
206 str += "reduce[NEEDS_WORK](";
207 iter_.prettyPrint(str,format);
208 str += ")";
209 }
210
211 template<class T_shape>
212 _bz_bool shapeCheck(const T_shape& shape) const
213 {
214 // NEEDS_WORK-- do a real shape check (tricky)
215 return _bz_true;
216 }
217
218private:
219 _bz_ArrayExprReduce() { }
220
221 T_reduction reduce_;
222 T_expr iter_;
223};
224
225#define BZ_DECL_ARRAY_PARTIAL_REDUCE(fn,reduction) \
226template<class T_expr, int N_index> \
227inline \
228_bz_ArrayExpr<_bz_ArrayExprReduce<_bz_ArrayExpr<T_expr>, N_index, \
229 reduction<_bz_typename T_expr::T_numtype> > > \
230fn(_bz_ArrayExpr<T_expr> expr, const IndexPlaceholder<N_index>&) \
231{ \
232 return _bz_ArrayExprReduce<_bz_ArrayExpr<T_expr>, N_index, \
233 reduction<_bz_typename T_expr::T_numtype> >(expr); \
234} \
235 \
236template<class T_numtype, int N_rank, int N_index> \
237inline \
238_bz_ArrayExpr<_bz_ArrayExprReduce<ArrayIterator<T_numtype,N_rank>, \
239 N_index, reduction<T_numtype> > > \
240fn(const Array<T_numtype, N_rank>& array, \
241 const IndexPlaceholder<N_index>&) \
242{ \
243 return _bz_ArrayExprReduce<ArrayIterator<T_numtype,N_rank>, \
244 N_index, reduction<T_numtype> > (array.begin()); \
245}
246
247BZ_DECL_ARRAY_PARTIAL_REDUCE(sum, ReduceSum)
248BZ_DECL_ARRAY_PARTIAL_REDUCE(mean, ReduceMean)
249BZ_DECL_ARRAY_PARTIAL_REDUCE(min, ReduceMin)
250BZ_DECL_ARRAY_PARTIAL_REDUCE(minIndex, ReduceMinIndex)
251BZ_DECL_ARRAY_PARTIAL_REDUCE(max, ReduceMax)
252BZ_DECL_ARRAY_PARTIAL_REDUCE(maxIndex, ReduceMaxIndex)
253BZ_DECL_ARRAY_PARTIAL_REDUCE(product, ReduceProduct)
254BZ_DECL_ARRAY_PARTIAL_REDUCE(count, ReduceCount)
255BZ_DECL_ARRAY_PARTIAL_REDUCE(any, ReduceAny)
256BZ_DECL_ARRAY_PARTIAL_REDUCE(all, ReduceAll)
257BZ_DECL_ARRAY_PARTIAL_REDUCE(first, ReduceFirst)
258
259/*
260 * Complete reductions
261 */
262
263// Prototype of reduction function
264template<class T_expr, class T_reduction>
265_bz_typename T_reduction::T_resulttype
266_bz_ArrayExprFullReduce(T_expr expr, T_reduction reduction);
267
268#define BZ_DECL_ARRAY_FULL_REDUCE(fn,reduction) \
269template<class T_expr> \
270inline \
271_bz_typename reduction<_bz_typename T_expr::T_numtype>::T_resulttype \
272fn(_bz_ArrayExpr<T_expr> expr) \
273{ \
274 return _bz_ArrayExprFullReduce(expr, \
275 reduction<_bz_typename T_expr::T_numtype>()); \
276} \
277 \
278template<class T_numtype, int N_rank> \
279inline \
280_bz_typename reduction<T_numtype>::T_resulttype \
281fn(const Array<T_numtype, N_rank>& array) \
282{ \
283 return _bz_ArrayExprFullReduce(array.begin(), \
284 reduction<T_numtype>()); \
285}
286
287BZ_DECL_ARRAY_FULL_REDUCE(sum, ReduceSum)
288BZ_DECL_ARRAY_FULL_REDUCE(mean, ReduceMean)
289BZ_DECL_ARRAY_FULL_REDUCE(min, ReduceMin)
290BZ_DECL_ARRAY_FULL_REDUCE(minIndex, ReduceMinIndex)
291BZ_DECL_ARRAY_FULL_REDUCE(max, ReduceMax)
292BZ_DECL_ARRAY_FULL_REDUCE(maxIndex, ReduceMaxIndex)
293BZ_DECL_ARRAY_FULL_REDUCE(product, ReduceProduct)
294BZ_DECL_ARRAY_FULL_REDUCE(count, ReduceCount)
295BZ_DECL_ARRAY_FULL_REDUCE(any, ReduceAny)
296BZ_DECL_ARRAY_FULL_REDUCE(all, ReduceAll)
297BZ_DECL_ARRAY_FULL_REDUCE(first, ReduceFirst)
298
299BZ_NAMESPACE_END
300
301#include <blitz/array/reduce.cc>
302
303#endif // BZ_ARRAYREDUCE_H
Note: See TracBrowser for help on using the repository browser.