DistributedVector.hpp

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 #ifndef DISTRIBUTEDVECTOR_HPP_
00031 #define DISTRIBUTEDVECTOR_HPP_
00032 
00033 #include <vector>
00034 #include <petscvec.h>
00035 #include <iostream>
00036 #include <cassert>
00037 #include "DistributedVectorException.hpp"
00038 
00045 class DistributedVector
00046 {
00047 private:
00048     // Data global to all vectors.
00050     static unsigned mLo;
00052     static unsigned mHi;
00054     static unsigned mGlobalHi;
00056     static bool mPetscStatusKnown;
00057 
00058     // Data local to a single vector.
00060     unsigned mNumChunks;
00062     Vec mVec;
00064     double *mpVec;
00065 
00069     static void CheckForPetsc();
00070 
00071 public:
00072 
00079     static void SetProblemSizePerProcessor(unsigned size, PetscInt local);
00080 
00086     static void SetProblemSize(unsigned size);
00087 
00093     static void SetProblemSize(Vec vec);
00094 
00098     static unsigned GetProblemSize();
00099 
00105     static bool IsGlobalIndexLocal(unsigned globalIndex);
00106 
00110     static Vec CreateVec();
00111 
00117     static Vec CreateVec(unsigned stride);
00118 
00127     DistributedVector(Vec vec);
00128 
00136     double& operator[](unsigned globalIndex) throw (DistributedVectorException);
00137 
00143     void Restore();
00144 
00149     class Iterator
00150     {
00151     public:
00152         unsigned Local;  
00153         unsigned Global; 
00160         bool operator!=(const Iterator& other);
00161 
00163         Iterator& operator++();
00164     };
00165 
00173     class Stripe
00174     {
00175     public:
00176         unsigned mStride;
00177         unsigned mStripe;
00178         double *mpVec;
00179 
00186         Stripe(DistributedVector parallelVec, unsigned stripe)
00187         {
00188             mStride = parallelVec.mNumChunks;
00189             mStripe = stripe;
00190             assert(mStripe < mStride);
00191             mpVec = parallelVec.mpVec;
00192         }
00193 
00201         double& operator[](unsigned globalIndex) throw (DistributedVectorException)
00202         {
00203             if (mLo<=globalIndex && globalIndex <mHi)
00204             {
00205                 return mpVec[(globalIndex - mLo)*mStride + mStripe];
00206             }
00207             throw DistributedVectorException();
00208         }
00209 
00214         double& operator[](Iterator index) throw (DistributedVectorException)
00215         {
00216             return mpVec[index.Local*mStride+mStripe];
00217         }
00218 
00219     };
00220 
00228     class Chunk
00229     {
00230     public:
00231         //unsigned mChunk;
00232         unsigned mOffset;  
00233         double *mpVec;
00234 
00240         Chunk(DistributedVector parallelVec, unsigned chunk)
00241         {
00242             assert(chunk<parallelVec.mNumChunks);
00243             mOffset = chunk*(parallelVec.mHi - parallelVec.mLo);
00244             mpVec = parallelVec.mpVec;
00245         }
00246 
00254         double& operator[](unsigned globalIndex) throw (DistributedVectorException)
00255         {
00256             if (mLo<=globalIndex && globalIndex <mHi)
00257             {
00258                 //localIndex = globalIndex - mLo
00259                 return mpVec[mOffset + globalIndex - mLo];
00260             }
00261             throw DistributedVectorException();
00262          }
00263 
00268         double& operator[](Iterator index) throw (DistributedVectorException)
00269         {
00270             return mpVec[mOffset + index.Local];
00271         }
00272 
00273     };
00274 
00279     static Iterator Begin();
00280 
00285     static Iterator End();
00286 
00292     double& operator[](Iterator index) throw (DistributedVectorException);
00293 };
00294 
00295 
00296 
00297 #endif /*DISTRIBUTEDVECTOR_HPP_*/

Generated on Wed Mar 18 12:51:50 2009 for Chaste by  doxygen 1.5.5