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 #ifndef _MSC_VER
00059 #include <unistd.h>
00060 #endif
00061 
00062 namespace Zoltan2{
00063 
00064 #ifndef _WIN32
00065 /* On a linux node, find the total memory currently allocated
00066  * to this process.
00067  * Return the number of kilobytes allocated to this process.
00068  * Return 0 if it is not possible to determine this.
00069  */
00070 long getProcessKilobytes()
00071 {
00072 long pageSize;
00073 
00074 #ifdef _SC_PAGESIZE
00075   pageSize = sysconf(_SC_PAGESIZE);
00076 #else
00077 #warning "Page size query is not possible.  No per-process memory stats."
00078   return 0;
00079 #endif
00080 
00081   pid_t pid = getpid();
00082   std::ostringstream fname;
00083   fname << "/proc/" << pid << "/statm";
00084   std::ifstream memFile;
00085 
00086   try{
00087     memFile.open(fname.str().c_str());
00088   }
00089   catch (...){
00090     return 0;
00091   }
00092 
00093   char buf[128];
00094   memset(buf, 0, 128);
00095   while (memFile.good()){
00096     memFile.getline(buf, 128);
00097     break;
00098   }
00099 
00100   memFile.close();
00101 
00102   std::istringstream sbuf(buf);
00103   long totalPages;
00104   sbuf >> totalPages;
00105 
00106   long pageKBytes = pageSize / 1024;
00107   totalPages = atol(buf);
00108 
00109   return totalPages * pageKBytes;
00110 }
00111 #else
00112 long getProcessKilobytes()
00113 {
00114 #pragma message ("Zoltan2_Util.cpp: Page size query is not implemented on windows.  No per-process memory stats.")
00115   return 0;
00116 }
00117 #endif
00118 
00119 
00120 #ifdef HAVE_ZOLTAN2_MPI
00121 
00125 RCP<Teuchos::MpiComm<int> >
00126   MPI2Teuchos(const MPI_Comm &comm)
00127 {
00128   typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00129   RCP<mpiWrapper_t>handle = Teuchos::opaqueWrapper<MPI_Comm>(comm);
00130   RCP<Teuchos::MpiComm<int> > tcommPtr(
00131     new Teuchos::MpiComm<int>(handle));
00132 
00133   return tcommPtr;
00134 }
00135 
00136 RCP<const Teuchos::MpiComm<int> >
00137   MPI2TeuchosConst(const MPI_Comm &comm)
00138 {
00139   typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00140   RCP<mpiWrapper_t>handle = Teuchos::opaqueWrapper<MPI_Comm>(comm);
00141   RCP<const Teuchos::MpiComm<int> > tcommPtr(
00142     new Teuchos::MpiComm<int>(handle));
00143 
00144   return tcommPtr;
00145 }
00146 
00150 MPI_Comm
00151   Teuchos2MPI(const RCP<Comm<int> > &comm)
00152 {
00153   MPI_Comm mpiComm;
00154   typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00155 
00156   Comm<int> *c = comm.get();
00157   Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
00158   if (mc){
00159     RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
00160     mpiComm = (*wrappedComm.getRawPtr())();
00161   }
00162   else{
00163     mpiComm = MPI_COMM_SELF;   // or would this be an error?
00164   }
00165 
00166   return mpiComm;
00167 }
00168 
00169 MPI_Comm
00170   TeuchosConst2MPI(const RCP<const Comm<int> > &comm)
00171 {
00172   MPI_Comm mpiComm;
00173   typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00174 
00175   const Comm<int> *cConst = comm.get();
00176   Comm<int> *c = const_cast<Comm<int> *>(cConst);
00177   Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
00178   if (mc){
00179     RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
00180     mpiComm = (*wrappedComm.getRawPtr())();
00181   }
00182   else{
00183     mpiComm = MPI_COMM_SELF;   // or would this be an error?
00184   }
00185 
00186   return mpiComm;
00187 }
00188 
00189 #endif
00190 
00191 } // namespace Zoltan2