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 00030 #ifndef DISTRIBUTEDVECTOR_HPP_ 00031 #define DISTRIBUTEDVECTOR_HPP_ 00032 00033 #include <vector> 00034 #include <petscvec.h> 00035 #include <iostream> 00036 #include <cassert> 00037 00038 #include "DistributedVectorException.hpp" 00039 00040 class DistributedVectorFactory; 00041 00048 class DistributedVector 00049 { 00050 private: 00051 friend class TestDistributedVector; 00052 00053 // Data global to all vectors 00054 00056 unsigned mLo; 00057 00059 unsigned mHi; 00060 00062 unsigned mProblemSize; 00063 00064 // Data local to a single vector 00065 00067 unsigned mSizeMultiplier; 00068 00070 Vec mVec; 00071 00073 double* mpVec; 00074 00079 DistributedVectorFactory* mpFactory; 00080 00081 public: 00082 00088 bool IsGlobalIndexLocal(unsigned globalIndex); 00089 00099 DistributedVector(Vec vec, DistributedVectorFactory* pFactory); 00100 00104 unsigned GetHigh() const 00105 { 00106 return mHi; 00107 } 00108 00112 unsigned GetLow() const 00113 { 00114 return mLo; 00115 } 00116 00120 DistributedVectorFactory* GetFactory() 00121 { 00122 return mpFactory; 00123 } 00124 00132 double& operator[](unsigned globalIndex) throw (DistributedVectorException); 00133 00139 void Restore(); 00140 00145 class Iterator 00146 { 00147 public: 00148 unsigned Local; 00149 unsigned Global; 00156 bool operator!=(const Iterator& rOther); 00157 00159 Iterator& operator++(); 00160 }; 00161 00169 class Stripe 00170 { 00171 unsigned mStride; 00172 unsigned mStripe; 00173 double* mpVec; 00174 unsigned mLo; 00175 unsigned mHi; 00176 DistributedVectorFactory* mpFactory; 00178 public: 00185 Stripe(DistributedVector parallelVec, unsigned stripe) 00186 { 00187 mStride = parallelVec.mSizeMultiplier; 00188 mStripe = stripe; 00189 assert(mStripe < mStride); 00190 mpVec = parallelVec.mpVec; 00191 mLo = parallelVec.GetLow(); 00192 mHi = parallelVec.GetHigh(); 00193 mpFactory = parallelVec.GetFactory(); 00194 } 00195 00199 DistributedVectorFactory* GetFactory() 00200 { 00201 return mpFactory; 00202 } 00203 00212 double& operator[](unsigned globalIndex) throw (DistributedVectorException) 00213 { 00214 if (mLo <= globalIndex && globalIndex < mHi) 00215 { 00216 return mpVec[(globalIndex - mLo)*mStride + mStripe]; 00217 } 00218 throw DistributedVectorException(); 00219 } 00220 00225 double& operator[](Iterator index) throw (DistributedVectorException) 00226 { 00227 return mpVec[index.Local*mStride + mStripe]; 00228 } 00229 00230 }; 00231 00239 class Chunk 00240 { 00241 unsigned mOffset; 00242 double* mpVec; 00243 unsigned mLo; 00244 unsigned mHi; 00246 public: 00253 Chunk(DistributedVector parallelVec, unsigned chunk) 00254 { 00255 assert(chunk < parallelVec.mSizeMultiplier); 00256 mLo = parallelVec.GetLow(); 00257 mHi = parallelVec.GetHigh(); 00258 mOffset = chunk * (mHi - mLo); 00259 mpVec = parallelVec.mpVec; 00260 } 00261 00270 double& operator[](unsigned globalIndex) throw (DistributedVectorException) 00271 { 00272 if (mLo <= globalIndex && globalIndex < mHi) 00273 { 00274 //localIndex = globalIndex - mLo 00275 return mpVec[mOffset + globalIndex - mLo]; 00276 } 00277 throw DistributedVectorException(); 00278 } 00279 00284 double& operator[](Iterator index) throw (DistributedVectorException) 00285 { 00286 return mpVec[mOffset + index.Local]; 00287 } 00288 00289 }; 00290 00295 Iterator Begin(); 00296 00301 Iterator End(); 00302 00308 double& operator[](Iterator index) throw (DistributedVectorException); 00309 }; 00310 00311 #endif /*DISTRIBUTEDVECTOR_HPP_*/