PetscTools.hpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #ifndef PETSCTOOLS_HPP_
00030 #define PETSCTOOLS_HPP_
00031 
00040 #include <string>
00041 #include <vector>
00042 #include <cstdlib> // For EXIT_FAILURE
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 // Uncomment this to trace calls to PetscTools::Barrier
00058 //#define DEBUG_BARRIERS
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 
00151     static void BeginRoundRobin();
00152 
00156     static void EndRoundRobin();
00157 
00158 #ifndef SPECIAL_SERIAL
00159 
00167     static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
00168 
00174     static Vec CreateVec(std::vector<double> data);
00175 
00183     static Vec CreateAndSetVec(int size, double value);
00184 
00199     static void SetupMat(Mat& rMat, int numRows, int numColumns,
00200                          unsigned rowPreallocation,
00201                          int numLocalRows=PETSC_DECIDE,
00202                          int numLocalColumns=PETSC_DECIDE,
00203                          bool ignoreOffProcEntries=true);
00204 
00211     static bool ReplicateBool(bool flag);
00212 
00219     static void ReplicateException(bool flag);
00220 
00227     static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
00228 
00235     static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
00236 
00244     static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00245 
00253     static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00254 
00255 #endif //SPECIAL_SERIAL (ifndef)
00256 
00265     static void Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00266 
00267 };
00268 
00275 #define TERMINATE(message) PetscTools::Terminate(message, __FILE__, __LINE__)
00276 
00308 #define NEVER_REACHED  PetscTools::Terminate("Should have been impossible to reach this line of code", __FILE__, __LINE__); exit(EXIT_FAILURE)
00309 
00310 #endif /*PETSCTOOLS_HPP_*/

Generated on Tue May 31 14:31:41 2011 for Chaste by  doxygen 1.5.5