[neurun] Use host memory in acl_cl (#3974)
authorДилшоджон Умронхонович Пошшоев/AI Tools Lab /SRR/Engineer/삼성전자 <d.poshshoev@samsung.com>
Wed, 12 Dec 2018 10:10:56 +0000 (13:10 +0300)
committer이춘석/동작제어Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Wed, 12 Dec 2018 10:10:56 +0000 (19:10 +0900)
Related issue:3735
Add CLTensor::setBuffer to be able to use host memory from the acl_cl

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
runtimes/neurun/src/backend/acl_cl/operand/CLTensor.cc
runtimes/neurun/src/backend/acl_cl/operand/CLTensor.h

index 2a65d5a..6f798cb 100644 (file)
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#include <arm_compute/runtime/CL/CLScheduler.h>
+#include <arm_compute/runtime/CL/CLMemory.h>
+#include <arm_compute/runtime/CL/CLMemoryRegion.h>
 #include "CLTensor.h"
 
 #include "backend/acl_cl/Convert.h"
@@ -51,6 +54,23 @@ uint8_t *CLTensor::doMap(cl::CommandQueue &q, bool blocking)
 
 void CLTensor::doUnmap(cl::CommandQueue &q) { allocator()->unmap(q, buffer()); }
 
+void CLTensor::setBuffer(void *host_ptr)
+{
+  // create empty MemoryRegion: just context. Since flag isn't used here, no matter which flag to
+  // pass
+  auto memory = arm_compute::CLMemory(std::make_shared<arm_compute::CLBufferMemoryRegion>(
+      arm_compute::CLScheduler::get().context(), CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, 0));
+
+  // set buffer
+  auto mem = reinterpret_cast<cl::Buffer *>(memory.region()->handle());
+  *mem = cl::Buffer(arm_compute::CLScheduler::get().context(),
+                    CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, info()->total_size(), host_ptr);
+  // set correct buffer size
+  memory.region()->set_size(info()->total_size());
+  // import memory
+  allocator()->import_memory(memory);
+}
+
 } // namespace operand
 } // namespace acl_cl
 } // namespace backend
index 11a9e3f..31c96e2 100644 (file)
@@ -49,6 +49,7 @@ public:
   arm_compute::CLTensorAllocator *allocator();
   void map(bool blocking = true);
   void unmap();
+  void setBuffer(void *host_ptr);
 
 protected:
   uint8_t *doMap(cl::CommandQueue &q, bool blocking) override;