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 
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 //     * For debugging.
00251 //     */
00252 //    void Dump(std::string msg=std::string())
00253 //    {
00254 //        std::cout << "DVF(" << this << "): " << msg;
00255 //        std::cout << " lo=" << mLo << " hi=" << mHi << " size=" << mProblemSize << " np=" << mNumProcs;
00256 //        std::cout << " [running np=" << PetscTools::GetNumProcs() << " rank=" << PetscTools::GetMyRank() << "]\n" << std::flush;
00257 //    }
00258 };
00259 #include "SerializationExportWrapper.hpp"
00260 // Declare identifier for the serializer
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             // We need to have a ::new here, or Boost will try to free non-allocated memory
00307             // when the exception is thrown.  However, if we use the ::new line after this if,
00308             // then PETSc complains about wrong sizes.
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 } // namespace ...
00318 
00319 #endif /*DISTRIBUTEDVECTORFACTORY_HPP_*/

Generated on Mon Nov 1 12:35:16 2010 for Chaste by  doxygen 1.5.5