Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-10

cpu_relax() in freepascal

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Matias Vara

Hello everyone,

I want to implement the cpu_relax() function in order to use it inside loops. However before, I would like to know if the fpc compiler is doing an optimization of this kind.

Kinds regards, Matias.

Reply by email

Matias Vara
to Matias Vara

Hello,

I think it would be better if I put my question in context. In order to tell the processor that we are in a loop, the pause instruction could be used (also I think the "rep nop" produces the same opcode). This tells the processor that we are in a loop so it improves the access to the cache and the branching mechanism. My question is if the compiler is doing any improvement of this kind in a loop or I should write my own "pause" procedure/macro. For more info
https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops

Cheers!

2017-10-29 16:59 GMT+01:00 Matias Vara :

Show quoted text 8 lines
Hello everyone, I want to implement the cpu_relax() function in order to use it inside loops. However before, I would like to know if the fpc compiler is doing an optimization of this kind. Kinds regards, Matias.

Reply by email

Adriaan van Os
to Matias Vara
I want to implement the cpu_relax() function in order to use it inside loops. However before, I would like to know if the fpc compiler is doing an optimization of this kind.

I don't think one would you use that "inside a loop" or "to improve branching" but rather inside a wait-loop where constant polling, without regular pausing, keeps the processor unnecessarily busy. The operating system has calls for that purpose (e.g. usleep, see <https://linux.die.net/man/3/usleep>). That is not up to the compiler.

Regards,

Adriaan van Os

Reply by email

Karoly Balogh (Charlie/SGR
to Matias Vara: I think it would be better if I put my question in context. In order to tell the…

Hi,

Matias Vara wrote 9 lines
Hello,  I think it would be better if I put my question in context. In order to tell the processor that we are in a loop, the pause instruction could be used (also I think the "rep nop" produces the same opcode). This tells the processor that we are in a loop so it improves the access to the cache and the branching mechanism. My question is if the compiler is doing any improvement of this kind in a loop or I should write my own "pause" procedure/macro. For more info https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops

This feature seems to be a compiler intrinsic at least in the Intel C compiler. We don't have anything similar, so you have to fall back either to an asm procedure or a line of inline assembler. The compiler is also not doing an optimization similar to this. Free Pascal's inline assembler supports the PAUSE instruction though.

But unless you plan to use this in a multithreaded spinloop because you're loading multiple cores of the CPU, it's better to let the operating system handle the wait. This instruction is certainly not the way to fix a "my app uses 100% CPU" scenario, just saying.

Charlie

Reply by email

Matias Vara
to Karoly Balogh (Charlie/SGR

Hi and thanks for the answers,

This feature seems to be a compiler intrinsic at least in the Intel C

Show quoted text 6 lines
compiler. We don't have anything similar, so you have to fall back either to an asm procedure or a line of inline assembler. The compiler is also not doing an optimization similar to this. Free Pascal's inline assembler supports the PAUSE instruction though.

good to know that.

Show quoted text 6 lines
But unless you plan to use this in a multithreaded spinloop because you're loading multiple cores of the CPU, it's better to let the operating system handle the wait. This instruction is certainly not the way to fix a "my app uses 100% CPU" scenario, just saying. Charlie

I am currently investigating the use of this instruction in my kernel. I have wait-loops when accessing shared resources in a multicore system so that could be a place to use it.
Also, I have situations in which the core just spends time in a loop so this instruction may help to reduce energy consumption. All this work is however very experimental.

Matias

Reply by email