0
\$\begingroup\$

If I have a program (e.g. a small Python program) on a specific machine. Would the program be able to measure how much energy its computation costed?

I think, there would be two possible ways to go:

A) There is a function that measures the energy of the device before and after the execution of the program.

然而,会有关于交流的问题curacy of the measurement of the device on which the program is executed. Also, at the same time other processes may be executed on the machine, which would (potentially) corrupt the process of measuring. Except for this, it does not seem elegant to me.

B) There is a program that counts the bit-wise computation steps and returns it at the end of the execution. From this, I could possibly calculate the energy consumption of the whole programme knowing the average energy the device takes to carry out one single computation step. However, this seems a bit contradictory to me, since the programme would essentially (at least partly) need to observe itself. Also, I am not sure if this would be a valid measure at all, since it is indirect, and somehow theoretical. -- On the other hand, there would be no perturbations by other programs running on the computer.

Is one of these ways reasonable or are there possibly more convenient of doing this?

Note: I am interested in therealenergy usage, not the theoretical. (This is why I think the O-measure of some algorithms within the code would essentially not help me a lot.)

Also: I am not interested in the time measurement, as running the program on different machines will result in different run times. Additionally, knowing the time, does not (in practice) automatically give me the energy usage an all machines.

Rather I am interested in the pure energy usage.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ What are you trying to do anyway? You know, programs can run on CPU, some of them can be fully or partially offloaded to GPU. Some CPUs might have special hardware optimized for certain type of calculations, what will reduce the power for certain operations and so on. Upd: Hey, where are all of the comments gone? Some were on-topic. \$\endgroup\$Eugene Sh. Jul 26 at 14:46
  • 2
    \$\begingroup\$ I don't understand why mods deleted the comments which asked for clarification from the OP? ♂️ (you can delete this anyway) \$\endgroup\$Mitu Raj Jul 26 at 14:49
  • \$\begingroup\$ Yes, I know the general trends when you optimize your code for example for specific machines. - I am more interested, however, in the exact amount of energy that my program takes to be executed on a computer. \$\endgroup\$Tho Re Jul 26 at 14:51
  • \$\begingroup\$ Specifically I run calculations on a high performance computer and try to figure out, if I can include an "energy usage" measure into my code, that tells me how much energy my simulation consumed \$\endgroup\$Tho Re Jul 26 at 15:36
  • \$\begingroup\$ "However, there would beproblemsconcerning theaccuracyof the measurement of the device on which the program is executed."- how accurate do you need it to be? \$\endgroup\$Bruce Abbott Jul 26 at 21:25

2 Answers2

3
\$\begingroup\$

To measure the power consumed by a device it is necessary to measure the voltage (or know its constant value) and to measure the current. The power demand at any instant is given by$$ P = UI $$where\$P\$is power in watts (W),\$U\$is the voltage (V) and\$I\$is the current (A).

The energy consumed is given by$$ E = \int_0^T P dt$$where\$E\$is in joules (J) or watt-seconds (Ws).

Virtually all processors run on constant voltage although it may vary on battery-powered devices. Some processors will report their operating voltage so that will help you determine\$U\$.

Measuring current (and power) will usually require some additional hardware and this is not normally supplied in most computer systems. You have two options:

  1. Add in a DC current / power meter between the power supply and the computer.
  2. Add in a mains power meter between the mains and the computer's power supply.

enter image description here

Figure 1. A current sensing circuit.Simplifying Current Sensing (Rev. A) - Texas Instruments.

The circuit of Figure 1 is how you would do case 1. It adds a low resistance RSENSEin series with the positive supply and the output voltage of the amplifier is proportional to the current being drawn. This in turn would be fed into an analog to digital converter (ADC) on your computer - if it has one! Your software would then have to do the integration\$ \int_0^T UI dt \$to keep track of the energy consumed.

I think your idea is impractical because you won't have control of the hardware your program is to run on.


Since laptop battery management has come up a few times in the comments we should consider that.

schematic

simulate this circuit——原理创建使用CircuitLab

Figure 2. Laptop battery management can count charge in and out of the battery but it has no idea how much power is being consumed directly from the mains PSU and it has no idea which components are consuming the power.

\$\endgroup\$
11
  • \$\begingroup\$ So, just to make it clear for me: The way computers are built is not meant to calculate or store their own energy (or power) consumption? \$\endgroup\$Tho Re Jul 26 at 15:54
  • \$\begingroup\$ Correct. What made you think they might? \$\endgroup\$Transistor Jul 26 at 16:13
  • \$\begingroup\$ Why would a computer know it's own power consumption? Is there a consumer advantage to knowing this information? \$\endgroup\$StainlessSteelRat Jul 26 at 16:14
  • \$\begingroup\$ As to my understanding the process of computing on a computing machine breaks down to the movement of atoms from one position to another (for storing 'information'). I was supposing that each of these steps is - in theory - well defined (as well as an estimate of the energy that is necessary to do such an elementary step). Adding up these steps should for sure be possible for a counting device positioned inside - or very close to - the machine. (Even though the number might become quite high very fast.) \$\endgroup\$Tho Re Jul 26 at 16:35
  • \$\begingroup\$ So, what I have in mind is actually a high number of small counting devices that count the number of elementary steps, which then can be easily transfered into an energy usage number. \$\endgroup\$Tho Re Jul 26 at 16:39
1
\$\begingroup\$

You can't realistically measure theenergyconsumed by something you plug into mains. What you must do is measure the instantaneouspowerit consumes and integrate that over the run time of the program to calculate the total energy consumed.

Your best hope is to add a power measuring device to your setup and write a program that reads the current power consumption periodically. Run your system for a whilewithout运行我的程序nterest and determine the average power consumed. You might want to stop all unnecessary processes while doing this.

Repeat the power measurements while running your program repeatedly many times. Again calculate average power consumption. Subtract the background power from the power-with-program-running and then multiply by the average run time of your program. The result is an estimate of the average energy needed to run your program.

Note that you will need to do this separately for each "machine" of interest. The energy required to run the program will be different for different computing hardware and different operating systems, possibly even different versions of the same operating system.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ "You can't realistically measure the energy consumed by something you plug into mains." Why is that so? \$\endgroup\$Tho Re Jul 26 at 14:42
  • \$\begingroup\$ ... do you mean practically or even theoretically? \$\endgroup\$Tho Re Jul 26 at 14:43
  • \$\begingroup\$ The way to directly measureenergyis to use a power source that contains a known amount of energy when you start, then measure the amount of energy remaining at the end. This would be possible if you used a battery as the power source, but you can't measure the energy available from mains you can only measure the instantaneous power drawn. \$\endgroup\$Elliot Alderson Jul 26 at 14:47
  • 1
    \$\begingroup\$ So, you mean a program will not even in theory be able to ask the machine on which it runs, how much energy its execution costed? \$\endgroup\$Tho Re Jul 26 at 14:54
  • 1
    \$\begingroup\$ Or do you mean it is not even a well defined measure? \$\endgroup\$Tho Re Jul 26 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to ourterms of service,privacy policyandcookie policy

Not the answer you're looking for? Browse other questions taggedorask your own question.