Zoltan2
Zoltan2_Util.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #include <Zoltan2_Util.hpp>
51 #include <Teuchos_OpaqueWrapper.hpp>
52 
53 #include <iostream>
54 #include <sstream>
55 #include <fstream>
56 #include <string>
57 
58 #ifndef _MSC_VER
59 #include <unistd.h>
60 #endif
61 
62 namespace Zoltan2{
63 
64 #ifndef _WIN32
65 /* On a linux node, find the total memory currently allocated
66  * to this process.
67  * Return the number of kilobytes allocated to this process.
68  * Return 0 if it is not possible to determine this.
69  */
71 {
72 long pageSize;
73 
74 #ifdef _SC_PAGESIZE
75  pageSize = sysconf(_SC_PAGESIZE);
76 #else
77 #warning "Page size query is not possible. No per-process memory stats."
78  return 0;
79 #endif
80 
81  pid_t pid = getpid();
82  std::ostringstream fname;
83  fname << "/proc/" << pid << "/statm";
84  std::ifstream memFile;
85 
86  try{
87  memFile.open(fname.str().c_str());
88  }
89  catch (...){
90  return 0;
91  }
92 
93  char buf[128];
94  memset(buf, 0, 128);
95  while (memFile.good()){
96  memFile.getline(buf, 128);
97  break;
98  }
99 
100  memFile.close();
101 
102  std::istringstream sbuf(buf);
103  long totalPages;
104  sbuf >> totalPages;
105 
106  long pageKBytes = pageSize / 1024;
107  totalPages = atol(buf);
108 
109  return totalPages * pageKBytes;
110 }
111 #else
112 long getProcessKilobytes()
113 {
114 #pragma message ("Zoltan2_Util.cpp: Page size query is not implemented on windows. No per-process memory stats.")
115  return 0;
116 }
117 #endif
118 
119 
120 #ifdef HAVE_ZOLTAN2_MPI
121 
125 RCP<Teuchos::MpiComm<int> >
126  MPI2Teuchos(const MPI_Comm &comm)
127 {
128  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
129  RCP<mpiWrapper_t>handle = Teuchos::opaqueWrapper<MPI_Comm>(comm);
130  RCP<Teuchos::MpiComm<int> > tcommPtr(
131  new Teuchos::MpiComm<int>(handle));
132 
133  return tcommPtr;
134 }
135 
136 RCP<const Teuchos::MpiComm<int> >
137  MPI2TeuchosConst(const MPI_Comm &comm)
138 {
139  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
140  RCP<mpiWrapper_t>handle = Teuchos::opaqueWrapper<MPI_Comm>(comm);
141  RCP<const Teuchos::MpiComm<int> > tcommPtr(
142  new Teuchos::MpiComm<int>(handle));
143 
144  return tcommPtr;
145 }
146 
150 MPI_Comm
151  Teuchos2MPI(const RCP<Comm<int> > &comm)
152 {
153  MPI_Comm mpiComm;
154  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
155 
156  Comm<int> *c = comm.get();
157  Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
158  if (mc){
159  RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
160  mpiComm = (*wrappedComm.getRawPtr())();
161  }
162  else{
163  mpiComm = MPI_COMM_SELF; // or would this be an error?
164  }
165 
166  return mpiComm;
167 }
168 
169 MPI_Comm
170  TeuchosConst2MPI(const RCP<const Comm<int> > &comm)
171 {
172  MPI_Comm mpiComm;
173  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
174 
175  const Comm<int> *cConst = comm.get();
176  Comm<int> *c = const_cast<Comm<int> *>(cConst);
177  Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
178  if (mc){
179  RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
180  mpiComm = (*wrappedComm.getRawPtr())();
181  }
182  else{
183  mpiComm = MPI_COMM_SELF; // or would this be an error?
184  }
185 
186  return mpiComm;
187 }
188 
189 #endif
190 
191 } // namespace Zoltan2
long getProcessKilobytes()
A gathering of useful namespace methods.