Chaste  Release::2018.1
DistributedVectorFactory.cpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 #include <cassert>
37 
38 #include "DistributedVectorFactory.hpp"
39 #include "PetscTools.hpp"
40 
41 // Initialise static data
43 
45 {
46 #ifndef NDEBUG
47  if (!mPetscStatusKnown)
48  {
49  CheckForPetsc();
50  }
51 #endif
52 
53  // Calculate my range
54  PetscInt petsc_lo, petsc_hi;
55  VecGetOwnershipRange(vec, &petsc_lo, &petsc_hi);
56  mGlobalLows.clear();
57  mLo = (unsigned)petsc_lo;
58  mHi = (unsigned)petsc_hi;
59  // vector size
60  PetscInt size;
61  VecGetSize(vec, &size);
62  mProblemSize = (unsigned) size;
64 }
65 
67 {
68  if (pFactory->GetNumProcs() != mNumProcs)
69  {
70  EXCEPTION("Cannot set from a factory for a different number of processes.");
71  }
72  if (pFactory->GetProblemSize() != mProblemSize)
73  {
74  EXCEPTION("Cannot set from a factory for a different problem size.");
75  }
76  mGlobalLows.clear();
77  mLo = pFactory->GetLow();
78  mHi = pFactory->GetHigh();
79 }
80 
82  : mPetscStatusKnown(false),
83  mpOriginalFactory(nullptr)
84 {
85  CalculateOwnership(vec);
86 }
87 
89  : mPetscStatusKnown(false),
90  mpOriginalFactory(nullptr)
91 {
92 #ifndef NDEBUG
93  CheckForPetsc();
94 #endif
95  Vec vec = PetscTools::CreateVec(size, local);
96  CalculateOwnership(vec);
98 }
99 
101  : mPetscStatusKnown(false),
102  mpOriginalFactory(pOriginalFactory)
103 {
104  assert(mpOriginalFactory != nullptr);
105 
106  /*
107  * Normally called when mpOriginalFactory->GetNumProcs() != PetscTools::GetNumProcs()
108  * so ignore mpOriginalFactory->GetLocalOwnership()
109  */
111 
112  CalculateOwnership(vec);
113  PetscTools::Destroy(vec);
114 }
115 
116 DistributedVectorFactory::DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs)
117  : mLo(lo),
118  mHi(hi),
119  mProblemSize(size),
120  mNumProcs(numProcs),
121  mPetscStatusKnown(false),
122  mpOriginalFactory(nullptr)
123 {
124 #ifndef NDEBUG
125  CheckForPetsc();
126 #endif
127 }
128 
130 {
131  delete mpOriginalFactory;
132 }
133 
135 {
136  assert(mPetscStatusKnown==false);
137  PetscBool petsc_is_initialised;
138  PetscInitialized(&petsc_is_initialised);
139 
140  /*
141  * Tripping this assertion means that PETSc and MPI weren't intialised.
142  * A unit test should include the global fixture:
143  * #include "PetscSetupAndFinalize.hpp"
144  */
145  assert(petsc_is_initialised);
146  mPetscStatusKnown = true;
147 }
148 
150 {
151  return (mLo<=globalIndex && globalIndex<mHi);
152 }
153 
155 {
157  return vec;
158 }
159 
161 {
162  Vec vec;
163 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 3) //PETSc 3.3 or later
164  //In PETSc 3.3 we cannot alter the stride (block size) after it has been created
165  //There is no VecCreateMPI with block size
166  VecCreate(PETSC_COMM_WORLD, &vec);
167  VecSetBlockSize(vec, stride);
168  VecSetSizes(vec, stride*(mHi-mLo), stride*mProblemSize);
169  VecSetType(vec,VECMPI);
170  //VecCreateMPIWithArray(PETSC_COMM_WORLD, stride, stride*(mHi-mLo), stride*mProblemSize, PETSC_NULL/*No array*/, &vec);
171 #else
172  VecCreateMPI(PETSC_COMM_WORLD, stride*(mHi-mLo), stride*mProblemSize, &vec);
173  VecSetBlockSize(vec, stride);
174 #endif
175 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x
176  VecSetOption(vec, VEC_IGNORE_OFF_PROC_ENTRIES, PETSC_TRUE);
177 #else
178  VecSetOption(vec, VEC_IGNORE_OFF_PROC_ENTRIES);
179 #endif
180  return vec;
181 }
182 
184 {
185  DistributedVector dist_vector(vec, this, readOnly);
186  return dist_vector;
187 }
188 
190 {
191  if (mGlobalLows.size() != PetscTools::GetNumProcs())
192  {
193  assert( mGlobalLows.empty());
195 
196  // Exchange data
197  MPI_Allgather( &mLo, 1, MPI_UNSIGNED, &mGlobalLows[0], 1, MPI_UNSIGNED, PETSC_COMM_WORLD);
198  }
199 
200  return mGlobalLows;
201 }
202 
203 // Serialization for Boost >= 1.36
std::vector< unsigned > & rGetGlobalLows()
std::vector< unsigned > mGlobalLows
DistributedVector CreateDistributedVector(Vec vec, bool readOnly=false)
#define EXCEPTION(message)
Definition: Exception.hpp:143
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
Definition: PetscTools.cpp:214
bool IsGlobalIndexLocal(unsigned globalIndex)
void SetFromFactory(DistributedVectorFactory *pFactory)
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
DistributedVectorFactory * mpOriginalFactory
#define CHASTE_CLASS_EXPORT(T)
static unsigned GetNumProcs()
Definition: PetscTools.cpp:108