Skip to content

Commit 481b6d0

Browse files
Ilia Cherniavskiifacebook-github-bot
authored andcommitted
Allow a non-OpenMP based build (pytorch#19749)
Summary: Pull Request resolved: pytorch#19749 ghimport-source-id: a6636c0 Differential Revision: D15141993 Pulled By: ilia-cher fbshipit-source-id: 96085608398b2a4c97c68b2948f5184d07f9ad3d
1 parent 8c97f0b commit 481b6d0

File tree

16 files changed

+211
-52
lines changed

16 files changed

+211
-52
lines changed

.jenkins/pytorch/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ test_xla() {
203203
}
204204

205205
(cd test && python -c "import torch; print(torch.__config__.show())")
206+
(cd test && python -c "import torch; print(torch.__config__.parallel_info())")
206207

207208
if [[ "${BUILD_ENVIRONMENT}" == *xla* ]]; then
208209
test_torchvision

aten/src/ATen/Parallel.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include <ATen/Parallel.h>
22

3+
#include <ATen/Config.h>
4+
#include <ATen/Version.h>
5+
36
#include <atomic>
7+
#include <sstream>
48

59
#ifdef TH_BLAS_MKL
610
#include <mkl.h>
@@ -60,6 +64,41 @@ size_t get_num_threads() {
6064
#endif
6165
}
6266

67+
namespace {
68+
const char* get_env_var(const char* var_name) {
69+
const char* value = std::getenv(var_name);
70+
return value ? value : "[not set]";
71+
}
72+
}
73+
74+
std::string get_parallel_info() {
75+
std::ostringstream ss;
76+
77+
ss << "ATen/Parallel:\n\tat::get_num_threads() : "
78+
<< at::get_num_threads() << std::endl;
79+
80+
ss << at::get_openmp_version() << std::endl;
81+
#ifdef _OPENMP
82+
ss << "\tomp_get_max_threads() : " << omp_get_max_threads() << std::endl;
83+
#endif
84+
85+
ss << at::get_mkl_version() << std::endl;
86+
#ifdef TH_BLAS_MKL
87+
ss << "\tmkl_get_max_threads() : " << mkl_get_max_threads() << std::endl;
88+
#endif
89+
90+
ss << at::get_mkldnn_version() << std::endl;
91+
92+
ss << "std::thread::hardware_concurrency() : "
93+
<< std::thread::hardware_concurrency() << std::endl;
94+
95+
ss << "Environment variables:" << std::endl;
96+
ss << "\tOMP_NUM_THREADS : " << get_env_var("OMP_NUM_THREADS") << std::endl;
97+
ss << "\tMKL_NUM_THREADS : " << get_env_var("MKL_NUM_THREADS") << std::endl;
98+
99+
return ss.str();
100+
}
101+
63102
PTThreadPool::PTThreadPool(
64103
std::size_t pool_size,
65104
int numa_node_id)

aten/src/ATen/Parallel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ inline scalar_t parallel_reduce(
143143
}
144144
}
145145

