1 #include "caffe2/core/numa.h" 4 caffe2_cpu_numa_enabled,
6 "Use NUMA whenever possible.");
8 #if defined(__linux__) && !defined(CAFFE2_DISABLE_NUMA) && CAFFE2_MOBILE == 0 11 #define CAFFE2_NUMA_ENABLED 16 #ifdef CAFFE2_NUMA_ENABLED 17 bool IsNUMAEnabled() {
18 return FLAGS_caffe2_cpu_numa_enabled && numa_available() >= 0;
21 void NUMABind(
int numa_node_id) {
22 if (numa_node_id < 0) {
25 if (!IsNUMAEnabled()) {
26 VLOG(1) <<
"NUMA is not enabled";
31 numa_node_id <= numa_max_node(),
32 "NUMA node id " + caffe2::to_string(numa_node_id) +
" is unavailable");
34 auto bm = numa_allocate_nodemask();
35 numa_bitmask_clearall(bm);
36 numa_bitmask_setbit(bm, numa_node_id);
38 numa_bitmask_free(bm);
41 int GetNUMANode(
const void* ptr) {
42 if (!IsNUMAEnabled()) {
43 VLOG(1) <<
"NUMA is not enabled";
51 &numa_node, NULL, 0, (
void*)ptr, MPOL_F_NODE | MPOL_F_ADDR) == 0,
52 "Unable to get memory policy");
56 int GetNumNUMANodes() {
57 if (!IsNUMAEnabled()) {
58 VLOG(1) <<
"NUMA is not enabled";
62 return numa_num_configured_nodes();
65 void NUMAMove(
void* ptr,
size_t size,
int numa_node_id) {
66 if (numa_node_id < 0) {
69 if (!IsNUMAEnabled()) {
70 VLOG(1) <<
"NUMA is not enabled";
75 size_t page_start_ptr = (((size_t)ptr) & ~(getpagesize() - 1));
76 size_t offset = ((size_t)ptr) - page_start_ptr;
78 CAFFE_ENFORCE(numa_node_id >= 0 && numa_node_id <
sizeof(
unsigned long) * 8);
79 unsigned long mask = 1UL << numa_node_id;
82 (
void*)page_start_ptr,
87 MPOL_MF_MOVE | MPOL_MF_STRICT) == 0,
88 "Could not move memory to a NUMA node");
91 int GetCurrentNUMANode() {
92 if (!IsNUMAEnabled()) {
93 VLOG(1) <<
"NUMA is not enabled";
97 return numa_node_of_cpu(sched_getcpu());
100 #else // CAFFE2_NUMA_ENABLED 102 bool IsNUMAEnabled() {
106 void NUMABind(
int numa_node_id) {
107 if (numa_node_id >= 0) {
108 VLOG(1) <<
"NUMA is not enabled";
112 int GetNUMANode(
const void* ptr) {
113 VLOG(1) <<
"NUMA is not enabled";
117 int GetNumNUMANodes() {
118 VLOG(1) <<
"NUMA is not enabled";
122 void NUMAMove(
void* ptr,
size_t size,
int numa_node_id) {
123 if (numa_node_id >= 0) {
124 VLOG(1) <<
"NUMA is not enabled";
128 int GetCurrentNUMANode() {
129 VLOG(1) <<
"NUMA is not enabled";
133 #endif // CAFFE2_NUMA_ENABLED A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...