Chaste  Release::2017.1
PetscSetupUtils.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "PetscSetupUtils.hpp"
37 
38 #include <cassert>
39 #include <cstdlib>
40 #include <cstring>
41 #include <iostream>
42 #include <petsc.h>
43 
44 #include "ChasteBuildRoot.hpp"
45 #include "ChasteSyscalls.hpp"
46 #include "Citations.hpp"
47 #include "CommandLineArguments.hpp"
48 #include "Exception.hpp"
49 #include "GetCurrentWorkingDirectory.hpp"
50 #include "PetscException.hpp"
51 #include "PetscTools.hpp"
52 
53 #include "PetscAndChasteCitations.hpp"
54 
55 #ifdef TEST_FOR_FPE
56 #include <fenv.h>
57 #include <signal.h>
58 
59 void FpeSignalToAbort(int sig_num, siginfo_t* info, void* context)
60 {
61  if (info->si_code == FPE_FLTDIV)
62  {
63  std::cerr << "SIGFPE: floating point exception was divide by zero.\n";
64  }
65  else if (info->si_code == FPE_FLTINV)
66  {
67  std::cerr << "SIGFPE: floating point exception was an invalid operation (like 0.0/0.0).\n";
68  }
69  else
70  {
71  std::cerr << "SIGFPE: unexpected error code.\n";
72  }
73 }
74 #endif
75 
77 {
78  // The CommandLineArguments instance is filled in by the cxxtest test suite runner.
80  PETSCEXCEPT(PetscInitialize(p_args->p_argc, p_args->p_argv, PETSC_NULL, PETSC_NULL));
81  // Work around what seems to be an Intel compiler bug/quirk that makes the cache stale,
82  // by using an explicit reset to ensure all code is aware we're running in parallel.
84 }
85 
87 {
89 
90  if ((PETSC_VERSION_MAJOR < 3) || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 5)) // PETSc 3.4 or earlier
91  {
92  // Add some PETSc citations if we're not using the built-in citation mechanisms
93  Citations::Register(PetscCitation1, &PetscCite1);
94  Citations::Register(PetscCitation2, &PetscCite2);
95  }
96  // Add main Chaste citation
97  Citations::Register(ChasteCitation, &ChasteCite);
98 
99  // Check that the working directory is correct, or many tests will fail
100  std::string cwd = GetCurrentWorkingDirectory() + "/";
101  if (strcmp(cwd.c_str(), ChasteSourceRootDir()) != 0)
102  {
103  // Change directory
104  std::cout << std::endl
105  << "Changing directory from '" << cwd << "' to '" << ChasteSourceRootDir() << "'." << std::endl;
106  EXPECT0(chdir, ChasteSourceRootDir());
107  std::cout << "CWD now: " << GetCurrentWorkingDirectory() << std::endl;
108  }
109 
110 #ifdef TEST_FOR_FPE
111  // Give all PETSc enabled tests the ability to trap for divide-by-zero
112  feenableexcept(FE_DIVBYZERO | FE_INVALID);
113  // Catch all SIGFPE signals and convert them to exceptions (before PETSc gets to them)
114  struct sigaction sa;
115  sa.sa_sigaction = FpeSignalToAbort;
116  sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
117  sa.sa_restorer = 0;
118  sigaction(SIGFPE, &sa, NULL);
119 #endif
120 }
121 
123 {
124  // This does nothing if we are on a new PETSc, just allows Chaste to print citations instead in this case
126 
127  PETSCEXCEPT(PetscFinalize());
128 }
129 
131 {
133 }
const char * ChasteSourceRootDir()
static void CommonSetup()
static void InitialisePetsc()
#define EXPECT0(cmd, arg)
Definition: Exception.hpp:254
static void CommonFinalize()
static void ResetStatusCache()
static void Register(const char *pCitation, PetscBool *pSet)
Definition: Citations.cpp:43
static CommandLineArguments * Instance()
static void ResetCache()
Definition: PetscTools.cpp:58
static void Print()
Definition: Citations.cpp:71