DistributedVectorFactory.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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     // Data global to all vectors created by this factory.
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         // Nothing to do - all done in load_construct_data
00102     }
00103 
00104 public:
00110     DistributedVectorFactory(Vec vec);
00111 
00118     DistributedVectorFactory(unsigned size, PetscInt local=PETSC_DECIDE);
00119 
00124     DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory);
00125 
00135     DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs=PetscTools::GetNumProcs());
00136 
00138     ~DistributedVectorFactory();
00139 
00145     Vec CreateVec();
00146 
00152     Vec CreateVec(unsigned stride);
00153 
00160     DistributedVector CreateDistributedVector(Vec vec);
00161 
00167     bool IsGlobalIndexLocal(unsigned globalIndex);
00168 
00172     unsigned GetLocalOwnership() const
00173     {
00174         return mHi - mLo;
00175     }
00176 
00180     unsigned GetHigh() const
00181     {
00182         return mHi;
00183     }
00184 
00188     unsigned GetLow() const
00189     {
00190         return mLo;
00191     }
00192 
00196     unsigned GetProblemSize() const
00197     {
00198         return mProblemSize;
00199     }
00200 
00204     unsigned GetNumProcs() const
00205     {
00206         return mNumProcs;
00207     }
00208 
00215     static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
00216     {
00217         msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
00218     }
00219 
00224     static bool CheckNumberOfProcessesOnLoad()
00225     {
00226         return msCheckNumberOfProcessesOnLoad;
00227     }
00228 
00235     DistributedVectorFactory* GetOriginalFactory()
00236     {
00237         return mpOriginalFactory;
00238     }
00239 
00244     void SetFromFactory(DistributedVectorFactory* pFactory);
00245 
00246 //    /**
00247 //     * For debugging.
00248 //     */
00249 //    void Dump(std::string msg=std::string())
00250 //    {
00251 //        std::cout << "DVF(" << this << "): " << msg;
00252 //        std::cout << " lo=" << mLo << " hi=" << mHi << " size=" << mProblemSize << " np=" << mNumProcs;
00253 //        std::cout << " [running np=" << PetscTools::GetNumProcs() << " rank=" << PetscTools::GetMyRank() << "]\n" << std::flush;
00254 //    }
00255 };
00256 #include "SerializationExportWrapper.hpp"
00257 // Declare identifier for the serializer
00258 CHASTE_CLASS_EXPORT(DistributedVectorFactory);
00259 
00260 namespace boost
00261 {
00262 namespace serialization
00263 {
00264 
00265 template<class Archive>
00266 inline void save_construct_data(
00267     Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
00268 {
00269     unsigned num_procs, lo, hi, size;
00270     hi = t->GetHigh();
00271     ar << hi;
00272     lo = t->GetLow();
00273     ar << lo;
00274     size = t->GetProblemSize();
00275     ar << size;
00276     num_procs = PetscTools::GetNumProcs();
00277     ar << num_procs;
00278 }
00279 
00284 template<class Archive>
00285 inline void load_construct_data(
00286     Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
00287 {
00288     unsigned num_procs, lo, hi, size;
00289     ar >> hi;
00290     ar >> lo;
00291     ar >> size;
00292     ar >> num_procs;
00293 
00294     if (!DistributedVectorFactory::CheckNumberOfProcessesOnLoad())
00295     {
00296         DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
00297         ::new(t)DistributedVectorFactory(p_original_factory);
00298     }
00299     else
00300     {
00301         if (num_procs != PetscTools::GetNumProcs())
00302         {
00303             // We need to have a ::new here, or Boost will try to free non-allocated memory
00304             // when the exception is thrown.  However, if we use the ::new line after this if,
00305             // then PETSc complains about wrong sizes.
00306             ::new(t)DistributedVectorFactory(size);
00307             EXCEPTION("This archive was written for a different number of processors");
00308         }
00309         ::new(t)DistributedVectorFactory(size, hi-lo);
00310     }
00311 }
00312 
00313 }
00314 } // namespace ...
00315 
00316 #endif /*DISTRIBUTEDVECTORFACTORY_HPP_*/

Generated by  doxygen 1.6.2