Skip to content

Commit

Permalink
Add sycl opencl support (#270)
Browse files Browse the repository at this point in the history
* remove opencl spv kernels blob header

* update tests: avoid running kernels with dynamic allocation

* add noassert configuration

* add env to select default sycl context by name

* add sycl-opencl ci

* try to fix build

* try to fix build

* try to fix build

* try to fix build
  • Loading branch information
alifahrri committed Mar 24, 2024
1 parent 08e3733 commit 74fbb6e
Show file tree
Hide file tree
Showing 49 changed files with 645 additions and 92,739 deletions.
11 changes: 7 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@
// "dockerFile": "../docker/sycl.dockerfile",

// DOCKERFILE: cuda-sycl
"name": "sycl-clang14 ubuntu22.04 with cuda toolchain",
"dockerFile": "../docker/cuda-sycl.dockerfile",
"name": "devcontainer",
"dockerFile": "../docker/sycl.dockerfile",

"build": {
"args": { "USERNAME": "${localEnv:USER}" },
"args": {
"USERNAME": "${localEnv:USER}",
"BASE": "nvidia/cuda:11.8.0-devel-ubuntu20.04"
},
"target": "dev"
},

Expand All @@ -63,7 +66,7 @@

// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined", "--env=DISPLAY", "--env=QT_X11_NO_MITSHM=1", "--network=host" ],
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined", "--env=DISPLAY", "--env=QT_X11_NO_MITSHM=1", "--network=host", "--runtime=nvidia" ],
"runArgs": [ "--privileged=true", "--security-opt", "seccomp=unconfined", "--env=DISPLAY", "--env=QT_X11_NO_MITSHM=1", "--network=host", "--gpus=all", "--device=/dev/dri:/dev/dri"],

// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
"mounts": [ "source=/tmp/.X11-unix,target=/tmp/.X11-unix,type=bind" ],
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/sycl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ jobs:
- name: run tests
run: |
docker run --rm nmtools:sycl-clang14-omp
hipsycl-clang14-opencl-docker:
name: hipsycl-clang14-opencl-docker
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: build docker
run: |
docker build . --tag nmtools:sycl-clang14-opencl --build-arg opencl_backend=ON --build-arg toolchain=sycl-clang14-generic --file docker/sycl.dockerfile
- name: run tests
run: |
docker run -e NMTOOLS_SYCL_DEFAULT_PLATFORM=opencl --rm nmtools:sycl-clang14-opencl
hipsycl-clang14-cuda-docker:
name: hipsycl-clang14-cuda-docker
runs-on: ubuntu-20.04
Expand Down
20 changes: 20 additions & 0 deletions cmake/toolchains/sycl-clang14-generic.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
set(CMAKE_C_COMPILER /usr/local/bin/syclcc)
set(CMAKE_CXX_COMPILER /usr/local/bin/syclcc)

set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)

add_compile_options(-W -Wall -Wextra -Werror
-Wno-gnu-string-literal-operator-template
-Wno-unknown-cuda-version
-fopenmp
--acpp-targets="generic"
--acpp-clang=/usr/bin/clang++-14
)
add_link_options(
--acpp-targets="generic"
--acpp-clang=/usr/bin/clang++-14
)
# To avoid linking device code with assert
add_compile_definitions(NMTOOLS_NOASSERT NMTOOLS_DISABLE_STL)
link_libraries(rt pthread)
2 changes: 1 addition & 1 deletion cmake/toolchains/sycl-clang14-omp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ set(CMAKE_C_COMPILER /usr/local/bin/syclcc)
set(CMAKE_CXX_COMPILER /usr/local/bin/syclcc)

