File: RelaxedAtomic.hpp

package info (click to toggle)
primecount 8.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,648 kB
  • sloc: cpp: 21,887; ansic: 121; sh: 100; makefile: 89
file content (52 lines) | stat: -rw-r--r-- 1,519 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
///
/// @file  RelaxedAtomic.hpp
///        LLVM/OpenMP dynamic scheduling caused a severe scaling issue
///        in primecount: https://bugs.llvm.org/show_bug.cgi?id=49588
///        By default OpenMP dynamic scheduling may process iterations
///        in random order, it is likely that this causes many cache
///        misses when computing the easy special leaves in primecount.
///        It is possible to use schedule(monotonic:dynamic) to ensure
///        that iterations are processed in sequential order. This
///        seems to fix primecount's scaling issue.
///
///        However as a precaution measure we try to avoid OpenMP
///        dynamic scheduling in primecount and instead implement
///        schedule(monotonic:dynamic, 1) ourselves using relaxed
///        atomics.
///
/// Copyright (C) 2022 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#ifndef RELAXEDATOMIC_HPP
#define RELAXEDATOMIC_HPP

#include <macros.hpp>
#include <primecount-config.hpp>

#include <atomic>

namespace {

template <typename T>
class RelaxedAtomic
{
public:
  RelaxedAtomic(T n) : atomic_(n) { }
  // Postfix Increment
  T operator++(int)
  {
    return atomic_.fetch_add(1, std::memory_order_relaxed);
  }
private:
  // Use padding to avoid CPU false sharing
  MAYBE_UNUSED char pad1[MAX_CACHE_LINE_SIZE];
  std::atomic<T> atomic_;
  MAYBE_UNUSED char pad2[MAX_CACHE_LINE_SIZE];
};

} // namespace

#endif