array_dec.h 17.2 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.20 2002-12-31 17:19:46 forbrig Exp $
7
 * \author Thomas Forbriger
8
 * \since 08/12/2002
9
10
 * 
 * array class declarations (prototypes)
11
12
13
 *
 * \note
 * This should never be included directly. Use array.h or binarray.h instead.
14
 *
15
16
17
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
18
 *  - 08/12/2002   V1.0   copied from libcontxx
19
20
21
22
 *  - 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
23
24
 *  - 17/12/2002   V1.2   (thof)
 *                        - introduced access declarations
thomas.forbriger's avatar
thomas.forbriger committed
25
26
27
28
 *  - 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
29
30
31
32
 *                        - 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
33
34
35
36
 *  - 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
37
38
 *  - 23/12/2002   V1.5   (thof)
 *                        - copyin() and copyout() work
thomas.forbriger's avatar
thomas.forbriger committed
39
40
41
 *  - 28/12/2002   V1.6   (thof)
 *                        - changed base class from specialization to
 *                          independent class template
42
43
44
 *  - 29/12/2002   V1.7   (thof)
 *                        - ConstArray now inherits from ConstSharedHeap
 *                          (see "\ref sec_design_replicated")
45
46
47
48
49
50
51
 *                        - 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
52
53
54
55
56
 *  - 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.
57
58
59
60
61
 * 
 * ============================================================================
 */

// include guard
62
#ifndef AFF_ARRAY_DEC_H_VERSION
63

64
#define AFF_ARRAY_DEC_H_VERSION \
thomas.forbriger's avatar
thomas.forbriger committed
65
  "AFF_ARRAY_DEC_H   V1.8"
66
#define AFF_ARRAY_DEC_H_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
67
  "$Id: array_dec.h,v 1.20 2002-12-31 17:19:46 forbrig Exp $"
68

thomas.forbriger's avatar
thomas.forbriger committed
69
70
#include <aff/lib/sharedheap.h>
#include <aff/lib/strided.h>
71
72
#include <aff/lib/stridedstepper.h>
#include <aff/lib/deepcopy.h>
73

