Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Epetra_DistObject.cpp
Go to the documentation of this file.
1 
2 //@HEADER
3 // ************************************************************************
4 //
5 // Epetra: Linear Algebra Services Package
6 // Copyright 2011 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 
43 #include "Epetra_DistObject.h"
44 #include "Epetra_Comm.h"
45 #include "Epetra_Import.h"
46 #include "Epetra_Export.h"
47 #include "Epetra_Distributor.h"
48 
49 
50 
51 //=============================================================================
52 // Constructor
53 
55  : Epetra_Object("Epetra::DistObject"),
56  Map_(map),
57  Comm_(&Map_.Comm()),
58  Exports_(0),
59  Imports_(0),
60  LenExports_(0),
61  LenImports_(0),
62  Sizes_(0)
63 {}
64 
65 //=============================================================================
66 // Constructor (label given)
67 
68 Epetra_DistObject::Epetra_DistObject(const Epetra_BlockMap& map, const char* const label)
69  : Epetra_Object(label),
70  Map_(map),
71  Comm_(&Map_.Comm()),
72  Exports_(0),
73  Imports_(0),
74  LenExports_(0),
75  LenImports_(0),
76  Sizes_(0)
77 {}
78 
79 //=============================================================================
80 // Copy Constructor
81 
83  : Epetra_Object(Source),
84  Map_(Source.Map_),
85  Comm_(&Map_.Comm()),
86  Exports_(0),
87  Imports_(0),
88  LenExports_(0),
89  LenImports_(0),
90  Sizes_(0)
91 {}
92 
93 //=============================================================================
95 
96 
97  if (LenExports_!=0) {
98  delete[] Exports_;
99  Exports_ = 0;
100  LenExports_ = 0;
101  }
102  if (LenImports_!=0) {
103  delete[] Imports_;
104  Imports_ = 0;
105  LenImports_ = 0;
106  }
107 
108  if (Sizes_!=0) delete [] Sizes_;
109  Sizes_ = 0;
110 }
111 
112 //=============================================================================
114  const Epetra_Import& Importer,
115  Epetra_CombineMode CombineMode,
116  const Epetra_OffsetIndex * Indexor)
117 {
118 
119 #ifdef HAVE_EPETRA_DEBUG
120  // Only perform the expensive map comparisons in the debug version
121  if (!Map_.SameAs(Importer.TargetMap())) EPETRA_CHK_ERR(-2);
122  if (!A.Map().SameAs(Importer.SourceMap())) EPETRA_CHK_ERR(-3);
123 #endif
124 
125  int NumSameIDs = Importer.NumSameIDs();
126  int NumPermuteIDs = Importer.NumPermuteIDs();
127  int NumRemoteIDs = Importer.NumRemoteIDs();
128  int NumExportIDs = Importer.NumExportIDs();
129  int* ExportLIDs = Importer.ExportLIDs();
130  int* RemoteLIDs = Importer.RemoteLIDs();
131  int* PermuteToLIDs = Importer.PermuteToLIDs();
132  int* PermuteFromLIDs = Importer.PermuteFromLIDs();
133 
134  EPETRA_CHK_ERR(DoTransfer(A, CombineMode, NumSameIDs, NumPermuteIDs, NumRemoteIDs, NumExportIDs,
135  PermuteToLIDs, PermuteFromLIDs, RemoteLIDs, ExportLIDs,
137  false, Indexor));
138  return(0);
139 }
140 
141 //=============================================================================
143  const Epetra_Export& Exporter,
144  Epetra_CombineMode CombineMode,
145  const Epetra_OffsetIndex * Indexor)
146 {
147 #ifdef HAVE_EPETRA_DEBUG
148  // Only perform the expensive map comparisons in the debug version
149  if (!Map_.SameAs(Exporter.TargetMap())) EPETRA_CHK_ERR(-2);
150  if (!A.Map().SameAs(Exporter.SourceMap())) EPETRA_CHK_ERR(-3);
151 #endif
152 
153  int NumSameIDs = Exporter.NumSameIDs();
154  int NumPermuteIDs = Exporter.NumPermuteIDs();
155  int NumRemoteIDs = Exporter.NumRemoteIDs();
156  int NumExportIDs = Exporter.NumExportIDs();
157  int* ExportLIDs = Exporter.ExportLIDs();
158  int* RemoteLIDs = Exporter.RemoteLIDs();
159  int* PermuteToLIDs = Exporter.PermuteToLIDs();
160  int* PermuteFromLIDs = Exporter.PermuteFromLIDs();
161 
162  EPETRA_CHK_ERR(DoTransfer(A, CombineMode, NumSameIDs, NumPermuteIDs, NumRemoteIDs, NumExportIDs,
163  PermuteToLIDs, PermuteFromLIDs, RemoteLIDs, ExportLIDs,
165  false, Indexor));
166  return(0);
167 }
168 
169 //=============================================================================
171  const Epetra_Export& Exporter,
172  Epetra_CombineMode CombineMode,
173  const Epetra_OffsetIndex * Indexor)
174 {
175 #ifdef HAVE_EPETRA_DEBUG
176  // Only perform the expensive map comparisons in the debug version
177  if (!Map_.SameAs(Exporter.SourceMap())) EPETRA_CHK_ERR(-2);
178  if (!A.Map().SameAs(Exporter.TargetMap())) EPETRA_CHK_ERR(-3);
179 #endif
180 
181  int NumSameIDs = Exporter.NumSameIDs();
182  int NumPermuteIDs = Exporter.NumPermuteIDs();
183  int NumRemoteIDs = Exporter.NumExportIDs();
184  int NumExportIDs = Exporter.NumRemoteIDs();
185  int* ExportLIDs = Exporter.RemoteLIDs();
186  int* RemoteLIDs = Exporter.ExportLIDs();
187  int* PermuteToLIDs = Exporter.PermuteFromLIDs();
188  int* PermuteFromLIDs = Exporter.PermuteToLIDs();
189 
190  EPETRA_CHK_ERR(DoTransfer(A, CombineMode, NumSameIDs, NumPermuteIDs, NumRemoteIDs, NumExportIDs,
191  PermuteToLIDs, PermuteFromLIDs, RemoteLIDs, ExportLIDs,
193  true, Indexor));
194  return(0);
195 }
196 
197 //=============================================================================
199  const Epetra_Import& Importer,
200  Epetra_CombineMode CombineMode,
201  const Epetra_OffsetIndex * Indexor)
202 {
203 #ifdef HAVE_EPETRA_DEBUG
204  // Only perform the expensive map comparisons in the debug version
205  if (!Map_.SameAs(Importer.SourceMap())) EPETRA_CHK_ERR(-2);
206  if (!A.Map().SameAs(Importer.TargetMap())) EPETRA_CHK_ERR(-3);
207 #endif
208 
209  int NumSameIDs = Importer.NumSameIDs();
210  int NumPermuteIDs = Importer.NumPermuteIDs();
211  int NumRemoteIDs = Importer.NumExportIDs();
212  int NumExportIDs = Importer.NumRemoteIDs();
213  int* ExportLIDs = Importer.RemoteLIDs();
214  int* RemoteLIDs = Importer.ExportLIDs();
215  int* PermuteToLIDs = Importer.PermuteFromLIDs();
216  int* PermuteFromLIDs = Importer.PermuteToLIDs();
217 
218  EPETRA_CHK_ERR(DoTransfer(A, CombineMode, NumSameIDs, NumPermuteIDs, NumRemoteIDs, NumExportIDs,
219  PermuteToLIDs, PermuteFromLIDs, RemoteLIDs, ExportLIDs,
221  Importer.Distributor(), true, Indexor));
222  return(0);
223 }
224 
225 //=============================================================================
227  Epetra_CombineMode CombineMode,
228  int NumSameIDs,
229  int NumPermuteIDs,
230  int NumRemoteIDs,
231  int NumExportIDs,
232  int* PermuteToLIDs,
233  int* PermuteFromLIDs,
234  int* RemoteLIDs,
235  int* ExportLIDs,
236  int& LenExports,
237  char*& Exports,
238  int& LenImports,
239  char*& Imports,
240  Epetra_Distributor& Distor,
241  bool DoReverse,
242  const Epetra_OffsetIndex * Indexor)
243 {
244 
246 
247  if (NumSameIDs + NumPermuteIDs > 0) {
248  EPETRA_CHK_ERR(CopyAndPermute(A, NumSameIDs, NumPermuteIDs, PermuteToLIDs, PermuteFromLIDs,Indexor, CombineMode));
249  }
250 
251  // Once CopyAndPermute is done, switch to Add so rest works as before.
252  if(CombineMode == Epetra_AddLocalAlso) {
253  CombineMode = Add;
254  }
255 
256  if (CombineMode==Zero)
257  return(0); // All done if CombineMode only involves copying and permuting
258 
259  int SizeOfPacket;
260  bool VarSizes = false;
261  if( NumExportIDs > 0) {
262  delete [] Sizes_;
263  Sizes_ = new int[NumExportIDs];
264  }
265  EPETRA_CHK_ERR(PackAndPrepare(A, NumExportIDs, ExportLIDs,
266  LenExports, Exports, SizeOfPacket, Sizes_, VarSizes, Distor));
267 
268  if ((Map_.DistributedGlobal() && DoReverse) || (A.Map().DistributedGlobal() && !DoReverse)) {
269  if (DoReverse) {
270  // Do the exchange of remote data
271  if( VarSizes ) {
272  EPETRA_CHK_ERR(Distor.DoReverse(Exports, SizeOfPacket, Sizes_, LenImports, Imports));
273  }
274  else {
275  EPETRA_CHK_ERR(Distor.DoReverse(Exports, SizeOfPacket, LenImports, Imports));
276  }
277  }
278  else {
279  // Do the exchange of remote data
280  if( VarSizes ) {
281  EPETRA_CHK_ERR(Distor.Do(Exports, SizeOfPacket, Sizes_, LenImports, Imports));
282  }
283  else {
284  EPETRA_CHK_ERR(Distor.Do(Exports, SizeOfPacket, LenImports, Imports));
285  }
286  }
287  EPETRA_CHK_ERR(UnpackAndCombine(A, NumRemoteIDs, RemoteLIDs, LenImports, Imports, SizeOfPacket, Distor, CombineMode, Indexor));
288  }
289 
290  return(0);
291 }
292 
293 //=============================================================================
294 void Epetra_DistObject::Print(std::ostream& os) const {
295  int MyPID = Comm().MyPID();
296  int NumProc = Comm().NumProc();
297 
298  for (int iproc=0; iproc < NumProc; iproc++) {
299  if (MyPID==iproc) {
300  Comm().PrintInfo(os);
301  os << "Length of Export buffer (in chars) = " << LenExports_ << std::endl;
302  os << "Length of Import buffer (in chars) = " << LenImports_ << std::endl;
303  os << std::flush;
304  }
305  }
306  return;
307 }
308 
309 //------------------------------------------------------------------------------
311 {
312  (void)src;
313  //not currently supported
314  bool throw_error = true;
315  if (throw_error) {
316  throw ReportError("Epetra_DistObject::operator= is not supported.",-1);
317  }
318 
319  return(*this);
320 }
bool DistributedGlobal() const
Returns true if map is defined across more than one processor.
int * ExportLIDs() const
List of elements that will be sent to other processors.
int NumRemoteIDs() const
Returns the number of elements that are not on the calling processor.
const Epetra_Comm & Comm() const
Returns the address of the Epetra_Comm for this multi-vector.
Epetra_Distributor: The Epetra Gather/Scatter Setup Base Class.
virtual int CopyAndPermute(const Epetra_SrcDistObject &Source, int NumSameIDs, int NumPermuteIDs, int *PermuteToLIDs, int *PermuteFromLIDs, const Epetra_OffsetIndex *Indexor, Epetra_CombineMode CombineMode=Zero)=0
Perform ID copies and permutations that are on processor.
bool SameAs(const Epetra_BlockMap &Map) const
Returns true if this and Map are identical maps.
int NumSameIDs() const
Returns the number of elements that are identical between the source and target maps, up to the first different ID.
virtual int DoReverse(char *export_objs, int obj_size, int &len_import_objs, char *&import_objs)=0
Execute reverse of plan on buffer of export objects in a single step.
Epetra_OffsetIndex: This class builds index for efficient mapping of data from one Epetra_CrsGraph ba...
const Epetra_BlockMap & TargetMap() const
Returns the TargetMap used to construct this exporter.
const Epetra_BlockMap & SourceMap() const
Returns the SourceMap used to construct this exporter.
int NumPermuteIDs() const
Returns the number of elements that are local to the calling processor, but not part of the first Num...
virtual void Print(std::ostream &os) const
Print method.
Epetra_Distributor & Distributor() const
int NumSameIDs() const
Returns the number of elements that are identical between the source and target maps, up to the first different ID.
#define EPETRA_CHK_ERR(a)
virtual ~Epetra_DistObject()
Epetra_DistObject destructor.
Epetra_Export: This class builds an export object for efficient exporting of off-processor elements...
Definition: Epetra_Export.h:70
Epetra_Distributor & Distributor() const
Epetra_Import: This class builds an import object for efficient importing of off-processor elements...
Definition: Epetra_Import.h:71
int * ExportLIDs() const
List of elements that will be sent to other processors.
virtual int CheckSizes(const Epetra_SrcDistObject &Source)=0
Allows the source and target (this) objects to be compared for compatibility, return nonzero if not...
virtual int MyPID() const =0
Return my process ID.
int NumExportIDs() const
Returns the number of elements that must be sent by the calling processor to other processors...
int Export(const Epetra_SrcDistObject &A, const Epetra_Import &Importer, Epetra_CombineMode CombineMode, const Epetra_OffsetIndex *Indexor=0)
Exports an Epetra_DistObject using the Epetra_Import object.
virtual int DoTransfer(const Epetra_SrcDistObject &A, Epetra_CombineMode CombineMode, int NumSameIDs, int NumPermuteIDs, int NumRemoteIDs, int NumExportIDs, int *PermuteToLIDs, int *PermuteFromLIDs, int *RemoteLIDs, int *ExportLIDs, int &LenExports, char *&Exports, int &LenImports, char *&Imports, Epetra_Distributor &Distor, bool DoReverse, const Epetra_OffsetIndex *Indexor)
Perform actual transfer (redistribution) of data across memory images, using Epetra_Distributor objec...
int * PermuteFromLIDs() const
List of elements in the source map that are permuted.
const Epetra_BlockMap & TargetMap() const
Returns the TargetMap used to construct this importer.
Epetra_Object: The base Epetra class.
Definition: Epetra_Object.h:65
virtual const Epetra_BlockMap & Map() const =0
Returns a reference to the Epetra_BlockMap for this object.
virtual int UnpackAndCombine(const Epetra_SrcDistObject &Source, int NumImportIDs, int *ImportLIDs, int LenImports, char *Imports, int &SizeOfPacket, Epetra_Distributor &Distor, Epetra_CombineMode CombineMode, const Epetra_OffsetIndex *Indexor)=0
Perform any unpacking and combining after call to DoTransfer().
Epetra_BlockMap: A class for partitioning block element vectors and matrices.
const Epetra_BlockMap & SourceMap() const
Returns the SourceMap used to construct this importer.
int * RemoteLIDs() const
List of elements in the target map that are coming from other processors.
int * PermuteFromLIDs() const
List of elements in the source map that are permuted.
Epetra_DistObject & operator=(const Epetra_DistObject &src)
virtual int Do(char *export_objs, int obj_size, int &len_import_objs, char *&import_objs)=0
Execute plan on buffer of export objects in a single step.
int NumPermuteIDs() const
Returns the number of elements that are local to the calling processor, but not part of the first Num...
int NumExportIDs() const
Returns the number of elements that must be sent by the calling processor to other processors...
Epetra_DistObject(const Epetra_BlockMap &Map)
Basic Epetra_DistObject constuctor.
virtual int NumProc() const =0
Returns total number of processes.
int * RemoteLIDs() const
List of elements in the target map that are coming from other processors.
Epetra_BlockMap Map_
Epetra_CombineMode
virtual int ReportError(const std::string Message, int ErrorCode) const
Error reporting method.
virtual void PrintInfo(std::ostream &os) const =0
Print object to an output stream.
int NumRemoteIDs() const
Returns the number of elements that are not on the calling processor.
Epetra_SrcDistObject: A class for supporting flexible source distributed objects for import/export op...
int Import(const Epetra_SrcDistObject &A, const Epetra_Import &Importer, Epetra_CombineMode CombineMode, const Epetra_OffsetIndex *Indexor=0)
Imports an Epetra_DistObject using the Epetra_Import object.
int * PermuteToLIDs() const
List of elements in the target map that are permuted.
int * PermuteToLIDs() const
List of elements in the target map that are permuted.
Epetra_DistObject: A class for constructing and using dense multi-vectors, vectors and matrices in pa...
virtual int PackAndPrepare(const Epetra_SrcDistObject &Source, int NumExportIDs, int *ExportLIDs, int &LenExports, char *&Exports, int &SizeOfPacket, int *Sizes, bool &VarSizes, Epetra_Distributor &Distor)=0
Perform any packing or preparation required for call to DoTransfer().