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;
00065 std::vector<unsigned> mGlobalLows;
00066
00071 static bool msCheckNumberOfProcessesOnLoad;
00072
00077 DistributedVectorFactory* mpOriginalFactory;
00078
00082 void CheckForPetsc();
00083
00089 void CalculateOwnership(Vec vec);
00090
00092 friend class boost::serialization::access;
00093
00100 template<class Archive>
00101 void serialize(Archive & archive, const unsigned int version)
00102 {
00103
00104 }
00105
00106 public:
00112 DistributedVectorFactory(Vec vec);
00113
00120 DistributedVectorFactory(unsigned size, PetscInt local=PETSC_DECIDE);
00121
00129 DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory);
00130
00140 DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs=PetscTools::GetNumProcs());
00141
00143 ~DistributedVectorFactory();
00144
00150 Vec CreateVec();
00151
00157 Vec CreateVec(unsigned stride);
00158
00165 DistributedVector CreateDistributedVector(Vec vec);
00166
00172 bool IsGlobalIndexLocal(unsigned globalIndex);
00173
00177 unsigned GetLocalOwnership() const
00178 {
00179 return mHi - mLo;
00180 }
00181
00185 unsigned GetHigh() const
00186 {
00187 return mHi;
00188 }
00189
00193 unsigned GetLow() const
00194 {
00195 return mLo;
00196 }
00197
00201 unsigned GetProblemSize() const
00202 {
00203 return mProblemSize;
00204 }
00205
00209 unsigned GetNumProcs() const
00210 {
00211 return mNumProcs;
00212 }
00213
00220 static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
00221 {
00222 msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
00223 }
00224
00229 static bool CheckNumberOfProcessesOnLoad()
00230 {
00231 return msCheckNumberOfProcessesOnLoad;
00232 }
00233
00240 DistributedVectorFactory* GetOriginalFactory()
00241 {
00242 return mpOriginalFactory;
00243 }
00244
00249 void SetOriginalFactory(DistributedVectorFactory* pOriginalFactory)
00250 {
00251 mpOriginalFactory = pOriginalFactory;
00252 }
00253
00258 void SetFromFactory(DistributedVectorFactory* pFactory);
00259
00264 std::vector<unsigned> &rGetGlobalLows();
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 };
00278 #include "SerializationExportWrapper.hpp"
00279
00280 CHASTE_CLASS_EXPORT(DistributedVectorFactory)
00281
00282 namespace boost
00283 {
00284 namespace serialization
00285 {
00286
00287 template<class Archive>
00288 inline void save_construct_data(
00289 Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
00290 {
00291 unsigned num_procs, lo, hi, size;
00292 hi = t->GetHigh();
00293 ar << hi;
00294 lo = t->GetLow();
00295 ar << lo;
00296 size = t->GetProblemSize();
00297 ar << size;
00298 num_procs = PetscTools::GetNumProcs();
00299 ar << num_procs;
00300 }
00301
00306 template<class Archive>
00307 inline void load_construct_data(
00308 Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
00309 {
00310 unsigned num_procs, lo, hi, size;
00311 ar >> hi;
00312 ar >> lo;
00313 ar >> size;
00314 ar >> num_procs;
00315 DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
00316
00317 if (!DistributedVectorFactory::CheckNumberOfProcessesOnLoad())
00318 {
00319 ::new(t)DistributedVectorFactory(p_original_factory);
00320 }
00321 else
00322 {
00323 if (num_procs != PetscTools::GetNumProcs())
00324 {
00325
00326
00327
00328 ::new(t)DistributedVectorFactory(size);
00329 EXCEPTION("This archive was written for a different number of processors");
00330 }
00331 ::new(t)DistributedVectorFactory(size, hi-lo);
00332 t->SetOriginalFactory(p_original_factory);
00333 }
00334 }
00335
00336 }
00337 }
00338
00339 #endif