Sundance.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 //
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 //
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 
00044 
00045 #include "Sundance.hpp"
00046 #include "Teuchos_CommandLineProcessor.hpp"
00047 #include "PlayaMPIComm.hpp"
00048 #include "PlayaMPISession.hpp"
00049 #include "SundanceAssembler.hpp"
00050 #include "SundanceQuadratureIntegral.hpp"
00051 #include "SundanceQuadratureEvalMediator.hpp"
00052 #include "SundanceDiscreteFuncEvaluator.hpp"
00053 #include "SundanceRefIntegral.hpp"
00054 #include "SundancePathUtils.hpp"
00055 #include "SundanceEvaluator.hpp"
00056 #include "SundanceEvalVector.hpp"
00057 #include "SundanceCellJacobianBatch.hpp"
00058 #include "SundanceUnaryFunctor.hpp"
00059 #include "SundanceGrouperBase.hpp"
00060 #include "SundanceSparsitySuperset.hpp"
00061 #include "SundanceDefaultPath.hpp"
00062 #include "SundanceDOFMapBuilder.hpp"
00063 #include "SundanceVersionString.hpp"
00064 #include "SundanceProductTransformation.hpp"
00065 #include <unistd.h>
00066 #ifndef _MSC_VER
00067 #include <sys/unistd.h>
00068 #else
00069 #include "winprocess.h"
00070 #endif
00071 #ifdef HAVE_MPI
00072 #include "mpi.h"
00073 #endif
00074 
00075 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00076 #include "PlayaLinearOperatorImpl.hpp"
00077 #endif
00078 
00079 namespace Sundance
00080 {
00081   using Playa::MPIDataType;
00082   using Playa::MPIOp;
00083   using std::ostringstream;
00084 
00085 
00086 void handleException(std::exception& e)
00087 {SundanceGlobal::handleException(e);}
00088 
00089 
00090 bool passFailTest(bool pass)
00091 {return SundanceGlobal::passFailTest(pass);}
00092 
00093 bool passFailTest(double error, double tol)
00094 {return SundanceGlobal::passFailTest(error, tol);}
00095 
00096 
00097 bool passFailTest(const std::string& statusMsg,
00098   bool status, double error, double tol)
00099 {return SundanceGlobal::passFailTest(statusMsg, status, error, tol);}
00100 
00101 int& testStatus()
00102 {return SundanceGlobal::testStatus();}
00103 
00104 
00105 CommandLineProcessor& clp()
00106 {return SundanceGlobal::clp();}
00107 
00108 
00109 int init(int* argc, char*** argv)
00110 {return SundanceGlobal::init(argc, argv);}
00111 
00112 
00113 int finalize() {return SundanceGlobal::finalize();}
00114 
00115 
00116 
00117 void setOption(const std::string& optionName,
00118   int& value,
00119   const std::string& helpMsg)
00120 {
00121   SundanceGlobal::setOption(optionName, value, helpMsg);
00122 }
00123 
00124 void setOption(const std::string& optionName,
00125   double& value,
00126   const std::string& helpMsg)
00127 {
00128   SundanceGlobal::setOption(optionName, value, helpMsg);
00129 }
00130 
00131 void setOption(const std::string& optionName,
00132   std::string& value,
00133   const std::string& helpMsg)
00134 {
00135   SundanceGlobal::setOption(optionName, value, helpMsg);
00136 }
00137 
00138 void setOption(const std::string& optionTrueName,
00139   const std::string& optionFalseName,
00140   bool& value,
00141   const std::string& helpMsg)
00142 {
00143   SundanceGlobal::setOption(optionTrueName,
00144     optionFalseName,
00145     value,
00146     helpMsg);
00147 }
00148 } // namespace Sundance
00149 
00150 static Time& totalTimer()
00151 {
00152   static RCP<Time> rtn
00153     = TimeMonitor::getNewTimer("total Sundance time");
00154   return *rtn;
00155 }
00156 
00157 int SundanceGlobal::init(int* argc, char*** argv)
00158 {
00159 
00160   try
00161   {
00162     /* start up MPI. In a serial run, this will be a no-op */
00163     //      MPISession::init(argc, argv);
00164     MPISession::init(argc, (char***) argv);
00165 
00166     /* Start a stopwatch. It will be stopped upon a call to finalize() */
00167     totalTimer().start();
00168 
00169     Tabs tab;
00170 
00171     /* read standard command line flags */
00172     std::string configFilename = "";
00173 
00174     bool defaultFpCheck = false;
00175     bool debugWait = false;
00176     bool showVersion = false;
00177     bool showBanner = true;
00178     bool showTimings = false;
00179     bool cmdFpCheck = defaultFpCheck;
00180     int defaultWorkSetSize = 400;
00181     int cmdWorkSetSize = defaultWorkSetSize;
00182 
00183     Assembler::workSetSize() = defaultWorkSetSize;
00184 
00185     clp().setOption("config", &configFilename, "Configuration file");
00186     clp().setOption("fpcheck", "nofpcheck", &cmdFpCheck,
00187       "Check results of math lib calls in expr evals");
00188     clp().setOption("version", "noversion", &showVersion,
00189       "Show Sundance version number and exit");
00190     clp().setOption("banner", "nobanner", &showBanner,
00191       "Show Sundance banner on root processor at start of run");
00192     clp().setOption("timings", "notimings", &showTimings,
00193       "Show timings at end of run");
00194 
00195     clp().setOption("workset", &cmdWorkSetSize,
00196       "Work set size");
00197 
00198 
00199     clp().setOption("debug", "nodebug", &debugWait, "Whether to attach a debugger to this process, holding until 'wait' is set to 0");
00200 
00201 
00202     clp().throwExceptions(false);
00203     clp().recogniseAllOptions(false);
00204 
00205     CommandLineProcessor::EParseCommandLineReturn rtn
00206       = clp().parse(*argc, (char**) *argv);
00207 
00208     TEUCHOS_TEST_FOR_EXCEPTION(rtn != CommandLineProcessor::PARSE_SUCCESSFUL,
00209       std::runtime_error,
00210       "Command-line parsing failed");
00211 
00212 
00213     if (showVersion)
00214     {
00215       if (MPIComm::world().getRank()==0)
00216       {
00217         cout << "Simulation built using Sundance version "
00218              << VersionString::number()
00219              << " (" << VersionString::date() << ")" << std::endl;
00220         exit(0);
00221       }
00222     }
00223     if (showBanner && MPIComm::world().getRank()==0)
00224     {
00225       ostringstream oss;
00226       oss << "Simulation built using Sundance version "
00227            << VersionString::number()
00228            << " (" << VersionString::date() << ")" << std::endl;
00229 
00230       oss << "Sundance is copyright"
00231            << std::endl << " (C) 2005-2012 Sandia National Laboratories "
00232            << std::endl
00233            << " (C) 2007-2012 Texas Tech University"
00234            << std::endl;
00235       oss << "and is licensed under the BSD License" << std::endl;
00236       oss << std::endl;
00237       cout << oss.str() << std::flush;
00238     }
00239 
00240     MPIComm::world().synchronize();
00241     if (!showTimings) skipTimingOutput() = true;
00242 
00243     //      debugWait = true;
00244     if (debugWait)
00245     {
00246       int wait=1;
00247       int pid = getpid();
00248       std::string myCommandName=((char**)(*argv))[0];
00249       std::string debugCmd = "ddd --gdb -x ~/.gdbinit " + myCommandName
00250         + " " + Teuchos::toString(pid) + " &";
00251       cout << "launching " << debugCmd << std::endl;
00252       TEUCHOS_ASSERT_EQUALITY(0, system(debugCmd.c_str()));
00253       while (wait) {;}
00254     }
00255 
00256 
00257     bool worksetSetOnCmdLine = cmdWorkSetSize != defaultWorkSetSize;
00258     if (worksetSetOnCmdLine)
00259     {
00260       Assembler::workSetSize() = (int) cmdWorkSetSize;
00261     }
00262   }
00263   catch(std::exception& e)
00264   {
00265     handleException(e);
00266     return 1;
00267   }
00268   return 0;
00269 }
00270 
00271 
00272 bool& SundanceGlobal::showStartupMessage()
00273 {
00274   return MPISession::showStartupMessage();
00275 }
00276 
00277 
00278 
00279 void SundanceGlobal::handleException(std::exception& e)
00280 {
00281   cout << "Sundance detected exception: " << std::endl;
00282   cout << e.what() << std::endl;
00283   cout << "test FAILED" << std::endl;
00284   testStatus() = -1;
00285 }
00286 
00287 
00288 int SundanceGlobal::finalize()
00289 {
00290   totalTimer().stop();
00291 
00292   try
00293   {
00294     Tabs tab;
00295     /* we may need to skip timing summaries because of a Trilinos 6.0.x bug */
00296     if (!skipTimingOutput()) TimeMonitor::summarize();
00297     //  MPISession::finalize();
00298   }
00299   catch(std::exception& e)
00300   {
00301     handleException(e);
00302     return 1;
00303   }
00304   return 0;
00305 }
00306 
00307 
00308 
00309 
00310 
00311 
00312 bool SundanceGlobal::checkTest(double error, double tol)
00313 {
00314   int myFail = error > tol;
00315   int anyFail = myFail;
00316   MPIComm::world().allReduce((void*) &myFail, (void*) &anyFail, 1, MPIDataType::intType(),
00317     MPIOp::sumOp());
00318   return (anyFail == 0);
00319 }
00320 
00321 bool SundanceGlobal:: passFailTest(double error, double tol)
00322 {
00323   MPIComm::world().synchronize();
00324   bool pass;
00325   if (MPIComm::world().getRank()==0)
00326   {
00327     cout << "error norm = " << error << std::endl;
00328     cout << "tolerance = " << tol << std::endl;
00329   }
00330   pass = checkTest(error, tol);
00331   if (MPIComm::world().getRank()==0)
00332   {
00333     if (pass)
00334     {
00335       cout << "test PASSED" << std::endl;
00336     }
00337     else
00338     {
00339       cout << "test FAILED" << std::endl;
00340     }
00341   }
00342   testStatus() = pass!=true;
00343   return pass;
00344 }
00345 
00346 
00347 bool SundanceGlobal:: passFailTest(const std::string& statusMsg,
00348   bool status, double error, double tol)
00349 {
00350   MPIComm::world().synchronize();
00351   bool pass;
00352   if (MPIComm::world().getRank()==0)
00353   {
00354 
00355     cout << statusMsg << ": ";
00356     if (status) cout << "true" << std::endl;
00357     else cout << "false" << std::endl;
00358     cout << "error norm = " << error << std::endl;
00359     cout << "tolerance = " << tol << std::endl;
00360   }
00361   pass = checkTest(error, tol);
00362   if (MPIComm::world().getRank()==0)
00363   {
00364     if (status && pass)
00365     {
00366       cout << "test PASSED" << std::endl;
00367     }
00368     else
00369     {
00370       cout << "test FAILED" << std::endl;
00371     }
00372   }
00373   testStatus() = pass!=true;
00374   return pass;
00375 }
00376 
00377 bool SundanceGlobal:: passFailTest(bool pass)
00378 {
00379   MPIComm::world().synchronize();
00380   if (MPIComm::world().getRank()==0)
00381   {
00382     if (pass)
00383     {
00384       cout << "test PASSED" << std::endl;
00385     }
00386     else
00387     {
00388       cout << "test FAILED" << std::endl;
00389     }
00390   }
00391   testStatus() = pass!=true;
00392   return pass;
00393 }
00394 
00395 
00396 void SundanceGlobal::setOption(const std::string& optionName,
00397   int& value,
00398   const std::string& helpMsg)
00399 {
00400   clp().setOption(optionName.c_str(), &value, helpMsg.c_str());
00401 }
00402 
00403 void SundanceGlobal::setOption(const std::string& optionName,
00404   double& value,
00405   const std::string& helpMsg)
00406 {
00407   clp().setOption(optionName.c_str(), &value, helpMsg.c_str());
00408 }
00409 
00410 void SundanceGlobal::setOption(const std::string& optionName,
00411   std::string& value,
00412   const std::string& helpMsg)
00413 {
00414   clp().setOption(optionName.c_str(), &value, helpMsg.c_str());
00415 }
00416 
00417 void SundanceGlobal::setOption(const std::string& optionTrueName,
00418   const std::string& optionFalseName,
00419   bool& value,
00420   const std::string& helpMsg)
00421 {
00422   clp().setOption(optionTrueName.c_str(),
00423     optionFalseName.c_str(),
00424     &value,
00425     helpMsg.c_str());
00426 }
00427 
00428 

Site Contact