Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mapMemoryLeak.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_CommHelpers.hpp"
11 #include "Teuchos_DefaultComm.hpp"
12 #include "Teuchos_RCP.hpp"
13 #include "Tpetra_Map.hpp"
14 #include "Tpetra_MultiVector.hpp"
15 
16 #include <string>
17 #include <sstream>
18 #include <iostream>
19 
20 typedef int z2TestLO;
21 typedef int z2TestGO;
22 typedef double z2TestScalar;
23 
25 /* On a linux node, find the total memory currently allocated
26  * to this process.
27  * Return the number of kilobytes allocated to this process.
28  * Return 0 if it is not possible to determine this.
29  */
30 static long getProcessKilobytes()
31 {
32 long pageSize;
33 
34 #ifdef _SC_PAGESIZE
35  pageSize = sysconf(_SC_PAGESIZE);
36 #else
37 #warning "Page size query is not possible. No per-process memory stats."
38  return 0;
39 #endif
40 
41  pid_t pid = getpid();
42  std::ostringstream fname;
43  fname << "/proc/" << pid << "/statm";
44  std::ifstream memFile;
45 
46  try{
47  memFile.open(fname.str().c_str());
48  }
49  catch (...){
50  return 0;
51  }
52 
53  char buf[128];
54  memset(buf, 0, 128);
55  while (memFile.good()){
56  memFile.getline(buf, 128);
57  break;
58  }
59 
60  memFile.close();
61 
62  std::istringstream sbuf(buf);
63  long totalPages;
64  sbuf >> totalPages;
65 
66  long pageKBytes = pageSize / 1024;
67  totalPages = atol(buf);
68 
69  return totalPages * pageKBytes;
70 }
71 
73 
74 int main(int narg, char **arg)
75 {
76  Tpetra::ScopeGuard tscope(&narg, &arg);
77  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
78 
79  int me = comm->getRank();
80  int nprocs = comm->getSize();
81 
82  if (nprocs != 4)
83  std::cout << "Run with 4 MPI ranks " << std::endl;
84 
85  typedef Tpetra::Map<z2TestLO, z2TestGO> map_t;
86  z2TestGO numGlobalCoords = 4000000;
87  z2TestLO numLocalCoords = 1000000;
88  Teuchos::ParameterList myParams("testParameterList");
89  myParams.set("memory_procs", "0");
90  myParams.set("memory_output_stream", "std::cout");
91 
92  z2TestLO newnumLocalCoords = 1000000;
93  if (me == 0)
94  newnumLocalCoords = 999999;
95  else if (me == 1)
96  newnumLocalCoords = 1000001;
97  else
98  newnumLocalCoords = 1000000;
99 
100  typedef Tpetra::MultiVector<z2TestScalar, z2TestLO, z2TestGO> mvector_t;
101 
102  long before = getProcessKilobytes();
103  if (me == 0)
104  std::cout << me << " "
106  << " Before map construction "
107  << std::endl;
108 
109  for (int i = 0 ; i < 20; i++)
110  {
111  if (me == 0)
112  std::cout << me << " "
114  << " Inside the loop " << i
115  << std::endl;
116  Teuchos::RCP<const map_t> tmap = rcp(new map_t(numGlobalCoords,
117  numLocalCoords, 0, comm));
118  Teuchos::RCP<const map_t> newTmap = rcp(new map_t(numGlobalCoords,
119  newnumLocalCoords, 0, comm));
120  Teuchos::RCP<mvector_t> newMvector = rcp(new mvector_t(tmap, 3, true));
121  Teuchos::RCP<Tpetra::Import<z2TestLO, z2TestGO> > importer = rcp(
122  new Tpetra::Import<z2TestLO, z2TestGO>(tmap, newTmap));
123  //defEnv->memory("Inside the loop after i = 0");
124  }
125 
126  long after = getProcessKilobytes();
127  if (me == 0)
128  std::cout << me << " "
130  << " After map construction "
131  << std::endl;
132 
133  int iAmOK = (before == after);
134  int weAreOK;
135  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MIN, iAmOK, &weAreOK);
136 
137  if (me == 0) {
138  if (weAreOK) std::cout << "PASS" << std::endl;
139  else std::cout << "FAIL before " << before
140  << " != after " << after << std::endl;
141  }
142 }
zgno_t z2TestGO
Definition: coloring1.cpp:41
zlno_t z2TestLO
Definition: coloring1.cpp:40
int main(int narg, char **arg)
Definition: coloring1.cpp:164
list fname
Begin.
Definition: validXML.py:19
Tpetra::Map map_t
Definition: mapRemotes.cpp:25
zscalar_t z2TestScalar
Definition: coloring1.cpp:42
static long getProcessKilobytes()