Thyra  Version of the Day
Thyra_SpmdVectorSpaceDefaultBase_def.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef THYRA_SPMD_VECTOR_SPACE_BASE_DEF_HPP
43 #define THYRA_SPMD_VECTOR_SPACE_BASE_DEF_HPP
44 
45 #include "Thyra_SpmdVectorSpaceDefaultBase_decl.hpp"
46 #include "Thyra_ScalarProdVectorSpaceBase.hpp"
47 #include "Thyra_DefaultSpmdVectorSpaceFactory.hpp"
48 #include "Thyra_SpmdVectorSpaceUtilities.hpp"
49 #include "Thyra_ProductVectorSpaceBase.hpp"
50 
51 
52 namespace Thyra {
53 
54 
55 template<class Scalar>
57  :mapCode_(-1), defaultLocalOffset_(-1), defaultGlobalDim_(-1),
58  localSubDim_(-1), isLocallyReplicated_(false)
59 {}
60 
61 
62 // Virtual methods with default implementations
63 
64 
65 template<class Scalar>
67 {
68  return defaultLocalOffset_;
69 }
70 
71 
72 template<class Scalar>
74 {
75  return mapCode_;
76 }
77 
78 
79 template<class Scalar>
81 {
82  return isLocallyReplicated_;
83 }
84 
85 
86 template<class Scalar>
88 {
89  using Teuchos::RCP; using Teuchos::Comm; using Teuchos::null;
90  std::ostringstream ostr;
91  ostr << Teuchos::typeName(*this) << "{";
92  ostr << "globalDim="<<this->dim();
93  ostr << ",localSubDim="<<this->localSubDim();
94  ostr << ",localOffset="<<this->localOffset();
95  ostr << ",comm=";
97  if ( (comm=this->getComm())!=null ) {
98  ostr << comm->description();
99  }
100  else {
101  ostr << "NULL";
102  }
103  ostr << "}";
104  return ostr.str();
105 }
106 
107 
108 // Overridden from VectorSpaceBase
109 
110 
111 template<class Scalar>
113 {
114  return defaultGlobalDim_;
115 }
116 
117 
118 template<class Scalar>
121 {
122  return smallVecSpcFcty_;
123 }
124 
125 
126 template<class Scalar>
128  const VectorSpaceBase<Scalar>& vecSpc
129  ) const
130 {
131 
132  using Teuchos::ptrFromRef;
133  using Teuchos::ptr_dynamic_cast;
134 
135  // Check for exact match of vector space
137  spmdVecSpc = ptr_dynamic_cast<const SpmdVectorSpaceBase<Scalar> >(ptrFromRef(vecSpc));
138  if (nonnull(spmdVecSpc)) {
139  return mapCode() == spmdVecSpc->mapCode();
140  }
141 
142  // Check for product vector interface
144  ptr_dynamic_cast<const ProductVectorSpaceBase<Scalar> >(ptrFromRef(vecSpc));
145 
146  if (nonnull(pvsb)) {
147  if (pvsb->numBlocks() == 1 ) {
148  return pvsb->getBlock(0)->isCompatible(*this);
149  }
150  else {
151  return false;
152  }
153  }
154 
155  // If we get here, we are not compatible!
156  return false;
157 
158 }
159 
160 
161 // protected
162 
163 
164 template<class Scalar>
166  const bool isLocallyReplicated_in)
167 {
168  namespace SVSU = SpmdVectorSpaceUtilities;
169 
170  //
171  // A) Get the comm, comm info, and local subdim
172  //
173 
174  localSubDim_ = this->localSubDim();
175 
177  comm = this->getComm();
178 
179  int numProcs = 1;
180  if (nonnull(comm)) {
181  numProcs = comm->getSize();
182  }
183 
184  //
185  // B) Determine the type of vector space
186  //
187 
188  Ordinal sumLocalSubDims = localSubDim_;
189 
190  bool isSerialOrLocallyReplicated = false;
191  bool isEmpty = false;
192  bool isDistributed = false;
193 
194  if (isLocallyReplicated_in) {
195  // Avoid communication when we know this is locally replicated
196  isSerialOrLocallyReplicated = true;
197  if (sumLocalSubDims == 0) {
198  isEmpty = true;
199  }
200  TEUCHOS_ASSERT_EQUALITY(localSubDim_, globalDim);
201  }
202  else {
203  if (numProcs > 1) {
204  sumLocalSubDims = SVSU::computeGlobalDim(*comm, localSubDim_);
205  }
206  if (sumLocalSubDims == 0) {
207  // This is an uninitialized space (zero on every process)
208  isEmpty = true;
209  }
210  else if (
211  numProcs == 1
212  ||
213  (
214  sumLocalSubDims / numProcs == globalDim
215  &&
216  sumLocalSubDims % numProcs == 0
217  )
218  )
219  {
220  // This is a serial or a locally-replicated parallel
221  // vector space.
222  isSerialOrLocallyReplicated = true;
223  //TEUCHOS_TEST_FOR_EXCEPTION(numProcs > 1, std::logic_error,
224  // "Error, you are creating a locally replicated vector space implicitly which"
225  // " is very inefficient. Please pass in isLocallyReplicated=true to avoid"
226  // " unnecessary global communication!");
227  // ToDo: Add runtime option to assert that an implicit VS is not being
228  // created which is a performance problem.
229  }
230  else {
231  // This is a regular distributed vector space
232  isDistributed = true;
233  }
234  }
235 
236  //
237  // C) Set the state of the vector space for the given type
238  //
239 
240  if (isEmpty) {
241  mapCode_ = 0;
242  defaultLocalOffset_ = 0;
243  defaultGlobalDim_ = 0;
244  }
245  else if (isSerialOrLocallyReplicated) {
246  isLocallyReplicated_ = true;
247  mapCode_ = localSubDim_;
248  defaultLocalOffset_ = 0;
249  defaultGlobalDim_ = localSubDim_;
250  }
251  else {
252  TEUCHOS_ASSERT(isDistributed);
253  defaultGlobalDim_ = sumLocalSubDims;
254  mapCode_ = SVSU::computeMapCode(*comm, localSubDim_);
255  defaultLocalOffset_ = SVSU::computeLocalOffset(*comm, localSubDim_);
256  }
257 
258  smallVecSpcFcty_ = defaultSpmdVectorSpaceFactory<Scalar>(comm);
259 
260 }
261 
262 
263 } // end namespace Thyra
264 
265 
266 #endif // THYRA_SPMD_VECTOR_SPACE_BASE_DEF_HPP
virtual void updateState(const Ordinal globalDim, const bool isLocallyReplicated=false)
This function must be called whenever the state of this changes and some internal state must be updat...
Teuchos::RCP< const VectorSpaceFactoryBase< Scalar > > smallVecSpcFcty() const
Returns a DefaultSpmdVectorSpaceFactory object that has been given getComm().
bool isLocallyReplicated() const
Returns true if vector space is locally replicated space.
Ordinal dim() const
Returns the sum of the local number of elements on every process.
bool isCompatible(const VectorSpaceBase< Scalar > &vecSpc) const
Checks the general compatibility of parallel (or serial on one process) Spmd-based vector spaces.
Abstract interface for objects that represent a space for vectors.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
bool nonnull(const boost::shared_ptr< T > &p)
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
std::string typeName(const T &t)