DistributedVectorFactory.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
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     // Data global to all vectors created by this factory
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         // Nothing to do - all done in load_construct_data
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 //     * For debugging.
00276 //     */
00277 //    void Dump(std::string msg=std::string())
00278 //    {
00279 //        std::cout << "DVF(" << this << "): " << msg;
00280 //        std::cout << " lo=" << mLo << " hi=" << mHi << " size=" << mProblemSize << " np=" << mNumProcs;
00281 //        std::cout << " [running np=" << PetscTools::GetNumProcs() << " rank=" << PetscTools::GetMyRank() << "]\n" << std::flush;
00282 //    }
00283 };
00284 
00285 #include "SerializationExportWrapper.hpp"
00286 // Declare identifier for the serializer
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             // We need to have a ::new here, or Boost will try to free non-allocated memory
00333             // when the exception is thrown.  However, if we use the ::new line after this if,
00334             // then PETSc complains about wrong sizes.
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 } // namespace ...
00345 
00346 #endif /*DISTRIBUTEDVECTORFACTORY_HPP_*/
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3