forked from mindspore-Ecosystem/mindspore
!5356 add reference count for opencl runtime
Merge pull request !5356 from liuchao/master
This commit is contained in:
commit
c19905ce31
|
@ -326,6 +326,11 @@ LiteSession::~LiteSession() {
|
|||
}
|
||||
}
|
||||
input_vec_.clear();
|
||||
#if SUPPORT_GPU
|
||||
if (context_->device_ctx_.type == DT_GPU) {
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
#endif
|
||||
delete this->context_;
|
||||
delete this->executor;
|
||||
this->executor = nullptr;
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
namespace mindspore::lite::opencl {
|
||||
|
||||
OpenCLAllocator::OpenCLAllocator() {}
|
||||
OpenCLAllocator::~OpenCLAllocator() {}
|
||||
OpenCLAllocator::~OpenCLAllocator() { Clear(); }
|
||||
|
||||
void OpenCLAllocator::SetContext(const AllocatorContext &ctx) {
|
||||
lock_flag_ = ctx.lockFlag;
|
||||
|
|
|
@ -40,12 +40,29 @@ static std::mutex g_mtx;
|
|||
static std::mutex g_init_mtx;
|
||||
|
||||
bool OpenCLRuntime::init_done_ = false;
|
||||
OpenCLRuntime *OpenCLRuntime::ocl_runtime_instance_ = nullptr;
|
||||
size_t OpenCLRuntime::instance_count_ = 0;
|
||||
|
||||
OpenCLRuntime *OpenCLRuntime::GetInstance() {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
static OpenCLRuntime ocl_runtime;
|
||||
ocl_runtime.Init();
|
||||
return &ocl_runtime;
|
||||
if (instance_count_ == 0) {
|
||||
ocl_runtime_instance_ = &ocl_runtime;
|
||||
ocl_runtime_instance_->Init();
|
||||
}
|
||||
instance_count_++;
|
||||
return ocl_runtime_instance_;
|
||||
}
|
||||
|
||||
void OpenCLRuntime::DeleteInstance() {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
if (instance_count_ == 0) {
|
||||
MS_LOG(ERROR) << "No OpenCLRuntime instance could delete!";
|
||||
}
|
||||
instance_count_--;
|
||||
if (instance_count_ == 0) {
|
||||
ocl_runtime_instance_->Uninit();
|
||||
}
|
||||
}
|
||||
|
||||
OpenCLRuntime::OpenCLRuntime() { default_build_opts_ = " -cl-mad-enable -cl-fast-relaxed-math -Werror"; }
|
||||
|
@ -207,16 +224,25 @@ int OpenCLRuntime::Init() {
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
OpenCLRuntime::~OpenCLRuntime() {
|
||||
init_done_ = false;
|
||||
int OpenCLRuntime::Uninit() {
|
||||
program_map_.clear();
|
||||
delete allocator_;
|
||||
delete default_command_queue_;
|
||||
delete context_;
|
||||
delete device_;
|
||||
allocator_ = nullptr;
|
||||
default_command_queue_ = nullptr;
|
||||
context_ = nullptr;
|
||||
device_ = nullptr;
|
||||
#ifdef USE_OPENCL_WRAPPER
|
||||
OpenCLWrapper::GetInstance()->UnLoadOpenCLLibrary();
|
||||
#endif
|
||||
init_done_ = false;
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
OpenCLRuntime::~OpenCLRuntime() { Uninit(); }
|
||||
|
||||
cl::Context *OpenCLRuntime::Context() { return context_; }
|
||||
|
||||
cl::Device *OpenCLRuntime::Device() { return device_; }
|
||||
|
|
|
@ -47,6 +47,7 @@ class OpenCLRuntime {
|
|||
OpenCLRuntime &operator=(const OpenCLRuntime &) = delete;
|
||||
|
||||
int Init();
|
||||
int Uninit();
|
||||
|
||||
cl::Context *Context();
|
||||
cl::Device *Device();
|
||||
|
@ -143,6 +144,8 @@ class OpenCLRuntime {
|
|||
|
||||
private:
|
||||
static bool init_done_;
|
||||
static size_t instance_count_;
|
||||
static OpenCLRuntime *ocl_runtime_instance_;
|
||||
cl::CommandQueue *default_command_queue_{nullptr};
|
||||
cl::Context *context_{nullptr};
|
||||
cl::Device *device_{nullptr};
|
||||
|
|
|
@ -377,6 +377,7 @@ TEST_F(TestActivationOpenCL, SigmoidFp32_dim4) {
|
|||
delete input_tensor;
|
||||
delete output_tensor;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
TEST_F(TestActivationOpenCL, LeakyReluFp32_dim4) {
|
||||
|
@ -480,5 +481,6 @@ TEST_F(TestActivationOpenCL, LeakyReluFp32_dim4) {
|
|||
delete input_tensor;
|
||||
delete output_tensor;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -202,6 +202,7 @@ void TestCase(const std::vector<int> &shape_a, const std::vector<int> &shape_b)
|
|||
for (auto tensor : outputs) {
|
||||
delete tensor;
|
||||
}
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
class TestArithmeticOpenCL : public mindspore::CommonTest {
|
||||
|
|
|
@ -142,6 +142,7 @@ TEST_F(TestAvgPoolingOpenCL, AvgPoolFp32) {
|
|||
delete pooling_kernel;
|
||||
delete pGraph;
|
||||
delete param;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -158,6 +158,7 @@ TEST_F(TestBatchnormOpenCLfp16, Batchnormfp16input_dim4) {
|
|||
delete param;
|
||||
delete batchnorm_kernel;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
TEST_F(TestBatchnormOpenCLfp32, Batchnormfp32input_dim4) {
|
||||
MS_LOG(INFO) << "begin test";
|
||||
|
@ -277,5 +278,6 @@ TEST_F(TestBatchnormOpenCLfp32, Batchnormfp32input_dim4) {
|
|||
delete param;
|
||||
delete batchnorm_kernel;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -198,5 +198,6 @@ TEST_F(TestBiasAddOpenCL, BiasAddFp32_dim4) {
|
|||
delete sub_graph;
|
||||
delete param;
|
||||
delete biasadd_kernel;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -218,6 +218,7 @@ TEST_F(TestConcatOpenCLfp16, ConcatFp16_2input_dim4_axis3) {
|
|||
delete param;
|
||||
delete concat_kernel;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
TEST_F(TestConcatOpenCLfp32, ConcatFp32_2input_dim4_axis3) {
|
||||
|
@ -338,5 +339,6 @@ TEST_F(TestConcatOpenCLfp32, ConcatFp32_2input_dim4_axis3) {
|
|||
delete param;
|
||||
delete concat_kernel;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -134,6 +134,7 @@ void RunTestCaseConv2dTranspose(const std::vector<int> &shape, void *input_data,
|
|||
|
||||
inputs[0]->SetData(nullptr);
|
||||
outputs[0]->SetData(nullptr);
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
void RunTestCaseConv2dTranspose(const std::vector<int> shape, const std::vector<std::string> file_path, bool fp16) {
|
||||
|
|
|
@ -158,6 +158,7 @@ void TEST_MAIN(schema::Format input_format, schema::Format output_format, const
|
|||
bias_tensor.SetData(nullptr);
|
||||
delete param;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
TEST_F(TestConvolutionOpenCL, in1x224x224x3_out1x112x112x32_k33_s22_p0101_fp32) {
|
||||
|
|
|
@ -162,6 +162,7 @@ void DepthWiseTestMain(ConvParameter *conv_param, T2 *input_data, T1 *weight_dat
|
|||
inputs[1]->SetData(nullptr);
|
||||
inputs[2]->SetData(nullptr);
|
||||
delete[] packed_input;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -587,5 +588,6 @@ TEST_F(TestConvolutionDwOpenCL, ProfilingMobilenetv2Fp32) {
|
|||
}
|
||||
delete [] input_data;
|
||||
delete [] weight_data;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -111,6 +111,7 @@ void RunTestCaseMatMul(const std::vector<int> shape, const std::vector<std::stri
|
|||
tensor_x->SetData(nullptr);
|
||||
tensor_out->SetData(nullptr);
|
||||
MS_LOG(INFO) << "TestMatMulFp32 passed";
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
TEST_F(TestMatMulOpenCL, MatMulFp32) {
|
||||
|
|
|
@ -118,6 +118,7 @@ TEST_F(TestMaxPoolingOpenCL, MaxPool_1_32_512_96) {
|
|||
}
|
||||
delete pooling_kernel;
|
||||
delete pGraph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -183,5 +183,6 @@ TEST_F(TestPReluOpenCL, PReluFp32_dim4) {
|
|||
delete param;
|
||||
delete prelu_kernel;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -106,5 +106,6 @@ TEST_F(TestReshapeOpenCL, ReshapeFp32) {
|
|||
outputs[0]->SetData(nullptr);
|
||||
|
||||
MS_LOG(INFO) << "Test ReshapeFp32 passed";
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -148,6 +148,7 @@ TEST_F(TestSliceOpenCLfp32, Slicefp32input_dim4) {
|
|||
}
|
||||
delete slice_kernel;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
TEST_F(TestSliceOpenCLfp16, Slicefp16input_dim4) {
|
||||
MS_LOG(INFO) << "begin test";
|
||||
|
@ -258,5 +259,6 @@ TEST_F(TestSliceOpenCLfp16, Slicefp16input_dim4) {
|
|||
}
|
||||
delete slice_kernel;
|
||||
delete sub_graph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -92,6 +92,7 @@ void RunTestCase(std::vector<int> input_shape, std::vector<int> output_shape, st
|
|||
}
|
||||
delete kernel;
|
||||
delete pGraph;
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
|
||||
TEST_F(TestSoftmaxOpenCL, Softmax_1) {
|
||||
|
|
|
@ -103,5 +103,6 @@ TEST_F(TestToFormatOpenCL, ToFormatNHWC2NCHW) {
|
|||
// compare
|
||||
CompareOutputData(output_data, correct_data, h * w * c, 0.00001);
|
||||
MS_LOG(INFO) << "Test TransposeFp32 passed";
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -108,5 +108,6 @@ TEST_F(TestTransposeOpenCL, TransposeFp32) {
|
|||
outputs[0]->SetData(nullptr);
|
||||
|
||||
MS_LOG(INFO) << "Test TransposeFp32 passed";
|
||||
lite::opencl::OpenCLRuntime::DeleteInstance();
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
Loading…
Reference in New Issue