|
| 1 | +// -*- C++ -*- |
| 2 | +//===-- for_loop.h --------------------------------------------------------===// |
| 3 | +// |
| 4 | +// Copyright (C) 2019 Intel Corporation |
| 5 | +// |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +// This file incorporates work covered by the following copyright and permission |
| 9 | +// notice: |
| 10 | +// |
| 11 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 12 | +// See https://llvm.org/LICENSE.txt for license information. |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#ifndef __PSTL_experimental_for_loop_H |
| 17 | +#define __PSTL_experimental_for_loop_H |
| 18 | + |
| 19 | +#include <tuple> |
| 20 | + |
| 21 | +#include "../../internal/pstl_config.h" |
| 22 | +#include "../../internal/execution_impl.h" |
| 23 | +#include "../../internal/utils.h" |
| 24 | +#include "for_loop_impl.h" |
| 25 | + |
| 26 | +namespace std |
| 27 | +{ |
| 28 | +namespace experimental |
| 29 | +{ |
| 30 | +inline namespace parallelism_v2 |
| 31 | +{ |
| 32 | + |
| 33 | +// TODO: type_identity should be available in type_traits starting from C++20 |
| 34 | +// Perhaps we need to add an internal structure if PSTL is used with older versions |
| 35 | +template <typename _Tp> |
| 36 | +struct type_identity |
| 37 | +{ |
| 38 | + using type = _Tp; |
| 39 | +}; |
| 40 | +template <typename _Tp> |
| 41 | +using type_identity_t = typename type_identity<_Tp>::type; |
| 42 | + |
| 43 | +// TODO: add static asserts for parameters according to the requirements |
| 44 | +template <typename _ExecutionPolicy, typename _Ip, typename... _Rest> |
| 45 | +void |
| 46 | +for_loop(_ExecutionPolicy&& __exec, type_identity_t<_Ip> __start, _Ip __finish, _Rest&&... __rest) |
| 47 | +{ |
| 48 | + __pstl::__internal::__for_loop_repack(std::forward<_ExecutionPolicy>(__exec), __start, __finish, |
| 49 | + __pstl::__internal::__single_stride_type{}, |
| 50 | + std::forward_as_tuple(std::forward<_Rest>(__rest)...)); |
| 51 | +} |
| 52 | + |
| 53 | +template <typename _ExecutionPolicy, typename _Ip, typename _Sp, typename... _Rest> |
| 54 | +void |
| 55 | +for_loop_strided(_ExecutionPolicy&& __exec, type_identity_t<_Ip> __start, _Ip __finish, _Sp __stride, _Rest&&... __rest) |
| 56 | +{ |
| 57 | + __pstl::__internal::__for_loop_repack(std::forward<_ExecutionPolicy>(__exec), __start, __finish, __stride, |
| 58 | + std::forward_as_tuple(std::forward<_Rest>(__rest)...)); |
| 59 | +} |
| 60 | + |
| 61 | +template <typename _ExecutionPolicy, typename _Ip, typename _Size, typename... _Rest> |
| 62 | +void |
| 63 | +for_loop_n(_ExecutionPolicy&& __exec, _Ip __start, _Size __n, _Rest&&... __rest) |
| 64 | +{ |
| 65 | + __pstl::__internal::__for_loop_repack_n(std::forward<_ExecutionPolicy>(__exec), __start, __n, |
| 66 | + __pstl::__internal::__single_stride_type{}, |
| 67 | + std::forward_as_tuple(std::forward<_Rest>(__rest)...)); |
| 68 | +} |
| 69 | + |
| 70 | +template <typename _ExecutionPolicy, typename _Ip, typename _Size, typename _Sp, typename... _Rest> |
| 71 | +void |
| 72 | +for_loop_n_strided(_ExecutionPolicy&& __exec, _Ip __start, _Size __n, _Sp __stride, _Rest&&... __rest) |
| 73 | +{ |
| 74 | + __pstl::__internal::__for_loop_repack_n(std::forward<_ExecutionPolicy>(__exec), __start, __n, __stride, |
| 75 | + std::forward_as_tuple(std::forward<_Rest>(__rest)...)); |
| 76 | +} |
| 77 | + |
| 78 | +// Serial implementations |
| 79 | +template <typename _Ip, typename... _Rest> |
| 80 | +void |
| 81 | +for_loop(type_identity_t<_Ip> __start, _Ip __finish, _Rest&&... __rest) |
| 82 | +{ |
| 83 | + std::experimental::parallelism_v2::for_loop(__pstl::execution::v1::seq, __start, __finish, |
| 84 | + std::forward<_Rest>(__rest)...); |
| 85 | +} |
| 86 | + |
| 87 | +template <typename _Ip, typename _Sp, typename... _Rest> |
| 88 | +void |
| 89 | +for_loop_strided(type_identity_t<_Ip> __start, _Ip __finish, _Sp __stride, _Rest&&... __rest) |
| 90 | +{ |
| 91 | + std::experimental::parallelism_v2::for_loop_strided(__pstl::execution::v1::seq, __start, __finish, __stride, |
| 92 | + std::forward<_Rest>(__rest)...); |
| 93 | +} |
| 94 | + |
| 95 | +template <typename _Ip, typename _Size, typename... _Rest> |
| 96 | +void |
| 97 | +for_loop_n(_Ip __start, _Size __n, _Rest&&... __rest) |
| 98 | +{ |
| 99 | + std::experimental::parallelism_v2::for_loop_n(__pstl::execution::v1::seq, __start, __n, |
| 100 | + std::forward<_Rest>(__rest)...); |
| 101 | +} |
| 102 | + |
| 103 | +template <typename _Ip, typename _Size, typename _Sp, typename... _Rest> |
| 104 | +void |
| 105 | +for_loop_n_strided(_Ip __start, _Size __n, _Sp __stride, _Rest&&... __rest) |
| 106 | +{ |
| 107 | + std::experimental::parallelism_v2::for_loop_n_strided(__pstl::execution::v1::seq, __start, __n, __stride, |
| 108 | + std::forward<_Rest>(__rest)...); |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace parallelism_v2 |
| 112 | +} // namespace experimental |
| 113 | +} // namespace std |
| 114 | + |
| 115 | +#endif /* __PSTL_experimental_for_loop_H */ |
0 commit comments