Epetra Package Browser (Single Doxygen Collection)  Development
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 }
Epetra_Object
Epetra_Object: The base Epetra class.
Definition: Epetra_Object.h:57
Epetra_Export::PermuteFromLIDs
int * PermuteFromLIDs() const
List of elements in the source map that are permuted.
Definition: Epetra_Export.h:249
Epetra_DistObject::Exports_
char * Exports_
Definition: Epetra_DistObject.h:272
Epetra_Import::Distributor
Epetra_Distributor & Distributor() const
Definition: Epetra_Import.h:299
Epetra_Import::PermuteFromLIDs
int * PermuteFromLIDs() const
List of elements in the source map that are permuted.
Definition: Epetra_Import.h:268
Epetra_Comm::NumProc
virtual int NumProc() const =0
Returns total number of processes.
Epetra_DistObject::Comm
const Epetra_Comm & Comm() const
Returns the address of the Epetra_Comm for this multi-vector.
Definition: Epetra_DistObject.h:195
Epetra_Export::PermuteToLIDs
int * PermuteToLIDs() const
List of elements in the target map that are permuted.
Definition: Epetra_Export.h:251
Epetra_Export::ExportLIDs
int * ExportLIDs() const
List of elements that will be sent to other processors.
Definition: Epetra_Export.h:263
Epetra_DistObject::UnpackAndCombine
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_Import::NumRemoteIDs
int NumRemoteIDs() const
Returns the number of elements that are not on the calling processor.
Definition: Epetra_Import.h:273
Epetra_DistObject::PackAndPrepare
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().
Add
Definition: Epetra_CombineMode.h:64
Epetra_SrcDistObject
Epetra_SrcDistObject: A class for supporting flexible source distributed objects for import/export op...
Definition: Epetra_SrcDistObject.h:63
EPETRA_CHK_ERR
#define EPETRA_CHK_ERR(a)
Definition: Epetra_ConfigDefs.h:307
Epetra_DistObject::CopyAndPermute
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.
Epetra_DistObject.h
Epetra_Export::NumRemoteIDs
int NumRemoteIDs() const
Returns the number of elements that are not on the calling processor.
Definition: Epetra_Export.h:254
Epetra_Import::NumPermuteIDs
int NumPermuteIDs() const
Returns the number of elements that are local to the calling processor, but not part of the first Num...
Definition: Epetra_Import.h:265
Epetra_DistObject::Imports_
char * Imports_
Definition: Epetra_DistObject.h:273
Epetra_Export::NumSameIDs
int NumSameIDs() const
Returns the number of elements that are identical between the source and target maps,...
Definition: Epetra_Export.h:243
Zero
Definition: Epetra_CombineMode.h:66
Epetra_DistObject::Export
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.
Definition: Epetra_DistObject.cpp:198
Epetra_DistObject::operator=
Epetra_DistObject & operator=(const Epetra_DistObject &src)
Definition: Epetra_DistObject.cpp:310
Epetra_DistObject::LenImports_
int LenImports_
Definition: Epetra_DistObject.h:275
Epetra_Distributor::DoReverse
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_DistObject::~Epetra_DistObject
virtual ~Epetra_DistObject()
Epetra_DistObject destructor.
Definition: Epetra_DistObject.cpp:94
Epetra_DistObject::DoTransfer
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...
Definition: Epetra_DistObject.cpp:226
Epetra_DistObject::Sizes_
int * Sizes_
Definition: Epetra_DistObject.h:276
Epetra_Distributor::Do
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.
Epetra_Import::SourceMap
const Epetra_BlockMap & SourceMap() const
Returns the SourceMap used to construct this importer.
Definition: Epetra_Import.h:294
Epetra_Object::ReportError
virtual int ReportError(const std::string Message, int ErrorCode) const
Error reporting method.
Definition: Epetra_Object.cpp:103
Epetra_Export.h
Epetra_Export::RemoteLIDs
int * RemoteLIDs() const
List of elements in the target map that are coming from other processors.
Definition: Epetra_Export.h:257
Epetra_BlockMap::SameAs
bool SameAs(const Epetra_BlockMap &Map) const
Returns true if this and Map are identical maps.
Definition: Epetra_BlockMap.cpp:718
Epetra_BlockMap
Epetra_BlockMap: A class for partitioning block element vectors and matrices.
Definition: Epetra_BlockMap.h:194
Epetra_Import::ExportLIDs
int * ExportLIDs() const
List of elements that will be sent to other processors.
Definition: Epetra_Import.h:282
Epetra_Import::PermuteToLIDs
int * PermuteToLIDs() const
List of elements in the target map that are permuted.
Definition: Epetra_Import.h:270
Epetra_CombineMode
Epetra_CombineMode
Definition: Epetra_CombineMode.h:64
Epetra_OffsetIndex
Epetra_OffsetIndex: This class builds index for efficient mapping of data from one Epetra_CrsGraph ba...
Definition: Epetra_OffsetIndex.h:60
Epetra_Import.h
Epetra_AddLocalAlso
Definition: Epetra_CombineMode.h:89
Epetra_BlockMap::DistributedGlobal
bool DistributedGlobal() const
Returns true if map is defined across more than one processor.
Definition: Epetra_BlockMap.h:694
Epetra_Comm.h
Epetra_DistObject::Epetra_DistObject
Epetra_DistObject(const Epetra_BlockMap &Map)
Basic Epetra_DistObject constuctor.
Definition: Epetra_DistObject.cpp:54
Epetra_Export::TargetMap
const Epetra_BlockMap & TargetMap() const
Returns the TargetMap used to construct this exporter.
Definition: Epetra_Export.h:278
Epetra_Export::SourceMap
const Epetra_BlockMap & SourceMap() const
Returns the SourceMap used to construct this exporter.
Definition: Epetra_Export.h:275
A
Epetra_Import::TargetMap
const Epetra_BlockMap & TargetMap() const
Returns the TargetMap used to construct this importer.
Definition: Epetra_Import.h:297
Epetra_DistObject
Epetra_DistObject: A class for constructing and using dense multi-vectors, vectors and matrices in pa...
Definition: Epetra_DistObject.h:80
Epetra_Export::NumPermuteIDs
int NumPermuteIDs() const
Returns the number of elements that are local to the calling processor, but not part of the first Num...
Definition: Epetra_Export.h:246
Epetra_DistObject::Print
virtual void Print(std::ostream &os) const
Print method.
Definition: Epetra_DistObject.cpp:294
Epetra_Import::NumExportIDs
int NumExportIDs() const
Returns the number of elements that must be sent by the calling processor to other processors.
Definition: Epetra_Import.h:279
Epetra_Distributor
Epetra_Distributor: The Epetra Gather/Scatter Setup Base Class.
Definition: Epetra_Distributor.h:61
Epetra_DistObject::Import
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.
Definition: Epetra_DistObject.cpp:113
Epetra_Comm::PrintInfo
virtual void PrintInfo(std::ostream &os) const =0
Print object to an output stream.
Epetra_Export::NumExportIDs
int NumExportIDs() const
Returns the number of elements that must be sent by the calling processor to other processors.
Definition: Epetra_Export.h:260
Epetra_DistObject::Map_
Epetra_BlockMap Map_
Definition: Epetra_DistObject.h:270
Epetra_DistObject::LenExports_
int LenExports_
Definition: Epetra_DistObject.h:274
Epetra_Import::NumSameIDs
int NumSameIDs() const
Returns the number of elements that are identical between the source and target maps,...
Definition: Epetra_Import.h:262
Epetra_Export
Epetra_Export: This class builds an export object for efficient exporting of off-processor elements.
Definition: Epetra_Export.h:62
Epetra_Export::Distributor
Epetra_Distributor & Distributor() const
Definition: Epetra_Export.h:280
Epetra_Distributor.h
Epetra_Import::RemoteLIDs
int * RemoteLIDs() const
List of elements in the target map that are coming from other processors.
Definition: Epetra_Import.h:276
Epetra_Comm::MyPID
virtual int MyPID() const =0
Return my process ID.
Epetra_Import
Epetra_Import: This class builds an import object for efficient importing of off-processor elements.
Definition: Epetra_Import.h:63
Epetra_DistObject::CheckSizes
virtual int CheckSizes(const Epetra_SrcDistObject &Source)=0
Allows the source and target (this) objects to be compared for compatibility, return nonzero if not.