Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
DistributedVector.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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{
57private:
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
75
78
80 double* mpVec;
81
87
94
95
96public:
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
143
151 double& operator[](unsigned globalIndex);
152
161 void Restore();
162
168 {
169 public:
170 unsigned Local;
171 unsigned Global;
179 bool operator!=(const Iterator& rOther);
180
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
228
237 double& operator[](unsigned globalIndex)
238 {
239 if (mLo <= globalIndex && globalIndex < mHi)
240 {
241 return mpVec[(globalIndex - mLo)*mStride + mStripe];
242 }
244 }
245
250 double& operator[](Iterator index)
251 {
252 return mpVec[index.Local*mStride + mStripe];
253 }
254 };
255
263 class Chunk
264 {
265 unsigned mOffset;
266 double* mpVec;
267 unsigned mLo;
268 unsigned mHi;
270 public:
277 Chunk(DistributedVector parallelVec, unsigned chunk)
278 {
279 assert(chunk < parallelVec.mSizeMultiplier);
280 mLo = parallelVec.GetLow();
281 mHi = parallelVec.GetHigh();
282 mOffset = chunk * (mHi - mLo);
283 mpVec = parallelVec.mpVec;
284 }
285
294 double& operator[](unsigned globalIndex)
295 {
296 if (mLo <= globalIndex && globalIndex < mHi)
297 {
298 //localIndex = globalIndex - mLo
299 return mpVec[mOffset + globalIndex - mLo];
300 }
302 }
303
308 double& operator[](Iterator index)
309 {
310 return mpVec[mOffset + index.Local];
311 }
312 };
313
318 Iterator Begin();
319
324 Iterator End();
325
331 double& operator[](Iterator index);
332};
333
334#endif /*DISTRIBUTEDVECTOR_HPP_*/
double & operator[](Iterator index)
Chunk(DistributedVector parallelVec, unsigned chunk)
double & operator[](unsigned globalIndex)
bool operator!=(const Iterator &rOther)
double & operator[](unsigned globalIndex)
double & operator[](Iterator index)
DistributedVectorFactory * GetFactory()
Stripe(DistributedVector parallelVec, unsigned stripe)
DistributedVectorFactory * mpFactory
unsigned GetHigh() const
unsigned GetLow() const
double & operator[](unsigned globalIndex)
bool IsGlobalIndexLocal(unsigned globalIndex)
DistributedVectorFactory * GetFactory()
DistributedVectorFactory * mpFactory