Zoltan2  Version of the Day
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 #include <unistd.h>
59 
60 namespace Zoltan2{
61 
62 /* On a linux node, find the total memory currently allocated
63  * to this process.
64  * Return the number of kilobytes allocated to this process.
65  * Return 0 if it is not possible to determine this.
66  */
68 {
69 long pageSize;
70 
71 #ifdef _SC_PAGESIZE
72  pageSize = sysconf(_SC_PAGESIZE);
73 #else
74 #warning "Page size query is not possible. No per-process memory stats."
75  return 0;
76 #endif
77 
78  pid_t pid = getpid();
79  std::ostringstream fname;
80  fname << "/proc/" << pid << "/statm";
81  std::ifstream memFile;
82 
83  try{
84  memFile.open(fname.str().c_str());
85  }
86  catch (...){
87  return 0;
88  }
89 
90  char buf[128];
91  memset(buf, 0, 128);
92  while (memFile.good()){
93  memFile.getline(buf, 128);
94  break;
95  }
96 
97  memFile.close();
98 
99  std::istringstream sbuf(buf);
100  long totalPages;
101  sbuf >> totalPages;
102 
103  long pageKBytes = pageSize / 1024;
104  totalPages = atol(buf);
105 
106  return totalPages * pageKBytes;
107 }
108 
109 
110 
111 #ifdef HAVE_ZOLTAN2_MPI
112 
116 RCP<Teuchos::MpiComm<int> >
117  MPI2Teuchos(const MPI_Comm &comm)
118 {
119  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
120  RCP<mpiWrapper_t>handle = Teuchos::opaqueWrapper<MPI_Comm>(comm);
121  RCP<Teuchos::MpiComm<int> > tcommPtr(
122  new Teuchos::MpiComm<int>(handle));
123 
124  return tcommPtr;
125 }
126 
127 RCP<const Teuchos::MpiComm<int> >
128  MPI2TeuchosConst(const MPI_Comm &comm)
129 {
130  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
131  RCP<mpiWrapper_t>handle = Teuchos::opaqueWrapper<MPI_Comm>(comm);
132  RCP<const Teuchos::MpiComm<int> > tcommPtr(
133  new Teuchos::MpiComm<int>(handle));
134 
135  return tcommPtr;
136 }
137 
141 MPI_Comm
142  Teuchos2MPI(const RCP<Comm<int> > &comm)
143 {
144  MPI_Comm mpiComm;
145  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
146 
147  Comm<int> *c = comm.get();
148  Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
149  if (mc){
150  RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
151  mpiComm = (*wrappedComm.getRawPtr())();
152  }
153  else{
154  mpiComm = MPI_COMM_SELF; // or would this be an error?
155  }
156 
157  return mpiComm;
158 }
159 
160 MPI_Comm
161  TeuchosConst2MPI(const RCP<const Comm<int> > &comm)
162 {
163  MPI_Comm mpiComm;
164  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
165 
166  const Comm<int> *cConst = comm.get();
167  Comm<int> *c = const_cast<Comm<int> *>(cConst);
168  Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
169  if (mc){
170  RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
171  mpiComm = (*wrappedComm.getRawPtr())();
172  }
173  else{
174  mpiComm = MPI_COMM_SELF; // or would this be an error?
175  }
176 
177  return mpiComm;
178 }
179 
180 #endif
181 
182 } // namespace Zoltan2
long getProcessKilobytes()
A gathering of useful namespace methods.