context.cc 9.8 KB
Newer Older
S
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

S
Superjomn 已提交
15
#include "paddle/fluid/lite/core/context.h"
T
tensor-tang 已提交
16 17
#include "paddle/fluid/lite/core/cpu_info.h"

18
#ifdef LITE_WITH_LINUX
T
tensor-tang 已提交
19 20 21 22 23 24 25 26 27 28 29 30
#include <sys/syscall.h>
#include <unistd.h>
#endif
#if __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE
#include <mach/machine.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#endif  // TARGET_OS_IPHONE
#endif  // __APPLE__

T
tensor-tang 已提交
31 32 33 34
#ifdef ARM_WITH_OMP
#include <omp.h>
#endif

T
tensor-tang 已提交
35 36 37 38 39
namespace paddle {
namespace lite {

#ifdef LITE_WITH_ARM

40
void Context<TargetType::kARM>::SetCache(int l1size, int l2size, int l3size) {
T
tensor-tang 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
  DeviceInfo& dev = DeviceInfo::Global();
  int cpu_count = arm_get_cpucount();
  dev.L1_cache_.resize(cpu_count);
  dev.L2_cache_.resize(cpu_count);
  dev.L3_cache_.resize(cpu_count);
  for (int i = 0; i < cpu_count; ++i) {
    dev.L1_cache_[i] = l1size;
    dev.L2_cache_[i] = l2size;
    dev.L3_cache_[i] = l3size;
  }
  workspace_.Resize({2 * (l1size + l2size)});
}

54
Context<TargetType::kARM>::Context() {
T
tensor-tang 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
  active_ids_ = {0};
  mode_ = LITE_POWER_HIGH;
  DeviceInfo& dev = DeviceInfo::Global();
  workspace_.Resize(
      {static_cast<int64_t>(dev.L2_cache_[active_ids_[0]] / sizeof(float))});
#ifdef TARGET_IOS
  arch_ = APPLE;  // use 6x8
#else
  if (dev.big_core_ids_.size() > 0) {
    arch_ = dev.archs_[dev.big_core_ids_[0]];
  }
#endif
}

69
PowerMode Context<TargetType::kARM>::mode() const { return mode_; }
T
tensor-tang 已提交
70

71
int Context<TargetType::kARM>::threads() const { return active_ids_.size(); }
T
tensor-tang 已提交
72

73
Context<TargetType::kARM>::Context(const ARMContext& ctx) {
T
tensor-tang 已提交
74 75 76 77 78 79 80
  mode_ = ctx.mode_;
  active_ids_ = ctx.active_ids_;
  workspace_ = ctx.workspace_;
  arch_ = ctx.arch_;
  count_ = ctx.count_;
}

81
ARMContext& Context<TargetType::kARM>::operator=(const ARMContext& ctx) {
T
tensor-tang 已提交
82 83 84 85 86 87 88 89
  mode_ = ctx.mode_;
  active_ids_ = ctx.active_ids_;
  workspace_ = ctx.workspace_;
  arch_ = ctx.arch_;
  count_ = ctx.count_;
  return *this;
}

90
void Context<TargetType::kARM>::BindDev() {
T
tensor-tang 已提交
91
#ifdef ARM_WITH_OMP
T
tensor-tang 已提交
92 93
  int num_threads = active_ids_.size();
  omp_set_num_threads(num_threads);
94
#ifdef LITE_WITH_LINUX
T
tensor-tang 已提交
95 96 97 98 99 100 101 102 103 104
  std::vector<int> ssarets;
  for (int j = 0; j < num_threads; ++j) {
    ssarets.push_back(0);
  }
#pragma omp parallel for
  for (int i = 0; i < num_threads; i++) {
    ssarets[i] = set_sched_affinity(active_ids_);
  }
  for (int i = 0; i < num_threads; i++) {
    if (ssarets[i] != 0) {
T
tensor-tang 已提交
105
      LOG(ERROR) << "set cpu affinity failed, cpuID: " << active_ids_[i];
T
tensor-tang 已提交
106 107 108
      return;
    }
  }
109
#endif  // LITE_WITH_LINUX
T
tensor-tang 已提交
110
#else   // ARM_WITH_OMP
111
#ifdef LITE_WITH_LINUX
T
tensor-tang 已提交
112 113 114 115 116 117 118
  std::vector<int> cpuid1;
  cpuid1.push_back(active_ids_[0]);
  int ssaret = set_sched_affinity(cpuid1);
  if (ssaret != 0) {
    printf("set cpu affinity failed, cpuID: %d\n", active_ids_[0]);
    return;
  }
119
#endif  // LITE_WITH_LINUX
T
tensor-tang 已提交
120
#endif  // ARM_WITH_OMP
T
tensor-tang 已提交
121 122
}

123
void Context<TargetType::kARM>::SetRunMode(PowerMode mode, int threads) {
T
tensor-tang 已提交
124 125 126 127 128 129
  DeviceInfo& dev = DeviceInfo::Global();
  int big_core_size = dev.big_core_ids_.size();
  int small_core_size = dev.little_core_ids_.size();
  if (threads > big_core_size + small_core_size) {
    threads = big_core_size + small_core_size;
  }
T
tensor-tang 已提交
130
#ifdef ARM_WITH_OMP
T
tensor-tang 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  count_++;
  int shift_num = (count_ / 10) % big_core_size;
  switch (mode) {
    case LITE_POWER_FULL:
      mode_ = mode;
      active_ids_.clear();
      for (int i = 0; i < threads; ++i) {
        if (i < big_core_size) {
          active_ids_.push_back(dev.big_core_ids_[i]);
        } else {
          active_ids_.push_back(dev.little_core_ids_[i - big_core_size]);
        }
      }
      if (active_ids_.size() == 0) {
        active_ids_.push_back(0);
      }
      break;
    case LITE_POWER_HIGH:
      active_ids_.clear();
      if (big_core_size > 0) {
        mode_ = LITE_POWER_HIGH;
        if (threads > big_core_size) {
T
tensor-tang 已提交
153 154
          LOG(ERROR) << "threads: " << threads
                     << ", exceed the big cores size: " << big_core_size;
T
tensor-tang 已提交
155 156 157 158 159 160 161 162
          active_ids_ = dev.big_core_ids_;
        } else {
          for (int i = 0; i < threads; ++i) {
            active_ids_.push_back(dev.big_core_ids_[i]);
          }
        }
      } else {
        mode_ = LITE_POWER_LOW;
T
tensor-tang 已提交
163
        LOG(ERROR) << "HIGH POWER MODE is not support, switch to little cores";
T
tensor-tang 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        if (threads > small_core_size) {
          active_ids_ = dev.little_core_ids_;
        } else {
          for (int i = 0; i < threads; ++i) {
            active_ids_.push_back(dev.little_core_ids_[i]);
          }
        }
      }
      if (active_ids_.size() == 0) {
        active_ids_.push_back(0);
      }
      break;
    case LITE_POWER_LOW:
      active_ids_.clear();
      if (small_core_size > 0) {
        mode_ = LITE_POWER_LOW;
        if (threads > small_core_size) {
T
tensor-tang 已提交
181 182
          LOG(WARNING) << "threads: " << threads
                       << ", exceed the little cores size: " << small_core_size;
T
tensor-tang 已提交
183 184 185 186 187 188 189 190
          active_ids_ = dev.little_core_ids_;
        } else {
          for (int i = 0; i < threads; ++i) {
            active_ids_.push_back(dev.little_core_ids_[i]);
          }
        }
      } else {
        mode_ = LITE_POWER_HIGH;
T
tensor-tang 已提交
191
        LOG(WARNING) << "LOW POWER MODE is not support, switch to big cores";
T
tensor-tang 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        if (threads > big_core_size) {
          active_ids_ = dev.big_core_ids_;
        } else {
          for (int i = 0; i < threads; ++i) {
            active_ids_.push_back(dev.big_core_ids_[i]);
          }
        }
      }
      if (active_ids_.size() == 0) {
        active_ids_.push_back(0);
      }
      break;
    case LITE_POWER_NO_BIND:
      mode_ = LITE_POWER_NO_BIND;
      active_ids_.clear();
      if (threads > dev.core_ids_.size()) {
        active_ids_.resize(dev.core_ids_.size());
      } else {
        active_ids_.resize(threads);
      }
      break;
    case LITE_POWER_RAND_HIGH:
      active_ids_.clear();
      if (big_core_size > 0) {
        mode_ = LITE_POWER_RAND_HIGH;
        if (threads > big_core_size) {
T
tensor-tang 已提交
218 219
          LOG(WARNING) << "threads: " << threads
                       << ", exceed the big cores size: " << big_core_size;
T
tensor-tang 已提交
220 221 222 223 224 225 226 227 228
          active_ids_ = dev.big_core_ids_;
        } else {
          for (int i = 0; i < threads; ++i) {
            active_ids_.push_back(
                dev.big_core_ids_[(i + shift_num) % big_core_size]);
          }
        }
      } else {
        mode_ = LITE_POWER_LOW;
T
tensor-tang 已提交
229 230
        LOG(WARNING)
            << "HIGH POWER MODE is not support, switch to little cores";
T
tensor-tang 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        if (threads > small_core_size) {
          active_ids_ = dev.little_core_ids_;
        } else {
          for (int i = 0; i < threads; ++i) {
            active_ids_.push_back(dev.little_core_ids_[i]);
          }
        }
      }
      if (active_ids_.size() == 0) {
        active_ids_.push_back(0);
      }
      break;
    case LITE_POWER_RAND_LOW:
      active_ids_.clear();
      if (small_core_size > 0) {
        mode_ = LITE_POWER_RAND_LOW;
        if (threads > small_core_size) {
T
tensor-tang 已提交
248 249
          LOG(WARNING) << "threads: " << threads
                       << ", exceed the little cores size: " << small_core_size;
T
tensor-tang 已提交
250 251 252 253 254 255 256 257 258
          active_ids_ = dev.little_core_ids_;
        } else {
          for (int i = 0; i < threads; ++i) {
            active_ids_.push_back(
                dev.little_core_ids_[(i + shift_num) % small_core_size]);
          }
        }
      } else {
        mode_ = LITE_POWER_HIGH;
T
tensor-tang 已提交
259
        LOG(WARNING) << "LOW POWER MODE is not support, switch to big cores";
T
tensor-tang 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        if (threads > big_core_size) {
          active_ids_ = dev.big_core_ids_;
        } else {
          for (int i = 0; i < threads; ++i) {
            active_ids_.push_back(dev.big_core_ids_[i]);
          }
        }
      }
      if (active_ids_.size() == 0) {
        active_ids_.push_back(0);
      }
      break;
  }
  //! fix multi-threads LITE_POWER_HIGH mode
  if (mode_ == LITE_POWER_NO_BIND || threads > 1) {
    int threads = active_ids_.size();
    omp_set_num_threads(threads);
  } else {
    if (check_online(active_ids_)) {
      BindDev();
    } else {
      LOG(ERROR) << "core id " << active_ids_[0]
                 << " is offline, switch to NO BIND MODE";
      int threads = active_ids_.size();
      omp_set_num_threads(threads);
    }
  }
#else
  if (big_core_size > 0) {
    active_ids_ = {dev.big_core_ids_[0]};
  } else {
    active_ids_ = {0};
  }
#endif
  //! alloc memory for sgemm in this context
  int temp_mem_size =
      DeviceInfo::Global().L2_cache_[active_ids_[0]] / sizeof(float);
  workspace_.Resize({temp_mem_size});
  arch_ = DeviceInfo::Global().archs_[active_ids_[0]];
}

301
ARMArch Context<TargetType::kARM>::arch() const { return arch_; }
T
tensor-tang 已提交
302

303
void Context<TargetType::kARM>::SetArch(ARMArch arch) { arch_ = arch; }
T
tensor-tang 已提交
304

305
int Context<TargetType::kARM>::l1_cache_size() const {
T
tensor-tang 已提交
306 307 308 309
  DeviceInfo& dev = DeviceInfo::Global();
  return dev.L1_cache_[active_ids_[0]];
}

310
int Context<TargetType::kARM>::l2_cache_size() const {
T
tensor-tang 已提交
311 312 313 314
  DeviceInfo& dev = DeviceInfo::Global();
  return dev.L2_cache_[active_ids_[0]];
}

315
int Context<TargetType::kARM>::l3_cache_size() const {
T
tensor-tang 已提交
316 317 318 319
  DeviceInfo& dev = DeviceInfo::Global();
  return dev.L3_cache_[active_ids_[0]];
}

320
bool Context<TargetType::kARM>::ExtendWorkspace(DDimLite dims) {
T
tensor-tang 已提交
321 322 323 324 325 326 327 328 329 330 331
  auto count = dims.product();
  auto old = workspace_.dims();
  if (count == old.product()) {
    return false;
  }

  workspace_.Resize(
      {static_cast<int64_t>(count + l2_cache_size() / sizeof(float))});
  return true;
}
#endif  // LITE_WITH_ARM
332

T
tensor-tang 已提交
333 334
}  // namespace lite
}  // namespace paddle