74
namespace aff {
75

76
#ifdef AFF_PREBUILT
77
78
79
namespace prebuilt {
#endif

thomas.forbriger's avatar
thomas.forbriger committed
80
  /*! \brief Array base class
81
   *
thomas.forbriger's avatar
thomas.forbriger committed
82
83
84
   * 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.
85
   *
thomas.forbriger's avatar
thomas.forbriger committed
86
87
88
   * \sa \ref sec_design_multidimensional
   * \sa \ref sec_design_interface
   * \sa \ref sec_design_const
89
   * \sa \ref page_representation
90
   * \sa \ref sec_design_replicated
thomas.forbriger's avatar
thomas.forbriger committed
91
   * \sa aff::Strided
thomas.forbriger's avatar
thomas.forbriger committed
92
93
94
95
   * \sa aff::SharedHeap
   * \sa tests/arraytest.cc
   * \sa tests/f77test.cc
   * \sa tests/helpertest.cc
96
97
   */
  template<class T>
thomas.forbriger's avatar
thomas.forbriger committed
98
    class ConstArray:
99
      private aff::Strided
100
101
102
103
104
105
106
107
    {
      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
108
109
110
111
112
113
114
         * 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
115
         * \sa \ref sec_design_const
116
117
118
         */
        //@{
        //! Type of representation
thomas.forbriger's avatar
thomas.forbriger committed
119
        typedef aff::ConstSharedHeap<T> Trepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
120
        //! Type of shape
121
122
123
124
125
126
127
        typedef aff::Strided Tshape;
        //! Element type
        typedef const T Tvalue;
        //! Type of pointer to element
        typedef const T* Tpointer;
        //! Type of reference to element
        typedef const T& Treference;
thomas.forbriger's avatar
thomas.forbriger committed
128
129
130
131
132
133
        //! 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;
134
        //! Type of this array
thomas.forbriger's avatar
thomas.forbriger committed
135
        typedef ConstArray<T> Tcontainer;
136
137
        //! Type of the array of const values
        typedef Tcontainer Tcontainer_of_const;
thomas.forbriger's avatar
thomas.forbriger committed
138
        //! Short for Tcontainer_of_const
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        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
156
        ConstArray() { }
157
        //! construct from shape and representation
thomas.forbriger's avatar
thomas.forbriger committed
158
        ConstArray(const Tshape& shape, 
159
160
161
                   const Trepresentation& representation):
          Tshape(shape), Mrepresentation(representation)
          { check_consistency(); }
thomas.forbriger's avatar
thomas.forbriger committed
162
163
164
        //@}

        /*-----------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
165
      
thomas.forbriger's avatar
thomas.forbriger committed
166
167
168
169
        /*! \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
170
         * \ref sec_design_const), we provide const element access operators
thomas.forbriger's avatar
thomas.forbriger committed
171
172
173
174
         * 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
175
        //! full dimensionality access
176
        const T& operator()(const TIndexVec& index) const
177
        { return(Mrepresentation[offset(index)]); }
thomas.forbriger's avatar
thomas.forbriger committed
178
        //! access from 1 index value
179
        const T& operator()(const Tsubscript& i0) const
180
        { return(Mrepresentation[offset(i0)]); }
thomas.forbriger's avatar
thomas.forbriger committed
181
        //! access from 2 index values
182
        const T& operator()(const Tsubscript& i0,
thomas.forbriger's avatar
thomas.forbriger committed
183
                            const Tsubscript& i1) const
184
        { return(Mrepresentation[offset(i0, i1)]); }
thomas.forbriger's avatar
thomas.forbriger committed
185
        //! access from 3 index values
186
        const T& operator()(const Tsubscript& i0,
thomas.forbriger's avatar
thomas.forbriger committed
187
188
                            const Tsubscript& i1,
                            const Tsubscript& i2) const
189
        { return(Mrepresentation[offset(i0, i1, i2)]); }
thomas.forbriger's avatar
thomas.forbriger committed
190
        //! access from 4 index values
191
        const T& operator()(const Tsubscript& i0,
thomas.forbriger's avatar
thomas.forbriger committed
192
193
194
                            const Tsubscript& i1,
                            const Tsubscript& i2,
                            const Tsubscript& i3) const
195
        { return(Mrepresentation[offset(i0, i1, i2, i3)]); }
thomas.forbriger's avatar
thomas.forbriger committed
196
197
198
        //@}

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

thomas.forbriger's avatar
thomas.forbriger committed
200
201
202
        /*! \name Shape access
         */
        //@{
thomas.forbriger's avatar
thomas.forbriger committed
203
204
205
206
207
208
        //! 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
209
        //@}
thomas.forbriger's avatar
thomas.forbriger committed
210

thomas.forbriger's avatar
thomas.forbriger committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
        /*! \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.
         */
229
        Tcontainer copyout() const;
thomas.forbriger's avatar
thomas.forbriger committed
230
231
232
233
      
        //! \name access declarations
        //@{
        //! access to base class function
234
235
236
        using Tshape::first;
        using Tshape::last;
        using Tshape::size;
thomas.forbriger's avatar
thomas.forbriger committed
237
238
        //@}
     
thomas.forbriger's avatar
thomas.forbriger committed
239
240
241
242
243
        //! provide access to const shape
        const Tshape& shape() const
        { return(*this); }
     
        //! provide restricted access representation
thomas.forbriger's avatar
thomas.forbriger committed
244
        const Trepresentation& representation() const
245
        { return (Mrepresentation); }
246
247

      protected:
248
249
        //! provide access to shape offset functions for derived class
        using Tshape::offset;
250
          
251
252
253
254
255
256
      private:
        //! check consitency between representation and shape
        void check_consistency() const;
        
        //! representation member
        Trepresentation Mrepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270

    }; // 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
271
   * \sa \ref sec_design_replicated
thomas.forbriger's avatar
thomas.forbriger committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
   */
  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)
291
        typedef aff::ConstArray<T> Tbase;
thomas.forbriger's avatar
thomas.forbriger committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
        //! 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
316
        typedef aff::SharedHeap<T> Trepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
317
        //! Type of subscriptor
318
        typedef aff::Strided Tshape;
thomas.forbriger's avatar
thomas.forbriger committed
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
        //! 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)
347
        Array() { }
thomas.forbriger's avatar
thomas.forbriger committed
348
349
350
        //! construct from shape and representation
        Array(const Tshape& shape, 
              const Trepresentation& representation):
351
352
          Tbase(shape, representation),
          Mrepresentation(representation) { }
thomas.forbriger's avatar
thomas.forbriger committed
353
        //! construct from shape (defines size and layout)
354
355
356
357
358
359
360
        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
361
        explicit Array(const Tsize& s0, const Tsize& s1=1, 
362
363
364
365
366
367
                       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
368
369
370
371
372
373
374
        //@}

        /*-----------------------------------------------------------------*/
      
        //! \name access declarations
        //@{
        //! access to base class function
375
376
        using Tbase::operator();
        using Tbase::shape;
thomas.forbriger's avatar
thomas.forbriger committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
        //@}

        /*! \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.
         *
398
399
         * \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
400
401
         * \return itself
         */
402
403
        template<class C>
          Array& copyin(const C& a)
thomas.forbriger's avatar
thomas.forbriger committed
404
          {
405
            aff::util::deepcopy(a, *this);
thomas.forbriger's avatar
thomas.forbriger committed
406
407
            return(*this);
          }
408

thomas.forbriger's avatar
thomas.forbriger committed
409
410
411
        //! create a copy of this array
        Tcontainer copyout() const
        {
412
          Tcontainer copy(Tshape(this->first(),this->last()));
thomas.forbriger's avatar
thomas.forbriger committed
413
414
415
416
417
418
          copy.copyin(*this);
          return(copy);
        }
     
        //! return full access representation
        const Trepresentation& representation()
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
        { return (Mrepresentation); }

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

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

        //! full dimensionality access
        T& operator()(const TIndexVec& index) 
        { return(Mrepresentation[this->Tbase::offset(index)]); }
        //! access from 1 index value
        T& operator()(const Tsubscript& i0) 
        { return(Mrepresentation[this->Tbase::offset(i0)]); }
        //! access from 2 index values
        T& operator()(const Tsubscript& i0,
                      const Tsubscript& i1) 
        { return(Mrepresentation[this->Tbase::offset(i0, i1)]); }
        //! access from 3 index values
        T& operator()(const Tsubscript& i0,
                      const Tsubscript& i1,
                      const Tsubscript& i2) 
        { return(Mrepresentation[this->Tbase::offset(i0, i1, i2)]); }
        //! access from 4 index values
        T& operator()(const Tsubscript& i0,
                      const Tsubscript& i1,
                      const Tsubscript& i2,
                      const Tsubscript& i3) 
        { return(Mrepresentation[this->Tbase::offset(i0, i1, i2, i3)]); }
thomas.forbriger's avatar
thomas.forbriger committed
449
450

      private:
451
452
        //! my (mutable) data representation
        Trepresentation Mrepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
453
454
455

    }; // class Array

456
/*----------------------------------------------------------------------*/
457

458
459
#ifdef AFF_PREBUILT
} // namespace prebuilt
460
461
#endif

462
} // namespace aff
463

464
465
#ifndef AFF_NO_DEFINITIONS
#include <aff/array_def.h>
466
467
#endif

468
#endif // AFF_ARRAY_DEC_H_VERSION (includeguard)
469
470

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