DistributedVectorFactory.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 void DistributedVectorFactory::CalculateOwnership(Vec vec)
00034 {
00035 #ifndef NDEBUG
00036     if (!mPetscStatusKnown)
00037     {
00038         CheckForPetsc();
00039     }
00040 #endif
00041     // calculate my range
00042     PetscInt petsc_lo, petsc_hi;
00043     VecGetOwnershipRange(vec, &petsc_lo, &petsc_hi);
00044     mLo = (unsigned)petsc_lo;
00045     mHi = (unsigned)petsc_hi;
00046     // vector size
00047     PetscInt size;
00048     VecGetSize(vec, &size);
00049     mProblemSize = (unsigned) size;
00050 }
00051 
00052 DistributedVectorFactory::DistributedVectorFactory(Vec vec) : mPetscStatusKnown(false)
00053 {
00054     CalculateOwnership(vec);
00055 }
00056 
00057 DistributedVectorFactory::DistributedVectorFactory(unsigned size, PetscInt local) : mPetscStatusKnown(false)
00058 {
00059 #ifndef NDEBUG
00060     if (!mPetscStatusKnown)
00061     {
00062         CheckForPetsc();
00063     }
00064 #endif
00065     Vec vec;
00066     VecCreate(PETSC_COMM_WORLD, &vec);
00067     VecSetSizes(vec, local, size);
00068     VecSetFromOptions(vec);
00069     CalculateOwnership(vec);
00070     VecDestroy(vec);    
00071 }
00072 
00073 
00074 void DistributedVectorFactory::CheckForPetsc()
00075 {
00076     assert(mPetscStatusKnown==false);
00077     PetscTruth petsc_is_initialised;
00078     PetscInitialized(&petsc_is_initialised);
00079 
00080     //Tripping this assertion means that PETSc and MPI weren't intialised
00081     //A unit test should include the global fixture:
00082     //#include "PetscSetupAndFinalize.hpp"
00083     assert(petsc_is_initialised);
00084     mPetscStatusKnown=true;
00085 }
00086 
00087 bool DistributedVectorFactory::IsGlobalIndexLocal(unsigned globalIndex)
00088 {
00089     return (mLo<=globalIndex && globalIndex<mHi);
00090 }
00091 
00092 Vec DistributedVectorFactory::CreateVec()
00093 {
00094     Vec vec;
00095     VecCreate(PETSC_COMM_WORLD, &vec);
00096     VecSetSizes(vec, mHi-mLo, mProblemSize);
00097     VecSetFromOptions(vec);
00098     return vec;
00099 }
00100 
00101 Vec DistributedVectorFactory::CreateVec(unsigned stride)
00102 {
00103     Vec vec;
00104     VecCreateMPI(PETSC_COMM_WORLD, stride*(mHi-mLo), stride*mProblemSize, &vec);
00105     return vec;
00106 }
00107 
00108 DistributedVector DistributedVectorFactory::CreateDistributedVector(Vec vec)
00109 {
00110     DistributedVector dist_vector(vec, this); 
00111     return dist_vector;
00112 }

Generated on Tue Aug 4 16:10:21 2009 for Chaste by  doxygen 1.5.5