DistributedVectorFactory.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef DISTRIBUTEDVECTORFACTORY_HPP_
00030 #define DISTRIBUTEDVECTORFACTORY_HPP_
00031
00032 #include "ChasteSerialization.hpp"
00033 #include <petscvec.h>
00034
00035 #include "DistributedVector.hpp"
00036 #include "Exception.hpp"
00037 #include "PetscTools.hpp"
00038
00050 class DistributedVectorFactory
00051 {
00052 private:
00053
00055 unsigned mLo;
00057 unsigned mHi;
00059 unsigned mProblemSize;
00061 unsigned mNumProcs;
00063 bool mPetscStatusKnown;
00064
00069 static bool msCheckNumberOfProcessesOnLoad;
00075 DistributedVectorFactory* mpOriginalFactory;
00076
00080 void CheckForPetsc();
00081
00087 void CalculateOwnership(Vec vec);
00088
00090 friend class boost::serialization::access;
00091
00098 template<class Archive>
00099 void serialize(Archive & archive, const unsigned int version)
00100 {
00101
00102 }
00103
00104 public:
00110 DistributedVectorFactory(Vec vec);
00111
00118 DistributedVectorFactory(unsigned size, PetscInt local=PETSC_DECIDE);
00119
00124 DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory);
00125
00135 DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs=PetscTools::GetNumProcs());
00136
00138 ~DistributedVectorFactory();
00139
00145 Vec CreateVec();
00146
00152 Vec CreateVec(unsigned stride);
00153
00160 DistributedVector CreateDistributedVector(Vec vec);
00161
00167 bool IsGlobalIndexLocal(unsigned globalIndex);
00168
00172 unsigned GetLocalOwnership() const
00173 {
00174 return mHi - mLo;
00175 }
00176
00180 unsigned GetHigh() const
00181 {
00182 return mHi;
00183 }
00184
00188 unsigned GetLow() const
00189 {
00190 return mLo;
00191 }
00192
00196 unsigned GetProblemSize() const
00197 {
00198 return mProblemSize;
00199 }
00200
00204 unsigned GetNumProcs() const
00205 {
00206 return mNumProcs;
00207 }
00208
00215 static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
00216 {
00217 msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
00218 }
00219
00224 static bool CheckNumberOfProcessesOnLoad()
00225 {
00226 return msCheckNumberOfProcessesOnLoad;
00227 }
00228
00235 DistributedVectorFactory* GetOriginalFactory()
00236 {
00237 return mpOriginalFactory;
00238 }
00239
00244 void SetFromFactory(DistributedVectorFactory* pFactory);
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 };
00256 #include "SerializationExportWrapper.hpp"
00257
00258 CHASTE_CLASS_EXPORT(DistributedVectorFactory);
00259
00260 namespace boost
00261 {
00262 namespace serialization
00263 {
00264
00265 template<class Archive>
00266 inline void save_construct_data(
00267 Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
00268 {
00269 unsigned num_procs, lo, hi, size;
00270 hi = t->GetHigh();
00271 ar << hi;
00272 lo = t->GetLow();
00273 ar << lo;
00274 size = t->GetProblemSize();
00275 ar << size;
00276 num_procs = PetscTools::GetNumProcs();
00277 ar << num_procs;
00278 }
00279
00284 template<class Archive>
00285 inline void load_construct_data(
00286 Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
00287 {
00288 unsigned num_procs, lo, hi, size;
00289 ar >> hi;
00290 ar >> lo;
00291 ar >> size;
00292 ar >> num_procs;
00293
00294 if (!DistributedVectorFactory::CheckNumberOfProcessesOnLoad())
00295 {
00296 DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
00297 ::new(t)DistributedVectorFactory(p_original_factory);
00298 }
00299 else
00300 {
00301 if (num_procs != PetscTools::GetNumProcs())
00302 {
00303
00304
00305
00306 ::new(t)DistributedVectorFactory(size);
00307 EXCEPTION("This archive was written for a different number of processors");
00308 }
00309 ::new(t)DistributedVectorFactory(size, hi-lo);
00310 }
00311 }
00312
00313 }
00314 }
00315
00316 #endif