threading_backend.cc 6.49 KB
Newer Older
Minjie Wang's avatar
Minjie Wang committed
1
2
3
4
5
6
7
/*!
 *  Copyright (c) 2018 by Contributors
 * \file threading_backend.cc
 * \brief Native threading backend
 */
#include <dgl/runtime/threading_backend.h>
#include <dmlc/logging.h>
8

Minjie Wang's avatar
Minjie Wang committed
9
#include <algorithm>
10
#include <thread>
Minjie Wang's avatar
Minjie Wang committed
11
12
13
14
15
16
17
18
#if defined(__linux__) || defined(__ANDROID__)
#include <fstream>
#else
#endif
#if defined(__linux__)
#include <sched.h>
#endif

19
namespace dgl {
Minjie Wang's avatar
Minjie Wang committed
20
21
22
23
24
namespace runtime {
namespace threading {

class ThreadGroup::Impl {
 public:
25
26
27
  Impl(
      int num_workers, std::function<void(int)> worker_callback,
      bool exclude_worker0)
Minjie Wang's avatar
Minjie Wang committed
28
29
      : num_workers_(num_workers) {
    CHECK_GE(num_workers, 1)
30
        << "Requested a non-positive number of worker threads.";
Minjie Wang's avatar
Minjie Wang committed
31
32
33
34
35
36
37
38
    for (int i = exclude_worker0; i < num_workers_; ++i) {
      threads_.emplace_back([worker_callback, i] { worker_callback(i); });
    }
    InitSortedOrder();
  }
  ~Impl() { Join(); }

  void Join() {
39
    for (auto &t : threads_) {
Minjie Wang's avatar
Minjie Wang committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      if (t.joinable()) t.join();
    }
  }

  int Configure(AffinityMode mode, int nthreads, bool exclude_worker0) {
    int num_workers_used = 0;
    if (mode == kLittle) {
      num_workers_used = little_count_;
    } else if (mode == kBig) {
      num_workers_used = big_count_;
    } else {
      // use default
      num_workers_used = threading::MaxConcurrency();
    }
    // if a specific number was given, use that
    if (nthreads) {
      num_workers_used = nthreads;
    }
    // if MaxConcurrency restricted the number of workers (e.g., due to
    // hyperthreading), respect the restriction. On CPUs with N logical cores
    // and N/2 physical cores this will set affinity to the first N/2 logical
    // ones.
    num_workers_used = std::min(num_workers_, num_workers_used);

64
    const char *val = getenv("DGL_BIND_THREADS");
Minjie Wang's avatar
Minjie Wang committed
65
66
67
    if (val == nullptr || atoi(val) == 1) {
      // Do not set affinity if there are more workers than found cores
      if (sorted_order_.size() >= static_cast<unsigned int>(num_workers_)) {
68
        SetAffinity(exclude_worker0, mode == kLittle);
Minjie Wang's avatar
Minjie Wang committed
69
70
      } else {
        LOG(WARNING)
71
72
            << "The thread affinity cannot be set when the number of workers"
            << "is larger than the number of available cores in the system.";
Minjie Wang's avatar
Minjie Wang committed
73
74
75
76
77
78
79
80
81
82
83
84
85
      }
    }
    return num_workers_used;
  }

