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
00054
00055
00057 unsigned mLo;
00058
00060 unsigned mHi;
00061
00063 unsigned mProblemSize;
00064
00066 unsigned mNumProcs;
00067
00069 bool mPetscStatusKnown;
00070
00072 std::vector<unsigned> mGlobalLows;
00073
00078 static bool msCheckNumberOfProcessesOnLoad;
00079
00084 DistributedVectorFactory* mpOriginalFactory;
00085
00089 void CheckForPetsc();
00090
00096 void CalculateOwnership(Vec vec);
00097
00099 friend class boost::serialization::access;
00100
00107 template<class Archive>
00108 void serialize(Archive & archive, const unsigned int version)
00109 {
00110
00111 }
00112
00113 public:
00114
00120 DistributedVectorFactory(Vec vec);
00121
00128 DistributedVectorFactory(unsigned size, PetscInt local=PETSC_DECIDE);
00129
00137 DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory);
00138
00148 DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs=PetscTools::GetNumProcs());
00149
00151 ~DistributedVectorFactory();
00152
00158 Vec CreateVec();
00159
00165 Vec CreateVec(unsigned stride);
00166
00173 DistributedVector CreateDistributedVector(Vec vec);
00174
00180 bool IsGlobalIndexLocal(unsigned globalIndex);
00181
00185 unsigned GetLocalOwnership() const
00186 {
00187 return mHi - mLo;
00188 }
00189
00193 unsigned GetHigh() const
00194 {
00195 return mHi;
00196 }
00197
00201 unsigned GetLow() const
00202 {
00203 return mLo;
00204 }
00205
00209 unsigned GetProblemSize() const
00210 {
00211 return mProblemSize;
00212 }
00213
00217 unsigned GetNumProcs() const
00218 {
00219 return mNumProcs;
00220 }
00221
00228 static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
00229 {
00230 msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
00231 }
00232
00237 static bool CheckNumberOfProcessesOnLoad()
00238 {
00239 return msCheckNumberOfProcessesOnLoad;
00240 }
00241
00248 DistributedVectorFactory* GetOriginalFactory()
00249 {
00250 return mpOriginalFactory;
00251 }
00252
00257 void SetOriginalFactory(DistributedVectorFactory* pOriginalFactory)
00258 {
00259 mpOriginalFactory = pOriginalFactory;
00260 }
00261
00266 void SetFromFactory(DistributedVectorFactory* pFactory);
00267
00272 std::vector<unsigned> &rGetGlobalLows();
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 };
00284
00285 #include "SerializationExportWrapper.hpp"
00286
00287 CHASTE_CLASS_EXPORT(DistributedVectorFactory)
00288
00289 namespace boost
00290 {
00291 namespace serialization
00292 {
00293
00294 template<class Archive>
00295 inline void save_construct_data(
00296 Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
00297 {
00298 unsigned num_procs, lo, hi, size;
00299 hi = t->GetHigh();
00300 ar << hi;
00301 lo = t->GetLow();
00302 ar << lo;
00303 size = t->GetProblemSize();
00304 ar << size;
00305 num_procs = PetscTools::GetNumProcs();
00306 ar << num_procs;
00307 }
00308
00313 template<class Archive>
00314 inline void load_construct_data(
00315 Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
00316 {
00317 unsigned num_procs, lo, hi, size;
00318 ar >> hi;
00319 ar >> lo;
00320 ar >> size;
00321 ar >> num_procs;
00322 DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
00323
00324 if (!DistributedVectorFactory::CheckNumberOfProcessesOnLoad())
00325 {
00326 ::new(t)DistributedVectorFactory(p_original_factory);
00327 }
00328 else
00329 {
00330 if (num_procs != PetscTools::GetNumProcs())
00331 {
00332
00333
00334
00335 ::new(t)DistributedVectorFactory(size);
00336 EXCEPTION("This archive was written for a different number of processors");
00337 }
00338 ::new(t)DistributedVectorFactory(size, hi-lo);
00339 t->SetOriginalFactory(p_original_factory);
00340 }
00341 }
00342
00343 }
00344 }
00345
00346 #endif