energy.hpp
1 #ifndef MAMMUT_ENERGY_HPP_
2 #define MAMMUT_ENERGY_HPP_
3 
4 #include "../communicator.hpp"
5 #include "../module.hpp"
6 #include "../topology/topology.hpp"
7 
8 namespace mammut{
9 namespace energy{
10 
11 typedef double Joules;
12 class JoulesCpu;
13 
14 typedef enum{
15  COUNTER_CPUS = 0,// Power measured at CPU level
16  COUNTER_PLUG, // Power measured at the plug
17 }CounterType;
18 
19 /*
20  * ! \class Counter
21  * \brief A generic energy counter.
22  *
23  * A generic energy counter.
24  */
25 class Counter{
26 public:
27  virtual ~Counter(){;}
28 
33  virtual Joules getJoules() = 0;
34 
38  virtual void reset() = 0;
39 
44  virtual bool init() = 0;
45 
50  virtual CounterType getType() = 0;
51 };
52 
53 /*
54  * ! \class CounterPlug
55  * \brief A plug energy counter.
56  *
57  * A plug energy counter.
58  */
59 class CounterPlug: public Counter{
60 public:
61  virtual Joules getJoules() = 0;
62  virtual void reset() = 0;
63  virtual bool init() = 0;
64  CounterType getType(){return COUNTER_PLUG;}
65 };
66 
67 /*
68  * ! \class CounterCpus
69  * \brief A CPUs energy counter.
70  *
71  * A CPUs energy counter.
72  */
73 class CounterCpus: public Counter{
74 protected:
75  topology::Topology* _topology;
76  std::vector<topology::Cpu*> _cpus;
78 public:
83  const std::vector<topology::Cpu*>& getCpus(){return _cpus;}
84 
92  virtual JoulesCpu getJoulesComponents(topology::CpuId cpuId) = 0;
93 
102 
110 
118  virtual Joules getJoulesCpu(topology::CpuId cpuId) = 0;
119 
127  Joules getJoulesCpu(topology::Cpu* cpu);
128 
135  virtual Joules getJoulesCpuAll();
136 
144  virtual Joules getJoulesCores(topology::CpuId cpuId) = 0;
145 
153  Joules getJoulesCores(topology::Cpu* cpu);
154 
161  virtual Joules getJoulesCoresAll();
162 
167  virtual bool hasJoulesGraphic() = 0;
168 
176  virtual Joules getJoulesGraphic(topology::CpuId cpuId) = 0;
177 
185  Joules getJoulesGraphic(topology::Cpu* cpu);
186 
193  virtual Joules getJoulesGraphicAll();
194 
199  virtual bool hasJoulesDram() = 0;
200 
208  virtual Joules getJoulesDram(topology::CpuId cpuId) = 0;
209 
217  Joules getJoulesDram(topology::Cpu* cpu);
218 
225  virtual Joules getJoulesDramAll();
226 
227  Joules getJoules();
228  virtual void reset() = 0;
229  virtual bool init() = 0;
230  virtual ~CounterCpus(){;}
231  CounterType getType(){return COUNTER_CPUS;}
232 };
233 
234 class Energy: public Module{
235  MAMMUT_MODULE_DECL(Energy)
236 private:
237  CounterPlug* _counterPlug;
238  CounterCpus* _counterCpus;
239  Energy();
240  Energy(Communicator* const communicator);
241  ~Energy();
242  bool processMessage(const std::string& messageIdIn, const std::string& messageIn,
243  std::string& messageIdOut, std::string& messageOut);
244 public:
251  Counter* getCounter() const;
252 
261  std::vector<CounterType> getCountersTypes() const;
262 
268  Counter* getCounter(CounterType type) const;
269 };
270 
271 
276 class JoulesCpu{
277  friend std::ostream& operator<<(std::ostream& os, const JoulesCpu& obj);
278 public:
279  Joules cpu;
280  Joules cores;
281  Joules graphic;
282  Joules dram;
283 
284  JoulesCpu():cpu(0), cores(0), graphic(0), dram(0){;}
285 
286  JoulesCpu(Joules cpu, Joules cores, Joules graphic, Joules dram):
287  cpu(cpu), cores(cores), graphic(graphic), dram(dram){;}
288 
292  void zero(){cpu = 0; cores = 0; graphic = 0; dram = 0;}
293 
294  void swap(JoulesCpu& x){
295  using std::swap;
296 
297  swap(cpu, x.cpu);
298  swap(cores, x.cores);
299  swap(graphic, x.graphic);
300  swap(dram, x.dram);
301  }
302 
303  JoulesCpu& operator=(JoulesCpu rhs){
304  swap(rhs);
305  return *this;
306  }
307 
308  JoulesCpu& operator+=(const JoulesCpu& rhs){
309  cpu += rhs.cpu;
310  cores += rhs.cores;
311  graphic += rhs.graphic;
312  dram += rhs.dram;
313  return *this;
314  }
315 
316  JoulesCpu& operator-=(const JoulesCpu& rhs){
317  cpu -= rhs.cpu;
318  cores -= rhs.cores;
319  graphic -= rhs.graphic;
320  dram -= rhs.dram;
321  return *this;
322  }
323 
324  JoulesCpu& operator*=(const JoulesCpu& rhs){
325  cpu *= rhs.cpu;
326  cores *= rhs.cores;
327  graphic *= rhs.graphic;
328  dram *= rhs.dram;
329  return *this;
330  }
331 
332  JoulesCpu& operator/=(const JoulesCpu& rhs){
333  cpu /= rhs.cpu;
334  cores /= rhs.cores;
335  graphic /= rhs.graphic;
336  dram /= rhs.dram;
337  return *this;
338  }
339 
340  JoulesCpu operator/=(double x){
341  cpu /= x;
342  cores /= x;
343  graphic /= x;
344  dram /= x;
345  return *this;
346  }
347 
348  JoulesCpu operator*=(double x){
349  cpu *= x;
350  cores *= x;
351  graphic *= x;
352  dram *= x;
353  return *this;
354  }
355 };
356 
357 inline JoulesCpu operator+(const JoulesCpu& lhs, const JoulesCpu& rhs){
358  JoulesCpu r = lhs;
359  r += rhs;
360  return r;
361 }
362 
363 inline JoulesCpu operator-(const JoulesCpu& lhs, const JoulesCpu& rhs){
364  JoulesCpu r = lhs;
365  r -= rhs;
366  return r;
367 }
368 
369 inline JoulesCpu operator*(const JoulesCpu& lhs, const JoulesCpu& rhs){
370  JoulesCpu r = lhs;
371  r *= rhs;
372  return r;
373 }
374 
375 inline JoulesCpu operator/(const JoulesCpu& lhs, const JoulesCpu& rhs){
376  JoulesCpu r = lhs;
377  r /= rhs;
378  return r;
379 }
380 
381 inline JoulesCpu operator/(const JoulesCpu& lhs, double x){
382  JoulesCpu r = lhs;
383  r /= x;
384  return r;
385 }
386 
387 inline JoulesCpu operator*(const JoulesCpu& lhs, double x){
388  JoulesCpu r = lhs;
389  r *= x;
390  return r;
391 }
392 
393 inline std::ostream& operator<<(std::ostream& os, const JoulesCpu& obj){
394  os << obj.cpu << "\t";
395  os << obj.cores << "\t";
396  os << obj.graphic << "\t";
397  os << obj.dram << "\t";
398  return os;
399 }
400 
401 }
402 }
403 
404 #endif /* MAMMUT_ENERGY_HPP_ */
virtual Joules getJoulesCoresAll()
virtual Joules getJoules()=0
virtual Joules getJoulesDram(topology::CpuId cpuId)=0
CounterType getType()
Definition: energy.hpp:64
Definition: topology.hpp:127
CounterType getType()
Definition: energy.hpp:231
virtual Joules getJoulesCores(topology::CpuId cpuId)=0
virtual bool hasJoulesDram()=0
Counter * getCounter() const
virtual Joules getJoulesGraphicAll()
virtual bool init()=0
Definition: topology.hpp:56
void zero()
Definition: energy.hpp:292
virtual CounterType getType()=0
Represents the values that can be read from a Cpu energy counter.
Definition: energy.hpp:276
virtual Joules getJoulesCpuAll()
virtual Joules getJoulesCpu(topology::CpuId cpuId)=0
virtual void reset()=0
virtual Joules getJoulesDramAll()
Definition: energy.hpp:73
virtual bool hasJoulesGraphic()=0
Definition: energy.hpp:234
Definition: energy.hpp:25
const std::vector< topology::Cpu * > & getCpus()
Definition: energy.hpp:83
virtual JoulesCpu getJoulesComponents(topology::CpuId cpuId)=0
std::vector< CounterType > getCountersTypes() const
virtual JoulesCpu getJoulesComponentsAll()
virtual Joules getJoules()=0
virtual Joules getJoulesGraphic(topology::CpuId cpuId)=0
Definition: energy.hpp:59