File: disarray.h

package info (click to toggle)
rheolef 7.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 88,200 kB
  • sloc: cpp: 110,259; sh: 16,733; makefile: 5,406; python: 1,391; yacc: 218; javascript: 203; xml: 191; awk: 61; sed: 5
file content (808 lines) | stat: -rw-r--r-- 33,741 bytes parent folder | download | duplicates (3)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#ifndef _RHEO_DISARRAY_H
#define _RHEO_DISARRAY_H
//
// This file is part of Rheolef.
//
// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
//
// Rheolef 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.
//
// Rheolef 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 Rheolef; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// =========================================================================

namespace rheolef {
/**
@linalgclassfile disarray distributed container

Description
===========
This class provides a `std::vector` like
container for a distributed memory model array.

The `disarray` interface is similar to those of the `std::vector`
with the addition of some communication features 
for a distributed memory model.

Example
=======

        int main (int argc, char**argv) {
          environment distributed(argc, argv);
          distributor ownership (100);
          disarray<double> x (ownership, 3.14);
          dout << x << endl;
        }

Notation
========
There are two kind of indexes:

`dis_i`
>        This index refers to the complete array.
>        It is valid on all processes.
`i` 
>	 This index refers to the subset of the array
>        that is owned by the current process.
>        It is valid only on the local process.

Read and write accessors to the 
subset of the array that is owned by the current process
are still denoted by square brackets,
e.g. `value = x[i]` and `x[i] = value`, respectively.

Read and write accessors to
the complete array,
including array subsets that are owned by some others processors,
are introduced with the help of new member functions:

`x.dis_entry(dis_i) = value` 
>	write access at any location
`value = x.dis_at(dis_i)`
>	read access at any location

In order to optimize communications, they should be grouped.

Grouped writes
==============
Loop on any `dis_i` and write your value:

        for (...) { 
          x.dis_entry (dis_i) = value;
        }
 
Finally, perform all communications in one pass:
 
	x.dis_entry_assembly();

After this command, each value is stored in `x`,
at its right place, depending on `dis_i` and its `ownership`.
Note that, when `dis_i` is owned by the current process, 
the value is directly written as `x[i] = value`
and no communication are generated.

Grouped reads
=============
First, define the set of indexes that you want to access:

        std::set<size_t> dis_i_set; 

Then, loop on `dis_i` all these indexes and append it to this set:

        for (...) {
          dis_i_set.insert (dis_i);
        }

Next, perform communications:

        x.set_dis_indexes (dis_i_set);

After this command, each values associated to the `dis_i` index,
and that belongs to the index set, is also available also on the
current processor:
 
        for (...) {
          value = x.dis_at (dis_i);
        }

Note that, when `dis_i` is owned by the current process, 
the value is directly read as `value = x[i]`
and no communication are generated.

Template parameters
===================
Recall that the `std::class`
that takes two template parameters,
a `T` one for the stored elements and
a `A` one for the memory allocator,
the present `disarray` class
take three template parameters:

- `T`: the stored object type
- `M`: the memory model, i.e. `sequential` or `distributed`,
       by default `default_memory_model` with is 
       defined at the @ref configuration_page stage
- `A`: the memory allocator, by default `std::allocator`

Constructor
===========
In the `sequential` case,
the class interface provides a simplified constructor:

	int local_size = 100;
        disarray<double,sequential> x (local_size, init_val);
   
This declaration is similar to those of a `std::vector` one:
no communications are possible.
In order to enable communication, your have to replace the
`local_size` by information on how the array is distributed in memory:

          distributor ownership (100);
          disarray<double> x (ownership, 3.14);

The @ref distributor_4 class does this job.

Implementation
==============
@showfromfile
@snippet disarray.h verbatim_disarray
*/
} // namespace rheolef


