Chaste  Release::3.4
ArchiveOpener.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 // 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 <iostream>
42 
43 #include "ArchiveOpener.hpp"
44 #include "ArchiveLocationInfo.hpp"
45 #include "ProcessSpecificArchive.hpp"
46 #include "Exception.hpp"
47 #include "OutputFileHandler.hpp"
48 
55 template<>
57  const FileFinder& rDirectory,
58  const std::string& rFileNameBase,
59  unsigned procId)
60  : mpCommonStream(NULL),
61  mpPrivateStream(NULL),
62  mpCommonArchive(NULL),
63  mpPrivateArchive(NULL)
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 #define COVERAGE_IGNORE
96  throw boost_exception;
97 #undef COVERAGE_IGNORE
98  }
99  }
100 
101  // Try to open the secondary archive for distributed data
102  mpPrivateStream = new std::ifstream(private_path.c_str(), std::ios::binary);
103  if (!mpPrivateStream->is_open())
104  {
105  delete mpPrivateStream;
106  delete mpCommonArchive;
107  delete mpCommonStream;
108  EXCEPTION("Cannot load secondary archive file: " + private_path);
109  }
110  mpPrivateArchive = new boost::archive::text_iarchive(*mpPrivateStream);
112 }
113 
114 template<>
116 {
118  delete mpPrivateArchive;
119  delete mpPrivateStream;
120  delete mpCommonArchive;
121  delete mpCommonStream;
122 }
123 
130 template<>
132  const FileFinder& rDirectory,
133  const std::string& rFileNameBase,
134  unsigned procId)
135  : mpCommonStream(NULL),
136  mpPrivateStream(NULL),
137  mpCommonArchive(NULL),
138  mpPrivateArchive(NULL)
139 {
140  // Check for user error
141  if (procId != PetscTools::GetMyRank())
142  {
143  EXCEPTION("Specifying the secondary archive file ID doesn't make sense when writing.");
144  }
145 
146  // Figure out where things live
149  {
150  // Ensure the directory exists
152  }
153  std::string private_path = ArchiveLocationInfo::GetProcessUniqueFilePath(rFileNameBase);
154  std::stringstream common_path;
155  common_path << ArchiveLocationInfo::GetArchiveDirectory() << rFileNameBase;
156 
157  // Create master archive for replicated data
158  if (PetscTools::AmMaster())
159  {
160  mpCommonStream = new std::ofstream(common_path.str().c_str(), std::ios::binary | std::ios::trunc);
161  if (!mpCommonStream->is_open())
162  {
163  delete mpCommonStream;
164  EXCEPTION("Failed to open main archive file for writing: " + common_path.str());
165  }
166  }
167  else
168  {
169  // Non-master processes need to go through the serialization methods, but not write any data
170 #ifdef _MSC_VER
171  mpCommonStream = new std::ofstream("NUL", std::ios::binary | std::ios::trunc);
172 #else
173  mpCommonStream = new std::ofstream("/dev/null", std::ios::binary | std::ios::trunc);
174 #endif
175  #define COVERAGE_IGNORE
176  if (!mpCommonStream->is_open())
177  {
178  delete mpCommonStream;
179  EXCEPTION("Failed to open dummy archive file '/dev/null' for writing");
180  }
181  #undef COVERAGE_IGNORE
182  }
183  mpCommonArchive = new boost::archive::text_oarchive(*mpCommonStream);
184 
185  // Create secondary archive for distributed data
186  mpPrivateStream = new std::ofstream(private_path.c_str(), std::ios::binary | std::ios::trunc);
187  if (!mpPrivateStream->is_open())
188  {
189  delete mpPrivateStream;
190  delete mpCommonArchive;
191  delete mpCommonStream;
192  EXCEPTION("Failed to open secondary archive file for writing: " + private_path);
193  }
194  mpPrivateArchive = new boost::archive::text_oarchive(*mpPrivateStream);
196 }
197 
198 template<>
200 {
202  delete mpPrivateArchive;
203  delete mpPrivateStream;
204  delete mpCommonArchive;
205  delete mpCommonStream;
206 
207  /* In a parallel setting, make sure all processes have finished writing before
208  * continuing, to avoid nasty race conditions.
209  * For example, many tests will write an archive then immediately read it back
210  * in, which could easily break without this.
211  */
212  PetscTools::Barrier("~ArchiveOpener");
213 }
static void Barrier(const std::string callerId="")
Definition: PetscTools.cpp:134
static std::string GetProcessUniqueFilePath(const std::string &rFileName, unsigned procId=PetscTools::GetMyRank())
ArchiveOpener(const FileFinder &rDirectory, const std::string &rFileNameBase, unsigned procId=PetscTools::GetMyRank())
Stream * mpPrivateStream
static bool GetIsDirRelativeToChasteTestOutput()
Stream * mpCommonStream
#define EXCEPTION(message)
Definition: Exception.hpp:143
Archive * mpPrivateArchive
static bool AmMaster()
Definition: PetscTools.cpp:120
static unsigned GetMyRank()
Definition: PetscTools.cpp:114
static std::string GetArchiveDirectory()
static std::string GetArchiveRelativePath()
static void SetArchiveDirectory(const FileFinder &rDirectory)
Archive * mpCommonArchive
static void Set(Archive *pArchive)