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
00030
00031
00032
00033
00034
00035
00036 #ifndef DISTRIBUTEDVECTORFACTORY_HPP_
00037 #define DISTRIBUTEDVECTORFACTORY_HPP_
00038
00039 #include "ChasteSerialization.hpp"
00040 #include <petscvec.h>
00041
00042 #include "DistributedVector.hpp"
00043 #include "Exception.hpp"
00044 #include "PetscTools.hpp"
00045
00057 class DistributedVectorFactory
00058 {
00059 private:
00060
00061
00062
00064 unsigned mLo;
00065
00067 unsigned mHi;
00068
00070 unsigned mProblemSize;
00071
00073 unsigned mNumProcs;
00074
00076 bool mPetscStatusKnown;
00077
00079 std::vector<unsigned> mGlobalLows;
00080
00085 static bool msCheckNumberOfProcessesOnLoad;
00086
00091 DistributedVectorFactory* mpOriginalFactory;
00092
00096 void CheckForPetsc();
00097
00103 void CalculateOwnership(Vec vec);
00104
00106 friend class boost::serialization::access;
00107
00114 template<class Archive>
00115 void serialize(Archive & archive, const unsigned int version)
00116 {
00117
00118 }
00119
00120 public:
00121
00127 DistributedVectorFactory(Vec vec);
00128
00135 DistributedVectorFactory(unsigned size, PetscInt local=PETSC_DECIDE);
00136
00144 DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory);
00145
00155 DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs=PetscTools::GetNumProcs());
00156
00158 ~DistributedVectorFactory();
00159
00165 Vec CreateVec();
00166
00173 Vec CreateVec(unsigned stride);
00174
00181 DistributedVector CreateDistributedVector(Vec vec);
00182
00189 bool IsGlobalIndexLocal(unsigned globalIndex);
00190
00194 unsigned GetLocalOwnership() const
00195 {
00196 return mHi - mLo;
00197 }
00198
00202 unsigned GetHigh() const
00203 {
00204 return mHi;
00205 }
00206
00210 unsigned GetLow() const
00211 {
00212 return mLo;
00213 }
00214
00218 unsigned GetProblemSize() const
00219 {
00220 return mProblemSize;
00221 }
00222
00226 unsigned GetNumProcs() const
00227 {
00228 return mNumProcs;
00229 }
00230
00237 static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
00238 {
00239 msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
00240 }
00241
00247 static bool CheckNumberOfProcessesOnLoad()
00248 {
00249 return msCheckNumberOfProcessesOnLoad;
00250 }
00251
00259 DistributedVectorFactory* GetOriginalFactory()
00260 {
00261 return mpOriginalFactory;
00262 }
00263
00268 void SetOriginalFactory(DistributedVectorFactory* pOriginalFactory)
00269 {
00270 mpOriginalFactory = pOriginalFactory;
00271 }
00272
00277 void SetFromFactory(DistributedVectorFactory* pFactory);
00278
00283 std::vector<unsigned> &rGetGlobalLows();
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 };
00295
00296 #include "SerializationExportWrapper.hpp"
00297
00298 CHASTE_CLASS_EXPORT(DistributedVectorFactory)
00299
00300 namespace boost
00301 {
00302 namespace serialization
00303 {
00304
00305 template<class Archive>
00306 inline void save_construct_data(
00307 Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
00308 {
00309 unsigned num_procs, lo, hi, size;
00310 hi = t->GetHigh();
00311 ar << hi;
00312 lo = t->GetLow();
00313 ar << lo;
00314 size = t->GetProblemSize();
00315 ar << size;
00316 num_procs = PetscTools::GetNumProcs();
00317 ar << num_procs;
00318 }
00319
00324 template<class Archive>
00325 inline void load_construct_data(
00326 Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
00327 {
00328 unsigned num_procs, lo, hi, size;
00329 ar >> hi;
00330 ar >> lo;
00331 ar >> size;
00332 ar >> num_procs;
00333 DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
00334
00335 if (!DistributedVectorFactory::CheckNumberOfProcessesOnLoad())
00336 {
00337 ::new(t)DistributedVectorFactory(p_original_factory);
00338 }
00339 else
00340 {
00341 if (num_procs != PetscTools::GetNumProcs())
00342 {
00343
00344
00345
00346 ::new(t)DistributedVectorFactory(size);
00347 EXCEPTION("This archive was written for a different number of processors");
00348 }
00349 ::new(t)DistributedVectorFactory(size, hi-lo);
00350 t->SetOriginalFactory(p_original_factory);
00351 }
00352 }
00353
00354 }
00355 }
00356
00357 #endif