Chaste Commit::29c3c4ab2bee00ddd311a035a1e522c03cbe928a
CvodeContextManager.cpp
1/*
2
3Copyright (c) 2005-2026, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#if CHASTE_SUNDIALS_VERSION >= 60000
37#include <cassert>
38#include <memory>
39
40#include <nvector/nvector_serial.h>
41#include <sundials/sundials_types.h> // For SUN_COMM_NULL
42#include "CvodeContextManager.hpp"
43
44CvodeContextManager::CvodeContextManager()
45{
46#if CHASTE_SUNDIALS_VERSION >= 70000
47 SUNContext_Create(SUN_COMM_NULL, &mSundialsContext);
48#else
49 SUNContext_Create(NULL, &mSundialsContext); //"a pointer to the MPI communicator or NULL if not using MPI"
50#endif
51}
52
53CvodeContextManager::~CvodeContextManager()
54{
55 SUNContext_Free(&mSundialsContext);
56}
57
58std::shared_ptr<CvodeContextManager> CvodeContextManager::Instance()
59{
60 // Single instance per thread. Ownership is shared rather than exclusive because the
61 // context has to outlive everything created against it: thread-local destructors run at
62 // the start of exit(), before the destructors of static objects in shared libraries, and
63 // some of those statics (e.g. CellCycleModelOdeSolver's mpInstance) still hold a
64 // CvodeAdaptor whose destructor calls CVodeFree(). Freeing the context when the thread
65 // goes away would therefore be a use-after-free at exit, which Sundials 7 detects via
66 // SUNContext_PeekLastError. Note that make_shared cannot be used here as the constructor
67 // is private.
68 static thread_local std::shared_ptr<CvodeContextManager> p_instance(new CvodeContextManager());
69 return p_instance;
70}
71
72SUNContext& CvodeContextManager::GetSundialsContext()
73{
74 assert(mSundialsContext);
75 return mSundialsContext;
76}
77
78#endif // CHASTE_SUNDIALS_VERSION >= 60000