array_dec.h 18.7 KB
Newer Older
1
2
3
4
5
/*! \file array_dec.h
 * \brief array class declarations (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id: array_dec.h,v 1.29 2006/06/20 07:26:29 tforb Exp $
7
 * \author Thomas Forbriger
8
 * \since 08/12/2002
9
10
 * 
 * array class declarations (prototypes)
11
 *
thomas.forbriger's avatar
thomas.forbriger committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
28
29
 * \note
 * This should never be included directly. Use array.h or binarray.h instead.
30
 *
31
32
33
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
34
 *  - 08/12/2002   V1.0   copied from libcontxx
35
36
37
38
 *  - 16/12/2002   V1.1   (thof)
 *                        - introduced new concept of const-correctness;
 *                          this should work for passing arrays as intrinsic
 *                          references for const element type
thomas.forbriger's avatar
...    
thomas.forbriger committed
39
40
 *  - 17/12/2002   V1.2   (thof)
 *                        - introduced access declarations
thomas.forbriger's avatar
thomas.forbriger committed
41
42
43
44
 *  - 19/12/2002   V1.3   (thof)
 *                        - provide read access to base classes
 *                          and read/write access to representation of
 *                          non-const array
thomas.forbriger's avatar
thomas.forbriger committed
45
46
47
48
 *                        - replaced four size-argument constructors by one
 *                          constructor with default arguments
 *                        - distinguish between mutable and non-mutable
 *                          representation
thomas.forbriger's avatar
thomas.forbriger committed
49
50
51
52
 *  - 20/12/2002   V1.4   (thof)
 *                        - Access declarations are nor useable with functions
 *                          that return a reference to *this. They must be
 *                          reimplemented.
thomas.forbriger's avatar
thomas.forbriger committed
53
54
 *  - 23/12/2002   V1.5   (thof)
 *                        - copyin() and copyout() work
thomas.forbriger's avatar
thomas.forbriger committed
55
56
57
 *  - 28/12/2002   V1.6   (thof)
 *                        - changed base class from specialization to
 *                          independent class template
58
59
60
 *  - 29/12/2002   V1.7   (thof)
 *                        - ConstArray now inherits from ConstSharedHeap
 *                          (see "\ref sec_design_replicated")
61
62
63
64
65
66
67
 *                        - use "using" syntax for access declarations
 *                        - arrays do no longer derive from shared heap
 *                          classes
 *                        - reworked shared heap access concept fundamentally;
 *                          code looks much simpler now 
 *                        - member template must be defined here
 *                        - factored out copyin code
thomas.forbriger's avatar
thomas.forbriger committed
68
69
70
71
72
 *  - 31/12/2002   V1.8   (thof)
 *                        - Removed non-initializing constructors in
 *                          ConstArray (except default constructor, which is
 *                          needed, when object is a container element) as
 *                          suggested by Wolfgang.
thomas.forbriger's avatar
thomas.forbriger committed
73
74
 *  - 02/01/2003   V1.9   (thof)
 *                        - type TIndexVec was not declared
thomas.forbriger's avatar
thomas.forbriger committed
75
76
77
78
 *  - 03/01/2003   V1.10  (thof)
 *                        - copyout function now returns an aff::Array rather
 *                          than an aff::ConstArray
 *                        - aff::Array inherits copyout from aff::ConstArray
79
 *                        - deepcopy is now in namespace aff
80
81
 *  - 04/07/2005   V1.11  (thof)
 *                        - provide data modification through const Array
82
83
 *  - 05/07/2005   V1.12  (thof)
 *                        - expose const representation too if declared const 
thomas.forbriger's avatar
thomas.forbriger committed
84
85
 *  - 19/06/2006   V1.13  (thof)
 *                        - offer Tvalue and friends as non-const types
86
87
88
89
90
 * 
 * ============================================================================
 */

// include guard
91
#ifndef AFF_ARRAY_DEC_H_VERSION
92

93
#define AFF_ARRAY_DEC_H_VERSION \
thomas.forbriger's avatar
thomas.forbriger committed
94
  "AFF_ARRAY_DEC_H   V1.13"
95
#define AFF_ARRAY_DEC_H_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
96
  "$Id: array_dec.h,v 1.29 2006/06/20 07:26:29 tforb Exp $"
97

