Boost logo

Boost :

Subject: Re: [boost] boost atomic question
From: Edouard A. (edouard_at_[hidden])
Date: 2010-04-30 12:36:13


>This is for YADE, our software for discrete modelling.
>
>Imagine a std::vector of 125000 spheres:
>
>https://yade-dem.org/wiki/Screenshots_and_videos#Ball_Mill_-_125_000_sphere
s
>
>it is currently parallelized with OPEN MP, but there are some global
>locks in few places. Saying that each sphere is atomic (instead of
>locking whole container) would solve the problem.

Actually you will only add problems if you do that. You need to use a
concurrent container, not a classical containers with atomics variables.

First, atomic is more expensive than you think, to convince yourself of
this, benchmark the two following codes:

int i = 0;
while(i < 10 * 1000 * 1000) ++i;

and

atomic<int> i = 0;
while(i < 10 * 1000 * 1000) ++i;

Second, you will lock nevertheless because your memory entries will belong
to the same cache line. You need to use a special allocator that makes sure
that each entry doesn't overlap so that reading and writing to a slot will
not result in false sharing.

Third, wrapping a variable in atomic doesn't magically make your variable
immune to the reader/writer problem (or other synchronization problems). You
probably want access to your object to be atomic, in which case a container
that guarantees that affectation to an entry is atomic is enough for you.
Making your business logic reader/writer proof is generally more complex
than a simple atomic<> wrapping.

All in all, I think that what you need is a concurrent_vector<T> not a
vector<atomic<T>>.

Regards.

-Edouard
 

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 5075 (20100430) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk