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     // Data global to all vectors created by this factory.
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         // Nothing to do - all done in load_construct_data
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 //     * For debugging.
00270 //     */
00271 //    void Dump(std::string msg=std::string())
00272 //    {
00273 //        std::cout << "DVF(" << this << "): " << msg;
00274 //        std::cout << " lo=" << mLo << " hi=" << mHi << " size=" << mProblemSize << " np=" << mNumProcs;
00275 //        std::cout << " [running np=" << PetscTools::GetNumProcs() << " rank=" << PetscTools::GetMyRank() << "]\n" << std::flush;
00276 //    }
00277 };
00278 #include "SerializationExportWrapper.hpp"
00279 // Declare identifier for the serializer
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             // We need to have a ::new here, or Boost will try to free non-allocated memory
00326             // when the exception is thrown.  However, if we use the ::new line after this if,
00327             // then PETSc complains about wrong sizes.
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 } // namespace ...
00338 
00339 #endif /*DISTRIBUTEDVECTORFACTORY_HPP_*/

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5