share unwrapping cl_mem of Interpreter for Zero-Copy
~/MNN/source/backend/opencl/core/OpenCLBackend.cpp cl::Buffer* buffer = mCLRuntime->mBufferPool->alloc(sizetypeSize); ((Tensor)nativeTensor)->buffer().device = (uint64_t)buffer;
~/MNN/source/backend/opencl/core/runtime/OpenCLWrapper.hpp #define CL_HPP_TARGET_OPENCL_VERSION 110 #define CL_HPP_MINIMUM_OPENCL_VERSION 110 #if !defined(_MSC_VER) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wignored-attributes" #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #include "CL/cl2.hpp" #pragma GCC diagnostic pop #else #include "CL/cl2.hpp" #endif #include "CL/cl_ext_qcom.h"
~/MNN/3rd_party/OpenCLHeaders/CL/cl2.hpp const cl_type& operator ()() const { return object_; } cl_type& operator ()() { return object_; } const cl_type get() const { return object_; } cl_type get() { return object_; }
MNN v3.6.1 cp -f ~/MNN/3rd_party/OpenCLHeaders/CL/cl_ext_qcom.h /opt/opencl/arm64/include/CL cp -f ~/MNN/3rd_party/OpenCLHeaders/CL/cl2.hpp /opt/opencl/arm64/include/CL
//source code #define CL_TARGET_OPENCL_VERSION 310 #define CL_HPP_TARGET_OPENCL_VERSION 110 #define CL_HPP_MINIMUM_OPENCL_VERSION 110 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wignored-attributes" #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #include "CL/cl.h" #include "CL/cl_ext_qcom.h" #include "CL/cl2.hpp" #pragma GCC diagnostic pop
size_t actual_size=0; cl_context ctx; cl_device_id oneDev; std::vector<cl_device_id> devIds; MNN::Interpreter* faceNet; MNN::ScheduleConfig sc{}; MNN::BackendConfig bc{};
faceNet = MNN::Interpreter::createFromFile("/volume1/slim-320.mnn"); sc.type = MNN_FORWARD_OPENCL; sc.mode = MNN_GPU_RECORD_BATCH | MNN_GPU_MEMORY_BUFFER | MNN_GPU_TUNING_NONE; bc.precision = MNN::BackendConfig::Precision_High; bc.power = MNN::BackendConfig::Power_High; sc.backendConfig= &bc;
faceNet->setCacheFile("/volume1/slim320.cache"); faceNet->setSessionMode(MNN::Interpreter::Session_Backend_Fix); faceSess= faceNet->createSession(sc); faceIn = faceNet->getSessionInput(faceSess, nullptr); faceNet->resizeTensor(faceIn, {1, 3, model_height, model_width}); faceNet->resizeSession(faceSess); faceNet->updateCacheFile(faceSess);
// First, execute runSession once to trigger the actual allocation of GPU memory within the MNN.
faceNet->runSession(faceSess);
cl_mem bOut = ((cl::Buffer*)faceIn->deviceId())->operator()(); clGetMemObjectInfo(bOut, CL_MEM_CONTEXT, sizeof(ctx), &ctx, nullptr); clGetContextInfo(ctx, CL_CONTEXT_DEVICES, 0, nullptr, &actual_size); devIds.resize(actual_size / sizeof(cl_device_id)); clGetContextInfo(ctx, CL_CONTEXT_DEVICES, actual_size, devIds.data(), nullptr); oneDev = devIds[0];
Source: alibaba/MNN