pytorch/caffe2/core/init_omp.cc
Nikita Shulga 4cb534f92e Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os

def get_compiled_files_list():
    import json
    with open("build/compile_commands.json") as f:
        data = json.load(f)
    files = [os.path.relpath(node['file']) for node in data]
    for idx, fname in enumerate(files):
        if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
            files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
    return files

def run_clang_tidy(fname):
    check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
    changes = check_output(["git", "ls-files", "-m"])
    if len(changes) == 0:
        return
    check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])

def main():
    git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
    compiled_files = get_compiled_files_list()
    for idx, fname in enumerate(git_files):
        if fname not in compiled_files:
            continue
        if fname.startswith("caffe2/contrib/aten/"):
            continue
        print(f"[{idx}/{len(git_files)}] Processing {fname}")
        run_clang_tidy(fname)

if __name__ == "__main__":
    main()
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892

Reviewed By: H-Huang

Differential Revision: D27991944

Pulled By: malfet

fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 14:10:25 -07:00

81 lines
2.5 KiB
C++

// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <stdlib.h>
#include "caffe2/core/common.h"
#ifdef _OPENMP
#include "caffe2/core/common_omp.h"
#endif // _OPENMP
#ifdef CAFFE2_USE_MKL
#include <mkl.h>
#endif // CAFFE2_USE_MKL
#include "caffe2/core/init.h"
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
C10_DEFINE_int(
caffe2_omp_num_threads,
0,
"The number of openmp threads. 0 to use default value. "
"Does not have effect if OpenMP is disabled.");
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
C10_DEFINE_int(
caffe2_mkl_num_threads,
0,
"The number of mkl threads. 0 to use default value. If set, "
"this overrides the caffe2_omp_num_threads flag if both are set. "
"Does not have effect if MKL is not used.");
namespace caffe2 {
#ifdef _OPENMP
bool Caffe2SetOpenMPThreads(int*, char***) {
if (!getenv("OMP_NUM_THREADS")) {
// OMP_NUM_THREADS not passed explicitly, so *disable* OMP by
// default. The user can use the CLI flag to override.
VLOG(1) << "OMP_NUM_THREADS not passed, defaulting to 1 thread";
omp_set_num_threads(1);
}
if (FLAGS_caffe2_omp_num_threads > 0) {
VLOG(1) << "Setting omp_num_threads to " << FLAGS_caffe2_omp_num_threads;
omp_set_num_threads(FLAGS_caffe2_omp_num_threads);
}
VLOG(1) << "Caffe2 running with " << omp_get_max_threads() << " OMP threads";
return true;
}
REGISTER_CAFFE2_INIT_FUNCTION(Caffe2SetOpenMPThreads,
&Caffe2SetOpenMPThreads,
"Set OpenMP threads.");
#endif // _OPENMP
#ifdef CAFFE2_USE_MKL
bool Caffe2SetMKLThreads(int*, char***) {
if (!getenv("MKL_NUM_THREADS")) {
VLOG(1) << "MKL_NUM_THREADS not passed, defaulting to 1 thread";
mkl_set_num_threads(1);
}
// If caffe2_omp_num_threads is set, we use that for MKL as well.
if (FLAGS_caffe2_omp_num_threads > 0) {
VLOG(1) << "Setting mkl_num_threads to " << FLAGS_caffe2_omp_num_threads
<< " as inherited from omp_num_threads.";
mkl_set_num_threads(FLAGS_caffe2_omp_num_threads);
}
// Override omp_num_threads if mkl_num_threads is set.
if (FLAGS_caffe2_mkl_num_threads > 0) {
VLOG(1) << "Setting mkl_num_threads to " << FLAGS_caffe2_mkl_num_threads;
mkl_set_num_threads(FLAGS_caffe2_mkl_num_threads);
}
VLOG(1) << "Caffe2 running with " << mkl_get_max_threads() << " MKL threads";
return true;
}
REGISTER_CAFFE2_INIT_FUNCTION(
Caffe2SetMKLThreads,
&Caffe2SetMKLThreads,
"Set MKL threads.");
#endif // CAFFE2_USE_MKL
} // namespace caffe2