ArchiveOpener.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 // Must be included before any other serialization headers
00030 #include <boost/archive/text_oarchive.hpp>
00031 #include <boost/archive/text_iarchive.hpp>
00032 
00033 #include <sstream>
00034 #include <iostream>
00035 
00036 #include "ArchiveOpener.hpp"
00037 #include "ArchiveLocationInfo.hpp"
00038 #include "ProcessSpecificArchive.hpp"
00039 #include "Exception.hpp"
00040 #include "OutputFileHandler.hpp"
00041 
00048 template<>
00049 ArchiveOpener<boost::archive::text_iarchive, std::ifstream>::ArchiveOpener(
00050         const FileFinder& rDirectory,
00051         const std::string& rFileNameBase,
00052         unsigned procId)
00053     : mpCommonStream(NULL),
00054       mpPrivateStream(NULL),
00055       mpCommonArchive(NULL),
00056       mpPrivateArchive(NULL)
00057 {
00058     // Figure out where things live
00059     ArchiveLocationInfo::SetArchiveDirectory(rDirectory);
00060     std::string private_path = ArchiveLocationInfo::GetProcessUniqueFilePath(rFileNameBase, procId);
00061     std::stringstream common_path;
00062     common_path << ArchiveLocationInfo::GetArchiveDirectory() << rFileNameBase;
00063 
00064     // Try to open the main archive for replicated data
00065     mpCommonStream = new std::ifstream(common_path.str().c_str(), std::ios::binary);
00066     if (!mpCommonStream->is_open())
00067     {
00068         delete mpCommonStream;
00069         EXCEPTION("Cannot load main archive file: " + common_path.str());
00070     }
00071 
00072     try
00073     {
00074         mpCommonArchive = new boost::archive::text_iarchive(*mpCommonStream);
00075     }
00076     catch (boost::archive::archive_exception& boost_exception)
00077     {
00078         if (boost_exception.code == boost::archive::archive_exception::unsupported_version)
00079         {
00080             // This is forward compatibility issue.  We can't open the archive because it's been written by a more recent Boost.
00081             delete mpCommonArchive;
00082             delete mpCommonStream;
00083             EXCEPTION("Could not open Boost archive '" + common_path.str() + "' because it was written by a more recent Boost.  Check process-specific archives too");
00084         }
00085         else
00086         {
00087         // We don't understand the exception, so we shouldn't continue
00088 #define COVERAGE_IGNORE
00089             throw boost_exception;
00090 #undef COVERAGE_IGNORE
00091         }
00092     }
00093     // Try to open the secondary archive for distributed data
00094     mpPrivateStream = new std::ifstream(private_path.c_str(), std::ios::binary);
00095     if (!mpPrivateStream->is_open())
00096     {
00097         delete mpPrivateStream;
00098         delete mpCommonArchive;
00099         delete mpCommonStream;
00100         EXCEPTION("Cannot load secondary archive file: " + private_path);
00101     }
00102     mpPrivateArchive = new boost::archive::text_iarchive(*mpPrivateStream);
00103     ProcessSpecificArchive<boost::archive::text_iarchive>::Set(mpPrivateArchive);
00104 }
00105 
00106 template<>
00107 ArchiveOpener<boost::archive::text_iarchive, std::ifstream>::~ArchiveOpener()
00108 {
00109     ProcessSpecificArchive<boost::archive::text_iarchive>::Set(NULL);
00110     delete mpPrivateArchive;
00111     delete mpPrivateStream;
00112     delete mpCommonArchive;
00113     delete mpCommonStream;
00114 }
00115 
00122 template<>
00123 ArchiveOpener<boost::archive::text_oarchive, std::ofstream>::ArchiveOpener(
00124         const FileFinder& rDirectory,
00125         const std::string& rFileNameBase,
00126         unsigned procId)
00127     : mpCommonStream(NULL),
00128       mpPrivateStream(NULL),
00129       mpCommonArchive(NULL),
00130       mpPrivateArchive(NULL)
00131 {
00132     // Check for user error
00133     if (procId != PetscTools::GetMyRank())
00134     {
00135         EXCEPTION("Specifying the secondary archive file ID doesn't make sense when writing.");
00136     }
00137     // Figure out where things live
00138     ArchiveLocationInfo::SetArchiveDirectory(rDirectory);
00139     if (ArchiveLocationInfo::GetIsDirRelativeToChasteTestOutput())
00140     {
00141         // Ensure the directory exists
00142         OutputFileHandler handler(ArchiveLocationInfo::GetArchiveRelativePath(), false);
00143     }
00144     std::string private_path = ArchiveLocationInfo::GetProcessUniqueFilePath(rFileNameBase);
00145     std::stringstream common_path;
00146     common_path << ArchiveLocationInfo::GetArchiveDirectory() << rFileNameBase;
00147 
00148     // Create master archive for replicated data
00149     if (PetscTools::AmMaster())
00150     {
00151         mpCommonStream = new std::ofstream(common_path.str().c_str());
00152         if (!mpCommonStream->is_open())
00153         {
00154             delete mpCommonStream;
00155             EXCEPTION("Failed to open main archive file for writing: " + common_path.str());
00156         }
00157     }
00158     else
00159     {
00160         // Non-master processes need to go through the serialization methods,
00161         // but not write any data.
00162         mpCommonStream = new std::ofstream("/dev/null");
00163         #define COVERAGE_IGNORE
00164         if (!mpCommonStream->is_open())
00165         {
00166             delete mpCommonStream;
00167             EXCEPTION("Failed to open dummy archive file '/dev/null' for writing");
00168         }
00169         #undef COVERAGE_IGNORE
00170     }
00171     mpCommonArchive = new boost::archive::text_oarchive(*mpCommonStream);
00172 
00173     // Create secondary archive for distributed data
00174     mpPrivateStream = new std::ofstream(private_path.c_str());
00175     if (!mpPrivateStream->is_open())
00176     {
00177         delete mpPrivateStream;
00178         delete mpCommonArchive;
00179         delete mpCommonStream;
00180         EXCEPTION("Failed to open secondary archive file for writing: " + private_path);
00181     }
00182     mpPrivateArchive = new boost::archive::text_oarchive(*mpPrivateStream);
00183     ProcessSpecificArchive<boost::archive::text_oarchive>::Set(mpPrivateArchive);
00184 }
00185 
00186 template<>
00187 ArchiveOpener<boost::archive::text_oarchive, std::ofstream>::~ArchiveOpener()
00188 {
00189     ProcessSpecificArchive<boost::archive::text_oarchive>::Set(NULL);
00190     delete mpPrivateArchive;
00191     delete mpPrivateStream;
00192     delete mpCommonArchive;
00193     delete mpCommonStream;
00194 
00195     /* In a parallel setting, make sure all processes have finished writing before
00196      * continuing, to avoid nasty race conditions.
00197      * For example, many tests will write an archive then immediately read it back
00198      * in, which could easily break without this.
00199      */
00200     PetscTools::Barrier("~ArchiveOpener");
00201 }
00202 
00203 
00204 

Generated on Tue May 31 14:31:41 2011 for Chaste by  doxygen 1.5.5