Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #89018 > unrolled thread

What does "current time" mean?

Started byManu Raju <MR@invalid.invalid>
First post2023-02-07 19:00 +0000
Last post2023-02-10 20:53 -0800
Articles 10 — 7 participants

Back to article view | Back to comp.lang.c++


Contents

  What does "current time" mean? Manu Raju <MR@invalid.invalid> - 2023-02-07 19:00 +0000
    Re: What does "current time" mean? scott@slp53.sl.home (Scott Lurndal) - 2023-02-07 19:39 +0000
      Re: What does "current time" mean? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-07 12:00 -0800
        Re: What does "current time" mean? Vir Campestris <vir.campestris@invalid.invalid> - 2023-02-09 12:11 +0000
          Re: What does "current time" mean? David Brown <david.brown@hesbynett.no> - 2023-02-09 14:13 +0100
    Re: What does "current time" mean? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-07 11:51 -0800
      Re: What does "current time" mean? David Brown <david.brown@hesbynett.no> - 2023-02-07 23:16 +0100
    Re: What does "current time" mean? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 04:19 -0800
    Re: What does "current time" mean? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 05:27 -0800
    Re: What does "current time" mean? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-02-10 20:53 -0800

#89018 — What does "current time" mean?

FromManu Raju <MR@invalid.invalid>
Date2023-02-07 19:00 +0000
SubjectWhat does "current time" mean?
Message-ID<tru716$3r5bl$1@dont-email.me>
Some of you might be interested in this paper by Amazon Engineer:

<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>


[toc] | [next] | [standalone]


#89020

Fromscott@slp53.sl.home (Scott Lurndal)
Date2023-02-07 19:39 +0000
Message-ID<FNxEL.100924$ZnFc.14325@fx41.iad>
In reply to#89018
Manu Raju <MR@invalid.invalid> writes:
>Some of you might be interested in this paper by Amazon Engineer:
>
><https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>

Removing sequence points from the C++11 and following  standards was sure to bite.

[toc] | [prev] | [next] | [standalone]


#89022

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-07 12:00 -0800
Message-ID<875ycddz4w.fsf@nosuchdomain.example.com>
In reply to#89020
scott@slp53.sl.home (Scott Lurndal) writes:
> Manu Raju <MR@invalid.invalid> writes:
>>Some of you might be interested in this paper by Amazon Engineer:
>>
>><https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>
>
> Removing sequence points from the C++11 and following standards was
> sure to bite.

But C++11 still has this in [intro.execution]:

    Every value computation and side effect associated with a
    full-expression is sequenced before every value computation and side
    effect associated with the next full-expression to be evaluated.

I would think that would make it illegal to reorder these statements
(quoted from the paper); the calls to now(), fib(), and now() are full
expressions.

    auto start = chrono::high_resolution_clock::now();
    auto result = fib(which);
    auto end = chrono::high_resolution_clock::now();

It seems to me the author encountered a compiler bug, not a flaw in the
language.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#89063

FromVir Campestris <vir.campestris@invalid.invalid>
Date2023-02-09 12:11 +0000
Message-ID<ts2nu9$lopp$1@dont-email.me>
In reply to#89022
On 07/02/2023 20:00, Keith Thompson wrote:
> It seems to me the author encountered a compiler bug, not a flaw in the
> language.

That was my first thought. but then I read on...

On 07/02/2023 22:16, David Brown wrote:
 > It would also be fine for the compiler to pre-calculate fib(42) at
 > compile time and skip the run-time calculation entirely.

And that also seemed quite possible. So I looked at his stuff on 
Godbolt. It's clearly making two calls to now(), passing an address on 
the stack in eax to both calls.

Only then does it call fib(). The odd thing is that the parameter to 
fib() is on the stack - and it calls fib() twice with values 41 and 40, 
then adds the two results together.

It then does something with cout I can't be bothered to decode.

I don't understand why it adds fib for 40 and 41 together either, 
instead of just calling it with 42.

I'll go with compiler bug.

Andy

[toc] | [prev] | [next] | [standalone]


#89068

FromDavid Brown <david.brown@hesbynett.no>
Date2023-02-09 14:13 +0100
Message-ID<ts2ri8$m6vf$1@dont-email.me>
In reply to#89063
On 09/02/2023 13:11, Vir Campestris wrote:
> On 07/02/2023 20:00, Keith Thompson wrote:
>> It seems to me the author encountered a compiler bug, not a flaw in the
>> language.
> 
> That was my first thought. but then I read on...
> 
> On 07/02/2023 22:16, David Brown wrote:
>  > It would also be fine for the compiler to pre-calculate fib(42) at
>  > compile time and skip the run-time calculation entirely.
> 
> And that also seemed quite possible. So I looked at his stuff on 
> Godbolt. It's clearly making two calls to now(), passing an address on 
> the stack in eax to both calls.
> 
> Only then does it call fib(). The odd thing is that the parameter to 
> fib() is on the stack - and it calls fib() twice with values 41 and 40, 
> then adds the two results together.
> 
> It then does something with cout I can't be bothered to decode.

