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

Enable testing with SYCL #234

Merged
merged 7 commits into from
Jan 30, 2023
Merged
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
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ option(MDSPAN_ENABLE_EXAMPLES "Build examples." Off)
option(MDSPAN_ENABLE_BENCHMARKS "Enable benchmarks." Off)
option(MDSPAN_ENABLE_COMP_BENCH "Enable compilation benchmarks." Off)
option(MDSPAN_ENABLE_CUDA "Enable Cuda tests/benchmarks/examples if tests/benchmarks/examples are enabled." Off)
option(MDSPAN_ENABLE_SYCL "Enable SYCL tests/benchmarks/examples if tests/benchmarks/examples are enabled." Off)
option(MDSPAN_ENABLE_HIP "Enable HIP tests/benchmarks/examples if tests/benchmarks/examples are enabled." Off)
option(MDSPAN_ENABLE_OPENMP "Enable OpenMP benchmarks if benchmarks are enabled." On)
option(MDSPAN_USE_SYSTEM_GTEST "Use system-installed GoogleTest library for tests." Off)
Expand Down Expand Up @@ -137,6 +138,11 @@ endif()
add_library(mdspan INTERFACE)
add_library(std::mdspan ALIAS mdspan)

if(MDSPAN_ENABLE_SYCL)
target_compile_options(mdspan INTERFACE "-fsycl")
target_link_options(mdspan INTERFACE "-fsycl")
endif()

target_include_directories(mdspan INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
Expand Down
6 changes: 6 additions & 0 deletions include/experimental/__p0009_bits/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ static_assert(_MDSPAN_CPLUSPLUS >= MDSPAN_CXX_STD_14, "mdspan requires C++14 or
# endif
#endif

#ifndef _MDSPAN_HAS_SYCL
# if defined(SYCL_LANGUAGE_VERSION)
# define _MDSPAN_HAS_SYCL SYCL_LANGUAGE_VERSION
# endif
#endif

#ifndef __has_cpp_attribute
# define __has_cpp_attribute(x) 0
#endif
Expand Down
66 changes: 66 additions & 0 deletions tests/offload_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
//
//@HEADER

#ifdef _MDSPAN_HAS_SYCL
#include <sycl/sycl.hpp>
#endif

#ifdef _MDSPAN_HAS_HIP
#include <hip/hip_runtime.h>
#include <hip/hip_runtime_api.h>
Expand All @@ -24,11 +28,19 @@
namespace {
bool dispatch_host = true;

#ifdef _MDSPAN_HAS_SYCL
#define __MDSPAN_DEVICE_ASSERT_EQ(LHS, RHS) \
if (!(LHS == RHS)) { \
sycl::ext::oneapi::experimental::printf("expected equality of %s and %s\n", #LHS, #RHS); \
errors[0]++; \
}
#else
#define __MDSPAN_DEVICE_ASSERT_EQ(LHS, RHS) \
if (!(LHS == RHS)) { \
printf("expected equality of %s and %s\n", #LHS, #RHS); \
errors[0]++; \
}
#endif

#if defined(_MDSPAN_HAS_CUDA) || defined(_MDSPAN_HAS_HIP)

Expand Down Expand Up @@ -90,6 +102,60 @@ void free_array(T* ptr) {
#define __MDSPAN_TESTS_DISPATCH_DEFINED
#endif // _MDSPAN_HAS_CUDA

#ifdef _MDSPAN_HAS_SYCL

sycl::queue get_test_queue()
{
static sycl::queue q;
return q;
}

template<class LAMBDA>
void dispatch(LAMBDA&& f) {
if(dispatch_host) {
static_cast<LAMBDA&&>(f)();
} else {
sycl::queue q = get_test_queue();
q.submit([&](sycl::handler &cgh) {
cgh.single_task([=]() {
f();
});
});
q.wait_and_throw();
}
}

template<class T>
T* allocate_array(size_t size) {
if(dispatch_host == true)
return new T[size];
else
{
sycl::queue q = get_test_queue();
return sycl::malloc_shared<T>(size, q);
}
}

template<class T>
void free_array(T* ptr) {
if(dispatch_host == true)
delete [] ptr;
else
{
sycl::queue q = get_test_queue();
sycl::free(ptr, q);
}
}

#define __MDSPAN_TESTS_RUN_TEST(A) \
dispatch_host = true; \
A; \
dispatch_host = false; \
A;

#define __MDSPAN_TESTS_DISPATCH_DEFINED
#endif // _MDSPAN_HAS_SYCL

#ifndef __MDSPAN_TESTS_DISPATCH_DEFINED
template<class LAMBDA>
void dispatch(LAMBDA&& f) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mdarray_ctors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void test_mdarray_ctor_data_carray() {
errors[0] = 0;

dispatch([=] _MDSPAN_HOST_DEVICE () {
stdex::mdarray<int, stdex::extents<size_t,1>> m(stdex::extents<int,1>{});
stdex::mdarray<int, stdex::extents<size_t,1>, stdex::layout_right, std::array<int, 1>> m(stdex::extents<int,1>{});
__MDSPAN_DEVICE_ASSERT_EQ(m.rank(), 1);
__MDSPAN_DEVICE_ASSERT_EQ(m.rank_dynamic(), 0);
__MDSPAN_DEVICE_ASSERT_EQ(m.extent(0), 1);
Expand Down