Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
DistributedVectorFactory.cpp
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#include <cassert>
37
38#include "DistributedVectorFactory.hpp"
39#include "PetscTools.hpp"
40
41// Initialise static data
43
45{
46#ifndef NDEBUG
48 {
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{
86}
87
89 : mPetscStatusKnown(false),
90 mpOriginalFactory(nullptr)
91{
92#ifndef NDEBUG
94#endif
95 Vec vec = PetscTools::CreateVec(size, local);
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
114}
115
116DistributedVectorFactory::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
126#endif
127}
128
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
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, CHASTE_PETSC_NULLPTR/*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
#define EXCEPTION(message)
#define CHASTE_CLASS_EXPORT(T)
DistributedVector CreateDistributedVector(Vec vec, bool readOnly=false)
std::vector< unsigned > mGlobalLows
std::vector< unsigned > & rGetGlobalLows()
void SetFromFactory(DistributedVectorFactory *pFactory)
DistributedVectorFactory * mpOriginalFactory
bool IsGlobalIndexLocal(unsigned globalIndex)
static void Destroy(Vec &rVec)
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
static unsigned GetNumProcs()