(When looking at this kind of stuff, I usually remove any main, cout or 
printf nonsense and replace them with calls to a simple extern function 
that is never defined.  Then the generated assembly code is vastly 
easier to understand.)

> 
> I don't understand why it adds fib for 40 and 41 together either, 
> instead of just calling it with 42.
> 

It is inlining the first call to fib(42), to get fib(41) + fib(40). 
That too is a perfectly valid optimisation (and the two fib's can be 
called in either order).  It is not necessarily a particularly helpful 
optimisation, but it is valid.

> I'll go with compiler bug.
> 

I disagree.

Again, you can happily argue that the transformations made here are not 
helpful, but I cannot see that any are invalid.


It all comes down to observable behaviour.  The calls to "now()" are 
observable, and must be done in the order specified in the code.  The 
outputs to cout is also observable, and must be ordered correctly with 
respect to the calls to "now()".  The call(s) to "fib" are not 
observable, and can be moved around freely or eliminated entirely as 
long as the result is known before it is used in the "cout" output.

[toc] | [prev] | [next] | [standalone]


#89021

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-07 11:51 -0800
Message-ID<87a61pdzkq.fsf@nosuchdomain.example.com>
In reply to#89018
Manu Raju <MR@invalid.invalid> writes:
> Some of you might be interested in this paper by Amazon Engineer:
>
> <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>

From the paper:

    auto start = chrono::high_resolution_clock::now();
    auto result = fib(which);
    auto end = chrono::high_resolution_clock::now();
    ...

    I was surprised to discover that the program printed an elapsed time
    of zero milliseconds on at least one compiler. Looking at the
    generated code at https://godbolt.org/z/vY5d9bref revealed that the
    compiler had reordered the code to get the end time prior to running
    the calculation.

I'm surprised such a transformation would be legal.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#89027

FromDavid Brown <david.brown@hesbynett.no>
Date2023-02-07 23:16 +0100
Message-ID<truij2$3t2f2$1@dont-email.me>
In reply to#89021
On 07/02/2023 20:51, Keith Thompson wrote:
> Manu Raju <MR@invalid.invalid> writes:
>> Some of you might be interested in this paper by Amazon Engineer:
>>
>> <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>
> 
>  From the paper:
> 
>      auto start = chrono::high_resolution_clock::now();
>      auto result = fib(which);
>      auto end = chrono::high_resolution_clock::now();
>      ...
> 
>      I was surprised to discover that the program printed an elapsed time
>      of zero milliseconds on at least one compiler. Looking at the
>      generated code at https://godbolt.org/z/vY5d9bref revealed that the
>      compiler had reordered the code to get the end time prior to running
>      the calculation.
> 
> I'm surprised such a transformation would be legal.
> 

(Note - I haven't read the paper yet, just this post.)

It seems perfectly reasonable to me.  Neither C nor C++ care about 
timing, nor the ordering of anything except observable behaviour.  The 
compiler can see that "fib" does not have any observable behaviour - it 
is a "pure" function.  It doesn't matter when the calculation is done, 
as far as the observable behaviour is concerned, so it is fine to move 
it before or after the calls to now() if that gives more efficient code.

It would also be fine for the compiler to pre-calculate fib(42) at 
compile time and skip the run-time calculation entirely.

I see this kind of misunderstanding regularly when people try to make 
benchmarks.  Usually the solution is to put "volatile" in the right 
place - force observable behaviour to force the ordering by making 
"which" and "result" volatile.

[toc] | [prev] | [next] | [standalone]


#89065

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-09 04:19 -0800
Message-ID<ts2obs$lpqs$1@dont-email.me>
In reply to#89018
On 2/7/2023 11:00 AM, Manu Raju wrote:
> Some of you might be interested in this paper by Amazon Engineer:
> 
> <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>
> 
> 
> 

[OT], drift, Sorry. Some comic relief... It's the now, now:

https://youtu.be/nRGCZh5A8T4?t=78

;^)

[toc] | [prev] | [next] | [standalone]


#89069

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-02-09 05:27 -0800
Message-ID<ts2sc1$mbbk$2@dont-email.me>
In reply to#89018
On 2/7/2023 11:00 AM, Manu Raju wrote:
> Some of you might be interested in this paper by Amazon Engineer:
> 
> <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>
> 
> 
> 

Always remember to keep a delta from the last time and the current time...

[toc] | [prev] | [next] | [standalone]


#89088

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2023-02-10 20:53 -0800
Message-ID<ts76vk$1b0me$1@dont-email.me>
In reply to#89018
On 02/07/23 11:00 AM, Manu Raju wrote:
> Some of you might be interested in this paper by Amazon Engineer:
> 
> <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>

We have already seen issues of that same general nature previously with 
thread synchronization primitives. Before the proper memory fencing was 
introduced into the language there was no guarantee that

   mutex.lock();
   <access to the resource>
   mutex.unlock();

would not get reordered into

   <access to the resource>
   mutex.lock();
   mutex.unlock();

thus defeating the synchronization. Back in C++98 times volatile access 
was used as a workaround to introduce observable behavior and force the 
desired sequencing.

Apparently, a more general mechanism is needed for sequence fencing not 
tied to memory access.

-- 
Best regards,
Andrey

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web