Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[2b666a]: blitz / array / methods.cc Maximize Restore History

Download this file

methods.cc    438 lines (373 with data), 13.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#ifndef BZ_ARRAYMETHODS_CC
#define BZ_ARRAYMETHODS_CC
#ifndef BZ_ARRAY_H
#error <blitz/array/methods.cc> must be included via <blitz/array.h>
#endif
BZ_NAMESPACE(blitz)
template<typename P_numtype, int N_rank> template<typename T_expr>
Array<P_numtype,N_rank>::Array(_bz_ArrayExpr<T_expr> expr)
{
// Determine extent of the array expression
TinyVector<int,N_rank> lbound, extent, ordering;
TinyVector<bool,N_rank> ascendingFlag;
TinyVector<bool,N_rank> in_ordering;
in_ordering = false;
int j = 0;
for (int i=0; i < N_rank; ++i)
{
lbound(i) = expr.lbound(i);
int ubound = expr.ubound(i);
extent(i) = ubound - lbound(i) + 1;
int orderingj = expr.ordering(i);
if (orderingj != INT_MIN && orderingj < N_rank &&
!in_ordering( orderingj )) { // unique value in ordering array
in_ordering( orderingj ) = true;
ordering(j++) = orderingj;
}
int ascending = expr.ascending(i);
ascendingFlag(i) = (ascending == 1);
#ifdef BZ_DEBUG
if ((lbound(i) == INT_MIN) || (ubound == INT_MAX)
|| (ordering(i) == INT_MIN) || (ascending == INT_MIN))
{
BZPRECHECK(0,
"Attempted to construct an array from an expression " << endl
<< "which does not have a shape. To use this constructor, "
<< endl
<< "the expression must contain at least one array operand.");
return;
}
#endif
}
// It is possible that ordering is not a permutation of 0,...,N_rank-1.
// In that case j will be less than N_rank. We fill in ordering with the
// usused values in decreasing order.
for (int i = N_rank-1; j < N_rank; ++j) {
while (in_ordering(i))
--i;
ordering(j) = i--;
}
Array<T_numtype,N_rank> A(lbound,extent,
GeneralArrayStorage<N_rank>(ordering,ascendingFlag));
A = expr;
reference(A);
}
template<typename P_numtype, int N_rank>
Array<P_numtype,N_rank>::Array(const TinyVector<int, N_rank>& lbounds,
const TinyVector<int, N_rank>& extent,
const GeneralArrayStorage<N_rank>& storage)
: storage_(storage)
{
length_ = extent;
storage_.setBase(lbounds);
setupStorage(N_rank - 1);
}
/*
* This routine takes the storage information for the array
* (ascendingFlag_[], base_[], and ordering_[]) and the size
* of the array (length_[]) and computes the stride vector
* (stride_[]) and the zero offset (see explanation in array.h).
*/
template<typename P_numtype, int N_rank>
_bz_inline2 void Array<P_numtype, N_rank>::computeStrides()
{
if (N_rank > 1)
{
diffType stride = 1;
// This flag simplifies the code in the loop, encouraging
// compile-time computation of strides through constant folding.
bool allAscending = storage_.allRanksStoredAscending();
// BZ_OLD_FOR_SCOPING
int n;
for (n=0; n < N_rank; ++n)
{
int strideSign = +1;
// If this rank is stored in descending order, then the stride
// will be negative.
if (!allAscending)
{
if (!isRankStoredAscending(ordering(n)))
strideSign = -1;
}
// The stride for this rank is the product of the lengths of
// the ranks minor to it.
stride_[ordering(n)] = stride * strideSign;
stride *= length_[ordering(n)];
}
}
else {
// Specialization for N_rank == 1
// This simpler calculation makes it easier for the compiler
// to propagate stride values.
if (isRankStoredAscending(0))
stride_[0] = 1;
else
stride_[0] = -1;
}
calculateZeroOffset();
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::calculateZeroOffset()
{
// Calculate the offset of (0,0,...,0)
zeroOffset_ = 0;
// zeroOffset_ = - sum(where(ascendingFlag_, stride_ * base_,
// (length_ - 1 + base_) * stride_))
for (int n=0; n < N_rank; ++n)
{
if (!isRankStoredAscending(n))
zeroOffset_ -= (length_[n] - 1 + base(n)) * stride_[n];
else
zeroOffset_ -= stride_[n] * base(n);
}
}
template<typename P_numtype, int N_rank>
bool Array<P_numtype, N_rank>::isStorageContiguous() const
{
// The storage is contiguous if for the set
// { | stride[i] * extent[i] | }, i = 0..N_rank-1,
// there is only one value which is not in the set
// of strides; and if there is one stride which is 1.
// This algorithm is quadratic in the rank. It is hard
// to imagine this being a serious problem.
int numStridesMissing = 0;
bool haveUnitStride = false;
for (int i=0; i < N_rank; ++i)
{
diffType stride = BZ_MATHFN_SCOPE(abs)(stride_[i]);
if (stride == 1)
haveUnitStride = true;
diffType vi = stride * length_[i];
int j = 0;
for (j=0; j < N_rank; ++j)
if (BZ_MATHFN_SCOPE(abs)(stride_[j]) == vi)
break;
if (j == N_rank)
{
++numStridesMissing;
if (numStridesMissing == 2)
return false;
}
}
return haveUnitStride;
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::dumpStructureInformation(ostream& os) const
{
os << "Dump of Array<" << BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(P_numtype)
<< ", " << N_rank << ">:" << endl
<< "ordering_ = " << storage_.ordering() << endl
<< "ascendingFlag_ = " << storage_.ascendingFlag() << endl
<< "base_ = " << storage_.base() << endl
<< "length_ = " << length_ << endl
<< "stride_ = " << stride_ << endl
<< "zeroOffset_ = " << zeroOffset_ << endl
<< "numElements() = " << numElements() << endl
<< "isStorageContiguous() = " << isStorageContiguous() << endl;
}
/*
* Make this array a view of another array's data.
*/
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::reference(const Array<P_numtype, N_rank>& array)
{
storage_ = array.storage_;
length_ = array.length_;
stride_ = array.stride_;
zeroOffset_ = array.zeroOffset_;
T_base::changeBlock(array.noConst());
}
/* This method makes the array reference another, but it does it as a
"weak" reference that is not counted. If you can guarantee that the
array memory block containing the data is persistent, this will
allow reference counting to be bypassed for this array, which if
mutex-locking is involved is a significant overhead. */
template<typename P_numtype, int N_rank>
void
Array<P_numtype, N_rank>::weakReference(const Array<P_numtype, N_rank>& array)
{
reference(Array<P_numtype, N_rank>(array.noConst().data(),
array.shape(),neverDeleteData));
}
/*
* Modify the Array storage. Array must be unallocated.
*/
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::setStorage(GeneralArrayStorage<N_rank> x)
{
#ifdef BZ_DEBUG
if (size() != 0) {
BZPRECHECK(0,"Cannot modify storage format of an Array that has already been allocated!" << endl);
return;
}
#endif
storage_ = x;
return;
}
/*
* This method is called to allocate memory for a new array.
*/
template<typename P_numtype, int N_rank>
_bz_inline2 void Array<P_numtype, N_rank>::setupStorage(int lastRankInitialized)
{
TAU_TYPE_STRING(p1, "Array<T,N>::setupStorage() [T="
+ CT(P_numtype) + ",N=" + CT(N_rank) + "]");
TAU_PROFILE(" ", p1, TAU_BLITZ);
/*
* If the length of some of the ranks was unspecified, fill these
* in using the last specified value.
*
* e.g. Array<int,3> A(40) results in a 40x40x40 array.
*/
for (int i=lastRankInitialized + 1; i < N_rank; ++i)
{
storage_.setBase(i, storage_.base(lastRankInitialized));
length_[i] = length_[lastRankInitialized];
}
// Compute strides
computeStrides();
// Allocate a block of memory
sizeType numElem = numElements();
if (numElem==0)
T_base::changeToNullBlock();
else
T_base::newBlock(numElem);
// Adjust the base of the array to account for non-zero base
// indices and reversals
data_ += zeroOffset_;
}
template<typename P_numtype, int N_rank>
Array<P_numtype, N_rank> Array<P_numtype, N_rank>::copy() const
{
if (numElements())
{
Array<T_numtype, N_rank> z(length_, storage_);
z = *this;
return z;
}
else {
// Null array-- don't bother allocating an empty block.
return *this;
}
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::makeUnique()
{
if (T_base::numReferences() > 1)
{
T_array tmp = copy();
reference(tmp);
}
}
template<typename P_numtype, int N_rank>
Array<P_numtype, N_rank> Array<P_numtype, N_rank>::transpose(int r0, int r1,
int r2, int r3, int r4, int r5, int r6, int r7, int r8, int r9, int r10) const
{
T_array B(*this);
B.transposeSelf(r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10);
return B;
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::transposeSelf(int r0, int r1, int r2, int r3,
int r4, int r5, int r6, int r7, int r8, int r9, int r10)
{
BZPRECHECK(r0+r1+r2+r3+r4+r5+r6+r7+r8+r9+r10 == N_rank * (N_rank-1) / 2,
"Invalid array transpose() arguments." << endl
<< "Arguments must be a permutation of the numerals (0,...,"
<< (N_rank - 1) << ")");
// Create a temporary reference copy of this array
Array<T_numtype, N_rank> x(*this);
// Now reorder the dimensions using the supplied permutation
doTranspose(0, r0, x);
doTranspose(1, r1, x);
doTranspose(2, r2, x);
doTranspose(3, r3, x);
doTranspose(4, r4, x);
doTranspose(5, r5, x);
doTranspose(6, r6, x);
doTranspose(7, r7, x);
doTranspose(8, r8, x);
doTranspose(9, r9, x);
doTranspose(10, r10, x);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::doTranspose(int destRank, int sourceRank,
Array<T_numtype, N_rank>& array)
{
// BZ_NEEDS_WORK: precondition check
if (destRank >= N_rank)
return;
length_[destRank] = array.length_[sourceRank];
stride_[destRank] = array.stride_[sourceRank];
storage_.setAscendingFlag(destRank,
array.isRankStoredAscending(sourceRank));
storage_.setBase(destRank, array.base(sourceRank));
// BZ_NEEDS_WORK: Handling the storage ordering is currently O(N^2)
// but it can be done fairly easily in linear time by constructing
// the appropriate permutation.
// Find sourceRank in array.storage_.ordering_
int i=0;
for (; i < N_rank; ++i)
if (array.storage_.ordering(i) == sourceRank)
break;
storage_.setOrdering(i, destRank);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::reverseSelf(int rank)
{
BZPRECONDITION(rank < N_rank);
storage_.setAscendingFlag(rank, !isRankStoredAscending(rank));
diffType adjustment = static_cast<ptrdiff_t>(stride_[rank]) * (lbound(rank) + ubound(rank));
zeroOffset_ += adjustment;
data_ += adjustment;
stride_[rank] *= -1;
}
template<typename P_numtype, int N_rank>
Array<P_numtype, N_rank> Array<P_numtype,N_rank>::reverse(int rank)
{
T_array B(*this);
B.reverseSelf(rank);
return B;
}
template<typename P_numtype, int N_rank> template<typename P_numtype2>
Array<P_numtype2,N_rank> Array<P_numtype,N_rank>::extractComponent(P_numtype2,
int componentNumber, int numComponents) const
{
BZPRECONDITION((componentNumber >= 0)
&& (componentNumber < numComponents));
TinyVector<diffType, N_rank> stride2;
for (int i=0; i < N_rank; ++i)
stride2(i) = stride_(i) * numComponents;
const P_numtype2* dataFirst2 =
((const P_numtype2*)dataFirst()) + componentNumber;
return Array<P_numtype2,N_rank>(const_cast<P_numtype2*>(dataFirst2),
length_, stride2, storage_);
}
/*
* These routines reindex the current array to use a new base vector.
* The first reindexes the array, the second just returns a reindex view
* of the current array, leaving the current array unmodified.
* (Contributed by Derrick Bass)
*/
template<typename P_numtype, int N_rank>
_bz_inline2 void Array<P_numtype, N_rank>::reindexSelf(const
TinyVector<int, N_rank>& newBase)
{
diffType delta = 0;
for (int i=0; i < N_rank; ++i)
delta += (base(i) - newBase(i)) * stride_(i);
data_ += delta;
// WAS: dot(base() - newBase, stride_);
storage_.setBase(newBase);
calculateZeroOffset();
}
template<typename P_numtype, int N_rank>
_bz_inline2 Array<P_numtype, N_rank>
Array<P_numtype, N_rank>::reindex(const TinyVector<int, N_rank>& newBase)
{
T_array B(*this);
B.reindexSelf(newBase);
return B;
}
BZ_NAMESPACE_END
#endif // BZ_ARRAY_CC