PetscTools.hpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2005-2015, 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 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2 || PETSC_VERSION_MAJOR<3 ) // Before PETSc 3.2
00058 
00061 typedef PetscTruth PetscBool;
00069 #define PETSC_DESTROY_PARAM(x) x
00070 #else
00071 
00078 #define PETSC_DESTROY_PARAM(x) &x
00079 #endif
00080 
00090 #define TRY_IF_MASTER(method) {              \
00091     if (PetscTools::AmMaster())              \
00092     { try {                                  \
00093         method;                              \
00094     } catch (Exception& e) {                 \
00095         PetscTools::ReplicateException(true);\
00096         throw(e);                            \
00097     } }                                      \
00098     PetscTools::ReplicateException(false);   \
00099     }
00100 
00110 class PetscTools
00111 {
00112 private:
00113 
00115     static bool mPetscIsInitialised;
00116 
00118     static unsigned mNumProcessors;
00119 
00121     static unsigned mRank;
00122 
00124     static bool mIsolateProcesses;
00125 
00127     static inline void CheckCache()
00128     {
00129         if (mNumProcessors == 0)
00130         {
00131             ResetCache();
00132         }
00133     }
00134 
00135 public:
00136 
00140     static const unsigned MASTER_RANK=0;
00141 
00146     static void ResetCache();
00147 
00151     static bool IsInitialised();
00152 
00156     static bool IsSequential();
00157 
00161     static bool IsParallel();
00162 
00166     static bool IsIsolated();
00167 
00171     static unsigned GetNumProcs();
00172 
00178     static unsigned GetMyRank();
00179 
00185     static bool AmMaster();
00186 
00192     static bool AmTopMost();
00193 
00200     static void Barrier(const std::string callerId="");
00201 
00209     static void BeginRoundRobin();
00210 
00214     static void EndRoundRobin();
00215 
00225     static void IsolateProcesses(bool isolate=true);
00226 
00232     static MPI_Comm GetWorld();
00233 
00242     static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries = true);
00243 
00250     static Vec CreateVec(std::vector<double> data);
00251 
00260     static Vec CreateAndSetVec(int size, double value);
00261 
00280     static void SetupMat(Mat& rMat, int numRows, int numColumns,
00281                          unsigned rowPreallocation,
00282                          int numLocalRows=PETSC_DECIDE,
00283                          int numLocalColumns=PETSC_DECIDE,
00284                          bool ignoreOffProcEntries=true,
00285                          bool newAllocationError=true);
00286 
00293     static bool ReplicateBool(bool flag);
00294 
00301     static void ReplicateException(bool flag);
00302 
00309     static void DumpPetscObject(const Mat& rMat, const std::string& rOutputFileFullPath);
00310 
00317     static void DumpPetscObject(const Vec& rVec, const std::string& rOutputFileFullPath);
00318 
00326     static void ReadPetscObject(Mat& rMat, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00327 
00335     static void ReadPetscObject(Vec& rVec, const std::string& rOutputFileFullPath, Vec rParallelLayout=NULL);
00336 
00342     static bool HasParMetis();
00343 
00351      static inline void Destroy(Vec& rVec)
00352      {
00353 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00354         VecDestroy(&rVec);
00355 #else
00356         VecDestroy(rVec);
00357 #endif
00358      }
00359 
00367      static inline void Destroy(Mat& rMat)
00368      {
00369 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00370         MatDestroy(&rMat);
00371 #else
00372         MatDestroy(rMat);
00373 #endif
00374      }
00375 };
00376 
00377 #endif /*PETSCTOOLS_HPP_*/

Generated by  doxygen 1.6.2