Chaste  Release::2017.1
DistributedVectorFactory.hpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef DISTRIBUTEDVECTORFACTORY_HPP_
37 #define DISTRIBUTEDVECTORFACTORY_HPP_
38 
39 #include "ChasteSerialization.hpp"
40 #include <petscvec.h>
41 
42 #include "DistributedVector.hpp"
43 #include "Exception.hpp"
44 #include "PetscTools.hpp"
45 
58 {
59 private:
60 
61  // Data global to all vectors created by this factory
62 
64  unsigned mLo;
65 
67  unsigned mHi;
68 
70  unsigned mProblemSize;
71 
73  unsigned mNumProcs;
74 
77 
79  std::vector<unsigned> mGlobalLows;
80 
86 
92 
96  void CheckForPetsc();
97 
103  void CalculateOwnership(Vec vec);
104 
107 
114  template<class Archive>
115  void serialize(Archive & archive, const unsigned int version)
116  {
117  // Nothing to do - all done in load_construct_data
118  }
119 
120 public:
121 
128 
135  DistributedVectorFactory(unsigned size, PetscInt local=PETSC_DECIDE);
136 
145 
155  DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size,
156  unsigned numProcs=PetscTools::GetNumProcs());
157 
160 
166  Vec CreateVec();
167 
174  Vec CreateVec(unsigned stride);
175 
183  DistributedVector CreateDistributedVector(Vec vec, bool readOnly=false);
184 
191  bool IsGlobalIndexLocal(unsigned globalIndex);
192 
196  unsigned GetLocalOwnership() const
197  {
198  return mHi - mLo;
199  }
200 
204  unsigned GetHigh() const
205  {
206  return mHi;
207  }
208 
212  unsigned GetLow() const
213  {
214  return mLo;
215  }
216 
220  unsigned GetProblemSize() const
221  {
222  return mProblemSize;
223  }
224 
228  unsigned GetNumProcs() const
229  {
230  return mNumProcs;
231  }
232 
239  static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
240  {
241  msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
242  }
243 
250  {
252  }
253 
262  {
263  return mpOriginalFactory;
264  }
265 
271  {
272  mpOriginalFactory = pOriginalFactory;
273  }
274 
280 
285  std::vector<unsigned> &rGetGlobalLows();
286 
287 // /**
288 // * For debugging.
289 // */
290 // void Dump(std::string msg=std::string())
291 // {
292 // std::cout << "DVF(" << this << "): " << msg;
293 // std::cout << " lo=" << mLo << " hi=" << mHi << " size=" << mProblemSize << " np=" << mNumProcs;
294 // std::cout << " [running np=" << PetscTools::GetNumProcs() << " rank=" << PetscTools::GetMyRank() << "]\n" << std::flush;
295 // }
296 };
297 
299 // Declare identifier for the serializer
301 
302 namespace boost
303 {
304 namespace serialization
305 {
306 
307 template<class Archive>
308 inline void save_construct_data(
309  Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
310 {
311  unsigned num_procs, lo, hi, size;
312  hi = t->GetHigh();
313  ar << hi;
314  lo = t->GetLow();
315  ar << lo;
316  size = t->GetProblemSize();
317  ar << size;
318  num_procs = PetscTools::GetNumProcs();
319  ar << num_procs;
320 }
321 
326 template<class Archive>
327 inline void load_construct_data(
328  Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
329 {
330  unsigned num_procs, lo, hi, size;
331  ar >> hi;
332  ar >> lo;
333  ar >> size;
334  ar >> num_procs;
335  DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
336 
338  {
339  ::new(t)DistributedVectorFactory(p_original_factory);
340  }
341  else
342  {
343  if (num_procs != PetscTools::GetNumProcs())
344  {
345  // We need to have a ::new here, or Boost will try to free non-allocated memory
346  // when the exception is thrown. However, if we use the ::new line after this if,
347  // then PETSc complains about wrong sizes.
348  ::new(t)DistributedVectorFactory(size);
349  EXCEPTION("This archive was written for a different number of processors");
350  }
351  ::new(t)DistributedVectorFactory(size, hi-lo);
352  t->SetOriginalFactory(p_original_factory);
353  }
354 }
355 
356 }
357 } // namespace ...
358 
359 #endif /*DISTRIBUTEDVECTORFACTORY_HPP_*/
std::vector< unsigned > & rGetGlobalLows()
static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
std::vector< unsigned > mGlobalLows
friend class boost::serialization::access
void serialize(Archive &archive, const unsigned int version)
DistributedVector CreateDistributedVector(Vec vec, bool readOnly=false)
#define EXCEPTION(message)
Definition: Exception.hpp:143
DistributedVectorFactory * GetOriginalFactory()
bool IsGlobalIndexLocal(unsigned globalIndex)
void SetFromFactory(DistributedVectorFactory *pFactory)
void SetOriginalFactory(DistributedVectorFactory *pOriginalFactory)
DistributedVectorFactory * mpOriginalFactory
#define CHASTE_CLASS_EXPORT(T)
static unsigned GetNumProcs()
Definition: PetscTools.cpp:108
gcov doesn&#39;t like this file...