thomas.forbriger's avatar
thomas.forbriger committed
98
99
#include <aff/lib/sharedheap.h>
#include <aff/lib/strided.h>
100
101
#include <aff/lib/stridedstepper.h>
#include <aff/lib/deepcopy.h>
102

103
namespace aff {
104

105
#ifdef AFF_PREBUILT
106
107
108
namespace prebuilt {
#endif

thomas.forbriger's avatar
thomas.forbriger committed
109
110
111
  // forward declaration
  template<class T> class Array;

thomas.forbriger's avatar
thomas.forbriger committed
112
  /*! \brief Array base class
113
   *
thomas.forbriger's avatar
thomas.forbriger committed
114
115
116
   * This is a multidimensional (array) container that uses a strided memory
   * layout (Fortran shape) and counted references to data in global memory.
   * For examples see the various test programs.
117
   *
thomas.forbriger's avatar
thomas.forbriger committed
118
119
120
   * \sa \ref sec_design_multidimensional
   * \sa \ref sec_design_interface
   * \sa \ref sec_design_const
121
   * \sa \ref page_representation
122
   * \sa \ref sec_design_replicated
thomas.forbriger's avatar
thomas.forbriger committed
123
   * \sa aff::Strided
thomas.forbriger's avatar
thomas.forbriger committed
124
125
126
127
   * \sa aff::SharedHeap
   * \sa tests/arraytest.cc
   * \sa tests/f77test.cc
   * \sa tests/helpertest.cc
128
129
   */
  template<class T>
thomas.forbriger's avatar
thomas.forbriger committed
130
    class ConstArray:
131
      private aff::Strided
132
133
134
135
136
137
138
139
    {
      public:
        /*! \name Various types
         *
         * In particular due to our concept of const-correctness we need
         * several typedefs to declare types derived from the element type of
         * the array.
         *
thomas.forbriger's avatar
thomas.forbriger committed
140
141
142
143
144
145
146
         * This is in particular usefull for function templates that may take
         * any container class as argument (e.g. aff::subarray) or for other
         * classes dealing with any container (e.g. aff::Iterator).
         * By means of these typedefs they may retrieve appropriate types they
         * have to deal with.
         *
         * \sa \ref sec_design_interface_typedef
thomas.forbriger's avatar
thomas.forbriger committed
147
         * \sa \ref sec_design_const
148
149
150
         */
        //@{
        //! Type of representation
thomas.forbriger's avatar
thomas.forbriger committed
151
        typedef aff::ConstSharedHeap<T> Trepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
152
        //! Type of shape
153
        typedef aff::Strided Tshape;
thomas.forbriger's avatar
thomas.forbriger committed
154
155
        //! Type of shape stepper
        typedef Tshape::Tstepper Tstepper;
thomas.forbriger's avatar
thomas.forbriger committed
156
157
        //! we use this for one of the access operators
        typedef Tshape::TIndexVec TIndexVec;
158
        //! Element type
thomas.forbriger's avatar
thomas.forbriger committed
159
        typedef T Tvalue;
160
        //! Type of pointer to element
thomas.forbriger's avatar
thomas.forbriger committed
161
        typedef T* Tpointer;
162
        //! Type of reference to element
thomas.forbriger's avatar
thomas.forbriger committed
163
        typedef T& Treference;
thomas.forbriger's avatar
thomas.forbriger committed
164
165
166
167
168
169
        //! const element type
        typedef const T Tconst_value;
        //! Type of pointer to const element
        typedef const T* Tconst_pointer;
        //! Type of reference to const element
        typedef const T& Tconst_reference;
170
        //! Type of this array
thomas.forbriger's avatar
thomas.forbriger committed
171
        typedef ConstArray<T> Tcontainer;
172
173
        //! Type of the array of const values
        typedef Tcontainer Tcontainer_of_const;
thomas.forbriger's avatar
thomas.forbriger committed
174
        //! Short for Tcontainer_of_const
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        typedef Tcontainer Tcoc;
        //@}

        /*-----------------------------------------------------------------*/
        
        /*! \name Constructors
         *
         * \note
         * We use the default copy constructor, which automatically invokes
         * the copy constructors of the base classes aff::Strided and
         * aff::SharedHeap<T>. This essentially is a shallow copy of the
         * array, i.e. the copy will reference to the same elements in memory.
         * See aff::Array<T>::copyin() and aff::Array<T>::copyout() for deep
         * copy operations.
         */
        //@{
        //! construct from nothing (empty)
thomas.forbriger's avatar
thomas.forbriger committed
192
        ConstArray() { }
193
        //! construct from shape and representation
thomas.forbriger's avatar
thomas.forbriger committed
194
        ConstArray(const Tshape& shape, 
195
196
197
                   const Trepresentation& representation):
          Tshape(shape), Mrepresentation(representation)
          { check_consistency(); }
thomas.forbriger's avatar
thomas.forbriger committed
198
199
200
        //@}

        /*-----------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
201
      
thomas.forbriger's avatar
thomas.forbriger committed
202
203
204
205
        /*! \name Const access operators
         *
         * Although we generally distinguish between the constness of the
         * container and the constness of the contained data (see 
thomas.forbriger's avatar
thomas.forbriger committed
206
         * \ref sec_design_const), we provide const element access operators
thomas.forbriger's avatar
thomas.forbriger committed
207
208
209
210
         * the prohibit element modification. We assume that a const version
         * of the array is usually meant to be used only for reading.
         */
        //@{
thomas.forbriger's avatar
thomas.forbriger committed
211
        //! full dimensionality access
212
        const T& operator()(const TIndexVec& index) const
213
        { return(Mrepresentation[offset(index)]); }
thomas.forbriger's avatar
thomas.forbriger committed
214
        //! access from 1 index value
215
        const T& operator()(const Tsubscript& i0) const
216
        { return(Mrepresentation[offset(i0)]); }
thomas.forbriger's avatar
thomas.forbriger committed
217
        //! access from 2 index values
218
        const T& operator()(const Tsubscript& i0,
thomas.forbriger's avatar
thomas.forbriger committed
219
                            const Tsubscript& i1) const
220
        { return(Mrepresentation[offset(i0, i1)]); }
thomas.forbriger's avatar
thomas.forbriger committed
221
        //! access from 3 index values
222
        const T& operator()(const Tsubscript& i0,
thomas.forbriger's avatar
thomas.forbriger committed
223
224
                            const Tsubscript& i1,
                            const Tsubscript& i2) const
225
        { return(Mrepresentation[offset(i0, i1, i2)]); }
thomas.forbriger's avatar
thomas.forbriger committed
226
        //! access from 4 index values
227
        const T& operator()(const Tsubscript& i0,
thomas.forbriger's avatar
thomas.forbriger committed
228
229
230
                            const Tsubscript& i1,
                            const Tsubscript& i2,
                            const Tsubscript& i3) const
231
        { return(Mrepresentation[offset(i0, i1, i2, i3)]); }
thomas.forbriger's avatar
thomas.forbriger committed
232
233
234
        //@}

        /*-----------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
235

thomas.forbriger's avatar
thomas.forbriger committed
236
237
238
        /*! \name Shape access
         */
        //@{
thomas.forbriger's avatar
thomas.forbriger committed
239
240
241
242
243
244
        //! return first index of dimension i
        const Tsubscript& f(const Tsubscript& i) const
        { return(this->Tshape::first(i)); }
        //! return last index of dimension i
        const Tsubscript& l(const Tsubscript& i) const
        { return(this->Tshape::last(i)); }
thomas.forbriger's avatar
thomas.forbriger committed
245
        //@}
thomas.forbriger's avatar
thomas.forbriger committed
246

thomas.forbriger's avatar
thomas.forbriger committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        /*! \brief create an identical copy (deep copy) of this array
         *
         * This is mainly used to create a copy of a truely identical array
         * (i.e. array with same element type or at least const version of
         * same element type). Use this function in conjunction with the
         * assignment operator. E.g.:
         * \code
         *   aff::Array<int> A(3,4);
         *   A=5.;
         *   aff::Array<int> B, C;
         *   B=A.copyout();
         *   C=A;
         * \endcode
         * Here arrays \c A and \c B have exactly the same contents but use
         * different memory. While changes to elements of \c C will also
         * affect elements of \c A, this not the case for changes applied to
         * \c B.
         */
thomas.forbriger's avatar
thomas.forbriger committed
265
        Array<T> copyout() const;
thomas.forbriger's avatar
thomas.forbriger committed
266
267
268
269
      
        //! \name access declarations
        //@{
        //! access to base class function
270
271
272
        using Tshape::first;
        using Tshape::last;
        using Tshape::size;
thomas.forbriger's avatar
thomas.forbriger committed
273
274
        //@}
     
thomas.forbriger's avatar
thomas.forbriger committed
275
        //! provide access to const shape
thomas.forbriger's avatar
thomas.forbriger committed
276
        const Tshape& shape() const { return(*this); }
thomas.forbriger's avatar
thomas.forbriger committed
277
278
     
        //! provide restricted access representation
thomas.forbriger's avatar
thomas.forbriger committed
279
        const Trepresentation& representation() const
280
        { return (Mrepresentation); }
281
282

      protected:
283
284
        //! provide access to shape offset functions for derived class
        using Tshape::offset;
285
          
286
287
288
289
290
291
      private:
        //! check consitency between representation and shape
        void check_consistency() const;
        
        //! representation member
        Trepresentation Mrepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305

    }; // class ConstArray

