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