Skip to content

Commit

Permalink
Merge pull request #168 from krasznaa/SYCLAtomicRef-main-20220224
Browse files Browse the repository at this point in the history
sycl::atomic_ref, main branch (2022.02.24.)
  • Loading branch information
krasznaa authored Feb 24, 2022
2 parents b191907 + 3e69f74 commit 498c0a8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
10 changes: 9 additions & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ if(NOT VECMEM_HAVE_DEFAULT_RESOURCE)
FILES "src/memory/default_resource_polyfill.cpp" )
endif()

# Figure out how to produce SYCL debug printouts.
# Figure out how to use various SYCL features.
if( VECMEM_BUILD_SYCL_LIBRARY )

# Test which printf function(s) is/are available.
Expand All @@ -190,4 +190,12 @@ if( VECMEM_BUILD_SYCL_LIBRARY )
target_compile_definitions( vecmem_core PUBLIC
VECMEM_ONEAPI_PRINTF_FUNCTION=printf )
endif()

# Test whether sycl::atomic_ref is available.
vecmem_check_sycl_code_compiles( VECMEM_HAVE_SYCL_ATOMIC_REF
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/atomic_ref_test.sycl" )
if( VECMEM_HAVE_SYCL_ATOMIC_REF )
target_compile_definitions( vecmem_core PUBLIC
VECMEM_HAVE_SYCL_ATOMIC_REF )
endif()
endif()
21 changes: 21 additions & 0 deletions core/cmake/atomic_ref_test.sycl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** VecMem project, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// SYCL include(s).
#include <CL/sycl.hpp>

int main() {
// Try to use sycl::atomic_ref.
int dummy = 0;
sycl::atomic_ref<int, sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::global_space>
atomic_dummy(dummy);
atomic_dummy.store(3);
atomic_dummy.fetch_add(1);
return 0;
}
51 changes: 50 additions & 1 deletion core/include/vecmem/memory/atomic.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -13,6 +13,53 @@
// System include(s).
#include <type_traits>

// Provide a different implementation for modern SYCL and everything else
#if (defined(CL_SYCL_LANGUAGE_VERSION) || defined(SYCL_LANGUAGE_VERSION)) && \
defined(VECMEM_HAVE_SYCL_ATOMIC_REF)

namespace vecmem {

/// In modern SYCL code @c vecmem::atomic is an alias for @c sycl::atomic_ref
///
/// It has to be an actual class and not just a typedef, because
/// @c sycl::atomic_ref, as the name implies, is created on top of references,
/// and not on top of pointers.
///
template <typename T>
class atomic
: public ::sycl::atomic_ref<T, ::sycl::memory_order::relaxed,
::sycl::memory_scope::device,
::sycl::access::address_space::global_space> {

public:
/// @name Type definitions
/// @{

/// Type managed by the object
typedef T value_type;
/// Difference between two objects
typedef value_type difference_type;
/// Pointer to the value in global memory
typedef value_type* pointer;
/// Reference to a value given by the user
typedef value_type& reference;

/// @}

/// Constructor, with a pointer to the managed variable
VECMEM_HOST_AND_DEVICE
atomic(pointer ptr)
: ::sycl::atomic_ref<T, ::sycl::memory_order::relaxed,
::sycl::memory_scope::device,
::sycl::access::address_space::global_space>(
*ptr) {}

}; // class atomic

} // namespace vecmem

#else

namespace vecmem {

/// Class providing atomic operations for the VecMem code
Expand Down Expand Up @@ -107,3 +154,5 @@ class atomic {

// Include the implementation.
#include "vecmem/memory/impl/atomic.ipp"

#endif // sycl::atomic_ref

0 comments on commit 498c0a8

Please sign in to comment.