/* 
Misc notes:
  "scatter" via "get_dis_entry".

  "gather" via "dis_entry(dis_i) = value"
  or "dis_entry(dis_i) += value". Note that += applies when T=idx_set where
  idx_set is a wrapper class of std::set<size_t> ; the += operator represents the
  union of a set. The operator= is used when T=double or others simple T types
  without algebra. If there is a conflict, i.e. several processes set the dis_i
  index, then the result of operator+= depends upon the order of the process at
  each run and is not deterministic. Such ambiguous behavior is not detected
  yet at run time.

*/

#include "rheolef/communicator.h"
#include "rheolef/distributor.h"
#include "rheolef/diststream.h"
#include "rheolef/heap_allocator.h"
#include "rheolef/msg_util.h"
#ifdef _RHEOLEF_HAVE_MPI
#include "rheolef/mpi_pair_datatype.h"
#endif // _RHEOLEF_HAVE_MPI

namespace rheolef {
/// @brief disarray element output helper
template <class T>
struct _disarray_put_element_type {
  std::ostream& operator() (std::ostream& os, const T& x) const { return os << x; }
};
template <class T>
struct _disarray_put_matlab_type {
  std::ostream& operator() (std::ostream& os, const T& x) const { return os << x << ";"; }
};
/// @brief disarray element input helper
template <class T>
struct _disarray_get_element_type {
  std::istream& operator() (std::istream& is, T& x) const { return is >> x; }
};
} // namespace rheolef
// -------------------------------------------------------------
// the sequential representation
// -------------------------------------------------------------
namespace rheolef {

template <class T, class M, class A> class disarray_rep {};

template <class T, class A>
class disarray_rep<T,sequential,A> : public std::vector<T> { // TODO: vector<T,A> for heap_alloc
public:
    typedef T                                         value_type;
    typedef A                                         allocator_type;
    typedef typename A::difference_type               difference_type;
    typedef std::vector<T>                            base; // TODO: vector<T,A> for heap_alloc
    typedef typename base::size_type                  size_type;
    typedef typename base::iterator                   iterator;
    typedef typename base::const_iterator             const_iterator;
    typedef typename base::const_reference            const_reference;
    typedef typename base::reference                  reference;
    typedef reference	                              dis_reference;
    typedef distributor::communicator_type            communicator_type;
    typedef sequential                                memory_type;

    explicit disarray_rep (const A& alloc = A());
    disarray_rep (const distributor& ownership, const T& init_val = T(), const A& alloc = A());
    void resize   (const distributor& ownership, const T& init_val = T());
    disarray_rep (size_type loc_size = 0,       const T& init_val = T(), const A& alloc = A());
    void resize   (size_type loc_size = 0,       const T& init_val = T());
    disarray_rep (const disarray_rep<T,sequential,A>& x);

    A get_allocator() const { return base::get_allocator(); }
    size_type size() const { return base::size(); }
    iterator begin() { return base::begin(); }
    const_iterator begin() const { return base::begin(); }
    iterator end() { return base::end(); }
    const_iterator end() const { return base::end(); }
    const distributor& ownership() const { return _ownership; }

    reference       operator[] (size_type i)       { return base::operator[] (i); }
    const_reference operator[] (size_type i) const { return base::operator[] (i); }
    const_reference dis_at (size_type dis_i) const { return operator[] (dis_i); }
   
    size_type dis_size () const { return base::size(); }
    size_type first_index () const { return 0; }
    size_type last_index () const { return base::size(); }
    reference dis_entry     (size_type dis_i) { return operator[](dis_i); }
    void get_dis_indexes (std::set<size_type>& ext_idx_set) const;
    void reset_dis_indexes() const {}
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly_begin (SetOp = SetOp()) {}
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly_end (SetOp = SetOp()) {}
    void repartition (					      // old_numbering for *this
        const disarray_rep<size_type,sequential,A>& partition,	      // old_ownership
        disarray_rep<T,sequential,A>&               new_disarray,	      // new_ownership (created)
        disarray_rep<size_type,sequential,A>&       old_numbering,      // new_ownership
        disarray_rep<size_type,sequential,A>&       new_numbering) const // old_ownership
    {
	error_macro ("not yet");
    }
    template<class A2>
    void reverse_permutation (                                // old_ownership for *this=iold2dis_inew
        disarray_rep<size_type,sequential,A2>& inew2dis_iold) const;    // new_ownership

