OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
IpplMemoryUsage.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 // -*- C++ -*-
17 /***************************************************************************
18  *
19  * The IPPL Framework
20  *
21  *
22  * Visit http://people.web.psi.ch/adelmann/ for more details
23  *
24  ***************************************************************************/
25 
26 // include files
28 
31 { }
32 
33 
35  : who_m(RUSAGE_SELF)
36 {
37  globalMemPerCore_m = std::unique_ptr<double[]>(new double[Ippl::getNodes()]);
38 
39  switch ( unit ) {
40  case Unit::BIT:
41  conversion_factor_m = 8.0e3;
42  unit_m = "bit";
43  break;
44  case Unit::B:
45  conversion_factor_m = 1.0e3;
46  unit_m = "B";
47  break;
48  case Unit::KB:
49  conversion_factor_m = 1.0;
50  unit_m = "kB";
51  break;
52  case Unit::KiB:
53  conversion_factor_m = 1.0 / 1.024;
54  unit_m = "KiB";
55  break;
56  case Unit::MB:
57  conversion_factor_m = 1.0e-3;
58  unit_m = "MB";
59  break;
60  case Unit::MiB:
61  conversion_factor_m = 1.0 / (1.024 * 1024.0);
62  unit_m = "MiB";
63  break;
64  case Unit::GiB:
65  conversion_factor_m = 1.0 / (1.024 * 1024.0 * 1024.0);
66  unit_m = "GiB";
67  break;
68  case Unit::GB:
69  default:
70  conversion_factor_m = 1.0e-6;
71  unit_m = "GB";
72  break;
73  }
74 
75  initial_memory_m = 0.0;
76  if ( reset ) {
77  this->sample_m();
79  }
80 }
81 
82 
84  bool reset)
85 {
86  static IpplMemory_t instance_mp = IpplMemory_t(new IpplMemoryUsage(unit, reset));
87  return instance_mp.get();
88 }
89 
90 
91 double IpplMemoryUsage::getMemoryUsage(int core) const {
92  return globalMemPerCore_m[core];
93 }
94 
95 
97  // update max_rss_m
98  this->sample_m();
99 
100  for(int i = 0; i < Ippl::getNodes(); i++)
101  globalMemPerCore_m[i] = 0;
102 
103  double localMemPerCore = max_rss_m;
104 
105  gather(&localMemPerCore, &globalMemPerCore_m[0], 1);
106 }
107 
108 
109 const std::string& IpplMemoryUsage::getUnit() const {
110  return unit_m;
111 }
112 
113 
115  rusage usage;
116  if ( getrusage(who_m, &usage) == -1 )
117  throw std::runtime_error(
118  "IpplMemoryUsage::sample_m(): Error in collecting memory!");
119 
120  max_rss_m = usage.ru_maxrss * conversion_factor_m - initial_memory_m;
121 }
static int getNodes()
Definition: IpplInfo.cpp:773
std::unique_ptr< IpplMemoryUsage > IpplMemory_t
static IpplMemory_t instance_mp
*this
double initial_memory_m
memory usage at construction time [GB] or [GiB]
double conversion_factor_m
to various units. getrusage() returns in kB
static IpplMemory_p getInstance(Unit unit=Unit::GB, bool reset=true)
double who_m
RUSAGE_SELF, RUSAGE_CHILDREN (, RUSAGE_THREAD)
void gather(const T *input, T *output, int count, int root=0)
Definition: GlobalComm.hpp:449
std::string unit_m
what&#39;s the unit of the memory
const std::string & getUnit() const
double max_rss_m
max. resident set size [GB] or [GiB]
std::unique_ptr< double[]> globalMemPerCore_m
memory of all cores
double getMemoryUsage(int core) const