From a0713b7b8d80cb07083b8f0975089c50f2260a69 Mon Sep 17 00:00:00 2001 From: xiebaiyuan Date: Thu, 14 Mar 2019 21:59:48 +0800 Subject: [PATCH] add init check interface --- src/framework/cl/cl_engine.cpp | 23 ++++++++++++++--------- src/framework/cl/cl_engine.h | 22 +++++++++++++++++++++- src/framework/cl/cl_scope.h | 16 ++++++++-------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/framework/cl/cl_engine.cpp b/src/framework/cl/cl_engine.cpp index 04d1675227..c39ae00b00 100644 --- a/src/framework/cl/cl_engine.cpp +++ b/src/framework/cl/cl_engine.cpp @@ -27,9 +27,9 @@ bool CLEngine::Init() { return true; } cl_int status; - SetPlatform(); - SetClDeviceId(); - + bool is_setplatform_success = SetPlatform(); + bool is_setcldeviceid_success = SetClDeviceId(); + is_init_success_ = is_setplatform_success && is_setcldeviceid_success; initialized_ = true; return initialized_; // setClCommandQueue(); @@ -44,11 +44,14 @@ CLEngine *CLEngine::Instance() { return &cl_engine_; } +bool CLEngine::isInitSuccess() { return is_init_success_; } bool CLEngine::SetPlatform() { platform_ = NULL; // the chosen platform cl_uint numPlatforms; // the NO. of platforms cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms); - + if (status != CL_SUCCESS) { + return false; + } /**For clarity, choose the first available platform. */ if (numPlatforms > 0) { cl_platform_id *platforms = reinterpret_cast( @@ -56,10 +59,10 @@ bool CLEngine::SetPlatform() { status = clGetPlatformIDs(numPlatforms, platforms, NULL); platform_ = platforms[0]; free(platforms); - return true; - } else { - return false; + return status == CL_SUCCESS; } + + return false; } bool CLEngine::SetClDeviceId() { @@ -67,13 +70,15 @@ bool CLEngine::SetClDeviceId() { devices_ = NULL; cl_int status = clGetDeviceIDs(platform_, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices); - + if (status != CL_SUCCESS) { + return false; + } if (numDevices > 0) { devices_ = reinterpret_cast( malloc(numDevices * sizeof(cl_device_id))); status = clGetDeviceIDs(platform_, CL_DEVICE_TYPE_GPU, numDevices, devices_, NULL); - return true; + return status == CL_SUCCESS; } return false; } diff --git a/src/framework/cl/cl_engine.h b/src/framework/cl/cl_engine.h index d7b1c912da..566d3154f6 100644 --- a/src/framework/cl/cl_engine.h +++ b/src/framework/cl/cl_engine.h @@ -31,7 +31,7 @@ class CLEngine { static CLEngine *Instance(); bool Init(); - + bool isInitSuccess(); std::unique_ptr<_cl_context, CLContextDeleter> CreateContext() { cl_int status; cl_context c = clCreateContext(NULL, 1, devices_, NULL, NULL, &status); @@ -51,6 +51,20 @@ class CLEngine { return std::move(command_queue_ptr); } + cl_context getContext() { + if (context_ == nullptr) { + context_ = CreateContext(); + } + return context_.get(); + } + + cl_command_queue getClCommandQueue() { + if (command_queue_ == nullptr) { + command_queue_ = CreateClCommandQueue(getContext()); + } + return command_queue_.get(); + } + std::unique_ptr<_cl_program, CLProgramDeleter> CreateProgramWith( cl_context context, std::string file_name) { FILE *file = fopen(file_name.c_str(), "rb"); @@ -137,6 +151,11 @@ class CLEngine { std::string cl_path_; std::unique_ptr<_cl_program, CLProgramDeleter> program_; + std::unique_ptr<_cl_context, CLContextDeleter> context_ = nullptr; + + std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_ = + nullptr; + // bool SetClContext(); // bool SetClCommandQueue(); @@ -144,6 +163,7 @@ class CLEngine { // bool LoadKernelFromFile(const char *kernel_file); // bool BuildProgram(); + bool is_init_success_ = false; }; } // namespace framework diff --git a/src/framework/cl/cl_scope.h b/src/framework/cl/cl_scope.h index c7c06ca75f..e8f7d80036 100644 --- a/src/framework/cl/cl_scope.h +++ b/src/framework/cl/cl_scope.h @@ -29,12 +29,12 @@ namespace framework { class CLScope { public: CLScope() { - CLEngine *engin = CLEngine::Instance(); - context_ = engin->CreateContext(); - command_queue_ = engin->CreateClCommandQueue(context_.get()); + CLEngine *engine = CLEngine::Instance(); + context_ = engine->getContext(); + command_queue_ = engine->getClCommandQueue(); } - cl_command_queue CommandQueue() { return command_queue_.get(); } + cl_command_queue CommandQueue() { return command_queue_; } std::unique_ptr<_cl_kernel, CLKernelDeleter> GetKernel( const std::string &kernel_name, const std::string &file_name) { @@ -49,7 +49,7 @@ class CLScope { return std::move(kernel); } - cl_context Context() { return context_.get(); } + cl_context Context() { return context_; } cl_program Program(const std::string &file_name) { auto it = programs_.find(file_name); @@ -58,7 +58,7 @@ class CLScope { } auto program = CLEngine::Instance()->CreateProgramWith( - context_.get(), + context_, CLEngine::Instance()->GetCLPath() + "/cl_kernel/" + file_name); DLOG << " --- begin build program -> " << file_name << " --- "; @@ -72,8 +72,8 @@ class CLScope { private: cl_int status_; - std::unique_ptr<_cl_context, CLContextDeleter> context_; - std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_; + cl_context context_; + cl_command_queue command_queue_; std::unordered_map> programs_; -- GitLab