    idiststream& get_values (idiststream& s);
    odiststream& put_values (odiststream& s) const;
    odiststream& put_matlab (odiststream& s) const;
    template <class GetFunction> idiststream& get_values (idiststream& ips, GetFunction get_element);
    template <class PutFunction> odiststream& put_values (odiststream& ops, PutFunction put_element) const;
    void dump (std::string name) const;
protected:
// data:
    distributor      _ownership;
};
// -------------------------------------------------------------
// the distributed representation
// -------------------------------------------------------------
#ifdef _RHEOLEF_HAVE_MPI
template <class T, class A> struct disarray_dis_reference; /// forward decl

template <class T, class A>
class disarray_rep<T,distributed,A> : public disarray_rep<T,sequential,A> {
public:

// typedefs:

    using base = disarray_rep<T,sequential,A>;
    using size_type       = typename base::size_type;
    using value_type      = typename base::value_type;
    using memory_type     = distributed;
    using iterator        = typename base::iterator;
    using const_iterator  = typename base::const_iterator;
    using reference       = typename base::reference;
    using const_reference = typename base::const_reference;
    using difference_type = typename base::difference_type;
    using communicator_type = distributor::communicator_type;
    using scatter_map_type  = std::map <size_type, T>; // TODO: map<T,A> for heap_alloc
    using dis_reference   = disarray_dis_reference<T,A>;

// allocators:

    disarray_rep (const distributor& ownership, const T&  init_val = T(), const A& alloc = A());
    void resize   (const distributor& ownership, const T&  init_val = T());
    disarray_rep (const disarray_rep<T,distributed,A>& x);

    A get_allocator() const        { return base::get_allocator(); }
    size_type size() const         { return base::size(); }
    const_iterator begin() const   { return base::begin(); }
    const_iterator end() const     { return base::end(); }
    iterator begin()               { return base::begin(); }
    iterator end()                 { return base::end(); }

    const distributor& ownership() const  { return base::_ownership; }
    const mpi::communicator& comm() const { return ownership().comm(); }
    size_type first_index () const        { return ownership().first_index(); }
    size_type last_index () const         { return ownership().last_index(); }
    size_type dis_size () const           { return ownership().dis_size(); }

    dis_reference dis_entry (size_type dis_i) { return dis_reference (*this, dis_i); }

    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly_begin (SetOp my_set_op = SetOp());
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly_end   (SetOp my_set_op = SetOp());
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly       (SetOp my_set_op = SetOp())
   		{ dis_entry_assembly_begin (my_set_op); dis_entry_assembly_end (my_set_op); }

    template<class Set, class Map>
    void append_dis_entry (const Set& ext_idx_set, Map& ext_idx_map) const;

    template<class Set, class Map>
    void get_dis_entry (const Set& ext_idx_set, Map& ext_idx_map) const {
    	    ext_idx_map.clear();
    	    append_dis_entry (ext_idx_set, ext_idx_map);
	}

    template<class Set>
    void append_dis_indexes (const Set& ext_idx_set) const { append_dis_entry (ext_idx_set, _ext_x); }

    template<class Set>
    void set_dis_indexes    (const Set& ext_idx_set) const { get_dis_entry    (ext_idx_set, _ext_x); }
    void get_dis_indexes (std::set<size_type>& ext_idx_set) const;
    void reset_dis_indexes() const;

    const_reference dis_at (size_type dis_i) const;

