Browse Source

!5356 add reference count for opencl runtime

Merge pull request !5356 from liuchao/master
tags/v1.0.0
mindspore-ci-bot Gitee 5 years ago
parent
commit
c19905ce31
21 changed files with 61 additions and 5 deletions
  1. +5
    -0
      mindspore/lite/src/lite_session.cc
  2. +1
    -1
      mindspore/lite/src/runtime/opencl/opencl_allocator.cc
  3. +30
    -4
      mindspore/lite/src/runtime/opencl/opencl_runtime.cc
  4. +3
    -0
      mindspore/lite/src/runtime/opencl/opencl_runtime.h
  5. +2
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/activation_tests.cc
  6. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/arithmetic_tests.cc
  7. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/avg_pooling_tests.cc
  8. +2
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/batchnorm_tests.cc
  9. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/biasadd_tests.cc
  10. +2
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/concat_tests.cc
  11. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/conv2d_transpose_tests.cc
  12. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/convolution_tests.cc
  13. +2
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/depthwise_conv2d_tests.cc
  14. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/matmul_tests.cc
  15. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/max_pooling_tests.cc
  16. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/prelu_tests.cc
  17. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc
  18. +2
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/slice_tests.cc
  19. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/softmax_tests.cc
  20. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/to_format_tests.cc
  21. +1
    -0
      mindspore/lite/test/ut/src/runtime/kernel/opencl/transpose_tests.cc

+ 5
- 0
mindspore/lite/src/lite_session.cc View File

@@ -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;


+ 1
- 1
mindspore/lite/src/runtime/opencl/opencl_allocator.cc View File

@@ -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;


+ 30
- 4
mindspore/lite/src/runtime/opencl/opencl_runtime.cc View File

@@ -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_; }


+ 3
- 0
mindspore/lite/src/runtime/opencl/opencl_runtime.h View File

@@ -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};


+ 2
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/activation_tests.cc View File

@@ -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

+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/arithmetic_tests.cc View File

@@ -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 {


+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/avg_pooling_tests.cc View File

@@ -142,6 +142,7 @@ TEST_F(TestAvgPoolingOpenCL, AvgPoolFp32) {
delete pooling_kernel;
delete pGraph;
delete param;
lite::opencl::OpenCLRuntime::DeleteInstance();
}

} // namespace mindspore

+ 2
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/batchnorm_tests.cc View File

@@ -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

+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/biasadd_tests.cc View File

@@ -198,5 +198,6 @@ TEST_F(TestBiasAddOpenCL, BiasAddFp32_dim4) {
delete sub_graph;
delete param;
delete biasadd_kernel;
lite::opencl::OpenCLRuntime::DeleteInstance();
}
} // namespace mindspore

+ 2
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/concat_tests.cc View File

@@ -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

+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/conv2d_transpose_tests.cc View File

@@ -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) {


+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/convolution_tests.cc View File

@@ -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) {


+ 2
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/depthwise_conv2d_tests.cc View File

@@ -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

+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/matmul_tests.cc View File

@@ -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) {


+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/max_pooling_tests.cc View File

@@ -118,6 +118,7 @@ TEST_F(TestMaxPoolingOpenCL, MaxPool_1_32_512_96) {
}
delete pooling_kernel;
delete pGraph;
lite::opencl::OpenCLRuntime::DeleteInstance();
}

} // namespace mindspore

+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/prelu_tests.cc View File

@@ -183,5 +183,6 @@ TEST_F(TestPReluOpenCL, PReluFp32_dim4) {
delete param;
delete prelu_kernel;
delete sub_graph;
lite::opencl::OpenCLRuntime::DeleteInstance();
}
} // namespace mindspore

+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc View File

@@ -106,5 +106,6 @@ TEST_F(TestReshapeOpenCL, ReshapeFp32) {
outputs[0]->SetData(nullptr);

MS_LOG(INFO) << "Test ReshapeFp32 passed";
lite::opencl::OpenCLRuntime::DeleteInstance();
}
} // namespace mindspore

+ 2
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/slice_tests.cc View File

@@ -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

+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/softmax_tests.cc View File

@@ -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) {


+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/to_format_tests.cc View File

@@ -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

+ 1
- 0
mindspore/lite/test/ut/src/runtime/kernel/opencl/transpose_tests.cc View File

@@ -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…
Cancel
Save