1 | /*************************************************************************** |
---|
2 | * blitz/array/reduce.cc Array reductions. |
---|
3 | * |
---|
4 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU General Public License |
---|
8 | * as published by the Free Software Foundation; either version 2 |
---|
9 | * of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * Suggestions: blitz-dev@oonumerics.org |
---|
17 | * Bugs: blitz-bugs@oonumerics.org |
---|
18 | * |
---|
19 | * For more information, please see the Blitz++ Home Page: |
---|
20 | * http://oonumerics.org/blitz/ |
---|
21 | * |
---|
22 | ****************************************************************************/ |
---|
23 | #ifndef BZ_ARRAYREDUCE_H |
---|
24 | #error <blitz/array/reduce.cc> must be included via <blitz/array/reduce.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | BZ_NAMESPACE(blitz) |
---|
28 | |
---|
29 | template<typename T_expr, typename T_reduction> |
---|
30 | _bz_typename T_reduction::T_resulttype |
---|
31 | _bz_reduceWithIndexTraversal(T_expr expr, T_reduction reduction); |
---|
32 | |
---|
33 | template<typename T_expr, typename T_reduction> |
---|
34 | _bz_typename T_reduction::T_resulttype |
---|
35 | _bz_reduceWithStackTraversal(T_expr expr, T_reduction reduction); |
---|
36 | |
---|
37 | template<typename T_expr, typename T_reduction> |
---|
38 | _bz_typename T_reduction::T_resulttype |
---|
39 | _bz_ArrayExprFullReduce(T_expr expr, T_reduction reduction) |
---|
40 | { |
---|
41 | #ifdef BZ_TAU_PROFILING |
---|
42 | // Tau profiling code. Provide Tau with a pretty-printed version of |
---|
43 | // the expression. |
---|
44 | static BZ_STD_SCOPE(string) exprDescription; |
---|
45 | if (!exprDescription.length()) // faked static initializer |
---|
46 | { |
---|
47 | exprDescription = T_reduction::name(); |
---|
48 | exprDescription += "("; |
---|
49 | prettyPrintFormat format(true); // Terse mode on |
---|
50 | expr.prettyPrint(exprDescription, format); |
---|
51 | exprDescription += ")"; |
---|
52 | } |
---|
53 | TAU_PROFILE(" ", exprDescription, TAU_BLITZ); |
---|
54 | #endif // BZ_TAU_PROFILING |
---|
55 | |
---|
56 | return _bz_reduceWithIndexTraversal(expr, reduction); |
---|
57 | |
---|
58 | #ifdef BZ_NOT_IMPLEMENTED_FLAG |
---|
59 | if ((T_expr::numIndexPlaceholders > 0) || (T_reduction::needIndex)) |
---|
60 | { |
---|
61 | // The expression involves index placeholders, so have to |
---|
62 | // use index traversal rather than stack traversal. |
---|
63 | return reduceWithIndexTraversal(expr, reduction); |
---|
64 | } |
---|
65 | else { |
---|
66 | // Use a stack traversal |
---|
67 | return reduceWithStackTraversal(expr, reduction); |
---|
68 | } |
---|
69 | #endif |
---|
70 | } |
---|
71 | |
---|
72 | template<typename T_expr, typename T_reduction> |
---|
73 | _bz_typename T_reduction::T_resulttype |
---|
74 | _bz_reduceWithIndexTraversal(T_expr expr, T_reduction reduction) |
---|
75 | { |
---|
76 | // This is optimized assuming C-style arrays. |
---|
77 | |
---|
78 | reduction.reset(); |
---|
79 | |
---|
80 | const int rank = T_expr::rank; |
---|
81 | |
---|
82 | TinyVector<int,T_expr::rank> index, first, last; |
---|
83 | |
---|
84 | unsigned long count = 1; |
---|
85 | |
---|
86 | for (int i=0; i < rank; ++i) |
---|
87 | { |
---|
88 | index(i) = expr.lbound(i); |
---|
89 | first(i) = index(i); |
---|
90 | last(i) = expr.ubound(i) + 1; |
---|
91 | count *= last(i) - first(i); |
---|
92 | } |
---|
93 | |
---|
94 | const int maxRank = rank - 1; |
---|
95 | int lastlbound = expr.lbound(maxRank); |
---|
96 | int lastubound = expr.ubound(maxRank); |
---|
97 | |
---|
98 | int lastIndex = lastubound + 1; |
---|
99 | |
---|
100 | bool loopFlag = true; |
---|
101 | |
---|
102 | while(loopFlag) { |
---|
103 | for (index[maxRank]=lastlbound;index[maxRank]<lastIndex;++index[maxRank]) |
---|
104 | if (!reduction(expr(index), index[maxRank])) { |
---|
105 | loopFlag = false; |
---|
106 | break; |
---|
107 | } |
---|
108 | |
---|
109 | int j = rank-2; |
---|
110 | for (; j >= 0; --j) { |
---|
111 | index(j+1) = first(j+1); |
---|
112 | ++index(j); |
---|
113 | if (index(j) != last(j)) |
---|
114 | break; |
---|
115 | } |
---|
116 | |
---|
117 | if (j < 0) |
---|
118 | break; |
---|
119 | } |
---|
120 | |
---|
121 | return reduction.result(count); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | template<typename T_expr, typename T_reduction> |
---|
126 | _bz_typename T_reduction::T_resulttype |
---|
127 | _bz_reduceWithIndexVectorTraversal(T_expr expr, T_reduction reduction) |
---|
128 | { |
---|
129 | // This version is for reductions that require a vector |
---|
130 | // of index positions. |
---|
131 | |
---|
132 | reduction.reset(); |
---|
133 | |
---|
134 | const int rank = T_expr::rank; |
---|
135 | |
---|
136 | TinyVector<int,T_expr::rank> index, first, last; |
---|
137 | |
---|
138 | unsigned long count = 1; |
---|
139 | |
---|
140 | for (int i=0; i < rank; ++i) |
---|
141 | { |
---|
142 | index(i) = expr.lbound(i); |
---|
143 | first(i) = index(i); |
---|
144 | last(i) = expr.ubound(i) + 1; |
---|
145 | count *= last(i) - first(i); |
---|
146 | } |
---|
147 | |
---|
148 | const int maxRank = rank - 1; |
---|
149 | int lastlbound = expr.lbound(maxRank); |
---|
150 | int lastubound = expr.ubound(maxRank); |
---|
151 | |
---|
152 | int lastIndex = lastubound + 1; |
---|
153 | |
---|
154 | bool loopFlag = true; |
---|
155 | |
---|
156 | while(loopFlag) { |
---|
157 | for (index[maxRank]=lastlbound;index[maxRank]<lastIndex;++index[maxRank]) |
---|
158 | if (!reduction(expr(index),index)) { |
---|
159 | loopFlag = false; |
---|
160 | break; |
---|
161 | } |
---|
162 | |
---|
163 | int j = rank-2; |
---|
164 | for (; j >= 0; --j) { |
---|
165 | index(j+1) = first(j+1); |
---|
166 | ++index(j); |
---|
167 | if (index(j) != last(j)) |
---|
168 | break; |
---|
169 | } |
---|
170 | |
---|
171 | if (j < 0) |
---|
172 | break; |
---|
173 | } |
---|
174 | |
---|
175 | return reduction.result(count); |
---|
176 | } |
---|
177 | |
---|
178 | BZ_NAMESPACE_END |
---|
179 | |
---|