Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
DistributedVectorFactory.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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
40#include <petscvec.h>
41
42#include "DistributedVector.hpp"
43#include "Exception.hpp"
44#include "PetscTools.hpp"
45
58{
59private:
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
120public:
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
253
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
302namespace boost
303{
304namespace serialization
305{
306
307template<class Archive>
308inline 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
326template<class Archive>
327inline 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_*/
#define EXCEPTION(message)
gcov doesn't like this file...
#define CHASTE_CLASS_EXPORT(T)
static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
void SetOriginalFactory(DistributedVectorFactory *pOriginalFactory)
DistributedVectorFactory * GetOriginalFactory()
DistributedVector CreateDistributedVector(Vec vec, bool readOnly=false)
std::vector< unsigned > mGlobalLows
std::vector< unsigned > & rGetGlobalLows()
void SetFromFactory(DistributedVectorFactory *pFactory)
friend class boost::serialization::access
void serialize(Archive &archive, const unsigned int version)
DistributedVectorFactory * mpOriginalFactory
bool IsGlobalIndexLocal(unsigned globalIndex)
static unsigned GetNumProcs()