Chaste  Release::2018.1
PetscTools.hpp
Go to the documentation of this file.
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 #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;}
55 
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
59 
62 typedef PetscTruth PetscBool;
70 #define PETSC_DESTROY_PARAM(x) x
71 #else
72 
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 {
113 private:
114 
116  static bool mPetscIsInitialised;
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 
136 public:
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 
406 #endif /*PETSCTOOLS_HPP_*/
static bool ReplicateBool(bool flag)
Definition: PetscTools.cpp:186
static void Barrier(const std::string callerId="")
Definition: PetscTools.cpp:134
static void DumpPetscObject(const Mat &rMat, const std::string &rOutputFileFullPath)
Definition: PetscTools.cpp:333
static void SetOption(const char *pOptionName, const char *pOptionValue)
Definition: PetscTools.hpp:384
PetscTruth PetscBool
Definition: PetscTools.hpp:62
static bool AmMaster()
Definition: PetscTools.cpp:120
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
Definition: PetscTools.cpp:214
static MPI_Comm GetWorld()
Definition: PetscTools.cpp:174
static unsigned mNumProcessors
Definition: PetscTools.hpp:119
static bool mPetscIsInitialised
Definition: PetscTools.hpp:116
static unsigned mRank
Definition: PetscTools.hpp:122
static bool IsSequential()
Definition: PetscTools.cpp:91
static void Destroy(Mat &rMat)
Definition: PetscTools.hpp:368
static void EndRoundRobin()
Definition: PetscTools.cpp:160
static bool HasParMetis()
Definition: PetscTools.cpp:445
static Vec CreateAndSetVec(int size, double value)
Definition: PetscTools.cpp:254
static void ReplicateException(bool flag)
Definition: PetscTools.cpp:198
static void BeginRoundRobin()
Definition: PetscTools.cpp:150
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)
Definition: PetscTools.cpp:268
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
static bool IsParallel()
Definition: PetscTools.cpp:97
static bool IsIsolated()
Definition: PetscTools.cpp:103
static void CheckCache()
Definition: PetscTools.hpp:128
static bool IsInitialised()
Definition: PetscTools.cpp:85
static bool AmTopMost()
Definition: PetscTools.cpp:126
static void IsolateProcesses(bool isolate=true)
Definition: PetscTools.cpp:169
static unsigned GetNumProcs()
Definition: PetscTools.cpp:108
static bool mIsolateProcesses
Definition: PetscTools.hpp:125
static unsigned GetMyRank()
Definition: PetscTools.cpp:114
static void ReadPetscObject(Mat &rMat, const std::string &rOutputFileFullPath, Vec rParallelLayout=nullptr)
Definition: PetscTools.cpp:361
static void ResetCache()
Definition: PetscTools.cpp:58
static const unsigned MASTER_RANK
Definition: PetscTools.hpp:141