    // get all external pairs (dis_i, values):
    const scatter_map_type&    get_dis_map_entries() const { return _ext_x; }

    template<class A2>
    void repartition (					      // old_numbering for *this
        const disarray_rep<size_type,distributed,A2>& partition,	      // old_ownership
        disarray_rep<T,distributed,A>&                new_disarray,	      // new_ownership (created)
        disarray_rep<size_type,distributed,A2>&       old_numbering,     // new_ownership
        disarray_rep<size_type,distributed,A2>&       new_numbering) const; // old_ownership

    template<class A2>
    void permutation_apply (		 		       // old_numbering for *this
        const disarray_rep<size_type,distributed,A2>& new_numbering,      // old_ownership
        disarray_rep<T,distributed,A>&                new_disarray) const;   // new_ownership (already allocated)

    template<class A2>
    void reverse_permutation (                                // old_ownership for *this=iold2dis_inew
        disarray_rep<size_type,distributed,A2>& inew2dis_iold) const;    // new_ownership

    idiststream& get_values (idiststream& s);
    odiststream& put_values (odiststream& s) const;
    odiststream& put_matlab (odiststream& s) const;
    template <class GetFunction> idiststream& get_values (idiststream& ips, GetFunction get_element);
    template <class PutFunction> odiststream& put_values (odiststream& ops, PutFunction put_element) const;
    template <class PutFunction, class A2> odiststream& permuted_put_values (odiststream& ops, const disarray_rep<size_type,distributed,A2>& perm,
		PutFunction put_element) const;
    void dump (std::string name) const;
//protected:
    template<class U, class SetOp>
    void set_dis_entry       (size_type dis_i, const U& val, const SetOp& set_op);
#ifdef TO_CLEAN
    template<class U, class SetOp>
    void set_plus_dis_entry  (size_type dis_i, const U& val, const SetOp& set_op);
    template<class U, class SetOp>
    void set_minus_dis_entry (size_type dis_i, const U& val, const SetOp& set_op);
#endif // TO_CLEAN
// typedefs:
    /** 1) stash: store data before assembly() communications:
      *   select multimap<U> when T=set<U> and map<T> otherwise
      */
    template<class Pair>
    struct remove_const_in_pair {
        typedef Pair type;
    };
    template<class T1, class T2>
    struct remove_const_in_pair<std::pair<T1,T2> > {
        typedef std::pair<typename std::decay<T1>::type,
                          typename std::decay<T2>::type> type;
    };
    template<class U, class IsContainer> struct stash_traits {};
    template<class U>
    struct stash_traits<U,std::false_type> {
        typedef U mapped_type;
        typedef std::map <size_type, U>   map_type; // TODO: map<T,A> for heap_alloc
    };
    template<class U>
    struct stash_traits<U,std::true_type> {
        typedef typename remove_const_in_pair<typename U::value_type>::type mapped_type;
        typedef std::multimap <size_type, mapped_type>   map_type; // TODO: map<T,A> for heap_alloc
    };
    typedef typename details::is_container_of_mpi_datatype<T>::type     is_container;
    typedef typename stash_traits<T,is_container>::mapped_type stash_value;
    typedef typename stash_traits<T,is_container>::map_type    stash_map_type;

    /** 2) message: for communication during assembly_begin(), assembly_end()
      */
    struct message_type {
        std::list<std::pair<size_type,mpi::request> >    waits; // TODO: list<T,A> for heap_alloc
        std::vector<std::pair<size_type,stash_value> >   data;  // TODO: vector<T,A> for heap_alloc
        message_type() : waits(), data() {}
    };
    /** 3) scatter (get_entry): specialized versions for T=container and T=simple type
      */
    template<class Set, class Map>
    void append_dis_entry (const Set& ext_idx_set, Map& ext_idx_map, std::true_type) const;
    template<class Set, class Map>
    void append_dis_entry (const Set& ext_idx_set, Map& ext_idx_map, std::false_type) const;

// data:
    stash_map_type   _stash;		// for assembly msgs:
    message_type     _send;
    message_type     _receive;
    size_type        _receive_max_size;
    mutable scatter_map_type _ext_x;		// for ext values (scatter)
};
// custom reference that interacts with stach
// for ext values on others partitions
template <class T, class A>
struct disarray_dis_reference
{
  using size_type = typename disarray_rep<T,distributed,A>::size_type;