146+
// Returns a detailed string describing parallelization settings
147+
CAFFE2_API std::string get_parallel_info();
148+
146149
class CAFFE2_API PTThreadPool : public c10::ThreadPool {
147150
public:
148151
explicit PTThreadPool(

aten/src/ATen/Version.cpp

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,78 @@
1616

1717
namespace at {
1818

19+
std::string get_mkl_version() {
20+
std::string version;
21+
#if AT_MKL_ENABLED()
22+
{
23+
// Magic buffer number is from MKL documentation
24+
// https://software.intel.com/en-us/mkl-developer-reference-c-mkl-get-version-string
25+
char buf[198];
26+
mkl_get_version_string(buf, 198);
27+
version = buf;
28+
}
29+
#else
30+
version = "MKL not found";
31+
#endif
32+
return version;
33+
}
34+
35+
std::string get_mkldnn_version() {
36+
std::ostringstream ss;
37+
#if AT_MKLDNN_ENABLED()
38+
// Cribbed from mkl-dnn/src/common/verbose.cpp
39+
// Too bad: can't get ISA info conveniently :(
40+
// Apparently no way to get ideep version?
41+
// https://github.com/intel/ideep/issues/29
42+
{
43+
const mkldnn_version_t* ver = mkldnn_version();
44+
ss << "Intel(R) MKL-DNN v" << ver->major << "." << ver->minor << "." << ver->patch
45+
<< " (Git Hash " << ver->hash << ")";
46+
}
47+
#else
48+
ss << "MKLDNN not found";
49+
#endif
50+
return ss.str();
51+
}
52+
53+
std::string get_openmp_version() {
54+
std::ostringstream ss;
55+
#ifdef _OPENMP
56+
{
57+
ss << "OpenMP " << _OPENMP;
58+
// Reference:
59+
// https://stackoverflow.com/questions/1304363/how-to-check-the-version-of-openmp-on-linux
60+
const char* ver_str = nullptr;
61+
switch (_OPENMP) {
62+
case 200505:
63+
ver_str = "2.5";
64+
break;
65+
case 200805:
66+
ver_str = "3.0";
67+
break;
68+
case 201107:
69+
ver_str = "3.1";
70+
break;
71+
case 201307:
72+
ver_str = "4.0";
73+
break;
74+
case 201511:
75+
ver_str = "4.5";
76+
break;
77+
default:
78+
ver_str = nullptr;
79+
break;
80+
}
81+
if (ver_str) {
82+
ss << " (a.k.a. OpenMP " << ver_str << ")";
83+
}
84+
}
85+
#else
86+
ss << "OpenMP not found";
87+
#endif
88+
return ss.str();
89+
}
90+
1991
std::string show_config() {
2092
std::ostringstream ss;
2193
ss << "PyTorch built with:\n"; // TODO add the version of PyTorch
@@ -42,58 +114,15 @@ std::string show_config() {
42114
#endif
43115

44116
#if AT_MKL_ENABLED()
45-
{
46-
// Magic buffer number is from MKL documentation
47-
// https://software.intel.com/en-us/mkl-developer-reference-c-mkl-get-version-string
48-
char buf[198];
49-
mkl_get_version_string(buf, 198);
50-
ss << " - " << buf << "\n";
51-
}
117+
ss << " - " << get_mkl_version() << "\n";
52118
#endif
53119

54120
#if AT_MKLDNN_ENABLED()
55-
// Cribbed from mkl-dnn/src/common/verbose.cpp
56-
// Too bad: can't get ISA info conveniently :(
57-
// Apparently no way to get ideep version?
58-
// https://github.com/intel/ideep/issues/29
59-
{
60-
const mkldnn_version_t* ver = mkldnn_version();
61-
ss << " - Intel(R) MKL-DNN v" << ver->major << "." << ver->minor << "." << ver->patch
62-
<< " (Git Hash " << ver->hash << ")\n";
63-
}
121+
ss << " - " << get_mkldnn_version() << "\n";
64122
#endif
65123

66124
#ifdef _OPENMP
67-
{
68-
ss << " - OpenMP " << _OPENMP;
69-
// Reference:
70-
// https://stackoverflow.com/questions/1304363/how-to-check-the-version-of-openmp-on-linux
71-
const char* ver_str = nullptr;
72-
switch (_OPENMP) {
73-
case 200505:
74-
ver_str = "2.5";
75-
break;
76-
case 200805:
77-
ver_str = "3.0";
78-
break;
79-
case 201107:
80-
ver_str = "3.1";
81-
break;
82-
case 201307:
83-
ver_str = "4.0";
84-
break;
85-
case 201511:
86-
ver_str = "4.5";
87-
break;
88-
default:
89-
ver_str = nullptr;
90-
break;
91-
}
92-
if (ver_str) {
93-
ss << " (a.k.a. OpenMP " << ver_str << ")";
94-
}
95-
ss << "\n";
96-
}
125+
ss << " - " << get_openmp_version() << "\n";
97126
#endif
98127

99128
#ifdef USE_LAPACK

aten/src/ATen/Version.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ namespace at {
55
/// Returns a detailed string describing the configuration PyTorch.
66
CAFFE2_API std::string show_config();
77

8+
CAFFE2_API std::string get_mkl_version();
9+
10+
CAFFE2_API std::string get_mkldnn_version();
11+
12+
CAFFE2_API std::string get_openmp_version();
13+
814
} // namespace at

aten/src/ATen/test/thread_init_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ int main() {
2828
t1.join();
2929

3030
at::set_num_threads(4);
31-
std::thread t2(test, 4);
32-
std::thread t3(test, 4);
33-
std::thread t4(test, 4);
31+
std::thread t2(test, at::get_num_threads());
32+
std::thread t3(test, at::get_num_threads());
33+
std::thread t4(test, at::get_num_threads());
3434
t4.join();
3535
t3.join();
3636
t2.join();
3737

3838
at::set_num_threads(5);
39-
test(5);
39+
test(at::get_num_threads());
4040

4141
return 0;
4242
}

binaries/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ caffe2_binary_target("convert_caffe_image_db.cc")
22
caffe2_binary_target("convert_db.cc")
33
caffe2_binary_target("make_cifar_db.cc")
44
caffe2_binary_target("make_mnist_db.cc")
5+
if (NOT ANDROID)
6+
caffe2_binary_target("parallel_info.cc")
7+
target_include_directories(parallel_info PUBLIC
8+
${CMAKE_BINARY_DIR}/aten/src) # provides "ATen/TypeExtendedInterface.h" to ATen.h
9+
endif()
510
caffe2_binary_target("predictor_verifier.cc")
611
caffe2_binary_target("print_registered_core_operators.cc")
712
caffe2_binary_target("run_plan.cc")

binaries/parallel_info.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2016-present, Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "ATen/Parallel.h"
18+
19+
#include <iostream>
20+
#include <sstream>
21+
22+
#ifdef __linux__
23+
#include <sys/types.h>
24+
#include <unistd.h>
25+
#endif
26+
27+
int main(int argc, char** argv) {
28+
at::init_num_threads();
29+
30+
std::cout << at::get_parallel_info() << std::endl;
31+
32+
# ifdef __linux__
33+
std::ostringstream cmd;
34+
cmd << "lsof -p " << getpid() << " | grep .so";
35+
std::cout << "Loaded .so:" << std::endl;
36+
std::cout << cmd.str() << std::endl;
37+
std::system(cmd.str().c_str());
38+
# endif
39+
40+
return 0;
41+
}

cmake/Modules/FindMKLDNN.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ ENDIF(MKL_FOUND)
8989

9090
IF(MKL_FOUND)
9191
SET(MKL_cmake_included TRUE)
92-
SET(MKLDNN_THREADING "OMP:COMP" CACHE STRING "" FORCE)
92+
SET(MKLDNN_THREADING "OMP:COMP" CACHE STRING "")
9393
ENDIF(MKL_FOUND)
9494
SET(WITH_TEST FALSE CACHE BOOL "" FORCE)
9595
SET(WITH_EXAMPLE FALSE CACHE BOOL "" FORCE)

docs/source/__config__.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ torch.__config__
44
.. automodule:: torch.__config__
55

66
.. autofunction:: show
7+
.. autofunction:: parallel_info

0 commit comments

Comments
 (0)