This is an automated email from the ASF dual-hosted git repository.
echuraev pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new 77a7b01081 [RUNTIME][OPENCL] Bugfix for ciImage create with host ptr
(#16768)
77a7b01081 is described below
commit 77a7b010817d2d8fbdf89223bb814e9c38f68365
Author: Siva <[email protected]>
AuthorDate: Sat Mar 23 20:23:36 2024 +0530
[RUNTIME][OPENCL] Bugfix for ciImage create with host ptr (#16768)
Added couple more tests for host ptr data validation
---
src/runtime/opencl/opencl_device_api.cc | 2 +-
tests/cpp-runtime/opencl/opencl_nativeptr.cc | 40 +++++++++++++++++++++++++++-
2 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/src/runtime/opencl/opencl_device_api.cc
b/src/runtime/opencl/opencl_device_api.cc
index 96ec8ed69f..ab553052bb 100644
--- a/src/runtime/opencl/opencl_device_api.cc
+++ b/src/runtime/opencl/opencl_device_api.cc
@@ -294,7 +294,7 @@ cl_mem OpenCLWorkspace::AllocTexture(Device dev, size_t
width, size_t height,
cl_channel_type cl_type = DTypeToOpenCLChannelType(type_hint);
cl_image_format format = {CL_RGBA, cl_type};
cl_image_desc descriptor = {CL_MEM_OBJECT_IMAGE2D, width, height, 0, 0, 0,
0, 0, 0};
- cl_mem mptr = clCreateImage(this->contexts[platform], CL_MEM_CREATE_FLAGS,
&format, &descriptor,
+ cl_mem mptr = clCreateImage(this->contexts[platform], CL_MEM_READ_WRITE,
&format, &descriptor,
nullptr, &err_code);
OPENCL_CHECK_ERROR(err_code);
return mptr;
diff --git a/tests/cpp-runtime/opencl/opencl_nativeptr.cc
b/tests/cpp-runtime/opencl/opencl_nativeptr.cc
index ebfb62e920..8f894c4bff 100644
--- a/tests/cpp-runtime/opencl/opencl_nativeptr.cc
+++ b/tests/cpp-runtime/opencl/opencl_nativeptr.cc
@@ -20,17 +20,55 @@
#include <gtest/gtest.h>
#include <tvm/runtime/container/optional.h>
+#include <cmath>
+#include <random>
+
#include "../src/runtime/opencl/opencl_common.h"
using namespace tvm::runtime;
using namespace tvm::runtime::cl;
#if defined(OPENCL_ENABLE_HOST_PTR)
-TEST(OpenCLNDArray, native_ptr) {
+TEST(OpenCLNativePtr, access_memory) {
OpenCLWorkspace* workspace = OpenCLWorkspace::Global();
auto A = tvm::runtime::NDArray::Empty({128, 128}, {kDLFloat, 32, 1},
{kDLOpenCL, 0});
void* nptr = workspace->GetNativePtr(A);
memset(nptr, 0x0, 128 * 128 * 4);
}
+
+TEST(OpenCLNatvePtr, data_loop) {
+ OpenCLWorkspace* workspace = OpenCLWorkspace::Global();
+
+ auto cl_arr = tvm::runtime::NDArray::Empty({1024}, {kDLFloat, 32, 1},
{kDLOpenCL, 0});
+ auto cpu_arr = tvm::runtime::NDArray::Empty({1024}, {kDLFloat, 32, 1},
{kDLCPU, 0});
+
+ std::random_device rdev;
+ std::mt19937 mt(rdev());
+ std::uniform_real_distribution<> random(-10.0, 10.0);
+
+ // Random initialize host ndarray
+ for (size_t i = 0; i < 1024; i++) {
+ static_cast<float*>(cpu_arr->data)[i] = random(mt);
+ }
+ // Do a roundtrip from cpu arr to opencl array and native ptr.
+ cpu_arr.CopyTo(cl_arr);
+ void* nptr = workspace->GetNativePtr(cl_arr);
+ for (size_t i = 0; i < 1024; ++i) {
+ ICHECK_LT(std::fabs(static_cast<float*>(cpu_arr->data)[i] -
static_cast<float*>(nptr)[i]),
+ 1e-5);
+ }
+
+ // Random initialize cl ndarray
+ for (size_t i = 0; i < 1024; i++) {
+ static_cast<float*>(nptr)[i] = random(mt);
+ }
+ // Do a roundtrip from native ptr to cl arr to cpu array.
+ cl_arr.CopyTo(cpu_arr);
+ for (size_t i = 0; i < 1024; ++i) {
+ ICHECK_LT(std::fabs(static_cast<float*>(cpu_arr->data)[i] -
static_cast<float*>(nptr)[i]),
+ 1e-5);
+ }
+}
+
#endif