mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: Hi guys, I'd like to build Caffe2 with more supported options in Windows with Microsoft Visual Studios. This is the first pull request. Running scripts/build_windows_shared.bat is able to build Caffe2 with both CMAKE_BUILD_TYPE=Debug and CMAKE_BUILD_TYPE=Release with Visual Studio 14 2015. CUDA is 9.0, cudnn is 7.0.5, glog, gflags and lmdb are supported on my system. Python is 3.5, Detectron works from python interface as well. It was even possible to debug detectron code and step into caffe2_gpu.dll with pdbs built. What is disappointing, that c10/experimental ops don't build with this Visual Studio generator, I added special option INCLUDE_EXPERIMENTAL_C10_OPS (default ON) to deal with it in build_windows_shared.bat. After this pull request the next step is to add Visual Studio 2017 support in the script. Pull Request resolved: https://github.com/pytorch/pytorch/pull/13550 Reviewed By: ezyang Differential Revision: D13042597 Pulled By: orionr fbshipit-source-id: f313f909f599cd582a1d000eff766eef3a9fc4fc
133 lines
2.8 KiB
C++
133 lines
2.8 KiB
C++
#include "caffe2/core/numa.h"
|
|
|
|
C10_DEFINE_bool(caffe2_cpu_numa_enabled, false, "Use NUMA whenever possible.");
|
|
|
|
#if defined(__linux__) && !defined(CAFFE2_DISABLE_NUMA) && CAFFE2_MOBILE == 0
|
|
#include <numa.h>
|
|
#include <numaif.h>
|
|
#define CAFFE2_NUMA_ENABLED
|
|
#endif
|
|
|
|
namespace caffe2 {
|
|
|
|
#ifdef CAFFE2_NUMA_ENABLED
|
|
bool IsNUMAEnabled() {
|
|
return FLAGS_caffe2_cpu_numa_enabled && numa_available() >= 0;
|
|
}
|
|
|
|
void NUMABind(int numa_node_id) {
|
|
if (numa_node_id < 0) {
|
|
return;
|
|
}
|
|
if (!IsNUMAEnabled()) {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
return;
|
|
}
|
|
|
|
CAFFE_ENFORCE(
|
|
numa_node_id <= numa_max_node(),
|
|
"NUMA node id " + c10::to_string(numa_node_id) + " is unavailable");
|
|
|
|
auto bm = numa_allocate_nodemask();
|
|
numa_bitmask_clearall(bm);
|
|
numa_bitmask_setbit(bm, numa_node_id);
|
|
numa_bind(bm);
|
|
numa_bitmask_free(bm);
|
|
}
|
|
|
|
int GetNUMANode(const void* ptr) {
|
|
if (!IsNUMAEnabled()) {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
return -1;
|
|
}
|
|
CAFFE_ENFORCE(ptr);
|
|
|
|
int numa_node = -1;
|
|
CAFFE_ENFORCE(
|
|
get_mempolicy(
|
|
&numa_node, NULL, 0, (void*)ptr, MPOL_F_NODE | MPOL_F_ADDR) == 0,
|
|
"Unable to get memory policy");
|
|
return numa_node;
|
|
}
|
|
|
|
int GetNumNUMANodes() {
|
|
if (!IsNUMAEnabled()) {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
return -1;
|
|
}
|
|
|
|
return numa_num_configured_nodes();
|
|
}
|
|
|
|
void NUMAMove(void* ptr, size_t size, int numa_node_id) {
|
|
if (numa_node_id < 0) {
|
|
return;
|
|
}
|
|
if (!IsNUMAEnabled()) {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
return;
|
|
}
|
|
CAFFE_ENFORCE(ptr);
|
|
|
|
size_t page_start_ptr = (((size_t)ptr) & ~(getpagesize() - 1));
|
|
size_t offset = ((size_t)ptr) - page_start_ptr;
|
|
// Avoid extra dynamic allocation and NUMA api calls
|
|
CAFFE_ENFORCE(numa_node_id >= 0 && (unsigned)numa_node_id < sizeof(unsigned long) * 8);
|
|
unsigned long mask = 1UL << numa_node_id;
|
|
CAFFE_ENFORCE(
|
|
mbind(
|
|
(void*)page_start_ptr,
|
|
size + offset,
|
|
MPOL_BIND,
|
|
&mask,
|
|
sizeof(mask) * 8,
|
|
MPOL_MF_MOVE | MPOL_MF_STRICT) == 0,
|
|
"Could not move memory to a NUMA node");
|
|
}
|
|
|
|
int GetCurrentNUMANode() {
|
|
if (!IsNUMAEnabled()) {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
return -1;
|
|
}
|
|
|
|
return numa_node_of_cpu(sched_getcpu());
|
|
}
|
|
|
|
#else // CAFFE2_NUMA_ENABLED
|
|
|
|
bool IsNUMAEnabled() {
|
|
return false;
|
|
}
|
|
|
|
void NUMABind(int numa_node_id) {
|
|
if (numa_node_id >= 0) {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
}
|
|
}
|
|
|
|
int GetNUMANode(const void* ptr) {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
return -1;
|
|
}
|
|
|
|
int GetNumNUMANodes() {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
return -1;
|
|
}
|
|
|
|
void NUMAMove(void* ptr, size_t size, int numa_node_id) {
|
|
if (numa_node_id >= 0) {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
}
|
|
}
|
|
|
|
int GetCurrentNUMANode() {
|
|
VLOG(1) << "NUMA is not enabled";
|
|
return -1;
|
|
}
|
|
|
|
#endif // CAFFE2_NUMA_ENABLED
|
|
|
|
} // namespace caffe2
|