PetscTools.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef PETSCTOOLS_HPP_
00037 #define PETSCTOOLS_HPP_
00038
00044 #include <string>
00045 #include <vector>
00046 #include <cstdlib>
00047
00048 #include <petsc.h>
00049 #include <petscvec.h>
00050 #include <petscmat.h>
00051
00053 #define EXIT_IF_PARALLEL if(PetscTools::IsParallel()){TS_TRACE("This test does not pass in parallel yet.");return;}
00054
00055 #define EXIT_IF_SEQUENTIAL if(PetscTools::IsSequential()){TS_TRACE("This test is not meant to be executed in sequential.");return;}
00056
00057 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2 || PETSC_VERSION_MAJOR<3 ) // Before PETSc 3.2
00058
00061 typedef PetscTruth PetscBool;
00069 #define PETSC_DESTROY_PARAM(x) x
00070 #else
00071
00078 #define PETSC_DESTROY_PARAM(x) &x
00079 #endif
00080
00090 #define TRY_IF_MASTER(method) { \
00091 if (PetscTools::AmMaster()) \
00092 { try { \
00093 method; \
00094 } catch (Exception& e) { \
00095 PetscTools::ReplicateException(true);\
00096 throw(e); \
00097 } } \
00098 PetscTools::ReplicateException(false); \
00099 }
00100
00110 class PetscTools
00111 {
00112 private:
00113
00115 static bool mPetscIsInitialised;
00116
00118 static unsigned mNumProcessors;
00119
00121 static unsigned mRank;
00122
00124 static bool mIsolateProcesses;
00125
00127 static inline void CheckCache()
00128 {
00129 if (mNumProcessors == 0)
00130 {
00131 ResetCache();
00132 }
00133 }
00134
00135 public:
00136
00140 static const unsigned MASTER_RANK=0;
00141
00146 static void ResetCache();
00147
00151 static bool IsInitialised();
00152
00156 static bool IsSequential();
00157
00161 static bool IsParallel();
00162
00166 static bool IsIsolated();
00167
00171 static unsigned GetNumProcs();
00172
00178 static unsigned GetMyRank();
00179
00185 static bool AmMaster();
00186
00192 static bool AmTopMost();
00193
00200 static void Barrier(const std::string callerId="");
00201
00209 static void BeginRoundRobin();
00210
00214 static void EndRoundRobin();
00215
00225 static void IsolateProcesses(bool isolate=true);
00226
00232 static MPI_Comm GetWorld();
00233
00242 static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
00243
00250 static Vec CreateVec(std::vector<double> data);
00251
00260 static Vec CreateAndSetVec(int size, double value);
00261
00280 static void SetupMat(Mat& rMat, int numRows, int numColumns,
00281 unsigned rowPreallocation,
00282 int numLocalRows=PETSC_DECIDE,
00283 int numLocalColumns=PETSC_DECIDE,
00284 bool ignoreOffProcEntries=true,
00285 bool newAllocationError=true);
00286
00293 static bool ReplicateBool(bool flag);
00294
00301 static void ReplicateException(bool flag);
00302
00309 static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
00310
00317 static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
00318
00326 static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00327
00335 static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00336
00342 static bool HasParMetis();
00343
00351 static inline void Destroy(Vec& rVec)
00352 {
00353 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00354 VecDestroy(&rVec);
00355 #else
00356 VecDestroy(rVec);
00357 #endif
00358 }
00359
00367 static inline void Destroy(Mat& rMat)
00368 {
00369 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00370 MatDestroy(&rMat);
00371 #else
00372 MatDestroy(rMat);
00373 #endif
00374 }
00375 };
00376
00377 #endif