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

OpenCL and DPCPP kernel hashing #583

Merged
merged 6 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update OpenCL platform and device info queries.
  • Loading branch information
kris-rowe committed May 16, 2022
commit d4f73498616479e254d512e9c028417823711346
2 changes: 1 addition & 1 deletion src/occa/internal/modes/opencl/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ namespace occa {
}

udim_t device::memorySize() const {
return opencl::getDeviceMemorySize(clDevice);
return opencl::deviceGlobalMemSize(clDevice);
}
//==================================
}
Expand Down
37 changes: 30 additions & 7 deletions src/occa/internal/modes/opencl/registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,40 @@ namespace occa {
if (section.size() == 0) {
int platformCount = getPlatformCount();
for (int platformId = 0; platformId < platformCount; ++platformId) {
std::string platform_name_str = platformName(platformId);
section
.add("Platform " + toString(platformId), platform_name_str)
.addDivider();

int deviceCount = getDeviceCountInPlatform(platformId);
for (int deviceId = 0; deviceId < deviceCount; ++deviceId) {
udim_t bytes = getDeviceMemorySize(platformId, deviceId);
std::string bytesStr = stringifyBytes(bytes);
std::string device_name_str = deviceName(platformId, deviceId);
info::device_type type = deviceType(platformId, deviceId);
std::string device_type_str;
switch (type) {
case info::device_type::cpu:
device_type_str = "cpu";
break;
case info::device_type::gpu:
device_type_str = "gpu";
break;
case info::device_type::accelerator:
device_type_str = "accelerator";
break;
case info::device_type::all:
device_type_str = "all!?";
break;
}

int compute_cores = deviceCoreCount(platformId, deviceId);
udim_t global_memory_B = deviceGlobalMemSize(platformId, deviceId);
std::string global_memory_str = stringifyBytes(global_memory_B);

section
.add("Device Name" , deviceName(platformId, deviceId))
.add("Driver Vendor", info::vendor(deviceVendor(platformId, deviceId)))
.add("Platform ID" , toString(platformId))
.add("Device ID" , toString(deviceId))
.add("Memory" , bytesStr)
.add("Device " + toString(deviceId), device_name_str)
.add("Device Type", device_type_str)
.add("Compute Cores", toString(compute_cores))
.add("Global Memory", global_memory_str)
.addDivider();
}
}
Expand Down
178 changes: 107 additions & 71 deletions src/occa/internal/modes/opencl/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,6 @@ namespace occa {
clProgram(NULL),
clKernel(NULL) {}

namespace info {
std::string deviceType(int type) {
if (type & CPU) return "CPU";
if (type & GPU) return "GPU";
if (type & FPGA) return "FPGA";
if (type & XeonPhi) return "Xeon Phi";

return "N/A";
}

std::string vendor(int type) {
if (type & Intel) return "Intel";
if (type & AMD) return "AMD";
if (type & NVIDIA) return "NVIDIA";
if (type & Altera) return "Altera";

return "N/A";
}
}

bool isEnabled() {
cl_uint platformCount = 0;
cl_int error = clGetPlatformIDs(0, NULL, &platformCount);
Expand All @@ -51,17 +31,6 @@ namespace occa {
return false;
}

cl_device_type deviceType(int type) {
cl_device_type ret = 0;

if (type & info::CPU) ret |= CL_DEVICE_TYPE_CPU;
if (type & info::GPU) ret |= CL_DEVICE_TYPE_GPU;
if (type & info::FPGA) ret |= CL_DEVICE_TYPE_ACCELERATOR;
if (type & info::XeonPhi) ret |= CL_DEVICE_TYPE_ACCELERATOR;

return ret;
}

int getPlatformCount() {
cl_uint platformCount;

Expand All @@ -84,7 +53,89 @@ namespace occa {
return ret;
}

int getDeviceCount(int type) {
std::string platformStrInfo(cl_platform_id clPID,
cl_platform_info clInfo) {
size_t bytes;

OCCA_OPENCL_ERROR("OpenCL: Getting Platform String Info",
clGetPlatformInfo(clPID,
clInfo,
0, NULL, &bytes));

char *buffer = new char[bytes + 1];
buffer[bytes] = '\0';

OCCA_OPENCL_ERROR("OpenCL: Getting Platform String Info",
clGetPlatformInfo(clPID,
clInfo,
bytes, buffer, NULL));

std::string ret = buffer;

delete [] buffer;

size_t firstNS = ret.size();
size_t lastNS = ret.size();

size_t i;

for (i = 0; i < ret.size(); ++i) {
if ((ret[i] != ' ') &&
(ret[i] != '\t') &&
(ret[i] != '\n')) {
firstNS = i;
break;
}
}

if (i == ret.size()) {
return "";
}

for (i = (ret.size() - 1); i > firstNS; --i) {
if ((ret[i] != ' ') &&
(ret[i] != '\t') &&
(ret[i] != '\n')) {
lastNS = i;
break;
}
}

if (i == firstNS) {
return "";
}
return ret.substr(firstNS, (lastNS - firstNS + 1));
}

std::string platformName(int pID) {
cl_platform_id clPID = platformID(pID);
return platformStrInfo(clPID, CL_PLATFORM_NAME);
}

std::string platformVendor(int pID) {
cl_platform_id clPID = platformID(pID);
return platformStrInfo(clPID, CL_PLATFORM_VENDOR);
}

std::string platformVersion(int pID) {
cl_platform_id clPID = platformID(pID);
return platformStrInfo(clPID, CL_PLATFORM_VERSION);
}

cl_device_type deviceType(info::device_type type) {
switch (type) {
case info::device_type::cpu:
return CL_DEVICE_TYPE_CPU;
case info::device_type::gpu:
return CL_DEVICE_TYPE_GPU;
case info::device_type::accelerator:
return CL_DEVICE_TYPE_ACCELERATOR;
case info::device_type::all:
return CL_DEVICE_TYPE_ALL;
}
}

int getDeviceCount(info::device_type type) {
int pCount = opencl::getPlatformCount();
int ret = 0;

Expand All @@ -94,17 +145,16 @@ namespace occa {
return ret;
}

int getDeviceCountInPlatform(int pID, int type) {
int getDeviceCountInPlatform(int pID, info::device_type type) {
cl_platform_id clPID = platformID(pID);
cl_uint deviceCount = 0;

clGetDeviceIDs(clPID, deviceType(type),
0, NULL, &deviceCount);

return deviceCount;
}

cl_device_id deviceID(int pID, int dID, int type) {
cl_device_id deviceID(int pID, int dID, info::device_type type) {
cl_device_id *devices = new cl_device_id[dID + 1];

cl_platform_id clPID = platformID(pID);
Expand Down Expand Up @@ -180,47 +230,33 @@ namespace occa {
return deviceStrInfo(clDID, CL_DEVICE_NAME);
}

int deviceType(int pID, int dID) {
info::device_type deviceType(int pID, int dID) {
cl_device_id clDID = deviceID(pID, dID);
int ret = 0;

cl_device_type clDeviceType;

OCCA_OPENCL_ERROR("OpenCL: Get Device Type",
clGetDeviceInfo(clDID,
CL_DEVICE_TYPE,
sizeof(clDeviceType), &clDeviceType, NULL));
OCCA_OPENCL_ERROR(
"OpenCL: Get Device Type",
clGetDeviceInfo(clDID,CL_DEVICE_TYPE,sizeof(clDeviceType), &clDeviceType, NULL)
);

if (clDeviceType & CL_DEVICE_TYPE_CPU) {
ret |= info::CPU;
} else if (clDeviceType & CL_DEVICE_TYPE_GPU) {
ret |= info::GPU;
}
return ret;
if (clDeviceType & CL_DEVICE_TYPE_CPU)
return info::device_type::cpu;
if (clDeviceType & CL_DEVICE_TYPE_GPU)
return info::device_type::gpu;
if (clDeviceType & CL_DEVICE_TYPE_ACCELERATOR)
return info::device_type::accelerator;

return info::device_type::all;
}

int deviceVendor(int pID, int dID) {
std::string deviceVendor(int pID, int dID) {
cl_device_id clDID = deviceID(pID, dID);
int ret = 0;

std::string vendor = deviceStrInfo(clDID, CL_DEVICE_VENDOR);

if (vendor.find("AMD") != std::string::npos ||
vendor.find("Advanced Micro Devices") != std::string::npos ||
vendor.find("ATI") != std::string::npos) {

ret |= info::AMD;
} else if (vendor.find("Intel") != std::string::npos) {
ret |= info::Intel;
} else if (vendor.find("Altera") != std::string::npos) {
ret |= info::Altera;
} else if (vendor.find("Nvidia") != std::string::npos ||
vendor.find("NVIDIA") != std::string::npos) {

ret |= info::NVIDIA;
}
return deviceStrInfo(clDID, CL_DEVICE_VENDOR);
}

return ret;
std::string deviceVersion(int pID, int dID) {
cl_device_id clDID = deviceID(pID, dID);
return deviceStrInfo(clDID, CL_DEVICE_VERSION);
}

int deviceCoreCount(int pID, int dID) {
Expand All @@ -235,7 +271,7 @@ namespace occa {
return ret;
}

udim_t getDeviceMemorySize(cl_device_id dID) {
udim_t deviceGlobalMemSize(cl_device_id dID) {
cl_ulong ret;

OCCA_OPENCL_ERROR("OpenCL: Get Device Available Memory",
Expand All @@ -246,10 +282,10 @@ namespace occa {
return ret;
}

udim_t getDeviceMemorySize(int pID, int dID) {
udim_t deviceGlobalMemSize(int pID, int dID) {
cl_device_id clDID = deviceID(pID, dID);

return getDeviceMemorySize(clDID);
return deviceGlobalMemSize(clDID);
}

void buildProgramFromSource(info_t &info,
Expand Down
46 changes: 17 additions & 29 deletions src/occa/internal/modes/opencl/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,38 @@ namespace occa {
};

namespace info {
static const int CPU = (1 << 0);
static const int GPU = (1 << 1);
static const int FPGA = (1 << 3);
static const int XeonPhi = (1 << 2);
static const int anyType = (CPU | GPU | FPGA | XeonPhi);

static const int Intel = (1 << 4);
static const int AMD = (1 << 5);
static const int Altera = (1 << 6);
static const int NVIDIA = (1 << 7);
static const int anyVendor = (Intel | AMD | Altera | NVIDIA);

static const int any = (anyType | anyVendor);

std::string deviceType(int type);
std::string vendor(int type);
enum class device_type {
cpu, gpu, accelerator, all = cpu | gpu | accelerator
};
}

bool isEnabled();

cl_device_type deviceType(int type);

int getPlatformCount();

cl_platform_id platformID(int pID);

int getDeviceCount(int type = info::any);
int getDeviceCountInPlatform(int pID, int type = info::any);
std::string platformStrInfo(cl_platform_id clPID, cl_platform_info clInfo);
std::string platformName(int pID);
std::string platformVendor(int pID);
std::string platformVersion(int pID);

cl_device_id deviceID(int pID, int dID, int type = info::any);
int getDeviceCount(info::device_type deviceType = info::device_type::all);
int getDeviceCountInPlatform(int pID, info::device_type type = info::device_type::all);

std::string deviceStrInfo(cl_device_id clDID,
cl_device_info clInfo);
cl_device_id deviceID(int pID, int dID, info::device_type deviceType = info::device_type::all);

std::string deviceStrInfo(cl_device_id clDID, cl_device_info clInfo);
std::string deviceName(int pID, int dID);
std::string deviceVendor(int pID, int dID);
std::string deviceVersion(int pID, int dID);

int deviceType(int pID, int dID);

int deviceVendor(int pID, int dID);
cl_device_type deviceType(info::device_type type);
info::device_type deviceType(int pID, int dID);

int deviceCoreCount(int pID, int dID);

udim_t getDeviceMemorySize(cl_device_id dID);
udim_t getDeviceMemorySize(int pID, int dID);
udim_t deviceGlobalMemSize(cl_device_id dID);
udim_t deviceGlobalMemSize(int pID, int dID);

void buildProgramFromSource(info_t &info,
const std::string &source,
Expand Down