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 #ifndef NDEBUG
00058
00059
00060 #endif
00061
00062 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00063 typedef PetscBool PetscTruth;
00071 #define PETSC_DESTROY_PARAM(x) &x
00072 #else
00073
00080 #define PETSC_DESTROY_PARAM(x) x
00081 #endif
00082
00092 #define TRY_IF_MASTER(method) { \
00093 if (PetscTools::AmMaster()) \
00094 { try { \
00095 method; \
00096 } catch (Exception& e) { \
00097 PetscTools::ReplicateException(true);\
00098 throw(e); \
00099 } } \
00100 PetscTools::ReplicateException(false); \
00101 }
00102
00112 class PetscTools
00113 {
00114 private:
00115
00117 static bool mPetscIsInitialised;
00118
00120 static unsigned mNumProcessors;
00121
00122 #ifdef DEBUG_BARRIERS
00123
00124 static unsigned mNumBarriers;
00125 #endif
00126
00128 static unsigned mRank;
00129
00131 static bool mIsolateProcesses;
00132
00134 static inline void CheckCache()
00135 {
00136 if (mNumProcessors == 0)
00137 {
00138 ResetCache();
00139 }
00140 }
00141
00142 public:
00143
00147 static const unsigned MASTER_RANK=0;
00148
00153 static void ResetCache();
00154
00158 static bool IsSequential();
00159
00163 static bool IsParallel();
00164
00168 static bool IsIsolated();
00169
00173 static unsigned GetNumProcs();
00174
00180 static unsigned GetMyRank();
00181
00187 static bool AmMaster();
00188
00194 static bool AmTopMost();
00195
00202 static void Barrier(const std::string callerId="");
00203
00207 static void BeginRoundRobin();
00208
00212 static void EndRoundRobin();
00213
00223 static void IsolateProcesses(bool isolate=true);
00224
00230 static MPI_Comm GetWorld();
00231
00240 static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
00241
00248 static Vec CreateVec(std::vector<double> data);
00249
00258 static Vec CreateAndSetVec(int size, double value);
00259
00278 static void SetupMat(Mat& rMat, int numRows, int numColumns,
00279 unsigned rowPreallocation,
00280 int numLocalRows=PETSC_DECIDE,
00281 int numLocalColumns=PETSC_DECIDE,
00282 bool ignoreOffProcEntries=true,
00283 bool newAllocationError=true);
00284
00291 static bool ReplicateBool(bool flag);
00292
00299 static void ReplicateException(bool flag);
00300
00307 static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
00308
00315 static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
00316
00324 static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00325
00333 static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00334
00340 static bool HasParMetis();
00341
00349 static inline void Destroy(Vec& rVec)
00350 {
00351 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00352 VecDestroy(&rVec);
00353 #else
00354 VecDestroy(rVec);
00355 #endif
00356 }
00357
00365 static inline void Destroy(Mat& rMat)
00366 {
00367 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00368 MatDestroy(&rMat);
00369 #else
00370 MatDestroy(rMat);
00371 #endif
00372 }
00373 };
00374
00375 #endif