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