  disarray_dis_reference (disarray_rep<T,distributed,A>& x, size_type dis_i)
    : _x(x), _dis_i(dis_i) {}

#ifndef TO_CLEAN // not usefull: default behavior
  disarray_dis_reference (const disarray_dis_reference<T,A>& r)
    : _x(r._x), _dis_i(r._dis_i) {}

  disarray_dis_reference<T,A>&
  operator= (const disarray_dis_reference<T,A>& r) {
    _x = r._x; _dis_i = r._dis_i; return *this; }
#endif // TO_CLEAN

  disarray_dis_reference<T,A>&
  operator= (const T& value) {
    trace_macro("_x.set_dis_entry (dis_i="<<_dis_i<<", value="<<value<<", op=)");
    _x.set_dis_entry (_dis_i, value, details::generic_set_op());
    return *this;
  }
  template<class U>
  disarray_dis_reference<T,A>&
  operator+= (const U& value) {
    trace_macro("_x.set_dis_entry (dis_i="<<_dis_i<<", value="<<value<<", op+=)");
    _x.set_dis_entry (_dis_i, value, details::generic_set_plus_op());
    return *this;
  }
  template<class U>
  disarray_dis_reference<T,A>&
  operator-= (const U& value) {
    trace_macro("_x.set_dis_entry (dis_i="<<_dis_i<<", value="<<value<<", op-=)");
    _x.set_dis_entry (_dis_i, value, details::generic_set_minus_op());
    return *this;
  }
// data:
protected:
  disarray_rep<T,distributed,A>& _x;
  size_type                      _dis_i;
};

namespace details {
template <class T, class A>
struct is_class_reference<disarray_dis_reference<T,A>> : std::true_type {};
} // namespace details

#endif // _RHEOLEF_HAVE_MPI
// -------------------------------------------------------------
// the basic class with a smart pointer to representation
// the user-level class with memory-model parameter
// -------------------------------------------------------------
template <class T, class M = rheo_default_memory_model, class A = std::allocator<T> >
class disarray {
public:
    typedef M memory_type;
    typedef disarray_rep<T,sequential,A>  	      rep;
    typedef typename rep::base::size_type      size_type;
    typedef typename rep::base::iterator       iterator;
    typedef typename rep::base::const_iterator const_iterator;
};
template <class T, class A>
class disarray<T,sequential,A> : public smart_pointer<disarray_rep<T,sequential,A> > {
public:

// typedefs:

    typedef disarray_rep<T,sequential,A>  	  rep;
    typedef smart_pointer<rep> 		  base;

    typedef sequential 			  memory_type;
    typedef typename rep::size_type 	  size_type;
    typedef typename rep::difference_type difference_type;
    typedef typename rep::value_type 	  value_type;
    typedef typename rep::reference 	  reference;
    typedef typename rep::dis_reference   dis_reference;
    typedef typename rep::iterator 	  iterator;
    typedef typename rep::const_reference const_reference;
    typedef typename rep::const_iterator  const_iterator;

// allocators:


    disarray       (size_type loc_size = 0,       const T& init_val = T(), const A& alloc = A());
    void resize (size_type loc_size = 0,       const T& init_val = T());
    disarray       (const distributor& ownership, const T& init_val = T(), const A& alloc = A());
    void resize (const distributor& ownership, const T& init_val = T());

// local accessors & modifiers:

