Skip to content

Commit

Permalink
Refactor OpenCL mode and update CI testing (#730)
Browse files Browse the repository at this point in the history
* Refactor OpenCL utilities and device creation.

* Update OpenCL default device type.

* Only run math function test for active modes.

* Update GitHub workflows.

* Remove OpenCL from math function tests.
  • Loading branch information
kris-rowe committed Dec 15, 2023
1 parent 5226290 commit 8957558
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 253 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
os: ubuntu-22.04
CC: icx
CXX: icpx
FC: ifx
CXXFLAGS: -Wno-uninitialized
OCCA_COVERAGE: 0
useCMake: true
Expand Down Expand Up @@ -124,14 +125,16 @@ jobs:
OCCA_CXX: ${{ matrix.CXX }}
run: |
source /opt/intel/oneapi/setvars.sh
export SYCL_ROOT=/opt/intel/oneapi/compiler/latest
cmake -S . -B build \
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
-DCMAKE_INSTALL_PREFIX=install \
-DCMAKE_C_COMPILER=${CC} \
-DCMAKE_CXX_COMPILER=${CXX} \
-DCMAKE_Fortran_COMPILER=${FC} \
-DENABLE_TESTS=ON \
-DENABLE_EXAMPLES=ON \
-DCMAKE_PREFIX_PATH="/opt/intel/oneapi/compiler/latest/linux;/opt/intel/oneapi/compiler/latest/linux/compiler"
-DENABLE_FORTRAN=ON
- name: CMake build
if: ${{ matrix.useCMake && !matrix.useoneAPI}}
Expand Down Expand Up @@ -179,7 +182,7 @@ jobs:
run: |
source /opt/intel/oneapi/setvars.sh
export ONEAPI_DEVICE_SELECTOR=*:cpu
ctest --test-dir build --progress --output-on-failure --parallel 8 --schedule-random -E "examples_cpp_arrays-opencl|examples_cpp_for_loops-opencl|examples_cpp_generic_inline_kernel-opencl|examples_cpp_shared_memory-opencl|examples_cpp_nonblocking_streams-opencl|examples_cpp_for_loops-dpcpp|examples_cpp_arrays-dpcpp|examples_cpp_generic_inline_kernel-dpcpp|examples_cpp_nonblocking_streams-dpcpp"
ctest --test-dir build --progress --output-on-failure --parallel 8 --schedule-random -E "opencl-*|examples_cpp_for_loops-dpcpp|examples_cpp_arrays-dpcpp|examples_cpp_generic_inline_kernel-dpcpp|examples_cpp_nonblocking_streams-dpcpp"
- name: Upload code coverage
if: ${{ matrix.OCCA_COVERAGE }}
Expand Down
38 changes: 11 additions & 27 deletions src/occa/internal/modes/opencl/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,10 @@

namespace occa {
namespace opencl {
device::device(const occa::json &properties_) :
occa::launchedModeDevice_t(properties_) {
device::device(const occa::json &properties_, cl_device_id clDevice_) :
occa::launchedModeDevice_t(properties_), clDevice(clDevice_) {

if (!properties.has("wrapped")) {
cl_int error;
OCCA_ERROR("[OpenCL] device not given a [platform_id] integer",
properties.has("platform_id") &&
properties["platform_id"].isNumber());

OCCA_ERROR("[OpenCL] device not given a [device_id] integer",
properties.has("device_id") &&
properties["device_id"].isNumber());

platformID = properties.get<int>("platform_id");
deviceID = properties.get<int>("device_id");

clDevice = opencl::deviceID(platformID, deviceID);

clContext = clCreateContext(NULL, 1, &clDevice, NULL, NULL, &error);
OCCA_OPENCL_ERROR("Device: Creating Context", error);
}
clContext = createContextFromDevice(clDevice);

occa::json &kernelProps = properties["kernel"];
std::string compilerFlags;
Expand Down Expand Up @@ -68,7 +51,7 @@ namespace occa {

kernelProps["compiler_flags"] = compilerFlags;

arch = deviceName(platformID, deviceID);
arch = opencl::deviceStrInfo(clDevice, CL_DEVICE_NAME);
}

device::~device() {
Expand All @@ -85,13 +68,14 @@ namespace occa {

hash_t device::hash() const {
if (!hash_.initialized) {
cl_platform_id platform_id = getPlatformFromDevice(clDevice);
std::stringstream ss;
ss << "platform name: " << opencl::platformName(platformID)
<< " platform vendor: " << opencl::platformVendor(platformID)
<< " platform version: " << opencl::platformVersion(platformID)
<< " device name: " << opencl::deviceName(platformID,deviceID)
<< " device vendor: " << opencl::deviceVendor(platformID,deviceID)
<< " device version: " << opencl::deviceVersion(platformID,deviceID);
ss << "platform name: " << opencl::platformName(platform_id)
<< " platform vendor: " << opencl::platformVendor(platform_id)
<< " platform version: " << opencl::platformVersion(platform_id)
<< " device name: " << opencl::deviceName(clDevice)
<< " device vendor: " << opencl::deviceVendor(clDevice)
<< " device version: " << opencl::deviceVersion(clDevice);
hash_ = occa::hash(ss.str());
}
return hash_;
Expand Down
5 changes: 1 addition & 4 deletions src/occa/internal/modes/opencl/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ namespace occa {
class info_t;

class device : public occa::launchedModeDevice_t {
friend cl_context getContext(occa::device device);

private:
mutable hash_t hash_;

public:
int platformID, deviceID;

cl_device_id clDevice;
cl_context clContext;

device(const occa::json &properties_);
device(const occa::json &properties_, cl_device_id clDevice_);
virtual ~device();

bool hasSeparateMemorySpace() const override;
Expand Down
11 changes: 6 additions & 5 deletions src/occa/internal/modes/opencl/polyfill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ namespace occa {
static cl_device_info CL_DEVICE_VERSION = 5;
static cl_device_info CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS = 6;
static cl_device_info CL_DEVICE_MAX_WORK_ITEM_SIZES = 7;
static cl_device_info CL_DEVICE_PLATFORM = 8;

static cl_device_type CL_DEVICE_TYPE_ACCELERATOR = 0;
static cl_device_type CL_DEVICE_TYPE_CPU = 1;
static cl_device_type CL_DEVICE_TYPE_GPU = 2;
static cl_device_type CL_DEVICE_TYPE_ALL = 3;
static cl_device_type CL_DEVICE_TYPE_DEFAULT = 4;
static constexpr cl_device_type CL_DEVICE_TYPE_ACCELERATOR = 0;
static constexpr cl_device_type CL_DEVICE_TYPE_CPU = 1;
static constexpr cl_device_type CL_DEVICE_TYPE_GPU = 2;
static constexpr cl_device_type CL_DEVICE_TYPE_ALL = 3;
static constexpr cl_device_type CL_DEVICE_TYPE_DEFAULT = 4;

static cl_kernel_work_group_info CL_KERNEL_WORK_GROUP_SIZE = 0;

Expand Down
81 changes: 56 additions & 25 deletions src/occa/internal/modes/opencl/registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,96 @@ namespace occa {
}

styling::section& openclMode::getDescription() {
static styling::section section("OpenCL");
static styling::section section(modeName);
if (section.size() == 0) {
int platformCount = getPlatformCount();
for (int platformId = 0; platformId < platformCount; ++platformId) {
std::string platform_name_str = platformName(platformId);
int platform_id{0};
auto platform_list = getPlatforms();
for (auto& p : platform_list) {
std::string platform_name_str = platformName(p);
section
.add("Platform " + toString(platformId), platform_name_str)
.add("Platform " + toString(platform_id), platform_name_str)
.addDivider();

int deviceCount = getDeviceCountInPlatform(platformId);
for (int deviceId = 0; deviceId < deviceCount; ++deviceId) {
std::string device_name_str = deviceName(platformId, deviceId);
info::device_type type = deviceType(platformId, deviceId);
int device_id{0};
auto device_list = getDevicesInPlatform(p);
for (auto& d : device_list) {
std::string device_name_str = deviceName(d);
cl_device_type device_type = deviceType(d);
std::string device_type_str;
switch (type) {
case info::device_type::cpu:
switch (device_type) {
case CL_DEVICE_TYPE_CPU:
device_type_str = "cpu";
break;
case info::device_type::gpu:
case CL_DEVICE_TYPE_GPU:
device_type_str = "gpu";
break;
case info::device_type::accelerator:
case CL_DEVICE_TYPE_ACCELERATOR:
device_type_str = "accelerator";
break;
case info::device_type::all:
device_type_str = "all!?";
case CL_DEVICE_TYPE_ALL:
device_type_str = "all";
break;
default:
device_type_str = "???";
break;
}

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

section
.add("Device " + toString(deviceId), device_name_str)
.add("Device " + toString(device_id), device_name_str)
.add("Device Type", device_type_str)
.add("Compute Cores", toString(compute_cores))
.add("Global Memory", global_memory_str)
.addDivider();

++device_id;
}
++platform_id;
}
// Remove last divider
section.groups.pop_back();
}
return section;
}

modeDevice_t* openclMode::newDevice(const occa::json &props) {
return new device(setModeProp(props));
modeDevice_t* openclMode::newDevice(const occa::json& properties) {
OCCA_ERROR("[OpenCL] device not given a [platform_id] integer",
properties.has("platform_id") &&
properties["platform_id"].isNumber());
int platform_id = properties.get<int>("platform_id");

auto platforms{getPlatforms()};
OCCA_ERROR("Invalid platform number (" + toString(platform_id) + ")",
(static_cast<size_t>(platform_id) < platforms.size()));
auto& platform = platforms[platform_id];

OCCA_ERROR("[OpenCL] device not given a [device_id] integer",
properties.has("device_id") &&
properties["device_id"].isNumber());
int device_id = properties.get<int>("device_id");

auto devices{getDevicesInPlatform(platform)};
OCCA_ERROR("Invalid device number (" + toString(device_id) + ")",
(static_cast<size_t>(device_id) < devices.size()));
auto& opencl_device = devices[device_id];

return new device(setModeProp(properties), opencl_device);
}

int openclMode::getDeviceCount(const occa::json &props) {
int openclMode::getDeviceCount(const occa::json& properties) {
OCCA_ERROR("[OpenCL] getDeviceCount not given a [platform_id] integer",
props.has("platform_id") &&
props["platform_id"].isNumber());
properties.has("platform_id") && properties["platform_id"].isNumber());
int platform_id = properties.get<int>("platform_id");

int platformId = props.get<int>("platform_id");
auto platforms{getPlatforms()};
OCCA_ERROR("Invalid platform number (" + toString(platform_id) + ")",
(static_cast<size_t>(platform_id) < platforms.size()));
auto& platform = platforms[platform_id];

return getDeviceCountInPlatform(platformId);
return getDeviceCountInPlatform(platform);
}

openclMode mode;
Expand Down
Loading

0 comments on commit 8957558

Please sign in to comment.