Chaste Release::3.1
PetscTools.hpp
Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #ifndef PETSCTOOLS_HPP_
00037 #define PETSCTOOLS_HPP_
00038 
00044 #include <string>
00045 #include <vector>
00046 #include <cstdlib> // For EXIT_FAILURE
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 // Uncomment this to trace calls to PetscTools::Barrier
00059 //#define DEBUG_BARRIERS
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 
00086 class PetscTools
00087 {
00088 private:
00089 
00091     static bool mPetscIsInitialised;
00092 
00094     static unsigned mNumProcessors;
00095 
00096 #ifdef DEBUG_BARRIERS
00097 
00098     static unsigned mNumBarriers;
00099 #endif
00100 
00102     static unsigned mRank;
00103 
00105     static bool mIsolateProcesses;
00106 
00108     static inline void CheckCache()
00109     {
00110         if (mNumProcessors == 0)
00111         {
00112             ResetCache();
00113         }
00114     }
00115 
00116 public:
00117 
00121     static const unsigned MASTER_RANK=0;
00122 
00127     static void ResetCache();
00128 
00132     static bool IsSequential();
00133 
00137     static bool IsParallel();
00138 
00142     static unsigned GetNumProcs();
00143 
00149     static unsigned GetMyRank();
00150 
00156     static bool AmMaster();
00157 
00163     static bool AmTopMost();
00164 
00171     static void Barrier(const std::string callerId="");
00172 
00176     static void BeginRoundRobin();
00177 
00181     static void EndRoundRobin();
00182 
00192     static void IsolateProcesses(bool isolate=true);
00193 
00201     static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
00202 
00208     static Vec CreateVec(std::vector<double> data);
00209 
00217     static Vec CreateAndSetVec(int size, double value);
00218 
00233     static void SetupMat(Mat& rMat, int numRows, int numColumns,
00234                          unsigned rowPreallocation,
00235                          int numLocalRows=PETSC_DECIDE,
00236                          int numLocalColumns=PETSC_DECIDE,
00237                          bool ignoreOffProcEntries=true);
00238 
00245     static bool ReplicateBool(bool flag);
00246 
00253     static void ReplicateException(bool flag);
00254 
00261     static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
00262 
00269     static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
00270 
00278     static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00279 
00287     static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00288 
00294     static bool HasParMetis();
00295 
00303      static inline void Destroy(Vec& rVec)
00304      {
00305 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00306         VecDestroy(&rVec);
00307 #else
00308         VecDestroy(rVec);
00309 #endif
00310      }
00311 
00319      static inline void Destroy(Mat& rMat)
00320      {
00321 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00322         MatDestroy(&rMat);
00323 #else
00324         MatDestroy(rMat);
00325 #endif
00326      }
00327 };
00328 
00329 #endif /*PETSCTOOLS_HPP_*/