1
/***************************************************************************
2
* blitz/array/interlace.cc
4
* Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
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.
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.
16
* Suggestions: blitz-dev@oonumerics.org
17
* Bugs: blitz-bugs@oonumerics.org
19
* For more information, please see the Blitz++ Home Page:
20
* http://oonumerics.org/blitz/
22
****************************************************************************/
23
#ifndef BZ_ARRAYINTERLACE_CC
24
#define BZ_ARRAYINTERLACE_CC
27
#error <blitz/array/interlace.cc> must be included via <blitz/array.h>
30
#ifndef BZ_ARRAYSHAPE_H
31
#include <blitz/array/shape.h>
37
* This header provides two collections of routines:
39
* interlaceArrays(shape, A1, A2, ...);
40
* allocateArrays(shape, A1, A2, ...);
42
* interlaceArrays allocates a set of arrays so that their data is
43
* interlaced. For example,
46
* interlaceArrays(shape(10,10), A, B);
48
* sets up the array storage so that A(0,0) is followed by B(0,0) in
49
* memory; then A(0,1) and B(0,1), and so on.
51
* The allocateArrays() routines may or may not interlace the data,
52
* depending on whether interlacing is advantageous for the architecture.
53
* This is controlled by the setting of BZ_INTERLACE_ARRAYS in
57
// Warning: Can't instantiate TinyVector<Range,N> because causes
58
// conflict between TinyVector<T,N>::operator=(T) and
59
// TinyVector<T,N>::operator=(Range)
61
// NEEDS_WORK -- also shape for up to N=11
62
// NEEDS_WORK -- shape(Range r1, Range r2, ...) (return TinyVector<Range,n>)
63
// maybe use Domain objects
64
// NEEDS_WORK -- doesn't make a lot of sense for user to provide a
65
// GeneralArrayStorage<N_rank+1>
67
template<typename T_numtype>
68
void makeInterlacedArray(Array<T_numtype,2>& mainArray,
69
Array<T_numtype,1>& subarray, int slice)
71
Array<T_numtype,1> tmp = mainArray(Range::all(), slice);
72
subarray.reference(tmp);
75
template<typename T_numtype>
76
void makeInterlacedArray(Array<T_numtype,3>& mainArray,
77
Array<T_numtype,2>& subarray, int slice)
79
Array<T_numtype,2> tmp = mainArray(Range::all(), Range::all(),
81
subarray.reference(tmp);
84
template<typename T_numtype>
85
void makeInterlacedArray(Array<T_numtype,4>& mainArray,
86
Array<T_numtype,3>& subarray, int slice)
88
Array<T_numtype,3> tmp = mainArray(Range::all(), Range::all(),
90
subarray.reference(tmp);
93
// These routines always allocate interlaced arrays
94
template<typename T_numtype, int N_rank>
95
void interlaceArrays(const TinyVector<int,N_rank>& shape,
96
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2)
98
GeneralArrayStorage<N_rank+1> storage;
99
Array<T_numtype, N_rank+1> array(shape, 2, storage);
100
makeInterlacedArray(array, a1, 0);
101
makeInterlacedArray(array, a2, 1);
104
template<typename T_numtype, int N_rank>
105
void interlaceArrays(const TinyVector<int,N_rank>& shape,
106
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
107
Array<T_numtype,N_rank>& a3)
109
GeneralArrayStorage<N_rank+1> storage;
110
Array<T_numtype, N_rank+1> array(shape, 3, storage);
111
makeInterlacedArray(array, a1, 0);
112
makeInterlacedArray(array, a2, 1);
113
makeInterlacedArray(array, a3, 2);
116
template<typename T_numtype, int N_rank>
117
void interlaceArrays(const TinyVector<int,N_rank>& shape,
118
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
119
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4)
121
GeneralArrayStorage<N_rank+1> storage;
122
Array<T_numtype, N_rank+1> array(shape, 4, storage);
123
makeInterlacedArray(array, a1, 0);
124
makeInterlacedArray(array, a2, 1);
125
makeInterlacedArray(array, a3, 2);
126
makeInterlacedArray(array, a4, 3);
129
template<typename T_numtype, int N_rank>
130
void interlaceArrays(const TinyVector<int,N_rank>& shape,
131
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
132
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
133
Array<T_numtype,N_rank>& a5)
135
GeneralArrayStorage<N_rank+1> storage;
136
Array<T_numtype, N_rank+1> array(shape, 5, storage);
137
makeInterlacedArray(array, a1, 0);
138
makeInterlacedArray(array, a2, 1);
139
makeInterlacedArray(array, a3, 2);
140
makeInterlacedArray(array, a4, 3);
141
makeInterlacedArray(array, a5, 4);
144
template<typename T_numtype, int N_rank>
145
void interlaceArrays(const TinyVector<int,N_rank>& shape,
146
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
147
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
148
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6)
150
GeneralArrayStorage<N_rank+1> storage;
151
Array<T_numtype, N_rank+1> array(shape, 6, storage);
152
makeInterlacedArray(array, a1, 0);
153
makeInterlacedArray(array, a2, 1);
154
makeInterlacedArray(array, a3, 2);
155
makeInterlacedArray(array, a4, 3);
156
makeInterlacedArray(array, a5, 4);
157
makeInterlacedArray(array, a6, 5);
160
template<typename T_numtype, int N_rank>
161
void interlaceArrays(const TinyVector<int,N_rank>& shape,
162
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
163
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
164
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
165
Array<T_numtype,N_rank>& a7)
167
GeneralArrayStorage<N_rank+1> storage;
168
Array<T_numtype, N_rank+1> array(shape, 7, storage);
169
makeInterlacedArray(array, a1, 0);
170
makeInterlacedArray(array, a2, 1);
171
makeInterlacedArray(array, a3, 2);
172
makeInterlacedArray(array, a4, 3);
173
makeInterlacedArray(array, a5, 4);
174
makeInterlacedArray(array, a6, 5);
175
makeInterlacedArray(array, a7, 6);
178
template<typename T_numtype, int N_rank>
179
void interlaceArrays(const TinyVector<int,N_rank>& shape,
180
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
181
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
182
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
183
Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8)
185
GeneralArrayStorage<N_rank+1> storage;
186
Array<T_numtype, N_rank+1> array(shape, 8, storage);
187
makeInterlacedArray(array, a1, 0);
188
makeInterlacedArray(array, a2, 1);
189
makeInterlacedArray(array, a3, 2);
190
makeInterlacedArray(array, a4, 3);
191
makeInterlacedArray(array, a5, 4);
192
makeInterlacedArray(array, a6, 5);
193
makeInterlacedArray(array, a7, 6);
194
makeInterlacedArray(array, a8, 7);
197
template<typename T_numtype, int N_rank>
198
void interlaceArrays(const TinyVector<int,N_rank>& shape,
199
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
200
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
201
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
202
Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
203
Array<T_numtype,N_rank>& a9)
205
GeneralArrayStorage<N_rank+1> storage;
206
Array<T_numtype, N_rank+1> array(shape, 9, storage);
207
makeInterlacedArray(array, a1, 0);
208
makeInterlacedArray(array, a2, 1);
209
makeInterlacedArray(array, a3, 2);
210
makeInterlacedArray(array, a4, 3);
211
makeInterlacedArray(array, a5, 4);
212
makeInterlacedArray(array, a6, 5);
213
makeInterlacedArray(array, a7, 6);
214
makeInterlacedArray(array, a8, 7);
215
makeInterlacedArray(array, a9, 8);
218
template<typename T_numtype, int N_rank>
219
void interlaceArrays(const TinyVector<int,N_rank>& shape,
220
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
221
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
222
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
223
Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
224
Array<T_numtype,N_rank>& a9, Array<T_numtype,N_rank>& a10)
226
GeneralArrayStorage<N_rank+1> storage;
227
Array<T_numtype, N_rank+1> array(shape, 10, storage);
228
makeInterlacedArray(array, a1, 0);
229
makeInterlacedArray(array, a2, 1);
230
makeInterlacedArray(array, a3, 2);
231
makeInterlacedArray(array, a4, 3);
232
makeInterlacedArray(array, a5, 4);
233
makeInterlacedArray(array, a6, 5);
234
makeInterlacedArray(array, a7, 6);
235
makeInterlacedArray(array, a8, 7);
236
makeInterlacedArray(array, a9, 8);
237
makeInterlacedArray(array, a10, 9);
240
template<typename T_numtype, int N_rank>
241
void interlaceArrays(const TinyVector<int,N_rank>& shape,
242
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
243
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
244
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
245
Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
246
Array<T_numtype,N_rank>& a9, Array<T_numtype,N_rank>& a10,
247
Array<T_numtype,N_rank>& a11)
249
GeneralArrayStorage<N_rank+1> storage;
250
Array<T_numtype, N_rank+1> array(shape, 11, storage);
251
makeInterlacedArray(array, a1, 0);
252
makeInterlacedArray(array, a2, 1);
253
makeInterlacedArray(array, a3, 2);
254
makeInterlacedArray(array, a4, 3);
255
makeInterlacedArray(array, a5, 4);
256
makeInterlacedArray(array, a6, 5);
257
makeInterlacedArray(array, a7, 6);
258
makeInterlacedArray(array, a8, 7);
259
makeInterlacedArray(array, a9, 8);
260
makeInterlacedArray(array, a10, 9);
261
makeInterlacedArray(array, a11, 10);
264
// NEEDS_WORK -- make `storage' a parameter in these routines
265
// Will be tricky: have to convert GeneralArrayStorage<N_rank> to
266
// GeneralArrayStorage<N_rank+1>
268
// These routines may or may not interlace arrays, depending on
269
// whether it is advantageous for this platform.
271
template<typename T_numtype, int N_rank>
272
void allocateArrays(const TinyVector<int,N_rank>& shape,
273
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2)
275
#ifdef BZ_INTERLACE_ARRAYS
276
interlaceArrays(shape, a1, a2);
283
template<typename T_numtype, int N_rank>
284
void allocateArrays(const TinyVector<int,N_rank>& shape,
285
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
286
Array<T_numtype,N_rank>& a3)
288
#ifdef BZ_INTERLACE_ARRAYS
289
interlaceArrays(shape, a1, a2, a3);
297
template<typename T_numtype, int N_rank>
298
void allocateArrays(const TinyVector<int,N_rank>& shape,
299
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
300
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4)
302
#ifdef BZ_INTERLACE_ARRAYS
303
interlaceArrays(shape, a1, a2, a3, a4);
312
template<typename T_numtype, int N_rank>
313
void allocateArrays(const TinyVector<int,N_rank>& shape,
314
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
315
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
316
Array<T_numtype,N_rank>& a5)
318
#ifdef BZ_INTERLACE_ARRAYS
319
interlaceArrays(shape, a1, a2, a3, a4, a5);
329
template<typename T_numtype, int N_rank>
330
void allocateArrays(const TinyVector<int,N_rank>& shape,
331
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
332
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
333
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6)
335
#ifdef BZ_INTERLACE_ARRAYS
336
interlaceArrays(shape, a1, a2, a3, a4, a5, a6);
347
template<typename T_numtype, int N_rank>
348
void allocateArrays(const TinyVector<int,N_rank>& shape,
349
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
350
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
351
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
352
Array<T_numtype,N_rank>& a7)
354
#ifdef BZ_INTERLACE_ARRAYS
355
interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7);
367
template<typename T_numtype, int N_rank>
368
void allocateArrays(const TinyVector<int,N_rank>& shape,
369
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
370
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
371
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
372
Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8)
374
#ifdef BZ_INTERLACE_ARRAYS
375
interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7, a8);
388
template<typename T_numtype, int N_rank>
389
void allocateArrays(const TinyVector<int,N_rank>& shape,
390
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
391
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
392
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
393
Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
394
Array<T_numtype,N_rank>& a9)
396
#ifdef BZ_INTERLACE_ARRAYS
397
interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7, a8, a9);
411
template<typename T_numtype, int N_rank>
412
void allocateArrays(const TinyVector<int,N_rank>& shape,
413
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
414
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
415
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
416
Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
417
Array<T_numtype,N_rank>& a9, Array<T_numtype,N_rank>& a10)
419
#ifdef BZ_INTERLACE_ARRAYS
420
interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
435
template<typename T_numtype, int N_rank>
436
void allocateArrays(const TinyVector<int,N_rank>& shape,
437
Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
438
Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
439
Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
440
Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
441
Array<T_numtype,N_rank>& a9, Array<T_numtype,N_rank>& a10,
442
Array<T_numtype,N_rank>& a11)
444
#ifdef BZ_INTERLACE_ARRAYS
445
interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
461
// NEEDS_WORK -- allocateArrays for TinyVector<Range,N_rank>
463
// This constructor is used to create interlaced arrays.
464
template<typename T_numtype, int N_rank>
465
Array<T_numtype,N_rank>::Array(const TinyVector<int,N_rank-1>& shape,
466
int lastExtent, const GeneralArrayStorage<N_rank>& storage)
469
// Create an array with the given shape, plus an extra dimension
470
// for the number of arrays being allocated. This extra dimension
471
// must have minor storage order.
473
if (ordering(0) == 0)
475
// Column major storage order (or something like it)
476
length_[0] = lastExtent;
477
storage_.setBase(0,0);
478
for (int i=1; i < N_rank; ++i)
479
length_[i] = shape[i-1];
481
else if (ordering(0) == N_rank-1)
483
// Row major storage order (or something like it)
484
for (int i=0; i < N_rank-1; ++i)
485
length_[i] = shape[i];
486
length_[N_rank-1] = lastExtent;
487
storage_.setBase(N_rank-1, 0);
490
BZPRECHECK(0, "Used allocateArrays() with a peculiar storage format");
493
setupStorage(N_rank-1);
496
// NEEDS_WORK -- see note about TinyVector<Range,N> in <blitz/arrayshape.h>
498
template<typename T_numtype, int N_rank>
499
Array<T_numtype,N_rank>::Array(const TinyVector<Range,N_rank-1>& shape,
500
int lastExtent, const GeneralArrayStorage<N_rank>& storage)
504
for (int i=0; i < N_rank; ++i)
505
BZPRECHECK(shape[i].isAscendingContiguous(),
506
"In call to allocateArrays(), a Range object is not ascending" << endl
507
<< "contiguous: " << shape[i] << endl);
510
if (ordering(0) == 0)
512
// Column major storage order (or something like it)
513
length_[0] = lastExtent;
514
storage_.setBase(0,0);
515
for (int i=1; i < N_rank; ++i)
517
length_[i] = shape[i-1].length();
518
storage_.setBase(i, shape[i-1].first());
521
else if (ordering(0) == N_rank-1)
523
// Row major storage order (or something like it)
524
for (int i=0; i < N_rank-1; ++i)
526
length_[i] = shape[i];
527
storage_.setBase(i, shape[i].first());
529
length_[N_rank-1] = lastExtent;
530
storage_.setBase(N_rank-1, 0);
533
BZPRECHECK(0, "Used allocateArrays() with a peculiar storage format");
536
setupStorage(N_rank-1);
542
#endif // BZ_ARRAYINTER_CC