Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
PetscTools.hpp
Go to the documentation of this file.
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#ifndef PETSCTOOLS_HPP_
37#define PETSCTOOLS_HPP_
38
44#include <string>
45#include <vector>
46#include <cstdlib> // For EXIT_FAILURE
47
48#include <petsc.h>
49#include <petscvec.h>
50#include <petscmat.h>
51#include <petscsys.h>
52
54#define EXIT_IF_PARALLEL if(PetscTools::IsParallel()){TS_TRACE("This test does not pass in parallel yet.");return;}
56#define EXIT_IF_SEQUENTIAL if(PetscTools::IsSequential()){TS_TRACE("This test is not meant to be executed in sequential.");return;}
57
66#if PETSC_VERSION_GE(3, 19, 0)
67#define CHASTE_PETSC_NULLPTR PETSC_NULLPTR
68#else
69#define CHASTE_PETSC_NULLPTR PETSC_NULL
70#endif
71
72#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2 || PETSC_VERSION_MAJOR<3 ) // Before PETSc 3.2
76typedef PetscTruth PetscBool;
84#define PETSC_DESTROY_PARAM(x) x
85#else
93#define PETSC_DESTROY_PARAM(x) &x
94#endif
95
105#define TRY_IF_MASTER(method) { \
106 if (PetscTools::AmMaster()) \
107 { try { \
108 method; \
109 } catch (Exception& e) { \
110 PetscTools::ReplicateException(true);\
111 throw(e); \
112 } } \
113 PetscTools::ReplicateException(false); \
114 }
115
126{
127private:
128
131
133 static unsigned mNumProcessors;
134
136 static unsigned mRank;
137
139 static bool mIsolateProcesses;
140
142 static inline void CheckCache()
143 {
144 if (mNumProcessors == 0)
145 {
146 ResetCache();
147 }
148 }
149
150public:
151
155 static const unsigned MASTER_RANK=0;
156
161 static void ResetCache();
162
166 static bool IsInitialised();
167
171 static bool IsSequential();
172
176 static bool IsParallel();
177
181 static bool IsIsolated();
182
186 static unsigned GetNumProcs();
187
193 static unsigned GetMyRank();
194
200 static bool AmMaster();
201
207 static bool AmTopMost();
208
215 static void Barrier(const std::string callerId="");
216
224 static void BeginRoundRobin();
225
229 static void EndRoundRobin();
230
240 static void IsolateProcesses(bool isolate=true);
241
247 static MPI_Comm GetWorld();
248
257 static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
258
265 static Vec CreateVec(std::vector<double> data);
266
275 static Vec CreateAndSetVec(int size, double value);
276
295 static void SetupMat(Mat& rMat, int numRows, int numColumns,
296 unsigned rowPreallocation,
297 int numLocalRows=PETSC_DECIDE,
298 int numLocalColumns=PETSC_DECIDE,
299 bool ignoreOffProcEntries=true,
300 bool newAllocationError=true);
301
308 static bool ReplicateBool(bool flag);
309
316 static void ReplicateException(bool flag);
317
324 static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
325
332 static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
333
341 static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=nullptr);
342
350 static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=nullptr);
351
357 static bool HasParMetis();
358
366 static inline void Destroy(Vec& rVec)
367 {
368#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
369 VecDestroy(&rVec);
370#else
371 VecDestroy(rVec);
372#endif
373 }
374
382 static inline void Destroy(Mat& rMat)
383 {
384#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
385 MatDestroy(&rMat);
386#else
387 MatDestroy(rMat);
388#endif
389 }
390
398 static inline void SetOption(const char* pOptionName, const char* pOptionValue)
399 {
400 // If this option turns on logging, PETSc needs to be made aware in different ways for different versions
401 // See #2933 for details
402 const std::string str_option_name(pOptionName);
403 if (str_option_name.find("log") != std::string::npos)
404 {
405#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR == 6) // PETSc 3.6
406 PetscLogBegin();
407#elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7) // PETSc 3.7 or later
408 PetscLogDefaultBegin();
409#endif
410 }
411
412#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7) // PETSc 3.7 or later
413 PetscOptionsSetValue(NULL, pOptionName, pOptionValue);
414#else
415 PetscOptionsSetValue(pOptionName, pOptionValue);
416#endif
417 }
418
419#if PETSC_VERSION_GE(3, 11, 2) // PETSc 3.11.2 or newer
432 static PetscErrorCode ChasteMatCopy(Mat A, Mat B, MatStructure str);
433#endif
434};
435
436#endif /*PETSCTOOLS_HPP_*/
static Vec CreateAndSetVec(int size, double value)
static void Destroy(Vec &rVec)
static bool ReplicateBool(bool flag)
static void DumpPetscObject(const Mat &rMat, const std::string &rOutputFileFullPath)
static MPI_Comm GetWorld()
static bool AmMaster()
static unsigned mNumProcessors
static void IsolateProcesses(bool isolate=true)
static bool AmTopMost()
static void ReadPetscObject(Mat &rMat, const std::string &rOutputFileFullPath, Vec rParallelLayout=nullptr)
static unsigned mRank
static bool mIsolateProcesses
static void Barrier(const std::string callerId="")
static bool IsParallel()
static bool IsSequential()
static bool mPetscIsInitialised
static void EndRoundRobin()
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
static void SetOption(const char *pOptionName, const char *pOptionValue)
static bool IsIsolated()
static unsigned GetMyRank()
static void ReplicateException(bool flag)
static bool IsInitialised()
static void BeginRoundRobin()
static const unsigned MASTER_RANK
static void SetupMat(Mat &rMat, int numRows, int numColumns, unsigned rowPreallocation, int numLocalRows=PETSC_DECIDE, int numLocalColumns=PETSC_DECIDE, bool ignoreOffProcEntries=true, bool newAllocationError=true)
static void ResetCache()
static unsigned GetNumProcs()
static bool HasParMetis()
static void Destroy(Mat &rMat)
static void CheckCache()