source: Sophya/trunk/SophyaExt/Blitz/blitz/array/interlace.cc@ 3586

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

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

File size: 17.5 KB
Line 
1/***************************************************************************
2 * blitz/array/interlace.cc
3 *
4 * $Id: interlace.cc,v 1.1.1.1 1999-04-09 17:59:03 ansari Exp $
5 *
6 * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * Suggestions: blitz-suggest@cybervision.com
19 * Bugs: blitz-bugs@cybervision.com
20 *
21 * For more information, please see the Blitz++ Home Page:
22 * http://seurat.uwaterloo.ca/blitz/
23 *
24 ***************************************************************************
25 * $Log: not supported by cvs2svn $
26 *
27 */
28
29#ifndef BZ_ARRAYINTERLACE_CC
30#define BZ_ARRAYINTERLACE_CC
31
32#ifndef BZ_ARRAY_H
33 #error <blitz/array/interlace.cc> must be included via <blitz/array.h>
34#endif
35
36#ifndef BZ_ARRAYSHAPE_H
37 #include <blitz/array/shape.h>
38#endif
39
40BZ_NAMESPACE(blitz)
41
42/*
43 * This header provides two collections of routines:
44 *
45 * interlaceArrays(shape, A1, A2, ...);
46 * allocateArrays(shape, A1, A2, ...);
47 *
48 * interlaceArrays allocates a set of arrays so that their data is
49 * interlaced. For example,
50 *
51 * Array<int,2> A, B;
52 * interlaceArrays(shape(10,10), A, B);
53 *
54 * sets up the array storage so that A(0,0) is followed by B(0,0) in
55 * memory; then A(0,1) and B(0,1), and so on.
56 *
57 * The allocateArrays() routines may or may not interlace the data,
58 * depending on whether interlacing is advantageous for the architecture.
59 * This is controlled by the setting of BZ_INTERLACE_ARRAYS in
60 * <blitz/tuning.h>.
61 */
62
63// Warning: Can't instantiate TinyVector<Range,N> because causes
64// conflict between TinyVector<T,N>::operator=(T) and
65// TinyVector<T,N>::operator=(Range)
66
67// NEEDS_WORK -- also shape for up to N=11
68// NEEDS_WORK -- shape(Range r1, Range r2, ...) (return TinyVector<Range,n>)
69// maybe use Domain objects
70// NEEDS_WORK -- doesn't make a lot of sense for user to provide a
71// GeneralArrayStorage<N_rank+1>
72
73template<class T_numtype>
74void makeInterlacedArray(Array<T_numtype,2>& mainArray,
75 Array<T_numtype,1>& subarray, int slice)
76{
77 Array<T_numtype,1> tmp = mainArray(Range::all(), slice);
78 subarray.reference(tmp);
79}
80
81template<class T_numtype>
82void makeInterlacedArray(Array<T_numtype,3>& mainArray,
83 Array<T_numtype,2>& subarray, int slice)
84{
85 Array<T_numtype,2> tmp = mainArray(Range::all(), Range::all(),
86 slice);
87 subarray.reference(tmp);
88}
89
90template<class T_numtype>
91void makeInterlacedArray(Array<T_numtype,4>& mainArray,
92 Array<T_numtype,3>& subarray, int slice)
93{
94 Array<T_numtype,3> tmp = mainArray(Range::all(), Range::all(),
95 Range::all(), slice);
96 subarray.reference(tmp);
97}
98
99// These routines always allocate interlaced arrays
100template<class T_numtype, int N_rank>
101void interlaceArrays(const TinyVector<int,N_rank>& shape,
102 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2)
103{
104 GeneralArrayStorage<N_rank+1> storage;
105 Array<T_numtype, N_rank+1> array(shape, 2, storage);
106 makeInterlacedArray(array, a1, 0);
107 makeInterlacedArray(array, a2, 1);
108}
109
110template<class T_numtype, int N_rank>
111void interlaceArrays(const TinyVector<int,N_rank>& shape,
112 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
113 Array<T_numtype,N_rank>& a3)
114{
115 GeneralArrayStorage<N_rank+1> storage;
116 Array<T_numtype, N_rank+1> array(shape, 3, storage);
117 makeInterlacedArray(array, a1, 0);
118 makeInterlacedArray(array, a2, 1);
119 makeInterlacedArray(array, a3, 2);
120}
121
122template<class T_numtype, int N_rank>
123void interlaceArrays(const TinyVector<int,N_rank>& shape,
124 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
125 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4)
126{
127 GeneralArrayStorage<N_rank+1> storage;
128 Array<T_numtype, N_rank+1> array(shape, 4, storage);
129 makeInterlacedArray(array, a1, 0);
130 makeInterlacedArray(array, a2, 1);
131 makeInterlacedArray(array, a3, 2);
132 makeInterlacedArray(array, a4, 3);
133}
134
135template<class T_numtype, int N_rank>
136void interlaceArrays(const TinyVector<int,N_rank>& shape,
137 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
138 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
139 Array<T_numtype,N_rank>& a5)
140{
141 GeneralArrayStorage<N_rank+1> storage;
142 Array<T_numtype, N_rank+1> array(shape, 5, storage);
143 makeInterlacedArray(array, a1, 0);
144 makeInterlacedArray(array, a2, 1);
145 makeInterlacedArray(array, a3, 2);
146 makeInterlacedArray(array, a4, 3);
147 makeInterlacedArray(array, a5, 4);
148}
149
150template<class T_numtype, int N_rank>
151void interlaceArrays(const TinyVector<int,N_rank>& shape,
152 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
153 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
154 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6)
155{
156 GeneralArrayStorage<N_rank+1> storage;
157 Array<T_numtype, N_rank+1> array(shape, 6, storage);
158 makeInterlacedArray(array, a1, 0);
159 makeInterlacedArray(array, a2, 1);
160 makeInterlacedArray(array, a3, 2);
161 makeInterlacedArray(array, a4, 3);
162 makeInterlacedArray(array, a5, 4);
163 makeInterlacedArray(array, a6, 5);
164}
165
166template<class T_numtype, int N_rank>
167void interlaceArrays(const TinyVector<int,N_rank>& shape,
168 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
169 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
170 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
171 Array<T_numtype,N_rank>& a7)
172{
173 GeneralArrayStorage<N_rank+1> storage;
174 Array<T_numtype, N_rank+1> array(shape, 7, storage);
175 makeInterlacedArray(array, a1, 0);
176 makeInterlacedArray(array, a2, 1);
177 makeInterlacedArray(array, a3, 2);
178 makeInterlacedArray(array, a4, 3);
179 makeInterlacedArray(array, a5, 4);
180 makeInterlacedArray(array, a6, 5);
181 makeInterlacedArray(array, a7, 6);
182}
183
184template<class T_numtype, int N_rank>
185void interlaceArrays(const TinyVector<int,N_rank>& shape,
186 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
187 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
188 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
189 Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8)
190{
191 GeneralArrayStorage<N_rank+1> storage;
192 Array<T_numtype, N_rank+1> array(shape, 8, storage);
193 makeInterlacedArray(array, a1, 0);
194 makeInterlacedArray(array, a2, 1);
195 makeInterlacedArray(array, a3, 2);
196 makeInterlacedArray(array, a4, 3);
197 makeInterlacedArray(array, a5, 4);
198 makeInterlacedArray(array, a6, 5);
199 makeInterlacedArray(array, a7, 6);
200 makeInterlacedArray(array, a8, 7);
201}
202
203template<class T_numtype, int N_rank>
204void interlaceArrays(const TinyVector<int,N_rank>& shape,
205 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
206 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
207 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
208 Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
209 Array<T_numtype,N_rank>& a9)
210{
211 GeneralArrayStorage<N_rank+1> storage;
212 Array<T_numtype, N_rank+1> array(shape, 9, storage);
213 makeInterlacedArray(array, a1, 0);
214 makeInterlacedArray(array, a2, 1);
215 makeInterlacedArray(array, a3, 2);
216 makeInterlacedArray(array, a4, 3);
217 makeInterlacedArray(array, a5, 4);
218 makeInterlacedArray(array, a6, 5);
219 makeInterlacedArray(array, a7, 6);
220 makeInterlacedArray(array, a8, 7);
221 makeInterlacedArray(array, a9, 8);
222}
223
224template<class T_numtype, int N_rank>
225void interlaceArrays(const TinyVector<int,N_rank>& shape,
226 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
227 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
228 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
229 Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
230 Array<T_numtype,N_rank>& a9, Array<T_numtype,N_rank>& a10)
231{
232 GeneralArrayStorage<N_rank+1> storage;
233 Array<T_numtype, N_rank+1> array(shape, 10, storage);
234 makeInterlacedArray(array, a1, 0);
235 makeInterlacedArray(array, a2, 1);
236 makeInterlacedArray(array, a3, 2);
237 makeInterlacedArray(array, a4, 3);
238 makeInterlacedArray(array, a5, 4);
239 makeInterlacedArray(array, a6, 5);
240 makeInterlacedArray(array, a7, 6);
241 makeInterlacedArray(array, a8, 7);
242 makeInterlacedArray(array, a9, 8);
243 makeInterlacedArray(array, a10, 9);
244}
245
246template<class T_numtype, int N_rank>
247void interlaceArrays(const TinyVector<int,N_rank>& shape,
248 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
249 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
250 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
251 Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
252 Array<T_numtype,N_rank>& a9, Array<T_numtype,N_rank>& a10,
253 Array<T_numtype,N_rank>& a11)
254{
255 GeneralArrayStorage<N_rank+1> storage;
256 Array<T_numtype, N_rank+1> array(shape, 11, storage);
257 makeInterlacedArray(array, a1, 0);
258 makeInterlacedArray(array, a2, 1);
259 makeInterlacedArray(array, a3, 2);
260 makeInterlacedArray(array, a4, 3);
261 makeInterlacedArray(array, a5, 4);
262 makeInterlacedArray(array, a6, 5);
263 makeInterlacedArray(array, a7, 6);
264 makeInterlacedArray(array, a8, 7);
265 makeInterlacedArray(array, a9, 8);
266 makeInterlacedArray(array, a10, 9);
267 makeInterlacedArray(array, a11, 10);
268}
269
270// NEEDS_WORK -- make `storage' a parameter in these routines
271// Will be tricky: have to convert GeneralArrayStorage<N_rank> to
272// GeneralArrayStorage<N_rank+1>
273
274// These routines may or may not interlace arrays, depending on
275// whether it is advantageous for this platform.
276
277template<class T_numtype, int N_rank>
278void allocateArrays(const TinyVector<int,N_rank>& shape,
279 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2)
280{
281#ifdef BZ_INTERLACE_ARRAYS
282 interlaceArrays(shape, a1, a2);
283#else
284 a1.resize(shape);
285 a2.resize(shape);
286#endif
287}
288
289template<class T_numtype, int N_rank>
290void allocateArrays(const TinyVector<int,N_rank>& shape,
291 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
292 Array<T_numtype,N_rank>& a3)
293{
294#ifdef BZ_INTERLACE_ARRAYS
295 interlaceArrays(shape, a1, a2, a3);
296#else
297 a1.resize(shape);
298 a2.resize(shape);
299 a3.resize(shape);
300#endif
301}
302
303template<class T_numtype, int N_rank>
304void allocateArrays(const TinyVector<int,N_rank>& shape,
305 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
306 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4)
307{
308#ifdef BZ_INTERLACE_ARRAYS
309 interlaceArrays(shape, a1, a2, a3, a4);
310#else
311 a1.resize(shape);
312 a2.resize(shape);
313 a3.resize(shape);
314 a4.resize(shape);
315#endif
316}
317
318template<class T_numtype, int N_rank>
319void allocateArrays(const TinyVector<int,N_rank>& shape,
320 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
321 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
322 Array<T_numtype,N_rank>& a5)
323{
324#ifdef BZ_INTERLACE_ARRAYS
325 interlaceArrays(shape, a1, a2, a3, a4, a5);
326#else
327 a1.resize(shape);
328 a2.resize(shape);
329 a3.resize(shape);
330 a4.resize(shape);
331 a5.resize(shape);
332#endif
333}
334
335template<class T_numtype, int N_rank>
336void allocateArrays(const TinyVector<int,N_rank>& shape,
337 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
338 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
339 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6)
340{
341#ifdef BZ_INTERLACE_ARRAYS
342 interlaceArrays(shape, a1, a2, a3, a4, a5, a6);
343#else
344 a1.resize(shape);
345 a2.resize(shape);
346 a3.resize(shape);
347 a4.resize(shape);
348 a5.resize(shape);
349 a6.resize(shape);
350#endif
351}
352
353template<class T_numtype, int N_rank>
354void allocateArrays(const TinyVector<int,N_rank>& shape,
355 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
356 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
357 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
358 Array<T_numtype,N_rank>& a7)
359{
360#ifdef BZ_INTERLACE_ARRAYS
361 interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7);
362#else
363 a1.resize(shape);
364 a2.resize(shape);
365 a3.resize(shape);
366 a4.resize(shape);
367 a5.resize(shape);
368 a6.resize(shape);
369 a7.resize(shape);
370#endif
371}
372
373template<class T_numtype, int N_rank>
374void allocateArrays(const TinyVector<int,N_rank>& shape,
375 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
376 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
377 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
378 Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8)
379{
380#ifdef BZ_INTERLACE_ARRAYS
381 interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7, a8);
382#else
383 a1.resize(shape);
384 a2.resize(shape);
385 a3.resize(shape);
386 a4.resize(shape);
387 a5.resize(shape);
388 a6.resize(shape);
389 a7.resize(shape);
390 a8.resize(shape);
391#endif
392}
393
394template<class T_numtype, int N_rank>
395void allocateArrays(const TinyVector<int,N_rank>& shape,
396 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
397 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
398 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
399 Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
400 Array<T_numtype,N_rank>& a9)
401{
402#ifdef BZ_INTERLACE_ARRAYS
403 interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7, a8, a9);
404#else
405 a1.resize(shape);
406 a2.resize(shape);
407 a3.resize(shape);
408 a4.resize(shape);
409 a5.resize(shape);
410 a6.resize(shape);
411 a7.resize(shape);
412 a8.resize(shape);
413 a9.resize(shape);
414#endif
415}
416
417template<class T_numtype, int N_rank>
418void allocateArrays(const TinyVector<int,N_rank>& shape,
419 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
420 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
421 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
422 Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
423 Array<T_numtype,N_rank>& a9, Array<T_numtype,N_rank>& a10)
424{
425#ifdef BZ_INTERLACE_ARRAYS
426 interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
427#else
428 a1.resize(shape);
429 a2.resize(shape);
430 a3.resize(shape);
431 a4.resize(shape);
432 a5.resize(shape);
433 a6.resize(shape);
434 a7.resize(shape);
435 a8.resize(shape);
436 a9.resize(shape);
437 a10.resize(shape);
438#endif
439}
440
441template<class T_numtype, int N_rank>
442void allocateArrays(const TinyVector<int,N_rank>& shape,
443 Array<T_numtype,N_rank>& a1, Array<T_numtype,N_rank>& a2,
444 Array<T_numtype,N_rank>& a3, Array<T_numtype,N_rank>& a4,
445 Array<T_numtype,N_rank>& a5, Array<T_numtype,N_rank>& a6,
446 Array<T_numtype,N_rank>& a7, Array<T_numtype,N_rank>& a8,
447 Array<T_numtype,N_rank>& a9, Array<T_numtype,N_rank>& a10,
448 Array<T_numtype,N_rank>& a11)
449{
450#ifdef BZ_INTERLACE_ARRAYS
451 interlaceArrays(shape, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
452#else
453 a1.resize(shape);
454 a2.resize(shape);
455 a3.resize(shape);
456 a4.resize(shape);
457 a5.resize(shape);
458 a6.resize(shape);
459 a7.resize(shape);
460 a8.resize(shape);
461 a9.resize(shape);
462 a10.resize(shape);
463 a11.resize(shape);
464#endif
465}
466
467// NEEDS_WORK -- allocateArrays for TinyVector<Range,N_rank>
468
469// This constructor is used to create interlaced arrays.
470template<class T_numtype, int N_rank>
471Array<T_numtype,N_rank>::Array(const TinyVector<int,N_rank-1>& shape,
472 int lastExtent, const GeneralArrayStorage<N_rank>& storage)
473 : storage_(storage)
474{
475 // Create an array with the given shape, plus an extra dimension
476 // for the number of arrays being allocated. This extra dimension
477 // must have minor storage order.
478
479 if (ordering(0) == 0)
480 {
481 // Column major storage order (or something like it)
482 length_[0] = lastExtent;
483 storage_.setBase(0,0);
484 for (int i=1; i < N_rank; ++i)
485 length_[i] = shape[i-1];
486 }
487 else if (ordering(0) == N_rank-1)
488 {
489 // Row major storage order (or something like it)
490 for (int i=0; i < N_rank-1; ++i)
491 length_[i] = shape[i];
492 length_[N_rank-1] = lastExtent;
493 storage_.setBase(N_rank-1, 0);
494 }
495 else {
496 BZPRECHECK(0, "Used allocateArrays() with a peculiar storage format");
497 }
498
499 setupStorage(N_rank-1);
500}
501
502// NEEDS_WORK -- see note about TinyVector<Range,N> in <blitz/arrayshape.h>
503#if 0
504template<class T_numtype, int N_rank>
505Array<T_numtype,N_rank>::Array(const TinyVector<Range,N_rank-1>& shape,
506 int lastExtent, const GeneralArrayStorage<N_rank>& storage)
507 : storage_(storage)
508{
509#ifdef BZ_DEBUG
510 for (int i=0; i < N_rank; ++i)
511 BZPRECHECK(shape[i].isAscendingContiguous(),
512 "In call to allocateArrays(), a Range object is not ascending" << endl
513 << "contiguous: " << shape[i] << endl);
514#endif
515
516 if (ordering(0) == 0)
517 {
518 // Column major storage order (or something like it)
519 length_[0] = lastExtent;
520 storage_.setBase(0,0);
521 for (int i=1; i < N_rank; ++i)
522 {
523 length_[i] = shape[i-1].length();
524 storage_.setBase(i, shape[i-1].first());
525 }
526 }
527 else if (ordering(0) == N_rank-1)
528 {
529 // Row major storage order (or something like it)
530 for (int i=0; i < N_rank-1; ++i)
531 {
532 length_[i] = shape[i];
533 storage_.setBase(i, shape[i].first());
534 }
535 length_[N_rank-1] = lastExtent;
536 storage_.setBase(N_rank-1, 0);
537 }
538 else {
539 BZPRECHECK(0, "Used allocateArrays() with a peculiar storage format");
540 }
541
542 setupStorage(N_rank-1);
543}
544#endif
545
546BZ_NAMESPACE_END
547
548#endif // BZ_ARRAYINTER_CC
549
Note: See TracBrowser for help on using the repository browser.