    A get_allocator() const              { return base::data().get_allocator(); }
    size_type     size () const          { return base::data().size(); }
    size_type dis_size () const          { return base::data().dis_size(); }
    const distributor& ownership() const { return base::data().ownership(); }
    const communicator& comm() const     { return ownership().comm(); }

    reference       operator[] (size_type i)       { return base::data().operator[] (i); }
    const_reference operator[] (size_type i) const { return base::data().operator[] (i); }
    reference       operator() (size_type i)       { return base::data().operator[] (i); }
    const_reference operator() (size_type i) const { return base::data().operator[] (i); }
    const_reference dis_at (size_type dis_i) const { return operator[] (dis_i); }

          iterator begin()       { return base::data().begin(); }
    const_iterator begin() const { return base::data().begin(); }
          iterator end()         { return base::data().end(); }
    const_iterator end() const   { return base::data().end(); }

// global modifiers (for compatibility with distributed interface):

    dis_reference dis_entry (size_type dis_i) { return base::data().dis_entry(dis_i); }
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly (SetOp my_set_op = SetOp()) {}
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly_begin (SetOp my_set_op = SetOp()) {}
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly_end (SetOp my_set_op = SetOp()) {}

    void dis_entry_assembly_begin() {}
    void dis_entry_assembly_end()   {}
    void dis_entry_assembly()       {}

    void get_dis_indexes (std::set<size_type>& ext_idx_set) const { base::data().get_dis_indexes (ext_idx_set); }
    void reset_dis_indexes() const {}
    template<class Set> void set_dis_indexes    (const Set& ext_idx_set) const {}
    template<class Set> void append_dis_indexes (const Set& ext_idx_set) const {}
    template<class Set, class Map> void append_dis_entry (const Set& ext_idx_set, Map& ext_idx_map) const {}
    template<class Set, class Map> void get_dis_entry    (const Set& ext_idx_set, Map& ext_idx_map) const {}

// apply a partition:
 
    template<class RepSize>
    void repartition (			             // old_numbering for *this
        const RepSize&         partition,	     // old_ownership
        disarray<T,sequential,A>& new_disarray,	     // new_ownership (created)
        RepSize&               old_numbering,	     // new_ownership
        RepSize&               new_numbering) const  // old_ownership
        { return base::data().repartition (partition, new_disarray, old_numbering, new_numbering); }

    template<class RepSize>
    void permutation_apply (                       // old_numbering for *this
        const RepSize&          new_numbering,     // old_ownership
        disarray<T,sequential,A>&  new_disarray) const   // new_ownership (already allocated)
        { return base::data().permutation_apply (new_numbering, new_disarray); }

    void reverse_permutation (                                 // old_ownership for *this=iold2dis_inew
        disarray<size_type,sequential,A>& inew2dis_iold) const   // new_ownership
        { base::data().reverse_permutation (inew2dis_iold.data()); }

// i/o:

    odiststream& put_values (odiststream& ops) const { return base::data().put_values(ops); }
    idiststream& get_values (idiststream& ips)       { return base::data().get_values(ips); }
    template <class GetFunction>
    idiststream& get_values (idiststream& ips, GetFunction get_element)       { return base::data().get_values(ips, get_element); }
    template <class PutFunction>
    odiststream& put_values (odiststream& ops, PutFunction put_element) const { return base::data().put_values(ops, put_element); }
    void dump (std::string name) const { return base::data().dump(name); }
};
template <class T, class A>
inline
disarray<T,sequential,A>::disarray (
    	size_type loc_size,
	const T&  init_val,
        const A&  alloc)
 : base(new_macro(rep(loc_size,init_val,alloc)))
{
}
template <class T, class A>
inline
disarray<T,sequential,A>::disarray (
    	const distributor& ownership,
	const T&           init_val,
        const A&           alloc)
 : base(new_macro(rep(ownership,init_val,alloc)))
{
}
template <class T, class A>
inline
void
disarray<T,sequential,A>::resize (
    	size_type loc_size,
	const T&  init_val)
{
  base::data().resize (loc_size,init_val);
}
template <class T, class A>
inline
void
disarray<T,sequential,A>::resize (
    	const distributor& ownership,
	const T&           init_val)
{
  base::data().resize (ownership,init_val);
}
#ifdef _RHEOLEF_HAVE_MPI
// [verbatim_disarray]
template <class T, class A>
class disarray<T,distributed,A> : public smart_pointer<disarray_rep<T,distributed,A> > {
public:

// typedefs:

