Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
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
58#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2 || PETSC_VERSION_MAJOR<3 ) // Before PETSc 3.2
62typedef PetscTruth PetscBool;
70#define PETSC_DESTROY_PARAM(x) x
71#else
79#define PETSC_DESTROY_PARAM(x) &x
80#endif
81
91#define TRY_IF_MASTER(method) { \
92 if (PetscTools::AmMaster()) \
93 { try { \
94 method; \
95 } catch (Exception& e) { \
96 PetscTools::ReplicateException(true);\
97 throw(e); \
98 } } \
99 PetscTools::ReplicateException(false); \
100 }
101
112{
113private:
114
117
119 static unsigned mNumProcessors;
120
122 static unsigned mRank;
123
125 static bool mIsolateProcesses;
126
128 static inline void CheckCache()
129 {
130 if (mNumProcessors == 0)
131 {
132 ResetCache();
133 }
134 }
135
136public:
137
141 static const unsigned MASTER_RANK=0;
142
147 static void ResetCache();
148
152 static bool IsInitialised();
153
157 static bool IsSequential();
158
162 static bool IsParallel();
163
167 static bool IsIsolated();
168
172 static unsigned GetNumProcs();
173
179 static unsigned GetMyRank();
180
186 static bool AmMaster();
187
193 static bool AmTopMost();
194
201 static void Barrier(const std::string callerId="");
202
210 static void BeginRoundRobin();
211
215 static void EndRoundRobin();
216
226 static void IsolateProcesses(bool isolate=true);
227
233 static MPI_Comm GetWorld();
234
243 static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
244
251 static Vec CreateVec(std::vector<double> data);
252
261 static Vec CreateAndSetVec(int size, double value);
262
281 static void SetupMat(Mat& rMat, int numRows, int numColumns,
282 unsigned rowPreallocation,
283 int numLocalRows=PETSC_DECIDE,
284 int numLocalColumns=PETSC_DECIDE,
285 bool ignoreOffProcEntries=true,
286 bool newAllocationError=true);
287
294 static bool ReplicateBool(bool flag);
295
302 static void ReplicateException(bool flag);
303
310 static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
311
318 static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
319
327 static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=nullptr);
328
336 static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=nullptr);
337
343 static bool HasParMetis();
344
352 static inline void Destroy(Vec& rVec)
353 {
354#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
355 VecDestroy(&rVec);
356#else
357 VecDestroy(rVec);
358#endif
359 }
360
368 static inline void Destroy(Mat& rMat)
369 {
370#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
371 MatDestroy(&rMat);
372#else
373 MatDestroy(rMat);
374#endif
375 }
376
384 static inline void SetOption(const char* pOptionName, const char* pOptionValue)
385 {
386 // If this option turns on logging, PETSc needs to be made aware in different ways for different versions
387 // See #2933 for details
388 const std::string str_option_name(pOptionName);
389 if (str_option_name.find("log") != std::string::npos)
390 {
391#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR == 6) // PETSc 3.6
392 PetscLogBegin();
393#elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7) // PETSc 3.7 or later
394 PetscLogDefaultBegin();
395#endif
396 }
397
398#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7) // PETSc 3.7 or later
399 PetscOptionsSetValue(NULL, pOptionName, pOptionValue);
400#else
401 PetscOptionsSetValue(pOptionName, pOptionValue);
402#endif
403 }
404
405#if PETSC_VERSION_GE(3, 11, 2) // PETSc 3.11.2 or newer
418 static PetscErrorCode ChasteMatCopy(Mat A, Mat B, MatStructure str);
419#endif
420};
421
422#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()