Atomic doubles/floats in Intel TBB
Asked Answered
L

3

8

According to the documentation, an atomic supports T that is of an integral type, enumeration type, or a pointer type. Does Intel TBB support floats/doubles officially? I have seen some patches here and by Raf Schietekat here, which might/might not have been incorporated into the latest 4.0 release. From the patches I have read through, the only major difference I noticed was the addition of reinterpret_cast from integer type to float/double. If anyone could clarify this, I would appreciate it. Thanks!

Licastro answered 18/5, 2012 at 17:16 Comment(0)
D
1

You can easily add support for floating-point numbers building on top of 64 and 32 bit atomic integers. Atomic load/store/exchange can be implemented as direct wrappers using reinterpret_cast, the atomic arithmetic operations can be implemented using a loop with atomic compare exchange.

Deeannadeeanne answered 5/10, 2012 at 12:45 Comment(0)
K
1

C++11 supports atomic floats and doubles. Arithmetic functions like std::atomic_fetch_add are only supported for integral types, though.

If you have a C++11 compiler, I would recommend to switch to std::atomic so you don't have to rely on undocumented behavior.

Kleper answered 28/12, 2012 at 17:23 Comment(2)
Also worth noting that the arithmetic functions can be easily and efficiently emulated with std::atomic.Cristiecristin
Interesting. What is the best way to do it? Is it a compare-and-swap loop? (Fetch the current value and try to swap it with the new result. If it fails because the value was changed, retry.)Adara
C
0

The test for non-integral types in test_atomic.cpp was added back in 2008 (soon after the time of the discussion with Raf). Thus since even early than the time of the question, TBB supports float&double atomics (though restricted to fetch_and_store and compare_and_exchange read-modify-write ops only).

Cates answered 25/4, 2014 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.