Chaste  Release::3.4
DistributedVector.hpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 
37 #ifndef DISTRIBUTEDVECTOR_HPP_
38 #define DISTRIBUTEDVECTOR_HPP_
39 
40 #include <vector>
41 #include <petscvec.h>
42 #include <iostream>
43 #include <cassert>
44 
45 #include "DistributedVectorException.hpp"
46 
48 
56 {
57 private:
58  friend class TestDistributedVector;
59 
60  // Data global to all vectors
61 
63  unsigned mLo;
64 
66  unsigned mHi;
67 
69  unsigned mProblemSize;
70 
71  // Data local to a single vector
72 
74  unsigned mSizeMultiplier;
75 
78 
80  double* mpVec;
81 
87 
93  bool mReadOnly;
94 
95 
96 public:
97 
104  bool IsGlobalIndexLocal(unsigned globalIndex);
105 
118  DistributedVector(Vec vec, DistributedVectorFactory* pFactory, bool readOnly=false);
119 
123  unsigned GetHigh() const
124  {
125  return mHi;
126  }
127 
131  unsigned GetLow() const
132  {
133  return mLo;
134  }
135 
140  {
141  return mpFactory;
142  }
143 
151  double& operator[](unsigned globalIndex) throw (DistributedVectorException);
152 
161  void Restore();
162 
167  class Iterator
168  {
169  public:
170  unsigned Local;
171  unsigned Global;
179  bool operator!=(const Iterator& rOther);
180 
184  Iterator& operator++();
185  };
186 
194  class Stripe
195  {
196  unsigned mStride;
197  unsigned mStripe;
198  double* mpVec;
199  unsigned mLo;
200  unsigned mHi;
203  public:
210  Stripe(DistributedVector parallelVec, unsigned stripe)
211  {
212  mStride = parallelVec.mSizeMultiplier;
213  mStripe = stripe;
214  assert(mStripe < mStride);
215  mpVec = parallelVec.mpVec;
216  mLo = parallelVec.GetLow();
217  mHi = parallelVec.GetHigh();
218  mpFactory = parallelVec.GetFactory();
219  }
220 
225  {
226  return mpFactory;
227  }
228 
237  double& operator[](unsigned globalIndex) throw (DistributedVectorException)
238  {
239  if (mLo <= globalIndex && globalIndex < mHi)
240  {
241  return mpVec[(globalIndex - mLo)*mStride + mStripe];
242  }
244  }
245 
251  {
252  return mpVec[index.Local*mStride + mStripe];
253  }
254 
255  };
256 
264  class Chunk
265  {
266  unsigned mOffset;
267  double* mpVec;
268  unsigned mLo;
269  unsigned mHi;
271  public:
278  Chunk(DistributedVector parallelVec, unsigned chunk)
279  {
280  assert(chunk < parallelVec.mSizeMultiplier);
281  mLo = parallelVec.GetLow();
282  mHi = parallelVec.GetHigh();
283  mOffset = chunk * (mHi - mLo);
284  mpVec = parallelVec.mpVec;
285  }
286 
295  double& operator[](unsigned globalIndex) throw (DistributedVectorException)
296  {
297  if (mLo <= globalIndex && globalIndex < mHi)
298  {
299  //localIndex = globalIndex - mLo
300  return mpVec[mOffset + globalIndex - mLo];
301  }
303  }
304 
310  {
311  return mpVec[mOffset + index.Local];
312  }
313 
314  };
315 
320  Iterator Begin();
321 
326  Iterator End();
327 
333  double& operator[](Iterator index) throw (DistributedVectorException);
334 };
335 
336 #endif /*DISTRIBUTEDVECTOR_HPP_*/
DistributedVector(Vec vec, DistributedVectorFactory *pFactory, bool readOnly=false)
DistributedVectorFactory * mpFactory
DistributedVectorFactory * GetFactory()
double & operator[](unsigned globalIndex)
double & operator[](Iterator index)
DistributedVectorFactory * GetFactory()
bool operator!=(const Iterator &rOther)
bool IsGlobalIndexLocal(unsigned globalIndex)
unsigned GetLow() const
unsigned GetHigh() const
double & operator[](Iterator index)
Stripe(DistributedVector parallelVec, unsigned stripe)
double & operator[](unsigned globalIndex)
double & operator[](unsigned globalIndex)
DistributedVectorFactory * mpFactory
Chunk(DistributedVector parallelVec, unsigned chunk)