 private:
  // bind worker threads to disjoint cores
  // if worker 0 is offloaded to master, i.e. exclude_worker0 is true,
  // the master thread is bound to core 0.
  void SetAffinity(bool exclude_worker0, bool reverse = false) {
#if defined(__ANDROID__)
#ifndef CPU_SET
#define CPU_SETSIZE 1024
86
#define __NCPUBITS (8 * sizeof(uint64_t))
Minjie Wang's avatar
Minjie Wang committed
87
88
89
90
91
    typedef struct {
      uint64_t __bits[CPU_SETSIZE / __NCPUBITS];
    } cpu_set_t;

#define CPU_SET(cpu, cpusetp) \
92
93
  ((cpusetp)->__bits[(cpu) / __NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
#define CPU_ZERO(cpusetp) memset((cpusetp), 0, sizeof(cpu_set_t))
Minjie Wang's avatar
Minjie Wang committed
94
95
96
97
98
99
100
101
#endif
#endif
#if defined(__linux__) || defined(__ANDROID__)
    CHECK_GE(sorted_order_.size(), num_workers_);

    for (unsigned i = 0; i < threads_.size(); ++i) {
      unsigned core_id;
      if (reverse) {
102
103
        core_id =
            sorted_order_[sorted_order_.size() - (i + exclude_worker0) - 1];
Minjie Wang's avatar
Minjie Wang committed
104
105
106
107
108
109
110
      } else {
        core_id = sorted_order_[i + exclude_worker0];
      }
      cpu_set_t cpuset;
      CPU_ZERO(&cpuset);
      CPU_SET(core_id, &cpuset);
#if defined(__ANDROID__)
111
112
      sched_setaffinity(
          threads_[i].native_handle(), sizeof(cpu_set_t), &cpuset);
Minjie Wang's avatar
Minjie Wang committed
113
#else
114
115
      pthread_setaffinity_np(
          threads_[i].native_handle(), sizeof(cpu_set_t), &cpuset);
Minjie Wang's avatar
Minjie Wang committed
116
117
118
119
120
121
122
123
124
125
126
#endif
    }
    if (exclude_worker0) {  // bind the master thread to core 0
      cpu_set_t cpuset;
      CPU_ZERO(&cpuset);
      if (reverse) {
        CPU_SET(sorted_order_[sorted_order_.size() - 1], &cpuset);
      } else {
        CPU_SET(sorted_order_[0], &cpuset);
      }
#if defined(__ANDROID__)
127
      sched_setaffinity(pthread_self(), sizeof(cpu_set_t), &cpuset);
Minjie Wang's avatar
Minjie Wang committed
128
#else
129
      pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
Minjie Wang's avatar
Minjie Wang committed
130
131
132
133
134
135
136
#endif
    }
#endif
  }

  void InitSortedOrder() {
    unsigned int threads = std::thread::hardware_concurrency();
137
    std::vector<std::pair<unsigned int, int64_t> > max_freqs;
Minjie Wang's avatar
Minjie Wang committed
138
139
140

    for (unsigned int i = 0; i < threads; ++i) {
      int64_t cur_freq = 0;
141
142
143
144
145
146
147
148
#if defined(__linux__) || defined(__ANDROID__)
      std::ostringstream filepath;
      filepath << "/sys/devices/system/cpu/cpu" << i
               << "/cpufreq/cpuinfo_max_freq";
      std::ifstream ifs(filepath.str());
      if (!ifs.fail()) {
        if (!(ifs >> cur_freq)) {
          cur_freq = -1;
Minjie Wang's avatar
Minjie Wang committed
149
        }
150
151
152
        ifs.close();
      }
#endif
Minjie Wang's avatar
Minjie Wang committed
153
154
155
      max_freqs.push_back(std::make_pair(i, cur_freq));
    }

156
157
158
    auto fcmpbyfreq = [](const std::pair<unsigned int, int64_t> &a,
                         const std::pair<unsigned int, int64_t> &b) {
      return a.second == b.second ? a.first < b.first : a.second > b.second;
Minjie Wang's avatar
Minjie Wang committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    };
    std::sort(max_freqs.begin(), max_freqs.end(), fcmpbyfreq);
    int64_t big_freq = max_freqs.begin()->second;
    int64_t little_freq = max_freqs.rbegin()->second;
    for (auto it = max_freqs.begin(); it != max_freqs.end(); it++) {
      sorted_order_.push_back(it->first);
      if (big_freq == it->second) {
        big_count_++;
      }
      if (big_freq != little_freq && little_freq == it->second) {
        little_count_++;
      }
    }
    if (big_count_ + little_count_ != static_cast<int>(sorted_order_.size())) {
      LOG(WARNING) << "more than two frequencies detected!";
    }
  }

  int num_workers_;
  std::vector<std::thread> threads_;
  std::vector<unsigned int> sorted_order_;
  int big_count_ = 0;
  int little_count_ = 0;
};

184
185
186
187
188
ThreadGroup::ThreadGroup(
    int num_workers, std::function<void(int)> worker_callback,
    bool exclude_worker0)
    : impl_(new ThreadGroup::Impl(
          num_workers, worker_callback, exclude_worker0)) {}
Minjie Wang's avatar
Minjie Wang committed
189
190
191
ThreadGroup::~ThreadGroup() { delete impl_; }
void ThreadGroup::Join() { impl_->Join(); }

192
193
int ThreadGroup::Configure(
    AffinityMode mode, int nthreads, bool exclude_worker0) {
Minjie Wang's avatar
Minjie Wang committed
194
195
196
  return impl_->Configure(mode, nthreads, exclude_worker0);
}

197
void YieldThread() { std::this_thread::yield(); }
Minjie Wang's avatar
Minjie Wang committed
198
199
200

int MaxConcurrency() {
  int max_concurrency = 1;
201
  const char *val = getenv("DGL_NUM_THREADS");
Minjie Wang's avatar
Minjie Wang committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  if (val == nullptr) {
    val = getenv("OMP_NUM_THREADS");
  }
  if (val != nullptr) {
    max_concurrency = atoi(val);
  } else {
    max_concurrency = std::thread::hardware_concurrency();
#if defined(_M_X64) || defined(__x86_64__)
    max_concurrency /= 2;  // ignore hyper-threading
#endif
  }
  return std::max(max_concurrency, 1);
}

}  // namespace threading
}  // namespace runtime
218
}  // namespace dgl