    typedef disarray_rep<T,distributed,A> rep;
    typedef smart_pointer<rep> 		  base;

    typedef distributed 		  memory_type;
    typedef typename rep::size_type 	  size_type;
    typedef typename rep::difference_type difference_type;
    typedef typename rep::value_type 	  value_type;
    typedef typename rep::reference 	  reference;
    typedef typename rep::dis_reference   dis_reference;
    typedef typename rep::iterator 	  iterator;
    typedef typename rep::const_reference const_reference;
    typedef typename rep::const_iterator  const_iterator;
    typedef typename rep::scatter_map_type scatter_map_type;

// allocators:

    disarray       (const distributor& ownership = distributor(), const T& init_val = T(), const A& alloc = A());
    void resize (const distributor& ownership = distributor(), const T& init_val = T());

// local accessors & modifiers:

    A get_allocator() const              { return base::data().get_allocator(); }
    size_type     size () const          { return base::data().size(); }
    size_type dis_size () const          { return base::data().dis_size(); }
    const distributor& ownership() const { return base::data().ownership(); }
    const communicator& comm() const     { return base::data().comm(); }

    reference       operator[] (size_type i)       { return base::data().operator[] (i); }
    const_reference operator[] (size_type i) const { return base::data().operator[] (i); }
    reference       operator() (size_type i)       { return base::data().operator[] (i); }
    const_reference operator() (size_type i) const { return base::data().operator[] (i); }

          iterator begin()       { return base::data().begin(); }
    const_iterator begin() const { return base::data().begin(); }
          iterator end()         { return base::data().end(); }
    const_iterator end() const   { return base::data().end(); }

// global accessor:

    template<class Set, class Map>
    void append_dis_entry (const Set& ext_idx_set, Map& ext_idx_map) const { base::data().append_dis_entry (ext_idx_set, ext_idx_map); }

    template<class Set, class Map>
    void get_dis_entry    (const Set& ext_idx_set, Map& ext_idx_map) const { base::data().get_dis_entry (ext_idx_set, ext_idx_map); }

    template<class Set>
    void append_dis_indexes (const Set& ext_idx_set) const { base::data().append_dis_indexes (ext_idx_set); }
    void reset_dis_indexes() const { base::data().reset_dis_indexes(); }
    void get_dis_indexes (std::set<size_type>& ext_idx_set) const { base::data().get_dis_indexes (ext_idx_set); }

    template<class Set>
    void set_dis_indexes    (const Set& ext_idx_set) const { base::data().set_dis_indexes (ext_idx_set); }

    const T& dis_at (size_type dis_i) const { return base::data().dis_at (dis_i); }

    // get all external pairs (dis_i, values):
    const scatter_map_type& get_dis_map_entries() const { return base::data().get_dis_map_entries(); }

// global modifiers (for compatibility with distributed interface):

    dis_reference dis_entry (size_type dis_i) { return base::data().dis_entry(dis_i); }

    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly_begin (SetOp my_set_op = SetOp()) { base::data().dis_entry_assembly_begin (my_set_op); }
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly_end   (SetOp my_set_op = SetOp()) { base::data().dis_entry_assembly_end   (my_set_op); }
    template<class SetOp = typename details::default_set_op_traits<T>::type>
    void dis_entry_assembly       (SetOp my_set_op = SetOp()) { base::data().dis_entry_assembly       (my_set_op); }

