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 
00033 #include "CommandLineArguments.hpp"
00034 #include "Exception.hpp"
00035 #include "PetscTools.hpp"
00036 #include "PetscException.hpp"
00037 #include "Version.hpp"
00038 #include "OutputFileHandler.hpp"
00039 #include <sys/utsname.h>
00040 
00041 #include "ChasteSerialization.hpp"
00042 #include <hdf5.h>
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         for (unsigned i=0; i<PetscTools::GetNumProcs(); i++)
00107         {
00108             if (i==PetscTools::GetMyRank())
00109             {
00110                 std::cout << "Chaste launched on process " << PetscTools::GetMyRank()
00111                     << " of " << PetscTools::GetNumProcs() << "." << std::endl << std::flush;
00112             }
00113             PetscTools::Barrier();
00114         }
00115     }
00116 }
00117 
00118 void ExecutableSupport::WriteMachineInfoFile(std::string fileBaseName)
00119 {
00120     OutputFileHandler out_file_handler(mOutputDirectory, false);
00121     std::stringstream file_name;
00122     file_name << fileBaseName << "_" << PetscTools::GetMyRank() << ".txt";
00123     out_stream out_file = out_file_handler.OpenOutputFile(file_name.str());
00124     *out_file << "Process " << PetscTools::GetMyRank() << " of "
00125         << PetscTools::GetNumProcs() << "." << std::endl << std::flush;
00126 
00127     struct utsname uts_info;
00128     uname(&uts_info);
00129 
00130     *out_file << "uname sysname  = " << uts_info.sysname << std::endl << std::flush;
00131     *out_file << "uname nodename = " << uts_info.nodename << std::endl << std::flush;
00132     *out_file << "uname release  = " << uts_info.release << std::endl << std::flush;
00133     *out_file << "uname version  = " << uts_info.version << std::endl << std::flush;
00134     *out_file << "uname machine  = " << uts_info.machine << std::endl << std::flush;
00135     char buffer[100];
00136     FILE * system_info;
00137 
00138     *out_file << "\nInformation on number and type of processors:\n";
00139     system_info = popen("grep ^model.name /proc/cpuinfo", "r");
00140     while ( fgets(buffer, 100, system_info) != NULL )
00141     {
00142         *out_file << buffer;
00143     }
00144     fclose(system_info);
00145 
00146     *out_file << "\nInformation on processor caches, in the same order as above:\n";
00147     system_info = popen("grep ^cache.size /proc/cpuinfo", "r");
00148     while ( fgets(buffer, 100, system_info) != NULL )
00149     {
00150         *out_file << buffer;
00151     }
00152     fclose(system_info);
00153 
00154     *out_file << "\nInformation on system memory:\n";
00155     system_info = popen("grep ^MemTotal /proc/meminfo", "r");
00156     while ( fgets(buffer, 100, system_info) != NULL )
00157     {
00158         *out_file << buffer;
00159     }
00160     fclose(system_info);
00161 
00162     out_file->close();
00163 }
00164 
00165 void ExecutableSupport::WriteProvenanceInfoFile()
00166 {
00167     OutputFileHandler out_file_handler(mOutputDirectory, false);
00168     out_stream out_file = out_file_handler.OpenOutputFile("provenance_info_", PetscTools::GetMyRank(), ".txt");
00169 
00170     //Compilation information
00171     std::stringstream provenance_msg;
00172     provenance_msg << "This version of Chaste was compiled on:\n";
00173     provenance_msg << ChasteBuildInfo::GetBuildTime() << " by " << ChasteBuildInfo::GetBuilderUnameInfo() << " (uname)\n";
00174     provenance_msg << "from revision number " << ChasteBuildInfo::GetRevisionNumber() << " with build type " << ChasteBuildInfo::GetBuildInformation() << ".\n\n";
00175     *out_file << provenance_msg.str();
00176 
00177     WriteLibraryInfo( out_file );
00178 
00179     out_file->close();
00180 }
00181 
00182 void ExecutableSupport::WriteLibraryInfo( out_stream &outFile )
00183 {
00184 
00185     *outFile << "<ChasteBuildInfo>\n";
00186 
00187     *outFile << "\t<ProvenanceInfo>\n";
00188     *outFile << "\t\t<VersionString>"<< ChasteBuildInfo::GetVersionString() << "</VersionString> <!-- build specific -->\n";
00189     *outFile << "\t\t<IsWorkingCopyModified>"<< ChasteBuildInfo::IsWorkingCopyModified() << "</IsWorkingCopyModified>\n";
00190     *outFile << "\t\t<BuildInformation>"<< ChasteBuildInfo::GetBuildInformation() << "</BuildInformation>\n";
00191     *outFile << "\t\t<BuildTime>"<< ChasteBuildInfo::GetBuildTime() << "</BuildTime>\n";
00192     *outFile << "\t\t<CurrentTime>"<< ChasteBuildInfo::GetCurrentTime() << "</CurrentTime>\n";
00193     *outFile << "\t\t<BuilderUnameInfo>"<< ChasteBuildInfo::GetBuilderUnameInfo() << "</BuilderUnameInfo>\n";
00194     *outFile << "\t</ProvenanceInfo>\n";
00195 
00196     *outFile << "\t<Compiler>\n";
00197     *outFile << "\t\t<NameAndVersion>" << ChasteBuildInfo::GetCompilerType() << ", version " << ChasteBuildInfo::GetCompilerVersion() << "</NameAndVersion>\n" ;
00198     *outFile << "\t\t<Flags>" << ChasteBuildInfo::GetCompilerFlags() << "</Flags>\n" ;
00199     *outFile << "\t</Compiler>\n";
00200 
00201     *outFile << "\t<Libraries>\n";
00202 
00203     *outFile << "\t\t<CompiledIn>\n";
00204     *outFile << "\t\t\t<PETSc>" << PETSC_VERSION_MAJOR << "." << PETSC_VERSION_MINOR << "." << PETSC_VERSION_SUBMINOR << "</PETSc>\n";
00205     *outFile << "\t\t\t<Boost>" << BOOST_VERSION  / 100000 << "." << BOOST_VERSION / 100 % 1000 << "." << BOOST_VERSION % 100 << "</Boost>\n";
00206     *outFile << "\t\t\t<HDF5>" << H5_VERS_MAJOR <<  "." << H5_VERS_MINOR << "." << H5_VERS_RELEASE << "</HDF5>\n";
00207     *outFile << "\t\t</CompiledIn>\n";
00208 
00209     *outFile << "\t\t<Binaries>\n";
00210     *outFile << "\t\t\t<XSD>" <<  ChasteBuildInfo::GetXsdVersion() << "</XSD>\n";
00211     *outFile << "\t\t</Binaries>\n";
00212 
00213     *outFile << "\t\t<Optional>\n";
00214 #ifdef CHASTE_VTK
00215     *outFile << "\t\t\t<VTK>" << VTK_MAJOR_VERSION << "." << VTK_MINOR_VERSION << "</VTK>\n";
00216 #else
00217     *outFile << "\t\t\t<VTK>no</VTK>\n";
00218 #endif
00219 
00220 #ifdef CHASTE_CVODE
00221     *outFile << "\t\t\t<SUNDIALS>" << SUNDIALS_PACKAGE_VERSION << "</SUNDIALS> <!-- includes Cvode of a different version number --> \n";
00222 #else
00223     *outFile << "\t\t\t<SUNDIALS>no</SUNDIALS>\n";
00224 #endif
00225 
00226 #ifdef CHASTE_ADAPTIVITY
00227     *outFile << "\t\t\t<Adaptivity>yes</Adaptivity>\n";
00228 #else
00229     *outFile << "\t\t\t<Adaptivity>no</Adaptivity>\n";
00230 #endif
00231     *outFile << "\t\t</Optional>\n";
00232 
00233     *outFile << "\t</Libraries>\n";
00234 
00235     *outFile << "</ChasteBuildInfo>\n";
00236 }
00237 
00238 void ExecutableSupport::StandardStartup(int* pArgc, char*** pArgv)
00239 {
00240     InitializePetsc(pArgc, pArgv);
00241     ShowCopyright();
00242     ShowParallelLaunching();
00243 }
00244 
00245 void ExecutableSupport::PrintError(const std::string& rMessage, bool masterOnly)
00246 {
00247     if (!masterOnly || PetscTools::AmMaster())
00248     {
00249         // Write the error message to stderr
00250         std::cerr << rMessage << std::endl;
00251     }
00252 
00253     // Write the error message to file
00254     OutputFileHandler out_file_handler(mOutputDirectory, false);
00255     out_stream out_file = out_file_handler.OpenOutputFile("chaste_errors_", PetscTools::GetMyRank(), ".txt", std::ios::out | std::ios::app);
00256     *out_file << rMessage << std::endl;
00257     out_file->close();
00258 }
00259 
00260 void ExecutableSupport::Print(const std::string& rMessage)
00261 {
00262     if (PetscTools::AmMaster())
00263     {
00264         // Write the error message to stdout
00265         std::cout << rMessage << std::endl << std::flush;
00266     }
00267 }
00268 
00269 void ExecutableSupport::FinalizePetsc()
00270 {
00271     PetscFinalize();
00272 }

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5