Chaste  Release::3.4
PetscTools.hpp
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2005-2016, 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 
53 #define EXIT_IF_PARALLEL if(PetscTools::IsParallel()){TS_TRACE("This test does not pass in parallel yet.");return;}
54 
55 #define EXIT_IF_SEQUENTIAL if(PetscTools::IsSequential()){TS_TRACE("This test is not meant to be executed in sequential.");return;}
56 
57 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2 || PETSC_VERSION_MAJOR<3 ) // Before PETSc 3.2
58 
61 typedef PetscTruth PetscBool;
69 #define PETSC_DESTROY_PARAM(x) x
70 #else
71 
78 #define PETSC_DESTROY_PARAM(x) &x
79 #endif
80 
90 #define TRY_IF_MASTER(method) { \
91  if (PetscTools::AmMaster()) \
92  { try { \
93  method; \
94  } catch (Exception& e) { \
95  PetscTools::ReplicateException(true);\
96  throw(e); \
97  } } \
98  PetscTools::ReplicateException(false); \
99  }
100 
111 {
112 private:
113 
115  static bool mPetscIsInitialised;
116 
118  static unsigned mNumProcessors;
119 
121  static unsigned mRank;
122 
124  static bool mIsolateProcesses;
125 
127  static inline void CheckCache()
128  {
129  if (mNumProcessors == 0)
130  {
131  ResetCache();
132  }
133  }
134 
135 public:
136 
140  static const unsigned MASTER_RANK=0;
141 
146  static void ResetCache();
147 
151  static bool IsInitialised();
152 
156  static bool IsSequential();
157 
161  static bool IsParallel();
162 
166  static bool IsIsolated();
167 
171  static unsigned GetNumProcs();
172 
178  static unsigned GetMyRank();
179 
185  static bool AmMaster();
186 
192  static bool AmTopMost();
193 
200  static void Barrier(const std::string callerId="");
201 
209  static void BeginRoundRobin();
210 
214  static void EndRoundRobin();
215 
225  static void IsolateProcesses(bool isolate=true);
226 
232  static MPI_Comm GetWorld();
233 
242  static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
243 
250  static Vec CreateVec(std::vector<double> data);
251 
260  static Vec CreateAndSetVec(int size, double value);
261 
280  static void SetupMat(Mat& rMat, int numRows, int numColumns,
281  unsigned rowPreallocation,
282  int numLocalRows=PETSC_DECIDE,
283  int numLocalColumns=PETSC_DECIDE,
284  bool ignoreOffProcEntries=true,
285  bool newAllocationError=true);
286 
293  static bool ReplicateBool(bool flag);
294 
301  static void ReplicateException(bool flag);
302 
309  static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
310 
317  static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
318 
326  static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
327 
335  static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
336 
342  static bool HasParMetis();
343 
351  static inline void Destroy(Vec& rVec)
352  {
353 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
354  VecDestroy(&rVec);
355 #else
356  VecDestroy(rVec);
357 #endif
358  }
359 
367  static inline void Destroy(Mat& rMat)
368  {
369 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
370  MatDestroy(&rMat);
371 #else
372  MatDestroy(rMat);
373 #endif
374  }
375 };
376 
377 #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 ReadPetscObject(Mat &rMat, const std::string &rOutputFileFullPath, Vec rParallelLayout=NULL)
Definition: PetscTools.cpp:361
static void DumpPetscObject(const Mat &rMat, const std::string &rOutputFileFullPath)
Definition: PetscTools.cpp:333
PetscTruth PetscBool
Definition: PetscTools.hpp:61
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:118
static bool mPetscIsInitialised
Definition: PetscTools.hpp:115
static unsigned mRank
Definition: PetscTools.hpp:121
static bool IsSequential()
Definition: PetscTools.cpp:91
static void Destroy(Mat &rMat)
Definition: PetscTools.hpp:367
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:351
static bool IsParallel()
Definition: PetscTools.cpp:97
static bool IsIsolated()
Definition: PetscTools.cpp:103
static void CheckCache()
Definition: PetscTools.hpp:127
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:124
static unsigned GetMyRank()
Definition: PetscTools.cpp:114
static void ResetCache()
Definition: PetscTools.cpp:58
static const unsigned MASTER_RANK
Definition: PetscTools.hpp:140