    void dis_entry_assembly_begin() { base::data().template dis_entry_assembly_begin<typename details::default_set_op_traits<T>::type>(); }
    void dis_entry_assembly_end()   { base::data().template dis_entry_assembly_end<typename details::default_set_op_traits<T>::type>(); }
    void dis_entry_assembly()       { dis_entry_assembly_begin(); dis_entry_assembly_end(); }

// apply a partition:
 
    template<class RepSize>
    void repartition (			            // old_numbering for *this
        const RepSize&        partition,            // old_ownership
        disarray<T,distributed>& new_disarray,            // new_ownership (created)
        RepSize&              old_numbering,        // new_ownership
        RepSize&              new_numbering) const  // old_ownership
        { return base::data().repartition (partition.data(), new_disarray.data(), old_numbering.data(), new_numbering.data()); }

    template<class RepSize>
    void permutation_apply (                       // old_numbering for *this
        const RepSize&          new_numbering,     // old_ownership
        disarray<T,distributed,A>& new_disarray) const   // new_ownership (already allocated)
        { base::data().permutation_apply (new_numbering.data(), new_disarray.data()); }

    void reverse_permutation (                                 // old_ownership for *this=iold2dis_inew
        disarray<size_type,distributed,A>& inew2dis_iold) const   // new_ownership
        { base::data().reverse_permutation (inew2dis_iold.data()); }

// i/o:

    odiststream& put_values (odiststream& ops) const { return base::data().put_values(ops); }
    idiststream& get_values (idiststream& ips)       { return base::data().get_values(ips); }
    void dump (std::string name) const 	    { return base::data().dump(name); }

    template <class GetFunction>
    idiststream& get_values (idiststream& ips, GetFunction get_element)       { return base::data().get_values(ips, get_element); }
    template <class PutFunction>
    odiststream& put_values (odiststream& ops, PutFunction put_element) const { return base::data().put_values(ops, put_element); }
    template <class PutFunction, class A2> odiststream& permuted_put_values (
		odiststream& ops, const disarray<size_type,distributed,A2>& perm, PutFunction put_element) const 
								     { return base::data().permuted_put_values (ops, perm.data(), put_element); }
};
// [verbatim_disarray]
template <class T, class A>
inline
disarray<T,distributed,A>::disarray (
    	const distributor& ownership,
	const T&           init_val,
        const A&           alloc)
 : base(new_macro(rep(ownership,init_val,alloc)))
{
}
template <class T, class A>
inline
void
disarray<T,distributed,A>::resize (
    	const distributor& ownership,
	const T         &  init_val)
{
  base::data().resize (ownership,init_val);
}
#endif // _RHEOLEF_HAVE_MPI

// -------------------------------------------------------------
// i/o with operator<< & >>
// -------------------------------------------------------------
template <class T, class A>
inline
idiststream&
operator >> (idiststream& ips,  disarray<T,sequential,A>& x)
{ 
    return x.get_values(ips); 
}
template <class T, class A>
inline
odiststream&
operator << (odiststream& ops, const disarray<T,sequential,A>& x)
{
    return x.put_values(ops);
}
#ifdef _RHEOLEF_HAVE_MPI
template <class T, class A>
inline
idiststream&
operator >> (idiststream& ips,  disarray<T,distributed,A>& x)
{ 
    return x.get_values(ips); 
}
template <class T, class A>
inline
odiststream&
operator << (odiststream& ops, const disarray<T,distributed,A>& x)
{
    return x.put_values(ops);
}
#endif // _RHEOLEF_HAVE_MPI
} // namespace rheolef

// -------------------------------------------------------------
// not inlined : longer code
// -------------------------------------------------------------
#include "rheolef/disarray_seq.icc"
#include "rheolef/disarray_mpi.icc"
#endif // _RHEO_DISARRAY_H