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 #ifndef PETSCTOOLS_HPP_
00030 #define PETSCTOOLS_HPP_
00031
00040 #include <string>
00041 #include <vector>
00042 #include <cstdlib>
00043
00044 #ifndef SPECIAL_SERIAL
00045 #include <petsc.h>
00046 #include <petscvec.h>
00047 #include <petscmat.h>
00048 #endif //SPECIAL_SERIAL
00049
00051 #define EXIT_IF_PARALLEL if(PetscTools::IsParallel()){TS_TRACE("This test does not pass in parallel yet.");return;}
00052
00053 #define EXIT_IF_SEQUENTIAL if(PetscTools::IsSequential()){TS_TRACE("This test is not meant to be executed in sequential.");return;}
00054
00055
00056 #ifndef NDEBUG
00057
00058
00059 #endif
00060
00061
00065 class PetscTools
00066 {
00067 private:
00069 static bool mPetscIsInitialised;
00070
00072 static unsigned mNumProcessors;
00073
00074 #ifdef DEBUG_BARRIERS
00075
00076 static unsigned mNumBarriers;
00077 #endif
00078
00080 static unsigned mRank;
00081
00083 static inline void CheckCache()
00084 {
00085 if (mNumProcessors == 0)
00086 {
00087 ResetCache();
00088 }
00089 }
00090
00091 public:
00092
00096 static const unsigned MASTER_RANK=0;
00097
00102 static void ResetCache();
00103
00107 static bool IsSequential();
00108
00112 static bool IsParallel();
00113
00117 static unsigned GetNumProcs();
00118
00124 static unsigned GetMyRank();
00125
00131 static bool AmMaster();
00132
00138 static bool AmTopMost();
00139
00146 static void Barrier(const std::string callerId="");
00147
00148 #ifndef SPECIAL_SERIAL
00149
00157 static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
00158
00164 static Vec CreateVec(std::vector<double> data);
00165
00173 static Vec CreateAndSetVec(int size, double value);
00174
00189 static void SetupMat(Mat& rMat, int numRows, int numColumns,
00190 unsigned rowPreallocation,
00191 int numLocalRows=PETSC_DECIDE,
00192 int numLocalColumns=PETSC_DECIDE,
00193 bool ignoreOffProcEntries=true);
00194
00201 static bool ReplicateBool(bool flag);
00202
00209 static void ReplicateException(bool flag);
00210
00217 static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
00218
00225 static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
00226
00234 static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00235
00243 static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00244
00245 #endif //SPECIAL_SERIAL (ifndef)
00246
00255 static void Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00256
00257 };
00258
00265 #define TERMINATE(message) PetscTools::Terminate(message, __FILE__, __LINE__)
00266
00298 #define NEVER_REACHED PetscTools::Terminate("Should have been impossible to reach this line of code", __FILE__, __LINE__); exit(EXIT_FAILURE)
00299
00300 #endif