cpu_info.cc 4.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/platform/cpu_info.h"
T
tensor-tang 已提交
16 17

#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
18 19
#include "xbyak/xbyak.h"
#include "xbyak/xbyak_util.h"
T
tensor-tang 已提交
20
#endif
L
liaogang 已提交
21 22 23 24

#ifdef __APPLE__
#include <sys/sysctl.h>
#include <sys/types.h>
D
dzhwinter 已提交
25
#elif defined(_WIN32)
D
dzhwinter 已提交
26
#define NOMINMAX
D
dzhwinter 已提交
27 28
#include <windows.h>
#include <psapi.h>
L
liaogang 已提交
29 30 31 32
#else
#include <unistd.h>
#endif

33
#include <algorithm>
L
liaogang 已提交
34 35 36 37 38
#include "gflags/gflags.h"

DEFINE_double(fraction_of_cpu_memory_to_use, 1,
              "Default use 100% of CPU memory for PaddlePaddle,"
              "reserve the rest for page tables, etc");
D
dzhwinter 已提交
39
#if !defined(_WIN32)
40 41
DEFINE_uint64(initial_cpu_memory_in_mb,
#ifdef PADDLE_WITH_MKLDNN
42 43
              /* Aligned with mozga-intel, MKLDNN need at least 5000 MB
               * to obtain the best performance*/
D
dzhwinter 已提交
44
              5000ul,
45
#else
D
dzhwinter 已提交
46
              500ul,
47 48
#endif
              "Initial CPU memory for PaddlePaddle, in MD unit.");
D
dzhwinter 已提交
49 50 51
#else
DEFINE_uint64(initial_cpu_memory_in_mb, 500ul, "Initial CPU memory for PaddlePaddle, in MD unit.");
#endif // !defined(_WIN32)
52

C
chengduoZH 已提交
53 54 55 56
DEFINE_double(
    fraction_of_cuda_pinned_memory_to_use, 0.5,
    "Default use 50% of CPU memory as the pinned_memory for PaddlePaddle,"
    "reserve the rest for page tables, etc");
57

L
liaogang 已提交
58 59 60 61 62 63 64 65 66 67 68 69
namespace paddle {
namespace platform {

inline size_t CpuTotalPhysicalMemory() {
#ifdef __APPLE__
  int mib[2];
  mib[0] = CTL_HW;
  mib[1] = HW_MEMSIZE;
  int64_t size = 0;
  size_t len = sizeof(size);
  if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) return (size_t)size;
  return 0L;
D
dzhwinter 已提交
70 71 72 73 74
#elif defined(_WIN32)
MEMORYSTATUSEX sMeminfo;
sMeminfo.dwLength = sizeof(sMeminfo);
GlobalMemoryStatusEx(&sMeminfo);
return sMeminfo.ullTotalPhys;
L
liaogang 已提交
75
#else
L
liaogang 已提交
76 77
  int64_t pages = sysconf(_SC_PHYS_PAGES);
  int64_t page_size = sysconf(_SC_PAGE_SIZE);
L
liaogang 已提交
78 79 80 81
  return pages * page_size;
#endif
}

L
liaogang 已提交
82 83 84
size_t CpuMaxAllocSize() {
  // For distributed systems, it requires configuring and limiting
  // the fraction of memory to use.
85
  return FLAGS_fraction_of_cpu_memory_to_use * CpuTotalPhysicalMemory();
L
liaogang 已提交
86 87
}

L
liaogang 已提交
88
size_t CpuMinChunkSize() {
L
liaogang 已提交
89 90
  // Allow to allocate the minimum chunk size is 4 KB.
  return 1 << 12;
L
liaogang 已提交
91 92 93
}

size_t CpuMaxChunkSize() {
94 95 96 97 98
  // Allow to allocate the maximum chunk size is roughly 3% of CPU memory,
  // or the initial_cpu_memory_in_mb.
  return std::min(
      static_cast<size_t>(CpuMaxAllocSize() / 32),
      static_cast<size_t>(FLAGS_initial_cpu_memory_in_mb * 1 << 20));
L
liaogang 已提交
99 100
}

101 102 103 104 105 106 107 108 109 110 111 112
size_t CUDAPinnedMaxAllocSize() {
  // For distributed systems, it requires configuring and limiting
  // the fraction of memory to use.
  return FLAGS_fraction_of_cuda_pinned_memory_to_use * CpuTotalPhysicalMemory();
}

size_t CUDAPinnedMinChunkSize() {
  // Allow to allocate the minimum chunk size is 64 KB.
  return 1 << 16;
}

size_t CUDAPinnedMaxChunkSize() {
C
chengduoZH 已提交
113
  // Allow to allocate the maximum chunk size is roughly 1/256 of CUDA_PINNED
114 115 116 117
  // memory.
  return CUDAPinnedMaxAllocSize() / 256;
}

T
tensor-tang 已提交
118
namespace jit {
T
tensor-tang 已提交
119
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
120 121 122 123 124 125
static Xbyak::util::Cpu cpu;
bool MayIUse(const cpu_isa_t cpu_isa) {
  using namespace Xbyak::util;  // NOLINT
  switch (cpu_isa) {
    case sse42:
      return cpu.has(Cpu::tSSE42);
T
tensor-tang 已提交
126 127
    case avx:
      return cpu.has(Cpu::tAVX);
T
tensor-tang 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    case avx2:
      return cpu.has(Cpu::tAVX2);
    case avx512_common:
      return cpu.has(Cpu::tAVX512F);
    case avx512_core:
      return true && cpu.has(Cpu::tAVX512F) && cpu.has(Cpu::tAVX512BW) &&
             cpu.has(Cpu::tAVX512VL) && cpu.has(Cpu::tAVX512DQ);
    case avx512_core_vnni:
      return true && cpu.has(Cpu::tAVX512F) && cpu.has(Cpu::tAVX512BW) &&
             cpu.has(Cpu::tAVX512VL) && cpu.has(Cpu::tAVX512DQ) &&
             cpu.has(Cpu::tAVX512_VNNI);
    case avx512_mic:
      return true && cpu.has(Cpu::tAVX512F) && cpu.has(Cpu::tAVX512CD) &&
             cpu.has(Cpu::tAVX512ER) && cpu.has(Cpu::tAVX512PF);
    case avx512_mic_4ops:
      return true && MayIUse(avx512_mic) && cpu.has(Cpu::tAVX512_4FMAPS) &&
             cpu.has(Cpu::tAVX512_4VNNIW);
    case isa_any:
      return true;
  }
  return false;
}
T
tensor-tang 已提交
150 151 152 153 154 155 156 157 158
#else
bool MayIUse(const cpu_isa_t cpu_isa) {
  if (cpu_isa == isa_any) {
    return true;
  } else {
    return false;
  }
}
#endif
T
tensor-tang 已提交
159 160

}  // namespace jit
L
liaogang 已提交
161 162
}  // namespace platform
}  // namespace paddle