Skip to content

Commit ba5c243

Browse files
[cherry-pick][OpenCL] Save kernel cache & tuned params for APP and multi-threads (#6542)
* [OpenCL] Save kernel cache & tuned params for APP and multi-threads (#6440)
1 parent 7a7458f commit ba5c243

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

lite/backends/opencl/cl_runtime.cc

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ namespace paddle {
2424
namespace lite {
2525

2626
CLRuntime* CLRuntime::Global() {
27-
static CLRuntime cl_runtime_;
27+
thread_local CLRuntime cl_runtime_;
2828
cl_runtime_.Init();
2929
return &cl_runtime_;
3030
}
3131

3232
CLRuntime::~CLRuntime() {
33-
SaveProgram();
34-
SaveTuned();
35-
3633
#ifdef LITE_WITH_LOG
3734
LOG(INFO) << "is_cl_runtime_initialized_:" << is_cl_runtime_initialized_;
3835
#endif
@@ -399,9 +396,7 @@ void CLRuntime::SaveTuned() {
399396
if (tuned_path_name_.empty() || auto_tune() == lite_api::CL_TUNE_NONE) return;
400397
std::string tuned_file =
401398
tuned_path_name_.at(0) + "/" + tuned_path_name_.at(1);
402-
if (tuned_file == "/") {
403-
LOG(INFO) << "invalid tuned_file:" << tuned_file;
404-
} else if (IsFileExists(tuned_file)) {
399+
if (IsFileExists(tuned_file)) {
405400
LOG(INFO) << "OpenCL Tuned file existed:" << tuned_file;
406401
} else {
407402
bool ret = Serialize(tuned_file, tuned_lwss_map_);
@@ -848,15 +843,14 @@ void CLRuntime::set_auto_tune(lite_api::CLTuneMode tune_mode,
848843
auto_tune_ = lite_api::CL_TUNE_NONE;
849844
}
850845
lws_repeats_ = lws_repeats;
851-
if (tuned_path_name_.empty()) {
852-
tuned_path_name_.push_back(path);
853-
tuned_path_name_.push_back(name);
854-
}
855-
const std::string tuned_file = path + "/" + name;
856-
LOG(INFO) << "tuned_file.size():" << tuned_file.size()
857-
<< ", tuned_file:" << tuned_file;
858-
if (tuned_file.size() > 2 && IsFileExists(tuned_file) &&
859-
auto_tune() != lite_api::CL_TUNE_NONE) {
846+
847+
tuned_path_name_.clear();
848+
tuned_path_name_.push_back(path);
849+
tuned_path_name_.push_back(name);
850+
const std::string tuned_file =
851+
tuned_path_name_.at(0) + "/" + tuned_path_name_.at(1);
852+
LOG(INFO) << "tuned_file:" << tuned_file;
853+
if (IsFileExists(tuned_file) && auto_tune() != lite_api::CL_TUNE_NONE) {
860854
LOG(INFO) << "Load tuned file: " << tuned_file;
861855
bool status = Deserialize(tuned_file, &tuned_lwss_map_);
862856
if (!status) {
@@ -865,7 +859,6 @@ void CLRuntime::set_auto_tune(lite_api::CLTuneMode tune_mode,
865859
} else {
866860
LOG(INFO) << "Not found tuned file:" << tuned_file;
867861
}
868-
command_queue_ = CreateCommandQueue(context());
869862
}
870863

871864
bool CLRuntime::HasTunedLocalWorkSizeMap(const std::string& key,

lite/backends/opencl/cl_runtime.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class CLRuntime {
140140
lite_api::CLPrecisionType get_precision() { return precision_; }
141141

142142
void SetBinaryPathName(const std::string& path, const std::string& name) {
143+
binary_path_name_.clear();
143144
binary_path_name_.push_back(path);
144145
binary_path_name_.push_back(name);
145146
}
@@ -213,7 +214,10 @@ class CLRuntime {
213214

214215
private:
215216
CLRuntime() { Init(); }
216-
217+
CLRuntime(const CLRuntime&) = delete;
218+
CLRuntime(const CLRuntime&&) = delete;
219+
CLRuntime& operator=(const CLRuntime&) = delete;
220+
CLRuntime& operator=(const CLRuntime&&) = delete;
217221
~CLRuntime();
218222

219223
bool InitializePlatform();

lite/core/program.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ class LITE_API RuntimeProgram {
206206
Scope* exec_scope,
207207
int block_idx = kRootBlockIdx);
208208
~RuntimeProgram() {
209+
#ifdef LITE_WITH_OPENCL
210+
// save program kernel cache & tuned params
211+
CLRuntime::Global()->SaveProgram();
212+
CLRuntime::Global()->SaveTuned();
213+
#endif // LITE_WITH_OPENCL
209214
#ifdef LITE_WITH_PROFILE
210215
LOG(INFO) << "\n" << profiler_.Summary(profile::Type::kCreate);
211216
LOG(INFO) << "\n" << profiler_.Summary(profile::Type::kDispatch);

0 commit comments

Comments
 (0)