ReplicatableVector.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 
00030 #include "ReplicatableVector.hpp"
00031 
00032 #include <cassert>
00033 
00034 // Private methods
00035 
00036 void ReplicatableVector::RemovePetscContext()
00037 {
00038     if (mToAll != NULL)
00039     {
00040         VecScatterDestroy(mToAll);
00041         mToAll = NULL;
00042     }
00043 
00044     if (mReplicated != NULL)
00045     {
00046         VecDestroy(mReplicated);
00047         mReplicated = NULL;
00048     }
00049 
00050     if (mDistributed != NULL)
00051     {
00052         VecDestroy(mDistributed);
00053         mDistributed = NULL;
00054     }
00055 }
00056 
00057 // Constructors & destructors
00058 
00059 ReplicatableVector::ReplicatableVector()
00060     : mToAll(NULL),
00061       mReplicated(NULL),
00062       mDistributed(NULL)
00063 {
00064 }
00065 
00066 ReplicatableVector::ReplicatableVector(Vec vec)
00067     : mToAll(NULL),
00068       mReplicated(NULL),
00069       mDistributed(NULL)
00070 {
00071     ReplicatePetscVector(vec);
00072 }
00073 
00074 ReplicatableVector::ReplicatableVector(unsigned size)
00075     : mToAll(NULL),
00076       mReplicated(NULL),
00077       mDistributed(NULL)
00078 {
00079     resize(size);
00080 }
00081 
00082 ReplicatableVector::~ReplicatableVector()
00083 {
00084     RemovePetscContext();
00085 }
00086 
00087 
00088 // Vector interface methods
00089 
00090 unsigned ReplicatableVector::size()
00091 {
00092     return mData.size();
00093 }
00094 
00095 void ReplicatableVector::resize(unsigned size)
00096 {
00097     // PETSc stuff will be out of date
00098     RemovePetscContext();
00099     mData.resize(size);
00100 }
00101 
00102 double& ReplicatableVector::operator[](unsigned index)
00103 {
00104     assert(index < mData.size());
00105     return mData[index];
00106 }
00107 
00108 
00109 // The workhorse methods
00110 
00111 void ReplicatableVector::Replicate(unsigned lo, unsigned hi)
00112 {
00113     // Copy information into a PetSC vector
00114     if (mDistributed==NULL)
00115     {
00116         VecCreateMPI(PETSC_COMM_WORLD, hi-lo, this->size(), &mDistributed);
00117     }
00118 
00119     double *p_distributed;
00120     VecGetArray(mDistributed, &p_distributed);
00121     for (unsigned global_index=lo; global_index<hi; global_index++)
00122     {
00123         p_distributed[ (global_index-lo) ] = mData[global_index];
00124     }
00125     VecAssemblyBegin(mDistributed);
00126     VecAssemblyEnd(mDistributed);
00127 
00128     // Now do the real replication
00129     ReplicatePetscVector(mDistributed);
00130 }
00131 
00132 void ReplicatableVector::ReplicatePetscVector(Vec vec)
00133 {
00134     // If the size has changed then we'll need to make a new context
00135     PetscInt isize;
00136     VecGetSize(vec, &isize);
00137     unsigned size=isize;
00138 
00139     if (this->size() != size)
00140     {
00141         resize(size);
00142     }
00143     if (mReplicated == NULL)
00144     {
00145         // This creates mReplicated (the scatter context) and mReplicated (to store values)
00146         VecScatterCreateToAll(vec, &mToAll, &mReplicated);
00147     }
00148 
00149     // Replicate the data
00150 #if (PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)
00151     VecScatterBegin(mToAll, vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD);
00152     VecScatterEnd  (mToAll, vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD);
00153 #else
00154     VecScatterBegin(vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD, mToAll);
00155     VecScatterEnd  (vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD, mToAll);
00156 #endif
00157 
00158     // Information is now in mReplicated PETSc vector
00159     // Copy into mData
00160     double *p_replicated;
00161     VecGetArray(mReplicated, &p_replicated);
00162     for (unsigned i=0; i<size; i++)
00163     {
00164         mData[i] = p_replicated[i];
00165     }
00166 }

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