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 
00037 #include <string>
00038 #include <vector>
00039 #include <cstdlib> // For EXIT_FAILURE
00040 
00041 #include <petsc.h>
00042 #include <petscvec.h>
00043 #include <petscmat.h>
00044 
00046 #define EXIT_IF_PARALLEL if(PetscTools::IsParallel()){TS_TRACE("This test does not pass in parallel yet.");return;}
00047 
00048 #define EXIT_IF_SEQUENTIAL if(PetscTools::IsSequential()){TS_TRACE("This test is not meant to be executed in sequential.");return;}
00049 
00050 #ifndef NDEBUG
00051 // Uncomment this to trace calls to PetscTools::Barrier
00052 //#define DEBUG_BARRIERS
00053 #endif
00054 
00058 class PetscTools
00059 {
00060 private:
00061 
00063     static bool mPetscIsInitialised;
00064 
00066     static unsigned mNumProcessors;
00067 
00068 #ifdef DEBUG_BARRIERS
00069 
00070     static unsigned mNumBarriers;
00071 #endif
00072 
00074     static unsigned mRank;
00075 
00077     static bool mIsolateProcesses;
00078 
00080     static inline void CheckCache()
00081     {
00082         if (mNumProcessors == 0)
00083         {
00084             ResetCache();
00085         }
00086     }
00087 
00088 public:
00089 
00093     static const unsigned MASTER_RANK=0;
00094 
00099     static void ResetCache();
00100 
00104     static bool IsSequential();
00105 
00109     static bool IsParallel();
00110 
00114     static unsigned GetNumProcs();
00115 
00121     static unsigned GetMyRank();
00122 
00128     static bool AmMaster();
00129 
00135     static bool AmTopMost();
00136 
00143     static void Barrier(const std::string callerId="");
00144 
00148     static void BeginRoundRobin();
00149 
00153     static void EndRoundRobin();
00154 
00164     static void IsolateProcesses(bool isolate=true);
00165 
00173     static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
00174 
00180     static Vec CreateVec(std::vector<double> data);
00181 
00189     static Vec CreateAndSetVec(int size, double value);
00190 
00205     static void SetupMat(Mat& rMat, int numRows, int numColumns,
00206                          unsigned rowPreallocation,
00207                          int numLocalRows=PETSC_DECIDE,
00208                          int numLocalColumns=PETSC_DECIDE,
00209                          bool ignoreOffProcEntries=true);
00210 
00217     static bool ReplicateBool(bool flag);
00218 
00225     static void ReplicateException(bool flag);
00226 
00233     static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
00234 
00241     static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
00242 
00250     static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00251 
00259     static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00260 };
00261 
00262 #endif /*PETSCTOOLS_HPP_*/
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3