Chaste Release::3.1
DistributedVectorFactory.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, 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 
00172     Vec CreateVec(unsigned stride);
00173 
00180     DistributedVector CreateDistributedVector(Vec vec);
00181 
00187     bool IsGlobalIndexLocal(unsigned globalIndex);
00188 
00192     unsigned GetLocalOwnership() const
00193     {
00194         return mHi - mLo;
00195     }
00196 
00200     unsigned GetHigh() const
00201     {
00202         return mHi;
00203     }
00204 
00208     unsigned GetLow() const
00209     {
00210         return mLo;
00211     }
00212 
00216     unsigned GetProblemSize() const
00217     {
00218         return mProblemSize;
00219     }
00220 
00224     unsigned GetNumProcs() const
00225     {
00226         return mNumProcs;
00227     }
00228 
00235     static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
00236     {
00237         msCheckNumberOfProcessesOnLoad = checkNumberOfProcessesOnLoad;
00238     }
00239 
00244     static bool CheckNumberOfProcessesOnLoad()
00245     {
00246         return msCheckNumberOfProcessesOnLoad;
00247     }
00248 
00255     DistributedVectorFactory* GetOriginalFactory()
00256     {
00257         return mpOriginalFactory;
00258     }
00259 
00264     void SetOriginalFactory(DistributedVectorFactory* pOriginalFactory)
00265     {
00266         mpOriginalFactory = pOriginalFactory;
00267     }
00268 
00273     void SetFromFactory(DistributedVectorFactory* pFactory);
00274 
00279     std::vector<unsigned> &rGetGlobalLows();
00280 
00281 //    /**
00282 //     * For debugging.
00283 //     */
00284 //    void Dump(std::string msg=std::string())
00285 //    {
00286 //        std::cout << "DVF(" << this << "): " << msg;
00287 //        std::cout << " lo=" << mLo << " hi=" << mHi << " size=" << mProblemSize << " np=" << mNumProcs;
00288 //        std::cout << " [running np=" << PetscTools::GetNumProcs() << " rank=" << PetscTools::GetMyRank() << "]\n" << std::flush;
00289 //    }
00290 };
00291 
00292 #include "SerializationExportWrapper.hpp"
00293 // Declare identifier for the serializer
00294 CHASTE_CLASS_EXPORT(DistributedVectorFactory)
00295 
00296 namespace boost
00297 {
00298 namespace serialization
00299 {
00300 
00301 template<class Archive>
00302 inline void save_construct_data(
00303     Archive & ar, const DistributedVectorFactory * t, const unsigned int file_version)
00304 {
00305     unsigned num_procs, lo, hi, size;
00306     hi = t->GetHigh();
00307     ar << hi;
00308     lo = t->GetLow();
00309     ar << lo;
00310     size = t->GetProblemSize();
00311     ar << size;
00312     num_procs = PetscTools::GetNumProcs();
00313     ar << num_procs;
00314 }
00315 
00320 template<class Archive>
00321 inline void load_construct_data(
00322     Archive & ar, DistributedVectorFactory * t, const unsigned int file_version)
00323 {
00324     unsigned num_procs, lo, hi, size;
00325     ar >> hi;
00326     ar >> lo;
00327     ar >> size;
00328     ar >> num_procs;
00329     DistributedVectorFactory* p_original_factory = new DistributedVectorFactory(lo, hi, size, num_procs);
00330 
00331     if (!DistributedVectorFactory::CheckNumberOfProcessesOnLoad())
00332     {
00333         ::new(t)DistributedVectorFactory(p_original_factory);
00334     }
00335     else
00336     {
00337         if (num_procs != PetscTools::GetNumProcs())
00338         {
00339             // We need to have a ::new here, or Boost will try to free non-allocated memory
00340             // when the exception is thrown.  However, if we use the ::new line after this if,
00341             // then PETSc complains about wrong sizes.
00342             ::new(t)DistributedVectorFactory(size);
00343             EXCEPTION("This archive was written for a different number of processors");
00344         }
00345         ::new(t)DistributedVectorFactory(size, hi-lo);
00346         t->SetOriginalFactory(p_original_factory);
00347     }
00348 }
00349 
00350 }
00351 } // namespace ...
00352 
00353 #endif /*DISTRIBUTEDVECTORFACTORY_HPP_*/