================ @@ -0,0 +1,349 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_PSTL_BACKENDS_OPENMP_BACKEND_OMP_OFFLOAD_H +#define _LIBCPP___ALGORITHM_PSTL_BACKENDS_OPENMP_BACKEND_OMP_OFFLOAD_H + +#include <__assert> +#include <__config> +#include <__functional/operations.h> +#include <__iterator/iterator_traits.h> +#include <__iterator/wrap_iter.h> +#include <__memory/addressof.h> +#include <__memory/pointer_traits.h> +#include <__type_traits/is_pointer.h> +#include <__type_traits/is_same.h> +#include <__utility/move.h> +#include <cstddef> + +// is_same + +// __libcpp_is_contiguous_iterator + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace __par_backend { +inline namespace __omp_gpu_backend { + +// Functions for eaxtracting the pase pointers + +// In the general case we do not need to extract it. This is for instance the +// case for pointers. +template <typename _Tp> +_LIBCPP_HIDE_FROM_ABI inline auto __omp_extract_base_ptr(_Tp p) noexcept { + return p; +} + +// For vectors and arrays, etc, we need to extract the underlying base pointer. +template <typename _Tp> +_LIBCPP_HIDE_FROM_ABI inline _Tp __omp_extract_base_ptr(std::__wrap_iter<_Tp> w) noexcept { + std::pointer_traits<std::__wrap_iter<_Tp>> PT; + return PT.to_address(w); +} + +//===----------------------------------------------------------------------===// +// The following four functions differentiates between contiguous iterators and +// non-contiguous iterators. That allows to use the same implementations for +// reference and value iterators +//===----------------------------------------------------------------------===// + +template <class _Iterator, class _DifferenceType> +_LIBCPP_HIDE_FROM_ABI void +__omp_map_to([[maybe_unused]] const _Iterator p, [[maybe_unused]] const _DifferenceType len) noexcept { + if constexpr (__libcpp_is_contiguous_iterator<_Iterator>::value) { +# pragma omp target enter data map(to : p[0 : len]) + } else { +# pragma omp target enter data map(to : p) + } +} + +template <class _Iterator, class _DifferenceType> +_LIBCPP_HIDE_FROM_ABI void +__omp_map_from([[maybe_unused]] const _Iterator p, [[maybe_unused]] const _DifferenceType len) noexcept { + if constexpr (__libcpp_is_contiguous_iterator<_Iterator>::value) { +# pragma omp target exit data map(from : p[0 : len]) + } else { +# pragma omp target exit data map(release : p) + } +} + +template <class _Iterator, class _DifferenceType> +_LIBCPP_HIDE_FROM_ABI void +__omp_map_alloc([[maybe_unused]] const _Iterator p, [[maybe_unused]] const _DifferenceType len) noexcept { + if constexpr (__libcpp_is_contiguous_iterator<_Iterator>::value) { +# pragma omp target enter data map(alloc : p[0 : len]) + } else { +# pragma omp target enter data map(to : p) + } +} + +template <class _Iterator, class _DifferenceType> +_LIBCPP_HIDE_FROM_ABI void +__omp_map_free([[maybe_unused]] const _Iterator p, [[maybe_unused]] const _DifferenceType len) noexcept { + if constexpr (__libcpp_is_contiguous_iterator<_Iterator>::value) { +# pragma omp target exit data map(release : p[0 : len]) + } else { +# pragma omp target exit data map(release : p) + } +} + +//===----------------------------------------------------------------------===// +// Templates for one iterator +//===----------------------------------------------------------------------===// + +// Applying function or lambda in a loop + +template <class _Iterator, class _DifferenceType, class _Function> +_LIBCPP_HIDE_FROM_ABI _Iterator __omp_parallel_for_simd_1( + _Iterator __first, _DifferenceType __n, _Function __f, [[maybe_unused]] const int __device = 0) noexcept { + __omp_map_to(__first, __n); +# pragma omp target teams distribute parallel for simd device(__device) + for (_DifferenceType __i = 0; __i < __n; ++__i) + __f(__first[__i]); + __omp_map_from(__first, __n); + + return __first + __n; +} + +// Extracting the underlying pointer + +template <class _Iterator, class _DifferenceType, class _Function> +_LIBCPP_HIDE_FROM_ABI _Iterator __parallel_for_simd_1(_Iterator __first, _DifferenceType __n, _Function __f) noexcept { + __omp_parallel_for_simd_1(__omp_gpu_backend::__omp_extract_base_ptr(__first), __n, __f); ---------------- ldionne wrote:
You should qualify free function calls unless you really intend ADL to kick in, as explained during live review. https://github.com/llvm/llvm-project/pull/66968 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits