Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
ArchiveOpener.cpp
1/*
2
3Copyright (c) 2005-2024, 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// Must be included before any other serialization headers
37#include <boost/archive/text_oarchive.hpp>
38#include <boost/archive/text_iarchive.hpp>
39
40#include <sstream>
41#include <fstream>
42
43#include "ArchiveOpener.hpp"
44#include "ArchiveLocationInfo.hpp"
45#include "ProcessSpecificArchive.hpp"
46#include "Exception.hpp"
47#include "OutputFileHandler.hpp"
48
55template<>
57 const FileFinder& rDirectory,
58 const std::string& rFileNameBase,
59 unsigned procId)
60 : mpCommonStream(nullptr),
61 mpPrivateStream(nullptr),
62 mpCommonArchive(nullptr),
63 mpPrivateArchive(nullptr)
64{
65 // Figure out where things live
67 std::string private_path = ArchiveLocationInfo::GetProcessUniqueFilePath(rFileNameBase, procId);
68 std::stringstream common_path;
69 common_path << ArchiveLocationInfo::GetArchiveDirectory() << rFileNameBase;
70
71 // Try to open the main archive for replicated data
72 mpCommonStream = new std::ifstream(common_path.str().c_str(), std::ios::binary);
73 if (!mpCommonStream->is_open())
74 {
75 delete mpCommonStream;
76 EXCEPTION("Cannot load main archive file: " + common_path.str());
77 }
78
79 try
80 {
81 mpCommonArchive = new boost::archive::text_iarchive(*mpCommonStream);
82 }
83 catch (boost::archive::archive_exception& boost_exception)
84 {
85 if (boost_exception.code == boost::archive::archive_exception::unsupported_version)
86 {
87 // This is forward compatibility issue. We can't open the archive because it's been written by a more recent Boost.
88 delete mpCommonArchive;
89 delete mpCommonStream;
90 EXCEPTION("Could not open Boost archive '" + common_path.str() + "' because it was written by a more recent Boost. Check process-specific archives too");
91 }
92 else
93 {
94 // We don't understand the exception, so we shouldn't continue
95 throw boost_exception; // LCOV_EXCL_LINE
96 }
97 }
98
99 // Try to open the secondary archive for distributed data
100 mpPrivateStream = new std::ifstream(private_path.c_str(), std::ios::binary);
101 if (!mpPrivateStream->is_open())
102 {
103 delete mpPrivateStream;
104 delete mpCommonArchive;
105 delete mpCommonStream;
106 EXCEPTION("Cannot load secondary archive file: " + private_path);
107 }
108 mpPrivateArchive = new boost::archive::text_iarchive(*mpPrivateStream);
110}
111
112template<>
114{
116 delete mpPrivateArchive;
117 delete mpPrivateStream;
118 delete mpCommonArchive;
119 delete mpCommonStream;
120}
121
128template<>
130 const FileFinder& rDirectory,
131 const std::string& rFileNameBase,
132 unsigned procId)
133 : mpCommonStream(nullptr),
134 mpPrivateStream(nullptr),
135 mpCommonArchive(nullptr),
136 mpPrivateArchive(nullptr)
137{
138 // Check for user error
139 if (procId != PetscTools::GetMyRank())
140 {
141 EXCEPTION("Specifying the secondary archive file ID doesn't make sense when writing.");
142 }
143
144 // Figure out where things live
147 {
148 // Ensure the directory exists
150 }
151 std::string private_path = ArchiveLocationInfo::GetProcessUniqueFilePath(rFileNameBase);
152 std::stringstream common_path;
153 common_path << ArchiveLocationInfo::GetArchiveDirectory() << rFileNameBase;
154
155 // Create master archive for replicated data
157 {
158 mpCommonStream = new std::ofstream(common_path.str().c_str(), std::ios::binary | std::ios::trunc);
159 if (!mpCommonStream->is_open())
160 {
161 delete mpCommonStream;
162 EXCEPTION("Failed to open main archive file for writing: " + common_path.str());
163 }
164 }
165 else
166 {
167 // Non-master processes need to go through the serialization methods, but not write any data
168#ifdef _MSC_VER
169 mpCommonStream = new std::ofstream("NUL", std::ios::binary | std::ios::trunc);
170#else
171 mpCommonStream = new std::ofstream("/dev/null", std::ios::binary | std::ios::trunc);
172#endif
173 // LCOV_EXCL_START
174 if (!mpCommonStream->is_open())
175 {
176 delete mpCommonStream;
177 EXCEPTION("Failed to open dummy archive file '/dev/null' for writing");
178 }
179 // LCOV_EXCL_STOP
180 }
181 mpCommonArchive = new boost::archive::text_oarchive(*mpCommonStream);
182
183 // Create secondary archive for distributed data
184 mpPrivateStream = new std::ofstream(private_path.c_str(), std::ios::binary | std::ios::trunc);
185 if (!mpPrivateStream->is_open())
186 {
187 delete mpPrivateStream;
188 delete mpCommonArchive;
189 delete mpCommonStream;
190 EXCEPTION("Failed to open secondary archive file for writing: " + private_path);
191 }
192 mpPrivateArchive = new boost::archive::text_oarchive(*mpPrivateStream);
194}
195
196template<>
198{
200 delete mpPrivateArchive;
201 delete mpPrivateStream;
202 delete mpCommonArchive;
203 delete mpCommonStream;
204
205 /* In a parallel setting, make sure all processes have finished writing before
206 * continuing, to avoid nasty race conditions.
207 * For example, many tests will write an archive then immediately read it back
208 * in, which could easily break without this.
209 */
210 PetscTools::Barrier("~ArchiveOpener");
211}
#define EXCEPTION(message)
static std::string GetProcessUniqueFilePath(const std::string &rFileName, unsigned procId=PetscTools::GetMyRank())
static bool GetIsDirRelativeToChasteTestOutput()
static std::string GetArchiveDirectory()
static void SetArchiveDirectory(const FileFinder &rDirectory)
static std::string GetArchiveRelativePath()
Archive * mpCommonArchive
Stream * mpCommonStream
Stream * mpPrivateStream
ArchiveOpener(const FileFinder &rDirectory, const std::string &rFileNameBase, unsigned procId=PetscTools::GetMyRank())
Archive * mpPrivateArchive
static bool AmMaster()
static void Barrier(const std::string callerId="")
static unsigned GetMyRank()
static void Set(Archive *pArchive)