ExecutableSupport.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 #include "ExecutableSupport.hpp"
00030 
00031 #include <iostream>
00032 #include <sstream>
00033 #include <sys/utsname.h>
00034 #include <hdf5.h>
00035 
00036 #include "CommandLineArguments.hpp"
00037 #include "Exception.hpp"
00038 #include "PetscTools.hpp"
00039 #include "PetscException.hpp"
00040 #include "Version.hpp"
00041 #include "OutputFileHandler.hpp"
00042 #include "ChasteSerialization.hpp"
00043 
00044 #ifdef CHASTE_VTK
00045 #define _BACKWARD_BACKWARD_WARNING_H 1 //Cut out the strstream deprecated warning for now (gcc4.3)
00046 #include <vtkVersion.h>
00047 #endif
00048 
00049 #ifdef CHASTE_CVODE
00050 #include <sundials/sundials_config.h>
00051 #endif
00052 //#include <xsd/cxx/version.hxx>
00053 
00054 std::string ExecutableSupport::mOutputDirectory;
00055 
00056 void ExecutableSupport::SetOutputDirectory(const std::string& rOutputDirectory)
00057 {
00058     mOutputDirectory = rOutputDirectory;
00059 }
00060 
00061 void ExecutableSupport::InitializePetsc(int* pArgc, char*** pArgv)
00062 {
00063     // Store the arguments in case other code needs them
00064     CommandLineArguments::Instance()->p_argc = pArgc;
00065     CommandLineArguments::Instance()->p_argv = pArgv;
00066     // Initialise PETSc
00067     PETSCEXCEPT(PetscInitialize(pArgc, pArgv, PETSC_NULL, PETSC_NULL));
00068 }
00069 
00070 void ExecutableSupport::ShowCopyright()
00071 {
00072     //Compilation information
00073     std::stringstream provenance_msg;
00074     provenance_msg << "This version of Chaste was compiled on:\n";
00075     provenance_msg << ChasteBuildInfo::GetBuildTime() << " by " << ChasteBuildInfo::GetBuilderUnameInfo() << " (uname)\n";
00076     provenance_msg << "from revision number " << ChasteBuildInfo::GetRevisionNumber() << " with build type " << ChasteBuildInfo::GetBuildInformation() << ".\n\n";
00077 
00078     //Only show one copy of copyright/header
00079     if (PetscTools::AmMaster())
00080     {
00081         std::cout << "Copyright (C) University of Oxford, 2005-2011 \n\n\
00082 \
00083 Chaste is free software: you can redistribute it and/or modify \n\
00084 it under the terms of the Lesser GNU General Public License as published by \n\
00085 the Free Software Foundation, either version 2.1 of the License, or \n\
00086 (at your option) any later version. \n\n\
00087 \
00088 Chaste is distributed in the hope that it will be useful, \n\
00089 but WITHOUT ANY WARRANTY; without even the implied warranty of \n\
00090 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the \n\
00091 Lesser GNU General Public License for more details. \n\n\
00092 \
00093 You should have received a copy of the Lesser GNU General Public License \n\
00094 along with Chaste.  If not, see <http://www.gnu.org/licenses/>.\n\n";
00095 
00096         //Write provenance information to stdout
00097         std::cout << provenance_msg.str() << std::flush;
00098     }
00099 }
00100 
00101 void ExecutableSupport::ShowParallelLaunching()
00102 {
00103     if (PetscTools::IsParallel())
00104     {
00106         PetscTools::BeginRoundRobin();
00107         std::cout << "Chaste launched on process " << PetscTools::GetMyRank()
00108             << " of " << PetscTools::GetNumProcs() << "." << std::endl << std::flush;
00109         PetscTools::EndRoundRobin();
00110     }
00111 }
00112 
00113 void ExecutableSupport::WriteMachineInfoFile(std::string fileBaseName)
00114 {
00115     OutputFileHandler out_file_handler(mOutputDirectory, false);
00116     std::stringstream file_name;
00117     file_name << fileBaseName << "_" << PetscTools::GetMyRank() << ".txt";
00118     out_stream out_file = out_file_handler.OpenOutputFile(file_name.str());
00119     *out_file << "Process " << PetscTools::GetMyRank() << " of "
00120         << PetscTools::GetNumProcs() << "." << std::endl << std::flush;
00121 
00122     struct utsname uts_info;
00123     uname(&uts_info);
00124 
00125     *out_file << "uname sysname  = " << uts_info.sysname << std::endl << std::flush;
00126     *out_file << "uname nodename = " << uts_info.nodename << std::endl << std::flush;
00127     *out_file << "uname release  = " << uts_info.release << std::endl << std::flush;
00128     *out_file << "uname version  = " << uts_info.version << std::endl << std::flush;
00129     *out_file << "uname machine  = " << uts_info.machine << std::endl << std::flush;
00130     char buffer[100];
00131     FILE * system_info;
00132 
00133     *out_file << "\nInformation on number and type of processors:\n";
00134     system_info = popen("grep ^model.name /proc/cpuinfo", "r");
00135     while ( fgets(buffer, 100, system_info) != NULL )
00136     {
00137         *out_file << buffer;
00138     }
00139     fclose(system_info);
00140 
00141     *out_file << "\nInformation on processor caches, in the same order as above:\n";
00142     system_info = popen("grep ^cache.size /proc/cpuinfo", "r");
00143     while ( fgets(buffer, 100, system_info) != NULL )
00144     {
00145         *out_file << buffer;
00146     }
00147     fclose(system_info);
00148 
00149     *out_file << "\nInformation on system memory:\n";
00150     system_info = popen("grep ^MemTotal /proc/meminfo", "r");
00151     while ( fgets(buffer, 100, system_info) != NULL )
00152     {
00153         *out_file << buffer;
00154     }
00155     fclose(system_info);
00156 
00157     out_file->close();
00158 }
00159 
00160 void ExecutableSupport::WriteProvenanceInfoFile()
00161 {
00162     OutputFileHandler out_file_handler(mOutputDirectory, false);
00163     out_stream out_file = out_file_handler.OpenOutputFile("provenance_info_", PetscTools::GetMyRank(), ".txt");
00164 
00165     //Compilation information
00166     std::stringstream provenance_msg;
00167     provenance_msg << "This version of Chaste was compiled on:\n";
00168     provenance_msg << ChasteBuildInfo::GetBuildTime() << " by " << ChasteBuildInfo::GetBuilderUnameInfo() << " (uname)\n";
00169     provenance_msg << "from revision number " << ChasteBuildInfo::GetRevisionNumber() << " with build type " << ChasteBuildInfo::GetBuildInformation() << ".\n\n";
00170     *out_file << provenance_msg.str();
00171 
00172     WriteLibraryInfo( out_file );
00173 
00174     out_file->close();
00175 }
00176 
00177 void ExecutableSupport::WriteLibraryInfo( out_stream &outFile )
00178 {
00179 
00180     *outFile << "<ChasteBuildInfo>\n";
00181 
00182     *outFile << "\t<ProvenanceInfo>\n";
00183     *outFile << "\t\t<VersionString>"<< ChasteBuildInfo::GetVersionString() << "</VersionString> <!-- build specific -->\n";
00184     *outFile << "\t\t<IsWorkingCopyModified>"<< ChasteBuildInfo::IsWorkingCopyModified() << "</IsWorkingCopyModified>\n";
00185     *outFile << "\t\t<BuildInformation>"<< ChasteBuildInfo::GetBuildInformation() << "</BuildInformation>\n";
00186     *outFile << "\t\t<BuildTime>"<< ChasteBuildInfo::GetBuildTime() << "</BuildTime>\n";
00187     *outFile << "\t\t<CurrentTime>"<< ChasteBuildInfo::GetCurrentTime() << "</CurrentTime>\n";
00188     *outFile << "\t\t<BuilderUnameInfo>"<< ChasteBuildInfo::GetBuilderUnameInfo() << "</BuilderUnameInfo>\n";
00189     *outFile << "\t</ProvenanceInfo>\n";
00190 
00191     *outFile << "\t<Compiler>\n";
00192     *outFile << "\t\t<NameAndVersion>" << ChasteBuildInfo::GetCompilerType() << ", version " << ChasteBuildInfo::GetCompilerVersion() << "</NameAndVersion>\n" ;
00193     *outFile << "\t\t<Flags>" << ChasteBuildInfo::GetCompilerFlags() << "</Flags>\n" ;
00194     *outFile << "\t</Compiler>\n";
00195 
00196     *outFile << "\t<Libraries>\n";
00197 
00198     *outFile << "\t\t<CompiledIn>\n";
00199     *outFile << "\t\t\t<PETSc>" << PETSC_VERSION_MAJOR << "." << PETSC_VERSION_MINOR << "." << PETSC_VERSION_SUBMINOR << "</PETSc>\n";
00200     *outFile << "\t\t\t<Boost>" << BOOST_VERSION  / 100000 << "." << BOOST_VERSION / 100 % 1000 << "." << BOOST_VERSION % 100 << "</Boost>\n";
00201     *outFile << "\t\t\t<HDF5>" << H5_VERS_MAJOR <<  "." << H5_VERS_MINOR << "." << H5_VERS_RELEASE << "</HDF5>\n";
00202     *outFile << "\t\t</CompiledIn>\n";
00203 
00204     *outFile << "\t\t<Binaries>\n";
00205     *outFile << "\t\t\t<XSD>" <<  ChasteBuildInfo::GetXsdVersion() << "</XSD>\n";
00206     *outFile << "\t\t</Binaries>\n";
00207 
00208     *outFile << "\t\t<Optional>\n";
00209 #ifdef CHASTE_VTK
00210     *outFile << "\t\t\t<VTK>" << VTK_MAJOR_VERSION << "." << VTK_MINOR_VERSION << "</VTK>\n";
00211 #else
00212     *outFile << "\t\t\t<VTK>no</VTK>\n";
00213 #endif
00214 
00215 #ifdef CHASTE_CVODE
00216     *outFile << "\t\t\t<SUNDIALS>" << SUNDIALS_PACKAGE_VERSION << "</SUNDIALS> <!-- includes Cvode of a different version number --> \n";
00217 #else
00218     *outFile << "\t\t\t<SUNDIALS>no</SUNDIALS>\n";
00219 #endif
00220 
00221 #ifdef CHASTE_ADAPTIVITY
00222     *outFile << "\t\t\t<Adaptivity>yes</Adaptivity>\n";
00223 #else
00224     *outFile << "\t\t\t<Adaptivity>no</Adaptivity>\n";
00225 #endif
00226     *outFile << "\t\t</Optional>\n";
00227 
00228     *outFile << "\t</Libraries>\n";
00229 
00230     *outFile << "</ChasteBuildInfo>\n";
00231 }
00232 
00233 void ExecutableSupport::StandardStartup(int* pArgc, char*** pArgv)
00234 {
00235     InitializePetsc(pArgc, pArgv);
00236     ShowCopyright();
00237     ShowParallelLaunching();
00238 }
00239 
00240 void ExecutableSupport::PrintError(const std::string& rMessage, bool masterOnly)
00241 {
00242     if (!masterOnly || PetscTools::AmMaster())
00243     {
00244         // Write the error message to stderr
00245         std::cerr << rMessage << std::endl;
00246     }
00247 
00248     // Write the error message to file
00249     OutputFileHandler out_file_handler(mOutputDirectory, false);
00250     out_stream out_file = out_file_handler.OpenOutputFile("chaste_errors_", PetscTools::GetMyRank(), ".txt", std::ios::out | std::ios::app);
00251     *out_file << rMessage << std::endl;
00252     out_file->close();
00253 }
00254 
00255 void ExecutableSupport::Print(const std::string& rMessage)
00256 {
00257     if (PetscTools::AmMaster())
00258     {
00259         // Write the error message to stdout
00260         std::cout << rMessage << std::endl << std::flush;
00261     }
00262 }
00263 
00264 void ExecutableSupport::FinalizePetsc()
00265 {
00266     PetscFinalize();
00267 }

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