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
00127 DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory);
00128
00138 DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs=PetscTools::GetNumProcs());
00139
00141 ~DistributedVectorFactory();
00142
00148 Vec CreateVec();
00149
00155 Vec CreateVec(unsigned stride);
00156
00163 DistributedVector CreateDistributedVector(Vec vec);
00164
00170 bool IsGlobalIndexLocal(unsigned globalIndex);
00171
00175 unsigned GetLocalOwnership() const
00176 {
00177 return mHi - mLo;
00178 }
00179
00183 unsigned GetHigh() const
00184 {
00185 return mHi;
00186 }
00187
00191 unsigned GetLow() const
00192 {
00193 return mLo;
00194 }
00195
00199 unsigned GetProblemSize() const
00200 {
00201 return mProblemSize;
00202 }
00203
00207 unsigned GetNumProcs() const
00208 {
00209 return mNumProcs;
00210 }
00211
00218 static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
00219 {
00220 msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
00221 }
00222
00227 static bool CheckNumberOfProcessesOnLoad()
00228 {
00229 return msCheckNumberOfProcessesOnLoad;
00230 }
00231
00238 DistributedVectorFactory* GetOriginalFactory()
00239 {
00240 return mpOriginalFactory;
00241 }
00242
00247 void SetFromFactory(DistributedVectorFactory* pFactory);
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 };
00259 #include "SerializationExportWrapper.hpp"
00260
00261 CHASTE_CLASS_EXPORT(DistributedVectorFactory)
00262
00263 namespace boost
00264 {
00265 namespace serialization
00266 {
00267
00268 template<class Archive>
00269 inline void save_construct_data(
00270 Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
00271 {
00272 unsigned num_procs, lo, hi, size;
00273 hi = t->GetHigh();
00274 ar << hi;
00275 lo = t->GetLow();
00276 ar << lo;
00277 size = t->GetProblemSize();
00278 ar << size;
00279 num_procs = PetscTools::GetNumProcs();
00280 ar << num_procs;
00281 }
00282
00287 template<class Archive>
00288 inline void load_construct_data(
00289 Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
00290 {
00291 unsigned num_procs, lo, hi, size;
00292 ar >> hi;
00293 ar >> lo;
00294 ar >> size;
00295 ar >> num_procs;
00296
00297 if (!DistributedVectorFactory::CheckNumberOfProcessesOnLoad())
00298 {
00299 DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
00300 ::new(t)DistributedVectorFactory(p_original_factory);
00301 }
00302 else
00303 {
00304 if (num_procs != PetscTools::GetNumProcs())
00305 {
00306
00307
00308
00309 ::new(t)DistributedVectorFactory(size);
00310 EXCEPTION("This archive was written for a different number of processors");
00311 }
00312 ::new(t)DistributedVectorFactory(size, hi-lo);
00313 }
00314 }
00315
00316 }
00317 }
00318
00319 #endif