DistributedVectorFactory.hpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #ifndef DISTRIBUTEDVECTORFACTORY_HPP_
00037 #define DISTRIBUTEDVECTORFACTORY_HPP_
00038 
00039 #include "ChasteSerialization.hpp"
00040 #include <petscvec.h>
00041 
00042 #include "DistributedVector.hpp"
00043 #include "Exception.hpp"
00044 #include "PetscTools.hpp"
00045 
00057  class DistributedVectorFactory
00058 {
00059 private:
00060 
00061     // Data global to all vectors created by this factory
00062 
00064     unsigned mLo;
00065 
00067     unsigned mHi;
00068 
00070     unsigned mProblemSize;
00071 
00073     unsigned mNumProcs;
00074 
00076     bool mPetscStatusKnown;
00077 
00079     std::vector<unsigned> mGlobalLows;
00080 
00085     static bool msCheckNumberOfProcessesOnLoad;
00086 
00091     DistributedVectorFactory* mpOriginalFactory;
00092 
00096     void CheckForPetsc();
00097 
00103     void CalculateOwnership(Vec vec);
00104 
00106     friend class boost::serialization::access;
00107 
00114     template<class Archive>
00115     void serialize(Archive & archive, const unsigned int version)
00116     {
00117         // Nothing to do - all done in load_construct_data
00118     }
00119 
00120 public:
00121 
00127     DistributedVectorFactory(Vec vec);
00128 
00135     DistributedVectorFactory(unsigned size, PetscInt local=PETSC_DECIDE);
00136 
00144     DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory);
00145 
00155     DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs=PetscTools::GetNumProcs());
00156 
00158     ~DistributedVectorFactory();
00159 
00165     Vec CreateVec();
00166 
00173     Vec CreateVec(unsigned stride);
00174 
00181     DistributedVector CreateDistributedVector(Vec vec);
00182 
00189     bool IsGlobalIndexLocal(unsigned globalIndex);
00190 
00194     unsigned GetLocalOwnership() const
00195     {
00196         return mHi - mLo;
00197     }
00198 
00202     unsigned GetHigh() const
00203     {
00204         return mHi;
00205     }
00206 
00210     unsigned GetLow() const
00211     {
00212         return mLo;
00213     }
00214 
00218     unsigned GetProblemSize() const
00219     {
00220         return mProblemSize;
00221     }
00222 
00226     unsigned GetNumProcs() const
00227     {
00228         return mNumProcs;
00229     }
00230 
00237     static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
00238     {
00239         msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
00240     }
00241 
00247     static bool CheckNumberOfProcessesOnLoad()
00248     {
00249         return msCheckNumberOfProcessesOnLoad;
00250     }
00251 
00259     DistributedVectorFactory* GetOriginalFactory()
00260     {
00261         return mpOriginalFactory;
00262     }
00263 
00268     void SetOriginalFactory(DistributedVectorFactory* pOriginalFactory)
00269     {
00270         mpOriginalFactory = pOriginalFactory;
00271     }
00272 
00277     void SetFromFactory(DistributedVectorFactory* pFactory);
00278 
00283     std::vector<unsigned> &rGetGlobalLows();
00284 
00285 //    /**
00286 //     * For debugging.
00287 //     */
00288 //    void Dump(std::string msg=std::string())
00289 //    {
00290 //        std::cout << "DVF(" << this << "): " << msg;
00291 //        std::cout << " lo=" << mLo << " hi=" << mHi << " size=" << mProblemSize << " np=" << mNumProcs;
00292 //        std::cout << " [running np=" << PetscTools::GetNumProcs() << " rank=" << PetscTools::GetMyRank() << "]\n" << std::flush;
00293 //    }
00294 };
00295 
00296 #include "SerializationExportWrapper.hpp"
00297 // Declare identifier for the serializer
00298 CHASTE_CLASS_EXPORT(DistributedVectorFactory)
00299 
00300 namespace boost
00301 {
00302 namespace serialization
00303 {
00304 
00305 template<class Archive>
00306 inline void save_construct_data(
00307     Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
00308 {
00309     unsigned num_procs, lo, hi, size;
00310     hi = t->GetHigh();
00311     ar << hi;
00312     lo = t->GetLow();
00313     ar << lo;
00314     size = t->GetProblemSize();
00315     ar << size;
00316     num_procs = PetscTools::GetNumProcs();
00317     ar << num_procs;
00318 }
00319 
00324 template<class Archive>
00325 inline void load_construct_data(
00326     Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
00327 {
00328     unsigned num_procs, lo, hi, size;
00329     ar >> hi;
00330     ar >> lo;
00331     ar >> size;
00332     ar >> num_procs;
00333     DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
00334 
00335     if (!DistributedVectorFactory::CheckNumberOfProcessesOnLoad())
00336     {
00337         ::new(t)DistributedVectorFactory(p_original_factory);
00338     }
00339     else
00340     {
00341         if (num_procs != PetscTools::GetNumProcs())
00342         {
00343             // We need to have a ::new here, or Boost will try to free non-allocated memory
00344             // when the exception is thrown.  However, if we use the ::new line after this if,
00345             // then PETSc complains about wrong sizes.
00346             ::new(t)DistributedVectorFactory(size);
00347             EXCEPTION("This archive was written for a different number of processors");
00348         }
00349         ::new(t)DistributedVectorFactory(size, hi-lo);
00350         t->SetOriginalFactory(p_original_factory);
00351     }
00352 }
00353 
00354 }
00355 } // namespace ...
00356 
00357 #endif /*DISTRIBUTEDVECTORFACTORY_HPP_*/

Generated by  doxygen 1.6.2