Skip to content

Commit

Permalink
[RUNTIME][OPENCL] Bugfix for ciImage create with host ptr (#16768)
Browse files Browse the repository at this point in the history
Added couple more tests for host ptr data validation
  • Loading branch information
srkreddy1238 authored Mar 23, 2024
1 parent 21e1380 commit 77a7b01
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/runtime/opencl/opencl_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 39 additions & 1 deletion tests/cpp-runtime/opencl/opencl_nativeptr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 77a7b01

Please sign in to comment.