Zoltan2
Zoltan2_Util.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #include <Zoltan2_Util.hpp>
00051 #include <Teuchos_OpaqueWrapper.hpp>
00052 
00053 #include <iostream>
00054 #include <sstream>
00055 #include <fstream>
00056 #include <string>
00057 
00058 #include <unistd.h>
00059 
00060 namespace Zoltan2{
00061 
00062 /* On a linux node, find the total memory currently allocated
00063  * to this process.
00064  * Return the number of kilobytes allocated to this process.
00065  * Return 0 if it is not possible to determine this.
00066  */
00067 long getProcessKilobytes()
00068 {
00069 long pageSize;
00070 
00071 #ifdef _SC_PAGESIZE
00072   pageSize = sysconf(_SC_PAGESIZE);
00073 #else
00074 #warning "Page size query is not possible.  No per-process memory stats."
00075   return 0;
00076 #endif
00077 
00078   pid_t pid = getpid();
00079   std::ostringstream fname;
00080   fname << "/proc/" << pid << "/statm";
00081   std::ifstream memFile;
00082 
00083   try{
00084     memFile.open(fname.str().c_str());
00085   }
00086   catch (...){
00087     return 0;
00088   }
00089 
00090   char buf[128];
00091   memset(buf, 0, 128);
00092   while (memFile.good()){
00093     memFile.getline(buf, 128);
00094     break;
00095   }
00096 
00097   memFile.close();
00098 
00099   std::istringstream sbuf(buf);
00100   long totalPages;
00101   sbuf >> totalPages;
00102 
00103   long pageKBytes = pageSize / 1024;
00104   totalPages = atol(buf);
00105 
00106   return totalPages * pageKBytes;
00107 }
00108 
00109 
00110 
00111 #ifdef HAVE_ZOLTAN2_MPI
00112 
00116 RCP<Teuchos::MpiComm<int> >
00117   MPI2Teuchos(const MPI_Comm &comm)
00118 {
00119   typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00120   RCP<mpiWrapper_t>handle = Teuchos::opaqueWrapper<MPI_Comm>(comm);
00121   RCP<Teuchos::MpiComm<int> > tcommPtr(
00122     new Teuchos::MpiComm<int>(handle));
00123 
00124   return tcommPtr;
00125 }
00126 
00127 RCP<const Teuchos::MpiComm<int> >
00128   MPI2TeuchosConst(const MPI_Comm &comm)
00129 {
00130   typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00131   RCP<mpiWrapper_t>handle = Teuchos::opaqueWrapper<MPI_Comm>(comm);
00132   RCP<const Teuchos::MpiComm<int> > tcommPtr(
00133     new Teuchos::MpiComm<int>(handle));
00134 
00135   return tcommPtr;
00136 }
00137 
00141 MPI_Comm
00142   Teuchos2MPI(const RCP<Comm<int> > &comm)
00143 {
00144   MPI_Comm mpiComm;
00145   typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00146 
00147   Comm<int> *c = comm.get();
00148   Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
00149   if (mc){
00150     RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
00151     mpiComm = (*wrappedComm.getRawPtr())();
00152   }
00153   else{
00154     mpiComm = MPI_COMM_SELF;   // or would this be an error?
00155   }
00156 
00157   return mpiComm;
00158 }
00159 
00160 MPI_Comm
00161   TeuchosConst2MPI(const RCP<const Comm<int> > &comm)
00162 {
00163   MPI_Comm mpiComm;
00164   typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00165 
00166   const Comm<int> *cConst = comm.get();
00167   Comm<int> *c = const_cast<Comm<int> *>(cConst);
00168   Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
00169   if (mc){
00170     RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
00171     mpiComm = (*wrappedComm.getRawPtr())();
00172   }
00173   else{
00174     mpiComm = MPI_COMM_SELF;   // or would this be an error?
00175   }
00176 
00177   return mpiComm;
00178 }
00179 
00180 #endif
00181 
00182 } // namespace Zoltan2