From 5e100325fed836db5d1b81d426ea61555aa1b2da Mon Sep 17 00:00:00 2001 From: xiebaiyuan Date: Fri, 15 Mar 2019 12:09:12 +0800 Subject: [PATCH] add opencl outer interface by jiaying modify pridecttime judge --- src/io/opencl_interface.cpp | 33 +++++++++++++++++++++++++++ src/io/opencl_interface.h | 25 +++++++++++++++++++++ src/io/paddle_mobile.cpp | 45 ++++++++++++++++++------------------- 3 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 src/io/opencl_interface.cpp create mode 100644 src/io/opencl_interface.h diff --git a/src/io/opencl_interface.cpp b/src/io/opencl_interface.cpp new file mode 100644 index 0000000000..1a4b1f67cb --- /dev/null +++ b/src/io/opencl_interface.cpp @@ -0,0 +1,33 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include "io/opencl_interface.h" +#include "framework/cl/cl_engine.h" +#include "framework/cl/cl_scope.h" + +namespace paddle_mobile { + +cl_context getContext() { + return framework::CLEngine::Instance()->getContext(); +} + +cl_command_queue getClCommandQueue() { + return framework::CLEngine::Instance()->getClCommandQueue(); +} + +bool isInitSuccess() { + return framework::CLEngine::Instance()->isInitSuccess(); +} + +} // namespace paddle_mobile diff --git a/src/io/opencl_interface.h b/src/io/opencl_interface.h new file mode 100644 index 0000000000..3812abf00f --- /dev/null +++ b/src/io/opencl_interface.h @@ -0,0 +1,25 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#pragma once + +#include "CL/cl.h" + +namespace paddle_mobile { + +cl_context getContext(); +cl_command_queue getClCommandQueue(); +bool isInitSuccess(); + +} // namespace paddle_mobile diff --git a/src/io/paddle_mobile.cpp b/src/io/paddle_mobile.cpp index ceab4f4aee..412c5687b7 100644 --- a/src/io/paddle_mobile.cpp +++ b/src/io/paddle_mobile.cpp @@ -20,6 +20,8 @@ limitations under the License. */ #endif // _OPENMP #ifdef PADDLE_MOBILE_CL #include +#include +#include "framework/cl/cl_engine.h" #include "framework/cl/cl_tensor.h" #endif #include "operators/math/gemm.h" @@ -202,11 +204,15 @@ double PaddleMobile::GetPredictTime() { operators::math::Gemm gemm; auto time1 = paddle_mobile::time(); - gemm.Sgemm(m, n, k, static_cast(1), a, lda, b, ldb, - static_cast(0), c, ldc, false, - static_cast(nullptr)); + int times = 4; + for (int j = 0; j < times; ++j) { + gemm.Sgemm(m, n, k, static_cast(1), a, lda, b, ldb, + static_cast(0), c, ldc, false, + static_cast(nullptr)); + } + auto time2 = paddle_mobile::time(); - double cost = paddle_mobile::time_diff(time1, time2); + double cost = paddle_mobile::time_diff(time1, time2) / times; paddle_mobile::memory::Free(a); paddle_mobile::memory::Free(b); paddle_mobile::memory::Free(c); @@ -282,21 +288,11 @@ void PaddleMobile::SetCLPath(std::string path) { template <> double PaddleMobile::GetPredictTime() { cl_int status; - cl_uint nPlatform; - clGetPlatformIDs(0, NULL, &nPlatform); - cl_platform_id *listPlatform = reinterpret_cast( - malloc(nPlatform * sizeof(cl_platform_id))); - clGetPlatformIDs(nPlatform, listPlatform, NULL); - cl_uint nDevice = 0; - clGetDeviceIDs(listPlatform[0], CL_DEVICE_TYPE_GPU, 0, NULL, &nDevice); - cl_device_id *listDevice = - reinterpret_cast(malloc(nDevice * sizeof(cl_device_id))); - clGetDeviceIDs(listPlatform[0], CL_DEVICE_TYPE_GPU, nDevice, listDevice, - NULL); - cl_context context = - clCreateContext(NULL, nDevice, listDevice, NULL, NULL, &status); - cl_command_queue queue = - clCreateCommandQueue(context, listDevice[0], 0, &status); + if (!framework::CLEngine::Instance()->isInitSuccess()) { + return -1; + } + cl_context context = framework::CLEngine::Instance()->getContext(); + cl_command_queue queue = framework::CLEngine::Instance()->getClCommandQueue(); int n = 1; int c = 3; @@ -410,7 +406,7 @@ double PaddleMobile::GetPredictTime() { CL_CHECK_ERRORS(status); clFinish(queue); - queue = clCreateCommandQueue(context, listDevice[0], 0, &status); + // queue = clCreateCommandQueue(context, listDevice[0], 0, &status); path = framework::CLEngine::Instance()->GetCLPath() + "/cl_kernel/conv_kernel.cl"; @@ -465,15 +461,18 @@ double PaddleMobile::GetPredictTime() { // cl_event wait_event = param.Input()->GetClEvent(); size_t global_work_size2[3] = {8, 224, 224}; auto time1 = paddle_mobile::time(); - status = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, global_work_size2, - NULL, 0, NULL, NULL); + int times = 10; + for (int i = 0; i < times; ++i) { + status = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, global_work_size2, + NULL, 0, NULL, NULL); + } CL_CHECK_ERRORS(status); clFinish(queue); auto time2 = paddle_mobile::time(); paddle_mobile::memory::Free(input); paddle_mobile::memory::Free(filter); if (status == CL_SUCCESS) { - return paddle_mobile::time_diff(time1, time2); + return paddle_mobile::time_diff(time1, time2) / times; } else { return -1; } -- GitLab