PlayaMPISession.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 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 #include "PlayaMPISession.hpp"
00043 #include "PlayaMPIDataType.hpp"
00044 #include "PlayaMPIOp.hpp"
00045 #include "Teuchos_Assert.hpp"
00046 
00047 namespace Playa
00048 {
00049 using namespace Teuchos;
00050 
00051 int MPISession::rank_ = 0 ;
00052 int MPISession::nProc_ = 1 ;
00053 
00054 MPISession::MPISession(int* argc, char*** argv)
00055 {
00056   #ifdef HAVE_MPI
00057   /* initialize MPI */
00058   int mpiHasBeenStarted = 0;
00059   MPI_Initialized(& mpiHasBeenStarted);
00060   int mpierr = 0 ;
00061   if (!mpiHasBeenStarted)
00062     {
00063       mpierr = ::MPI_Init (argc, (char ***) argv);
00064       TEUCHOS_TEST_FOR_EXCEPTION(mpierr != 0, std::runtime_error,
00065                          "Error code=" << mpierr 
00066                          << " detected in MPI_Init()");
00067     }
00068   
00069   /* find rank */
00070   mpierr = ::MPI_Comm_rank (MPI_COMM_WORLD, &rank_);
00071   TEUCHOS_TEST_FOR_EXCEPTION(mpierr != 0, std::runtime_error,
00072                      "Error code=" << mpierr 
00073                      << " detected in MPI_Comm_rank()");
00074 
00075   /* find number of procs */
00076   mpierr = ::MPI_Comm_size (MPI_COMM_WORLD, &nProc_);
00077 
00078   TEUCHOS_TEST_FOR_EXCEPTION(mpierr != 0, std::runtime_error,
00079                      "Error code=" << mpierr 
00080                      << " detected in MPI_Comm_size()");
00081 
00082   /* get machine name */
00083   int nameLen;
00084   char procName[MPI_MAX_PROCESSOR_NAME];
00085   mpierr = ::MPI_Get_processor_name(procName,&nameLen);
00086 
00087   TEUCHOS_TEST_FOR_EXCEPTION(mpierr != 0, std::runtime_error,
00088                      "Error code=" << mpierr 
00089                      << " detected in MPI_Get_processor_name()");
00090 
00091   if (showStartupMessage())
00092     {
00093       std::cerr << "Playa::MPISession::init() started processor " 
00094            << procName << std::endl;
00095     }
00096   else
00097     {
00098 #else
00099   std::cerr << "Playa::MPISession::init() started serial run" << std::endl;
00100 #endif
00101 #ifdef HAVE_MPI
00102     }
00103 #endif
00104 }
00105 
00106 
00107 void MPISession::init(int* argc, char*** argv)
00108 {
00109   static RCP<MPISession> session = rcp(new MPISession(argc, argv));
00110 }
00111 
00112 void MPISession::init(int* argc, void*** argv)
00113 {
00114   MPISession::init(argc, (char***)argv);
00115 }
00116 
00117 void MPISession::finalize()
00118 {
00119   // nothing to do here
00120 }
00121 
00122 
00123 MPISession::~MPISession()
00124 {
00125 #ifdef HAVE_MPI
00126   int mpiHasBeenFinalized = 0;
00127   MPI_Finalized(& mpiHasBeenFinalized);
00128 
00129   if (!mpiHasBeenFinalized)
00130   {
00131 #define BROKEN_MPI_REGISTRIES
00132 #ifndef BROKEN_MPI_REGISTRIES
00133     MPIDataType::clearTypeRegistry();
00134     MPIOp::clearOpRegistry();
00135 #endif
00136     int mpierr = ::MPI_Finalize();
00137     
00138     TEUCHOS_TEST_FOR_EXCEPTION(mpierr != 0, std::runtime_error,
00139       "Error code=" << mpierr << " detected in MPI_Finalize()");
00140   }
00141 #endif
00142 }
00143 
00144 }

Site Contact