cpu_info.cc 2.7 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"
L
liaogang 已提交
16 17 18 19 20 21 22 23

#ifdef __APPLE__
#include <sys/sysctl.h>
#include <sys/types.h>
#else
#include <unistd.h>
#endif

24
#include <algorithm>
L
liaogang 已提交
25 26 27 28 29 30
#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");

31 32 33 34
DEFINE_uint64(
    initial_cpu_memory_in_mb, 500,
    "Default initial 500MB of CPU memory for PaddlePaddle, in MD unit.");

C
chengduoZH 已提交
35 36 37 38
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");
39

L
liaogang 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
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;
#else
L
liaogang 已提交
53 54
  int64_t pages = sysconf(_SC_PHYS_PAGES);
  int64_t page_size = sysconf(_SC_PAGE_SIZE);
L
liaogang 已提交
55 56 57 58
  return pages * page_size;
#endif
}

L
liaogang 已提交
59 60 61
size_t CpuMaxAllocSize() {
  // For distributed systems, it requires configuring and limiting
  // the fraction of memory to use.
Q
qiaolongfei 已提交
62 63 64 65
  return std::min(
      static_cast<size_t>(FLAGS_fraction_of_cpu_memory_to_use *
                          CpuTotalPhysicalMemory()),
      static_cast<size_t>(FLAGS_initial_cpu_memory_in_mb * 1 << 20));
L
liaogang 已提交
66 67
}

L
liaogang 已提交
68
size_t CpuMinChunkSize() {
L
liaogang 已提交
69 70
  // Allow to allocate the minimum chunk size is 4 KB.
  return 1 << 12;
L
liaogang 已提交
71 72 73 74 75 76 77
}

size_t CpuMaxChunkSize() {
  // Allow to allocate the maximum chunk size is roughly 3% of CPU memory.
  return CpuMaxAllocSize() / 32;
}

78 79 80 81 82 83 84 85 86 87 88 89
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 已提交
90
  // Allow to allocate the maximum chunk size is roughly 1/256 of CUDA_PINNED
91 92 93 94
  // memory.
  return CUDAPinnedMaxAllocSize() / 256;
}

L
liaogang 已提交
95 96
}  // namespace platform
}  // namespace paddle