Chaste Release::3.1
DistributedVector.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 
00037 #ifndef DISTRIBUTEDVECTOR_HPP_
00038 #define DISTRIBUTEDVECTOR_HPP_
00039 
00040 #include <vector>
00041 #include <petscvec.h>
00042 #include <iostream>
00043 #include <cassert>
00044 
00045 #include "DistributedVectorException.hpp"
00046 
00047 class DistributedVectorFactory;
00048 
00055 class DistributedVector
00056 {
00057 private:
00058     friend class TestDistributedVector;
00059 
00060     // Data global to all vectors
00061 
00063     unsigned mLo;
00064 
00066     unsigned mHi;
00067 
00069     unsigned mProblemSize;
00070 
00071     // Data local to a single vector
00072 
00074     unsigned mSizeMultiplier;
00075 
00077     Vec mVec;
00078 
00080     double* mpVec;
00081 
00086     DistributedVectorFactory* mpFactory;
00087 
00088 public:
00089 
00095     bool IsGlobalIndexLocal(unsigned globalIndex);
00096 
00106     DistributedVector(Vec vec, DistributedVectorFactory* pFactory);
00107 
00111     unsigned GetHigh() const
00112     {
00113         return mHi;
00114     }
00115 
00119     unsigned GetLow() const
00120     {
00121         return mLo;
00122     }
00123 
00127     DistributedVectorFactory* GetFactory()
00128     {
00129         return mpFactory;
00130     }
00131 
00139     double& operator[](unsigned globalIndex) throw (DistributedVectorException);
00140 
00146     void Restore();
00147 
00152     class Iterator
00153     {
00154     public:
00155         unsigned Local;  
00156         unsigned Global; 
00163         bool operator!=(const Iterator& rOther);
00164 
00166         Iterator& operator++();
00167     };
00168 
00176     class Stripe
00177     {
00178         unsigned mStride; 
00179         unsigned mStripe; 
00180         double* mpVec;    
00181         unsigned mLo;     
00182         unsigned mHi;     
00183         DistributedVectorFactory* mpFactory; 
00185     public:
00192         Stripe(DistributedVector parallelVec, unsigned stripe)
00193         {
00194             mStride = parallelVec.mSizeMultiplier;
00195             mStripe = stripe;
00196             assert(mStripe < mStride);
00197             mpVec = parallelVec.mpVec;
00198             mLo = parallelVec.GetLow();
00199             mHi = parallelVec.GetHigh();
00200             mpFactory = parallelVec.GetFactory();
00201         }
00202 
00206         DistributedVectorFactory* GetFactory()
00207         {
00208             return mpFactory;
00209         }
00210 
00219         double& operator[](unsigned globalIndex) throw (DistributedVectorException)
00220         {
00221             if (mLo <= globalIndex && globalIndex < mHi)
00222             {
00223                 return mpVec[(globalIndex - mLo)*mStride + mStripe];
00224             }
00225             throw DistributedVectorException();
00226         }
00227 
00232         double& operator[](Iterator index) throw (DistributedVectorException)
00233         {
00234             return mpVec[index.Local*mStride + mStripe];
00235         }
00236 
00237     };
00238 
00246     class Chunk
00247     {
00248         unsigned mOffset; 
00249         double* mpVec;    
00250         unsigned mLo;     
00251         unsigned mHi;     
00253     public:
00260         Chunk(DistributedVector parallelVec, unsigned chunk)
00261         {
00262             assert(chunk < parallelVec.mSizeMultiplier);
00263             mLo = parallelVec.GetLow();
00264             mHi = parallelVec.GetHigh();
00265             mOffset = chunk * (mHi - mLo);
00266             mpVec = parallelVec.mpVec;
00267         }
00268 
00277         double& operator[](unsigned globalIndex) throw (DistributedVectorException)
00278         {
00279             if (mLo <= globalIndex && globalIndex < mHi)
00280             {
00281                 //localIndex = globalIndex - mLo
00282                 return mpVec[mOffset + globalIndex - mLo];
00283             }
00284             throw DistributedVectorException();
00285          }
00286 
00291         double& operator[](Iterator index) throw (DistributedVectorException)
00292         {
00293             return mpVec[mOffset + index.Local];
00294         }
00295 
00296     };
00297 
00302     Iterator Begin();
00303 
00308     Iterator End();
00309 
00315     double& operator[](Iterator index) throw (DistributedVectorException);
00316 };
00317 
00318 #endif /*DISTRIBUTEDVECTOR_HPP_*/