Chaste Release::3.1
ArchiveOpener.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, 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 // Must be included before any other serialization headers
00037 #include <boost/archive/text_oarchive.hpp>
00038 #include <boost/archive/text_iarchive.hpp>
00039 
00040 #include <sstream>
00041 #include <iostream>
00042 
00043 #include "ArchiveOpener.hpp"
00044 #include "ArchiveLocationInfo.hpp"
00045 #include "ProcessSpecificArchive.hpp"
00046 #include "Exception.hpp"
00047 #include "OutputFileHandler.hpp"
00048 
00055 template<>
00056 ArchiveOpener<boost::archive::text_iarchive, std::ifstream>::ArchiveOpener(
00057         const FileFinder& rDirectory,
00058         const std::string& rFileNameBase,
00059         unsigned procId)
00060     : mpCommonStream(NULL),
00061       mpPrivateStream(NULL),
00062       mpCommonArchive(NULL),
00063       mpPrivateArchive(NULL)
00064 {
00065     // Figure out where things live
00066     ArchiveLocationInfo::SetArchiveDirectory(rDirectory);
00067     std::string private_path = ArchiveLocationInfo::GetProcessUniqueFilePath(rFileNameBase, procId);
00068     std::stringstream common_path;
00069     common_path << ArchiveLocationInfo::GetArchiveDirectory() << rFileNameBase;
00070 
00071     // Try to open the main archive for replicated data
00072     mpCommonStream = new std::ifstream(common_path.str().c_str(), std::ios::binary);
00073     if (!mpCommonStream->is_open())
00074     {
00075         delete mpCommonStream;
00076         EXCEPTION("Cannot load main archive file: " + common_path.str());
00077     }
00078 
00079     try
00080     {
00081         mpCommonArchive = new boost::archive::text_iarchive(*mpCommonStream);
00082     }
00083     catch (boost::archive::archive_exception& boost_exception)
00084     {
00085         if (boost_exception.code == boost::archive::archive_exception::unsupported_version)
00086         {
00087             // This is forward compatibility issue.  We can't open the archive because it's been written by a more recent Boost.
00088             delete mpCommonArchive;
00089             delete mpCommonStream;
00090             EXCEPTION("Could not open Boost archive '" + common_path.str() + "' because it was written by a more recent Boost.  Check process-specific archives too");
00091         }
00092         else
00093         {
00094             // We don't understand the exception, so we shouldn't continue
00095 #define COVERAGE_IGNORE
00096             throw boost_exception;
00097 #undef COVERAGE_IGNORE
00098         }
00099     }
00100 
00101     // Try to open the secondary archive for distributed data
00102     mpPrivateStream = new std::ifstream(private_path.c_str(), std::ios::binary);
00103     if (!mpPrivateStream->is_open())
00104     {
00105         delete mpPrivateStream;
00106         delete mpCommonArchive;
00107         delete mpCommonStream;
00108         EXCEPTION("Cannot load secondary archive file: " + private_path);
00109     }
00110     mpPrivateArchive = new boost::archive::text_iarchive(*mpPrivateStream);
00111     ProcessSpecificArchive<boost::archive::text_iarchive>::Set(mpPrivateArchive);
00112 }
00113 
00114 template<>
00115 ArchiveOpener<boost::archive::text_iarchive, std::ifstream>::~ArchiveOpener()
00116 {
00117     ProcessSpecificArchive<boost::archive::text_iarchive>::Set(NULL);
00118     delete mpPrivateArchive;
00119     delete mpPrivateStream;
00120     delete mpCommonArchive;
00121     delete mpCommonStream;
00122 }
00123 
00130 template<>
00131 ArchiveOpener<boost::archive::text_oarchive, std::ofstream>::ArchiveOpener(
00132         const FileFinder& rDirectory,
00133         const std::string& rFileNameBase,
00134         unsigned procId)
00135     : mpCommonStream(NULL),
00136       mpPrivateStream(NULL),
00137       mpCommonArchive(NULL),
00138       mpPrivateArchive(NULL)
00139 {
00140     // Check for user error
00141     if (procId != PetscTools::GetMyRank())
00142     {
00143         EXCEPTION("Specifying the secondary archive file ID doesn't make sense when writing.");
00144     }
00145 
00146     // Figure out where things live
00147     ArchiveLocationInfo::SetArchiveDirectory(rDirectory);
00148     if (ArchiveLocationInfo::GetIsDirRelativeToChasteTestOutput())
00149     {
00150         // Ensure the directory exists
00151         OutputFileHandler handler(ArchiveLocationInfo::GetArchiveRelativePath(), false);
00152     }
00153     std::string private_path = ArchiveLocationInfo::GetProcessUniqueFilePath(rFileNameBase);
00154     std::stringstream common_path;
00155     common_path << ArchiveLocationInfo::GetArchiveDirectory() << rFileNameBase;
00156 
00157     // Create master archive for replicated data
00158     if (PetscTools::AmMaster())
00159     {
00160         mpCommonStream = new std::ofstream(common_path.str().c_str());
00161         if (!mpCommonStream->is_open())
00162         {
00163             delete mpCommonStream;
00164             EXCEPTION("Failed to open main archive file for writing: " + common_path.str());
00165         }
00166     }
00167     else
00168     {
00169         // Non-master processes need to go through the serialization methods, but not write any data
00170         mpCommonStream = new std::ofstream("/dev/null");
00171         #define COVERAGE_IGNORE
00172         if (!mpCommonStream->is_open())
00173         {
00174             delete mpCommonStream;
00175             EXCEPTION("Failed to open dummy archive file '/dev/null' for writing");
00176         }
00177         #undef COVERAGE_IGNORE
00178     }
00179     mpCommonArchive = new boost::archive::text_oarchive(*mpCommonStream);
00180 
00181     // Create secondary archive for distributed data
00182     mpPrivateStream = new std::ofstream(private_path.c_str());
00183     if (!mpPrivateStream->is_open())
00184     {
00185         delete mpPrivateStream;
00186         delete mpCommonArchive;
00187         delete mpCommonStream;
00188         EXCEPTION("Failed to open secondary archive file for writing: " + private_path);
00189     }
00190     mpPrivateArchive = new boost::archive::text_oarchive(*mpPrivateStream);
00191     ProcessSpecificArchive<boost::archive::text_oarchive>::Set(mpPrivateArchive);
00192 }
00193 
00194 template<>
00195 ArchiveOpener<boost::archive::text_oarchive, std::ofstream>::~ArchiveOpener()
00196 {
00197     ProcessSpecificArchive<boost::archive::text_oarchive>::Set(NULL);
00198     delete mpPrivateArchive;
00199     delete mpPrivateStream;
00200     delete mpCommonArchive;
00201     delete mpCommonStream;
00202 
00203     /* In a parallel setting, make sure all processes have finished writing before
00204      * continuing, to avoid nasty race conditions.
00205      * For example, many tests will write an archive then immediately read it back
00206      * in, which could easily break without this.
00207      */
00208     PetscTools::Barrier("~ArchiveOpener");
00209 }