views:

497

answers:

8

How can I measure the power consumed by a C algorithm while running on a Pentium 4 processor (and any other processor will also do)?

+6  A: 

Run your algorithm in a long loop with a Kill-a-Watt attached to the machine?

Paolo
I tried attaching a Kill-a-Watt (or equivalent, can't actually recall which brand) to a PC. It fried the motherboard. Tried it again, fried another motherboard. Ran out of computers which I could trash without someone noticing. Whoops ! And be careful.
High Performance Mark
:D, visions of a smoldering pile of PCs being quietly hidden behind a server rack...
Paolo
Very strange - I've used a european version of Kill-a-Watt to measure power consumption of a PC under different loads (idle, gaming, MPEG-4 rendering...) and it worked very well. I would suggest an electrician to take a look at whether your sockets are wired correctly... Don't try this yourself :)
MaxVT
wireclamping or killawats will measure the statistical code run by the non-deterministic kernel of a normal OS, and the OS services. It will probably be tolerant to noise under an MPEG4 run but for small workloads I suspect the detail will be lost in the above mentioned noise.
Hassan Syed
+2  A: 

Hi

Excellent question; I upvoted it. I haven't got a clue, but here's a methodology:

-- get CPU spec sheet from Intel (or AMD or whoever) or see Wikipedia; that should tell you power consumption at max FLOP rate;

-- translate algorithm into FLOPs;

-- do some simple arithmetic;

-- post your data and calculations to SO and invite comments and further data

Of course, you'll have to frame your next post as another question, I'll watch with interest.

Regards

Mark

High Performance Mark
Note: Windows provides information about "CPU time" that a process has consumed. You can measure that and multiply by the CPU power rating. Although this is going to be awfully imprecise.
Vilx-
The "translate algorithm into FLOPs" step might be tricky, given that the P4 has a big and sophisticated cache system that will probably cause different loads in different situations. :-)
Ken
+4  A: 

Unless you run the code on a simple single tasking OS such as DOS or and RTOS where you get precise control of what runs at any time, the OS will typically be running many other processes simultaneously. It may be difficult to distinguish between your process and any others.

Clifford
Also other uncontrollable factors like paging, CPU cache hits, etc. can affect the final outcome (though depending on the desired precision this might be irrelevant).
Vilx-
+2  A: 

First, you need to be running the simplest OS that supports your code (probably a server version unix of some sort, I expect this to be impractical on Windows). That's to avoid the OS messing up your measurements.

Then you need to instrument the box with a sensitive datalogger between the power supply and motherboard. This is going to need some careful hardware engineering so as not to mess up the PCs voltage regulation, but someone must have done it.

I have actually done this with an embedded MIPS box and a logging multimeter, but that had a single 12V power supply. Actually, come to think of it, if you used a power supply built for running a PC in a vehicle, you would have a 12V supply and all you'd need then is a lab PSU with enough amps to run the thing.

Andrew McGregor
Why can't you measure the PC running in idle state, and while running the algorithm, to factor the OS out of the equations? Sure it won't be perfect but do it several times and you can easily apply standard statistical methods.
John
Yes, of course, and that would get you a more accurate result.
Andrew McGregor
+1  A: 

It's hard to say.

I would suggest you to use a Current Clamp, so you can measure all the power being consumed by your CPU. Then you should measure the idle consumption of your system, get the standard value with as low a standard deviation as possible.

Then run the critical code in a loop.

Previous suggestions about running your code under DOS/RTOS are also valid, but maybe it will not compile the same way as your production...

Douglas Gemignani
+2  A: 

Sorry, I find this question senseless.

Why ? Because an algorithm itself has (with the following exceptions*) no correlation with the power consumption, it is the priority on the program/thread/process runs. If you change the priority, you change the amount of idle time the processor has and therefore the power consumption. I think the only difference in energy consumption between the instructions is the number of cycles needed, so fast code will be power friendly. To measure power consumption of a "algorithm" is senseless if you don't mean the performance.

*Exceptions: Threads which can be idle while waiting for other threads, programs which use the HLT instruction.

Sure running the processor at fast as possible increases the amount of energy superlinearly (more heat, more cooling needed), but that is a hardware problem. If you want to spare energy, you can downclock the processor or use energy-efficient ones (Atom processor), but changing/tweaking the code won't change anything.

So I think it makes much more sense to ask the processor producer for specifications what different processor modes exist and what energy consumption they have. You also need to know that the periphery (fan, power supply, graphics card (!)) and the running software on the system will influence the results of measuring computer power.

Why do you need this task anyway ?

Thorsten S.
An *algorithm* has no correlation with power consumption but its implementation does. For example, processing a lot of internal instructions (instructions that don't fetch external memory or perform I/O) may heat up the processor's core more than I/O or instructions that access external memory.
Thomas Matthews
An algorithm uses a certain amount of energy to run to completion; the OS scheduling decisions don't change the number of operations required, only the length of time it takes.
Andrew McGregor
The OP request talks about "power consumption" which I equate with the physical description: Watts or Joule/sec...energy per time.So while an algorithm need a specific amount of energy to run to completion, changing the OS scheduling *does* influence the power consumption.Thomas has a point that different access modes (register, cache, memory and RW media) will have an impact of overall power consumption. But the OP asks to measure the processor power and as the processor (apart from the GPU) is the only thing which can perform algorithms I suspect the extra power will not show up.
Thorsten S.
+1  A: 

hey guys thanks for replying.... The code is a very simple C code( not even 150 lines) so I guess we can easily run it under DOS.... I need to do this for my college project and they are insisting on not using any external device or circuitry... Isn't there any way to do it logically.. by just using some operating system commands or something else of that sort..... I looped my code for 10000 times and the number of clock ticks required were 845 and the time required was 46.428570...Cant we perform some manipulation on these values and get some fair idea on the power consumed..... If you want I can upload the whole code

Shivam Kumar Sharma
Well, in that case, you can only estimate based on the specifications for the CPU. Trouble is, you don't know how much of the CPU's maximum current draw this particular code will induce, because there will be functional units in there that you're not using.Some embedded CPUs (AVRs for instance) have specified energy consumption for each instruction executed; you could compile it for one of those and add it all up, but Intel don't do that for x86 class processors, they're too complicated.
Andrew McGregor
+3  A: 

Since you know the execution time, you can calculate the energy used by the CPU by looking up the power consumption on the P4 datasheet. For example, a 2.2 GHz P4 with a 400 MHz FSB has a typical Vcc of 1.3725 Volts and Icc of 47.9 Amps which is (1.3725*47.9=) 65.74 watts. Since you know your loop of 10,000 algorithm cycles took 46.428570s, you assume a single loop will take 46.428570/10000 = 0.00454278s. The amount of energy consumed by your algorithm would then be 65.74 watts * 0.00454278s = 0.305 watt seconds (or joules).

To convert to kilowatt hours: 0.305 watt seconds * 1000 kilowatts/watt * 1 hour / 3600 seconds = 0.85 kwh. A utility company charges around $0.11 per kwh so this algorithm would cost 0.85 kwh * $0.11 = about a penny to run.

Keep in mind this is the CPU only...none of the rest of the computer.

semaj
(+1) this is the correct answer. Deleted mine.
Hassan Syed
I discussed all of your suggestions with my teacher he seems to have gone with the above mentioned method.....Thanks to all of you for your valuable suggestions
Shivam Kumar Sharma
+1 for looking it up and calculating the answer
Thorsten S.