SET (CMAKE_C_COMPILER_WORKS 1)
add_compile_options(-W -Wall -Werror -Wextra -Wno-gnu-string-literal-operator-template
add_compile_options(-W -Wall -Werror -Wextra -Wno-gnu-string-literal-operator-template -fopenmp
--acpp-targets=omp
--acpp-clang=/usr/bin/clang++-14
)
Expand Down
10 changes: 10 additions & 0 deletions docker/sycl.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ ENV LEVEL_ZERO_BACKEND=${level_zero_backend}
ARG toolchain=sycl-clang14-omp
ENV TOOLCHAIN=${toolchain}

# for llvm-spirv & pocl
ENV LLVM_VERSION="14"
ENV LLVM_SPV_VERSION="v14.0.0"
ENV POCL_VERSION="v5.0"

# TODO: make the following installation conditional on selected backend
RUN bash scripts/install_llvm.sh
RUN bash scripts/install_llvm_spirv.sh
RUN bash scripts/install_pocl.sh

RUN bash scripts/install_opensycl.sh

RUN mkdir -p build/${toolchain} && cd build/${toolchain} \
Expand Down
92,604 changes: 0 additions & 92,604 deletions include/nmtools/array/eval/opencl/kernels_spv.hpp

This file was deleted.

15 changes: 15 additions & 0 deletions include/nmtools/array/eval/sycl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,16 @@ namespace nmtools::array::sycl
if (!default_context) {
auto sycl_devices = ::sycl::device::get_devices();
auto platform_idx = 0ul;
auto platform_name = std::string();
if (auto env_idx = std::getenv("NMTOOLS_SYCL_DEFAULT_PLATFORM_IDX")) {
platform_idx = std::stoi(env_idx);
}
if (auto env_name = std::getenv("NMTOOLS_SYCL_DEFAULT_PLATFORM")) {
platform_name = env_name;
std::transform(platform_name.begin(), platform_name.end(), platform_name.begin(),
[](unsigned char c){ return std::tolower(c); }
);
}
// TODO: better logging utilities
std::cout << "\033[1;33m[nmtools sycl]\033[0m number of sycl devices: " << sycl_devices.size() << "\n";
for (auto i=0ul; i<sycl_devices.size(); i++) {
Expand All @@ -366,6 +373,14 @@ namespace nmtools::array::sycl
PRINT_PLATFORM_PROPERTY(platform, version);
PRINT_PLATFORM_PROPERTY(platform, profile);
PRINT_PLATFORM_PROPERTY(platform, extensions);

auto name = platform.get_info<::sycl::info::platform::name>();
std::transform(name.begin(), name.end(), name.begin(),
[](unsigned char c){ return std::tolower(c); }
);
if (name == platform_name) {
platform_idx = i;
}
}
auto selected_device = sycl_devices.at(platform_idx);
std::cout << "\033[1;33m[nmtools sycl]\033[0m default context using platform #" << platform_idx << "\n";
Expand Down
4 changes: 3 additions & 1 deletion include/nmtools/assert.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef NMTOOLS_ASSERT_HPP
#define NMTOOLS_ASSERT_HPP

#ifdef ARDUINO
#if defined(NMTOOLS_NOASSERT)
#include "nmtools/platform/assert/noassert.hpp"
#elif defined(ARDUINO)
#include "nmtools/platform/assert/arduino.hpp"
#elif defined(__CUDA__)
#include "nmtools/platform/assert/cuda.hpp"
Expand Down
29 changes: 29 additions & 0 deletions include/nmtools/platform/assert/noassert.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef NMTOOLS_PLATFORM_ASSERT_NOASSERT_HPP
#define NMTOOLS_PLATFORM_ASSERT_NOASSERT_HPP

#define nmtools_assert_optional(condition, message, return_type, ...) \
if (!(condition)) return return_type{nmtools::meta::Nothing};

#define nmtools_make_optional(name, type) \
using name [[maybe_unused]] = nmtools_maybe<type>;

#define nmtools_cassert(condition, ...) {}
#define nmtools_assert_throw(condition, ...) {}

#ifdef NMTOOLS_USE_OPTIONAL
#undef nmtools_assert
#define nmtools_assert nmtools_assert_optional
#endif // NMTOOLS_USE_OPTIONAL

#ifndef nmtools_assert
#define nmtools_assert nmtools_cassert
#endif // nmtools_assert

#ifdef NMTOOLS_USE_OPTIONAL
#define nmtools_assert_prepare_type nmtools_make_optional
#else
#define nmtools_assert_prepare_type(name, type) \
using name = type;
#endif // NMTOOLS_USE_OPTIONAL

#endif // NMTOOLS_PLATFORM_ASSERT_NOASSERT_HPP
4 changes: 3 additions & 1 deletion include/nmtools/testing/testing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ auto var_name = nmtools::testing::cast<T>(var<value_type_##var_name>);
#define STRINGIFY(array) \
nmtools::utils::to_string(array)

#define NMTOOLS_TESTING_OUTPUT_PRECISION 1e-6
#ifndef NMTOOLS_TESTING_OUTPUT_PRECISION
#define NMTOOLS_TESTING_OUTPUT_PRECISION (1e-6)
#endif

/**
* @brief implementation of typeinfo logging for doctest,
Expand Down
24 changes: 24 additions & 0 deletions scripts/install_igc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/bash

IGC_CORE_URL=https://github.com/intel/intel-graphics-compiler/releases/download/igc-1.0.15985.7/intel-igc-core_1.0.15985.7_amd64.deb
IGC_URL=https://github.com/intel/intel-graphics-compiler/releases/download/igc-1.0.15985.7/intel-igc-opencl_1.0.15985.7_amd64.deb
IGC_FILENAME=intel-igc-opencl_1.0.15985.7_amd64.deb
IGC_CORE_FILENAME=intel-igc-core_1.0.15985.7_amd64.deb

cd /opt/

echo "downloading package.."
if [[ -f "${IGC_FILENAME}" ]]; then
echo "file ${IGC_FILENAME} exists, skip downloading"
else
wget ${IGC_URL}
fi
echo "downloading package.."
if [[ -f "${IGC_CORE_FILENAME}" ]]; then
echo "file ${IGC_CORE_FILENAME} exists, skip downloading"
else
wget ${IGC_CORE_URL}
fi

echo "installing package.."
dpkg -i ${IGC_CORE_FILENAME} ${IGC_FILENAME}
22 changes: 22 additions & 0 deletions scripts/install_level_zero.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/bash

DIR=level-zero
GIT_REPOSITORY=https://github.com/oneapi-src/level-zero.git
VERSION=v1.16.9

if [[ -d "$DIR" ]]; then
echo "${DIR} exists. skipping cloning"
else
git clone --single-branch -b ${VERSION} ${GIT_REPOSITORY}
fi

cd ${DIR}

mkdir -p build

cd build

cmake ..
cmake --build . --config Release
cmake --build . --config Release --target package
cmake --build . --config Release --target install
4 changes: 2 additions & 2 deletions scripts/install_llvm.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

if [[ -z "${LLVM_VERSION}" ]]; then
LLVM_VERSION="15"
LLVM_VERSION="14"
else
LLVM_VERSION="${LLVM_VERSION}"
echo "set LLVM_VERSION from env: ${LLVM_VERSION}"
Expand All @@ -13,4 +13,4 @@ if [[ -f "llvm.sh" ]]; then
fi
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh ${LLVM_VERSION}
./llvm.sh ${LLVM_VERSION}
11 changes: 11 additions & 0 deletions scripts/install_opensycl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ echo "using LLVM_VERSION=${LLVM_VERSION}"
echo "using ACCELERATED_CPU=${ACCELERATED_CPU}"
echo "using OPENCL_BACKEND=${OPENCL_BACKEND}"
echo "using CUDA_BACKEND=${CUDA_BACKEND}"
echo "using LEVEL_ZERO_BACKEND=${LEVEL_ZERO_BACKEND}"

DIR=OpenSYCL
GIT_REPOSITORY=https://github.com/OpenSYCL/OpenSYCL
Expand All @@ -60,6 +61,16 @@ else
git clone --single-branch -b ${VERSION} ${GIT_REPOSITORY}
fi

apt install -y python3-dev libpython3-dev \
build-essential cmake git pkg-config \
make ninja-build apt-utils \
intel-opencl-icd clinfo \
libboost-context-dev libboost-fiber-dev \
libomp-dev libomp-${LLVM_VERSION}-dev libgomp1 \
libclang-${LLVM_VERSION}-dev clang-${LLVM_VERSION} \
llvm-${LLVM_VERSION} libclang-cpp${LLVM_VERSION}-dev libclang-cpp${LLVM_VERSION} \
llvm-${LLVM_VERSION}-dev --fix-missing

cd ${DIR}

mkdir -p build
Expand Down
25 changes: 15 additions & 10 deletions scripts/install_pocl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ else
fi

if [[ -z "${POCL_VERSION}" ]]; then
POCL_VERSION="v3.1"
POCL_VERSION="v5.0"
else
POCL_VERSION="${POCL_VERSION}"
echo "set POCL_VERSION from env"
Expand All @@ -20,32 +20,37 @@ else
echo "set POCL_BUILD_TYPE from env"
fi

LLVM_SPV_PATH=/usr/local/bin/llvm-spirv

echo "using LLVM_VERSION=${LLVM_VERSION}"

echo "using POCL_VERSION=${POCL_VERSION}"

echo "using POCL_BUILD_TYPE=${POCL_BUILD_TYPE}"

echo "using LLVM_SPV_PATH=${LLVM_SPV_PATH}"

apt install -y python3-dev libpython3-dev build-essential ocl-icd-libopencl1 \
cmake git pkg-config libclang-${LLVM_VERSION}-dev clang-${LLVM_VERSION} \
llvm-${LLVM_VERSION} make ninja-build ocl-icd-libopencl1 ocl-icd-dev \
ocl-icd-opencl-dev libhwloc-dev zlib1g zlib1g-dev clinfo dialog apt-utils \
libxml2-dev libclang-cpp${LLVM_VERSION}-dev libclang-cpp${LLVM_VERSION} \
llvm-${LLVM_VERSION}-dev

DIR=pocl
GIT_REPOSITORY=https://github.com/pocl/pocl.git
if [[ -d "$DIR" ]]; then
echo "$DIR exists. skipping cloning"
else
git clone https://github.com/pocl/pocl.git
git clone ${GIT_REPOSITORY}
fi

apt install -y xxd python3-dev libpython3-dev build-essential ocl-icd-libopencl1 intel-opencl-icd \
cmake git pkg-config libclang-${LLVM_VERSION}-dev clang \
llvm-${LLVM_VERSION} make ninja-build ocl-icd-libopencl1 ocl-icd-dev \
ocl-icd-opencl-dev libhwloc-dev zlib1g zlib1g-dev clinfo dialog apt-utils \
libxml2-dev libclang-cpp${LLVM_VERSION}-dev libclang-cpp${LLVM_VERSION} \
llvm-${LLVM_VERSION}-dev --fix-missing

cd ${DIR}
git fetch && git checkout ${POCL_VERSION}
mkdir -p build && cd build \
&& cmake -DCMAKE_BUILD_TYPE=${POCL_BUILD_TYPE} \
-DENABLE_TESTS=OFF \
-DENABLE_CUDA=OFF \
-DSPIRV=ON -DLLVM_SPIRV=/usr/local/bin/llvm-spirv \
-DSPIRV=ON -DLLVM_SPIRV=${LLVM_SPV_PATH} \
-DCMAKE_INSTALL_PREFIX=/usr .. \
&& make -j2 && make install
6 changes: 6 additions & 0 deletions tests/sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()

find_package(OpenMP)

enable_testing()

option(NMTOOLS_SYCL_TEST_ALL "test all sycl modules" ON)
Expand Down Expand Up @@ -118,6 +120,10 @@ if (doctest_FOUND)
target_link_libraries(${PROJECT_NAME}-doctest PRIVATE doctest::doctest)
endif ()

if(OpenMP_CXX_FOUND)
target_link_libraries(${PROJECT_NAME}-doctest PRIVATE OpenMP::OpenMP_CXX)
endif()

target_compile_definitions(${PROJECT_NAME}-doctest PRIVATE
NMTOOLS_ISCLOSE_NAN_HANDLING=1
NMTOOLS_ISCLOSE_INF_HANDLING=1
Expand Down
5 changes: 4 additions & 1 deletion tests/sycl/array/accumulations/add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ SUBCASE(#case_name) \
NMTOOLS_ASSERT_CLOSE( result, expect ); \
}

TEST_CASE("accumulate_add(case1)" * doctest::test_suite("array::add.accumulate"))
// TODO: fix sycl kernel jit compile error:
// InvalidBitWidth: Invalid bit width in input: 48
// LLVMToSpirv: llvm-spirv invocation failed with exit code 8
TEST_CASE("accumulate_add(case1)" * doctest::test_suite("array::add.accumulate") * doctest::skip())
{
auto dtype = nm::none_t{};
// ACCUMULATE_ADD( case1, a, axis );
Expand Down
10 changes: 10 additions & 0 deletions tests/sycl/array/atleast_1d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ TEST_CASE("atleast_1d(case2)" * doctest::test_suite("array::atleast_1d"))
ATLEAST_1D_SUBCASE(case2, a_hs_hb);
ATLEAST_1D_SUBCASE(case2, a_hs_db);

// Disable because:
// Cannot find symbol free in kernel library
// Cannot find symbol malloc in kernel library
// TODO: fix runtime for sycl: transform to hybrid shape
#if 0
ATLEAST_1D_SUBCASE(case2, a_ds_fb);
ATLEAST_1D_SUBCASE(case2, a_ds_hb);
ATLEAST_1D_SUBCASE(case2, a_ds_db);
#endif

// ATLEAST_1D_SUBCASE(case2, a_ls_fb);
// ATLEAST_1D_SUBCASE(case2, a_ls_hb);
Expand All @@ -88,9 +94,11 @@ TEST_CASE("atleast_1d(case3)" * doctest::test_suite("array::atleast_1d"))
ATLEAST_1D_SUBCASE(case3, a_hs_hb);
ATLEAST_1D_SUBCASE(case3, a_hs_db);

#if 0
ATLEAST_1D_SUBCASE(case3, a_ds_fb);
ATLEAST_1D_SUBCASE(case3, a_ds_hb);
ATLEAST_1D_SUBCASE(case3, a_ds_db);
#endif

// ATLEAST_1D_SUBCASE(case3, a_ls_fb);
// ATLEAST_1D_SUBCASE(case3, a_ls_hb);
Expand All @@ -117,9 +125,11 @@ TEST_CASE("atleast_1d(case4)" * doctest::test_suite("array::atleast_1d"))
ATLEAST_1D_SUBCASE(case4, a_hs_hb);
ATLEAST_1D_SUBCASE(case4, a_hs_db);

#if 0
ATLEAST_1D_SUBCASE(case4, a_ds_fb);
ATLEAST_1D_SUBCASE(case4, a_ds_hb);
ATLEAST_1D_SUBCASE(case4, a_ds_db);
#endif

// ATLEAST_1D_SUBCASE(case4, a_ls_fb);
// ATLEAST_1D_SUBCASE(case4, a_ls_hb);
Expand Down
Loading

0 comments on commit 74fbb6e

Please sign in to comment.