PetscTools.hpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2005-2014, 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 
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 /*PETSCTOOLS_HPP_*/

Generated by  doxygen 1.6.2