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