Skip to content

Commit e21edb2

Browse files
committed
add Set/GetCPUNumThreads api
1 parent 3fe2def commit e21edb2

File tree

9 files changed

+18
-8
lines changed

9 files changed

+18
-8
lines changed

paddle/fluid/inference/api/analysis_config.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ contrib::AnalysisConfig::AnalysisConfig(const contrib::AnalysisConfig &other) {
4646
prog_file = other.prog_file;
4747
param_file = other.param_file;
4848
specify_input_name = other.specify_input_name;
49+
cpu_num_threads_ = other.cpu_num_threads_;
4950
// fields from this.
5051
enable_ir_optim = other.enable_ir_optim;
5152
use_feed_fetch_ops = other.use_feed_fetch_ops;

paddle/fluid/inference/api/analysis_predictor.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "paddle/fluid/platform/profiler.h"
3636

3737
DECLARE_bool(profile);
38-
DECLARE_int32(paddle_num_threads);
3938

4039
namespace paddle {
4140

@@ -67,7 +66,7 @@ bool AnalysisPredictor::Init(
6766
#endif
6867

6968
// no matter with or without MKLDNN
70-
paddle::platform::SetNumThreads(FLAGS_paddle_num_threads);
69+
paddle::platform::SetNumThreads(config_.GetCPUNumThreads());
7170

7271
if (!PrepareScope(parent_scope)) {
7372
return false;

paddle/fluid/inference/api/api_impl.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ limitations under the License. */
2828
#include "paddle/fluid/platform/profiler.h"
2929

3030
DEFINE_bool(profile, false, "Turn on profiler for fluid");
31-
DECLARE_int32(paddle_num_threads);
3231

3332
namespace paddle {
3433
namespace {
@@ -76,7 +75,7 @@ bool NativePaddlePredictor::Init(
7675
#endif
7776

7877
// no matter with or without MKLDNN
79-
paddle::platform::SetNumThreads(FLAGS_paddle_num_threads);
78+
paddle::platform::SetNumThreads(config_.GetCPUNumThreads());
8079

8180
if (config_.use_gpu) {
8281
place_ = paddle::platform::CUDAPlace(config_.device);

paddle/fluid/inference/api/paddle_api.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ struct NativeConfig : public PaddlePredictor::Config {
186186
// Specify the variable's name of each input if input tensors don't follow the
187187
// `feeds` and `fetches` of the phase `save_inference_model`.
188188
bool specify_input_name{false};
189+
190+
// Set and get the number of cpu threads.
191+
void SetCPUNumThreads(int cpu_num_threads) {
192+
cpu_num_threads_ = cpu_num_threads;
193+
}
194+
int GetCPUNumThreads() const { return cpu_num_threads_; }
195+
196+
protected:
197+
int cpu_num_threads_{1}; // number of cpu threads for each instance.
189198
};
190199

191200
// A factory to help create different predictors.

paddle/fluid/inference/tests/api/analyzer_resnet50_tester.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void SetConfig(AnalysisConfig *cfg) {
2727
cfg->device = 0;
2828
cfg->enable_ir_optim = true;
2929
cfg->specify_input_name = true;
30+
cfg->SetCPUNumThreads(FLAGS_paddle_num_threads);
3031
}
3132

3233
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {

paddle/fluid/inference/tests/api/config_printer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ std::ostream &operator<<(std::ostream &os, const NativeConfig &config) {
5353
os << GenSpaces(num_spaces) << "param_file: " << config.param_file << "\n";
5454
os << GenSpaces(num_spaces)
5555
<< "specify_input_name: " << config.specify_input_name << "\n";
56+
os << GenSpaces(num_spaces)
57+
<< "cpu_num_threads: " << config.GetCPUNumThreads() << "\n";
5658
num_spaces--;
5759
os << GenSpaces(num_spaces) << "}\n";
5860
return os;

paddle/fluid/inference/tests/api/tester_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ DEFINE_bool(use_analysis, true,
4242
"Running the inference program in analysis mode.");
4343

4444
DECLARE_bool(profile);
45+
DECLARE_int32(paddle_num_threads);
4546

4647
namespace paddle {
4748
namespace inference {

paddle/fluid/operators/math/fc_compute.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License. */
1717
#include "paddle/fluid/operators/math/blas.h"
1818
#include "paddle/fluid/operators/math/jit_kernel.h"
1919

20-
DECLARE_int32(paddle_num_threads);
21-
2220
namespace paddle {
2321
namespace operators {
2422
namespace math {
@@ -43,7 +41,7 @@ inline void FCCompute(const BlasT<DeviceContext, T>& blas, const int M,
4341
.template Get<jitkernel::VAddKernel<T>>(N);
4442

4543
#ifdef PADDLE_WITH_MKLML
46-
#pragma omp parallel for if (FLAGS_paddle_num_threads > 1)
44+
#pragma omp parallel for
4745
#endif
4846
for (int i = 0; i < M; i++) {
4947
T* dst = Y + i * N;

paddle/fluid/platform/cpu_helper.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void SetNumThreads(int num_threads) {
4141
#elif defined(PADDLE_WITH_MKLML)
4242
int real_num_threads = num_threads > 1 ? num_threads : 1;
4343
platform::dynload::MKL_Set_Num_Threads(real_num_threads);
44-
omp_set_num_threads(num_threads);
44+
omp_set_num_threads(real_num_threads);
4545
#else
4646
PADDLE_ENFORCE(false, "To be implemented.");
4747
#endif

0 commit comments

Comments
 (0)