Skip to content

Commit 0e5e49c

Browse files
authored
[BugFix][OPENCL] Fix initalization sequence of opencl backend valid API. test=develop (#4003) (#4021)
* fix opencl backend. test=develop
1 parent dd3150a commit 0e5e49c

File tree

6 files changed

+112
-24
lines changed

6 files changed

+112
-24
lines changed

lite/api/paddle_api.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,26 @@ namespace lite_api {
3131

3232
bool IsOpenCLBackendValid() {
3333
bool opencl_valid = false;
34+
3435
#ifdef LITE_WITH_OPENCL
36+
bool opencl_lib_found = paddle::lite::CLWrapper::Global()->OpenclLibFound();
37+
#ifdef LITE_WITH_LOG
38+
LOG(INFO) << "opencl_lib_found:" << opencl_lib_found;
39+
#endif
40+
if (opencl_lib_found == false) return false;
41+
42+
bool dlsym_success = paddle::lite::CLWrapper::Global()->DlsymSuccess();
43+
#ifdef LITE_WITH_LOG
44+
LOG(INFO) << "dlsym_success:" << dlsym_success;
45+
#endif
46+
if (dlsym_success == false) return false;
47+
3548
opencl_valid = paddle::lite::CLRuntime::Global()->OpenCLAvaliableForDevice();
3649
#endif
50+
51+
#ifdef LITE_WITH_LOG
3752
LOG(INFO) << "opencl_valid:" << opencl_valid;
53+
#endif
3854
return opencl_valid;
3955
}
4056

lite/backends/opencl/cl_context.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@ cl::Program &CLContext::GetProgram(const std::string &file_name,
3434
std::string program_key = program_key_ss.str();
3535
auto it = programs_.find(program_key);
3636
if (it != programs_.end()) {
37+
#ifdef LITE_WITH_LOG
3738
VLOG(3) << " --- program -> " << program_key << " has been built --- ";
39+
#endif
3840
return *(it->second);
3941
}
4042

4143
auto program = CLRuntime::Global()->CreateProgram(GetContext(), file_name);
42-
44+
#ifdef LITE_WITH_LOG
4345
VLOG(3) << " --- begin build program -> " << program_key << " --- ";
46+
#endif
4447
CLRuntime::Global()->BuildProgram(program.get(), options);
48+
#ifdef LITE_WITH_LOG
4549
VLOG(3) << " --- end build program -> " << program_key << " --- ";
50+
#endif
4651

4752
programs_[program_key] = std::move(program);
4853

@@ -54,22 +59,30 @@ void CLContext::AddKernel(const std::string &kernel_name,
5459
const std::string &options,
5560
const std::string &time_stamp) {
5661
cl_int status{CL_SUCCESS};
62+
#ifdef LITE_WITH_LOG
5763
VLOG(3) << " --- to get program " << file_name << " --- ";
64+
#endif
5865
auto program = GetProgram(file_name, options);
66+
#ifdef LITE_WITH_LOG
5967
VLOG(3) << " --- end get program --- ";
6068
VLOG(3) << " --- to create kernel: " << kernel_name << " --- ";
69+
#endif
6170
std::shared_ptr<cl::Kernel> kernel(
6271
new cl::Kernel(program, kernel_name.c_str(), &status));
6372
CL_CHECK_FATAL(status);
73+
#ifdef LITE_WITH_LOG
6474
VLOG(3) << " --- end create kernel --- ";
75+
#endif
6576
kernels_.emplace_back(std::move(kernel));
6677
STL::stringstream kernel_key;
6778
kernel_key << kernel_name << options << time_stamp;
6879
kernel_offset_[kernel_key.str()] = kernels_.size() - 1;
6980
}
7081

7182
cl::Kernel &CLContext::GetKernel(const int index) {
83+
#ifdef LITE_WITH_LOG
7284
VLOG(3) << " --- kernel count: " << kernels_.size() << " --- ";
85+
#endif
7386
CHECK(static_cast<size_t>(index) < kernels_.size())
7487
<< "The index must be less than the size of kernels.";
7588
CHECK(kernels_[index] != nullptr)

lite/backends/opencl/cl_runtime.cc

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ CLRuntime* CLRuntime::Global() {
2525
}
2626

2727
CLRuntime::~CLRuntime() {
28+
#ifdef LITE_WITH_LOG
29+
LOG(INFO) << "is_cl_runtime_initialized_:" << is_cl_runtime_initialized_;
30+
#endif
31+
if (is_cl_runtime_initialized_ == false) {
32+
return;
33+
}
34+
2835
if (command_queue_ != nullptr) {
2936
command_queue_->flush();
3037
command_queue_->finish();
@@ -38,18 +45,53 @@ CLRuntime::~CLRuntime() {
3845
}
3946

4047
bool CLRuntime::Init() {
48+
#ifdef LITE_WITH_LOG
49+
LOG(INFO) << "is_cl_runtime_initialized_:" << is_cl_runtime_initialized_;
50+
#endif
4151
if (is_cl_runtime_initialized_) {
4252
return true;
4353
}
54+
55+
bool opencl_lib_found = paddle::lite::CLWrapper::Global()->OpenclLibFound();
56+
#ifdef LITE_WITH_LOG
57+
LOG(INFO) << "opencl_lib_found:" << opencl_lib_found;
58+
#endif
59+
if (opencl_lib_found == false) {
60+
return false;
61+
}
62+
63+
bool dlsym_success = paddle::lite::CLWrapper::Global()->DlsymSuccess();
64+
#ifdef LITE_WITH_LOG
65+
LOG(INFO) << "dlsym_success:" << dlsym_success;
66+
#endif
67+
if (dlsym_success == false) {
68+
return false;
69+
}
70+
4471
bool is_platform_init = InitializePlatform();
45-
bool is_device_init = InitializeDevice();
72+
#ifdef LITE_WITH_LOG
4673
LOG(INFO) << "is_platform_init:" << is_platform_init;
74+
#endif
75+
if (is_platform_init == false) {
76+
return false;
77+
}
78+
79+
bool is_device_init = InitializeDevice();
80+
#ifdef LITE_WITH_LOG
4781
LOG(INFO) << "is_device_init:" << is_device_init;
82+
#endif
83+
if (is_device_init == false) {
84+
return false;
85+
}
86+
4887
if ((is_platform_init == true) && (is_device_init == true)) {
4988
is_platform_device_init_success_ = true;
5089
context_ = CreateContext();
5190
command_queue_ = CreateCommandQueue(context());
5291
is_cl_runtime_initialized_ = true;
92+
#ifdef LITE_WITH_LOG
93+
LOG(INFO) << "set is_cl_runtime_initialized_ = true";
94+
#endif
5395
}
5496
return is_cl_runtime_initialized_;
5597
}
@@ -138,20 +180,24 @@ GpuType CLRuntime::ParseGpuTypeFromDeviceName(std::string device_name) {
138180
const std::string kMALI_PATTERN_STR = "Mali";
139181
const std::string kADRENO_PATTERN_STR = "QUALCOMM Adreno(TM)";
140182
const std::string kPOWERVR_PATTERN_STR = "PowerVR";
183+
std::string gpu_type_str = "";
141184

142185
if (device_name == kADRENO_PATTERN_STR) {
143-
LOG(INFO) << "adreno gpu";
186+
gpu_type_str = "adreno gpu";
144187
return GpuType::QUALCOMM_ADRENO;
145188
} else if (device_name.find(kMALI_PATTERN_STR) != std::string::npos) {
146-
LOG(INFO) << "mali gpu";
189+
gpu_type_str = "mali gpu";
147190
return GpuType::ARM_MALI;
148191
} else if (device_name.find(kPOWERVR_PATTERN_STR) != std::string::npos) {
149-
LOG(INFO) << "powerVR gpu";
192+
gpu_type_str = "powerVR gpu";
150193
return GpuType::IMAGINATION_POWERVR;
151194
} else {
152-
LOG(INFO) << "others gpu";
195+
gpu_type_str = "others gpu";
153196
return GpuType::UNKNOWN;
154197
}
198+
#ifdef LITE_WITH_LOG
199+
LOG(INFO) << "gpu_type_str:" << gpu_type_str;
200+
#endif
155201
}
156202

157203
bool CLRuntime::InitializeDevice() {

lite/backends/opencl/cl_runtime.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,23 @@ class CLRuntime {
7070
static CLRuntime* Global();
7171

7272
bool OpenCLAvaliableForDevice() {
73-
bool opencl_lib_found = paddle::lite::CLWrapper::Global()->OpenclLibFound();
74-
LOG(INFO) << "opencl_lib_found:" << opencl_lib_found;
75-
if (opencl_lib_found == false) return false;
76-
77-
bool dlsym_success = paddle::lite::CLWrapper::Global()->DlsymSuccess();
78-
LOG(INFO) << "dlsym_success:" << dlsym_success;
79-
if (opencl_lib_found == false) return false;
73+
// note(ysh329): entered this func means:
74+
// 1. opencl_lib_found must be true
75+
// 2. dlsym_success must be true
8076

8177
InitializeDevice();
8278
bool support_fp16 =
8379
static_cast<bool>(device_info_["CL_DEVICE_EXTENSIONS_FP16"]);
80+
#ifdef LITE_WITH_LOG
8481
LOG(INFO) << "support_fp16:" << support_fp16;
82+
#endif
8583
if (support_fp16 == false) return false;
8684

87-
is_device_avaliable_for_opencl_ =
88-
dlsym_success && opencl_lib_found && support_fp16;
85+
is_device_avaliable_for_opencl_ = support_fp16;
86+
#ifdef LITE_WITH_LOG
8987
LOG(INFO) << "is_device_avaliable_for_opencl_:"
9088
<< is_device_avaliable_for_opencl_;
89+
#endif
9190
return is_device_avaliable_for_opencl_;
9291
}
9392

lite/core/context.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,18 +364,23 @@ class Context<TargetType::kX86> {
364364
#ifdef LITE_WITH_OPENCL
365365
template <>
366366
class Context<TargetType::kOpenCL> {
367-
std::shared_ptr<CLContext> cl_context_;
367+
std::shared_ptr<CLContext> cl_context_{nullptr};
368368

369369
public:
370370
CLContext* cl_context() { return cl_context_.get(); }
371371

372372
void InitOnce() {
373-
// Init cl runtime.
374-
CHECK(CLRuntime::Global()->IsInitSuccess()) << "OpenCL runtime init failed";
373+
if (CLRuntime::Global()->IsInitSuccess() == false) {
374+
LOG(ERROR) << "OpenCL runtime init failed";
375+
}
375376
cl_context_ = std::make_shared<CLContext>();
376377
}
377378

378-
void CopySharedTo(OpenCLContext* ctx) { ctx->cl_context_ = cl_context_; }
379+
void CopySharedTo(OpenCLContext* ctx) {
380+
if (ctx && cl_context_) {
381+
ctx->cl_context_ = cl_context_;
382+
}
383+
}
379384
};
380385
#endif
381386

lite/core/program.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,12 @@ RuntimeProgram::RuntimeProgram(
161161
int block_idx)
162162
: exec_scope_(exec_scope) {
163163
#ifdef LITE_WITH_OPENCL
164+
bool opencl_valid = CLRuntime::Global()->OpenCLAvaliableForDevice();
164165
using OpenCLContext = Context<TargetType::kOpenCL>;
165-
std::unique_ptr<KernelContext> local_ctx(new KernelContext());
166-
local_ctx->As<OpenCLContext>().InitOnce();
166+
std::unique_ptr<KernelContext> unique_opencl_ctx(new KernelContext());
167+
if (opencl_valid) {
168+
unique_opencl_ctx->As<OpenCLContext>().InitOnce();
169+
}
167170
#endif
168171
CHECK(program_desc);
169172
auto block_size = program_desc->BlocksSize();
@@ -229,9 +232,15 @@ RuntimeProgram::RuntimeProgram(
229232
}
230233
#ifdef LITE_WITH_OPENCL
231234
if (kernel->target() == TARGET(kOpenCL)) {
232-
std::unique_ptr<KernelContext> ctx(new KernelContext());
233-
(*local_ctx).As<OpenCLContext>().CopySharedTo(&ctx->As<OpenCLContext>());
234-
kernel->SetContext(std::move(ctx));
235+
if (opencl_valid) {
236+
std::unique_ptr<KernelContext> ctx(new KernelContext());
237+
(*unique_opencl_ctx)
238+
.As<OpenCLContext>()
239+
.CopySharedTo(&ctx->As<OpenCLContext>());
240+
kernel->SetContext(std::move(ctx));
241+
} else {
242+
LOG(ERROR) << "opencl_valid:" << opencl_valid;
243+
}
235244
} else {
236245
kernel->SetContext(
237246
ContextScheduler::Global().NewContext(kernel->target()));

0 commit comments

Comments
 (0)