Re: [eigen] Help on solving a race condition
• To: eigen@xxxxxxxxxxxxxxxxxxx
• Subject: Re: [eigen] Help on solving a race condition
• From: Hauke Heibel <hauke.heibel@xxxxxxxxxxxxxx>
• Date: Fri, 8 Jun 2012 16:24:23 +0200
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=LvHoUfOEoNnpol3r6vDNs77VgAjdKBGcU+exrrUzKCc=; b=IjZt9uiPsivPJzX3vuLtNPEDDpfEMfxCITpLO7Ztihn3Kp+cBBf2Bhl8DhFNzMKvy4 4nOTFdmVlEavLcXxopTayK0o1CL4E0dk8bfmUGcZ0+i7kjCyZ6zp7XpzJkUGoEJ0p2XM CwefVdKcfY1xZMSIFFLE2g9dFMSwfP6Fj8ppP6kJ8rDcy1yxN65FHLPdHZMB6oz3JmY8 izgCSWU82Ojt2DP+t7+QhisTMq95hOez9LVdjgKpZClrN/4NoSnvSeCQu1m1zBEv8WQd 0UBa7UfpE1qmdCpU1nIohjRclsAGATGN7lS6OL4HciCKp/e2wuYOxyj3zX15YxOsqer5 U7Mw==

```Hi Gael,

I think the most efficient solution would be to use atomics. I am not
sure whether the code below is allowed and fixes the problem

#pragma omp atomic
static std::ptrdiff_t m_l1CacheSize =
manage_caching_sizes_helper(queryL1CacheSize(),8 * 1024);

#pragma omp atomic
static std::ptrdiff_t m_l2CacheSize =
manage_caching_sizes_helper(queryTopLevelCacheSize(),1*1024*1024);

Another alternative might be #pragma omp critical.

Yet another idea might be to look at 'atomic_do_once' (see [1] and
[2]) from Intel's TBB. The code from [2] probably gets much simpler
for the use case at hand because the cache size itself could be stored
as an atomic.

Regards,
Hauke

[2] https://akazarov.web.cern.ch/akazarov/cmt/releases/nightly/tbb/src/tbb/tbb_misc.h

On Fri, Jun 8, 2012 at 3:12 PM, Gael Guennebaud
<gael.guennebaud@xxxxxxxxx> wrote:
> Hi,
>
> I'm looking for some help on understanding how the following piece of
> code in Eigen can lead to a race condition when the function
> manage_caching_sizes is called from different threads:
>
>
> inline std::ptrdiff_t manage_caching_sizes_helper(std::ptrdiff_t a,
> std::ptrdiff_t b)
> {
>  return a<=0 ? b : a;
> }
>
> inline void manage_caching_sizes(Action action, std::ptrdiff_t* l1=0,
> std::ptrdiff_t* l2=0)
> {
>  static std::ptrdiff_t m_l1CacheSize =
> manage_caching_sizes_helper(queryL1CacheSize(),8 * 1024);
>  static std::ptrdiff_t m_l2CacheSize =
> manage_caching_sizes_helper(queryTopLevelCacheSize(),1*1024*1024);
>  ...
> }
>
>
> During it's first call, this function computes and store the cache sizes.
>
> I would like to find a solution that avoids making the static variable
> "thread private" as suggested there:
> The problem with this approach, is that the cache-sizes are recomputed
> for every thread.
>
>
> thanks,
>
> Gaël
>
>

```

 Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/