Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add querying OpenCL device infomation #1324

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/codegen/build_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <tvm/ir_pass.h>
#include <tvm/codegen.h>

#include "../runtime/opencl/opencl_common.h"

#include <algorithm>
#include <mutex>
#include <stack>
Expand Down Expand Up @@ -70,15 +72,21 @@ Target CreateTarget(const std::string& target_name,
// For now assume rocm schedule for opencl
if (target_name == "opencl") {
t->device_type = kDLOpenCL;
auto workspace = runtime::cl::OpenCLWorkspace::Global();
cl::OpenCLThreadEntry* clt = cl::OpenCLThreadEntry::ThreadLocal();
TVMContext context = clt->context;
TVMRetValue rv;

workspace->GetAttr(context, kMaxThreadsPerBlock, &rv);
t->max_num_threads = rv.operator int();

workspace->GetAttr(context, kWarpSize, &rv);
t->thread_warp_size = rv.operator int();
} else {
t->device_type = kDLROCM;
}
t->keys_array.push_back(ir::StringImm::make("rocm"));
t->keys_array.push_back(ir::StringImm::make("gpu"));
t->max_num_threads = 256;
if (t->device_name == "intel_gpu") {
t->thread_warp_size = 16;
}
} else if (target_name == "metal" || target_name == "vulkan") {
if (target_name == "metal") {
t->device_type = kDLMetal;
Expand Down
35 changes: 29 additions & 6 deletions src/runtime/opencl/opencl_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <dmlc/thread_local.h>
#include "./opencl_common.h"

#include <string>

namespace tvm {
namespace runtime {
namespace cl {
Expand Down Expand Up @@ -39,12 +41,33 @@ void OpenCLWorkspace::GetAttr(
break;
}
case kWarpSize: {
/* TODO: the warp size of OpenCL device is not always 1
e.g. Intel GPU has a sub group concept which contains 8 - 32 work items,
corresponding to the number of SIMD entries the heardware configures.
We need to figure out a way to query this information from the hardware.
*/
*rv = 1;
static const std::string dummy_kernel("__kernel void tvm_dummy_kernel(__global int* a)"
"{a[get_global_id(0)] = 0;}");
cl_kernel kernel{nullptr};
cl_program program{nullptr};
size_t prefered_mul = 0;
size_t kernel_src_size = dummy_kernel.size();
const char* src = dummy_kernel.c_str();
cl_int err;

program = clCreateProgramWithSource(context, 1, &src, &kernel_src_size, &err);
OPENCL_CALL(err);
OPENCL_CALL(clBuildProgram(
program, 1,
&devices[index], "",
nullptr, nullptr));

kernel = clCreateKernel(program, "tvm_dummy_kernel", &err);
OPENCL_CALL(err);
OPENCL_CALL(clGetKernelWorkGroupInfo(
kernel, devices[index],
CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE,
sizeof(prefered_mul), &prefered_mul, nullptr));

OPENCL_CALL(clReleaseKernel(kernel));
OPENCL_CALL(clReleaseProgram(program));

*rv = (int)prefered_mul;
break;
}
case kMaxSharedMemoryPerBlock: {
Expand Down