  /*======================================================================*/

  /*! \brief Full multi-dimensional array functionality.
   *
   * This is the full array class template. It adds no additional
   * functionality to its base class aff::ConstArray. But it provied acess to
   * all member functions of the base, also to functions that allow data
   * modification.
   *
   * \sa aff::ConstArray
   * \sa \ref sec_design_const
306
   * \sa \ref sec_design_replicated
thomas.forbriger's avatar
thomas.forbriger committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
   */
  template<class T>
    class Array:
      public ConstArray<T>
    {
      public:
        /*! \name Various types
         *
         * In particular due to our concept of const-correctness we need
         * several typedefs to declare types derived from the element type of
         * the array.
         *
         * \sa \ref sec_design_interface_typedef
         * \sa \ref sec_design_const
         */
        //@{
        //! Type of this array
        typedef Array<T> Tcontainer;
        //! base is container of const (see specialization below)
326
        typedef aff::ConstArray<T> Tbase;
thomas.forbriger's avatar
thomas.forbriger committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
        //! Type of the array of const values
        typedef Tbase Tcontainer_of_const;
        /*! \brief short for Tcontainer_of_const
         *
         * We generally distinguish between constness of the array and
         * constness of the contained data (see \ref sec_design_const).
         * There will be situations, when you want to promise that a function
         * will not change the contents of an array. In this case you may use
         * a declaration (prototype) like
         * \code
         *   typedef aff::Array<int> Tmyarray;
         *   void myfunction(const Tmyarray::Tcoc& array);
         * \endcode
         * and you may use this function like
         * \code
         *   Tmyarray A(6,7);
         *   A=4;
         *   myfunction(A);
         * \endcode
         *
         * \sa \ref sec_design_const
         */
        typedef Tbase Tcoc;
        //! Type of representation
351
        typedef aff::SharedHeap<T> Trepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
352
        //! Type of subscriptor
353
        typedef aff::Strided Tshape;
thomas.forbriger's avatar
thomas.forbriger committed
354
355
        //! we use this for one of the access operators
        typedef Tshape::TIndexVec TIndexVec;
thomas.forbriger's avatar
thomas.forbriger committed
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
        //! Element type
        typedef T Tvalue;
        //! Type of pointer to element
        typedef T* Tpointer;
        //! Type of reference to element
        typedef T& Treference;
        //! const element type
        typedef const T Tconst_value;
        //! Type of pointer to const element
        typedef const T* Tconst_pointer;
        //! Type of reference to const element
        typedef const T& Tconst_reference;
        //@}

        /*-----------------------------------------------------------------*/
        
        /*! \name Constructors
         *
         * \note
         * We use the default copy constructor, which automatically invokes
         * the copy constructors of the base classes aff::Strided and
         * aff::SharedHeap<T>. This essentially is a shallow copy of the
         * array, i.e. the copy will reference to the same elements in memory.
         * See aff::Array<T>::copyin() and aff::Array<T>::copyout() for deep
         * copy operations.
         */
        //@{
        //! construct from nothing (empty)
384
        Array() { }
thomas.forbriger's avatar
thomas.forbriger committed
385
386
387
        //! construct from shape and representation
        Array(const Tshape& shape, 
              const Trepresentation& representation):
388
389
          Tbase(shape, representation),
          Mrepresentation(representation) { }
thomas.forbriger's avatar
thomas.forbriger committed
390
        //! construct from shape (defines size and layout)
391
392
393
394
395
396
397
        explicit Array(const Tshape& shape)
          { 
            Tshape newshape(shape.first(), shape.last());
            Mrepresentation=Trepresentation(newshape.memory_size());
            this->Tbase::operator=(Tbase(newshape, Mrepresentation));
          }
        //! construct from dimension sizes
thomas.forbriger's avatar
thomas.forbriger committed
398
        explicit Array(const Tsize& s0, const Tsize& s1=1, 
399
400
401
402
403
404
                       const Tsize& s2=1, const Tsize& s3=1)
          { 
            Tshape newshape(s0, s1, s2, s3);
            Mrepresentation=Trepresentation(newshape.memory_size());
            this->Tbase::operator=(Tbase(newshape, Mrepresentation));
          }
thomas.forbriger's avatar
thomas.forbriger committed
405
406
407
408
409
410
411
        //@}

        /*-----------------------------------------------------------------*/
      
        //! \name access declarations
        //@{
        //! access to base class function
412
413
        using Tbase::operator();
        using Tbase::shape;
thomas.forbriger's avatar
thomas.forbriger committed
414
        using Tbase::copyout;
thomas.forbriger's avatar
thomas.forbriger committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
        //@}

        /*! \brief copy values (deep copy) from other array of convertible type
         *
         * This member function reads the element values of another array of
         * same shape and applies them to this array. In fact the shape needs
         * not be the same. The copy is done through sequential access and as
         * most number as possible will be copied in increasing memory address
         * order.
         *
         * Example:
         * \code
         *   aff::Array<float> A(24);
         *   A=15.
         *   aff::Array<double> B(3,8);
         *   B.copyin(A);
         * \endcode
         * \c B will preserve its shape but is filled with the contents of 
         * \c A (which are not where interesting in this example). Changes
         * applied to the contents of B will not affect the contents of A.
         *
436
437
         * \param a other container with element type convertible to element
         *        type of this array and appropriate stepper class
thomas.forbriger's avatar
thomas.forbriger committed
438
439
         * \return itself
         */
440
441
        template<class C>
          Array& copyin(const C& a)
thomas.forbriger's avatar
thomas.forbriger committed
442
          {
443
            aff::deepcopy(a, *this);
thomas.forbriger's avatar
thomas.forbriger committed
444
445
446
447
            return(*this);
          }
     
        //! return full access representation
448
        const Trepresentation& representation() const
449
450
451
452
453
454
455
456
457
458
        { return (Mrepresentation); }

        /*-----------------------------------------------------------------*/
          
        //! set whole array to value
        Tcontainer& operator=(const T& value);

        /*-----------------------------------------------------------------*/

        //! full dimensionality access
459
        T& operator()(const TIndexVec& index) const
460
461
        { return(Mrepresentation[this->Tbase::offset(index)]); }
        //! access from 1 index value
462
        T& operator()(const Tsubscript& i0) const
463
464
465
        { return(Mrepresentation[this->Tbase::offset(i0)]); }
        //! access from 2 index values
        T& operator()(const Tsubscript& i0,
466
                      const Tsubscript& i1) const
467
468
469
470
        { return(Mrepresentation[this->Tbase::offset(i0, i1)]); }
        //! access from 3 index values
        T& operator()(const Tsubscript& i0,
                      const Tsubscript& i1,
471
                      const Tsubscript& i2) const
472
473
474
475
476
        { return(Mrepresentation[this->Tbase::offset(i0, i1, i2)]); }
        //! access from 4 index values
        T& operator()(const Tsubscript& i0,
                      const Tsubscript& i1,
                      const Tsubscript& i2,
477
                      const Tsubscript& i3) const
478
        { return(Mrepresentation[this->Tbase::offset(i0, i1, i2, i3)]); }
thomas.forbriger's avatar
thomas.forbriger committed
479
480

      private:
481
482
        //! my (mutable) data representation
        Trepresentation Mrepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
483
484
485

    }; // class Array

486
/*----------------------------------------------------------------------*/
487

488
489
#ifdef AFF_PREBUILT
} // namespace prebuilt
490
491
#endif

492
} // namespace aff
493

494
495
#ifndef AFF_NO_DEFINITIONS
#include <aff/array_def.h>
496
497
#endif

498
#endif // AFF_ARRAY_DEC_H_VERSION (includeguard)
499
500

/* ----- END OF array_dec.h ----- */