Caffe2 - C++ API
A deep learning, cross platform ML framework
init_omp.cc
1 #include <stdlib.h>
2 
3 #include "caffe2/core/common.h"
4 
5 #ifdef _OPENMP
6 #include "caffe2/core/common_omp.h"
7 #endif // _OPENMP
8 
9 #ifdef CAFFE2_USE_MKL
10 #include <mkl.h>
11 #endif // CAFFE2_USE_MKL
12 
13 #include "caffe2/core/init.h"
14 
15 C10_DEFINE_int(
16  caffe2_omp_num_threads,
17  0,
18  "The number of openmp threads. 0 to use default value. "
19  "Does not have effect if OpenMP is disabled.");
20 C10_DEFINE_int(
21  caffe2_mkl_num_threads,
22  0,
23  "The number of mkl threads. 0 to use default value. If set, "
24  "this overrides the caffe2_omp_num_threads flag if both are set. "
25  "Does not have effect if MKL is not used.");
26 
27 namespace caffe2 {
28 
29 #ifdef _OPENMP
30 bool Caffe2SetOpenMPThreads(int*, char***) {
31  if (!getenv("OMP_NUM_THREADS")) {
32  // OMP_NUM_THREADS not passed explicitly, so *disable* OMP by
33  // default. The user can use the CLI flag to override.
34  VLOG(1) << "OMP_NUM_THREADS not passed, defaulting to 1 thread";
35  omp_set_num_threads(1);
36  }
37 
38  if (FLAGS_caffe2_omp_num_threads > 0) {
39  VLOG(1) << "Setting omp_num_threads to " << FLAGS_caffe2_omp_num_threads;
40  omp_set_num_threads(FLAGS_caffe2_omp_num_threads);
41  }
42  VLOG(1) << "Caffe2 running with " << omp_get_max_threads() << " OMP threads";
43  return true;
44 }
45 REGISTER_CAFFE2_INIT_FUNCTION(Caffe2SetOpenMPThreads,
46  &Caffe2SetOpenMPThreads,
47  "Set OpenMP threads.");
48 #endif // _OPENMP
49 
50 #ifdef CAFFE2_USE_MKL
51 bool Caffe2SetMKLThreads(int*, char***) {
52  if (!getenv("MKL_NUM_THREADS")) {
53  VLOG(1) << "MKL_NUM_THREADS not passed, defaulting to 1 thread";
54  mkl_set_num_threads(1);
55  }
56 
57  // If caffe2_omp_num_threads is set, we use that for MKL as well.
58  if (FLAGS_caffe2_omp_num_threads > 0) {
59  VLOG(1) << "Setting mkl_num_threads to " << FLAGS_caffe2_omp_num_threads
60  << " as inherited from omp_num_threads.";
61  mkl_set_num_threads(FLAGS_caffe2_omp_num_threads);
62  }
63 
64  // Override omp_num_threads if mkl_num_threads is set.
65  if (FLAGS_caffe2_mkl_num_threads > 0) {
66  VLOG(1) << "Setting mkl_num_threads to " << FLAGS_caffe2_mkl_num_threads;
67  mkl_set_num_threads(FLAGS_caffe2_mkl_num_threads);
68  }
69  VLOG(1) << "Caffe2 running with " << mkl_get_max_threads() << " MKL threads";
70  return true;
71 }
72 REGISTER_CAFFE2_INIT_FUNCTION(
73  Caffe2SetMKLThreads,
74  &Caffe2SetMKLThreads,
75  "Set MKL threads.");
76 #endif // CAFFE2_USE_MKL
77 
78 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13