Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Atomic Operations on non-integral types?

Intel_C_3
Beginner
501 Views
Would it be possible to support atomic operations for float and double through atomic.h? Is there some architecture restriction that make this impossible? I'm not very familiar with this type of thing. So, if this is a silly question, forgive me.

Thanks

0 Kudos
3 Replies
ARCH_R_Intel
Employee
501 Views

We limited atomic to operations that were commonly available in hardware. An alternative would be to provide many more operations, e.g. atomic <<=or floating-point+=,but so would be presenting "primitives" that really were not primitives. There are a few non-primitives even then. On Itanium, the fetch_and_add is implemented in hardware only for certain values +/-(1, 4, 8, 16) if I remember correctly..

If we were to add support for more operations, I would be inclined to add some kind of template that allowed any STL unary function object to be used to specify an atomic update. E.g., x.apply( std::negate ) to atomically negate a location. Binders (20.3.6 in ISO C++ standard) could be used for creating useful unary functions from binary functions. It would be implemented using a compare-and-swap loop. We' have to restrict the argument type to a POD type that fits in an integer.

Perhaps such a template should not even be a member of class atomic, but a separate template algorithm.

0 Kudos
Intel_C_3
Beginner
501 Views
I think the first paragraph answered my question. I was interested in doing a fetch_and_add operation with doubles. Am I correct in understanding that this is not commonly available in hardware?
0 Kudos
ARCH_R_Intel
Employee
501 Views

It's not available on x86 or Itanium. IBM's Power does atomic operations via a special load instruction followed by a later special conditional store, and so can emulate fetch-and-add for floating-point (and just about anything else). But in general, fetch-and-add for floating-point is not widely supported.

0 Kudos
Reply