DistributedVectorFactory.cpp

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 #include <cassert>
00030 
00031 #include "DistributedVectorFactory.hpp"
00032 
00033 // Initialise static data
00034 bool DistributedVectorFactory::msCheckNumberOfProcessesOnLoad = true;
00035 
00036 
00037 
00038 void DistributedVectorFactory::CalculateOwnership(Vec vec)
00039 {
00040 #ifndef NDEBUG
00041     if (!mPetscStatusKnown)
00042     {
00043         CheckForPetsc();
00044     }
00045 #endif
00046     // calculate my range
00047     PetscInt petsc_lo, petsc_hi;
00048     VecGetOwnershipRange(vec, &petsc_lo, &petsc_hi);
00049     mGlobalLows.clear();
00050     mLo = (unsigned)petsc_lo;
00051     mHi = (unsigned)petsc_hi;
00052     // vector size
00053     PetscInt size;
00054     VecGetSize(vec, &size);
00055     mProblemSize = (unsigned) size;
00056     mNumProcs = PetscTools::GetNumProcs();
00057 }
00058 
00059 
00060 void DistributedVectorFactory::SetFromFactory(DistributedVectorFactory* pFactory)
00061 {
00062     if (pFactory->GetNumProcs() != mNumProcs)
00063     {
00064         EXCEPTION("Cannot set from a factory for a different number of processes.");
00065     }
00066     if (pFactory->GetProblemSize() != mProblemSize)
00067     {
00068         EXCEPTION("Cannot set from a factory for a different problem size.");
00069     }
00070     mGlobalLows.clear();
00071     mLo = pFactory->GetLow();
00072     mHi = pFactory->GetHigh();
00073 }
00074 
00075 DistributedVectorFactory::DistributedVectorFactory(Vec vec)
00076     : mPetscStatusKnown(false),
00077       mpOriginalFactory(NULL)
00078 {
00079     CalculateOwnership(vec);
00080 }
00081 
00082 DistributedVectorFactory::DistributedVectorFactory(unsigned size, PetscInt local)
00083     : mPetscStatusKnown(false),
00084       mpOriginalFactory(NULL)
00085 {
00086 #ifndef NDEBUG
00087     CheckForPetsc();
00088 #endif
00089     Vec vec=PetscTools::CreateVec(size, local);
00090     CalculateOwnership(vec);
00091     VecDestroy(vec);
00092 }
00093 
00094 DistributedVectorFactory::DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory)
00095     : mPetscStatusKnown(false),
00096       mpOriginalFactory(pOriginalFactory)
00097 {
00098     assert(mpOriginalFactory != NULL);
00099     //Normally called when mpOriginalFactory->GetNumProcs() != PetscTools::GetNumProcs()
00100     //so ignore mpOriginalFactory->GetLocalOwnership()
00101     Vec vec=PetscTools::CreateVec(mpOriginalFactory->GetProblemSize());
00102     
00103     CalculateOwnership(vec);
00104     VecDestroy(vec);
00105 }
00106 
00107 DistributedVectorFactory::DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs)
00108     : mLo(lo),
00109       mHi(hi),
00110       mProblemSize(size),
00111       mNumProcs(numProcs),
00112       mPetscStatusKnown(false),
00113       mpOriginalFactory(NULL)
00114 {
00115 #ifndef NDEBUG
00116     CheckForPetsc();
00117 #endif
00118 }
00119 
00120 DistributedVectorFactory::~DistributedVectorFactory()
00121 {
00122     delete mpOriginalFactory;
00123 }
00124 
00125 
00126 void DistributedVectorFactory::CheckForPetsc()
00127 {
00128     assert(mPetscStatusKnown==false);
00129     PetscTruth petsc_is_initialised;
00130     PetscInitialized(&petsc_is_initialised);
00131 
00132     //Tripping this assertion means that PETSc and MPI weren't intialised
00133     //A unit test should include the global fixture:
00134     //#include "PetscSetupAndFinalize.hpp"
00135     assert(petsc_is_initialised);
00136     mPetscStatusKnown=true;
00137 }
00138 
00139 bool DistributedVectorFactory::IsGlobalIndexLocal(unsigned globalIndex)
00140 {
00141     return (mLo<=globalIndex && globalIndex<mHi);
00142 }
00143 
00144 Vec DistributedVectorFactory::CreateVec()
00145 {
00146     Vec vec=PetscTools::CreateVec(mProblemSize, mHi-mLo);
00147     return vec;
00148 }
00149 
00150 Vec DistributedVectorFactory::CreateVec(unsigned stride)
00151 {
00152     Vec vec;
00153     VecCreateMPI(PETSC_COMM_WORLD, stride*(mHi-mLo), stride*mProblemSize, &vec);
00154     return vec;
00155 }
00156 
00157 DistributedVector DistributedVectorFactory::CreateDistributedVector(Vec vec)
00158 {
00159     DistributedVector dist_vector(vec, this);
00160     return dist_vector;
00161 }
00162 
00163 std::vector<unsigned> &DistributedVectorFactory::rGetGlobalLows()
00164 {
00165     if (mGlobalLows.size() != PetscTools::GetNumProcs())
00166     {
00167         assert( mGlobalLows.empty());
00168         mGlobalLows.resize(PetscTools::GetNumProcs());
00169         //Exchange data
00170         MPI_Allgather( &mLo, 1, MPI_UNSIGNED, &mGlobalLows[0], 1, MPI_UNSIGNED, PETSC_COMM_WORLD);
00171       }
00172     
00173     return  mGlobalLows;
00174 }
00175 // Serialization for Boost >= 1.36
00176 #include "SerializationExportWrapperForCpp.hpp"
00177 CHASTE_CLASS_EXPORT(DistributedVectorFactory)

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