00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00050 static unsigned mLo;
00052 static unsigned mHi;
00054 static unsigned mGlobalHi;
00056 static bool mPetscStatusKnown;
00057
00058
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
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
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