diff options
author | Mehdi Amini <joker.eph@gmail.com> | 2025-06-26 23:05:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-26 23:05:41 +0200 |
commit | ad613ab79eb9250b1ab8262edcd87503c4b7bdbe (patch) | |
tree | 09d226e8979ebd77e08e6f495f98d956966636e0 | |
parent | 1dfdd1e6dea1f52eab9968ca9ce817018d499de5 (diff) | |
download | llvm-revert-143717-aballman-remove-pstl.zip llvm-revert-143717-aballman-remove-pstl.tar.gz llvm-revert-143717-aballman-remove-pstl.tar.bz2 |
Revert "Remove PSTL from the top-level monorepo (#143717)"revert-143717-aballman-remove-pstl
This reverts commit f8ed45611b75ecac1d459aa6ae3b7051d435abe6.
117 files changed, 22172 insertions, 4 deletions
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index 4f542bb..902f977 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -109,7 +109,7 @@ endif() # This allows an easy way of setting up a build directory for llvm and another # one for llvm+clang+... using the same sources. # These projects will be included when "all" is included in LLVM_ENABLE_PROJECTS. -set(LLVM_ALL_PROJECTS "bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libclc;lld;lldb;mlir;openmp;polly") +set(LLVM_ALL_PROJECTS "bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libclc;lld;lldb;mlir;openmp;polly;pstl") if (${CMAKE_SYSTEM_NAME} MATCHES "AIX") # Disallow 'openmp' as a LLVM PROJECT on AIX as the supported way is to use # LLVM_ENABLE_RUNTIMES. @@ -141,7 +141,7 @@ endforeach() # As we migrate runtimes to using the bootstrapping build, the set of default runtimes # should grow as we remove those runtimes from LLVM_ENABLE_PROJECTS above. set(LLVM_DEFAULT_RUNTIMES "libcxx;libcxxabi;libunwind") -set(LLVM_SUPPORTED_RUNTIMES "libc;libunwind;libcxxabi;libcxx;compiler-rt;openmp;llvm-libgcc;offload;flang-rt;libclc") +set(LLVM_SUPPORTED_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;llvm-libgcc;offload;flang-rt;libclc") set(LLVM_ENABLE_RUNTIMES "" CACHE STRING "Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.") if(LLVM_ENABLE_RUNTIMES STREQUAL "all") diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst index fc82235..72f19fd 100644 --- a/llvm/docs/CMake.rst +++ b/llvm/docs/CMake.rst @@ -588,7 +588,7 @@ enabled sub-projects. Nearly all of these variable names begin with The full list is: - ``bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly`` + ``bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly;pstl`` .. note:: Some projects listed here can also go in ``LLVM_ENABLE_RUNTIMES``. They @@ -610,7 +610,7 @@ enabled sub-projects. Nearly all of these variable names begin with The full list is: - ``libc;libunwind;libcxxabi;libcxx;compiler-rt;openmp;llvm-libgcc;offload`` + ``libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;llvm-libgcc;offload`` To enable all of them, use: diff --git a/pstl/.clang-format b/pstl/.clang-format new file mode 100644 index 0000000..8a110c1 --- /dev/null +++ b/pstl/.clang-format @@ -0,0 +1,20 @@ +BasedOnStyle: LLVM + +Language: Cpp +Standard: Cpp11 + +IndentWidth: 4 +ColumnLimit: 120 + +AlwaysBreakTemplateDeclarations: true +AlwaysBreakAfterReturnType: All +PointerAlignment: Left +AllowShortIfStatementsOnASingleLine: false +BreakBeforeBraces: Allman + +# Disable formatting options which may break tests. +SortIncludes: false +ReflowComments: false + +# Indent preprocessor directives +IndentPPDirectives: AfterHash diff --git a/pstl/CMakeLists.txt b/pstl/CMakeLists.txt new file mode 100644 index 0000000..592e11d --- /dev/null +++ b/pstl/CMakeLists.txt @@ -0,0 +1,103 @@ +#===-- CMakeLists.txt ----------------------------------------------------===## +# +# 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 +# +#===----------------------------------------------------------------------===## +cmake_minimum_required(VERSION 3.20.0) +set(LLVM_SUBPROJECT_TITLE "Parallel STL") + +set(PARALLELSTL_VERSION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/include/pstl/internal/pstl_config.h") +file(STRINGS "${PARALLELSTL_VERSION_FILE}" PARALLELSTL_VERSION_SOURCE REGEX "#define _PSTL_VERSION .*$") +string(REGEX REPLACE "#define _PSTL_VERSION (.*)$" "\\1" PARALLELSTL_VERSION_SOURCE "${PARALLELSTL_VERSION_SOURCE}") +math(EXPR VERSION_MAJOR "(${PARALLELSTL_VERSION_SOURCE} / 1000)") +math(EXPR VERSION_MINOR "((${PARALLELSTL_VERSION_SOURCE} % 1000) / 10)") +math(EXPR VERSION_PATCH "(${PARALLELSTL_VERSION_SOURCE} % 10)") + +project(ParallelSTL VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} LANGUAGES CXX) + +# Must go below project(..) +include(GNUInstallDirs) + +set(PSTL_PARALLEL_BACKEND "serial" CACHE STRING "Threading backend to use. Valid choices are 'serial', 'omp', and 'tbb'. The default is 'serial'.") +set(PSTL_HIDE_FROM_ABI_PER_TU OFF CACHE BOOL "Whether to constrain ABI-unstable symbols to each translation unit (basically, mark them with C's static keyword).") +set(_PSTL_HIDE_FROM_ABI_PER_TU ${PSTL_HIDE_FROM_ABI_PER_TU}) # For __pstl_config_site + +if (NOT TBB_DIR) + get_filename_component(PSTL_DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) + string(REPLACE pstl tbb TBB_DIR_NAME ${PSTL_DIR_NAME}) + if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake") + get_filename_component(TBB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake" ABSOLUTE) + endif() +endif() + +############################################################################### +# Setup the ParallelSTL library target +############################################################################### +add_library(ParallelSTL INTERFACE) +add_library(pstl::ParallelSTL ALIAS ParallelSTL) +target_compile_features(ParallelSTL INTERFACE cxx_std_17) + +if (PSTL_PARALLEL_BACKEND STREQUAL "serial") + message(STATUS "Parallel STL uses the serial backend") + set(_PSTL_PAR_BACKEND_SERIAL ON) +elseif (PSTL_PARALLEL_BACKEND STREQUAL "tbb") + find_package(TBB 2018 REQUIRED tbb OPTIONAL_COMPONENTS tbbmalloc) + message(STATUS "Parallel STL uses TBB ${TBB_VERSION} (interface version: ${TBB_INTERFACE_VERSION})") + target_link_libraries(ParallelSTL INTERFACE TBB::tbb) + set(_PSTL_PAR_BACKEND_TBB ON) +elseif (PSTL_PARALLEL_BACKEND STREQUAL "omp") + message(STATUS "Parallel STL uses the omp backend") + target_compile_options(ParallelSTL INTERFACE "-fopenmp=libomp") + set(_PSTL_PAR_BACKEND_OPENMP ON) +else() + message(FATAL_ERROR "Requested unknown Parallel STL backend '${PSTL_PARALLEL_BACKEND}'.") +endif() + +set(PSTL_GENERATED_HEADERS_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated_headers") +set(PSTL_CONFIG_SITE_PATH "${PSTL_GENERATED_HEADERS_DIR}/__pstl_config_site") +configure_file("include/__pstl_config_site.in" + "${PSTL_CONFIG_SITE_PATH}" + @ONLY) + +target_include_directories(ParallelSTL + INTERFACE + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> + $<BUILD_INTERFACE:${PSTL_GENERATED_HEADERS_DIR}> + $<INSTALL_INTERFACE:include>) + +############################################################################### +# Setup tests +############################################################################### +enable_testing() +add_subdirectory(test) + +############################################################################### +# Install the target and the associated CMake files +############################################################################### +include(CMakePackageConfigHelpers) +write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake" + COMPATIBILITY ExactVersion) + +configure_file(cmake/ParallelSTLConfig.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake" + @ONLY) + +install(TARGETS ParallelSTL + EXPORT ParallelSTLTargets) +install(EXPORT ParallelSTLTargets + FILE ParallelSTLTargets.cmake + NAMESPACE pstl:: + DESTINATION lib/cmake/ParallelSTL) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake" + DESTINATION lib/cmake/ParallelSTL) +install(DIRECTORY include/ + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + PATTERN "*.in" EXCLUDE) +install(FILES "${PSTL_CONFIG_SITE_PATH}" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") + +add_custom_target(install-pstl + COMMAND "${CMAKE_COMMAND}" -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" -DCOMPONENT=ParallelSTL) diff --git a/pstl/CREDITS.txt b/pstl/CREDITS.txt new file mode 100644 index 0000000..4945fd5 --- /dev/null +++ b/pstl/CREDITS.txt @@ -0,0 +1,21 @@ +This file is a partial list of people who have contributed to the LLVM/pstl
+(Parallel STL) project. If you have contributed a patch or made some other
+contribution to LLVM/pstl, please submit a patch to this file to add yourself,
+and it will be done!
+
+The list is sorted by surname and formatted to allow easy grepping and
+beautification by scripts. The fields are: name (N), email (E), web-address
+(W), PGP key ID and fingerprint (P), description (D), and snail-mail address
+(S).
+
+N: Intel Corporation
+W: http://www.intel.com
+D: Created the initial implementation.
+
+N: Thomas Rodgers
+E: trodgers@redhat.com
+D: Identifier name transformation for inclusion in a Standard C++ library.
+
+N: Christopher Nelson
+E: nadiasvertex@gmail.com
+D: Add support for an OpenMP backend.
diff --git a/pstl/LICENSE.TXT b/pstl/LICENSE.TXT new file mode 100644 index 0000000..9eda544 --- /dev/null +++ b/pstl/LICENSE.TXT @@ -0,0 +1,307 @@ +============================================================================== +The LLVM Project is under the Apache License v2.0 with LLVM Exceptions: +============================================================================== + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +---- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. + +============================================================================== +Software from third parties included in the LLVM Project: +============================================================================== +The LLVM Project contains third party software which is under different license +terms. All such code will be identified clearly using at least one of two +mechanisms: +1) It will be in a separate directory tree with its own `LICENSE.txt` or + `LICENSE` file at the top containing the specific license and restrictions + which apply to that software, or +2) It will contain specific license and restriction terms at the top of every + file. + +============================================================================== +Legacy LLVM License (https://llvm.org/docs/DeveloperPolicy.html#legacy): +============================================================================== + +The software contained in this directory tree is dual licensed under both the +University of Illinois "BSD-Like" license and the MIT license. As a user of +this code you may choose to use it under either license. As a contributor, +you agree to allow your code to be used under both. + +Full text of the relevant licenses is included below. + +============================================================================== + +University of Illinois/NCSA +Open Source License + +Copyright (c) 2017-2019 by the contributors listed in CREDITS.TXT + +All rights reserved. + +Developed by: + Threading Runtimes Team + Intel Corporation + http://www.intel.com + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of Intel Corporation Threading Runtimes Team nor the + names of its contributors may be used to endorse or promote products + derived from this Software without specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +============================================================================== + +Copyright (c) 2017-2019 by the contributors listed in CREDITS.TXT + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/pstl/README.md b/pstl/README.md new file mode 100644 index 0000000..5c862cb --- /dev/null +++ b/pstl/README.md @@ -0,0 +1,35 @@ +# Parallel STL + +Parallel STL is an implementation of the C++ standard library algorithms with support for execution policies, +as specified in ISO/IEC 14882:2017 standard, commonly called C++17. The implementation also supports the unsequenced +execution policy specified in Parallelism TS version 2 and proposed for the next version of the C++ standard in the +C++ working group paper [P1001](https://wg21.link/p1001). +Parallel STL offers efficient support for both parallel and vectorized execution of algorithms. For sequential +execution, it relies on an available implementation of the C++ standard library. + +## Prerequisites + +To use Parallel STL, you must have the following software installed: +* C++ compiler with: + * Support for C++11 + * Support for OpenMP* 4.0 SIMD constructs +* Threading Building Blocks (TBB) which is available for download at https://github.com/01org/tbb/ + +## Known issues and limitations + +* `unseq` and `par_unseq` policies only have effect with compilers that support `#pragma omp simd` or `#pragma simd`. +* Parallel and vector execution is only supported for the algorithms if random access iterators are provided, + while for other iterator types the execution will remain serial. +* The following algorithms do not allow efficient SIMD execution: `includes`, `inplace_merge`, `merge`, `nth_element`, + `partial_sort`, `partial_sort_copy`, `set_difference`, `set_intersection`, `set_symmetric_difference`, `set_union`, + `sort`, `stable_partition`, `stable_sort`, `unique`. +* The initial value type for `exclusive_scan`, `inclusive_scan`, `transform_exclusive_scan`, `transform_inclusive_scan` + shall be DefaultConstructible. A default constructed-instance of the initial value type shall be the identity element + for the specified binary operation. +* For `max_element`, `min_element`, `minmax_element`, `partial_sort`, `partial_sort_copy`, `sort`, `stable_sort` + the dereferenced value type of the provided iterators shall be DefaultConstructible. +* For `remove`, `remove_if`, `unique` the dereferenced value type of the provided iterators shall be MoveConstructible. +* The following algorithms require additional O(n) memory space for parallel execution: `copy_if`, `inplace_merge`, + `partial_sort`, `partial_sort_copy`, `partition_copy`, `remove`, `remove_if`, `rotate`, `sort`, `stable_sort`, + `unique`, `unique_copy`. + diff --git a/pstl/cmake/ParallelSTLConfig.cmake.in b/pstl/cmake/ParallelSTLConfig.cmake.in new file mode 100644 index 0000000..445ebaf --- /dev/null +++ b/pstl/cmake/ParallelSTLConfig.cmake.in @@ -0,0 +1,19 @@ +#===-- ParallelSTLConfig.cmake.in ----------------------------------------===## +# +# 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 +# +#===----------------------------------------------------------------------===## + +include(CMakeFindDependencyMacro) + +set(PSTL_PARALLEL_BACKEND "@PSTL_PARALLEL_BACKEND@") + +if (PSTL_PARALLEL_BACKEND STREQUAL "tbb") + find_dependency(TBB REQUIRED tbb) +endif() + +if (NOT TARGET pstl::ParallelSTL) + include("${CMAKE_CURRENT_LIST_DIR}/ParallelSTLTargets.cmake") +endif() diff --git a/pstl/docs/ReleaseNotes.rst b/pstl/docs/ReleaseNotes.rst new file mode 100644 index 0000000..a0b3751 --- /dev/null +++ b/pstl/docs/ReleaseNotes.rst @@ -0,0 +1,40 @@ +======================================= +PSTL 21.0.0 (In-Progress) Release Notes +======================================= + +.. contents:: + :local: + :depth: 2 + +Written by the `PSTL Team <https://pstl.llvm.org>`_ + +.. warning:: + + These are in-progress notes for the upcoming pstl 19 release. + Release notes for previous releases can be found on + `the Download Page <https://releases.llvm.org/download.html>`_. + +Introduction +============ + +This document contains the release notes for the PSTL parallel algorithms +library, part of the LLVM Compiler Infrastructure, release 17.0.0. Here we +describe the status of the library in some detail, including major improvements +from the previous release and new feature work. For the general LLVM release +notes, see `the LLVM documentation <https://llvm.org/docs/ReleaseNotes.html>`_. +All LLVM releases may be downloaded from the `LLVM releases web site +<https://llvm.org/releases/>`_. + +Note that if you are reading this file from a source checkout or the main PSTL +web page, this document applies to the *next* release, not the current one. +To see the release notes for a specific release, please see the `releases +page <https://llvm.org/releases/>`_. + +What's New in PSTL 19.0.0? +========================== + +New Features +------------ + +API Changes +----------- diff --git a/pstl/include/__pstl_algorithm b/pstl/include/__pstl_algorithm new file mode 100644 index 0000000..79c1838 --- /dev/null +++ b/pstl/include/__pstl_algorithm @@ -0,0 +1,15 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 __PSTL_ALGORITHM +#define __PSTL_ALGORITHM + +#include <pstl/internal/glue_algorithm_impl.h> + +#endif /* __PSTL_ALGORITHM */ diff --git a/pstl/include/__pstl_config_site.in b/pstl/include/__pstl_config_site.in new file mode 100644 index 0000000..a41a1c3 --- /dev/null +++ b/pstl/include/__pstl_config_site.in @@ -0,0 +1,17 @@ +//===----------------------------------------------------------------------===// +// +// 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 __PSTL_CONFIG_SITE +#define __PSTL_CONFIG_SITE + +#cmakedefine _PSTL_PAR_BACKEND_SERIAL +#cmakedefine _PSTL_PAR_BACKEND_TBB +#cmakedefine _PSTL_PAR_BACKEND_OPENMP +#cmakedefine _PSTL_HIDE_FROM_ABI_PER_TU + +#endif // __PSTL_CONFIG_SITE diff --git a/pstl/include/__pstl_execution b/pstl/include/__pstl_execution new file mode 100644 index 0000000..0e2cd44 --- /dev/null +++ b/pstl/include/__pstl_execution @@ -0,0 +1,15 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 __PSTL_EXECUTION +#define __PSTL_EXECUTION + +#include <pstl/internal/glue_execution_defs.h> + +#endif /* __PSTL_EXECUTION */ diff --git a/pstl/include/__pstl_memory b/pstl/include/__pstl_memory new file mode 100644 index 0000000..12b7f5a --- /dev/null +++ b/pstl/include/__pstl_memory @@ -0,0 +1,15 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 __PSTL_MEMORY +#define __PSTL_MEMORY + +#include <pstl/internal/glue_memory_impl.h> + +#endif /* __PSTL_MEMORY */ diff --git a/pstl/include/__pstl_numeric b/pstl/include/__pstl_numeric new file mode 100644 index 0000000..cf168ef7 --- /dev/null +++ b/pstl/include/__pstl_numeric @@ -0,0 +1,15 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 __PSTL_NUMERIC +#define __PSTL_NUMERIC + +#include <pstl/internal/glue_numeric_impl.h> + +#endif /* __PSTL_NUMERIC */ diff --git a/pstl/include/pstl/internal/algorithm_fwd.h b/pstl/include/pstl/internal/algorithm_fwd.h new file mode 100644 index 0000000..3dcf9e9 --- /dev/null +++ b/pstl/include/pstl/internal/algorithm_fwd.h @@ -0,0 +1,1202 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_ALGORITHM_FWD_H +#define _PSTL_ALGORITHM_FWD_H + +#include <iterator> +#include <type_traits> +#include <utility> + +#include "pstl_config.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __internal +{ + +//------------------------------------------------------------------------ +// any_of +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Pred> +bool +__brick_any_of(const _ForwardIterator, const _ForwardIterator, _Pred, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _Pred> +bool +__brick_any_of(const _RandomAccessIterator, const _RandomAccessIterator, _Pred, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Pred> +bool +__pattern_any_of(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Pred) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Pred> +bool +__pattern_any_of(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Pred); + +//------------------------------------------------------------------------ +// walk1 (pseudo) +// +// walk1 evaluates f(x) for each dereferenced value x drawn from [first,last) +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Function> +void __brick_walk1(_ForwardIterator, _ForwardIterator, _Function, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _Function> +void __brick_walk1(_RandomAccessIterator, _RandomAccessIterator, _Function, + /*vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Function> +void +__pattern_walk1(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Function) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Function> +void +__pattern_walk1(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Function); + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Brick> +void +__pattern_walk_brick(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Brick) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Brick> +void +__pattern_walk_brick(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Brick); + +//------------------------------------------------------------------------ +// walk1_n +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Size, class _Function> +_ForwardIterator __brick_walk1_n(_ForwardIterator, _Size, _Function, + /*_IsVectorTag=*/std::false_type); + +template <class _RandomAccessIterator, class _DifferenceType, class _Function> +_RandomAccessIterator __brick_walk1_n(_RandomAccessIterator, _DifferenceType, _Function, + /*vectorTag=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Function> +_ForwardIterator +__pattern_walk1_n(_Tag, _ExecutionPolicy&&, _ForwardIterator, _Size, _Function) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Function> +_RandomAccessIterator +__pattern_walk1_n(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _Size, _Function); + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Brick> +_ForwardIterator +__pattern_walk_brick_n(_Tag, _ExecutionPolicy&&, _ForwardIterator, _Size, _Brick) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Brick> +_RandomAccessIterator +__pattern_walk_brick_n(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _Size, _Brick); + +//------------------------------------------------------------------------ +// walk2 (pseudo) +// +// walk2 evaluates f(x,y) for deferenced values (x,y) drawn from [first1,last1) and [first2,...) +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _Function> +_ForwardIterator2 __brick_walk2(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _Function, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Function> +_RandomAccessIterator2 __brick_walk2(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Function, + /*vector=*/std::true_type) noexcept; + +template <class _ForwardIterator1, class _Size, class _ForwardIterator2, class _Function> +_ForwardIterator2 __brick_walk2_n(_ForwardIterator1, _Size, _ForwardIterator2, _Function, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _Size, class _RandomAccessIterator2, class _Function> +_RandomAccessIterator2 __brick_walk2_n(_RandomAccessIterator1, _Size, _RandomAccessIterator2, _Function, + /*vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Function> +_ForwardIterator2 +__pattern_walk2(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _Function) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Function> +_RandomAccessIterator2 +__pattern_walk2(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _Function); + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2, + class _Function> +_ForwardIterator2 +__pattern_walk2_n(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _Size, _ForwardIterator2, _Function) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _Size, + class _RandomAccessIterator2, class _Function> +_RandomAccessIterator2 +__pattern_walk2_n(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _Size, _RandomAccessIterator2, + _Function); + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Brick> +_ForwardIterator2 +__pattern_walk2_brick(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _Brick) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Brick> +_RandomAccessIterator2 +__pattern_walk2_brick(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _Brick); + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2, + class _Brick> +_ForwardIterator2 +__pattern_walk2_brick_n(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _Size, _ForwardIterator2, _Brick) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _Size, + class _RandomAccessIterator2, class _Brick> +_RandomAccessIterator2 +__pattern_walk2_brick_n(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _Size, + _RandomAccessIterator2, _Brick); + +//------------------------------------------------------------------------ +// walk3 (pseudo) +// +// walk3 evaluates f(x,y,z) for (x,y,z) drawn from [first1,last1), [first2,...), [first3,...) +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator3, class _Function> +_ForwardIterator3 __brick_walk3(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator3, _Function, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, class _Function> +_RandomAccessIterator3 __brick_walk3(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator3, _Function, + /*vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator3, + class _Function> +_ForwardIterator3 +__pattern_walk3(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator3, + _Function) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _RandomAccessIterator3, class _Function> +_RandomAccessIterator3 +__pattern_walk3(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator3, _Function); + +//------------------------------------------------------------------------ +// equal +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool __brick_equal(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _BinaryPredicate, + /* is_vector = */ std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +bool __brick_equal(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _BinaryPredicate, + /* is_vector = */ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool +__pattern_equal(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _BinaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +bool +__pattern_equal(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _BinaryPredicate); + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool __brick_equal(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, _BinaryPredicate, + /* is_vector = */ std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +bool __brick_equal(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _RandomAccessIterator2, + _BinaryPredicate, /* is_vector = */ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool +__pattern_equal(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _BinaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +bool +__pattern_equal(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _BinaryPredicate); + +//------------------------------------------------------------------------ +// find_if +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Predicate> +_ForwardIterator __brick_find_if(_ForwardIterator, _ForwardIterator, _Predicate, + /*is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _Predicate> +_RandomAccessIterator __brick_find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, + /*is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +_ForwardIterator +__pattern_find_if(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Predicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Predicate> +_RandomAccessIterator +__pattern_find_if(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Predicate); + +//------------------------------------------------------------------------ +// find_end +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 __brick_find_end(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _BinaryPredicate, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +_RandomAccessIterator1 __brick_find_end(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _BinaryPredicate, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__pattern_find_end(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _BinaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +_RandomAccessIterator1 +__pattern_find_end(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _BinaryPredicate) noexcept; + +//------------------------------------------------------------------------ +// find_first_of +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 __brick_find_first_of(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _BinaryPredicate, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +_RandomAccessIterator1 __brick_find_first_of(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _BinaryPredicate, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__pattern_find_first_of(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _ForwardIterator2, _BinaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +_RandomAccessIterator1 +__pattern_find_first_of(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _BinaryPredicate) noexcept; + +//------------------------------------------------------------------------ +// search +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 __brick_search(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _BinaryPredicate, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +_RandomAccessIterator1 __brick_search(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _BinaryPredicate, + /*vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__pattern_search(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _BinaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +_RandomAccessIterator1 +__pattern_search(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _BinaryPredicate) noexcept; + +//------------------------------------------------------------------------ +// search_n +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> +_ForwardIterator +__brick_search_n(_ForwardIterator, _ForwardIterator, _Size, const _Tp&, _BinaryPredicate, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _Size, class _Tp, class _BinaryPredicate> +_RandomAccessIterator +__brick_search_n(_RandomAccessIterator, _RandomAccessIterator, _Size, const _Tp&, _BinaryPredicate, + /*vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> +_ForwardIterator +__pattern_search_n(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Size, const _Tp&, + _BinaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Tp, + class _BinaryPredicate> +_RandomAccessIterator +__pattern_search_n(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Size, + const _Tp&, _BinaryPredicate) noexcept; + +//------------------------------------------------------------------------ +// copy_n +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Size, class _OutputIterator> +_OutputIterator __brick_copy_n(_ForwardIterator, _Size, _OutputIterator, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _Size, class _OutputIterator> +_OutputIterator __brick_copy_n(_RandomAccessIterator, _Size, _OutputIterator, + /*vector=*/std::true_type) noexcept; + +//------------------------------------------------------------------------ +// copy +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator> +_OutputIterator __brick_copy(_ForwardIterator, _ForwardIterator, _OutputIterator, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator> +_OutputIterator __brick_copy(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, + /*vector=*/std::true_type) noexcept; + +//------------------------------------------------------------------------ +// move +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator> +_OutputIterator __brick_move(_ForwardIterator, _ForwardIterator, _OutputIterator, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator> +_OutputIterator __brick_move(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, + /*vector=*/std::true_type) noexcept; + +//------------------------------------------------------------------------ +// swap_ranges +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _OutputIterator> +_OutputIterator __brick_swap_ranges(_ForwardIterator, _ForwardIterator, _OutputIterator, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator> +_OutputIterator __brick_swap_ranges(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, + /*vector=*/std::true_type) noexcept; + +//------------------------------------------------------------------------ +// copy_if +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator, class _UnaryPredicate> +_OutputIterator __brick_copy_if(_ForwardIterator, _ForwardIterator, _OutputIterator, _UnaryPredicate, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator, class _UnaryPredicate> +_OutputIterator __brick_copy_if(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, _UnaryPredicate, + /*vector=*/std::true_type) noexcept; + +template <class _DifferenceType, class _ForwardIterator, class _UnaryPredicate> +std::pair<_DifferenceType, _DifferenceType> +__brick_calc_mask_1(_ForwardIterator, _ForwardIterator, bool* __restrict, _UnaryPredicate, + /*vector=*/std::false_type) noexcept; +template <class _DifferenceType, class _RandomAccessIterator, class _UnaryPredicate> +std::pair<_DifferenceType, _DifferenceType> +__brick_calc_mask_1(_RandomAccessIterator, _RandomAccessIterator, bool* __restrict, _UnaryPredicate, + /*vector=*/std::true_type) noexcept; + +template <class _ForwardIterator, class _OutputIterator> +void +__brick_copy_by_mask(_ForwardIterator, _ForwardIterator, _OutputIterator, bool*, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator> +void +__brick_copy_by_mask(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, bool* __restrict, + /*vector=*/std::true_type) noexcept; + +template <class _ForwardIterator, class _OutputIterator1, class _OutputIterator2> +void +__brick_partition_by_mask(_ForwardIterator, _ForwardIterator, _OutputIterator1, _OutputIterator2, bool*, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator1, class _OutputIterator2> +void +__brick_partition_by_mask(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator1, _OutputIterator2, bool*, + /*vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _UnaryPredicate> +_OutputIterator +__pattern_copy_if(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _OutputIterator, + _UnaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, + class _UnaryPredicate> +_OutputIterator +__pattern_copy_if(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _OutputIterator, _UnaryPredicate); + +//------------------------------------------------------------------------ +// count +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator, class _Predicate> +typename std::iterator_traits<_RandomAccessIterator>::difference_type + __brick_count(_RandomAccessIterator, _RandomAccessIterator, _Predicate, + /* is_vector = */ std::true_type) noexcept; + +template <class _ForwardIterator, class _Predicate> +typename std::iterator_traits<_ForwardIterator>::difference_type + __brick_count(_ForwardIterator, _ForwardIterator, _Predicate, + /* is_vector = */ std::false_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +typename std::iterator_traits<_ForwardIterator>::difference_type +__pattern_count(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Predicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Predicate> +typename std::iterator_traits<_RandomAccessIterator>::difference_type +__pattern_count(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Predicate); + +//------------------------------------------------------------------------ +// unique +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _BinaryPredicate> +_ForwardIterator __brick_unique(_ForwardIterator, _ForwardIterator, _BinaryPredicate, + /*is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator __brick_unique(_RandomAccessIterator, _RandomAccessIterator, _BinaryPredicate, + /*is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate> +_ForwardIterator +__pattern_unique(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _BinaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator +__pattern_unique(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _BinaryPredicate) noexcept; + +//------------------------------------------------------------------------ +// unique_copy +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class OutputIterator, class _BinaryPredicate> +OutputIterator __brick_unique_copy(_ForwardIterator, _ForwardIterator, OutputIterator, _BinaryPredicate, + /*vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator, class _BinaryPredicate> +_OutputIterator __brick_unique_copy(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, _BinaryPredicate, + /*vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _BinaryPredicate> +_OutputIterator +__pattern_unique_copy(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _OutputIterator, + _BinaryPredicate) noexcept; + +template <class _ExecutionPolicy, class _DifferenceType, class _RandomAccessIterator, class _BinaryPredicate> +_DifferenceType +__brick_calc_mask_2(_RandomAccessIterator, _RandomAccessIterator, bool* __restrict, _BinaryPredicate, + /*vector=*/std::false_type) noexcept; + +template <class _DifferenceType, class _RandomAccessIterator, class _BinaryPredicate> +_DifferenceType +__brick_calc_mask_2(_RandomAccessIterator, _RandomAccessIterator, bool* __restrict, _BinaryPredicate, + /*vector=*/std::true_type) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, + class _BinaryPredicate> +_OutputIterator +__pattern_unique_copy(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _OutputIterator, _BinaryPredicate); + +//------------------------------------------------------------------------ +// reverse +//------------------------------------------------------------------------ + +template <class _BidirectionalIterator> +void __brick_reverse(_BidirectionalIterator, _BidirectionalIterator, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator> +void __brick_reverse(_RandomAccessIterator, _RandomAccessIterator, + /*__is_vector=*/std::true_type) noexcept; + +template <class _BidirectionalIterator> +void __brick_reverse(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, + /*is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator> +void __brick_reverse(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, + /*is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _BidirectionalIterator> +void +__pattern_reverse(_Tag, _ExecutionPolicy&&, _BidirectionalIterator, _BidirectionalIterator) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator> +void +__pattern_reverse(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator); + +//------------------------------------------------------------------------ +// reverse_copy +//------------------------------------------------------------------------ + +template <class _BidirectionalIterator, class _OutputIterator> +_OutputIterator __brick_reverse_copy(_BidirectionalIterator, _BidirectionalIterator, _OutputIterator, + /*is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator> +_OutputIterator __brick_reverse_copy(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, + /*is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _BidirectionalIterator, class _OutputIterator> +_OutputIterator +__pattern_reverse_copy(_Tag, _ExecutionPolicy&&, _BidirectionalIterator, _BidirectionalIterator, + _OutputIterator) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator> +_OutputIterator +__pattern_reverse_copy(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _OutputIterator); + +//------------------------------------------------------------------------ +// rotate +//------------------------------------------------------------------------ + +template <class _ForwardIterator> +_ForwardIterator __brick_rotate(_ForwardIterator, _ForwardIterator, _ForwardIterator, + /*is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator> +_RandomAccessIterator __brick_rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, + /*is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator> +_ForwardIterator +__pattern_rotate(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _ForwardIterator) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator> +_RandomAccessIterator +__pattern_rotate(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _RandomAccessIterator); + +//------------------------------------------------------------------------ +// rotate_copy +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator> +_OutputIterator __brick_rotate_copy(_ForwardIterator, _ForwardIterator, _ForwardIterator, _OutputIterator, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator> +_OutputIterator __brick_rotate_copy(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, + _OutputIterator, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator> +_OutputIterator +__pattern_rotate_copy(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _ForwardIterator, + _OutputIterator) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator> +_OutputIterator +__pattern_rotate_copy(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _RandomAccessIterator, _OutputIterator); + +//------------------------------------------------------------------------ +// is_partitioned +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _UnaryPredicate> +bool __brick_is_partitioned(_ForwardIterator, _ForwardIterator, _UnaryPredicate, + /*is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _UnaryPredicate> +bool __brick_is_partitioned(_RandomAccessIterator, _RandomAccessIterator, _UnaryPredicate, + /*is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +bool +__pattern_is_partitioned(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _UnaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryPredicate> +bool +__pattern_is_partitioned(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _UnaryPredicate); + +//------------------------------------------------------------------------ +// partition +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _UnaryPredicate> +_ForwardIterator __brick_partition(_ForwardIterator, _ForwardIterator, _UnaryPredicate, + /*is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator __brick_partition(_RandomAccessIterator, _RandomAccessIterator, _UnaryPredicate, + /*is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +_ForwardIterator +__pattern_partition(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _UnaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__pattern_partition(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _UnaryPredicate); + +//------------------------------------------------------------------------ +// stable_partition +//------------------------------------------------------------------------ + +template <class _BidirectionalIterator, class _UnaryPredicate> +_BidirectionalIterator __brick_stable_partition(_BidirectionalIterator, _BidirectionalIterator, _UnaryPredicate, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator __brick_stable_partition(_RandomAccessIterator, _RandomAccessIterator, _UnaryPredicate, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _BidirectionalIterator, class _UnaryPredicate> +_BidirectionalIterator +__pattern_stable_partition(_Tag, _ExecutionPolicy&&, _BidirectionalIterator, _BidirectionalIterator, + _UnaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__pattern_stable_partition(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _UnaryPredicate) noexcept; + +//------------------------------------------------------------------------ +// partition_copy +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator1, class _OutputIterator2, class _UnaryPredicate> +std::pair<_OutputIterator1, _OutputIterator2> + __brick_partition_copy(_ForwardIterator, _ForwardIterator, _OutputIterator1, _OutputIterator2, _UnaryPredicate, + /*is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator1, class _OutputIterator2, class _UnaryPredicate> +std::pair<_OutputIterator1, _OutputIterator2> __brick_partition_copy(_RandomAccessIterator, _RandomAccessIterator, + _OutputIterator1, _OutputIterator2, + _UnaryPredicate, + /*is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator1, class _OutputIterator2, + class _UnaryPredicate> +std::pair<_OutputIterator1, _OutputIterator2> +__pattern_partition_copy(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _OutputIterator1, + _OutputIterator2, _UnaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator1, + class _OutputIterator2, class _UnaryPredicate> +std::pair<_OutputIterator1, _OutputIterator2> +__pattern_partition_copy(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _OutputIterator1, _OutputIterator2, _UnaryPredicate); + +//------------------------------------------------------------------------ +// sort +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsMoveConstructible> +void +__pattern_sort(_Tag, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare, + _IsMoveConstructible) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_sort(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare, + /*is_move_constructible=*/std::true_type); + +//------------------------------------------------------------------------ +// stable_sort +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_stable_sort(_Tag, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_stable_sort(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Compare); + +//------------------------------------------------------------------------ +// partial_sort +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_partial_sort(_Tag, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, + _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_partial_sort(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _RandomAccessIterator, _Compare); + +//------------------------------------------------------------------------ +// partial_sort_copy +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Compare> +_RandomAccessIterator2 +__pattern_partial_sort_copy(_Tag, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Compare> +_RandomAccessIterator2 +__pattern_partial_sort_copy(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, + _RandomAccessIterator1, _RandomAccessIterator2, _RandomAccessIterator2, _Compare); + +//------------------------------------------------------------------------ +// adjacent_find +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator +__brick_adjacent_find(_RandomAccessIterator, _RandomAccessIterator, _BinaryPredicate, + /* IsVector = */ std::true_type, bool) noexcept; + +template <class _ForwardIterator, class _BinaryPredicate> +_ForwardIterator +__brick_adjacent_find(_ForwardIterator, _ForwardIterator, _BinaryPredicate, + /* IsVector = */ std::false_type, bool) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate> +_ForwardIterator +__pattern_adjacent_find(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _BinaryPredicate, bool) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator +__pattern_adjacent_find(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _BinaryPredicate, bool); + +//------------------------------------------------------------------------ +// nth_element +//------------------------------------------------------------------------ +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_nth_element(_Tag, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, + _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_nth_element(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _RandomAccessIterator, _Compare) noexcept; + +//------------------------------------------------------------------------ +// fill, fill_n +//------------------------------------------------------------------------ +template <class _RandomAccessIterator, class _Tp> +void +__brick_fill(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, + /* __is_vector = */ std::true_type) noexcept; + +template <class _ForwardIterator, class _Tp> +void +__brick_fill(_ForwardIterator, _ForwardIterator, const _Tp&, + /* __is_vector = */ std::false_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Tp> +void +__pattern_fill(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, const _Tp&) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Tp> +_RandomAccessIterator +__pattern_fill(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, const _Tp&); + +template <class _RandomAccessIterator, class _Size, class _Tp> +_RandomAccessIterator +__brick_fill_n(_RandomAccessIterator, _Size, const _Tp&, + /* __is_vector = */ std::true_type) noexcept; + +template <class _OutputIterator, class _Size, class _Tp> +_OutputIterator +__brick_fill_n(_OutputIterator, _Size, const _Tp&, + /* __is_vector = */ std::false_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _OutputIterator, class _Size, class _Tp> +_OutputIterator +__pattern_fill_n(_Tag, _ExecutionPolicy&&, _OutputIterator, _Size, const _Tp&) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Tp> +_RandomAccessIterator +__pattern_fill_n(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _Size, const _Tp&); + +//------------------------------------------------------------------------ +// generate, generate_n +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator, class _Generator> +void __brick_generate(_RandomAccessIterator, _RandomAccessIterator, _Generator, + /* is_vector = */ std::true_type) noexcept; + +template <class _ForwardIterator, class _Generator> +void __brick_generate(_ForwardIterator, _ForwardIterator, _Generator, + /* is_vector = */ std::false_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Generator> +void +__pattern_generate(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Generator) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Generator> +_RandomAccessIterator +__pattern_generate(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Generator); + +template <class _RandomAccessIterator, class Size, class _Generator> +_RandomAccessIterator __brick_generate_n(_RandomAccessIterator, Size, _Generator, + /* is_vector = */ std::true_type) noexcept; + +template <class OutputIterator, class Size, class _Generator> +OutputIterator __brick_generate_n(OutputIterator, Size, _Generator, + /* is_vector = */ std::false_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class OutputIterator, class Size, class _Generator> +OutputIterator +__pattern_generate_n(_Tag, _ExecutionPolicy&&, OutputIterator, Size, _Generator) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class Size, class _Generator> +_RandomAccessIterator +__pattern_generate_n(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, Size, _Generator); + +//------------------------------------------------------------------------ +// remove +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _UnaryPredicate> +_ForwardIterator __brick_remove_if(_ForwardIterator, _ForwardIterator, _UnaryPredicate, + /* __is_vector = */ std::false_type) noexcept; + +template <class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator __brick_remove_if(_RandomAccessIterator, _RandomAccessIterator, _UnaryPredicate, + /* __is_vector = */ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +_ForwardIterator +__pattern_remove_if(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _UnaryPredicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__pattern_remove_if(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _UnaryPredicate) noexcept; + +//------------------------------------------------------------------------ +// merge +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_merge(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _OutputIterator, _Compare, + /* __is_vector = */ std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_merge(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _OutputIterator, _Compare, + /* __is_vector = */ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_merge(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _OutputIterator, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _OutputIterator, class _Compare> +_OutputIterator +__pattern_merge(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _OutputIterator, _Compare); + +//------------------------------------------------------------------------ +// inplace_merge +//------------------------------------------------------------------------ + +template <class _BidirectionalIterator, class _Compare> +void __brick_inplace_merge(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, _Compare, + /* __is_vector = */ std::false_type) noexcept; + +template <class _RandomAccessIterator, class _Compare> +void __brick_inplace_merge(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare, + /* __is_vector = */ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _BidirectionalIterator, class _Compare> +void +__pattern_inplace_merge(_Tag, _ExecutionPolicy&&, _BidirectionalIterator, _BidirectionalIterator, + _BidirectionalIterator, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_inplace_merge(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _RandomAccessIterator, _Compare); + +//------------------------------------------------------------------------ +// includes +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare> +bool +__pattern_includes(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Compare> +bool +__pattern_includes(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _Compare); + +//------------------------------------------------------------------------ +// set_union +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_set_union(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _OutputIterator, _Compare, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_set_union(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _OutputIterator, _Compare, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_set_union(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _ForwardIterator2, _OutputIterator, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _OutputIterator, class _Compare> +_OutputIterator +__pattern_set_union(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _OutputIterator, _Compare); + +//------------------------------------------------------------------------ +// set_intersection +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_set_intersection(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _OutputIterator, _Compare, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_set_intersection(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _OutputIterator, _Compare, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_set_intersection(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _ForwardIterator2, _OutputIterator, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _OutputIterator, class _Compare> +_OutputIterator +__pattern_set_intersection(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, + _RandomAccessIterator1, _RandomAccessIterator2, _RandomAccessIterator2, _OutputIterator, + _Compare); + +//------------------------------------------------------------------------ +// set_difference +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_set_difference(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _OutputIterator, _Compare, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_set_difference(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _OutputIterator, _Compare, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_set_difference(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _ForwardIterator2, _OutputIterator, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _OutputIterator, class _Compare> +_OutputIterator +__pattern_set_difference(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _OutputIterator, _Compare); + +//------------------------------------------------------------------------ +// set_symmetric_difference +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_set_symmetric_difference(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _ForwardIterator2, _OutputIterator, _Compare, + /*__is_vector=*/std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _OutputIterator, class _Compare> +_OutputIterator __brick_set_symmetric_difference(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _OutputIterator, _Compare, + /*__is_vector=*/std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_set_symmetric_difference(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _ForwardIterator2, _OutputIterator, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _OutputIterator, class _Compare> +_OutputIterator +__pattern_set_symmetric_difference(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, + _RandomAccessIterator1, _RandomAccessIterator2, _RandomAccessIterator2, + _OutputIterator, _Compare); + +//------------------------------------------------------------------------ +// is_heap_until +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator, class _Compare> +_RandomAccessIterator __brick_is_heap_until(_RandomAccessIterator, _RandomAccessIterator, _Compare, + /* __is_vector = */ std::false_type) noexcept; + +template <class _RandomAccessIterator, class _Compare> +_RandomAccessIterator __brick_is_heap_until(_RandomAccessIterator, _RandomAccessIterator, _Compare, + /* __is_vector = */ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +_RandomAccessIterator +__pattern_is_heap_until(_Tag, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +_RandomAccessIterator +__pattern_is_heap_until(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Compare) noexcept; + +//------------------------------------------------------------------------ +// min_element +//------------------------------------------------------------------------ + +template <typename _ForwardIterator, typename _Compare> +_ForwardIterator __brick_min_element(_ForwardIterator, _ForwardIterator, _Compare, + /* __is_vector = */ std::false_type) noexcept; + +template <typename _RandomAccessIterator, typename _Compare> +_RandomAccessIterator __brick_min_element(_RandomAccessIterator, _RandomAccessIterator, _Compare, + /* __is_vector = */ std::true_type) noexcept; + +template <typename _Tag, typename _ExecutionPolicy, typename _ForwardIterator, typename _Compare> +_ForwardIterator +__pattern_min_element(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Compare) noexcept; + +template <typename _IsVector, typename _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare> +_RandomAccessIterator +__pattern_min_element(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Compare); + +//------------------------------------------------------------------------ +// minmax_element +//------------------------------------------------------------------------ + +template <typename _ForwardIterator, typename _Compare> +std::pair<_ForwardIterator, _ForwardIterator> __brick_minmax_element(_ForwardIterator, _ForwardIterator, _Compare, + /* __is_vector = */ std::false_type) noexcept; + +template <typename _RandomAccessIterator, typename _Compare> +std::pair<_RandomAccessIterator, _RandomAccessIterator> + __brick_minmax_element(_RandomAccessIterator, _RandomAccessIterator, _Compare, + /* __is_vector = */ std::true_type) noexcept; + +template <typename _Tag, typename _ExecutionPolicy, typename _ForwardIterator, typename _Compare> +std::pair<_ForwardIterator, _ForwardIterator> +__pattern_minmax_element(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Compare) noexcept; + +template <typename _IsVector, typename _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare> +std::pair<_RandomAccessIterator, _RandomAccessIterator> +__pattern_minmax_element(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Compare); + +//------------------------------------------------------------------------ +// mismatch +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _Predicate> +std::pair<_ForwardIterator1, _ForwardIterator2> __brick_mismatch(_ForwardIterator1, _ForwardIterator1, + _ForwardIterator2, _ForwardIterator2, _Predicate, + /* __is_vector = */ std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Predicate> +std::pair<_RandomAccessIterator1, _RandomAccessIterator2> + __brick_mismatch(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _RandomAccessIterator2, + _Predicate, + /* __is_vector = */ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate> +std::pair<_ForwardIterator1, _ForwardIterator2> +__pattern_mismatch(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _Predicate) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Predicate> +std::pair<_RandomAccessIterator1, _RandomAccessIterator2> +__pattern_mismatch(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, _RandomAccessIterator1, + _RandomAccessIterator2, _RandomAccessIterator2, _Predicate) noexcept; + +//------------------------------------------------------------------------ +// lexicographical_compare +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _Compare> +bool __brick_lexicographical_compare(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, + _Compare, + /* __is_vector = */ std::false_type) noexcept; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Compare> +bool __brick_lexicographical_compare(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, + _RandomAccessIterator2, _Compare, + /* __is_vector = */ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare> +bool +__pattern_lexicographical_compare(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, + _ForwardIterator2, _Compare) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Compare> +bool +__pattern_lexicographical_compare(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, + _RandomAccessIterator1, _RandomAccessIterator2, _RandomAccessIterator2, + _Compare) noexcept; + +} // namespace __internal +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_ALGORITHM_FWD_H */ diff --git a/pstl/include/pstl/internal/algorithm_impl.h b/pstl/include/pstl/internal/algorithm_impl.h new file mode 100644 index 0000000..2b505d9 --- /dev/null +++ b/pstl/include/pstl/internal/algorithm_impl.h @@ -0,0 +1,3819 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_ALGORITHM_IMPL_H +#define _PSTL_ALGORITHM_IMPL_H + +#include <iterator> +#include <type_traits> +#include <utility> +#include <functional> +#include <algorithm> + +#include "execution_impl.h" +#include "memory_impl.h" +#include "parallel_backend.h" +#include "parallel_backend_utils.h" +#include "parallel_impl.h" +#include "pstl_config.h" +#include "unseq_backend_simd.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __internal +{ + +//------------------------------------------------------------------------ +// any_of +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Pred> +bool +__brick_any_of(const _ForwardIterator __first, const _ForwardIterator __last, _Pred __pred, + /*__is_vector=*/std::false_type) noexcept +{ + return std::any_of(__first, __last, __pred); +}; + +template <class _RandomAccessIterator, class _Pred> +bool +__brick_any_of(const _RandomAccessIterator __first, const _RandomAccessIterator __last, _Pred __pred, + /*__is_vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_or(__first, __last - __first, __pred); +}; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Pred> +bool +__pattern_any_of(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Pred __pred) noexcept +{ + return __internal::__brick_any_of(__first, __last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Pred> +bool +__pattern_any_of(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Pred __pred) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + return __internal::__parallel_or(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__pred](_RandomAccessIterator __i, _RandomAccessIterator __j) + { return __internal::__brick_any_of(__i, __j, __pred, _IsVector{}); }); + }); +} + +// [alg.foreach] +// for_each_n with no policy + +template <class _ForwardIterator, class _Size, class _Function> +_ForwardIterator +__for_each_n_it_serial(_ForwardIterator __first, _Size __n, _Function __f) +{ + for (; __n > 0; ++__first, --__n) + __f(__first); + return __first; +} + +//------------------------------------------------------------------------ +// walk1 (pseudo) +// +// walk1 evaluates f(x) for each dereferenced value x drawn from [first,last) +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _Function> +void +__brick_walk1(_ForwardIterator __first, _ForwardIterator __last, _Function __f, /*vector=*/std::false_type) noexcept +{ + std::for_each(__first, __last, __f); +} + +template <class _RandomAccessIterator, class _Function> +void +__brick_walk1(_RandomAccessIterator __first, _RandomAccessIterator __last, _Function __f, + /*vector=*/std::true_type) noexcept +{ + __unseq_backend::__simd_walk_1(__first, __last - __first, __f); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Function> +void +__pattern_walk1(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Function __f) noexcept +{ + __internal::__brick_walk1(__first, __last, __f, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Function> +void +__pattern_walk1(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Function __f) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + __internal::__except_handler( + [&]() + { + __par_backend::__parallel_for(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__f](_RandomAccessIterator __i, _RandomAccessIterator __j) + { __internal::__brick_walk1(__i, __j, __f, _IsVector{}); }); + }); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Brick> +void +__pattern_walk_brick(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _Brick __brick) noexcept +{ + __brick(__first, __last); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Brick> +void +__pattern_walk_brick(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Brick __brick) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + __internal::__except_handler( + [&]() + { + __par_backend::__parallel_for(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__brick](_RandomAccessIterator __i, _RandomAccessIterator __j) + { __brick(__i, __j); }); + }); +} + +//------------------------------------------------------------------------ +// walk1_n +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _Size, class _Function> +_ForwardIterator +__brick_walk1_n(_ForwardIterator __first, _Size __n, _Function __f, /*_IsVectorTag=*/std::false_type) +{ + return __internal::__for_each_n_it_serial(__first, __n, + [&__f](_ForwardIterator __it) { __f(*__it); }); // calling serial version +} + +template <class _RandomAccessIterator, class _DifferenceType, class _Function> +_RandomAccessIterator +__brick_walk1_n(_RandomAccessIterator __first, _DifferenceType __n, _Function __f, + /*vectorTag=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_walk_1(__first, __n, __f); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Function> +_ForwardIterator +__pattern_walk1_n(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _Size __n, _Function __f) noexcept +{ + return __internal::__brick_walk1_n(__first, __n, __f, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Function> +_RandomAccessIterator +__pattern_walk1_n(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, _Size __n, + _Function __f) +{ + __internal::__pattern_walk1(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __first + __n, __f); + + return __first + __n; +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Brick> +_ForwardIterator +__pattern_walk_brick_n(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _Size __n, _Brick __brick) noexcept +{ + return __brick(__first, __n); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Brick> +_RandomAccessIterator +__pattern_walk_brick_n(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _Size __n, _Brick __brick) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __first + __n, + [__brick](_RandomAccessIterator __i, _RandomAccessIterator __j) { __brick(__i, __j - __i); }); + return __first + __n; + }); +} + +//------------------------------------------------------------------------ +// walk2 (pseudo) +// +// walk2 evaluates f(x,y) for deferenced values (x,y) drawn from [first1,last1) and [first2,...) +//------------------------------------------------------------------------ +template <class _ForwardIterator1, class _ForwardIterator2, class _Function> +_ForwardIterator2 +__brick_walk2(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _Function __f, + /*vector=*/std::false_type) noexcept +{ + for (; __first1 != __last1; ++__first1, ++__first2) + __f(*__first1, *__first2); + return __first2; +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Function> +_RandomAccessIterator2 +__brick_walk2(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _Function __f, + /*vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_walk_2(__first1, __last1 - __first1, __first2, __f); +} + +template <class _ForwardIterator1, class _Size, class _ForwardIterator2, class _Function> +_ForwardIterator2 +__brick_walk2_n(_ForwardIterator1 __first1, _Size __n, _ForwardIterator2 __first2, _Function __f, + /*vector=*/std::false_type) noexcept +{ + for (; __n > 0; --__n, ++__first1, ++__first2) + __f(*__first1, *__first2); + return __first2; +} + +template <class _RandomAccessIterator1, class _Size, class _RandomAccessIterator2, class _Function> +_RandomAccessIterator2 +__brick_walk2_n(_RandomAccessIterator1 __first1, _Size __n, _RandomAccessIterator2 __first2, _Function __f, + /*vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_walk_2(__first1, __n, __first2, __f); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Function> +_ForwardIterator2 +__pattern_walk2(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _Function __f) noexcept +{ + return __internal::__brick_walk2(__first1, __last1, __first2, __f, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Function> +_RandomAccessIterator2 +__pattern_walk2(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _Function __f) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + [__f, __first1, __first2](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) + { __internal::__brick_walk2(__i, __j, __first2 + (__i - __first1), __f, _IsVector{}); }); + return __first2 + (__last1 - __first1); + }); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2, + class _Function> +_ForwardIterator2 +__pattern_walk2_n(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _Size __n, _ForwardIterator2 __first2, + _Function __f) noexcept +{ + return __internal::__brick_walk2_n(__first1, __n, __first2, __f, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _Size, + class _RandomAccessIterator2, class _Function> +_RandomAccessIterator2 +__pattern_walk2_n(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _Size __n, _RandomAccessIterator2 __first2, _Function __f) +{ + return __internal::__pattern_walk2(__tag, std::forward<_ExecutionPolicy>(__exec), __first1, __first1 + __n, + __first2, __f); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Brick> +_ForwardIterator2 +__pattern_walk2_brick(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _Brick __brick) noexcept +{ + return __brick(__first1, __last1, __first2); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Brick> +_RandomAccessIterator2 +__pattern_walk2_brick(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _Brick __brick) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + [__first1, __first2, __brick](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) + { __brick(__i, __j, __first2 + (__i - __first1)); }); + return __first2 + (__last1 - __first1); + }); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2, + class _Brick> +_ForwardIterator2 +__pattern_walk2_brick_n(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _Size __n, _ForwardIterator2 __first2, + _Brick __brick) noexcept +{ + return __brick(__first1, __n, __first2); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _Size, + class _RandomAccessIterator2, class _Brick> +_RandomAccessIterator2 +__pattern_walk2_brick_n(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _Size __n, _RandomAccessIterator2 __first2, _Brick __brick) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __first1 + __n, + [__first1, __first2, __brick](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) + { __brick(__i, __j - __i, __first2 + (__i - __first1)); }); + return __first2 + __n; + }); +} + +//------------------------------------------------------------------------ +// walk3 (pseudo) +// +// walk3 evaluates f(x,y,z) for (x,y,z) drawn from [first1,last1), [first2,...), [first3,...) +//------------------------------------------------------------------------ +template <class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator3, class _Function> +_ForwardIterator3 +__brick_walk3(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator3 __first3, _Function __f, /*vector=*/std::false_type) noexcept +{ + for (; __first1 != __last1; ++__first1, ++__first2, ++__first3) + __f(*__first1, *__first2, *__first3); + return __first3; +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, class _Function> +_RandomAccessIterator3 +__brick_walk3(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator3 __first3, _Function __f, /*vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_walk_3(__first1, __last1 - __first1, __first2, __first3, __f); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator3, + class _Function> +_ForwardIterator3 +__pattern_walk3(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator3 __first3, _Function __f) noexcept +{ + return __internal::__brick_walk3(__first1, __last1, __first2, __first3, __f, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _RandomAccessIterator3, class _Function> +_RandomAccessIterator3 +__pattern_walk3(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _RandomAccessIterator3 __first3, + _Function __f) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + [__f, __first1, __first2, __first3](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) { + __internal::__brick_walk3(__i, __j, __first2 + (__i - __first1), __first3 + (__i - __first1), __f, + _IsVector{}); + }); + return __first3 + (__last1 - __first1); + }); +} + +//------------------------------------------------------------------------ +// equal +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool +__brick_equal(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _BinaryPredicate __p, /* IsVector = */ std::false_type) noexcept +{ + return std::equal(__first1, __last1, __first2, __last2, __p); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +bool +__brick_equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _BinaryPredicate __p, /* is_vector = */ std::true_type) noexcept +{ + if (__last1 - __first1 != __last2 - __first2) + return false; + + return __unseq_backend::__simd_first(__first1, __last1 - __first1, __first2, std::not_fn(__p)).first == __last1; +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool +__pattern_equal(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __p) noexcept +{ + return __internal::__brick_equal(__first1, __last1, __first2, __last2, __p, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +bool +__pattern_equal(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _BinaryPredicate __p) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (__last1 - __first1 != __last2 - __first2) + return false; + + return __internal::__except_handler( + [&]() + { + return !__internal::__parallel_or( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + [__first1, __first2, __p](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) + { + return !__internal::__brick_equal(__i, __j, __first2 + (__i - __first1), + __first2 + (__j - __first1), __p, _IsVector{}); + }); + }); +} + +//------------------------------------------------------------------------ +// equal version for sequences with equal length +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool +__brick_equal(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _BinaryPredicate __p, + /* IsVector = */ std::false_type) noexcept +{ + return std::equal(__first1, __last1, __first2, __p); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +bool +__brick_equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _BinaryPredicate __p, /* is_vector = */ std::true_type) noexcept +{ + return __unseq_backend::__simd_first(__first1, __last1 - __first1, __first2, std::not_fn(__p)).first == __last1; +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool +__pattern_equal(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _BinaryPredicate __p) noexcept +{ + return __internal::__brick_equal(__first1, __last1, __first2, __p, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +bool +__pattern_equal(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _BinaryPredicate __p) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + return !__internal::__parallel_or( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + [__first1, __first2, __p](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) + { return !__internal::__brick_equal(__i, __j, __first2 + (__i - __first1), __p, _IsVector{}); }); + }); +} + +//------------------------------------------------------------------------ +// find_if +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _Predicate> +_ForwardIterator +__brick_find_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, + /*is_vector=*/std::false_type) noexcept +{ + return std::find_if(__first, __last, __pred); +} + +template <class _RandomAccessIterator, class _Predicate> +_RandomAccessIterator +__brick_find_if(_RandomAccessIterator __first, _RandomAccessIterator __last, _Predicate __pred, + /*is_vector=*/std::true_type) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _SizeType; + return __unseq_backend::__simd_first( + __first, _SizeType(0), __last - __first, + [&__pred](_RandomAccessIterator __it, _SizeType __i) { return __pred(__it[__i]); }); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +_ForwardIterator +__pattern_find_if(_Tag __tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _Predicate __pred) noexcept +{ + return __internal::__brick_find_if(__first, __last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Predicate> +_RandomAccessIterator +__pattern_find_if(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Predicate __pred) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + return __internal::__parallel_find( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__pred](_RandomAccessIterator __i, _RandomAccessIterator __j) + { return __internal::__brick_find_if(__i, __j, __pred, _IsVector{}); }, + std::less<typename std::iterator_traits<_RandomAccessIterator>::difference_type>(), + /*is_first=*/true); + }); +} + +//------------------------------------------------------------------------ +// find_end +//------------------------------------------------------------------------ + +// find the first occurrence of the subsequence [s_first, s_last) +// or the last occurrence of the subsequence in the range [first, last) +// b_first determines what occurrence we want to find (first or last) +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate, class _IsVector> +_RandomAccessIterator1 +__find_subrange(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator1 __global_last, + _RandomAccessIterator2 __s_first, _RandomAccessIterator2 __s_last, _BinaryPredicate __pred, + bool __b_first, _IsVector __is_vector) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator2>::value_type _ValueType; + auto __n2 = __s_last - __s_first; + if (__n2 < 1) + { + return __b_first ? __first : __last; + } + + auto __n1 = __global_last - __first; + if (__n1 < __n2) + { + return __last; + } + + auto __cur = __last; + while (__first != __last && (__global_last - __first >= __n2)) + { + // find position of *s_first in [first, last) (it can be start of subsequence) + __first = __internal::__brick_find_if( + __first, __last, __equal_value_by_pred<_ValueType, _BinaryPredicate>(*__s_first, __pred), __is_vector); + + // if position that was found previously is the start of subsequence + // then we can exit the loop (b_first == true) or keep the position + // (b_first == false) + if (__first != __last && (__global_last - __first >= __n2) && + __internal::__brick_equal(__s_first + 1, __s_last, __first + 1, __pred, __is_vector)) + { + if (__b_first) + { + return __first; + } + else + { + __cur = __first; + } + } + else if (__first == __last) + { + break; + } + else + { + } + + // in case of b_first == false we try to find new start position + // for the next subsequence + ++__first; + } + return __cur; +} + +template <class _RandomAccessIterator, class _Size, class _Tp, class _BinaryPredicate, class _IsVector> +_RandomAccessIterator +__find_subrange(_RandomAccessIterator __first, _RandomAccessIterator __last, _RandomAccessIterator __global_last, + _Size __count, const _Tp& __value, _BinaryPredicate __pred, _IsVector __is_vector) noexcept +{ + if (static_cast<_Size>(__global_last - __first) < __count || __count < 1) + { + return __last; // According to the standard last shall be returned when count < 1 + } + + auto __unary_pred = __equal_value_by_pred<_Tp, _BinaryPredicate>(__value, __pred); + while (__first != __last && (static_cast<_Size>(__global_last - __first) >= __count)) + { + __first = __internal::__brick_find_if(__first, __last, __unary_pred, __is_vector); + + // check that all of elements in [first+1, first+count) equal to value + if (__first != __last && (static_cast<_Size>(__global_last - __first) >= __count) && + !__internal::__brick_any_of(__first + 1, __first + __count, std::not_fn(__unary_pred), __is_vector)) + { + return __first; + } + else if (__first == __last) + { + break; + } + else + { + ++__first; + } + } + return __last; +} + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__brick_find_end(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last, _BinaryPredicate __pred, /*__is_vector=*/std::false_type) noexcept +{ + return std::find_end(__first, __last, __s_first, __s_last, __pred); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +_RandomAccessIterator1 +__brick_find_end(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __s_first, + _RandomAccessIterator2 __s_last, _BinaryPredicate __pred, /*__is_vector=*/std::true_type) noexcept +{ + return __find_subrange(__first, __last, __last, __s_first, __s_last, __pred, false, std::true_type()); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__pattern_find_end(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred) noexcept +{ + return __internal::__brick_find_end(__first, __last, __s_first, __s_last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +_RandomAccessIterator1 +__pattern_find_end(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __last, _RandomAccessIterator2 __s_first, _RandomAccessIterator2 __s_last, + _BinaryPredicate __pred) noexcept +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (__last - __first == __s_last - __s_first) + { + const bool __res = __internal::__pattern_equal(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __s_first, __pred); + return __res ? __first : __last; + } + else + { + return __internal::__except_handler( + [&]() + { + return __internal::__parallel_find( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__last, __s_first, __s_last, __pred](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) { + return __internal::__find_subrange(__i, __j, __last, __s_first, __s_last, __pred, false, + _IsVector{}); + }, + std::greater<typename std::iterator_traits<_RandomAccessIterator1>::difference_type>(), + /*is_first=*/false); + }); + } +} + +//------------------------------------------------------------------------ +// find_first_of +//------------------------------------------------------------------------ +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__brick_find_first_of(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last, _BinaryPredicate __pred, /*__is_vector=*/std::false_type) noexcept +{ + return std::find_first_of(__first, __last, __s_first, __s_last, __pred); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +_RandomAccessIterator1 +__brick_find_first_of(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __s_first, + _RandomAccessIterator2 __s_last, _BinaryPredicate __pred, /*__is_vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_find_first_of(__first, __last, __s_first, __s_last, __pred); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__pattern_find_first_of(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred) noexcept +{ + return __internal::__brick_find_first_of(__first, __last, __s_first, __s_last, __pred, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +_RandomAccessIterator1 +__pattern_find_first_of(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __last, _RandomAccessIterator2 __s_first, + _RandomAccessIterator2 __s_last, _BinaryPredicate __pred) noexcept +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + return __internal::__parallel_find( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__s_first, __s_last, __pred](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) + { return __internal::__brick_find_first_of(__i, __j, __s_first, __s_last, __pred, _IsVector{}); }, + std::less<typename std::iterator_traits<_RandomAccessIterator1>::difference_type>(), /*is_first=*/true); + }); +} + +//------------------------------------------------------------------------ +// search +//------------------------------------------------------------------------ +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +_RandomAccessIterator1 +__brick_search(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __s_first, + _RandomAccessIterator2 __s_last, _BinaryPredicate __pred, /*vector=*/std::false_type) noexcept +{ + return std::search(__first, __last, __s_first, __s_last, __pred); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +_RandomAccessIterator1 +__brick_search(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __s_first, + _RandomAccessIterator2 __s_last, _BinaryPredicate __pred, /*vector=*/std::true_type) noexcept +{ + return __internal::__find_subrange(__first, __last, __last, __s_first, __s_last, __pred, true, std::true_type()); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__pattern_search(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred) noexcept +{ + return __internal::__brick_search(__first, __last, __s_first, __s_last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +_RandomAccessIterator1 +__pattern_search(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __last, _RandomAccessIterator2 __s_first, _RandomAccessIterator2 __s_last, + _BinaryPredicate __pred) noexcept +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (__last - __first == __s_last - __s_first) + { + const bool __res = __internal::__pattern_equal(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __s_first, __pred); + return __res ? __first : __last; + } + else + { + return __internal::__except_handler( + [&]() + { + return __internal::__parallel_find( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__last, __s_first, __s_last, __pred](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) { + return __internal::__find_subrange(__i, __j, __last, __s_first, __s_last, __pred, true, + _IsVector{}); + }, + std::less<typename std::iterator_traits<_RandomAccessIterator1>::difference_type>(), + /*is_first=*/true); + }); + } +} + +//------------------------------------------------------------------------ +// search_n +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> +_ForwardIterator +__brick_search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value, + _BinaryPredicate __pred, /*vector=*/std::false_type) noexcept +{ + return std::search_n(__first, __last, __count, __value, __pred); +} + +template <class _RandomAccessIterator, class _Size, class _Tp, class _BinaryPredicate> +_RandomAccessIterator +__brick_search_n(_RandomAccessIterator __first, _RandomAccessIterator __last, _Size __count, const _Tp& __value, + _BinaryPredicate __pred, /*vector=*/std::true_type) noexcept +{ + return __internal::__find_subrange(__first, __last, __last, __count, __value, __pred, std::true_type()); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> +_ForwardIterator +__pattern_search_n(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Size __count, + const _Tp& __value, _BinaryPredicate __pred) noexcept +{ + return __internal::__brick_search_n(__first, __last, __count, __value, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Tp, + class _BinaryPredicate> +_RandomAccessIterator +__pattern_search_n(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Size __count, const _Tp& __value, _BinaryPredicate __pred) noexcept +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (static_cast<_Size>(__last - __first) == __count) + { + const bool __result = + !__internal::__pattern_any_of(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [&__value, &__pred](const _Tp& __val) { return !__pred(__val, __value); }); + return __result ? __first : __last; + } + else + { + return __internal::__except_handler( + [&__exec, __first, __last, __count, &__value, __pred]() + { + return __internal::__parallel_find( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__last, __count, &__value, __pred](_RandomAccessIterator __i, _RandomAccessIterator __j) + { return __internal::__find_subrange(__i, __j, __last, __count, __value, __pred, _IsVector{}); }, + std::less<typename std::iterator_traits<_RandomAccessIterator>::difference_type>(), + /*is_first=*/true); + }); + } +} + +//------------------------------------------------------------------------ +// copy_n +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Size, class _OutputIterator> +_OutputIterator +__brick_copy_n(_ForwardIterator __first, _Size __n, _OutputIterator __result, /*vector=*/std::false_type) noexcept +{ + return std::copy_n(__first, __n, __result); +} + +template <class _RandomAccessIterator1, class _Size, class _RandomAccessIterator2> +_RandomAccessIterator2 +__brick_copy_n(_RandomAccessIterator1 __first, _Size __n, _RandomAccessIterator2 __result, + /*vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_assign( + __first, __n, __result, + [](_RandomAccessIterator1 __first, _RandomAccessIterator2 __result) { *__result = *__first; }); +} + +//------------------------------------------------------------------------ +// copy +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _OutputIterator> +_OutputIterator +__brick_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, + /*vector=*/std::false_type) noexcept +{ + return std::copy(__first, __last, __result); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator2 +__brick_copy(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, + /*vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_assign( + __first, __last - __first, __result, + [](_RandomAccessIterator1 __first, _RandomAccessIterator2 __result) { *__result = *__first; }); +} + +//------------------------------------------------------------------------ +// move +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _OutputIterator> +_OutputIterator +__brick_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, + /*vector=*/std::false_type) noexcept +{ + return std::move(__first, __last, __result); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator2 +__brick_move(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, + /*vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_assign( + __first, __last - __first, __result, + [](_RandomAccessIterator1 __first, _RandomAccessIterator2 __result) { *__result = std::move(*__first); }); +} + +struct __brick_move_destroy +{ + template <typename _RandomAccessIterator1, typename _RandomAccessIterator2> + _RandomAccessIterator2 + operator()(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, + /*vec*/ std::true_type) const + { + using _IteratorValueType = typename std::iterator_traits<_RandomAccessIterator1>::value_type; + + return __unseq_backend::__simd_assign(__first, __last - __first, __result, + [](_RandomAccessIterator1 __first, _RandomAccessIterator2 __result) { + *__result = std::move(*__first); + (*__first).~_IteratorValueType(); + }); + } + + template <typename _RandomAccessIterator1, typename _RandomAccessIterator2> + _RandomAccessIterator2 + operator()(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, + /*vec*/ std::false_type) const + { + using _IteratorValueType = typename std::iterator_traits<_RandomAccessIterator1>::value_type; + + for (; __first != __last; ++__first, ++__result) + { + *__result = std::move(*__first); + (*__first).~_IteratorValueType(); + } + return __result; + } +}; + +//------------------------------------------------------------------------ +// swap_ranges +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _OutputIterator> +_OutputIterator +__brick_swap_ranges(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, + /*vector=*/std::false_type) noexcept +{ + return std::swap_ranges(__first, __last, __result); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator2 +__brick_swap_ranges(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, + /*vector=*/std::true_type) noexcept +{ + using std::iter_swap; + return __unseq_backend::__simd_assign(__first, __last - __first, __result, + iter_swap<_RandomAccessIterator1, _RandomAccessIterator2>); +} + +//------------------------------------------------------------------------ +// copy_if +//------------------------------------------------------------------------ +template <class _ForwardIterator, class _OutputIterator, class _UnaryPredicate> +_OutputIterator +__brick_copy_if(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _UnaryPredicate __pred, + /*vector=*/std::false_type) noexcept +{ + return std::copy_if(__first, __last, __result, __pred); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _UnaryPredicate> +_RandomAccessIterator2 +__brick_copy_if(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, + _UnaryPredicate __pred, + /*vector=*/std::true_type) noexcept +{ +#if defined(_PSTL_MONOTONIC_PRESENT) + return __unseq_backend::__simd_copy_if(__first, __last - __first, __result, __pred); +#else + return std::copy_if(__first, __last, __result, __pred); +#endif +} + +// TODO: Try to use transform_reduce for combining __brick_copy_if_phase1 on IsVector. +template <class _DifferenceType, class _ForwardIterator, class _UnaryPredicate> +std::pair<_DifferenceType, _DifferenceType> +__brick_calc_mask_1(_ForwardIterator __first, _ForwardIterator __last, bool* __restrict __mask, _UnaryPredicate __pred, + /*vector=*/std::false_type) noexcept +{ + auto __count_true = _DifferenceType(0); + auto __size = __last - __first; + + static_assert(__are_random_access_iterators<_ForwardIterator>::value, + "Pattern-brick error. Should be a random access iterator."); + + for (; __first != __last; ++__first, ++__mask) + { + *__mask = __pred(*__first); + if (*__mask) + { + ++__count_true; + } + } + return std::make_pair(__count_true, __size - __count_true); +} + +template <class _DifferenceType, class _RandomAccessIterator, class _UnaryPredicate> +std::pair<_DifferenceType, _DifferenceType> +__brick_calc_mask_1(_RandomAccessIterator __first, _RandomAccessIterator __last, bool* __mask, _UnaryPredicate __pred, + /*vector=*/std::true_type) noexcept +{ + auto __result = __unseq_backend::__simd_calc_mask_1(__first, __last - __first, __mask, __pred); + return std::make_pair(__result, (__last - __first) - __result); +} + +template <class _ForwardIterator, class _OutputIterator, class _Assigner> +void +__brick_copy_by_mask(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, bool* __mask, + _Assigner __assigner, /*vector=*/std::false_type) noexcept +{ + for (; __first != __last; ++__first, ++__mask) + { + if (*__mask) + { + __assigner(__first, __result); + ++__result; + } + } +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Assigner> +void +__brick_copy_by_mask(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, + bool* __restrict __mask, _Assigner __assigner, /*vector=*/std::true_type) noexcept +{ +#if defined(_PSTL_MONOTONIC_PRESENT) + __unseq_backend::__simd_copy_by_mask(__first, __last - __first, __result, __mask, __assigner); +#else + __internal::__brick_copy_by_mask(__first, __last, __result, __mask, __assigner, std::false_type()); +#endif +} + +template <class _ForwardIterator, class _OutputIterator1, class _OutputIterator2> +void +__brick_partition_by_mask(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator1 __out_true, + _OutputIterator2 __out_false, bool* __mask, /*vector=*/std::false_type) noexcept +{ + for (; __first != __last; ++__first, ++__mask) + { + if (*__mask) + { + *__out_true = *__first; + ++__out_true; + } + else + { + *__out_false = *__first; + ++__out_false; + } + } +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3> +void +__brick_partition_by_mask(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, + _RandomAccessIterator2 __out_true, _RandomAccessIterator3 __out_false, bool* __mask, + /*vector=*/std::true_type) noexcept +{ +#if defined(_PSTL_MONOTONIC_PRESENT) + __unseq_backend::__simd_partition_by_mask(__first, __last - __first, __out_true, __out_false, __mask); +#else + __internal::__brick_partition_by_mask(__first, __last, __out_true, __out_false, __mask, std::false_type()); +#endif +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _UnaryPredicate> +_OutputIterator +__pattern_copy_if(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, + _UnaryPredicate __pred) noexcept +{ + return __internal::__brick_copy_if(__first, __last, __result, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _UnaryPredicate> +_RandomAccessIterator2 +__pattern_copy_if(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, _UnaryPredicate __pred) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType; + const _DifferenceType __n = __last - __first; + if (_DifferenceType(1) < __n) + { + __par_backend::__buffer<bool> __mask_buf(__n); + return __internal::__except_handler( + [&__exec, __n, __first, __result, __pred, &__mask_buf]() + { + bool* __mask = __mask_buf.get(); + _DifferenceType __m{}; + __par_backend::__parallel_strict_scan( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __n, _DifferenceType(0), + [=](_DifferenceType __i, _DifferenceType __len) { // Reduce + return __internal::__brick_calc_mask_1<_DifferenceType>(__first + __i, __first + (__i + __len), + __mask + __i, __pred, _IsVector{}) + .first; + }, + std::plus<_DifferenceType>(), // Combine + [=](_DifferenceType __i, _DifferenceType __len, _DifferenceType __initial) { // Scan + __internal::__brick_copy_by_mask( + __first + __i, __first + (__i + __len), __result + __initial, __mask + __i, + [](_RandomAccessIterator1 __x, _RandomAccessIterator2 __z) { *__z = *__x; }, _IsVector{}); + }, + [&__m](_DifferenceType __total) { __m = __total; }); + return __result + __m; + }); + } + // trivial sequence - use serial algorithm + return __internal::__brick_copy_if(__first, __last, __result, __pred, _IsVector{}); +} + +//------------------------------------------------------------------------ +// count +//------------------------------------------------------------------------ +template <class _RandomAccessIterator, class _Predicate> +typename std::iterator_traits<_RandomAccessIterator>::difference_type +__brick_count(_RandomAccessIterator __first, _RandomAccessIterator __last, _Predicate __pred, + /* is_vector = */ std::true_type) noexcept +{ + return __unseq_backend::__simd_count(__first, __last - __first, __pred); +} + +template <class _ForwardIterator, class _Predicate> +typename std::iterator_traits<_ForwardIterator>::difference_type +__brick_count(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, + /* is_vector = */ std::false_type) noexcept +{ + return std::count_if(__first, __last, __pred); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +typename std::iterator_traits<_ForwardIterator>::difference_type +__pattern_count(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) noexcept +{ + return __internal::__brick_count(__first, __last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Predicate> +typename std::iterator_traits<_RandomAccessIterator>::difference_type +__pattern_count(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Predicate __pred) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _SizeType; + return __internal::__except_handler( + [&]() + { + return __par_backend::__parallel_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, _SizeType(0), + [__pred](_RandomAccessIterator __begin, _RandomAccessIterator __end, _SizeType __value) -> _SizeType + { return __value + __internal::__brick_count(__begin, __end, __pred, _IsVector{}); }, + std::plus<_SizeType>()); + }); +} + +//------------------------------------------------------------------------ +// unique +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator +__brick_unique(_RandomAccessIterator __first, _RandomAccessIterator __last, _BinaryPredicate __pred, + /*is_vector=*/std::false_type) noexcept +{ + return std::unique(__first, __last, __pred); +} + +template <class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator +__brick_unique(_RandomAccessIterator __first, _RandomAccessIterator __last, _BinaryPredicate __pred, + /*is_vector=*/std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial"); + return std::unique(__first, __last, __pred); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate> +_ForwardIterator +__pattern_unique(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _BinaryPredicate __pred) noexcept +{ + return __internal::__brick_unique(__first, __last, __pred, typename _Tag::__is_vector{}); +} + +// That function is shared between two algorithms - remove_if (__pattern_remove_if) and unique (pattern unique). But a mask calculation is different. +// So, a caller passes _CalcMask brick into remove_elements. +template <class _IsVector, class _ExecutionPolicy, class _ForwardIterator, class _CalcMask> +_ForwardIterator +__remove_elements(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _ForwardIterator __first, + _ForwardIterator __last, _CalcMask __calc_mask) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_ForwardIterator>::difference_type _DifferenceType; + typedef typename std::iterator_traits<_ForwardIterator>::value_type _Tp; + _DifferenceType __n = __last - __first; + __par_backend::__buffer<bool> __mask_buf(__n); + // 1. find a first iterator that should be removed + return __internal::__except_handler([&]() { + bool* __mask = __mask_buf.get(); + _DifferenceType __min = __par_backend::__parallel_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), _DifferenceType(0), __n, __n, + [__first, __mask, &__calc_mask](_DifferenceType __i, _DifferenceType __j, + _DifferenceType __local_min) -> _DifferenceType + { + // Create mask + __calc_mask(__mask + __i, __mask + __j, __first + __i); + + // if minimum was found in a previous range we shouldn't do anymore + if (__local_min < __i) + { + return __local_min; + } + // find first iterator that should be removed + bool* __result = __internal::__brick_find_if( + __mask + __i, __mask + __j, [](bool __val) { return !__val; }, _IsVector{}); + if (__result - __mask == __j) + { + return __local_min; + } + return std::min(__local_min, _DifferenceType(__result - __mask)); + }, + [](_DifferenceType __local_min1, _DifferenceType __local_min2) -> _DifferenceType + { return std::min(__local_min1, __local_min2); }); + + // No elements to remove - exit + if (__min == __n) + { + return __last; + } + __n -= __min; + __first += __min; + + __par_backend::__buffer<_Tp> __buf(__n); + _Tp* __result = __buf.get(); + __mask += __min; + _DifferenceType __m{}; + // 2. Elements that doesn't satisfy pred are moved to result + __par_backend::__parallel_strict_scan( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __n, _DifferenceType(0), + [__mask](_DifferenceType __i, _DifferenceType __len) + { + return __internal::__brick_count( + __mask + __i, __mask + __i + __len, [](bool __val) { return __val; }, _IsVector{}); + }, + std::plus<_DifferenceType>(), + [=](_DifferenceType __i, _DifferenceType __len, _DifferenceType __initial) + { + __internal::__brick_copy_by_mask( + __first + __i, __first + __i + __len, __result + __initial, __mask + __i, + [](_ForwardIterator __x, _Tp* __z) + { + __internal::__invoke_if_else( + std::is_trivial<_Tp>(), [&]() { *__z = std::move(*__x); }, + [&]() { ::new (std::addressof(*__z)) _Tp(std::move(*__x)); }); + }, + _IsVector{}); + }, + [&__m](_DifferenceType __total) { __m = __total; }); + + // 3. Elements from result are moved to [first, last) + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __result, __result + __m, + [__result, __first](_Tp* __i, _Tp* __j) + { + __invoke_if_else( + std::is_trivial<_Tp>(), [&]() { __brick_move(__i, __j, __first + (__i - __result), _IsVector{}); }, + [&]() { __brick_move_destroy()(__i, __j, __first + (__i - __result), _IsVector{}); }); + }); + return __first + __m; + }); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator +__pattern_unique(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _BinaryPredicate __pred) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::reference _ReferenceType; + + if (__first == __last) + { + return __last; + } + if (__first + 1 == __last || __first + 2 == __last) + { + // Trivial sequence - use serial algorithm + return __internal::__brick_unique(__first, __last, __pred, _IsVector{}); + } + return __internal::__remove_elements( + __tag, std::forward<_ExecutionPolicy>(__exec), ++__first, __last, + [&__pred](bool* __b, bool* __e, _RandomAccessIterator __it) + { + __internal::__brick_walk3( + __b, __e, __it - 1, __it, + [&__pred](bool& __x, _ReferenceType __y, _ReferenceType __z) { __x = !__pred(__y, __z); }, _IsVector{}); + }); +} + +//------------------------------------------------------------------------ +// unique_copy +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class OutputIterator, class _BinaryPredicate> +OutputIterator +__brick_unique_copy(_ForwardIterator __first, _ForwardIterator __last, OutputIterator __result, _BinaryPredicate __pred, + /*vector=*/std::false_type) noexcept +{ + return std::unique_copy(__first, __last, __result, __pred); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _BinaryPredicate> +_RandomAccessIterator2 +__brick_unique_copy(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, + _BinaryPredicate __pred, /*vector=*/std::true_type) noexcept +{ +#if defined(_PSTL_MONOTONIC_PRESENT) + return __unseq_backend::__simd_unique_copy(__first, __last - __first, __result, __pred); +#else + return std::unique_copy(__first, __last, __result, __pred); +#endif +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _BinaryPredicate> +_OutputIterator +__pattern_unique_copy(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _OutputIterator __result, _BinaryPredicate __pred) noexcept +{ + return __internal::__brick_unique_copy(__first, __last, __result, __pred, typename _Tag::__is_vector{}); +} + +template <class _DifferenceType, class _RandomAccessIterator, class _BinaryPredicate> +_DifferenceType +__brick_calc_mask_2(_RandomAccessIterator __first, _RandomAccessIterator __last, bool* __restrict __mask, + _BinaryPredicate __pred, /*vector=*/std::false_type) noexcept +{ + _DifferenceType __count = 0; + for (; __first != __last; ++__first, ++__mask) + { + *__mask = !__pred(*__first, *(__first - 1)); + __count += *__mask; + } + return __count; +} + +template <class _DifferenceType, class _RandomAccessIterator, class _BinaryPredicate> +_DifferenceType +__brick_calc_mask_2(_RandomAccessIterator __first, _RandomAccessIterator __last, bool* __restrict __mask, + _BinaryPredicate __pred, /*vector=*/std::true_type) noexcept +{ + return __unseq_backend::__simd_calc_mask_2(__first, __last - __first, __mask, __pred); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryPredicate> +_RandomAccessIterator2 +__pattern_unique_copy(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __last, _RandomAccessIterator2 __result, _BinaryPredicate __pred) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType; + const _DifferenceType __n = __last - __first; + if (_DifferenceType(2) < __n) + { + __par_backend::__buffer<bool> __mask_buf(__n); + if (_DifferenceType(2) < __n) + { + return __internal::__except_handler( + [&__exec, __n, __first, __result, __pred, &__mask_buf]() + { + bool* __mask = __mask_buf.get(); + _DifferenceType __m{}; + __par_backend::__parallel_strict_scan( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __n, _DifferenceType(0), + [=](_DifferenceType __i, _DifferenceType __len) -> _DifferenceType { // Reduce + _DifferenceType __extra = 0; + if (__i == 0) + { + // Special boundary case + __mask[__i] = true; + if (--__len == 0) + return 1; + ++__i; + ++__extra; + } + return __internal::__brick_calc_mask_2<_DifferenceType>( + __first + __i, __first + (__i + __len), __mask + __i, __pred, _IsVector{}) + + __extra; + }, + std::plus<_DifferenceType>(), // Combine + [=](_DifferenceType __i, _DifferenceType __len, _DifferenceType __initial) { // Scan + // Phase 2 is same as for __pattern_copy_if + __internal::__brick_copy_by_mask( + __first + __i, __first + (__i + __len), __result + __initial, __mask + __i, + [](_RandomAccessIterator1 __x, _RandomAccessIterator2 __z) { *__z = *__x; }, + _IsVector{}); + }, + [&__m](_DifferenceType __total) { __m = __total; }); + return __result + __m; + }); + } + } + // trivial sequence - use serial algorithm + return __internal::__brick_unique_copy(__first, __last, __result, __pred, _IsVector{}); +} + +//------------------------------------------------------------------------ +// reverse +//------------------------------------------------------------------------ +template <class _BidirectionalIterator> +void +__brick_reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, /*__is_vector=*/std::false_type) noexcept +{ + std::reverse(__first, __last); +} + +template <class _RandomAccessIterator> +void +__brick_reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, /*__is_vector=*/std::true_type) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::reference _ReferenceType; + + const auto __n = (__last - __first) / 2; + __unseq_backend::__simd_walk_2(__first, __n, std::reverse_iterator<_RandomAccessIterator>(__last), + [](_ReferenceType __x, _ReferenceType __y) { + using std::swap; + swap(__x, __y); + }); +} + +// this brick is called in parallel version, so we can use iterator arithmetic +template <class _BidirectionalIterator> +void +__brick_reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, _BidirectionalIterator __d_last, + /*is_vector=*/std::false_type) noexcept +{ + for (--__d_last; __first != __last; ++__first, --__d_last) + { + using std::iter_swap; + iter_swap(__first, __d_last); + } +} + +// this brick is called in parallel version, so we can use iterator arithmetic +template <class _RandomAccessIterator> +void +__brick_reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, _RandomAccessIterator __d_last, + /*is_vector=*/std::true_type) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::reference _ReferenceType; + + __unseq_backend::__simd_walk_2(__first, __last - __first, std::reverse_iterator<_RandomAccessIterator>(__d_last), + [](_ReferenceType __x, _ReferenceType __y) { + using std::swap; + swap(__x, __y); + }); +} + +template <class _Tag, class _ExecutionPolicy, class _BidirectionalIterator> +void +__pattern_reverse(_Tag, _ExecutionPolicy&&, _BidirectionalIterator __first, _BidirectionalIterator __last) noexcept +{ + __internal::__brick_reverse(__first, __last, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator> +void +__pattern_reverse(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __first + (__last - __first) / 2, + [__first, __last](_RandomAccessIterator __inner_first, _RandomAccessIterator __inner_last) + { __internal::__brick_reverse(__inner_first, __inner_last, __last - (__inner_first - __first), _IsVector{}); }); +} + +//------------------------------------------------------------------------ +// reverse_copy +//------------------------------------------------------------------------ + +template <class _BidirectionalIterator, class _OutputIterator> +_OutputIterator +__brick_reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __d_first, + /*is_vector=*/std::false_type) noexcept +{ + return std::reverse_copy(__first, __last, __d_first); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator2 +__brick_reverse_copy(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __d_first, + /*is_vector=*/std::true_type) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator1>::reference _ReferenceType1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::reference _ReferenceType2; + + return __unseq_backend::__simd_walk_2(std::reverse_iterator<_RandomAccessIterator1>(__last), __last - __first, + __d_first, [](_ReferenceType1 __x, _ReferenceType2 __y) { __y = __x; }); +} + +template <class _Tag, class _ExecutionPolicy, class _BidirectionalIterator, class _OutputIterator> +_OutputIterator +__pattern_reverse_copy(_Tag, _ExecutionPolicy&&, _BidirectionalIterator __first, _BidirectionalIterator __last, + _OutputIterator __d_first) noexcept +{ + return __internal::__brick_reverse_copy(__first, __last, __d_first, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator2 +__pattern_reverse_copy(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __last, _RandomAccessIterator2 __d_first) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + auto __len = __last - __first; + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__first, __len, __d_first](_RandomAccessIterator1 __inner_first, _RandomAccessIterator1 __inner_last) + { + __internal::__brick_reverse_copy(__inner_first, __inner_last, + __d_first + (__len - (__inner_last - __first)), _IsVector{}); + }); + return __d_first + __len; +} + +//------------------------------------------------------------------------ +// rotate +//------------------------------------------------------------------------ +template <class _ForwardIterator> +_ForwardIterator +__brick_rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, + /*is_vector=*/std::false_type) noexcept +{ +#if defined(_PSTL_CPP11_STD_ROTATE_BROKEN) + std::rotate(__first, __middle, __last); + return std::next(__first, std::distance(__middle, __last)); +#else + return std::rotate(__first, __middle, __last); +#endif +} + +template <class _RandomAccessIterator> +_RandomAccessIterator +__brick_rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, + /*is_vector=*/std::true_type) noexcept +{ + auto __n = __last - __first; + auto __m = __middle - __first; + const _RandomAccessIterator __ret = __first + (__last - __middle); + + bool __is_left = (__m <= __n / 2); + if (!__is_left) + __m = __n - __m; + + while (__n > 1 && __m > 0) + { + using std::iter_swap; + const auto __m_2 = __m * 2; + if (__is_left) + { + for (; __last - __first >= __m_2; __first += __m) + { + __unseq_backend::__simd_assign(__first, __m, __first + __m, + iter_swap<_RandomAccessIterator, _RandomAccessIterator>); + } + } + else + { + for (; __last - __first >= __m_2; __last -= __m) + { + __unseq_backend::__simd_assign(__last - __m, __m, __last - __m_2, + iter_swap<_RandomAccessIterator, _RandomAccessIterator>); + } + } + __is_left = !__is_left; + __m = __n % __m; + __n = __last - __first; + } + + return __ret; +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator> +_ForwardIterator +__pattern_rotate(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __middle, + _ForwardIterator __last) noexcept +{ + return __internal::__brick_rotate(__first, __middle, __last, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator> +_RandomAccessIterator +__pattern_rotate(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __middle, _RandomAccessIterator __last) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _Tp; + auto __n = __last - __first; + auto __m = __middle - __first; + if (__m <= __n / 2) + { + __par_backend::__buffer<_Tp> __buf(__n - __m); + return __internal::__except_handler( + [&__exec, __n, __m, __first, __middle, __last, &__buf]() + { + _Tp* __result = __buf.get(); + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __middle, __last, + [__middle, __result](_RandomAccessIterator __b, _RandomAccessIterator __e) + { __internal::__brick_uninitialized_move(__b, __e, __result + (__b - __middle), _IsVector{}); }); + + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __middle, + [__last, __middle](_RandomAccessIterator __b, _RandomAccessIterator __e) + { __internal::__brick_move(__b, __e, __b + (__last - __middle), _IsVector{}); }); + + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __result, __result + (__n - __m), + [__first, __result](_Tp* __b, _Tp* __e) + { __brick_move_destroy()(__b, __e, __first + (__b - __result), _IsVector{}); }); + + return __first + (__last - __middle); + }); + } + else + { + __par_backend::__buffer<_Tp> __buf(__m); + return __internal::__except_handler( + [&__exec, __n, __m, __first, __middle, __last, &__buf]() + { + _Tp* __result = __buf.get(); + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __middle, + [__first, __result](_RandomAccessIterator __b, _RandomAccessIterator __e) + { __internal::__brick_uninitialized_move(__b, __e, __result + (__b - __first), _IsVector{}); }); + + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __middle, __last, + [__first, __middle](_RandomAccessIterator __b, _RandomAccessIterator __e) + { __internal::__brick_move(__b, __e, __first + (__b - __middle), _IsVector{}); }); + + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __result, __result + __m, + [__n, __m, __first, __result](_Tp* __b, _Tp* __e) + { __brick_move_destroy()(__b, __e, __first + ((__n - __m) + (__b - __result)), _IsVector{}); }); + + return __first + (__last - __middle); + }); + } +} + +//------------------------------------------------------------------------ +// rotate_copy +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator> +_OutputIterator +__brick_rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, + _OutputIterator __result, /*__is_vector=*/std::false_type) noexcept +{ + return std::rotate_copy(__first, __middle, __last, __result); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator2 +__brick_rotate_copy(_RandomAccessIterator1 __first, _RandomAccessIterator1 __middle, _RandomAccessIterator1 __last, + _RandomAccessIterator2 __result, /*__is_vector=*/std::true_type) noexcept +{ + _RandomAccessIterator2 __res = __internal::__brick_copy(__middle, __last, __result, std::true_type()); + return __internal::__brick_copy(__first, __middle, __res, std::true_type()); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator> +_OutputIterator +__pattern_rotate_copy(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __middle, + _ForwardIterator __last, _OutputIterator __result) noexcept +{ + return __internal::__brick_rotate_copy(__first, __middle, __last, __result, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator2 +__pattern_rotate_copy(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __middle, _RandomAccessIterator1 __last, _RandomAccessIterator2 __result) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__first, __last, __middle, __result](_RandomAccessIterator1 __b, _RandomAccessIterator1 __e) + { + if (__b > __middle) + { + __internal::__brick_copy(__b, __e, __result + (__b - __middle), _IsVector{}); + } + else + { + _RandomAccessIterator2 __new_result = __result + ((__last - __middle) + (__b - __first)); + if (__e < __middle) + { + __internal::__brick_copy(__b, __e, __new_result, _IsVector{}); + } + else + { + __internal::__brick_copy(__b, __middle, __new_result, _IsVector{}); + __internal::__brick_copy(__middle, __e, __result, _IsVector{}); + } + } + }); + return __result + (__last - __first); +} + +//------------------------------------------------------------------------ +// is_partitioned +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _UnaryPredicate> +bool +__brick_is_partitioned(_ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred, + /*is_vector=*/std::false_type) noexcept +{ + return std::is_partitioned(__first, __last, __pred); +} + +template <class _RandomAccessIterator, class _UnaryPredicate> +bool +__brick_is_partitioned(_RandomAccessIterator __first, _RandomAccessIterator __last, _UnaryPredicate __pred, + /*is_vector=*/std::true_type) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _SizeType; + if (__first == __last) + { + return true; + } + else + { + _RandomAccessIterator __result = __unseq_backend::__simd_first( + __first, _SizeType(0), __last - __first, + [&__pred](_RandomAccessIterator __it, _SizeType __i) { return !__pred(__it[__i]); }); + if (__result == __last) + { + return true; + } + else + { + ++__result; + return !__unseq_backend::__simd_or(__result, __last - __result, __pred); + } + } +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +bool +__pattern_is_partitioned(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _UnaryPredicate __pred) noexcept +{ + return __internal::__brick_is_partitioned(__first, __last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryPredicate> +bool +__pattern_is_partitioned(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _UnaryPredicate __pred) +{ + if (__first == __last) + { + return true; + } + else + { + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler([&]() { + // State of current range: + // broken - current range is not partitioned by pred + // all_true - all elements in current range satisfy pred + // all_false - all elements in current range don't satisfy pred + // true_false - elements satisfy pred are placed before elements that don't satisfy pred + enum _ReduceType + { + __not_init = -1, + __broken, + __all_true, + __all_false, + __true_false + }; + _ReduceType __init = __not_init; + + // Array with states that we'll have when state from the left branch is merged with state from the right branch. + // State is calculated by formula: new_state = table[left_state * 4 + right_state] + _ReduceType __table[] = {__broken, __broken, __broken, __broken, __broken, __all_true, + __true_false, __true_false, __broken, __broken, __all_false, __broken, + __broken, __broken, __true_false, __broken}; + + __init = __par_backend::__parallel_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, __init, + [&__pred, &__table](_RandomAccessIterator __i, _RandomAccessIterator __j, + _ReduceType __value) -> _ReduceType + { + if (__value == __broken) + { + return __broken; + } + _ReduceType __res = __not_init; + // if first element satisfy pred + if (__pred(*__i)) + { + // find first element that don't satisfy pred + _RandomAccessIterator __x = + __internal::__brick_find_if(__i + 1, __j, std::not_fn(__pred), _IsVector{}); + if (__x != __j) + { + // find first element after "x" that satisfy pred + _RandomAccessIterator __y = __internal::__brick_find_if(__x + 1, __j, __pred, _IsVector{}); + // if it was found then range isn't partitioned by pred + if (__y != __j) + { + return __broken; + } + else + { + __res = __true_false; + } + } + else + { + __res = __all_true; + } + } + else + { // if first element doesn't satisfy pred + // then we should find the first element that satisfy pred. + // If we found it then range isn't partitioned by pred + if (__internal::__brick_find_if(__i + 1, __j, __pred, _IsVector{}) != __j) + { + return __broken; + } + else + { + __res = __all_false; + } + } + // if we have value from left range then we should calculate the result + return (__value == -1) ? __res : __table[__value * 4 + __res]; + }, + + [&__table](_ReduceType __val1, _ReduceType __val2) -> _ReduceType + { + if (__val1 == __broken || __val2 == __broken) + { + return __broken; + } + // calculate the result for new big range + return __table[__val1 * 4 + __val2]; + }); + return __init != __broken; + }); + } +} + +//------------------------------------------------------------------------ +// partition +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _UnaryPredicate> +_ForwardIterator +__brick_partition(_ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred, + /*is_vector=*/std::false_type) noexcept +{ + return std::partition(__first, __last, __pred); +} + +template <class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__brick_partition(_RandomAccessIterator __first, _RandomAccessIterator __last, _UnaryPredicate __pred, + /*is_vector=*/std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial"); + return std::partition(__first, __last, __pred); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +_ForwardIterator +__pattern_partition(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _UnaryPredicate __pred) noexcept +{ + return __internal::__brick_partition(__first, __last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__pattern_partition(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _UnaryPredicate __pred) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + // partitioned range: elements before pivot satisfy pred (true part), + // elements after pivot don't satisfy pred (false part) + struct _PartitionRange + { + _RandomAccessIterator __begin; + _RandomAccessIterator __pivot; + _RandomAccessIterator __end; + }; + + return __internal::__except_handler([&]() { + _PartitionRange __init{__last, __last, __last}; + + // lambda for merging two partitioned ranges to one partitioned range + auto __reductor = [&__exec](_PartitionRange __val1, _PartitionRange __val2) -> _PartitionRange + { + auto __size1 = __val1.__end - __val1.__pivot; + auto __size2 = __val2.__pivot - __val2.__begin; + auto __new_begin = __val2.__begin - (__val1.__end - __val1.__begin); + + // if all elements in left range satisfy pred then we can move new pivot to pivot of right range + if (__val1.__end == __val1.__pivot) + { + return {__new_begin, __val2.__pivot, __val2.__end}; + } + // if true part of right range greater than false part of left range + // then we should swap the false part of left range and last part of true part of right range + else if (__size2 > __size1) + { + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __val1.__pivot, __val1.__pivot + __size1, + [__val1, __val2, __size1](_RandomAccessIterator __i, _RandomAccessIterator __j) { + __internal::__brick_swap_ranges(__i, __j, (__val2.__pivot - __size1) + (__i - __val1.__pivot), + _IsVector{}); + }); + return {__new_begin, __val2.__pivot - __size1, __val2.__end}; + } + // else we should swap the first part of false part of left range and true part of right range + else + { + __par_backend::__parallel_for( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __val1.__pivot, __val1.__pivot + __size2, + [__val1, __val2](_RandomAccessIterator __i, _RandomAccessIterator __j) { + __internal::__brick_swap_ranges(__i, __j, __val2.__begin + (__i - __val1.__pivot), _IsVector{}); + }); + return {__new_begin, __val1.__pivot + __size2, __val2.__end}; + } + }; + + _PartitionRange __result = __par_backend::__parallel_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, __init, + [__pred, __reductor](_RandomAccessIterator __i, _RandomAccessIterator __j, + _PartitionRange __value) -> _PartitionRange + { + //1. serial partition + _RandomAccessIterator __pivot = __internal::__brick_partition(__i, __j, __pred, _IsVector{}); + + // 2. merging of two ranges (left and right respectively) + return __reductor(__value, {__i, __pivot, __j}); + }, + __reductor); + return __result.__pivot; + }); +} + +//------------------------------------------------------------------------ +// stable_partition +//------------------------------------------------------------------------ + +template <class _BidirectionalIterator, class _UnaryPredicate> +_BidirectionalIterator +__brick_stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _UnaryPredicate __pred, + /*__is_vector=*/std::false_type) noexcept +{ + return std::stable_partition(__first, __last, __pred); +} + +template <class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__brick_stable_partition(_RandomAccessIterator __first, _RandomAccessIterator __last, _UnaryPredicate __pred, + /*__is_vector=*/std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial"); + return std::stable_partition(__first, __last, __pred); +} + +template <class _Tag, class _ExecutionPolicy, class _BidirectionalIterator, class _UnaryPredicate> +_BidirectionalIterator +__pattern_stable_partition(_Tag, _ExecutionPolicy&&, _BidirectionalIterator __first, _BidirectionalIterator __last, + _UnaryPredicate __pred) noexcept +{ + return __internal::__brick_stable_partition(__first, __last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__pattern_stable_partition(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _UnaryPredicate __pred) noexcept +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + // partitioned range: elements before pivot satisfy pred (true part), + // elements after pivot don't satisfy pred (false part) + struct _PartitionRange + { + _RandomAccessIterator __begin; + _RandomAccessIterator __pivot; + _RandomAccessIterator __end; + }; + + return __internal::__except_handler([&]() { + _PartitionRange __init{__last, __last, __last}; + + // lambda for merging two partitioned ranges to one partitioned range + auto __reductor = [](_PartitionRange __val1, _PartitionRange __val2) -> _PartitionRange + { + auto __size1 = __val1.__end - __val1.__pivot; + auto __new_begin = __val2.__begin - (__val1.__end - __val1.__begin); + + // if all elements in left range satisfy pred then we can move new pivot to pivot of right range + if (__val1.__end == __val1.__pivot) + { + return {__new_begin, __val2.__pivot, __val2.__end}; + } + // if true part of right range greater than false part of left range + // then we should swap the false part of left range and last part of true part of right range + else + { + __internal::__brick_rotate(__val1.__pivot, __val2.__begin, __val2.__pivot, _IsVector{}); + return {__new_begin, __val2.__pivot - __size1, __val2.__end}; + } + }; + + _PartitionRange __result = __par_backend::__parallel_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, __init, + [&__pred, __reductor](_RandomAccessIterator __i, _RandomAccessIterator __j, + _PartitionRange __value) -> _PartitionRange + { + //1. serial stable_partition + _RandomAccessIterator __pivot = __internal::__brick_stable_partition(__i, __j, __pred, _IsVector{}); + + // 2. merging of two ranges (left and right respectively) + return __reductor(__value, {__i, __pivot, __j}); + }, + __reductor); + return __result.__pivot; + }); +} + +//------------------------------------------------------------------------ +// partition_copy +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator1, class _OutputIterator2, class _UnaryPredicate> +std::pair<_OutputIterator1, _OutputIterator2> +__brick_partition_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator1 __out_true, + _OutputIterator2 __out_false, _UnaryPredicate __pred, /*is_vector=*/std::false_type) noexcept +{ + return std::partition_copy(__first, __last, __out_true, __out_false, __pred); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, + class _UnaryPredicate> +std::pair<_RandomAccessIterator2, _RandomAccessIterator3> +__brick_partition_copy(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, _RandomAccessIterator2 __out_true, + _RandomAccessIterator3 __out_false, _UnaryPredicate __pred, + /*is_vector=*/std::true_type) noexcept +{ +#if defined(_PSTL_MONOTONIC_PRESENT) + return __unseq_backend::__simd_partition_copy(__first, __last - __first, __out_true, __out_false, __pred); +#else + return std::partition_copy(__first, __last, __out_true, __out_false, __pred); +#endif +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator1, class _OutputIterator2, + class _UnaryPredicate> +std::pair<_OutputIterator1, _OutputIterator2> +__pattern_partition_copy(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _OutputIterator1 __out_true, _OutputIterator2 __out_false, _UnaryPredicate __pred) noexcept +{ + return __internal::__brick_partition_copy(__first, __last, __out_true, __out_false, __pred, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _RandomAccessIterator3, class _UnaryPredicate> +std::pair<_RandomAccessIterator2, _RandomAccessIterator3> +__pattern_partition_copy(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __last, _RandomAccessIterator2 __out_true, + _RandomAccessIterator3 __out_false, _UnaryPredicate __pred) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType; + typedef std::pair<_DifferenceType, _DifferenceType> _ReturnType; + const _DifferenceType __n = __last - __first; + if (_DifferenceType(1) < __n) + { + __par_backend::__buffer<bool> __mask_buf(__n); + return __internal::__except_handler( + [&__exec, __n, __first, __out_true, __out_false, __pred, &__mask_buf]() + { + bool* __mask = __mask_buf.get(); + _ReturnType __m{}; + __par_backend::__parallel_strict_scan( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __n, + std::make_pair(_DifferenceType(0), _DifferenceType(0)), + [=](_DifferenceType __i, _DifferenceType __len) { // Reduce + return __internal::__brick_calc_mask_1<_DifferenceType>(__first + __i, __first + (__i + __len), + __mask + __i, __pred, _IsVector{}); + }, + [](const _ReturnType& __x, const _ReturnType& __y) -> _ReturnType + { return std::make_pair(__x.first + __y.first, __x.second + __y.second); }, // Combine + [=](_DifferenceType __i, _DifferenceType __len, _ReturnType __initial) { // Scan + __internal::__brick_partition_by_mask( + __first + __i, __first + (__i + __len), __out_true + __initial.first, + __out_false + __initial.second, __mask + __i, _IsVector{}); + }, + [&__m](_ReturnType __total) { __m = __total; }); + return std::make_pair(__out_true + __m.first, __out_false + __m.second); + }); + } + // trivial sequence - use serial algorithm + return __internal::__brick_partition_copy(__first, __last, __out_true, __out_false, __pred, _IsVector{}); +} + +//------------------------------------------------------------------------ +// sort +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsMoveConstructible> +void +__pattern_sort(_Tag, _ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, + _IsMoveConstructible) noexcept +{ + std::sort(__first, __last, __comp); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_sort(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Compare __comp, /*is_move_constructible=*/std::true_type) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + __internal::__except_handler( + [&]() + { + __par_backend::__parallel_stable_sort( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp, + [](_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) + { std::sort(__first, __last, __comp); }); + }); +} + +//------------------------------------------------------------------------ +// stable_sort +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_stable_sort(_Tag, _ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, + _Compare __comp) noexcept +{ + std::stable_sort(__first, __last, __comp); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_stable_sort(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Compare __comp) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + __internal::__except_handler( + [&]() + { + __par_backend::__parallel_stable_sort( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp, + [](_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) + { std::stable_sort(__first, __last, __comp); }); + }); +} + +//------------------------------------------------------------------------ +// partial_sort +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_partial_sort(_Tag, _ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __middle, + _RandomAccessIterator __last, _Compare __comp) noexcept +{ + std::partial_sort(__first, __middle, __last, __comp); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_partial_sort(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __middle, _RandomAccessIterator __last, _Compare __comp) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + const auto __n = __middle - __first; + if (__n == 0) + return; + + __internal::__except_handler( + [&]() + { + __par_backend::__parallel_stable_sort( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp, + [__n](_RandomAccessIterator __begin, _RandomAccessIterator __end, _Compare __comp) + { + if (__n < __end - __begin) + std::partial_sort(__begin, __begin + __n, __end, __comp); + else + std::sort(__begin, __end, __comp); + }, + __n); + }); +} + +//------------------------------------------------------------------------ +// partial_sort_copy +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator, class _Compare> +_RandomAccessIterator +__pattern_partial_sort_copy(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _RandomAccessIterator __d_first, _RandomAccessIterator __d_last, _Compare __comp) noexcept +{ + return std::partial_sort_copy(__first, __last, __d_first, __d_last, __comp); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Compare> +_RandomAccessIterator2 +__pattern_partial_sort_copy(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first, + _RandomAccessIterator1 __last, _RandomAccessIterator2 __d_first, + _RandomAccessIterator2 __d_last, _Compare __comp) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (__last == __first || __d_last == __d_first) + { + return __d_first; + } + auto __n1 = __last - __first; + auto __n2 = __d_last - __d_first; + return __internal::__except_handler([&]() { + if (__n2 >= __n1) + { + __par_backend::__parallel_stable_sort( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __d_first, __d_first + __n1, __comp, + [__first, __d_first](_RandomAccessIterator2 __i, _RandomAccessIterator2 __j, _Compare __comp) + { + _RandomAccessIterator1 __i1 = __first + (__i - __d_first); + _RandomAccessIterator1 __j1 = __first + (__j - __d_first); + + // 1. Copy elements from input to output +#if !defined(_PSTL_ICC_18_OMP_SIMD_BROKEN) + __internal::__brick_copy(__i1, __j1, __i, _IsVector{}); +#else + std::copy(__i1, __j1, __i); +#endif + // 2. Sort elements in output sequence + std::sort(__i, __j, __comp); + }, + __n1); + return __d_first + __n1; + } + else + { + typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type _T1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::value_type _T2; + __par_backend::__buffer<_T1> __buf(__n1); + _T1* __r = __buf.get(); + + __par_backend::__parallel_stable_sort( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __r, __r + __n1, __comp, + [__n2, __first, __r](_T1* __i, _T1* __j, _Compare __comp) + { + _RandomAccessIterator1 __it = __first + (__i - __r); + + // 1. Copy elements from input to raw memory + for (_T1* __k = __i; __k != __j; ++__k, ++__it) + { + ::new (__k) _T2(*__it); + } + + // 2. Sort elements in temporary __buffer + if (__n2 < __j - __i) + std::partial_sort(__i, __i + __n2, __j, __comp); + else + std::sort(__i, __j, __comp); + }, + __n2); + + // 3. Move elements from temporary __buffer to output + __par_backend::__parallel_for(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __r, __r + __n2, + [__r, __d_first](_T1* __i, _T1* __j) + { __brick_move_destroy()(__i, __j, __d_first + (__i - __r), _IsVector{}); }); + __par_backend::__parallel_for(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __r + __n2, + __r + __n1, + [](_T1* __i, _T1* __j) { __brick_destroy(__i, __j, _IsVector{}); }); + + return __d_first + __n2; + } + }); +} + +//------------------------------------------------------------------------ +// adjacent_find +//------------------------------------------------------------------------ +template <class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator +__brick_adjacent_find(_RandomAccessIterator __first, _RandomAccessIterator __last, _BinaryPredicate __pred, + /* IsVector = */ std::true_type, bool __or_semantic) noexcept +{ + return __unseq_backend::__simd_adjacent_find(__first, __last, __pred, __or_semantic); +} + +template <class _ForwardIterator, class _BinaryPredicate> +_ForwardIterator +__brick_adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred, + /* IsVector = */ std::false_type, bool) noexcept +{ + return std::adjacent_find(__first, __last, __pred); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate> +_ForwardIterator +__pattern_adjacent_find(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _BinaryPredicate __pred, bool __or_semantic) noexcept +{ + return __internal::__brick_adjacent_find(__first, __last, __pred, typename _Tag::__is_vector{}, __or_semantic); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _BinaryPredicate> +_RandomAccessIterator +__pattern_adjacent_find(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _BinaryPredicate __pred, bool __or_semantic) +{ + if (__last - __first < 2) + return __last; + + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + return __par_backend::__parallel_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, __last, + [__last, __pred, __or_semantic](_RandomAccessIterator __begin, _RandomAccessIterator __end, + _RandomAccessIterator __value) -> _RandomAccessIterator + { + // TODO: investigate performance benefits from the use of shared variable for the result, + // checking (compare_and_swap idiom) its __value at __first. + if (__or_semantic && __value < __last) + { //found + __par_backend::__cancel_execution(); + return __value; + } + + if (__value > __begin) + { + // modify __end to check the predicate on the boundary __values; + // TODO: to use a custom range with boundaries overlapping + // TODO: investigate what if we remove "if" below and run algorithm on range [__first, __last-1) + // then check the pair [__last-1, __last) + if (__end != __last) + ++__end; + + //correct the global result iterator if the "brick" returns a local "__last" + const _RandomAccessIterator __res = + __internal::__brick_adjacent_find(__begin, __end, __pred, _IsVector{}, __or_semantic); + if (__res < __end) + __value = __res; + } + return __value; + }, + [](_RandomAccessIterator __x, _RandomAccessIterator __y) -> _RandomAccessIterator + { return __x < __y ? __x : __y; } //reduce a __value + ); + }); +} + +//------------------------------------------------------------------------ +// nth_element +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_nth_element(_Tag, _ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __nth, + _RandomAccessIterator __last, _Compare __comp) noexcept +{ + std::nth_element(__first, __nth, __last, __comp); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_nth_element(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp) noexcept +{ + if (__first == __last || __nth == __last) + { + return; + } + + using std::iter_swap; + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _Tp; + _RandomAccessIterator __x; + do + { + __x = __internal::__pattern_partition(__tag, std::forward<_ExecutionPolicy>(__exec), __first + 1, __last, + [&__comp, __first](const _Tp& __x) { return __comp(__x, *__first); }); + --__x; + if (__x != __first) + { + iter_swap(__first, __x); + } + // if x > nth then our new range for partition is [first, x) + if (__x - __nth > 0) + { + __last = __x; + } + // if x < nth then our new range for partition is [x, last) + else if (__x - __nth < 0) + { + // if *x == *nth then we can start new partition with x+1 + if (!__comp(*__nth, *__x) && !__comp(*__x, *__nth)) + { + ++__x; + } + else + { + iter_swap(__nth, __x); + } + __first = __x; + } + } while (__x != __nth); +} + +//------------------------------------------------------------------------ +// fill, fill_n +//------------------------------------------------------------------------ +template <class _RandomAccessIterator, class _Tp> +void +__brick_fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value, + /* __is_vector = */ std::true_type) noexcept +{ + __unseq_backend::__simd_fill_n(__first, __last - __first, __value); +} + +template <class _ForwardIterator, class _Tp> +void +__brick_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, + /* __is_vector = */ std::false_type) noexcept +{ + std::fill(__first, __last, __value); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Tp> +void +__pattern_fill(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) noexcept +{ + __internal::__brick_fill(__first, __last, __value, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Tp> +_RandomAccessIterator +__pattern_fill(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, const _Tp& __value) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&__exec, __first, __last, &__value]() + { + __par_backend::__parallel_for(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [&__value](_RandomAccessIterator __begin, _RandomAccessIterator __end) + { __internal::__brick_fill(__begin, __end, __value, _IsVector{}); }); + return __last; + }); +} + +template <class _RandomAccessIterator, class _Size, class _Tp> +_RandomAccessIterator +__brick_fill_n(_RandomAccessIterator __first, _Size __count, const _Tp& __value, + /* __is_vector = */ std::true_type) noexcept +{ + return __unseq_backend::__simd_fill_n(__first, __count, __value); +} + +template <class _OutputIterator, class _Size, class _Tp> +_OutputIterator +__brick_fill_n(_OutputIterator __first, _Size __count, const _Tp& __value, /* __is_vector = */ std::false_type) noexcept +{ + return std::fill_n(__first, __count, __value); +} + +template <class _Tag, class _ExecutionPolicy, class _OutputIterator, class _Size, class _Tp> +_OutputIterator +__pattern_fill_n(_Tag, _ExecutionPolicy&&, _OutputIterator __first, _Size __count, const _Tp& __value) noexcept +{ + return __internal::__brick_fill_n(__first, __count, __value, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Tp> +_RandomAccessIterator +__pattern_fill_n(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _Size __count, const _Tp& __value) +{ + return __internal::__pattern_fill(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __first + __count, + __value); +} + +//------------------------------------------------------------------------ +// generate, generate_n +//------------------------------------------------------------------------ +template <class _RandomAccessIterator, class _Generator> +void +__brick_generate(_RandomAccessIterator __first, _RandomAccessIterator __last, _Generator __g, + /* is_vector = */ std::true_type) noexcept +{ + __unseq_backend::__simd_generate_n(__first, __last - __first, __g); +} + +template <class _ForwardIterator, class _Generator> +void +__brick_generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __g, + /* is_vector = */ std::false_type) noexcept +{ + std::generate(__first, __last, __g); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Generator> +void +__pattern_generate(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Generator __g) noexcept +{ + __internal::__brick_generate(__first, __last, __g, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Generator> +_RandomAccessIterator +__pattern_generate(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Generator __g) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + __par_backend::__parallel_for(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__g](_RandomAccessIterator __begin, _RandomAccessIterator __end) + { __internal::__brick_generate(__begin, __end, __g, _IsVector{}); }); + return __last; + }); +} + +template <class _RandomAccessIterator, class Size, class _Generator> +_RandomAccessIterator +__brick_generate_n(_RandomAccessIterator __first, Size __count, _Generator __g, + /* is_vector = */ std::true_type) noexcept +{ + return __unseq_backend::__simd_generate_n(__first, __count, __g); +} + +template <class OutputIterator, class Size, class _Generator> +OutputIterator +__brick_generate_n(OutputIterator __first, Size __count, _Generator __g, /* is_vector = */ std::false_type) noexcept +{ + return std::generate_n(__first, __count, __g); +} + +template <class _Tag, class _ExecutionPolicy, class _OutputIterator, class _Size, class _Generator> +_OutputIterator +__pattern_generate_n(_Tag, _ExecutionPolicy&&, _OutputIterator __first, _Size __count, _Generator __g) noexcept +{ + return __internal::__brick_generate_n(__first, __count, __g, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Size, class _Generator> +_RandomAccessIterator +__pattern_generate_n(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _Size __count, _Generator __g) +{ + static_assert(__are_random_access_iterators<_RandomAccessIterator>::value, + "Pattern-brick error. Should be a random access iterator."); + return __internal::__pattern_generate(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __first + __count, + __g); +} + +//------------------------------------------------------------------------ +// remove +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _UnaryPredicate> +_ForwardIterator +__brick_remove_if(_ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred, + /* __is_vector = */ std::false_type) noexcept +{ + return std::remove_if(__first, __last, __pred); +} + +template <class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__brick_remove_if(_RandomAccessIterator __first, _RandomAccessIterator __last, _UnaryPredicate __pred, + /* __is_vector = */ std::true_type) noexcept +{ +#if defined(_PSTL_MONOTONIC_PRESENT) + return __unseq_backend::__simd_remove_if(__first, __last - __first, __pred); +#else + return std::remove_if(__first, __last, __pred); +#endif +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +_ForwardIterator +__pattern_remove_if(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _UnaryPredicate __pred) noexcept +{ + return __internal::__brick_remove_if(__first, __last, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryPredicate> +_RandomAccessIterator +__pattern_remove_if(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _UnaryPredicate __pred) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::reference _ReferenceType; + + if (__first == __last || __first + 1 == __last) + { + // Trivial sequence - use serial algorithm + return __internal::__brick_remove_if(__first, __last, __pred, _IsVector{}); + } + + return __internal::__remove_elements( + __tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [&__pred](bool* __b, bool* __e, _RandomAccessIterator __it) + { + __internal::__brick_walk2( + __b, __e, __it, [&__pred](bool& __x, _ReferenceType __y) { __x = !__pred(__y); }, _IsVector{}); + }); +} + +//------------------------------------------------------------------------ +// merge +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator +__brick_merge(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __d_first, _Compare __comp, + /* __is_vector = */ std::false_type) noexcept +{ + return std::merge(__first1, __last1, __first2, __last2, __d_first, __comp); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, class _Compare> +_RandomAccessIterator3 +__brick_merge(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _RandomAccessIterator3 __d_first, _Compare __comp, + /* __is_vector = */ std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial"); + return std::merge(__first1, __last1, __first2, __last2, __d_first, __comp); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_merge(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __d_first, + _Compare __comp) noexcept +{ + return __internal::__brick_merge(__first1, __last1, __first2, __last2, __d_first, __comp, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _RandomAccessIterator3, class _Compare> +_RandomAccessIterator3 +__pattern_merge(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _RandomAccessIterator3 __d_first, _Compare __comp) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + __par_backend::__parallel_merge( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __d_first, + __comp, + [](_RandomAccessIterator1 __f1, _RandomAccessIterator1 __l1, _RandomAccessIterator2 __f2, + _RandomAccessIterator2 __l2, _RandomAccessIterator3 __f3, _Compare __comp) + { return __internal::__brick_merge(__f1, __l1, __f2, __l2, __f3, __comp, _IsVector{}); }); + return __d_first + (__last1 - __first1) + (__last2 - __first2); +} + +//------------------------------------------------------------------------ +// inplace_merge +//------------------------------------------------------------------------ +template <class _BidirectionalIterator, class _Compare> +void +__brick_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, + _Compare __comp, /* __is_vector = */ std::false_type) noexcept +{ + std::inplace_merge(__first, __middle, __last, __comp); +} + +template <class _RandomAccessIterator, class _Compare> +void +__brick_inplace_merge(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, + _Compare __comp, /* __is_vector = */ std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial") + std::inplace_merge(__first, __middle, __last, __comp); +} + +template <class _Tag, class _ExecutionPolicy, class _BidirectionalIterator, class _Compare> +void +__pattern_inplace_merge(_Tag, _ExecutionPolicy&&, _BidirectionalIterator __first, _BidirectionalIterator __middle, + _BidirectionalIterator __last, _Compare __comp) noexcept +{ + __internal::__brick_inplace_merge(__first, __middle, __last, __comp, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +void +__pattern_inplace_merge(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __middle, _RandomAccessIterator __last, _Compare __comp) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (__first == __last || __first == __middle || __middle == __last) + { + return; + } + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _Tp; + auto __n = __last - __first; + __par_backend::__buffer<_Tp> __buf(__n); + _Tp* __r = __buf.get(); + __internal::__except_handler( + [&]() + { + auto __move_values = [](_RandomAccessIterator __x, _Tp* __z) + { + __internal::__invoke_if_else( + std::is_trivial<_Tp>(), [&]() { *__z = std::move(*__x); }, + [&]() { ::new (std::addressof(*__z)) _Tp(std::move(*__x)); }); + }; + + auto __move_sequences = [](_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Tp* __first2) + { return __internal::__brick_uninitialized_move(__first1, __last1, __first2, _IsVector()); }; + + __par_backend::__parallel_merge( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __middle, __middle, __last, __r, + __comp, + [__n, __move_values, __move_sequences](_RandomAccessIterator __f1, _RandomAccessIterator __l1, + _RandomAccessIterator __f2, _RandomAccessIterator __l2, + _Tp* __f3, _Compare __comp) + { + (__utils::__serial_move_merge(__n))(__f1, __l1, __f2, __l2, __f3, __comp, __move_values, + __move_values, __move_sequences, __move_sequences); + return __f3 + (__l1 - __f1) + (__l2 - __f2); + }); + __par_backend::__parallel_for(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __r, __r + __n, + [__r, __first](_Tp* __i, _Tp* __j) + { __brick_move_destroy()(__i, __j, __first + (__i - __r), _IsVector{}); }); + }); +} + +//------------------------------------------------------------------------ +// includes +//------------------------------------------------------------------------ + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare> +bool +__pattern_includes(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp) noexcept +{ + return std::includes(__first1, __last1, __first2, __last2, __comp); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Compare> +bool +__pattern_includes(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _Compare __comp) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (__first2 >= __last2) + return true; + + if (__first1 >= __last1 || __comp(*__first2, *__first1) || __comp(*(__last1 - 1), *(__last2 - 1))) + return false; + + __first1 = std::lower_bound(__first1, __last1, *__first2, __comp); + if (__first1 == __last1) + return false; + + if (__last2 - __first2 == 1) + return !__comp(*__first1, *__first2) && !__comp(*__first2, *__first1); + + return __internal::__except_handler( + [&]() + { + return !__internal::__parallel_or( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first2, __last2, + [__first1, __last1, __first2, __last2, &__comp](_RandomAccessIterator2 __i, _RandomAccessIterator2 __j) + { + _PSTL_ASSERT(__j > __i); + //_PSTL_ASSERT(__j - __i > 1); + + //1. moving boundaries to "consume" subsequence of equal elements + auto __is_equal = [&__comp](_RandomAccessIterator2 __a, _RandomAccessIterator2 __b) -> bool + { return !__comp(*__a, *__b) && !__comp(*__b, *__a); }; + + //1.1 left bound, case "aaa[aaaxyz...]" - searching "x" + if (__i > __first2 && __is_equal(__i, __i - 1)) + { + //whole subrange continues to content equal elements - return "no op" + if (__is_equal(__i, __j - 1)) + return false; + + __i = std::upper_bound(__i, __last2, *__i, __comp); + } + + //1.2 right bound, case "[...aaa]aaaxyz" - searching "x" + if (__j < __last2 && __is_equal(__j - 1, __j)) + __j = std::upper_bound(__j, __last2, *__j, __comp); + + //2. testing is __a subsequence of the second range included into the first range + auto __b = std::lower_bound(__first1, __last1, *__i, __comp); + + _PSTL_ASSERT(!__comp(*(__last1 - 1), *__b)); + _PSTL_ASSERT(!__comp(*(__j - 1), *__i)); + return !std::includes(__b, __last1, __i, __j, __comp); + }); + }); +} + +constexpr auto __set_algo_cut_off = 1000; + +template <class _IsVector, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, + class _OutputIterator, class _Compare, class _SizeFunction, class _SetOP> +_OutputIterator +__parallel_set_op(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _ForwardIterator1 __first1, + _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2, + _OutputIterator __result, _Compare __comp, _SizeFunction __size_func, _SetOP __set_op) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferenceType; + typedef typename std::iterator_traits<_OutputIterator>::value_type _Tp; + + struct _SetRange + { + _DifferenceType __pos, __len, __buf_pos; + bool + empty() const + { + return __len == 0; + } + }; + + const _DifferenceType __n1 = __last1 - __first1; + const _DifferenceType __n2 = __last2 - __first2; + + __par_backend::__buffer<_Tp> __buf(__size_func(__n1, __n2)); + + return __internal::__except_handler( + [&__exec, __n1, __first1, __last1, __first2, __last2, __result, __comp, __size_func, __set_op, &__buf]() + { + auto __buffer = __buf.get(); + _DifferenceType __m{}; + auto __scan = [=](_DifferenceType, _DifferenceType, const _SetRange& __s) { // Scan + if (!__s.empty()) + __brick_move_destroy()(__buffer + __s.__buf_pos, __buffer + (__s.__buf_pos + __s.__len), + __result + __s.__pos, _IsVector{}); + }; + __par_backend::__parallel_strict_scan( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __n1, _SetRange{0, 0, 0}, //-1, 0}, + [=](_DifferenceType __i, _DifferenceType __len) { // Reduce + //[__b; __e) - a subrange of the first sequence, to reduce + _ForwardIterator1 __b = __first1 + __i, __e = __first1 + (__i + __len); + + //try searching for the first element which not equal to *__b + if (__b != __first1) + __b = std::upper_bound(__b, __last1, *__b, __comp); + + //try searching for the first element which not equal to *__e + if (__e != __last1) + __e = std::upper_bound(__e, __last1, *__e, __comp); + + //check is [__b; __e) empty + if (__e - __b < 1) + { + _ForwardIterator2 __bb = __last2; + if (__b != __last1) + __bb = std::lower_bound(__first2, __last2, *__b, __comp); + + const _DifferenceType __buf_pos = __size_func((__b - __first1), (__bb - __first2)); + return _SetRange{0, 0, __buf_pos}; + } + + //try searching for "corresponding" subrange [__bb; __ee) in the second sequence + _ForwardIterator2 __bb = __first2; + if (__b != __first1) + __bb = std::lower_bound(__first2, __last2, *__b, __comp); + + _ForwardIterator2 __ee = __last2; + if (__e != __last1) + __ee = std::lower_bound(__bb, __last2, *__e, __comp); + + const _DifferenceType __buf_pos = __size_func((__b - __first1), (__bb - __first2)); + auto __buffer_b = __buffer + __buf_pos; + auto __res = __set_op(__b, __e, __bb, __ee, __buffer_b, __comp); + + return _SetRange{0, __res - __buffer_b, __buf_pos}; + }, + [](const _SetRange& __a, const _SetRange& __b) { // Combine + if (__b.__buf_pos > __a.__buf_pos || ((__b.__buf_pos == __a.__buf_pos) && !__b.empty())) + return _SetRange{__a.__pos + __a.__len + __b.__pos, __b.__len, __b.__buf_pos}; + return _SetRange{__b.__pos + __b.__len + __a.__pos, __a.__len, __a.__buf_pos}; + }, + __scan, // Scan + [&__m, &__scan](const _SetRange& __total) { // Apex + //final scan + __scan(0, 0, __total); + __m = __total.__pos + __total.__len; + }); + return __result + __m; + }); +} + +//a shared parallel pattern for '__pattern_set_union' and '__pattern_set_symmetric_difference' +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare, class _SetUnionOp> +_OutputIterator +__parallel_set_union_op(_Tag __tag, _ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, + _Compare __comp, _SetUnionOp __set_union_op) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferenceType; + + const auto __n1 = __last1 - __first1; + const auto __n2 = __last2 - __first2; + + auto copy_range1 = [](_ForwardIterator1 __begin, _ForwardIterator1 __end, _OutputIterator __res) + { return __internal::__brick_copy(__begin, __end, __res, typename _Tag::__is_vector{}); }; + auto copy_range2 = [](_ForwardIterator2 __begin, _ForwardIterator2 __end, _OutputIterator __res) + { return __internal::__brick_copy(__begin, __end, __res, typename _Tag::__is_vector{}); }; + + // {1} {}: parallel copying just first sequence + if (__n2 == 0) + return __internal::__pattern_walk2_brick(__tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + __result, copy_range1); + + // {} {2}: parallel copying justmake second sequence + if (__n1 == 0) + return __internal::__pattern_walk2_brick(__tag, std::forward<_ExecutionPolicy>(__exec), __first2, __last2, + __result, copy_range2); + + // testing whether the sequences are intersected + _ForwardIterator1 __left_bound_seq_1 = std::lower_bound(__first1, __last1, *__first2, __comp); + + if (__left_bound_seq_1 == __last1) + { + //{1} < {2}: seq2 is wholly greater than seq1, so, do parallel copying seq1 and seq2 + __par_backend::__parallel_invoke( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), + [=] + { + __internal::__pattern_walk2_brick(__tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + __result, copy_range1); + }, + [=] + { + __internal::__pattern_walk2_brick(__tag, std::forward<_ExecutionPolicy>(__exec), __first2, __last2, + __result + __n1, copy_range2); + }); + return __result + __n1 + __n2; + } + + // testing whether the sequences are intersected + _ForwardIterator2 __left_bound_seq_2 = std::lower_bound(__first2, __last2, *__first1, __comp); + + if (__left_bound_seq_2 == __last2) + { + //{2} < {1}: seq2 is wholly greater than seq1, so, do parallel copying seq1 and seq2 + __par_backend::__parallel_invoke( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), + [=] + { + __internal::__pattern_walk2_brick(__tag, std::forward<_ExecutionPolicy>(__exec), __first2, __last2, + __result, copy_range2); + }, + [=] + { + __internal::__pattern_walk2_brick(__tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + __result + __n2, copy_range1); + }); + return __result + __n1 + __n2; + } + + const auto __m1 = __left_bound_seq_1 - __first1; + if (__m1 > __set_algo_cut_off) + { + auto __res_or = __result; + __result += __m1; //we know proper offset due to [first1; left_bound_seq_1) < [first2; last2) + __par_backend::__parallel_invoke( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), + //do parallel copying of [first1; left_bound_seq_1) + [=] + { + __internal::__pattern_walk2_brick(__tag, std::forward<_ExecutionPolicy>(__exec), __first1, + __left_bound_seq_1, __res_or, copy_range1); + }, + [=, &__result] + { + __result = __internal::__parallel_set_op( + __tag, std::forward<_ExecutionPolicy>(__exec), __left_bound_seq_1, __last1, __first2, __last2, + __result, __comp, [](_DifferenceType __n, _DifferenceType __m) { return __n + __m; }, + __set_union_op); + }); + return __result; + } + + const auto __m2 = __left_bound_seq_2 - __first2; + _PSTL_ASSERT(__m1 == 0 || __m2 == 0); + if (__m2 > __set_algo_cut_off) + { + auto __res_or = __result; + __result += __m2; //we know proper offset due to [first2; left_bound_seq_2) < [first1; last1) + __par_backend::__parallel_invoke( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), + //do parallel copying of [first2; left_bound_seq_2) + [=] + { + __internal::__pattern_walk2_brick(__tag, std::forward<_ExecutionPolicy>(__exec), __first2, + __left_bound_seq_2, __res_or, copy_range2); + }, + [=, &__result] + { + __result = __internal::__parallel_set_op( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __left_bound_seq_2, __last2, + __result, __comp, [](_DifferenceType __n, _DifferenceType __m) { return __n + __m; }, + __set_union_op); + }); + return __result; + } + + return __internal::__parallel_set_op( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp, + [](_DifferenceType __n, _DifferenceType __m) { return __n + __m; }, __set_union_op); +} + +//------------------------------------------------------------------------ +// set_union +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator +__brick_set_union(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp, + /*__is_vector=*/std::false_type) noexcept +{ + return std::set_union(__first1, __last1, __first2, __last2, __result, __comp); +} + +template <typename _IsVector> +struct __BrickCopyConstruct +{ + template <typename _ForwardIterator, typename _OutputIterator> + _OutputIterator + operator()(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result) + { + return __brick_uninitialized_copy(__first, __last, __result, _IsVector()); + } +}; + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _OutputIterator, class _Compare> +_OutputIterator +__brick_set_union(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _OutputIterator __result, _Compare __comp, + /*__is_vector=*/std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial"); + return std::set_union(__first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_set_union(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, + _Compare __comp) noexcept +{ + return __internal::__brick_set_union(__first1, __last1, __first2, __last2, __result, __comp, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _OutputIterator, class _Compare> +_OutputIterator +__pattern_set_union(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _OutputIterator __result, _Compare __comp) +{ + + const auto __n1 = __last1 - __first1; + const auto __n2 = __last2 - __first2; + + // use serial algorithm + if (__n1 + __n2 <= __set_algo_cut_off) + return std::set_union(__first1, __last1, __first2, __last2, __result, __comp); + + typedef typename std::iterator_traits<_OutputIterator>::value_type _Tp; + return __parallel_set_union_op( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp, + [](_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _Tp* __result, _Compare __comp) + { + return __pstl::__utils::__set_union_construct(__first1, __last1, __first2, __last2, __result, __comp, + __BrickCopyConstruct<_IsVector>()); + }); +} + +//------------------------------------------------------------------------ +// set_intersection +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator +__brick_set_intersection(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp, + /*__is_vector=*/std::false_type) noexcept +{ + return std::set_intersection(__first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, class _Compare> +_RandomAccessIterator3 +__brick_set_intersection(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, + _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _RandomAccessIterator3 __result, _Compare __comp, + /*__is_vector=*/std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial"); + return std::set_intersection(__first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_set_intersection(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, + _Compare __comp) noexcept +{ + return __internal::__brick_set_intersection(__first1, __last1, __first2, __last2, __result, __comp, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _RandomAccessIterator3, class _Compare> +_RandomAccessIterator3 +__pattern_set_intersection(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _RandomAccessIterator3 __result, _Compare __comp) +{ + typedef typename std::iterator_traits<_RandomAccessIterator3>::value_type _Tp; + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType; + + const auto __n1 = __last1 - __first1; + const auto __n2 = __last2 - __first2; + + // intersection is empty + if (__n1 == 0 || __n2 == 0) + return __result; + + // testing whether the sequences are intersected + _RandomAccessIterator1 __left_bound_seq_1 = std::lower_bound(__first1, __last1, *__first2, __comp); + //{1} < {2}: seq 2 is wholly greater than seq 1, so, the intersection is empty + if (__left_bound_seq_1 == __last1) + return __result; + + // testing whether the sequences are intersected + _RandomAccessIterator2 __left_bound_seq_2 = std::lower_bound(__first2, __last2, *__first1, __comp); + //{2} < {1}: seq 1 is wholly greater than seq 2, so, the intersection is empty + if (__left_bound_seq_2 == __last2) + return __result; + + const auto __m1 = __last1 - __left_bound_seq_1 + __n2; + if (__m1 > __set_algo_cut_off) + { + //we know proper offset due to [first1; left_bound_seq_1) < [first2; last2) + return __internal::__parallel_set_op( + __tag, std::forward<_ExecutionPolicy>(__exec), __left_bound_seq_1, __last1, __first2, __last2, __result, + __comp, [](_DifferenceType __n, _DifferenceType __m) { return std::min(__n, __m); }, + [](_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _Tp* __result, _Compare __comp) { + return __pstl::__utils::__set_intersection_construct(__first1, __last1, __first2, __last2, __result, + __comp); + }); + } + + const auto __m2 = __last2 - __left_bound_seq_2 + __n1; + if (__m2 > __set_algo_cut_off) + { + //we know proper offset due to [first2; left_bound_seq_2) < [first1; last1) + __result = __internal::__parallel_set_op( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __left_bound_seq_2, __last2, __result, + __comp, [](_DifferenceType __n, _DifferenceType __m) { return std::min(__n, __m); }, + [](_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _Tp* __result, _Compare __comp) { + return __pstl::__utils::__set_intersection_construct(__first2, __last2, __first1, __last1, __result, + __comp); + }); + return __result; + } + + // [left_bound_seq_1; last1) and [left_bound_seq_2; last2) - use serial algorithm + return std::set_intersection(__left_bound_seq_1, __last1, __left_bound_seq_2, __last2, __result, __comp); +} + +//------------------------------------------------------------------------ +// set_difference +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator +__brick_set_difference(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp, + /*__is_vector=*/std::false_type) noexcept +{ + return std::set_difference(__first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, class _Compare> +_RandomAccessIterator3 +__brick_set_difference(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _RandomAccessIterator3 __result, _Compare __comp, + /*__is_vector=*/std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial"); + return std::set_difference(__first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_set_difference(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, + _Compare __comp) noexcept +{ + return __internal::__brick_set_difference(__first1, __last1, __first2, __last2, __result, __comp, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _RandomAccessIterator3, class _Compare> +_RandomAccessIterator3 +__pattern_set_difference(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _RandomAccessIterator3 __result, _Compare __comp) +{ + typedef typename std::iterator_traits<_RandomAccessIterator3>::value_type _Tp; + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType; + + const auto __n1 = __last1 - __first1; + const auto __n2 = __last2 - __first2; + + // {} \ {2}: the difference is empty + if (__n1 == 0) + return __result; + + // {1} \ {}: parallel copying just first sequence + if (__n2 == 0) + return __internal::__pattern_walk2_brick( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __result, + [](_RandomAccessIterator1 __begin, _RandomAccessIterator1 __end, _RandomAccessIterator3 __res) + { return __internal::__brick_copy(__begin, __end, __res, _IsVector{}); }); + + // testing whether the sequences are intersected + _RandomAccessIterator1 __left_bound_seq_1 = std::lower_bound(__first1, __last1, *__first2, __comp); + //{1} < {2}: seq 2 is wholly greater than seq 1, so, parallel copying just first sequence + if (__left_bound_seq_1 == __last1) + return __internal::__pattern_walk2_brick( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __result, + [](_RandomAccessIterator1 __begin, _RandomAccessIterator1 __end, _RandomAccessIterator3 __res) + { return __internal::__brick_copy(__begin, __end, __res, _IsVector{}); }); + + // testing whether the sequences are intersected + _RandomAccessIterator2 __left_bound_seq_2 = std::lower_bound(__first2, __last2, *__first1, __comp); + //{2} < {1}: seq 1 is wholly greater than seq 2, so, parallel copying just first sequence + if (__left_bound_seq_2 == __last2) + return __internal::__pattern_walk2_brick( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __result, + [](_RandomAccessIterator1 __begin, _RandomAccessIterator1 __end, _RandomAccessIterator3 __res) + { return __internal::__brick_copy(__begin, __end, __res, _IsVector{}); }); + + if (__n1 + __n2 > __set_algo_cut_off) + return __parallel_set_op( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp, + [](_DifferenceType __n, _DifferenceType) { return __n; }, + [](_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _Tp* __result, _Compare __comp) + { + return __pstl::__utils::__set_difference_construct(__first1, __last1, __first2, __last2, __result, + __comp, __BrickCopyConstruct<_IsVector>()); + }); + + // use serial algorithm + return std::set_difference(__first1, __last1, __first2, __last2, __result, __comp); +} + +//------------------------------------------------------------------------ +// set_symmetric_difference +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, class _Compare> +_OutputIterator +__brick_set_symmetric_difference(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp, + /*__is_vector=*/std::false_type) noexcept +{ + return std::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, class _Compare> +_RandomAccessIterator3 +__brick_set_symmetric_difference(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, + _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _RandomAccessIterator3 __result, _Compare __comp, + /*__is_vector=*/std::true_type) noexcept +{ + _PSTL_PRAGMA_MESSAGE("Vectorized algorithm unimplemented, redirected to serial"); + return std::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _OutputIterator, + class _Compare> +_OutputIterator +__pattern_set_symmetric_difference(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _OutputIterator __result, + _Compare __comp) noexcept +{ + return __internal::__brick_set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _RandomAccessIterator3, class _Compare> +_RandomAccessIterator3 +__pattern_set_symmetric_difference(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, + _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, + _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _RandomAccessIterator3 __result, _Compare __comp) +{ + + const auto __n1 = __last1 - __first1; + const auto __n2 = __last2 - __first2; + + // use serial algorithm + if (__n1 + __n2 <= __set_algo_cut_off) + return std::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, __comp); + + typedef typename std::iterator_traits<_RandomAccessIterator3>::value_type _Tp; + return __internal::__parallel_set_union_op( + __tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp, + [](_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _Tp* __result, _Compare __comp) + { + return __pstl::__utils::__set_symmetric_difference_construct(__first1, __last1, __first2, __last2, __result, + __comp, __BrickCopyConstruct<_IsVector>()); + }); +} + +//------------------------------------------------------------------------ +// is_heap_until +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator, class _Compare> +_RandomAccessIterator +__brick_is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, + /* __is_vector = */ std::false_type) noexcept +{ + return std::is_heap_until(__first, __last, __comp); +} + +template <class _RandomAccessIterator, class _Compare> +_RandomAccessIterator +__brick_is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, + /* __is_vector = */ std::true_type) noexcept +{ + if (__last - __first < 2) + return __last; + typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _SizeType; + return __unseq_backend::__simd_first( + __first, _SizeType(0), __last - __first, + [&__comp](_RandomAccessIterator __it, _SizeType __i) { return __comp(__it[(__i - 1) / 2], __it[__i]); }); +} + +template <class _Tag, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +_RandomAccessIterator +__pattern_is_heap_until(_Tag, _ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, + _Compare __comp) noexcept +{ + return __internal::__brick_is_heap_until(__first, __last, __comp, typename _Tag::__is_vector{}); +} + +template <class _RandomAccessIterator, class _DifferenceType, class _Compare> +_RandomAccessIterator +__is_heap_until_local(_RandomAccessIterator __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp, + /* __is_vector = */ std::false_type) noexcept +{ + _DifferenceType __i = __begin; + for (; __i < __end; ++__i) + { + if (__comp(__first[(__i - 1) / 2], __first[__i])) + { + break; + } + } + return __first + __i; +} + +template <class _RandomAccessIterator, class _DifferenceType, class _Compare> +_RandomAccessIterator +__is_heap_until_local(_RandomAccessIterator __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp, + /* __is_vector = */ std::true_type) noexcept +{ + return __unseq_backend::__simd_first( + __first, __begin, __end, + [&__comp](_RandomAccessIterator __it, _DifferenceType __i) { return __comp(__it[(__i - 1) / 2], __it[__i]); }); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +_RandomAccessIterator +__pattern_is_heap_until(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Compare __comp) noexcept +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (__last - __first < 2) + return __last; + + return __internal::__except_handler( + [&]() + { + return __parallel_find( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__first, __comp](_RandomAccessIterator __i, _RandomAccessIterator __j) { + return __internal::__is_heap_until_local(__first, __i - __first, __j - __first, __comp, + _IsVector{}); + }, + std::less<typename std::iterator_traits<_RandomAccessIterator>::difference_type>(), /*is_first=*/true); + }); +} + +//------------------------------------------------------------------------ +// min_element +//------------------------------------------------------------------------ + +template <typename _ForwardIterator, typename _Compare> +_ForwardIterator +__brick_min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp, + /* __is_vector = */ std::false_type) noexcept +{ + return std::min_element(__first, __last, __comp); +} + +template <typename _RandomAccessIterator, typename _Compare> +_RandomAccessIterator +__brick_min_element(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, + /* __is_vector = */ std::true_type) noexcept +{ +#if defined(_PSTL_UDR_PRESENT) + return __unseq_backend::__simd_min_element(__first, __last - __first, __comp); +#else + return std::min_element(__first, __last, __comp); +#endif +} + +template <typename _Tag, typename _ExecutionPolicy, typename _ForwardIterator, typename _Compare> +_ForwardIterator +__pattern_min_element(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _Compare __comp) noexcept +{ + return __internal::__brick_min_element(__first, __last, __comp, typename _Tag::__is_vector{}); +} + +template <typename _IsVector, typename _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare> +_RandomAccessIterator +__pattern_min_element(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Compare __comp) +{ + if (__first == __last) + return __last; + + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + return __par_backend::__parallel_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first + 1, __last, __first, + [=](_RandomAccessIterator __begin, _RandomAccessIterator __end, + _RandomAccessIterator __init) -> _RandomAccessIterator + { + const _RandomAccessIterator subresult = + __internal::__brick_min_element(__begin, __end, __comp, _IsVector{}); + return __internal::__cmp_iterators_by_values(__init, subresult, __comp); + }, + [=](_RandomAccessIterator __it1, _RandomAccessIterator __it2) -> _RandomAccessIterator + { return __internal::__cmp_iterators_by_values(__it1, __it2, __comp); }); + }); +} + +//------------------------------------------------------------------------ +// minmax_element +//------------------------------------------------------------------------ + +template <typename _ForwardIterator, typename _Compare> +std::pair<_ForwardIterator, _ForwardIterator> +__brick_minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp, + /* __is_vector = */ std::false_type) noexcept +{ + return std::minmax_element(__first, __last, __comp); +} + +template <typename _RandomAccessIterator, typename _Compare> +std::pair<_RandomAccessIterator, _RandomAccessIterator> +__brick_minmax_element(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, + /* __is_vector = */ std::true_type) noexcept +{ +#if defined(_PSTL_UDR_PRESENT) + return __unseq_backend::__simd_minmax_element(__first, __last - __first, __comp); +#else + return std::minmax_element(__first, __last, __comp); +#endif +} + +template <typename _Tag, typename _ExecutionPolicy, typename _ForwardIterator, typename _Compare> +std::pair<_ForwardIterator, _ForwardIterator> +__pattern_minmax_element(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _Compare __comp) noexcept +{ + return __internal::__brick_minmax_element(__first, __last, __comp, typename _Tag::__is_vector{}); +} + +template <typename _IsVector, typename _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare> +std::pair<_RandomAccessIterator, _RandomAccessIterator> +__pattern_minmax_element(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Compare __comp) +{ + if (__first == __last) + return std::make_pair(__first, __first); + + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler([&]() { + typedef std::pair<_RandomAccessIterator, _RandomAccessIterator> _Result; + + return __par_backend::__parallel_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first + 1, __last, + std::make_pair(__first, __first), + [=](_RandomAccessIterator __begin, _RandomAccessIterator __end, _Result __init) -> _Result + { + const _Result __subresult = __internal::__brick_minmax_element(__begin, __end, __comp, _IsVector{}); + return std::make_pair( + __internal::__cmp_iterators_by_values(__subresult.first, __init.first, __comp), + __internal::__cmp_iterators_by_values(__init.second, __subresult.second, std::not_fn(__comp))); + }, + [=](_Result __p1, _Result __p2) -> _Result + { + return std::make_pair( + __internal::__cmp_iterators_by_values(__p1.first, __p2.first, __comp), + __internal::__cmp_iterators_by_values(__p2.second, __p1.second, std::not_fn(__comp))); + }); + }); +} + +//------------------------------------------------------------------------ +// mismatch +//------------------------------------------------------------------------ +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +std::pair<_ForwardIterator1, _ForwardIterator2> +__mismatch_serial(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _BinaryPredicate __pred) +{ +#if defined(_PSTL_CPP14_2RANGE_MISMATCH_EQUAL_PRESENT) + return std::mismatch(__first1, __last1, __first2, __last2, __pred); +#else + for (; __first1 != __last1 && __first2 != __last2 && __pred(*__first1, *__first2); ++__first1, ++__first2) + { + } + return std::make_pair(__first1, __first2); +#endif +} + +template <class _ForwardIterator1, class _ForwardIterator2, class _Predicate> +std::pair<_ForwardIterator1, _ForwardIterator2> +__brick_mismatch(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _Predicate __pred, /* __is_vector = */ std::false_type) noexcept +{ + return __mismatch_serial(__first1, __last1, __first2, __last2, __pred); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Predicate> +std::pair<_RandomAccessIterator1, _RandomAccessIterator2> +__brick_mismatch(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, + _RandomAccessIterator2 __last2, _Predicate __pred, /* __is_vector = */ std::true_type) noexcept +{ + auto __n = std::min(__last1 - __first1, __last2 - __first2); + return __unseq_backend::__simd_first(__first1, __n, __first2, std::not_fn(__pred)); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate> +std::pair<_ForwardIterator1, _ForwardIterator2> +__pattern_mismatch(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Predicate __pred) noexcept +{ + return __internal::__brick_mismatch(__first1, __last1, __first2, __last2, __pred, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Predicate> +std::pair<_RandomAccessIterator1, _RandomAccessIterator2> +__pattern_mismatch(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _Predicate __pred) noexcept +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler([&]() { + auto __n = std::min(__last1 - __first1, __last2 - __first2); + auto __result = __internal::__parallel_find( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __first1 + __n, + [__first1, __first2, __pred](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) + { + return __internal::__brick_mismatch(__i, __j, __first2 + (__i - __first1), __first2 + (__j - __first1), + __pred, _IsVector{}) + .first; + }, + std::less<typename std::iterator_traits<_RandomAccessIterator1>::difference_type>(), /*is_first=*/true); + return std::make_pair(__result, __first2 + (__result - __first1)); + }); +} + +//------------------------------------------------------------------------ +// lexicographical_compare +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _Compare> +bool +__brick_lexicographical_compare(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _Compare __comp, + /* __is_vector = */ std::false_type) noexcept +{ + return std::lexicographical_compare(__first1, __last1, __first2, __last2, __comp); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Compare> +bool +__brick_lexicographical_compare(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, + _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _Compare __comp, + /* __is_vector = */ std::true_type) noexcept +{ + if (__first2 == __last2) + { // if second sequence is empty + return false; + } + else if (__first1 == __last1) + { // if first sequence is empty + return true; + } + else + { + typedef typename std::iterator_traits<_RandomAccessIterator1>::reference ref_type1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::reference ref_type2; + --__last1; + --__last2; + auto __n = std::min(__last1 - __first1, __last2 - __first2); + std::pair<_RandomAccessIterator1, _RandomAccessIterator2> __result = __unseq_backend::__simd_first( + __first1, __n, __first2, [__comp](const ref_type1 __x, const ref_type2 __y) mutable { + return __comp(__x, __y) || __comp(__y, __x); + }); + + if (__result.first == __last1 && __result.second != __last2) + { // if first sequence shorter than second + return !__comp(*__result.second, *__result.first); + } + else + { // if second sequence shorter than first or both have the same number of elements + return __comp(*__result.first, *__result.second); + } + } +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare> +bool +__pattern_lexicographical_compare(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp) noexcept +{ + return __internal::__brick_lexicographical_compare(__first1, __last1, __first2, __last2, __comp, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Compare> +bool +__pattern_lexicographical_compare(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, + _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, + _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _Compare __comp) noexcept +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + if (__first2 == __last2) + { // if second sequence is empty + return false; + } + else if (__first1 == __last1) + { // if first sequence is empty + return true; + } + else + { + typedef typename std::iterator_traits<_RandomAccessIterator1>::reference _RefType1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::reference _RefType2; + --__last1; + --__last2; + auto __n = std::min(__last1 - __first1, __last2 - __first2); + auto __result = __internal::__parallel_find( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __first1 + __n, + [__first1, __first2, &__comp](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j) + { + return __internal::__brick_mismatch( + __i, __j, __first2 + (__i - __first1), __first2 + (__j - __first1), + [&__comp](const _RefType1 __x, const _RefType2 __y) + { return !__comp(__x, __y) && !__comp(__y, __x); }, + _IsVector{}) + .first; + }, + std::less<typename std::iterator_traits<_RandomAccessIterator1>::difference_type>(), /*is_first=*/true); + + if (__result == __last1 && __first2 + (__result - __first1) != __last2) + { // if first sequence shorter than second + return !__comp(*(__first2 + (__result - __first1)), *__result); + } + else + { // if second sequence shorter than first or both have the same number of elements + return __comp(*__result, *(__first2 + (__result - __first1))); + } + } +} + +} // namespace __internal +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_ALGORITHM_IMPL_H */ diff --git a/pstl/include/pstl/internal/execution_defs.h b/pstl/include/pstl/internal/execution_defs.h new file mode 100644 index 0000000..d7c4126 --- /dev/null +++ b/pstl/include/pstl/internal/execution_defs.h @@ -0,0 +1,100 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_EXECUTION_POLICY_DEFS_H +#define _PSTL_EXECUTION_POLICY_DEFS_H + +#include <type_traits> + +#include "pstl_config.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace execution +{ +inline namespace v1 +{ + +// 2.4, Sequential execution policy +class sequenced_policy +{ +}; + +// 2.5, Parallel execution policy +class parallel_policy +{ +}; + +// 2.6, Parallel+Vector execution policy +class parallel_unsequenced_policy +{ +}; + +class unsequenced_policy +{ +}; + +// 2.8, Execution policy objects +constexpr sequenced_policy seq{}; +constexpr parallel_policy par{}; +constexpr parallel_unsequenced_policy par_unseq{}; +constexpr unsequenced_policy unseq{}; + +// 2.3, Execution policy type trait +template <class T> +struct is_execution_policy : std::false_type +{ +}; + +template <> +struct is_execution_policy<__pstl::execution::sequenced_policy> : std::true_type +{ +}; +template <> +struct is_execution_policy<__pstl::execution::parallel_policy> : std::true_type +{ +}; +template <> +struct is_execution_policy<__pstl::execution::parallel_unsequenced_policy> : std::true_type +{ +}; +template <> +struct is_execution_policy<__pstl::execution::unsequenced_policy> : std::true_type +{ +}; + +#if defined(_PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT) +template <class T> +constexpr bool is_execution_policy_v = __pstl::execution::is_execution_policy<T>::value; +#endif + +} // namespace v1 +} // namespace execution + +namespace __internal +{ +template <class ExecPolicy, class T> +using __enable_if_execution_policy = + typename std::enable_if<__pstl::execution::is_execution_policy<typename std::decay<ExecPolicy>::type>::value, + T>::type; + +template <class _IsVector> +struct __serial_tag; +template <class _IsVector> +struct __parallel_tag; + +} // namespace __internal + +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_EXECUTION_POLICY_DEFS_H */ diff --git a/pstl/include/pstl/internal/execution_impl.h b/pstl/include/pstl/internal/execution_impl.h new file mode 100644 index 0000000..5dc622b --- /dev/null +++ b/pstl/include/pstl/internal/execution_impl.h @@ -0,0 +1,105 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_EXECUTION_IMPL_H +#define _PSTL_EXECUTION_IMPL_H + +#include <iterator> +#include <type_traits> + +#include "pstl_config.h" +#include "execution_defs.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __internal +{ + +template <typename _IteratorTag, typename... _IteratorTypes> +using __are_iterators_of = std::conjunction< + std::is_base_of<_IteratorTag, typename std::iterator_traits<std::decay_t<_IteratorTypes>>::iterator_category>...>; + +template <typename... _IteratorTypes> +using __are_random_access_iterators = __are_iterators_of<std::random_access_iterator_tag, _IteratorTypes...>; + +struct __serial_backend_tag +{ +}; +struct __tbb_backend_tag +{ +}; +struct __openmp_backend_tag +{ +}; + +#if defined(_PSTL_PAR_BACKEND_TBB) +using __par_backend_tag = __tbb_backend_tag; +#elif defined(_PSTL_PAR_BACKEND_OPENMP) +using __par_backend_tag = __openmp_backend_tag; +#elif defined(_PSTL_PAR_BACKEND_SERIAL) +using __par_backend_tag = __serial_backend_tag; +#else +# error "A parallel backend must be specified"; +#endif + +template <class _IsVector> +struct __serial_tag +{ + using __is_vector = _IsVector; +}; + +template <class _IsVector> +struct __parallel_tag +{ + using __is_vector = _IsVector; + // backend tag can be change depending on + // TBB availability in the environment + using __backend_tag = __par_backend_tag; +}; + +template <class _IsVector, class... _IteratorTypes> +using __tag_type = typename std::conditional<__internal::__are_random_access_iterators<_IteratorTypes...>::value, + __parallel_tag<_IsVector>, __serial_tag<_IsVector>>::type; + +template <class... _IteratorTypes> +__serial_tag</*_IsVector = */ std::false_type> +__select_backend(__pstl::execution::sequenced_policy, _IteratorTypes&&...) +{ + return {}; +} + +template <class... _IteratorTypes> +__serial_tag<__internal::__are_random_access_iterators<_IteratorTypes...>> +__select_backend(__pstl::execution::unsequenced_policy, _IteratorTypes&&...) +{ + return {}; +} + +template <class... _IteratorTypes> +__tag_type</*_IsVector = */ std::false_type, _IteratorTypes...> +__select_backend(__pstl::execution::parallel_policy, _IteratorTypes&&...) +{ + return {}; +} + +template <class... _IteratorTypes> +__tag_type<__internal::__are_random_access_iterators<_IteratorTypes...>, _IteratorTypes...> +__select_backend(__pstl::execution::parallel_unsequenced_policy, _IteratorTypes&&...) +{ + return {}; +} + +} // namespace __internal +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_EXECUTION_IMPL_H */ diff --git a/pstl/include/pstl/internal/glue_algorithm_defs.h b/pstl/include/pstl/internal/glue_algorithm_defs.h new file mode 100644 index 0000000..28a7f92 --- /dev/null +++ b/pstl/include/pstl/internal/glue_algorithm_defs.h @@ -0,0 +1,558 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_GLUE_ALGORITHM_DEFS_H +#define _PSTL_GLUE_ALGORITHM_DEFS_H + +#include <functional> +#include <iterator> + +#include "execution_defs.h" +#include "pstl_config.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace std +{ + +// [alg.any_of] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +any_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred); + +// [alg.all_of] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +all_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred); + +// [alg.none_of] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +none_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred); + +// [alg.foreach] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Function> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +for_each(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Function __f); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Function> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +for_each_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, _Function __f); + +// [alg.find] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +find_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +find_if_not(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value); + +// [alg.find.end] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last, _BinaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last); + +// [alg.find_first_of] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __s_first, _ForwardIterator2 __s_last); + +// [alg.adjacent_find] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +adjacent_find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +adjacent_find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred); + +// [alg.count] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, + typename iterator_traits<_ForwardIterator>::difference_type> +count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, + typename iterator_traits<_ForwardIterator>::difference_type> +count_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred); + +// [alg.search] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last, _BinaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count, + const _Tp& __value, _BinaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count, + const _Tp& __value); + +// [alg.copy] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +copy_n(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _Size __n, _ForwardIterator2 __result); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 result, + _Predicate __pred); + +// [alg.swap] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +swap_ranges(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2); + +// [alg.transform] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +transform(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + _UnaryOperation __op); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +transform(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator __result, _BinaryOperation __op); + +// [alg.replace] + +template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +replace_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred, + const _Tp& __new_value); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +replace(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, + const _Tp& __new_value); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryPredicate, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +replace_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _UnaryPredicate __pred, const _Tp& __new_value); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +replace_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + const _Tp& __old_value, const _Tp& __new_value); + +// [alg.fill] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __count, const _Tp& __value); + +// [alg.generate] +template <class _ExecutionPolicy, class _ForwardIterator, class _Generator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +generate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Generator __g); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Generator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +generate_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size count, _Generator __g); + +// [alg.remove] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +remove_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _Predicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +remove_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + const _Tp& __value); + +template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +remove_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +remove(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value); + +// [alg.unique] + +template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + _BinaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result); + +// [alg.reverse] + +template <class _ExecutionPolicy, class _BidirectionalIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +reverse(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last); + +template <class _ExecutionPolicy, class _BidirectionalIterator, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +reverse_copy(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last, + _ForwardIterator __d_first); + +// [alg.rotate] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +rotate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +rotate_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __middle, _ForwardIterator1 __last, + _ForwardIterator2 __result); + +// [alg.partitions] + +template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_partitioned(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +partition(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred); + +template <class _ExecutionPolicy, class _BidirectionalIterator, class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _BidirectionalIterator> +stable_partition(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last, + _UnaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator, class _ForwardIterator1, class _ForwardIterator2, + class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +partition_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, + _ForwardIterator1 __out_true, _ForwardIterator2 __out_false, _UnaryPredicate __pred); + +// [alg.sort] + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last); + +// [stable.sort] + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last); + +// [mismatch] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _BinaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _BinaryPredicate __pred); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2); + +// [alg.equal] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _BinaryPredicate __p); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _BinaryPredicate __p); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2); + +// [alg.move] +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first); + +// [partial.sort] + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle, + _RandomAccessIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle, + _RandomAccessIterator __last); + +// [partial.sort.copy] + +template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator> +partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, + _RandomAccessIterator __d_first, _RandomAccessIterator __d_last, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator> +partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, + _RandomAccessIterator __d_first, _RandomAccessIterator __d_last); + +// [is.sorted] +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +is_sorted_until(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +is_sorted_until(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +// [alg.nth.element] + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth, + _RandomAccessIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth, + _RandomAccessIterator __last); + +// [alg.merge] +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _ForwardIterator __d_first, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _ForwardIterator __d_first); + +template <class _ExecutionPolicy, class _BidirectionalIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle, + _BidirectionalIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _BidirectionalIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle, + _BidirectionalIterator __last); + +// [includes] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2); + +// [set.union] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _ForwardIterator __result); + +// [set.intersection] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result); + +// [set.difference] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result); + +// [set.symmetric.difference] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator result, + _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result); + +// [is.heap] +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator> +is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator> +is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last); + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_heap(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_heap(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last); + +// [alg.min.max] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator, _ForwardIterator>> +minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator, _ForwardIterator>> +minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +// [alg.lex.comparison] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2); + +} // namespace std + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_GLUE_ALGORITHM_DEFS_H */ diff --git a/pstl/include/pstl/internal/glue_algorithm_impl.h b/pstl/include/pstl/internal/glue_algorithm_impl.h new file mode 100644 index 0000000..fb0c19d --- /dev/null +++ b/pstl/include/pstl/internal/glue_algorithm_impl.h @@ -0,0 +1,1108 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_GLUE_ALGORITHM_IMPL_H +#define _PSTL_GLUE_ALGORITHM_IMPL_H + +#include <functional> + +#include "pstl_config.h" + +#include "execution_defs.h" +#include "utils.h" +#include "algorithm_fwd.h" +#include "numeric_fwd.h" /* count and count_if use __pattern_transform_reduce */ + +#include "execution_impl.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace std +{ + +// [alg.any_of] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +any_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_any_of(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __pred); +} + +// [alg.all_of] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Pred> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +all_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Pred __pred) +{ + return !std::any_of(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::not_fn(__pred)); +} + +// [alg.none_of] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +none_of(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + return !std::any_of(std::forward<_ExecutionPolicy>(__exec), __first, __last, __pred); +} + +// [alg.foreach] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Function> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +for_each(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Function __f) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__pattern_walk1(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, __f); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Function> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +for_each_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, _Function __f) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_walk1_n(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, + __f); +} + +// [alg.find] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +find_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_find_if(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +find_if_not(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + return std::find_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::not_fn(__pred)); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) +{ + return std::find_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, + __pstl::__internal::__equal_value<_Tp>(__value)); +} + +// [alg.find.end] +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last, _BinaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __s_first); + + return __pstl::__internal::__pattern_find_end(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __s_first, __s_last, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +find_end(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last) +{ + return std::find_end(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last, + std::equal_to<>()); +} + +// [alg.find_first_of] +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __s_first, _ForwardIterator2 __s_last, _BinaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __s_first); + + return __pstl::__internal::__pattern_find_first_of(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __s_first, __s_last, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +find_first_of(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __s_first, _ForwardIterator2 __s_last) +{ + return std::find_first_of(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last, + std::equal_to<>()); +} + +// [alg.adjacent_find] +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +adjacent_find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + return __pstl::__internal::__pattern_adjacent_find(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, std::equal_to<_ValueType>(), /*first_semantic*/ false); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +adjacent_find(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + return __pstl::__internal::__pattern_adjacent_find(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __pred, /*first_semantic*/ false); +} + +// [alg.count] + +// Implementation note: count and count_if call the pattern directly instead of calling std::transform_reduce +// so that we do not have to include <numeric>. + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, + typename iterator_traits<_ForwardIterator>::difference_type> +count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + return __pstl::__internal::__pattern_count(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [&__value](const _ValueType& __x) { return __value == __x; }); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, + typename iterator_traits<_ForwardIterator>::difference_type> +count_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + return __pstl::__internal::__pattern_count(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __pred); +} + +// [alg.search] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last, _BinaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __s_first); + + return __pstl::__internal::__pattern_search(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __s_first, __s_last, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator1> +search(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last) +{ + return std::search(std::forward<_ExecutionPolicy>(__exec), __first, __last, __s_first, __s_last, std::equal_to<>()); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count, + const _Tp& __value, _BinaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_search_n(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __count, __value, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +search_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Size __count, + const _Tp& __value) +{ + return std::search_n(std::forward<_ExecutionPolicy>(__exec), __first, __last, __count, __value, + std::equal_to<typename iterator_traits<_ForwardIterator>::value_type>()); +} + +// [alg.copy] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__pattern_walk2_brick( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, + [](_ForwardIterator1 __begin, _ForwardIterator1 __end, _ForwardIterator2 __res) + { return __pstl::__internal::__brick_copy(__begin, __end, __res, __is_vector{}); }); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _Size, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +copy_n(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _Size __n, _ForwardIterator2 __result) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__pattern_walk2_brick_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, __result, + [](_ForwardIterator1 __begin, _Size __sz, _ForwardIterator2 __res) + { return __pstl::__internal::__brick_copy_n(__begin, __sz, __res, __is_vector{}); }); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + _Predicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + return __pstl::__internal::__pattern_copy_if(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __result, __pred); +} + +// [alg.swap] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +swap_ranges(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2) +{ + typedef typename iterator_traits<_ForwardIterator1>::reference _ReferenceType1; + typedef typename iterator_traits<_ForwardIterator2>::reference _ReferenceType2; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2); + + return __pstl::__internal::__pattern_walk2(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, + __last1, __first2, + [](_ReferenceType1 __x, _ReferenceType2 __y) + { + using std::swap; + swap(__x, __y); + }); +} + +// [alg.transform] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +transform(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + _UnaryOperation __op) +{ + typedef typename iterator_traits<_ForwardIterator1>::reference _InputType; + typedef typename iterator_traits<_ForwardIterator2>::reference _OutputType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + return __pstl::__internal::__pattern_walk2(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __result, + [__op](_InputType __x, _OutputType __y) mutable { __y = __op(__x); }); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +transform(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator __result, _BinaryOperation __op) +{ + typedef typename iterator_traits<_ForwardIterator1>::reference _Input1Type; + typedef typename iterator_traits<_ForwardIterator2>::reference _Input2Type; + typedef typename iterator_traits<_ForwardIterator>::reference _OutputType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2, __result); + + return __pstl::__internal::__pattern_walk3( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __result, + [__op](_Input1Type x, _Input2Type y, _OutputType z) mutable { z = __op(x, y); }); +} + +// [alg.replace] + +template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +replace_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred, + const _Tp& __new_value) +{ + typedef typename iterator_traits<_ForwardIterator>::reference _ElementType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__pattern_walk1(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [&__pred, &__new_value](_ElementType __elem) + { + if (__pred(__elem)) + { + __elem = __new_value; + } + }); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +replace(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, + const _Tp& __new_value) +{ + std::replace_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, + __pstl::__internal::__equal_value<_Tp>(__old_value), __new_value); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryPredicate, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +replace_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _UnaryPredicate __pred, const _Tp& __new_value) +{ + typedef typename iterator_traits<_ForwardIterator1>::reference _InputType; + typedef typename iterator_traits<_ForwardIterator2>::reference _OutputType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + return __pstl::__internal::__pattern_walk2( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, + [__pred, &__new_value](_InputType __x, _OutputType __y) mutable { __y = __pred(__x) ? __new_value : __x; }); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +replace_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + const _Tp& __old_value, const _Tp& __new_value) +{ + return std::replace_copy_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, + __pstl::__internal::__equal_value<_Tp>(__old_value), __new_value); +} + +// [alg.fill] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__pattern_fill(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __value); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __count, const _Tp& __value) +{ + if (__count <= 0) + return __first; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_fill_n(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __count, __value); +} + +// [alg.generate] +template <class _ExecutionPolicy, class _ForwardIterator, class _Generator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +generate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Generator __g) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__pattern_generate(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __g); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Generator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +generate_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __count, _Generator __g) +{ + if (__count <= 0) + return __first; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_generate_n(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __count, __g); +} + +// [alg.remove] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +remove_copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _Predicate __pred) +{ + return std::copy_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, std::not_fn(__pred)); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +remove_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + const _Tp& __value) +{ + return std::copy_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, + __pstl::__internal::__not_equal_value<_Tp>(__value)); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +remove_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_remove_if(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +remove(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) +{ + return std::remove_if(std::forward<_ExecutionPolicy>(__exec), __first, __last, + __pstl::__internal::__equal_value<_Tp>(__value)); +} + +// [alg.unique] + +template <class _ExecutionPolicy, class _ForwardIterator, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_unique(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + return std::unique(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::equal_to<>()); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result, + _BinaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + return __pstl::__internal::__pattern_unique_copy(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __result, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +unique_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __result) +{ + return std::unique_copy(__exec, __first, __last, __result, std::equal_to<>()); +} + +// [alg.reverse] + +template <class _ExecutionPolicy, class _BidirectionalIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +reverse(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__pattern_reverse(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last); +} + +template <class _ExecutionPolicy, class _BidirectionalIterator, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +reverse_copy(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last, + _ForwardIterator __d_first) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __d_first); + + return __pstl::__internal::__pattern_reverse_copy(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __d_first); +} + +// [alg.rotate] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +rotate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_rotate(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __middle, __last); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +rotate_copy(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __middle, _ForwardIterator1 __last, + _ForwardIterator2 __result) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + return __pstl::__internal::__pattern_rotate_copy(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __middle, __last, __result); +} + +// [alg.partitions] + +template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_partitioned(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + return __pstl::__internal::__pattern_is_partitioned(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +partition(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_partition(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __pred); +} + +template <class _ExecutionPolicy, class _BidirectionalIterator, class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _BidirectionalIterator> +stable_partition(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __last, + _UnaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + return __pstl::__internal::__pattern_stable_partition(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first, __last, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _ForwardIterator1, class _ForwardIterator2, + class _UnaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +partition_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, + _ForwardIterator1 __out_true, _ForwardIterator2 __out_false, _UnaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __out_true, __out_false); + + return __pstl::__internal::__pattern_partition_copy(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __out_true, __out_false, __pred); +} + +// [alg.sort] + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + typedef typename iterator_traits<_RandomAccessIterator>::value_type _InputType; + return __pstl::__internal::__pattern_sort(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __comp, typename std::is_move_constructible<_InputType>::type()); +} + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last) +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _InputType; + std::sort(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>()); +} + +// [stable.sort] + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_stable_sort(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __comp); +} + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +stable_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last) +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _InputType; + std::stable_sort(__exec, __first, __last, std::less<_InputType>()); +} + +// [mismatch] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _BinaryPredicate __pred) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2); + + return __pstl::__internal::__pattern_mismatch(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, + __last1, __first2, __last2, __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _BinaryPredicate __pred) +{ + return std::mismatch(__exec, __first1, __last1, __first2, std::next(__first2, std::distance(__first1, __last1)), + __pred); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2) +{ + return std::mismatch(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, + std::equal_to<>()); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator1, _ForwardIterator2>> +mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) +{ + //TODO: to get rid of "distance" + return std::mismatch(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, + std::next(__first2, std::distance(__first1, __last1))); +} + +// [alg.equal] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _BinaryPredicate __p) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2); + + return __pstl::__internal::__pattern_equal(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, + __last1, __first2, __p); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) +{ + return std::equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, std::equal_to<>()); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _BinaryPredicate __p) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2); + + return __pstl::__internal::__pattern_equal(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, + __last1, __first2, __last2, __p); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +equal(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2) +{ + return equal(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, std::equal_to<>()); +} + +// [alg.move] +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __d_first); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__pattern_walk2_brick( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first, + [](_ForwardIterator1 __begin, _ForwardIterator1 __end, _ForwardIterator2 __res) + { return __pstl::__internal::__brick_move(__begin, __end, __res, __is_vector{}); }); +} + +// [partial.sort] + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle, + _RandomAccessIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__pattern_partial_sort(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __middle, __last, __comp); +} + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +partial_sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __middle, + _RandomAccessIterator __last) +{ + typedef typename iterator_traits<_RandomAccessIterator>::value_type _InputType; + std::partial_sort(__exec, __first, __middle, __last, std::less<_InputType>()); +} + +// [partial.sort.copy] + +template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator> +partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, + _RandomAccessIterator __d_first, _RandomAccessIterator __d_last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __d_first); + + return __pstl::__internal::__pattern_partial_sort_copy(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first, __last, __d_first, __d_last, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator> +partial_sort_copy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, + _RandomAccessIterator __d_first, _RandomAccessIterator __d_last) +{ + return std::partial_sort_copy(std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first, __d_last, + std::less<>()); +} + +// [is.sorted] +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +is_sorted_until(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + const _ForwardIterator __res = + __pstl::__internal::__pattern_adjacent_find(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __pstl::__internal::__reorder_pred<_Compare>(__comp), + /*first_semantic*/ false); + return __res == __last ? __last : std::next(__res); +} + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +is_sorted_until(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename std::iterator_traits<_ForwardIterator>::value_type _InputType; + return is_sorted_until(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>()); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + return __pstl::__internal::__pattern_adjacent_find(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __pstl::__internal::__reorder_pred<_Compare>(__comp), + /*or_semantic*/ true) == __last; +} + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_sorted(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename std::iterator_traits<_ForwardIterator>::value_type _InputType; + return std::is_sorted(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>()); +} + +// [alg.merge] +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _ForwardIterator __d_first, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2, __d_first); + + return __pstl::__internal::__pattern_merge(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, + __last1, __first2, __last2, __d_first, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _ForwardIterator __d_first) +{ + return std::merge(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __d_first, + std::less<>()); +} + +template <class _ExecutionPolicy, class _BidirectionalIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle, + _BidirectionalIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__pattern_inplace_merge(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __middle, __last, __comp); +} + +template <class _ExecutionPolicy, class _BidirectionalIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +inplace_merge(_ExecutionPolicy&& __exec, _BidirectionalIterator __first, _BidirectionalIterator __middle, + _BidirectionalIterator __last) +{ + typedef typename std::iterator_traits<_BidirectionalIterator>::value_type _InputType; + std::inplace_merge(__exec, __first, __middle, __last, std::less<_InputType>()); +} + +// [includes] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2); + + return __pstl::__internal::__pattern_includes(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, + __last1, __first2, __last2, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2) +{ + return std::includes(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, std::less<>()); +} + +// [set.union] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2, __result); + + return __pstl::__internal::__pattern_set_union(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, + __last1, __first2, __last2, __result, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_union(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _ForwardIterator __result) +{ + return std::set_union(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, + std::less<>()); +} + +// [set.intersection] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2, __result); + + return __pstl::__internal::__pattern_set_intersection(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_intersection(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result) +{ + return std::set_intersection(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, + std::less<>()); +} + +// [set.difference] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2, __result); + + return __pstl::__internal::__pattern_set_difference(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result) +{ + return std::set_difference(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, + std::less<>()); +} + +// [set.symmetric.difference] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, + class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result, + _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2, __result); + + return __pstl::__internal::__pattern_set_symmetric_difference( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, __result, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +set_symmetric_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _ForwardIterator __result) +{ + return std::set_symmetric_difference(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, + __result, std::less<>()); +} + +// [is.heap] +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator> +is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__pattern_is_heap_until(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __comp); +} + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _RandomAccessIterator> +is_heap_until(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last) +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _InputType; + return std::is_heap_until(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>()); +} + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_heap(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) +{ + return std::is_heap_until(std::forward<_ExecutionPolicy>(__exec), __first, __last, __comp) == __last; +} + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +is_heap(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last) +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _InputType; + return std::is_heap(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>()); +} + +// [alg.min.max] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + return __pstl::__internal::__pattern_min_element(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +min_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename std::iterator_traits<_ForwardIterator>::value_type _InputType; + return std::min_element(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_InputType>()); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + return min_element(std::forward<_ExecutionPolicy>(__exec), __first, __last, + __pstl::__internal::__reorder_pred<_Compare>(__comp)); +} + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +max_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename std::iterator_traits<_ForwardIterator>::value_type _InputType; + return std::min_element(std::forward<_ExecutionPolicy>(__exec), __first, __last, + __pstl::__internal::__reorder_pred<std::less<_InputType>>(std::less<_InputType>())); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator, _ForwardIterator>> +minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + return __pstl::__internal::__pattern_minmax_element(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, std::pair<_ForwardIterator, _ForwardIterator>> +minmax_element(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + return std::minmax_element(std::forward<_ExecutionPolicy>(__exec), __first, __last, std::less<_ValueType>()); +} + +// [alg.nth.element] + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth, + _RandomAccessIterator __last, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__pattern_nth_element(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __nth, + __last, __comp); +} + +template <class _ExecutionPolicy, class _RandomAccessIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +nth_element(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __nth, + _RandomAccessIterator __last) +{ + typedef typename iterator_traits<_RandomAccessIterator>::value_type _InputType; + std::nth_element(std::forward<_ExecutionPolicy>(__exec), __first, __nth, __last, std::less<_InputType>()); +} + +// [alg.lex.comparison] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Compare> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _Compare __comp) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2); + + return __pstl::__internal::__pattern_lexicographical_compare(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first1, __last1, __first2, __last2, __comp); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, bool> +lexicographical_compare(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2) +{ + return std::lexicographical_compare(std::forward<_ExecutionPolicy>(__exec), __first1, __last1, __first2, __last2, + std::less<>()); +} + +} // namespace std + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_GLUE_ALGORITHM_IMPL_H */ diff --git a/pstl/include/pstl/internal/glue_execution_defs.h b/pstl/include/pstl/internal/glue_execution_defs.h new file mode 100644 index 0000000..df9a477 --- /dev/null +++ b/pstl/include/pstl/internal/glue_execution_defs.h @@ -0,0 +1,55 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_GLUE_EXECUTION_DEFS_H +#define _PSTL_GLUE_EXECUTION_DEFS_H + +#include <type_traits> + +#include "execution_defs.h" +#include "pstl_config.h" + +namespace std +{ +// Type trait +using __pstl::execution::is_execution_policy; +#if defined(_PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT) +# if defined(__INTEL_COMPILER) +template <class T> +constexpr bool is_execution_policy_v = is_execution_policy<T>::value; +# else +using __pstl::execution::is_execution_policy_v; +# endif +#endif + +namespace execution +{ +// Standard C++ policy classes +using __pstl::execution::parallel_policy; +using __pstl::execution::parallel_unsequenced_policy; +using __pstl::execution::sequenced_policy; + +// Standard predefined policy instances +using __pstl::execution::par; +using __pstl::execution::par_unseq; +using __pstl::execution::seq; + +// Implementation-defined names +// Unsequenced policy is not yet standard, but for consistency +// we include it into namespace std::execution as well +using __pstl::execution::unseq; +using __pstl::execution::unsequenced_policy; +} // namespace execution +} // namespace std + +#include "algorithm_impl.h" +#include "numeric_impl.h" +#include "parallel_backend.h" + +#endif /* _PSTL_GLUE_EXECUTION_DEFS_H */ diff --git a/pstl/include/pstl/internal/glue_memory_defs.h b/pstl/include/pstl/internal/glue_memory_defs.h new file mode 100644 index 0000000..ae52333 --- /dev/null +++ b/pstl/include/pstl/internal/glue_memory_defs.h @@ -0,0 +1,85 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_GLUE_MEMORY_DEFS_H +#define _PSTL_GLUE_MEMORY_DEFS_H + +#include "execution_defs.h" +#include "pstl_config.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace std +{ + +// [uninitialized.copy] + +template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_copy(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result); + +template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_copy_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result); + +// [uninitialized.move] + +template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_move(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result); + +template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_move_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result); + +// [uninitialized.fill] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +uninitialized_fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, const _Tp& __value); + +// [specialized.destroy] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +destroy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +destroy_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n); + +// [uninitialized.construct.default] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +uninitialized_default_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_default_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n); + +// [uninitialized.construct.value] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +uninitialized_value_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_value_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n); + +} // namespace std + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_GLUE_MEMORY_DEFS_H */ diff --git a/pstl/include/pstl/internal/glue_memory_impl.h b/pstl/include/pstl/internal/glue_memory_impl.h new file mode 100644 index 0000000..39c595d --- /dev/null +++ b/pstl/include/pstl/internal/glue_memory_impl.h @@ -0,0 +1,352 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_GLUE_MEMORY_IMPL_H +#define _PSTL_GLUE_MEMORY_IMPL_H + +#include "pstl_config.h" + +#include "execution_defs.h" +#include "utils.h" +#include "algorithm_fwd.h" + +#include "execution_impl.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace std +{ + +// [uninitialized.copy] + +template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_copy(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result) +{ + typedef typename iterator_traits<_InputIterator>::value_type _ValueType1; + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2; + typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__invoke_if_else( + std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (), + [&]() + { + return __pstl::__internal::__pattern_walk2_brick( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, + [](_InputIterator __begin, _InputIterator __end, _ForwardIterator __res) + { return __pstl::__internal::__brick_copy(__begin, __end, __res, __is_vector{}); }); + }, + [&]() + { + return __pstl::__internal::__pattern_walk2(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __result, + [](_ReferenceType1 __val1, _ReferenceType2 __val2) + { ::new (std::addressof(__val2)) _ValueType2(__val1); }); + }); +} + +template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_copy_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result) +{ + typedef typename iterator_traits<_InputIterator>::value_type _ValueType1; + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2; + typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__invoke_if_else( + std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (), + [&]() + { + return __pstl::__internal::__pattern_walk2_brick_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, __result, + [](_InputIterator __begin, _Size __sz, _ForwardIterator __res) + { return __pstl::__internal::__brick_copy_n(__begin, __sz, __res, __is_vector{}); }); + }, + [&]() + { + return __pstl::__internal::__pattern_walk2_n(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first, __n, __result, + [](_ReferenceType1 __val1, _ReferenceType2 __val2) + { ::new (std::addressof(__val2)) _ValueType2(__val1); }); + }); +} + +// [uninitialized.move] + +template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_move(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result) +{ + typedef typename iterator_traits<_InputIterator>::value_type _ValueType1; + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2; + typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__invoke_if_else( + std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (), + [&]() + { + return __pstl::__internal::__pattern_walk2_brick( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, + [](_InputIterator __begin, _InputIterator __end, _ForwardIterator __res) + { return __pstl::__internal::__brick_copy(__begin, __end, __res, __is_vector{}); }); + }, + [&]() + { + return __pstl::__internal::__pattern_walk2( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, + [](_ReferenceType1 __val1, _ReferenceType2 __val2) + { ::new (std::addressof(__val2)) _ValueType2(std::move(__val1)); }); + }); +} + +template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_move_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result) +{ + typedef typename iterator_traits<_InputIterator>::value_type _ValueType1; + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2; + typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__invoke_if_else( + std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (), + [&]() + { + return __pstl::__internal::__pattern_walk2_brick_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, __result, + [](_InputIterator __begin, _Size __sz, _ForwardIterator __res) + { return __pstl::__internal::__brick_copy_n(__begin, __sz, __res, __is_vector{}); }); + }, + [&]() + { + return __pstl::__internal::__pattern_walk2_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, __result, + [](_ReferenceType1 __val1, _ReferenceType2 __val2) + { ::new (std::addressof(__val2)) _ValueType2(std::move(__val1)); }); + }); +} + +// [uninitialized.fill] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +uninitialized_fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + __pstl::__internal::__invoke_if_else( + std::is_arithmetic<_ValueType>(), + [&]() + { + __pstl::__internal::__pattern_walk_brick( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [&__value](_ForwardIterator __begin, _ForwardIterator __end) + { __pstl::__internal::__brick_fill(__begin, __end, _ValueType(__value), __is_vector{}); }); + }, + [&]() + { + __pstl::__internal::__pattern_walk1(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [&__value](_ReferenceType __val) + { ::new (std::addressof(__val)) _ValueType(__value); }); + }); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, const _Tp& __value) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__invoke_if_else( + std::is_arithmetic<_ValueType>(), + [&]() + { + return __pstl::__internal::__pattern_walk_brick_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, + [&__value](_ForwardIterator __begin, _Size __count) + { return __pstl::__internal::__brick_fill_n(__begin, __count, _ValueType(__value), __is_vector{}); }); + }, + [&]() + { + return __pstl::__internal::__pattern_walk1_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, + [&__value](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(__value); }); + }); +} + +// [specialized.destroy] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +destroy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__invoke_if_not(std::is_trivially_destructible<_ValueType>(), + [&]() + { + __pstl::__internal::__pattern_walk1( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [](_ReferenceType __val) { __val.~_ValueType(); }); + }); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +destroy_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__invoke_if_else( + std::is_trivially_destructible<_ValueType>(), [&]() { return std::next(__first, __n); }, + [&]() + { + return __pstl::__internal::__pattern_walk1_n(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first, __n, + [](_ReferenceType __val) { __val.~_ValueType(); }); + }); +} + +// [uninitialized.construct.default] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +uninitialized_default_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + __pstl::__internal::__invoke_if_not(std::is_trivial<_ValueType>(), + [&]() + { + __pstl::__internal::__pattern_walk1( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType; }); + }); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_default_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + return __pstl::__internal::__invoke_if_else( + std::is_trivial<_ValueType>(), [&]() { return std::next(__first, __n); }, + [&]() + { + return __pstl::__internal::__pattern_walk1_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, + [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType; }); + }); +} + +// [uninitialized.construct.value] + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> +uninitialized_value_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + __pstl::__internal::__invoke_if_else( + std::is_trivial<_ValueType>(), + [&]() + { + __pstl::__internal::__pattern_walk_brick( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [](_ForwardIterator __begin, _ForwardIterator __end) + { __pstl::__internal::__brick_fill(__begin, __end, _ValueType(), __is_vector{}); }); + }, + [&]() + { + __pstl::__internal::__pattern_walk1(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [](_ReferenceType __val) + { ::new (std::addressof(__val)) _ValueType(); }); + }); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Size> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> +uninitialized_value_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + + using __is_vector = typename decltype(__dispatch_tag)::__is_vector; + + return __pstl::__internal::__invoke_if_else( + std::is_trivial<_ValueType>(), + [&]() + { + return __pstl::__internal::__pattern_walk_brick_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, + [](_ForwardIterator __begin, _Size __count) + { return __pstl::__internal::__brick_fill_n(__begin, __count, _ValueType(), __is_vector{}); }); + }, + [&]() + { + return __pstl::__internal::__pattern_walk1_n( + __dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __n, + [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(); }); + }); +} + +} // namespace std + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_GLUE_MEMORY_IMPL_H */ diff --git a/pstl/include/pstl/internal/glue_numeric_defs.h b/pstl/include/pstl/internal/glue_numeric_defs.h new file mode 100644 index 0000000..86cd38b --- /dev/null +++ b/pstl/include/pstl/internal/glue_numeric_defs.h @@ -0,0 +1,124 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_GLUE_NUMERIC_DEFS_H +#define _PSTL_GLUE_NUMERIC_DEFS_H + +#include <iterator> + +#include "execution_defs.h" +#include "pstl_config.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace std +{ +// [reduce] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init, + _BinaryOperation __binary_op); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init); + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, + typename iterator_traits<_ForwardIterator>::value_type> +reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _Tp __init); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, + class _BinaryOperation2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, + _BinaryOperation2 __binary_op2); + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init, + _BinaryOperation __binary_op, _UnaryOperation __unary_op); + +// [exclusive.scan] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _Tp __init); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _Tp __init, _BinaryOperation __binary_op); + +// [inclusive.scan] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _BinaryOperation __binary_op); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _BinaryOperation __binary_op, _Tp __init); + +// [transform.exclusive.scan] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation, + class _UnaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +transform_exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _Tp __init, _BinaryOperation __binary_op, + _UnaryOperation __unary_op); + +// [transform.inclusive.scan] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation, + class _UnaryOperation, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +transform_inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op, + _Tp __init); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryOperation, + class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +transform_inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op); + +// [adjacent.difference] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __d_first, _BinaryOperation op); + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __d_first); + +} // namespace std + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_GLUE_NUMERIC_DEFS_H */ diff --git a/pstl/include/pstl/internal/glue_numeric_impl.h b/pstl/include/pstl/internal/glue_numeric_impl.h new file mode 100644 index 0000000..ad268b5 --- /dev/null +++ b/pstl/include/pstl/internal/glue_numeric_impl.h @@ -0,0 +1,232 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_GLUE_NUMERIC_IMPL_H +#define _PSTL_GLUE_NUMERIC_IMPL_H + +#include <functional> + +#include "pstl_config.h" + +#include "utils.h" +#include "numeric_fwd.h" +#include "execution_impl.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace std +{ + +// [reduce] + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init, + _BinaryOperation __binary_op) +{ + return transform_reduce(std::forward<_ExecutionPolicy>(__exec), __first, __last, __init, __binary_op, + __pstl::__internal::__no_op()); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init) +{ + return transform_reduce(std::forward<_ExecutionPolicy>(__exec), __first, __last, __init, std::plus<_Tp>(), + __pstl::__internal::__no_op()); +} + +template <class _ExecutionPolicy, class _ForwardIterator> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, + typename iterator_traits<_ForwardIterator>::value_type> +reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + return transform_reduce(std::forward<_ExecutionPolicy>(__exec), __first, __last, _ValueType{}, + std::plus<_ValueType>(), __pstl::__internal::__no_op()); +} + +// [transform.reduce] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _Tp __init) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2); + + typedef typename iterator_traits<_ForwardIterator1>::value_type _InputType; + return __pstl::__internal::__pattern_transform_reduce(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first1, __last1, __first2, __init, std::plus<_InputType>(), + std::multiplies<_InputType>()); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, + class _BinaryOperation2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first1, __first2); + return __pstl::__internal::__pattern_transform_reduce(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first1, __last1, __first2, __init, __binary_op1, + __binary_op2); +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _Tp> +transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init, + _BinaryOperation __binary_op, _UnaryOperation __unary_op) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first); + return __pstl::__internal::__pattern_transform_reduce(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first, __last, __init, __binary_op, __unary_op); +} + +// [exclusive.scan] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _Tp __init) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + using namespace __pstl; + return __internal::__pattern_transform_scan(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __result, __pstl::__internal::__no_op(), __init, std::plus<_Tp>(), + /*inclusive=*/std::false_type()); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _Tp __init, _BinaryOperation __binary_op) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + using namespace __pstl; + return __internal::__pattern_transform_scan(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + __result, __pstl::__internal::__no_op(), __init, __binary_op, + /*inclusive=*/std::false_type()); +} + +// [inclusive.scan] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result) +{ + typedef typename iterator_traits<_ForwardIterator1>::value_type _InputType; + return transform_inclusive_scan(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, + std::plus<_InputType>(), __pstl::__internal::__no_op()); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _BinaryOperation __binary_op) +{ + return transform_inclusive_scan(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __binary_op, + __pstl::__internal::__no_op()); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _BinaryOperation __binary_op, _Tp __init) +{ + return transform_inclusive_scan(std::forward<_ExecutionPolicy>(__exec), __first, __last, __result, __binary_op, + __pstl::__internal::__no_op(), __init); +} + +// [transform.exclusive.scan] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation, + class _UnaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +transform_exclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _Tp __init, _BinaryOperation __binary_op, + _UnaryOperation __unary_op) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + return __pstl::__internal::__pattern_transform_scan(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __result, __unary_op, __init, __binary_op, + /*inclusive=*/std::false_type()); +} + +// [transform.inclusive.scan] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation, + class _UnaryOperation, class _Tp> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +transform_inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op, + _Tp __init) +{ + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __result); + + return __pstl::__internal::__pattern_transform_scan(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), __first, + __last, __result, __unary_op, __init, __binary_op, + /*inclusive=*/std::true_type()); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _UnaryOperation, + class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +transform_inclusive_scan(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op) +{ + if (__first != __last) + { + auto __tmp = __unary_op(*__first); + *__result = __tmp; + return transform_inclusive_scan(std::forward<_ExecutionPolicy>(__exec), ++__first, __last, ++__result, + __binary_op, __unary_op, __tmp); + } + else + { + return __result; + } +} + +// [adjacent.difference] + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __d_first, _BinaryOperation __op) +{ + + if (__first == __last) + return __d_first; + + auto __dispatch_tag = __pstl::__internal::__select_backend(__exec, __first, __d_first); + + return __pstl::__internal::__pattern_adjacent_difference(__dispatch_tag, std::forward<_ExecutionPolicy>(__exec), + __first, __last, __d_first, __op); +} + +template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> +__pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> +adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, + _ForwardIterator2 __d_first) +{ + typedef typename iterator_traits<_ForwardIterator1>::value_type _ValueType; + return adjacent_difference(std::forward<_ExecutionPolicy>(__exec), __first, __last, __d_first, + std::minus<_ValueType>()); +} + +} // namespace std + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_GLUE_NUMERIC_IMPL_H_ */ diff --git a/pstl/include/pstl/internal/memory_impl.h b/pstl/include/pstl/internal/memory_impl.h new file mode 100644 index 0000000..942a30e --- /dev/null +++ b/pstl/include/pstl/internal/memory_impl.h @@ -0,0 +1,112 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_MEMORY_IMPL_H +#define _PSTL_MEMORY_IMPL_H + +#include <iterator> + +#include "pstl_config.h" +#include "unseq_backend_simd.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __internal +{ + +//------------------------------------------------------------------------ +// uninitialized_move +//------------------------------------------------------------------------ + +template <typename _ForwardIterator, typename _OutputIterator> +_OutputIterator +__brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, + /*vector=*/std::false_type) noexcept +{ + using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type; + for (; __first != __last; ++__first, ++__result) + { + ::new (std::addressof(*__result)) _ValueType(std::move(*__first)); + } + return __result; +} + +template <typename _RandomAccessIterator, typename _OutputIterator> +_OutputIterator +__brick_uninitialized_move(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result, + /*vector=*/std::true_type) noexcept +{ + using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type; + using _ReferenceType1 = typename std::iterator_traits<_RandomAccessIterator>::reference; + using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference; + + return __unseq_backend::__simd_walk_2( + __first, __last - __first, __result, + [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(std::move(__x)); }); +} + +template <typename _Iterator> +void +__brick_destroy(_Iterator __first, _Iterator __last, /*vector*/ std::false_type) noexcept +{ + using _ValueType = typename std::iterator_traits<_Iterator>::value_type; + + for (; __first != __last; ++__first) + __first->~_ValueType(); +} + +template <typename _RandomAccessIterator> +void +__brick_destroy(_RandomAccessIterator __first, _RandomAccessIterator __last, /*vector*/ std::true_type) noexcept +{ + using _ValueType = typename std::iterator_traits<_RandomAccessIterator>::value_type; + using _ReferenceType = typename std::iterator_traits<_RandomAccessIterator>::reference; + + __unseq_backend::__simd_walk_1(__first, __last - __first, [](_ReferenceType __x) { __x.~_ValueType(); }); +} + +//------------------------------------------------------------------------ +// uninitialized copy +//------------------------------------------------------------------------ + +template <typename _ForwardIterator, typename _OutputIterator> +_OutputIterator +__brick_uninitialized_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, + /*vector=*/std::false_type) noexcept +{ + using _ValueType = typename std::iterator_traits<_OutputIterator>::value_type; + for (; __first != __last; ++__first, ++__result) + { + ::new (std::addressof(*__result)) _ValueType(*__first); + } + return __result; +} + +template <typename _RandomAccessIterator, typename _OutputIterator> +_OutputIterator +__brick_uninitialized_copy(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result, + /*vector=*/std::true_type) noexcept +{ + using __ValueType = typename std::iterator_traits<_OutputIterator>::value_type; + using _ReferenceType1 = typename std::iterator_traits<_RandomAccessIterator>::reference; + using _ReferenceType2 = typename std::iterator_traits<_OutputIterator>::reference; + + return __unseq_backend::__simd_walk_2( + __first, __last - __first, __result, + [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType(__x); }); +} + +} // namespace __internal +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_MEMORY_IMPL_H */ diff --git a/pstl/include/pstl/internal/numeric_fwd.h b/pstl/include/pstl/internal/numeric_fwd.h new file mode 100644 index 0000000..7f7845b --- /dev/null +++ b/pstl/include/pstl/internal/numeric_fwd.h @@ -0,0 +1,139 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_NUMERIC_FWD_H +#define _PSTL_NUMERIC_FWD_H + +#include <type_traits> +#include <utility> + +#include "pstl_config.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __internal +{ + +//------------------------------------------------------------------------ +// transform_reduce (version with two binary functions, according to draft N4659) +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Tp, class _BinaryOperation1, + class _BinaryOperation2> +_Tp __brick_transform_reduce(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Tp, + _BinaryOperation1, _BinaryOperation2, + /*__is_vector=*/std::true_type) noexcept; + +template <class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> +_Tp __brick_transform_reduce(_ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _Tp, _BinaryOperation1, + _BinaryOperation2, + /*__is_vector=*/std::false_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, + class _BinaryOperation1, class _BinaryOperation2> +_Tp +__pattern_transform_reduce(_Tag, _ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _Tp, + _BinaryOperation1, _BinaryOperation2) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Tp, class _BinaryOperation1, class _BinaryOperation2> +_Tp +__pattern_transform_reduce(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator1, + _RandomAccessIterator1, _RandomAccessIterator2, _Tp, _BinaryOperation1, _BinaryOperation2); + +//------------------------------------------------------------------------ +// transform_reduce (version with unary and binary functions) +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator, class _Tp, class _UnaryOperation, class _BinaryOperation> +_Tp __brick_transform_reduce(_RandomAccessIterator, _RandomAccessIterator, _Tp, _BinaryOperation, _UnaryOperation, + /*is_vector=*/std::true_type) noexcept; + +template <class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation> +_Tp __brick_transform_reduce(_ForwardIterator, _ForwardIterator, _Tp, _BinaryOperation, _UnaryOperation, + /*is_vector=*/std::false_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, + class _UnaryOperation> +_Tp +__pattern_transform_reduce(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _Tp, _BinaryOperation, + _UnaryOperation) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Tp, class _BinaryOperation, + class _UnaryOperation> +_Tp +__pattern_transform_reduce(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _Tp, _BinaryOperation, _UnaryOperation); + +//------------------------------------------------------------------------ +// transform_exclusive_scan +// +// walk3 evaluates f(x,y,z) for (x,y,z) drawn from [first1,last1), [first2,...), [first3,...) +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation> +std::pair<_OutputIterator, _Tp> __brick_transform_scan(_ForwardIterator, _ForwardIterator, _OutputIterator, + _UnaryOperation, _Tp, _BinaryOperation, + /*Inclusive*/ std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation> +std::pair<_OutputIterator, _Tp> __brick_transform_scan(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, + _UnaryOperation, _Tp, _BinaryOperation, + /*Inclusive*/ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _UnaryOperation, + class _Tp, class _BinaryOperation, class _Inclusive> +_OutputIterator +__pattern_transform_scan(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _OutputIterator, _UnaryOperation, + _Tp, _BinaryOperation, _Inclusive) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, + class _UnaryOperation, class _Tp, class _BinaryOperation, class _Inclusive> +typename std::enable_if<!std::is_floating_point<_Tp>::value, _OutputIterator>::type +__pattern_transform_scan(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&&, _RandomAccessIterator, + _RandomAccessIterator, _OutputIterator, _UnaryOperation, _Tp, _BinaryOperation, _Inclusive); + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, + class _UnaryOperation, class _Tp, class _BinaryOperation, class _Inclusive> +typename std::enable_if<std::is_floating_point<_Tp>::value, _OutputIterator>::type +__pattern_transform_scan(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, + _OutputIterator, _UnaryOperation, _Tp, _BinaryOperation, _Inclusive); + +//------------------------------------------------------------------------ +// adjacent_difference +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator, class _BinaryOperation> +_OutputIterator __brick_adjacent_difference(_ForwardIterator, _ForwardIterator, _OutputIterator, _BinaryOperation, + /*is_vector*/ std::false_type) noexcept; + +template <class _RandomAccessIterator, class _OutputIterator, class _BinaryOperation> +_OutputIterator __brick_adjacent_difference(_RandomAccessIterator, _RandomAccessIterator, _OutputIterator, + _BinaryOperation, + /*is_vector*/ std::true_type) noexcept; + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _BinaryOperation> +_OutputIterator +__pattern_adjacent_difference(_Tag, _ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, _OutputIterator, + _BinaryOperation) noexcept; + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, + class _BinaryOperation> +_OutputIterator +__pattern_adjacent_difference(__parallel_tag<_IsVector>, _ExecutionPolicy&&, _RandomAccessIterator, + _RandomAccessIterator, _OutputIterator, _BinaryOperation); + +} // namespace __internal +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_NUMERIC_FWD_H */ diff --git a/pstl/include/pstl/internal/numeric_impl.h b/pstl/include/pstl/internal/numeric_impl.h new file mode 100644 index 0000000..a0387ae --- /dev/null +++ b/pstl/include/pstl/internal/numeric_impl.h @@ -0,0 +1,383 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_NUMERIC_IMPL_H +#define _PSTL_NUMERIC_IMPL_H + +#include <iterator> +#include <type_traits> +#include <numeric> + +#include "parallel_backend.h" +#include "pstl_config.h" +#include "execution_impl.h" +#include "unseq_backend_simd.h" +#include "algorithm_fwd.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __internal +{ + +//------------------------------------------------------------------------ +// transform_reduce (version with two binary functions, according to draft N4659) +//------------------------------------------------------------------------ + +template <class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> +_Tp +__brick_transform_reduce(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _Tp __init, + _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2, + /*is_vector=*/std::false_type) noexcept +{ + return std::inner_product(__first1, __last1, __first2, __init, __binary_op1, __binary_op2); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _Tp, class _BinaryOperation1, + class _BinaryOperation2> +_Tp +__brick_transform_reduce(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, + _RandomAccessIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, + _BinaryOperation2 __binary_op2, + /*is_vector=*/std::true_type) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType; + return __unseq_backend::__simd_transform_reduce( + __last1 - __first1, __init, __binary_op1, + [=, &__binary_op2](_DifferenceType __i) { return __binary_op2(__first1[__i], __first2[__i]); }); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, + class _BinaryOperation1, class _BinaryOperation2> +_Tp +__pattern_transform_reduce(_Tag, _ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, + _BinaryOperation2 __binary_op2) noexcept +{ + return __brick_transform_reduce(__first1, __last1, __first2, __init, __binary_op1, __binary_op2, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _Tp, class _BinaryOperation1, class _BinaryOperation2> +_Tp +__pattern_transform_reduce(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _Tp __init, + _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + return __par_backend::__parallel_transform_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first1, __last1, + [__first1, __first2, __binary_op2](_RandomAccessIterator1 __i) mutable + { return __binary_op2(*__i, *(__first2 + (__i - __first1))); }, + __init, + __binary_op1, // Combine + [__first1, __first2, __binary_op1, __binary_op2](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j, + _Tp __init) -> _Tp + { + return __internal::__brick_transform_reduce(__i, __j, __first2 + (__i - __first1), __init, + __binary_op1, __binary_op2, _IsVector{}); + }); + }); +} + +//------------------------------------------------------------------------ +// transform_reduce (version with unary and binary functions) +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation> +_Tp +__brick_transform_reduce(_ForwardIterator __first, _ForwardIterator __last, _Tp __init, _BinaryOperation __binary_op, + _UnaryOperation __unary_op, /*is_vector=*/std::false_type) noexcept +{ + return std::transform_reduce(__first, __last, __init, __binary_op, __unary_op); +} + +template <class _RandomAccessIterator, class _Tp, class _UnaryOperation, class _BinaryOperation> +_Tp +__brick_transform_reduce(_RandomAccessIterator __first, _RandomAccessIterator __last, _Tp __init, + _BinaryOperation __binary_op, _UnaryOperation __unary_op, + /*is_vector=*/std::true_type) noexcept +{ + typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType; + return __unseq_backend::__simd_transform_reduce( + __last - __first, __init, __binary_op, + [=, &__unary_op](_DifferenceType __i) { return __unary_op(__first[__i]); }); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, + class _UnaryOperation> +_Tp +__pattern_transform_reduce(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Tp __init, + _BinaryOperation __binary_op, _UnaryOperation __unary_op) noexcept +{ + return __internal::__brick_transform_reduce(__first, __last, __init, __binary_op, __unary_op, + typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _Tp, class _BinaryOperation, + class _UnaryOperation> +_Tp +__pattern_transform_reduce(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Tp __init, _BinaryOperation __binary_op, + _UnaryOperation __unary_op) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + return __internal::__except_handler( + [&]() + { + return __par_backend::__parallel_transform_reduce( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__unary_op](_RandomAccessIterator __i) mutable { return __unary_op(*__i); }, __init, __binary_op, + [__unary_op, __binary_op](_RandomAccessIterator __i, _RandomAccessIterator __j, _Tp __init) { + return __internal::__brick_transform_reduce(__i, __j, __init, __binary_op, __unary_op, _IsVector{}); + }); + }); +} + +//------------------------------------------------------------------------ +// transform_exclusive_scan +// +// walk3 evaluates f(x,y,z) for (x,y,z) drawn from [first1,last1), [first2,...), [first3,...) +//------------------------------------------------------------------------ + +// Exclusive form +template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation> +std::pair<_OutputIterator, _Tp> +__brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, + _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, + /*Inclusive*/ std::false_type, /*is_vector=*/std::false_type) noexcept +{ + for (; __first != __last; ++__first, ++__result) + { + *__result = __init; + _PSTL_PRAGMA_FORCEINLINE + __init = __binary_op(__init, __unary_op(*__first)); + } + return std::make_pair(__result, __init); +} + +// Inclusive form +template <class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation> +std::pair<_OutputIterator, _Tp> +__brick_transform_scan(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result, + _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, + /*Inclusive*/ std::true_type, /*is_vector=*/std::false_type) noexcept +{ + for (; __first != __last; ++__first, ++__result) + { + _PSTL_PRAGMA_FORCEINLINE + __init = __binary_op(__init, __unary_op(*__first)); + *__result = __init; + } + return std::make_pair(__result, __init); +} + +// type is arithmetic and binary operation is a user defined operation. +template <typename _Tp, typename _BinaryOperation> +using is_arithmetic_udop = std::integral_constant<bool, std::is_arithmetic<_Tp>::value && + !std::is_same<_BinaryOperation, std::plus<_Tp>>::value>; + +// [restriction] - T shall be DefaultConstructible. +// [violation] - default ctor of T shall set the identity value for binary_op. +template <class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation, + class _Inclusive> +typename std::enable_if<!is_arithmetic_udop<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type +__brick_transform_scan(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result, + _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, _Inclusive, + /*is_vector=*/std::true_type) noexcept +{ +#if defined(_PSTL_UDS_PRESENT) + return __unseq_backend::__simd_scan(__first, __last - __first, __result, __unary_op, __init, __binary_op, + _Inclusive()); +#else + // We need to call serial brick here to call function for inclusive and exclusive scan that depends on _Inclusive() value + return __internal::__brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(), + /*is_vector=*/std::false_type()); +#endif +} + +template <class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation, + class _Inclusive> +typename std::enable_if<is_arithmetic_udop<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type +__brick_transform_scan(_RandomAccessIterator __first, _RandomAccessIterator __last, _OutputIterator __result, + _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, _Inclusive, + /*is_vector=*/std::true_type) noexcept +{ + return __internal::__brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(), + /*is_vector=*/std::false_type()); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _UnaryOperation, + class _Tp, class _BinaryOperation, class _Inclusive> +_OutputIterator +__pattern_transform_scan(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, + _Inclusive) noexcept +{ + return __internal::__brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(), + typename _Tag::__is_vector{}) + .first; +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, + class _UnaryOperation, class _Tp, class _BinaryOperation, class _Inclusive> +typename std::enable_if<!std::is_floating_point<_Tp>::value, _OutputIterator>::type +__pattern_transform_scan(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, + _BinaryOperation __binary_op, _Inclusive) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType; + + return __internal::__except_handler( + [&]() + { + __par_backend::__parallel_transform_scan( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __last - __first, + [__first, __unary_op](_DifferenceType __i) mutable { return __unary_op(__first[__i]); }, __init, + __binary_op, + [__first, __unary_op, __binary_op](_DifferenceType __i, _DifferenceType __j, _Tp __init) + { + // Execute serial __brick_transform_reduce, due to the explicit SIMD vectorization (reduction) requires a commutative operation for the guarantee of correct scan. + return __internal::__brick_transform_reduce(__first + __i, __first + __j, __init, __binary_op, + __unary_op, + /*__is_vector*/ std::false_type()); + }, + [__first, __unary_op, __binary_op, __result](_DifferenceType __i, _DifferenceType __j, _Tp __init) + { + return __internal::__brick_transform_scan(__first + __i, __first + __j, __result + __i, __unary_op, + __init, __binary_op, _Inclusive(), _IsVector{}) + .second; + }); + return __result + (__last - __first); + }); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, + class _UnaryOperation, class _Tp, class _BinaryOperation, class _Inclusive> +typename std::enable_if<std::is_floating_point<_Tp>::value, _OutputIterator>::type +__pattern_transform_scan(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, _RandomAccessIterator __first, + _RandomAccessIterator __last, _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, + _BinaryOperation __binary_op, _Inclusive) +{ + using __backend_tag = typename decltype(__tag)::__backend_tag; + + typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType; + _DifferenceType __n = __last - __first; + + if (__n <= 0) + { + return __result; + } + return __internal::__except_handler( + [&]() + { + __par_backend::__parallel_strict_scan( + __backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __n, __init, + [__first, __unary_op, __binary_op, __result](_DifferenceType __i, _DifferenceType __len) + { + return __internal::__brick_transform_scan(__first + __i, __first + (__i + __len), __result + __i, + __unary_op, _Tp{}, __binary_op, _Inclusive(), _IsVector{}) + .second; + }, + __binary_op, + [__result, &__binary_op](_DifferenceType __i, _DifferenceType __len, _Tp __initial) + { + return *(std::transform(__result + __i, __result + __i + __len, __result + __i, + [&__initial, &__binary_op](const _Tp& __x) + { + _PSTL_PRAGMA_FORCEINLINE + return __binary_op(__initial, __x); + }) - + 1); + }, + [](_Tp) {}); + return __result + (__last - __first); + }); +} + +//------------------------------------------------------------------------ +// adjacent_difference +//------------------------------------------------------------------------ + +template <class _ForwardIterator, class _OutputIterator, class _BinaryOperation> +_OutputIterator +__brick_adjacent_difference(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __d_first, + _BinaryOperation __op, /*is_vector*/ std::false_type) noexcept +{ + return std::adjacent_difference(__first, __last, __d_first, __op); +} + +template <class _RandomAccessIterator1, class _RandomAccessIterator2, class BinaryOperation> +_RandomAccessIterator2 +__brick_adjacent_difference(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, + _RandomAccessIterator2 __d_first, BinaryOperation __op, + /*is_vector=*/std::true_type) noexcept +{ + _PSTL_ASSERT(__first != __last); + + typedef typename std::iterator_traits<_RandomAccessIterator1>::reference _ReferenceType1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::reference _ReferenceType2; + + auto __n = __last - __first; + *__d_first = *__first; + return __unseq_backend::__simd_walk_3( + __first + 1, __n - 1, __first, __d_first + 1, + [&__op](_ReferenceType1 __x, _ReferenceType1 __y, _ReferenceType2 __z) { __z = __op(__x, __y); }); +} + +template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _BinaryOperation> +_OutputIterator +__pattern_adjacent_difference(_Tag, _ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, + _OutputIterator __d_first, _BinaryOperation __op) noexcept +{ + return __internal::__brick_adjacent_difference(__first, __last, __d_first, __op, typename _Tag::__is_vector{}); +} + +template <class _IsVector, class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, + class _BinaryOperation> +_RandomAccessIterator2 +__pattern_adjacent_difference(__parallel_tag<_IsVector> __tag, _ExecutionPolicy&& __exec, + _RandomAccessIterator1 __first, _RandomAccessIterator1 __last, + _RandomAccessIterator2 __d_first, _BinaryOperation __op) +{ + _PSTL_ASSERT(__first != __last); + typedef typename std::iterator_traits<_RandomAccessIterator1>::reference _ReferenceType1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::reference _ReferenceType2; + + using __backend_tag = typename decltype(__tag)::__backend_tag; + + *__d_first = *__first; + __par_backend::__parallel_for(__backend_tag{}, std::forward<_ExecutionPolicy>(__exec), __first, __last - 1, + [&__op, __d_first, __first](_RandomAccessIterator1 __b, _RandomAccessIterator1 __e) + { + _RandomAccessIterator2 __d_b = __d_first + (__b - __first); + __internal::__brick_walk3( + __b, __e, __b + 1, __d_b + 1, + [&__op](_ReferenceType1 __x, _ReferenceType1 __y, _ReferenceType2 __z) + { __z = __op(__y, __x); }, + _IsVector{}); + }); + return __d_first + (__last - __first); +} + +} // namespace __internal +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_NUMERIC_IMPL_H */ diff --git a/pstl/include/pstl/internal/omp/parallel_for.h b/pstl/include/pstl/internal/omp/parallel_for.h new file mode 100644 index 0000000..0f841e5 --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_for.h @@ -0,0 +1,64 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_FOR_H +#define _PSTL_INTERNAL_OMP_PARALLEL_FOR_H + +#include <cstddef> + +#include "util.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +template <class _Index, class _Fp> +void +__parallel_for_body(_Index __first, _Index __last, _Fp __f) +{ + // initial partition of the iteration space into chunks + auto __policy = __omp_backend::__chunk_partitioner(__first, __last); + + // To avoid over-subscription we use taskloop for the nested parallelism + _PSTL_PRAGMA(omp taskloop untied mergeable) + for (std::size_t __chunk = 0; __chunk < __policy.__n_chunks; ++__chunk) + { + __pstl::__omp_backend::__process_chunk(__policy, __first, __chunk, __f); + } +} + +//------------------------------------------------------------------------ +// Notation: +// Evaluation of brick f[i,j) for each subrange [i,j) of [first, last) +//------------------------------------------------------------------------ + +template <class _ExecutionPolicy, class _Index, class _Fp> +void +__parallel_for(__pstl::__internal::__openmp_backend_tag, _ExecutionPolicy&&, _Index __first, _Index __last, _Fp __f) +{ + if (omp_in_parallel()) + { + // we don't create a nested parallel region in an existing parallel + // region: just create tasks + __pstl::__omp_backend::__parallel_for_body(__first, __last, __f); + } + else + { + // in any case (nested or non-nested) one parallel region is created and + // only one thread creates a set of tasks + _PSTL_PRAGMA(omp parallel) + _PSTL_PRAGMA(omp single nowait) { __pstl::__omp_backend::__parallel_for_body(__first, __last, __f); } + } +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_FOR_H diff --git a/pstl/include/pstl/internal/omp/parallel_for_each.h b/pstl/include/pstl/internal/omp/parallel_for_each.h new file mode 100644 index 0000000..b9bfb05 --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_for_each.h @@ -0,0 +1,59 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_FOR_EACH_H +#define _PSTL_INTERNAL_OMP_PARALLEL_FOR_EACH_H + +#include "util.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +template <class _ForwardIterator, class _Fp> +void +__parallel_for_each_body(_ForwardIterator __first, _ForwardIterator __last, _Fp __f) +{ + using DifferenceType = typename std::iterator_traits<_ForwardIterator>::difference_type; + // TODO: Think of an approach to remove the std::distance call + auto __size = std::distance(__first, __last); + + _PSTL_PRAGMA(omp taskloop untied mergeable) + for (DifferenceType __index = 0; __index < __size; ++__index) + { + // TODO: Think of an approach to remove the increment here each time. + auto __iter = std::next(__first, __index); + __f(*__iter); + } +} + +template <class _ExecutionPolicy, class _ForwardIterator, class _Fp> +void +__parallel_for_each(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Fp __f) +{ + if (omp_in_parallel()) + { + // we don't create a nested parallel region in an existing parallel + // region: just create tasks + __pstl::__omp_backend::__parallel_for_each_body(__first, __last, __f); + } + else + { + // in any case (nested or non-nested) one parallel region is created and + // only one thread creates a set of tasks + _PSTL_PRAGMA(omp parallel) + _PSTL_PRAGMA(omp single nowait) { __pstl::__omp_backend::__parallel_for_each_body(__first, __last, __f); } + } +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_FOR_EACH_H diff --git a/pstl/include/pstl/internal/omp/parallel_invoke.h b/pstl/include/pstl/internal/omp/parallel_invoke.h new file mode 100644 index 0000000..045ccbe --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_invoke.h @@ -0,0 +1,50 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_INVOKE_H +#define _PSTL_INTERNAL_OMP_PARALLEL_INVOKE_H + +#include "util.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +template <typename _F1, typename _F2> +void +__parallel_invoke_body(_F1&& __f1, _F2&& __f2) +{ + _PSTL_PRAGMA(omp taskgroup) + { + _PSTL_PRAGMA(omp task untied mergeable) { std::forward<_F1>(__f1)(); } + _PSTL_PRAGMA(omp task untied mergeable) { std::forward<_F2>(__f2)(); } + } +} + +template <class _ExecutionPolicy, typename _F1, typename _F2> +void +__parallel_invoke(__pstl::__internal::__openmp_backend_tag, _ExecutionPolicy&&, _F1&& __f1, _F2&& __f2) +{ + if (omp_in_parallel()) + { + __pstl::__omp_backend::__parallel_invoke_body(std::forward<_F1>(__f1), std::forward<_F2>(__f2)); + } + else + { + _PSTL_PRAGMA(omp parallel) + _PSTL_PRAGMA(omp single nowait) + __pstl::__omp_backend::__parallel_invoke_body(std::forward<_F1>(__f1), std::forward<_F2>(__f2)); + } +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_INVOKE_H diff --git a/pstl/include/pstl/internal/omp/parallel_merge.h b/pstl/include/pstl/internal/omp/parallel_merge.h new file mode 100644 index 0000000..e6f82c5 --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_merge.h @@ -0,0 +1,98 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_MERGE_H +#define _PSTL_INTERNAL_OMP_PARALLEL_MERGE_H + +#include "util.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _RandomAccessIterator3, + typename _Compare, typename _LeafMerge> +void +__parallel_merge_body(std::size_t __size_x, std::size_t __size_y, _RandomAccessIterator1 __xs, + _RandomAccessIterator1 __xe, _RandomAccessIterator2 __ys, _RandomAccessIterator2 __ye, + _RandomAccessIterator3 __zs, _Compare __comp, _LeafMerge __leaf_merge) +{ + + if (__size_x + __size_y <= __omp_backend::__default_chunk_size) + { + __leaf_merge(__xs, __xe, __ys, __ye, __zs, __comp); + return; + } + + _RandomAccessIterator1 __xm; + _RandomAccessIterator2 __ym; + + if (__size_x < __size_y) + { + __ym = __ys + (__size_y / 2); + __xm = std::upper_bound(__xs, __xe, *__ym, __comp); + } + else + { + __xm = __xs + (__size_x / 2); + __ym = std::lower_bound(__ys, __ye, *__xm, __comp); + } + + auto __zm = __zs + (__xm - __xs) + (__ym - __ys); + + _PSTL_PRAGMA(omp task untied mergeable default(none) + firstprivate(__xs, __xm, __ys, __ym, __zs, __comp, __leaf_merge)) + __pstl::__omp_backend::__parallel_merge_body(__xm - __xs, __ym - __ys, __xs, __xm, __ys, __ym, __zs, __comp, + __leaf_merge); + + _PSTL_PRAGMA(omp task untied mergeable default(none) + firstprivate(__xm, __xe, __ym, __ye, __zm, __comp, __leaf_merge)) + __pstl::__omp_backend::__parallel_merge_body(__xe - __xm, __ye - __ym, __xm, __xe, __ym, __ye, __zm, __comp, + __leaf_merge); + + _PSTL_PRAGMA(omp taskwait) +} + +template <class _ExecutionPolicy, typename _RandomAccessIterator1, typename _RandomAccessIterator2, + typename _RandomAccessIterator3, typename _Compare, typename _LeafMerge> +void +__parallel_merge(__pstl::__internal::__openmp_backend_tag, _ExecutionPolicy&& /*__exec*/, _RandomAccessIterator1 __xs, + _RandomAccessIterator1 __xe, _RandomAccessIterator2 __ys, _RandomAccessIterator2 __ye, + _RandomAccessIterator3 __zs, _Compare __comp, _LeafMerge __leaf_merge) + +{ + std::size_t __size_x = __xe - __xs; + std::size_t __size_y = __ye - __ys; + + /* + * Run the merge in parallel by chunking it up. Use the smaller range (if any) as the iteration range, and the + * larger range as the search range. + */ + + if (omp_in_parallel()) + { + __pstl::__omp_backend::__parallel_merge_body(__size_x, __size_y, __xs, __xe, __ys, __ye, __zs, __comp, + __leaf_merge); + } + else + { + _PSTL_PRAGMA(omp parallel) + { + _PSTL_PRAGMA(omp single nowait) + __pstl::__omp_backend::__parallel_merge_body(__size_x, __size_y, __xs, __xe, __ys, __ye, __zs, __comp, + __leaf_merge); + } + } +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_MERGE_H diff --git a/pstl/include/pstl/internal/omp/parallel_reduce.h b/pstl/include/pstl/internal/omp/parallel_reduce.h new file mode 100644 index 0000000..841d48f --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_reduce.h @@ -0,0 +1,73 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_REDUCE_H +#define _PSTL_INTERNAL_OMP_PARALLEL_REDUCE_H + +#include "util.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +template <class _RandomAccessIterator, class _Value, typename _RealBody, typename _Reduction> +_Value +__parallel_reduce_body(_RandomAccessIterator __first, _RandomAccessIterator __last, _Value __identity, + _RealBody __real_body, _Reduction __reduce) +{ + if (__should_run_serial(__first, __last)) + { + return __real_body(__first, __last, __identity); + } + + auto __middle = __first + ((__last - __first) / 2); + _Value __v1(__identity), __v2(__identity); + __parallel_invoke_body( + [&]() { __v1 = __parallel_reduce_body(__first, __middle, __identity, __real_body, __reduce); }, + [&]() { __v2 = __parallel_reduce_body(__middle, __last, __identity, __real_body, __reduce); }); + + return __reduce(__v1, __v2); +} + +//------------------------------------------------------------------------ +// Notation: +// r(i,j,init) returns reduction of init with reduction over [i,j) +// c(x,y) combines values x and y that were the result of r +//------------------------------------------------------------------------ + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _Value, typename _RealBody, typename _Reduction> +_Value +__parallel_reduce(__pstl::__internal::__openmp_backend_tag, _ExecutionPolicy&&, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Value __identity, _RealBody __real_body, _Reduction __reduction) +{ + // We don't create a nested parallel region in an existing parallel region: + // just create tasks. + if (omp_in_parallel()) + { + return __pstl::__omp_backend::__parallel_reduce_body(__first, __last, __identity, __real_body, __reduction); + } + + // In any case (nested or non-nested) one parallel region is created and only + // one thread creates a set of tasks. + _Value __res = __identity; + + _PSTL_PRAGMA(omp parallel) + _PSTL_PRAGMA(omp single nowait) + { + __res = __pstl::__omp_backend::__parallel_reduce_body(__first, __last, __identity, __real_body, __reduction); + } + + return __res; +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_REDUCE_H diff --git a/pstl/include/pstl/internal/omp/parallel_scan.h b/pstl/include/pstl/internal/omp/parallel_scan.h new file mode 100644 index 0000000..f3eb967 --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_scan.h @@ -0,0 +1,136 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_SCAN_H +#define _PSTL_INTERNAL_OMP_PARALLEL_SCAN_H + +#include "parallel_invoke.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +template <typename _Index> +_Index +__split(_Index __m) +{ + _Index __k = 1; + while (2 * __k < __m) + __k *= 2; + return __k; +} + +template <typename _Index, typename _Tp, typename _Rp, typename _Cp> +void +__upsweep(_Index __i, _Index __m, _Index __tilesize, _Tp* __r, _Index __lastsize, _Rp __reduce, _Cp __combine) +{ + if (__m == 1) + __r[0] = __reduce(__i * __tilesize, __lastsize); + else + { + _Index __k = __split(__m); + __omp_backend::__parallel_invoke_body( + [=] { __omp_backend::__upsweep(__i, __k, __tilesize, __r, __tilesize, __reduce, __combine); }, + [=] { + __omp_backend::__upsweep(__i + __k, __m - __k, __tilesize, __r + __k, __lastsize, __reduce, __combine); + }); + if (__m == 2 * __k) + __r[__m - 1] = __combine(__r[__k - 1], __r[__m - 1]); + } +} + +template <typename _Index, typename _Tp, typename _Cp, typename _Sp> +void +__downsweep(_Index __i, _Index __m, _Index __tilesize, _Tp* __r, _Index __lastsize, _Tp __initial, _Cp __combine, + _Sp __scan) +{ + if (__m == 1) + __scan(__i * __tilesize, __lastsize, __initial); + else + { + const _Index __k = __split(__m); + __omp_backend::__parallel_invoke_body( + [=] { __omp_backend::__downsweep(__i, __k, __tilesize, __r, __tilesize, __initial, __combine, __scan); }, + // Assumes that __combine never throws. + // TODO: Consider adding a requirement for user functors to be constant. + [=, &__combine] + { + __omp_backend::__downsweep(__i + __k, __m - __k, __tilesize, __r + __k, __lastsize, + __combine(__initial, __r[__k - 1]), __combine, __scan); + }); + } +} + +template <typename _ExecutionPolicy, typename _Index, typename _Tp, typename _Rp, typename _Cp, typename _Sp, + typename _Ap> +void +__parallel_strict_scan_body(_Index __n, _Tp __initial, _Rp __reduce, _Cp __combine, _Sp __scan, _Ap __apex) +{ + _Index __p = omp_get_num_threads(); + const _Index __slack = 4; + _Index __tilesize = (__n - 1) / (__slack * __p) + 1; + _Index __m = (__n - 1) / __tilesize; + __buffer<_Tp> __buf(__m + 1); + _Tp* __r = __buf.get(); + + __omp_backend::__upsweep(_Index(0), _Index(__m + 1), __tilesize, __r, __n - __m * __tilesize, __reduce, __combine); + + std::size_t __k = __m + 1; + _Tp __t = __r[__k - 1]; + while ((__k &= __k - 1)) + { + __t = __combine(__r[__k - 1], __t); + } + + __apex(__combine(__initial, __t)); + __omp_backend::__downsweep(_Index(0), _Index(__m + 1), __tilesize, __r, __n - __m * __tilesize, __initial, + __combine, __scan); +} + +template <class _ExecutionPolicy, typename _Index, typename _Tp, typename _Rp, typename _Cp, typename _Sp, typename _Ap> +void +__parallel_strict_scan(__pstl::__internal::__openmp_backend_tag, _ExecutionPolicy&&, _Index __n, _Tp __initial, + _Rp __reduce, _Cp __combine, _Sp __scan, _Ap __apex) +{ + if (__n <= __default_chunk_size) + { + _Tp __sum = __initial; + if (__n) + { + __sum = __combine(__sum, __reduce(_Index(0), __n)); + } + __apex(__sum); + if (__n) + { + __scan(_Index(0), __n, __initial); + } + return; + } + + if (omp_in_parallel()) + { + __pstl::__omp_backend::__parallel_strict_scan_body<_ExecutionPolicy>(__n, __initial, __reduce, __combine, + __scan, __apex); + } + else + { + _PSTL_PRAGMA(omp parallel) + _PSTL_PRAGMA(omp single nowait) + { + __pstl::__omp_backend::__parallel_strict_scan_body<_ExecutionPolicy>(__n, __initial, __reduce, __combine, + __scan, __apex); + } + } +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_SCAN_H diff --git a/pstl/include/pstl/internal/omp/parallel_stable_partial_sort.h b/pstl/include/pstl/internal/omp/parallel_stable_partial_sort.h new file mode 100644 index 0000000..06cd55b --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_stable_partial_sort.h @@ -0,0 +1,33 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_STABLE_PARTIAL_SORT_H +#define _PSTL_INTERNAL_OMP_PARALLEL_STABLE_PARTIAL_SORT_H + +#include "util.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +template <typename _RandomAccessIterator, typename _Compare, typename _LeafSort> +void +__parallel_stable_partial_sort(__pstl::__internal::__openmp_backend_tag, _RandomAccessIterator __xs, + _RandomAccessIterator __xe, _Compare __comp, _LeafSort __leaf_sort, + std::size_t /* __nsort */) +{ + // TODO: "Parallel partial sort needs to be implemented."); + __leaf_sort(__xs, __xe, __comp); +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_STABLE_PARTIAL_SORT_H diff --git a/pstl/include/pstl/internal/omp/parallel_stable_sort.h b/pstl/include/pstl/internal/omp/parallel_stable_sort.h new file mode 100644 index 0000000..e4d0676 --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_stable_sort.h @@ -0,0 +1,160 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_STABLE_SORT_H +#define _PSTL_INTERNAL_OMP_PARALLEL_STABLE_SORT_H + +#include "util.h" +#include "parallel_merge.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +namespace __sort_details +{ +struct __move_value +{ + template <typename _Iterator, typename _OutputIterator> + void + operator()(_Iterator __x, _OutputIterator __z) const + { + *__z = std::move(*__x); + } +}; + +template <typename _RandomAccessIterator, typename _OutputIterator> +_OutputIterator +__parallel_move_range(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _OutputIterator __d_first) +{ + std::size_t __size = __last1 - __first1; + + // Perform serial moving of small chunks + + if (__size <= __default_chunk_size) + { + return std::move(__first1, __last1, __d_first); + } + + // Perform parallel moving of larger chunks + auto __policy = __pstl::__omp_backend::__chunk_partitioner(__first1, __last1); + + _PSTL_PRAGMA(omp taskloop) + for (std::size_t __chunk = 0; __chunk < __policy.__n_chunks; ++__chunk) + { + __pstl::__omp_backend::__process_chunk(__policy, __first1, __chunk, + [&](auto __chunk_first, auto __chunk_last) + { + auto __chunk_offset = __chunk_first - __first1; + auto __output_it = __d_first + __chunk_offset; + std::move(__chunk_first, __chunk_last, __output_it); + }); + } + + return __d_first + __size; +} + +struct __move_range +{ + template <typename _RandomAccessIterator, typename _OutputIterator> + _OutputIterator + operator()(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _OutputIterator __d_first) const + { + return __pstl::__omp_backend::__sort_details::__parallel_move_range(__first1, __last1, __d_first); + } +}; +} // namespace __sort_details + +template <typename _RandomAccessIterator, typename _Compare, typename _LeafSort> +void +__parallel_stable_sort_body(_RandomAccessIterator __xs, _RandomAccessIterator __xe, _Compare __comp, + _LeafSort __leaf_sort) +{ + using _ValueType = typename std::iterator_traits<_RandomAccessIterator>::value_type; + using _VecType = typename std::vector<_ValueType>; + using _OutputIterator = typename _VecType::iterator; + using _MoveValue = typename __omp_backend::__sort_details::__move_value; + using _MoveRange = __omp_backend::__sort_details::__move_range; + + if (__should_run_serial(__xs, __xe)) + { + __leaf_sort(__xs, __xe, __comp); + } + else + { + std::size_t __size = __xe - __xs; + auto __mid = __xs + (__size / 2); + __pstl::__omp_backend::__parallel_invoke_body( + [&]() { __parallel_stable_sort_body(__xs, __mid, __comp, __leaf_sort); }, + [&]() { __parallel_stable_sort_body(__mid, __xe, __comp, __leaf_sort); }); + + // Perform a parallel merge of the sorted ranges into __output_data. + _VecType __output_data(__size); + _MoveValue __move_value; + _MoveRange __move_range; + __utils::__serial_move_merge __merge(__size); + __pstl::__omp_backend::__parallel_merge_body( + __mid - __xs, __xe - __mid, __xs, __mid, __mid, __xe, __output_data.begin(), __comp, + [&__merge, &__move_value, &__move_range](_RandomAccessIterator __as, _RandomAccessIterator __ae, + _RandomAccessIterator __bs, _RandomAccessIterator __be, + _OutputIterator __cs, _Compare __comp) + { __merge(__as, __ae, __bs, __be, __cs, __comp, __move_value, __move_value, __move_range, __move_range); }); + + // Move the values from __output_data back in the original source range. + __pstl::__omp_backend::__sort_details::__parallel_move_range(__output_data.begin(), __output_data.end(), __xs); + } +} + +template <class _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare, typename _LeafSort> +void +__parallel_stable_sort(__pstl::__internal::__openmp_backend_tag __tag, _ExecutionPolicy&& /*__exec*/, + _RandomAccessIterator __xs, _RandomAccessIterator __xe, _Compare __comp, _LeafSort __leaf_sort, + std::size_t __nsort = 0) +{ + auto __count = static_cast<std::size_t>(__xe - __xs); + if (__count <= __default_chunk_size || __nsort < __count) + { + __leaf_sort(__xs, __xe, __comp); + return; + } + + // TODO: the partial sort implementation should + // be shared with the other backends. + + if (omp_in_parallel()) + { + if (__count <= __nsort) + { + __pstl::__omp_backend::__parallel_stable_sort_body(__xs, __xe, __comp, __leaf_sort); + } + else + { + __pstl::__omp_backend::__parallel_stable_partial_sort(__tag, __xs, __xe, __comp, __leaf_sort, __nsort); + } + } + else + { + _PSTL_PRAGMA(omp parallel) + _PSTL_PRAGMA(omp single nowait) + if (__count <= __nsort) + { + __pstl::__omp_backend::__parallel_stable_sort_body(__xs, __xe, __comp, __leaf_sort); + } + else + { + __pstl::__omp_backend::__parallel_stable_partial_sort(__tag, __xs, __xe, __comp, __leaf_sort, __nsort); + } + } +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_STABLE_SORT_H diff --git a/pstl/include/pstl/internal/omp/parallel_transform_reduce.h b/pstl/include/pstl/internal/omp/parallel_transform_reduce.h new file mode 100644 index 0000000..1d4cc0a --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_transform_reduce.h @@ -0,0 +1,113 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_TRANSFORM_REDUCE_H +#define _PSTL_INTERNAL_OMP_PARALLEL_TRANSFORM_REDUCE_H + +#include "util.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +//------------------------------------------------------------------------ +// parallel_transform_reduce +// +// Notation: +// r(i,j,init) returns reduction of init with reduction over [i,j) +// u(i) returns f(i,i+1,identity) for a hypothetical left identity element +// of r c(x,y) combines values x and y that were the result of r or u +//------------------------------------------------------------------------ + +template <class _RandomAccessIterator, class _UnaryOp, class _Value, class _Combiner, class _Reduction> +auto +__transform_reduce_body(_RandomAccessIterator __first, _RandomAccessIterator __last, _UnaryOp __unary_op, _Value __init, + _Combiner __combiner, _Reduction __reduction) +{ + const std::size_t __num_threads = omp_get_num_threads(); + const std::size_t __size = __last - __first; + + // Initial partition of the iteration space into chunks. If the range is too small, + // this will result in a nonsense policy, so we check on the size as well below. + auto __policy = __omp_backend::__chunk_partitioner(__first + __num_threads, __last); + + if (__size <= __num_threads || __policy.__n_chunks < 2) + { + return __reduction(__first, __last, __init); + } + + // Here, we cannot use OpenMP UDR because we must store the init value in + // the combiner and it will be used several times. Although there should be + // the only one; we manually generate the identity elements for each thread. + std::vector<_Value> __accums; + __accums.reserve(__num_threads); + + // initialize accumulators for all threads + for (std::size_t __i = 0; __i < __num_threads; ++__i) + { + __accums.emplace_back(__unary_op(__first + __i)); + } + + // main loop + _PSTL_PRAGMA(omp taskloop shared(__accums)) + for (std::size_t __chunk = 0; __chunk < __policy.__n_chunks; ++__chunk) + { + __pstl::__omp_backend::__process_chunk(__policy, __first + __num_threads, __chunk, + [&](auto __chunk_first, auto __chunk_last) + { + auto __thread_num = omp_get_thread_num(); + __accums[__thread_num] = + __reduction(__chunk_first, __chunk_last, __accums[__thread_num]); + }); + } + + // combine by accumulators + for (std::size_t __i = 0; __i < __num_threads; ++__i) + { + __init = __combiner(__init, __accums[__i]); + } + + return __init; +} + +template <class _ExecutionPolicy, class _RandomAccessIterator, class _UnaryOp, class _Value, class _Combiner, + class _Reduction> +_Value +__parallel_transform_reduce(__pstl::__internal::__openmp_backend_tag, _ExecutionPolicy&&, _RandomAccessIterator __first, + _RandomAccessIterator __last, _UnaryOp __unary_op, _Value __init, _Combiner __combiner, + _Reduction __reduction) +{ + _Value __result = __init; + if (omp_in_parallel()) + { + // We don't create a nested parallel region in an existing parallel + // region: just create tasks + __result = __pstl::__omp_backend::__transform_reduce_body(__first, __last, __unary_op, __init, __combiner, + __reduction); + } + else + { + // Create a parallel region, and a single thread will create tasks + // for the region. + _PSTL_PRAGMA(omp parallel) + _PSTL_PRAGMA(omp single nowait) + { + __result = __pstl::__omp_backend::__transform_reduce_body(__first, __last, __unary_op, __init, __combiner, + __reduction); + } + } + + return __result; +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_TRANSFORM_REDUCE_H diff --git a/pstl/include/pstl/internal/omp/parallel_transform_scan.h b/pstl/include/pstl/internal/omp/parallel_transform_scan.h new file mode 100644 index 0000000..f836289 --- /dev/null +++ b/pstl/include/pstl/internal/omp/parallel_transform_scan.h @@ -0,0 +1,32 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_PARALLEL_TRANSFORM_SCAN_H +#define _PSTL_INTERNAL_OMP_PARALLEL_TRANSFORM_SCAN_H + +#include "util.h" + +namespace __pstl +{ +namespace __omp_backend +{ + +template <class _ExecutionPolicy, class _Index, class _Up, class _Tp, class _Cp, class _Rp, class _Sp> +_Tp +__parallel_transform_scan(__pstl::__internal::__openmp_backend_tag, _ExecutionPolicy&&, _Index __n, _Up /* __u */, + _Tp __init, _Cp /* __combine */, _Rp /* __brick_reduce */, _Sp __scan) +{ + // TODO: parallelize this function. + return __scan(_Index(0), __n, __init); +} + +} // namespace __omp_backend +} // namespace __pstl +#endif // _PSTL_INTERNAL_OMP_PARALLEL_TRANSFORM_SCAN_H diff --git a/pstl/include/pstl/internal/omp/util.h b/pstl/include/pstl/internal/omp/util.h new file mode 100644 index 0000000..c88d980 --- /dev/null +++ b/pstl/include/pstl/internal/omp/util.h @@ -0,0 +1,173 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_INTERNAL_OMP_UTIL_H +#define _PSTL_INTERNAL_OMP_UTIL_H + +#include <algorithm> +#include <atomic> +#include <iterator> +#include <cstddef> +#include <cstdio> +#include <memory> +#include <vector> +#include <omp.h> + +#include "../parallel_backend_utils.h" +#include "../unseq_backend_simd.h" +#include "../utils.h" + +// Portability "#pragma" definition +#ifdef _MSC_VER +# define _PSTL_PRAGMA(x) __pragma(x) +#else +# define _PSTL_PRAGMA(x) _Pragma(# x) +#endif + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __omp_backend +{ + +//------------------------------------------------------------------------ +// use to cancel execution +//------------------------------------------------------------------------ +inline void +__cancel_execution() +{ + // TODO: Figure out how to make cancelation work. +} + +//------------------------------------------------------------------------ +// raw buffer +//------------------------------------------------------------------------ + +template <typename _Tp> +class __buffer +{ + std::allocator<_Tp> __allocator_; + _Tp* __ptr_; + const std::size_t __buf_size_; + __buffer(const __buffer&) = delete; + void + operator=(const __buffer&) = delete; + + public: + __buffer(std::size_t __n) : __allocator_(), __ptr_(__allocator_.allocate(__n)), __buf_size_(__n) {} + + operator bool() const { return __ptr_ != nullptr; } + + _Tp* + get() const + { + return __ptr_; + } + ~__buffer() { __allocator_.deallocate(__ptr_, __buf_size_); } +}; + +// Preliminary size of each chunk: requires further discussion +inline constexpr std::size_t __default_chunk_size = 2048; + +// Convenience function to determine when we should run serial. +template <typename _Iterator, std::enable_if_t<!std::is_integral<_Iterator>::value, bool> = true> +constexpr auto +__should_run_serial(_Iterator __first, _Iterator __last) -> bool +{ + using _difference_type = typename std::iterator_traits<_Iterator>::difference_type; + auto __size = std::distance(__first, __last); + return __size <= static_cast<_difference_type>(__default_chunk_size); +} + +template <typename _Index, std::enable_if_t<std::is_integral<_Index>::value, bool> = true> +constexpr auto +__should_run_serial(_Index __first, _Index __last) -> bool +{ + using _difference_type = _Index; + auto __size = __last - __first; + return __size <= static_cast<_difference_type>(__default_chunk_size); +} + +struct __chunk_metrics +{ + std::size_t __n_chunks; + std::size_t __chunk_size; + std::size_t __first_chunk_size; +}; + +// The iteration space partitioner according to __requested_chunk_size +template <class _RandomAccessIterator, class _Size = std::size_t> +auto +__chunk_partitioner(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Size __requested_chunk_size = __default_chunk_size) -> __chunk_metrics +{ + /* + * This algorithm improves distribution of elements in chunks by avoiding + * small tail chunks. The leftover elements that do not fit neatly into + * the chunk size are redistributed to early chunks. This improves + * utilization of the processor's prefetch and reduces the number of + * tasks needed by 1. + */ + + const _Size __n = __last - __first; + _Size __n_chunks = 0; + _Size __chunk_size = 0; + _Size __first_chunk_size = 0; + if (__n < __requested_chunk_size) + { + __chunk_size = __n; + __first_chunk_size = __n; + __n_chunks = 1; + return __chunk_metrics{__n_chunks, __chunk_size, __first_chunk_size}; + } + + __n_chunks = (__n / __requested_chunk_size) + 1; + __chunk_size = __n / __n_chunks; + __first_chunk_size = __chunk_size; + const _Size __n_leftover_items = __n - (__n_chunks * __chunk_size); + + if (__n_leftover_items == __chunk_size) + { + __n_chunks += 1; + return __chunk_metrics{__n_chunks, __chunk_size, __first_chunk_size}; + } + else if (__n_leftover_items == 0) + { + __first_chunk_size = __chunk_size; + return __chunk_metrics{__n_chunks, __chunk_size, __first_chunk_size}; + } + + const _Size __n_extra_items_per_chunk = __n_leftover_items / __n_chunks; + const _Size __n_final_leftover_items = __n_leftover_items - (__n_extra_items_per_chunk * __n_chunks); + + __chunk_size += __n_extra_items_per_chunk; + __first_chunk_size = __chunk_size + __n_final_leftover_items; + + return __chunk_metrics{__n_chunks, __chunk_size, __first_chunk_size}; +} + +template <typename _Iterator, typename _Index, typename _Func> +void +__process_chunk(const __chunk_metrics& __metrics, _Iterator __base, _Index __chunk_index, _Func __f) +{ + auto __this_chunk_size = __chunk_index == 0 ? __metrics.__first_chunk_size : __metrics.__chunk_size; + auto __index = __chunk_index == 0 ? 0 + : (__chunk_index * __metrics.__chunk_size) + + (__metrics.__first_chunk_size - __metrics.__chunk_size); + auto __first = __base + __index; + auto __last = __first + __this_chunk_size; + __f(__first, __last); +} + +} // namespace __omp_backend +} // namespace __pstl + +#endif // _PSTL_INTERNAL_OMP_UTIL_H diff --git a/pstl/include/pstl/internal/parallel_backend.h b/pstl/include/pstl/internal/parallel_backend.h new file mode 100644 index 0000000..4da871b --- /dev/null +++ b/pstl/include/pstl/internal/parallel_backend.h @@ -0,0 +1,37 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_PARALLEL_BACKEND_H +#define _PSTL_PARALLEL_BACKEND_H + +#include "pstl_config.h" + +#if defined(_PSTL_PAR_BACKEND_SERIAL) +# include "parallel_backend_serial.h" +namespace __pstl +{ +namespace __par_backend = __serial_backend; +} +#elif defined(_PSTL_PAR_BACKEND_TBB) +# include "parallel_backend_tbb.h" +namespace __pstl +{ +namespace __par_backend = __tbb_backend; +} +#elif defined(_PSTL_PAR_BACKEND_OPENMP) +# include "parallel_backend_omp.h" +namespace __pstl +{ +namespace __par_backend = __omp_backend; +} +#else +_PSTL_PRAGMA_MESSAGE("Parallel backend was not specified"); +#endif + +#endif /* _PSTL_PARALLEL_BACKEND_H */ diff --git a/pstl/include/pstl/internal/parallel_backend_omp.h b/pstl/include/pstl/internal/parallel_backend_omp.h new file mode 100644 index 0000000..7398cfe --- /dev/null +++ b/pstl/include/pstl/internal/parallel_backend_omp.h @@ -0,0 +1,58 @@ +// -*- C++ -*- +// -*-===----------------------------------------------------------------------===// +// +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// +//===----------------------------------------------------------------------===// + +#ifndef _PSTL_PARALLEL_BACKEND_OMP_H +#define _PSTL_PARALLEL_BACKEND_OMP_H + +//------------------------------------------------------------------------ +// parallel_invoke +//------------------------------------------------------------------------ + +#include "./omp/parallel_invoke.h" + +//------------------------------------------------------------------------ +// parallel_for +//------------------------------------------------------------------------ + +#include "./omp/parallel_for.h" + +//------------------------------------------------------------------------ +// parallel_for_each +//------------------------------------------------------------------------ + +#include "./omp/parallel_for_each.h" + +//------------------------------------------------------------------------ +// parallel_reduce +//------------------------------------------------------------------------ + +#include "./omp/parallel_reduce.h" +#include "./omp/parallel_transform_reduce.h" + +//------------------------------------------------------------------------ +// parallel_scan +//------------------------------------------------------------------------ + +#include "./omp/parallel_scan.h" +#include "./omp/parallel_transform_scan.h" + +//------------------------------------------------------------------------ +// parallel_stable_sort +//------------------------------------------------------------------------ + +#include "./omp/parallel_stable_partial_sort.h" +#include "./omp/parallel_stable_sort.h" + +//------------------------------------------------------------------------ +// parallel_merge +//------------------------------------------------------------------------ +#include "./omp/parallel_merge.h" + +#endif //_PSTL_PARALLEL_BACKEND_OMP_H diff --git a/pstl/include/pstl/internal/parallel_backend_serial.h b/pstl/include/pstl/internal/parallel_backend_serial.h new file mode 100644 index 0000000..ad2c5fc --- /dev/null +++ b/pstl/include/pstl/internal/parallel_backend_serial.h @@ -0,0 +1,137 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_PARALLEL_BACKEND_SERIAL_H +#define _PSTL_PARALLEL_BACKEND_SERIAL_H + +#include <algorithm> +#include <cstddef> +#include <memory> +#include <numeric> +#include <utility> + +#include "pstl_config.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __serial_backend +{ + +template <typename _Tp> +class __buffer +{ + std::allocator<_Tp> __allocator_; + _Tp* __ptr_; + const std::size_t __buf_size_; + __buffer(const __buffer&) = delete; + void + operator=(const __buffer&) = delete; + + public: + __buffer(std::size_t __n) : __allocator_(), __ptr_(__allocator_.allocate(__n)), __buf_size_(__n) {} + + operator bool() const { return __ptr_ != nullptr; } + _Tp* + get() const + { + return __ptr_; + } + ~__buffer() { __allocator_.deallocate(__ptr_, __buf_size_); } +}; + +inline void +__cancel_execution() +{ +} + +template <class _ExecutionPolicy, class _Index, class _Fp> +void +__parallel_for(__pstl::__internal::__serial_backend_tag, _ExecutionPolicy&&, _Index __first, _Index __last, _Fp __f) +{ + __f(__first, __last); +} + +template <class _ExecutionPolicy, class _Value, class _Index, typename _RealBody, typename _Reduction> +_Value +__parallel_reduce(__pstl::__internal::__serial_backend_tag, _ExecutionPolicy&&, _Index __first, _Index __last, + const _Value& __identity, const _RealBody& __real_body, const _Reduction&) +{ + if (__first == __last) + { + return __identity; + } + else + { + return __real_body(__first, __last, __identity); + } +} + +template <class _ExecutionPolicy, class _Index, class _UnaryOp, class _Tp, class _BinaryOp, class _Reduce> +_Tp +__parallel_transform_reduce(__pstl::__internal::__serial_backend_tag, _ExecutionPolicy&&, _Index __first, _Index __last, + _UnaryOp, _Tp __init, _BinaryOp, _Reduce __reduce) +{ + return __reduce(__first, __last, __init); +} + +template <class _ExecutionPolicy, typename _Index, typename _Tp, typename _Rp, typename _Cp, typename _Sp, typename _Ap> +void +__parallel_strict_scan(__pstl::__internal::__serial_backend_tag, _ExecutionPolicy&&, _Index __n, _Tp __initial, + _Rp __reduce, _Cp __combine, _Sp __scan, _Ap __apex) +{ + _Tp __sum = __initial; + if (__n) + __sum = __combine(__sum, __reduce(_Index(0), __n)); + __apex(__sum); + if (__n) + __scan(_Index(0), __n, __initial); +} + +template <class _ExecutionPolicy, class _Index, class _UnaryOp, class _Tp, class _BinaryOp, class _Reduce, class _Scan> +_Tp +__parallel_transform_scan(__pstl::__internal::__serial_backend_tag, _ExecutionPolicy&&, _Index __n, _UnaryOp, + _Tp __init, _BinaryOp, _Reduce, _Scan __scan) +{ + return __scan(_Index(0), __n, __init); +} + +template <class _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare, typename _LeafSort> +void +__parallel_stable_sort(__pstl::__internal::__serial_backend_tag, _ExecutionPolicy&&, _RandomAccessIterator __first, + _RandomAccessIterator __last, _Compare __comp, _LeafSort __leaf_sort, std::size_t = 0) +{ + __leaf_sort(__first, __last, __comp); +} + +template <class _ExecutionPolicy, typename _RandomAccessIterator1, typename _RandomAccessIterator2, + typename _RandomAccessIterator3, typename _Compare, typename _LeafMerge> +void +__parallel_merge(__pstl::__internal::__serial_backend_tag, _ExecutionPolicy&&, _RandomAccessIterator1 __first1, + _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, + _RandomAccessIterator3 __outit, _Compare __comp, _LeafMerge __leaf_merge) +{ + __leaf_merge(__first1, __last1, __first2, __last2, __outit, __comp); +} + +template <class _ExecutionPolicy, typename _F1, typename _F2> +void +__parallel_invoke(__pstl::__internal::__serial_backend_tag, _ExecutionPolicy&&, _F1&& __f1, _F2&& __f2) +{ + std::forward<_F1>(__f1)(); + std::forward<_F2>(__f2)(); +} + +} // namespace __serial_backend +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_PARALLEL_BACKEND_SERIAL_H */ diff --git a/pstl/include/pstl/internal/parallel_backend_tbb.h b/pstl/include/pstl/internal/parallel_backend_tbb.h new file mode 100644 index 0000000..e336f69 --- /dev/null +++ b/pstl/include/pstl/internal/parallel_backend_tbb.h @@ -0,0 +1,1296 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_PARALLEL_BACKEND_TBB_H +#define _PSTL_PARALLEL_BACKEND_TBB_H + +#include <algorithm> +#include <type_traits> + +#include "pstl_config.h" +#include "parallel_backend_utils.h" + +// Bring in minimal required subset of Intel TBB +#include <tbb/blocked_range.h> +#include <tbb/parallel_for.h> +#include <tbb/parallel_reduce.h> +#include <tbb/parallel_scan.h> +#include <tbb/parallel_invoke.h> +#include <tbb/task_arena.h> +#include <tbb/tbb_allocator.h> +#include <tbb/task.h> + +#if TBB_INTERFACE_VERSION < 10000 +# error Intel(R) Threading Building Blocks 2018 is required; older versions are not supported. +#endif + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __tbb_backend +{ + +//! Raw memory buffer with automatic freeing and no exceptions. +/** Some of our algorithms need to start with raw memory buffer, +not an initialize array, because initialization/destruction +would make the span be at least O(N). */ +// tbb::allocator can improve performance in some cases. +template <typename _Tp> +class __buffer +{ + tbb::tbb_allocator<_Tp> _M_allocator; + _Tp* _M_ptr; + const std::size_t _M_buf_size; + __buffer(const __buffer&) = delete; + void + operator=(const __buffer&) = delete; + + public: + //! Try to obtain buffer of given size to store objects of _Tp type + __buffer(std::size_t n) : _M_allocator(), _M_ptr(_M_allocator.allocate(n)), _M_buf_size(n) {} + //! True if buffer was successfully obtained, zero otherwise. + operator bool() const { return _M_ptr != NULL; } + //! Return pointer to buffer, or NULL if buffer could not be obtained. + _Tp* + get() const + { + return _M_ptr; + } + //! Destroy buffer + ~__buffer() { _M_allocator.deallocate(_M_ptr, _M_buf_size); } +}; + +// Wrapper for tbb::task +inline void +__cancel_execution() +{ +#if TBB_INTERFACE_VERSION <= 12000 + tbb::task::self().group()->cancel_group_execution(); +#else + tbb::task::current_context()->cancel_group_execution(); +#endif +} + +//------------------------------------------------------------------------ +// parallel_for +//------------------------------------------------------------------------ + +template <class _Index, class _RealBody> +class __parallel_for_body +{ + public: + __parallel_for_body(const _RealBody& __body) : _M_body(__body) {} + __parallel_for_body(const __parallel_for_body& __body) : _M_body(__body._M_body) {} + void + operator()(const tbb::blocked_range<_Index>& __range) const + { + _M_body(__range.begin(), __range.end()); + } + + private: + _RealBody _M_body; +}; + +//! Evaluation of brick f[i,j) for each subrange [i,j) of [first,last) +// wrapper over tbb::parallel_for +template <class _ExecutionPolicy, class _Index, class _Fp> +void +__parallel_for(__pstl::__internal::__tbb_backend_tag, _ExecutionPolicy&&, _Index __first, _Index __last, _Fp __f) +{ + tbb::this_task_arena::isolate([=]() { + tbb::parallel_for(tbb::blocked_range<_Index>(__first, __last), __parallel_for_body<_Index, _Fp>(__f)); + }); +} + +//! Evaluation of brick f[i,j) for each subrange [i,j) of [first,last) +// wrapper over tbb::parallel_reduce +template <class _ExecutionPolicy, class _Value, class _Index, typename _RealBody, typename _Reduction> +_Value +__parallel_reduce(__pstl::__internal::__tbb_backend_tag, _ExecutionPolicy&&, _Index __first, _Index __last, + const _Value& __identity, const _RealBody& __real_body, const _Reduction& __reduction) +{ + return tbb::this_task_arena::isolate([__first, __last, &__identity, &__real_body, &__reduction]() -> _Value { + return tbb::parallel_reduce( + tbb::blocked_range<_Index>(__first, __last), __identity, + [__real_body](const tbb::blocked_range<_Index>& __r, const _Value& __value) -> _Value { + return __real_body(__r.begin(), __r.end(), __value); + }, + __reduction); + }); +} + +//------------------------------------------------------------------------ +// parallel_transform_reduce +// +// Notation: +// r(i,j,init) returns reduction of init with reduction over [i,j) +// u(i) returns f(i,i+1,identity) for a hypothetical left identity element of r +// c(x,y) combines values x and y that were the result of r or u +//------------------------------------------------------------------------ + +template <class _Index, class _Up, class _Tp, class _Cp, class _Rp> +struct __par_trans_red_body +{ + alignas(_Tp) char _M_sum_storage[sizeof(_Tp)]; // Holds generalized non-commutative sum when has_sum==true + _Rp _M_brick_reduce; // Most likely to have non-empty layout + _Up _M_u; + _Cp _M_combine; + bool _M_has_sum; // Put last to minimize size of class + _Tp& + sum() + { + __TBB_ASSERT(_M_has_sum, "sum expected"); + return *(_Tp*)_M_sum_storage; + } + __par_trans_red_body(_Up __u, _Tp __init, _Cp __c, _Rp __r) + : _M_brick_reduce(__r), _M_u(__u), _M_combine(__c), _M_has_sum(true) + { + new (_M_sum_storage) _Tp(__init); + } + + __par_trans_red_body(__par_trans_red_body& __left, tbb::split) + : _M_brick_reduce(__left._M_brick_reduce), _M_u(__left._M_u), _M_combine(__left._M_combine), _M_has_sum(false) + { + } + + ~__par_trans_red_body() + { + // 17.6.5.12 tells us to not worry about catching exceptions from destructors. + if (_M_has_sum) + sum().~_Tp(); + } + + void + join(__par_trans_red_body& __rhs) + { + sum() = _M_combine(sum(), __rhs.sum()); + } + + void + operator()(const tbb::blocked_range<_Index>& __range) + { + _Index __i = __range.begin(); + _Index __j = __range.end(); + if (!_M_has_sum) + { + __TBB_ASSERT(__range.size() > 1, "there should be at least 2 elements"); + new (&_M_sum_storage) + _Tp(_M_combine(_M_u(__i), _M_u(__i + 1))); // The condition i+1 < j is provided by the grain size of 3 + _M_has_sum = true; + std::advance(__i, 2); + if (__i == __j) + return; + } + sum() = _M_brick_reduce(__i, __j, sum()); + } +}; + +template <class _ExecutionPolicy, class _Index, class _Up, class _Tp, class _Cp, class _Rp> +_Tp +__parallel_transform_reduce(__pstl::__internal::__tbb_backend_tag, _ExecutionPolicy&&, _Index __first, _Index __last, + _Up __u, _Tp __init, _Cp __combine, _Rp __brick_reduce) +{ + __tbb_backend::__par_trans_red_body<_Index, _Up, _Tp, _Cp, _Rp> __body(__u, __init, __combine, __brick_reduce); + // The grain size of 3 is used in order to provide mininum 2 elements for each body + tbb::this_task_arena::isolate( + [__first, __last, &__body]() { tbb::parallel_reduce(tbb::blocked_range<_Index>(__first, __last, 3), __body); }); + return __body.sum(); +} + +//------------------------------------------------------------------------ +// parallel_scan +//------------------------------------------------------------------------ + +template <class _Index, class _Up, class _Tp, class _Cp, class _Rp, class _Sp> +class __trans_scan_body +{ + alignas(_Tp) char _M_sum_storage[sizeof(_Tp)]; // Holds generalized non-commutative sum when has_sum==true + _Rp _M_brick_reduce; // Most likely to have non-empty layout + _Up _M_u; + _Cp _M_combine; + _Sp _M_scan; + bool _M_has_sum; // Put last to minimize size of class + public: + __trans_scan_body(_Up __u, _Tp __init, _Cp __combine, _Rp __reduce, _Sp __scan) + : _M_brick_reduce(__reduce), _M_u(__u), _M_combine(__combine), _M_scan(__scan), _M_has_sum(true) + { + new (_M_sum_storage) _Tp(__init); + } + + __trans_scan_body(__trans_scan_body& __b, tbb::split) + : _M_brick_reduce(__b._M_brick_reduce), _M_u(__b._M_u), _M_combine(__b._M_combine), _M_scan(__b._M_scan), + _M_has_sum(false) + { + } + + ~__trans_scan_body() + { + // 17.6.5.12 tells us to not worry about catching exceptions from destructors. + if (_M_has_sum) + sum().~_Tp(); + } + + _Tp& + sum() const + { + __TBB_ASSERT(_M_has_sum, "sum expected"); + return *const_cast<_Tp*>(reinterpret_cast<_Tp const*>(_M_sum_storage)); + } + + void + operator()(const tbb::blocked_range<_Index>& __range, tbb::pre_scan_tag) + { + _Index __i = __range.begin(); + _Index __j = __range.end(); + if (!_M_has_sum) + { + new (&_M_sum_storage) _Tp(_M_u(__i)); + _M_has_sum = true; + ++__i; + if (__i == __j) + return; + } + sum() = _M_brick_reduce(__i, __j, sum()); + } + + void + operator()(const tbb::blocked_range<_Index>& __range, tbb::final_scan_tag) + { + sum() = _M_scan(__range.begin(), __range.end(), sum()); + } + + void + reverse_join(__trans_scan_body& __a) + { + if (_M_has_sum) + { + sum() = _M_combine(__a.sum(), sum()); + } + else + { + new (&_M_sum_storage) _Tp(__a.sum()); + _M_has_sum = true; + } + } + + void + assign(__trans_scan_body& __b) + { + sum() = __b.sum(); + } +}; + +template <typename _Index> +_Index +__split(_Index __m) +{ + _Index __k = 1; + while (2 * __k < __m) + __k *= 2; + return __k; +} + +//------------------------------------------------------------------------ +// __parallel_strict_scan +//------------------------------------------------------------------------ + +template <typename _Index, typename _Tp, typename _Rp, typename _Cp> +void +__upsweep(_Index __i, _Index __m, _Index __tilesize, _Tp* __r, _Index __lastsize, _Rp __reduce, _Cp __combine) +{ + if (__m == 1) + __r[0] = __reduce(__i * __tilesize, __lastsize); + else + { + _Index __k = __split(__m); + tbb::parallel_invoke( + [=] { __tbb_backend::__upsweep(__i, __k, __tilesize, __r, __tilesize, __reduce, __combine); }, + [=] { + __tbb_backend::__upsweep(__i + __k, __m - __k, __tilesize, __r + __k, __lastsize, __reduce, __combine); + }); + if (__m == 2 * __k) + __r[__m - 1] = __combine(__r[__k - 1], __r[__m - 1]); + } +} + +template <typename _Index, typename _Tp, typename _Cp, typename _Sp> +void +__downsweep(_Index __i, _Index __m, _Index __tilesize, _Tp* __r, _Index __lastsize, _Tp __initial, _Cp __combine, + _Sp __scan) +{ + if (__m == 1) + __scan(__i * __tilesize, __lastsize, __initial); + else + { + const _Index __k = __split(__m); + tbb::parallel_invoke( + [=] { __tbb_backend::__downsweep(__i, __k, __tilesize, __r, __tilesize, __initial, __combine, __scan); }, + // Assumes that __combine never throws. + //TODO: Consider adding a requirement for user functors to be constant. + [=, &__combine] { + __tbb_backend::__downsweep(__i + __k, __m - __k, __tilesize, __r + __k, __lastsize, + __combine(__initial, __r[__k - 1]), __combine, __scan); + }); + } +} + +// Adapted from Intel(R) Cilk(TM) version from cilkpub. +// Let i:len denote a counted interval of length n starting at i. s denotes a generalized-sum value. +// Expected actions of the functors are: +// reduce(i,len) -> s -- return reduction value of i:len. +// combine(s1,s2) -> s -- return merged sum +// apex(s) -- do any processing necessary between reduce and scan. +// scan(i,len,initial) -- perform scan over i:len starting with initial. +// The initial range 0:n is partitioned into consecutive subranges. +// reduce and scan are each called exactly once per subrange. +// Thus callers can rely upon side effects in reduce. +// combine must not throw an exception. +// apex is called exactly once, after all calls to reduce and before all calls to scan. +// For example, it's useful for allocating a __buffer used by scan but whose size is the sum of all reduction values. +// T must have a trivial constructor and destructor. +template <class _ExecutionPolicy, typename _Index, typename _Tp, typename _Rp, typename _Cp, typename _Sp, typename _Ap> +void +__parallel_strict_scan(__pstl::__internal::__tbb_backend_tag, _ExecutionPolicy&&, _Index __n, _Tp __initial, + _Rp __reduce, _Cp __combine, _Sp __scan, _Ap __apex) +{ + tbb::this_task_arena::isolate([=, &__combine]() { + if (__n > 1) + { + _Index __p = tbb::this_task_arena::max_concurrency(); + const _Index __slack = 4; + _Index __tilesize = (__n - 1) / (__slack * __p) + 1; + _Index __m = (__n - 1) / __tilesize; + __buffer<_Tp> __buf(__m + 1); + _Tp* __r = __buf.get(); + __tbb_backend::__upsweep(_Index(0), _Index(__m + 1), __tilesize, __r, __n - __m * __tilesize, __reduce, + __combine); + + // When __apex is a no-op and __combine has no side effects, a good optimizer + // should be able to eliminate all code between here and __apex. + // Alternatively, provide a default value for __apex that can be + // recognized by metaprogramming that conditionlly executes the following. + size_t __k = __m + 1; + _Tp __t = __r[__k - 1]; + while ((__k &= __k - 1)) + __t = __combine(__r[__k - 1], __t); + __apex(__combine(__initial, __t)); + __tbb_backend::__downsweep(_Index(0), _Index(__m + 1), __tilesize, __r, __n - __m * __tilesize, __initial, + __combine, __scan); + return; + } + // Fewer than 2 elements in sequence, or out of memory. Handle has single block. + _Tp __sum = __initial; + if (__n) + __sum = __combine(__sum, __reduce(_Index(0), __n)); + __apex(__sum); + if (__n) + __scan(_Index(0), __n, __initial); + }); +} + +template <class _ExecutionPolicy, class _Index, class _Up, class _Tp, class _Cp, class _Rp, class _Sp> +_Tp +__parallel_transform_scan(__pstl::__internal::__tbb_backend_tag, _ExecutionPolicy&&, _Index __n, _Up __u, _Tp __init, + _Cp __combine, _Rp __brick_reduce, _Sp __scan) +{ + __trans_scan_body<_Index, _Up, _Tp, _Cp, _Rp, _Sp> __body(__u, __init, __combine, __brick_reduce, __scan); + auto __range = tbb::blocked_range<_Index>(0, __n); + tbb::this_task_arena::isolate([__range, &__body]() { tbb::parallel_scan(__range, __body); }); + return __body.sum(); +} + +//------------------------------------------------------------------------ +// parallel_stable_sort +//------------------------------------------------------------------------ + +//------------------------------------------------------------------------ +// stable_sort utilities +// +// These are used by parallel implementations but do not depend on them. +//------------------------------------------------------------------------ +#define _PSTL_MERGE_CUT_OFF 2000 + +template <typename _Func> +class __func_task; +template <typename _Func> +class __root_task; + +#if TBB_INTERFACE_VERSION <= 12000 +class __task : public tbb::task +{ + public: + template <typename _Fn> + __task* + make_continuation(_Fn&& __f) + { + return new (allocate_continuation()) __func_task<typename std::decay<_Fn>::type>(std::forward<_Fn>(__f)); + } + + template <typename _Fn> + __task* + make_child_of(__task* parent, _Fn&& __f) + { + return new (parent->allocate_child()) __func_task<typename std::decay<_Fn>::type>(std::forward<_Fn>(__f)); + } + + template <typename _Fn> + __task* + make_additional_child_of(tbb::task* parent, _Fn&& __f) + { + return new (tbb::task::allocate_additional_child_of(*parent)) + __func_task<typename std::decay<_Fn>::type>(std::forward<_Fn>(__f)); + } + + inline void + recycle_as_continuation() + { + tbb::task::recycle_as_continuation(); + } + + inline void + recycle_as_child_of(__task* parent) + { + tbb::task::recycle_as_child_of(*parent); + } + + inline void + spawn(__task* __t) + { + tbb::task::spawn(*__t); + } + + template <typename _Fn> + static inline void + spawn_root_and_wait(__root_task<_Fn>& __root) + { + tbb::task::spawn_root_and_wait(*__root._M_task); + } +}; + +template <typename _Func> +class __func_task : public __task +{ + _Func _M_func; + + tbb::task* + execute() + { + return _M_func(this); + }; + + public: + template <typename _Fn> + __func_task(_Fn&& __f) : _M_func{std::forward<_Fn>(__f)} + { + } + + _Func& + body() + { + return _M_func; + } +}; + +template <typename _Func> +class __root_task +{ + tbb::task* _M_task; + + public: + template <typename... Args> + __root_task(Args&&... args) + : _M_task{new (tbb::task::allocate_root()) __func_task<_Func>{_Func(std::forward<Args>(args)...)}} + { + } + + friend class __task; + friend class __func_task<_Func>; +}; + +#else // TBB_INTERFACE_VERSION <= 12000 +class __task : public tbb::detail::d1::task +{ + protected: + tbb::detail::d1::small_object_allocator _M_allocator{}; + tbb::detail::d1::execution_data* _M_execute_data{}; + __task* _M_parent{}; + std::atomic<int> _M_refcount{}; + bool _M_recycle{}; + + template <typename _Fn> + __task* + allocate_func_task(_Fn&& __f) + { + _PSTL_ASSERT(_M_execute_data != nullptr); + tbb::detail::d1::small_object_allocator __alloc{}; + auto __t = + __alloc.new_object<__func_task<typename std::decay<_Fn>::type>>(*_M_execute_data, std::forward<_Fn>(__f)); + __t->_M_allocator = __alloc; + return __t; + } + + public: + __task* + parent() + { + return _M_parent; + } + + void + set_ref_count(int __n) + { + _M_refcount.store(__n, std::memory_order_release); + } + + template <typename _Fn> + __task* + make_continuation(_Fn&& __f) + { + auto __t = allocate_func_task(std::forward<_Fn&&>(__f)); + __t->_M_parent = _M_parent; + _M_parent = nullptr; + return __t; + } + + template <typename _Fn> + __task* + make_child_of(__task* __parent, _Fn&& __f) + { + auto __t = allocate_func_task(std::forward<_Fn&&>(__f)); + __t->_M_parent = __parent; + return __t; + } + + template <typename _Fn> + __task* + make_additional_child_of(__task* __parent, _Fn&& __f) + { + auto __t = make_child_of(__parent, std::forward<_Fn>(__f)); + _PSTL_ASSERT(__parent->_M_refcount.load(std::memory_order_relaxed) > 0); + ++__parent->_M_refcount; + return __t; + } + + inline void + recycle_as_continuation() + { + _M_recycle = true; + } + + inline void + recycle_as_child_of(__task* parent) + { + _M_recycle = true; + _M_parent = parent; + } + + inline void + spawn(__task* __t) + { + _PSTL_ASSERT(_M_execute_data != nullptr); + tbb::detail::d1::spawn(*__t, *_M_execute_data->context); + } + + template <typename _Fn> + static inline void + spawn_root_and_wait(__root_task<_Fn>& __root) + { + tbb::detail::d1::execute_and_wait(*__root._M_func_task, __root._M_context, __root._M_wait_object, + __root._M_context); + } + + template <typename _Func> + friend class __func_task; +}; + +template <typename _Func> +class __func_task : public __task +{ + _Func _M_func; + + __task* + execute(tbb::detail::d1::execution_data& __ed) override + { + _M_execute_data = &__ed; + _M_recycle = false; + __task* __next = _M_func(this); + return finalize(__next); + }; + + __task* + cancel(tbb::detail::d1::execution_data& __ed) override + { + return finalize(nullptr); + } + + __task* + finalize(__task* __next) + { + bool __recycle = _M_recycle; + _M_recycle = false; + + if (__recycle) + { + return __next; + } + + auto __parent = _M_parent; + auto __alloc = _M_allocator; + auto __ed = _M_execute_data; + + this->~__func_task(); + + _PSTL_ASSERT(__parent != nullptr); + _PSTL_ASSERT(__parent->_M_refcount.load(std::memory_order_relaxed) > 0); + if (--__parent->_M_refcount == 0) + { + _PSTL_ASSERT(__next == nullptr); + __alloc.deallocate(this, *__ed); + return __parent; + } + + return __next; + } + + friend class __root_task<_Func>; + + public: + template <typename _Fn> + __func_task(_Fn&& __f) : _M_func(std::forward<_Fn>(__f)) + { + } + + _Func& + body() + { + return _M_func; + } +}; + +template <typename _Func> +class __root_task : public __task +{ + __task* + execute(tbb::detail::d1::execution_data& __ed) override + { + _M_wait_object.release(); + return nullptr; + }; + + __task* + cancel(tbb::detail::d1::execution_data& __ed) override + { + _M_wait_object.release(); + return nullptr; + } + + __func_task<_Func>* _M_func_task{}; + tbb::detail::d1::wait_context _M_wait_object{0}; + tbb::task_group_context _M_context{}; + + public: + template <typename... Args> + __root_task(Args&&... args) : _M_wait_object{1} + { + tbb::detail::d1::small_object_allocator __alloc{}; + _M_func_task = __alloc.new_object<__func_task<_Func>>(_Func(std::forward<Args>(args)...)); + _M_func_task->_M_allocator = __alloc; + _M_func_task->_M_parent = this; + _M_refcount.store(1, std::memory_order_relaxed); + } + + friend class __task; +}; +#endif // TBB_INTERFACE_VERSION <= 12000 + +template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Compare, typename _Cleanup, + typename _LeafMerge> +class __merge_func +{ + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _DifferenceType2; + typedef typename std::common_type<_DifferenceType1, _DifferenceType2>::type _SizeType; + typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type _ValueType; + + _RandomAccessIterator1 _M_x_beg; + _RandomAccessIterator2 _M_z_beg; + + _SizeType _M_xs, _M_xe; + _SizeType _M_ys, _M_ye; + _SizeType _M_zs; + _Compare _M_comp; + _LeafMerge _M_leaf_merge; + _SizeType _M_nsort; //number of elements to be sorted for partial_sort alforithm + + static const _SizeType __merge_cut_off = _PSTL_MERGE_CUT_OFF; + + bool _root; //means a task is merging root task + bool _x_orig; //"true" means X(or left ) subrange is in the original container; false - in the buffer + bool _y_orig; //"true" means Y(or right) subrange is in the original container; false - in the buffer + bool _split; //"true" means a merge task is a split task for parallel merging, the execution logic differs + + bool + is_partial() const + { + return _M_nsort > 0; + } + + struct __move_value + { + template <typename Iterator1, typename Iterator2> + void + operator()(Iterator1 __x, Iterator2 __z) + { + *__z = std::move(*__x); + } + }; + + struct __move_value_construct + { + template <typename Iterator1, typename Iterator2> + void + operator()(Iterator1 __x, Iterator2 __z) + { + ::new (std::addressof(*__z)) _ValueType(std::move(*__x)); + } + }; + + struct __move_range + { + template <typename Iterator1, typename Iterator2> + Iterator2 + operator()(Iterator1 __first1, Iterator1 __last1, Iterator2 __first2) + { + if (__last1 - __first1 < __merge_cut_off) + return std::move(__first1, __last1, __first2); + + auto __n = __last1 - __first1; + tbb::parallel_for(tbb::blocked_range<_SizeType>(0, __n, __merge_cut_off), + [__first1, __first2](const tbb::blocked_range<_SizeType>& __range) { + std::move(__first1 + __range.begin(), __first1 + __range.end(), + __first2 + __range.begin()); + }); + return __first2 + __n; + } + }; + + struct __move_range_construct + { + template <typename Iterator1, typename Iterator2> + Iterator2 + operator()(Iterator1 __first1, Iterator1 __last1, Iterator2 __first2) + { + if (__last1 - __first1 < __merge_cut_off) + { + for (; __first1 != __last1; ++__first1, ++__first2) + __move_value_construct()(__first1, __first2); + return __first2; + } + + auto __n = __last1 - __first1; + tbb::parallel_for(tbb::blocked_range<_SizeType>(0, __n, __merge_cut_off), + [__first1, __first2](const tbb::blocked_range<_SizeType>& __range) { + for (auto i = __range.begin(); i != __range.end(); ++i) + __move_value_construct()(__first1 + i, __first2 + i); + }); + return __first2 + __n; + } + }; + + struct __cleanup_range + { + template <typename Iterator> + void + operator()(Iterator __first, Iterator __last) + { + if (__last - __first < __merge_cut_off) + _Cleanup()(__first, __last); + else + { + auto __n = __last - __first; + tbb::parallel_for(tbb::blocked_range<_SizeType>(0, __n, __merge_cut_off), + [__first](const tbb::blocked_range<_SizeType>& __range) { + _Cleanup()(__first + __range.begin(), __first + __range.end()); + }); + } + } + }; + + public: + __merge_func(_SizeType __xs, _SizeType __xe, _SizeType __ys, _SizeType __ye, _SizeType __zs, _Compare __comp, + _Cleanup, _LeafMerge __leaf_merge, _SizeType __nsort, _RandomAccessIterator1 __x_beg, + _RandomAccessIterator2 __z_beg, bool __x_orig, bool __y_orig, bool __root) + : _M_xs(__xs), _M_xe(__xe), _M_ys(__ys), _M_ye(__ye), _M_zs(__zs), _M_x_beg(__x_beg), _M_z_beg(__z_beg), + _M_comp(__comp), _M_leaf_merge(__leaf_merge), _M_nsort(__nsort), _root(__root), + _x_orig(__x_orig), _y_orig(__y_orig), _split(false) + { + } + + bool + is_left(_SizeType __idx) const + { + return _M_xs == __idx; + } + + template <typename IndexType> + void + set_odd(IndexType __idx, bool __on_off) + { + if (is_left(__idx)) + _x_orig = __on_off; + else + _y_orig = __on_off; + } + + __task* + operator()(__task* __self); + + private: + __merge_func* + parent_merge(__task* __self) const + { + return _root ? nullptr : &static_cast<__func_task<__merge_func>*>(__self->parent())->body(); + } + bool + x_less_y() + { + const auto __nx = (_M_xe - _M_xs); + const auto __ny = (_M_ye - _M_ys); + _PSTL_ASSERT(__nx > 0 && __ny > 0); + + _PSTL_ASSERT(_x_orig == _y_orig); + _PSTL_ASSERT(!is_partial()); + + if (_x_orig) + { + _PSTL_ASSERT(std::is_sorted(_M_x_beg + _M_xs, _M_x_beg + _M_xe, _M_comp)); + _PSTL_ASSERT(std::is_sorted(_M_x_beg + _M_ys, _M_x_beg + _M_ye, _M_comp)); + return !_M_comp(*(_M_x_beg + _M_ys), *(_M_x_beg + _M_xe - 1)); + } + + _PSTL_ASSERT(std::is_sorted(_M_z_beg + _M_xs, _M_z_beg + _M_xe, _M_comp)); + _PSTL_ASSERT(std::is_sorted(_M_z_beg + _M_ys, _M_z_beg + _M_ye, _M_comp)); + return !_M_comp(*(_M_z_beg + _M_zs + __nx), *(_M_z_beg + _M_zs + __nx - 1)); + } + void + move_x_range() + { + const auto __nx = (_M_xe - _M_xs); + const auto __ny = (_M_ye - _M_ys); + _PSTL_ASSERT(__nx > 0 && __ny > 0); + + if (_x_orig) + __move_range_construct()(_M_x_beg + _M_xs, _M_x_beg + _M_xe, _M_z_beg + _M_zs); + else + { + __move_range()(_M_z_beg + _M_zs, _M_z_beg + _M_zs + __nx, _M_x_beg + _M_xs); + __cleanup_range()(_M_z_beg + _M_zs, _M_z_beg + _M_zs + __nx); + } + + _x_orig = !_x_orig; + } + void + move_y_range() + { + const auto __nx = (_M_xe - _M_xs); + const auto __ny = (_M_ye - _M_ys); + + if (_y_orig) + __move_range_construct()(_M_x_beg + _M_ys, _M_x_beg + _M_ye, _M_z_beg + _M_zs + __nx); + else + { + __move_range()(_M_z_beg + _M_zs + __nx, _M_z_beg + _M_zs + __nx + __ny, _M_x_beg + _M_ys); + __cleanup_range()(_M_z_beg + _M_zs + __nx, _M_z_beg + _M_zs + __nx + __ny); + } + + _y_orig = !_y_orig; + } + __task* + merge_ranges(__task* __self) + { + _PSTL_ASSERT(_x_orig == _y_orig); //two merged subrange must be lie into the same buffer + + const auto __nx = (_M_xe - _M_xs); + const auto __ny = (_M_ye - _M_ys); + const auto __n = __nx + __ny; + + // need to merge {x} and {y} + if (__n > __merge_cut_off) + return split_merging(__self); + + //merge to buffer + if (_x_orig) + { + _M_leaf_merge(_M_x_beg + _M_xs, _M_x_beg + _M_xe, _M_x_beg + _M_ys, _M_x_beg + _M_ye, _M_z_beg + _M_zs, + _M_comp, __move_value_construct(), __move_value_construct(), __move_range_construct(), + __move_range_construct()); + _PSTL_ASSERT(parent_merge(__self)); //not root merging task + } + //merge to "origin" + else + { + _PSTL_ASSERT(_x_orig == _y_orig); + + _PSTL_ASSERT(is_partial() || std::is_sorted(_M_z_beg + _M_xs, _M_z_beg + _M_xe, _M_comp)); + _PSTL_ASSERT(is_partial() || std::is_sorted(_M_z_beg + _M_ys, _M_z_beg + _M_ye, _M_comp)); + + const auto __nx = (_M_xe - _M_xs); + const auto __ny = (_M_ye - _M_ys); + + _M_leaf_merge(_M_z_beg + _M_xs, _M_z_beg + _M_xe, _M_z_beg + _M_ys, _M_z_beg + _M_ye, _M_x_beg + _M_zs, + _M_comp, __move_value(), __move_value(), __move_range(), __move_range()); + + __cleanup_range()(_M_z_beg + _M_xs, _M_z_beg + _M_xe); + __cleanup_range()(_M_z_beg + _M_ys, _M_z_beg + _M_ye); + } + return nullptr; + } + + __task* + process_ranges(__task* __self) + { + _PSTL_ASSERT(_x_orig == _y_orig); + _PSTL_ASSERT(!_split); + + auto p = parent_merge(__self); + + if (!p) + { //root merging task + + //optimization, just for sort algorithm, //{x} <= {y} + if (!is_partial() && x_less_y()) //we have a solution + { + if (!_x_orig) + { //we have to move the solution to the origin + move_x_range(); //parallel moving + move_y_range(); //parallel moving + } + return nullptr; + } + //else: if we have data in the origin, + //we have to move data to the buffer for final merging into the origin. + if (_x_orig) + { + move_x_range(); //parallel moving + move_y_range(); //parallel moving + } + // need to merge {x} and {y}. + return merge_ranges(__self); + } + //else: not root merging task (parent_merge() == NULL) + //optimization, just for sort algorithm, //{x} <= {y} + if (!is_partial() && x_less_y()) + { + const auto id_range = _M_zs; + p->set_odd(id_range, _x_orig); + return nullptr; + } + //else: we have to revert "_x(y)_orig" flag of the parent merging task + const auto id_range = _M_zs; + p->set_odd(id_range, !_x_orig); + + return merge_ranges(__self); + } + + //splitting as merge task into 2 of the same level + __task* + split_merging(__task* __self) + { + _PSTL_ASSERT(_x_orig == _y_orig); + const auto __nx = (_M_xe - _M_xs); + const auto __ny = (_M_ye - _M_ys); + + _SizeType __xm{}; + _SizeType __ym{}; + if (__nx < __ny) + { + __ym = _M_ys + __ny / 2; + + if (_x_orig) + __xm = std::upper_bound(_M_x_beg + _M_xs, _M_x_beg + _M_xe, *(_M_x_beg + __ym), _M_comp) - _M_x_beg; + else + __xm = std::upper_bound(_M_z_beg + _M_xs, _M_z_beg + _M_xe, *(_M_z_beg + __ym), _M_comp) - _M_z_beg; + } + else + { + __xm = _M_xs + __nx / 2; + + if (_y_orig) + __ym = std::lower_bound(_M_x_beg + _M_ys, _M_x_beg + _M_ye, *(_M_x_beg + __xm), _M_comp) - _M_x_beg; + else + __ym = std::lower_bound(_M_z_beg + _M_ys, _M_z_beg + _M_ye, *(_M_z_beg + __xm), _M_comp) - _M_z_beg; + } + + auto __zm = _M_zs + ((__xm - _M_xs) + (__ym - _M_ys)); + __merge_func __right_func(__xm, _M_xe, __ym, _M_ye, __zm, _M_comp, _Cleanup(), _M_leaf_merge, _M_nsort, + _M_x_beg, _M_z_beg, _x_orig, _y_orig, _root); + __right_func._split = true; + auto __merge_task = __self->make_additional_child_of(__self->parent(), std::move(__right_func)); + __self->spawn(__merge_task); + __self->recycle_as_continuation(); + + _M_xe = __xm; + _M_ye = __ym; + _split = true; + + return __self; + } +}; + +template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename __M_Compare, typename _Cleanup, + typename _LeafMerge> +__task* +__merge_func<_RandomAccessIterator1, _RandomAccessIterator2, __M_Compare, _Cleanup, _LeafMerge>:: +operator()(__task* __self) +{ + //a. split merge task into 2 of the same level; the special logic, + //without processing(process_ranges) adjacent sub-ranges x and y + if (_split) + return merge_ranges(__self); + + //b. General merging of adjacent sub-ranges x and y (with optimization in case of {x} <= {y} ) + + //1. x and y are in the even buffer + //2. x and y are in the odd buffer + if (_x_orig == _y_orig) + return process_ranges(__self); + + //3. x is in even buffer, y is in the odd buffer + //4. x is in odd buffer, y is in the even buffer + if (!parent_merge(__self)) + { //root merge task + if (_x_orig) + move_x_range(); + else + move_y_range(); + } + else + { + const _SizeType __nx = (_M_xe - _M_xs); + const _SizeType __ny = (_M_ye - _M_ys); + _PSTL_ASSERT(__nx > 0); + _PSTL_ASSERT(__nx > 0); + + if (__nx < __ny) + move_x_range(); + else + move_y_range(); + } + + return process_ranges(__self); +} + +template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Compare, typename _LeafSort> +class __stable_sort_func +{ + public: + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _DifferenceType2; + typedef typename std::common_type<_DifferenceType1, _DifferenceType2>::type _SizeType; + + private: + _RandomAccessIterator1 _M_xs, _M_xe, _M_x_beg; + _RandomAccessIterator2 _M_zs, _M_z_beg; + _Compare _M_comp; + _LeafSort _M_leaf_sort; + bool _M_root; + _SizeType _M_nsort; //zero or number of elements to be sorted for partial_sort alforithm + + public: + __stable_sort_func(_RandomAccessIterator1 __xs, _RandomAccessIterator1 __xe, _RandomAccessIterator2 __zs, + bool __root, _Compare __comp, _LeafSort __leaf_sort, _SizeType __nsort, + _RandomAccessIterator1 __x_beg, _RandomAccessIterator2 __z_beg) + : _M_xs(__xs), _M_xe(__xe), _M_x_beg(__x_beg), _M_zs(__zs), _M_z_beg(__z_beg), _M_comp(__comp), + _M_leaf_sort(__leaf_sort), _M_root(__root), _M_nsort(__nsort) + { + } + + __task* + operator()(__task* __self); +}; + +#define _PSTL_STABLE_SORT_CUT_OFF 500 + +template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Compare, typename _LeafSort> +__task* +__stable_sort_func<_RandomAccessIterator1, _RandomAccessIterator2, _Compare, _LeafSort>::operator()(__task* __self) +{ + typedef __merge_func<_RandomAccessIterator1, _RandomAccessIterator2, _Compare, __utils::__serial_destroy, + __utils::__serial_move_merge> + _MergeTaskType; + + const _SizeType __n = _M_xe - _M_xs; + const _SizeType __nmerge = _M_nsort > 0 ? _M_nsort : __n; + const _SizeType __sort_cut_off = _PSTL_STABLE_SORT_CUT_OFF; + if (__n <= __sort_cut_off) + { + _M_leaf_sort(_M_xs, _M_xe, _M_comp); + _PSTL_ASSERT(!_M_root); + return nullptr; + } + + const _RandomAccessIterator1 __xm = _M_xs + __n / 2; + const _RandomAccessIterator2 __zm = _M_zs + (__xm - _M_xs); + const _RandomAccessIterator2 __ze = _M_zs + __n; + _MergeTaskType __m(_MergeTaskType(_M_xs - _M_x_beg, __xm - _M_x_beg, __xm - _M_x_beg, _M_xe - _M_x_beg, + _M_zs - _M_z_beg, _M_comp, __utils::__serial_destroy(), + __utils::__serial_move_merge(__nmerge), _M_nsort, _M_x_beg, _M_z_beg, + /*x_orig*/ true, /*y_orig*/ true, /*root*/ _M_root)); + auto __parent = __self->make_continuation(std::move(__m)); + __parent->set_ref_count(2); + auto __right = __self->make_child_of( + __parent, __stable_sort_func(__xm, _M_xe, __zm, false, _M_comp, _M_leaf_sort, _M_nsort, _M_x_beg, _M_z_beg)); + __self->spawn(__right); + __self->recycle_as_child_of(__parent); + _M_root = false; + _M_xe = __xm; + + return __self; +} + +template <class _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare, typename _LeafSort> +void +__parallel_stable_sort(__pstl::__internal::__tbb_backend_tag, _ExecutionPolicy&&, _RandomAccessIterator __xs, + _RandomAccessIterator __xe, _Compare __comp, _LeafSort __leaf_sort, std::size_t __nsort = 0) +{ + tbb::this_task_arena::isolate([=, &__nsort]() { + //sorting based on task tree and parallel merge + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _ValueType; + typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType; + const _DifferenceType __n = __xe - __xs; + if (__nsort == __n) + __nsort = 0; // 'partial_sort' becames 'sort' + + const _DifferenceType __sort_cut_off = _PSTL_STABLE_SORT_CUT_OFF; + if (__n > __sort_cut_off) + { + __buffer<_ValueType> __buf(__n); + __root_task<__stable_sort_func<_RandomAccessIterator, _ValueType*, _Compare, _LeafSort>> __root{ + __xs, __xe, __buf.get(), true, __comp, __leaf_sort, __nsort, __xs, __buf.get()}; + __task::spawn_root_and_wait(__root); + return; + } + //serial sort + __leaf_sort(__xs, __xe, __comp); + }); +} + +//------------------------------------------------------------------------ +// parallel_merge +//------------------------------------------------------------------------ +template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _RandomAccessIterator3, + typename _Compare, typename _LeafMerge> +class __merge_func_static +{ + _RandomAccessIterator1 _M_xs, _M_xe; + _RandomAccessIterator2 _M_ys, _M_ye; + _RandomAccessIterator3 _M_zs; + _Compare _M_comp; + _LeafMerge _M_leaf_merge; + + public: + __merge_func_static(_RandomAccessIterator1 __xs, _RandomAccessIterator1 __xe, _RandomAccessIterator2 __ys, + _RandomAccessIterator2 __ye, _RandomAccessIterator3 __zs, _Compare __comp, + _LeafMerge __leaf_merge) + : _M_xs(__xs), _M_xe(__xe), _M_ys(__ys), _M_ye(__ye), _M_zs(__zs), _M_comp(__comp), _M_leaf_merge(__leaf_merge) + { + } + + __task* + operator()(__task* __self); +}; + +//TODO: consider usage of parallel_for with a custom blocked_range +template <typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _RandomAccessIterator3, + typename __M_Compare, typename _LeafMerge> +__task* +__merge_func_static<_RandomAccessIterator1, _RandomAccessIterator2, _RandomAccessIterator3, __M_Compare, _LeafMerge>:: +operator()(__task* __self) +{ + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _DifferenceType2; + typedef typename std::common_type<_DifferenceType1, _DifferenceType2>::type _SizeType; + const _SizeType __n = (_M_xe - _M_xs) + (_M_ye - _M_ys); + const _SizeType __merge_cut_off = _PSTL_MERGE_CUT_OFF; + if (__n <= __merge_cut_off) + { + _M_leaf_merge(_M_xs, _M_xe, _M_ys, _M_ye, _M_zs, _M_comp); + return nullptr; + } + + _RandomAccessIterator1 __xm; + _RandomAccessIterator2 __ym; + if (_M_xe - _M_xs < _M_ye - _M_ys) + { + __ym = _M_ys + (_M_ye - _M_ys) / 2; + __xm = std::upper_bound(_M_xs, _M_xe, *__ym, _M_comp); + } + else + { + __xm = _M_xs + (_M_xe - _M_xs) / 2; + __ym = std::lower_bound(_M_ys, _M_ye, *__xm, _M_comp); + } + const _RandomAccessIterator3 __zm = _M_zs + ((__xm - _M_xs) + (__ym - _M_ys)); + auto __right = __self->make_additional_child_of( + __self->parent(), __merge_func_static(__xm, _M_xe, __ym, _M_ye, __zm, _M_comp, _M_leaf_merge)); + __self->spawn(__right); + __self->recycle_as_continuation(); + _M_xe = __xm; + _M_ye = __ym; + + return __self; +} + +template <class _ExecutionPolicy, typename _RandomAccessIterator1, typename _RandomAccessIterator2, + typename _RandomAccessIterator3, typename _Compare, typename _LeafMerge> +void +__parallel_merge(__pstl::__internal::__tbb_backend_tag, _ExecutionPolicy&&, _RandomAccessIterator1 __xs, + _RandomAccessIterator1 __xe, _RandomAccessIterator2 __ys, _RandomAccessIterator2 __ye, + _RandomAccessIterator3 __zs, _Compare __comp, _LeafMerge __leaf_merge) +{ + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _DifferenceType2; + typedef typename std::common_type<_DifferenceType1, _DifferenceType2>::type _SizeType; + const _SizeType __n = (__xe - __xs) + (__ye - __ys); + const _SizeType __merge_cut_off = _PSTL_MERGE_CUT_OFF; + if (__n <= __merge_cut_off) + { + // Fall back on serial merge + __leaf_merge(__xs, __xe, __ys, __ye, __zs, __comp); + } + else + { + tbb::this_task_arena::isolate([=]() { + typedef __merge_func_static<_RandomAccessIterator1, _RandomAccessIterator2, _RandomAccessIterator3, + _Compare, _LeafMerge> + _TaskType; + __root_task<_TaskType> __root{__xs, __xe, __ys, __ye, __zs, __comp, __leaf_merge}; + __task::spawn_root_and_wait(__root); + }); + } +} + +//------------------------------------------------------------------------ +// parallel_invoke +//------------------------------------------------------------------------ +template <class _ExecutionPolicy, typename _F1, typename _F2> +void +__parallel_invoke(__pstl::__internal::__tbb_backend_tag, _ExecutionPolicy&&, _F1&& __f1, _F2&& __f2) +{ + //TODO: a version of tbb::this_task_arena::isolate with variadic arguments pack should be added in the future + tbb::this_task_arena::isolate([&]() { tbb::parallel_invoke(std::forward<_F1>(__f1), std::forward<_F2>(__f2)); }); +} + +} // namespace __tbb_backend +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_PARALLEL_BACKEND_TBB_H */ diff --git a/pstl/include/pstl/internal/parallel_backend_utils.h b/pstl/include/pstl/internal/parallel_backend_utils.h new file mode 100644 index 0000000..e176d7e --- /dev/null +++ b/pstl/include/pstl/internal/parallel_backend_utils.h @@ -0,0 +1,263 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_PARALLEL_BACKEND_UTILS_H +#define _PSTL_PARALLEL_BACKEND_UTILS_H + +#include <iterator> +#include <utility> +#include "utils.h" + +#include "pstl_config.h" + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ + +namespace __utils +{ + +//! Destroy sequence [xs,xe) +struct __serial_destroy +{ + template <typename _RandomAccessIterator> + void + operator()(_RandomAccessIterator __zs, _RandomAccessIterator __ze) + { + typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _ValueType; + while (__zs != __ze) + { + --__ze; + (*__ze).~_ValueType(); + } + } +}; + +//! Merge sequences [__xs,__xe) and [__ys,__ye) to output sequence [__zs,(__xe-__xs)+(__ye-__ys)), using std::move +struct __serial_move_merge +{ + const std::size_t _M_nmerge; + + explicit __serial_move_merge(std::size_t __nmerge) : _M_nmerge(__nmerge) {} + template <class _RandomAccessIterator1, class _RandomAccessIterator2, class _RandomAccessIterator3, class _Compare, + class _MoveValueX, class _MoveValueY, class _MoveSequenceX, class _MoveSequenceY> + void + operator()(_RandomAccessIterator1 __xs, _RandomAccessIterator1 __xe, _RandomAccessIterator2 __ys, + _RandomAccessIterator2 __ye, _RandomAccessIterator3 __zs, _Compare __comp, _MoveValueX __move_value_x, + _MoveValueY __move_value_y, _MoveSequenceX __move_sequence_x, _MoveSequenceY __move_sequence_y) + { + constexpr bool __same_move_val = std::is_same<_MoveValueX, _MoveValueY>::value; + constexpr bool __same_move_seq = std::is_same<_MoveSequenceX, _MoveSequenceY>::value; + + auto __n = _M_nmerge; + _PSTL_ASSERT(__n > 0); + + auto __nx = __xe - __xs; + //auto __ny = __ye - __ys; + _RandomAccessIterator3 __zs_beg = __zs; + + if (__xs != __xe) + { + if (__ys != __ye) + { + for (;;) + { + if (__comp(*__ys, *__xs)) + { + const auto __i = __zs - __zs_beg; + if (__i < __nx) + __move_value_x(__ys, __zs); + else + __move_value_y(__ys, __zs); + ++__zs, --__n; + if (++__ys == __ye) + { + break; + } + else if (__n == 0) + { + const auto __j = __zs - __zs_beg; + if (__same_move_seq || __j < __nx) + __zs = __move_sequence_x(__ys, __ye, __zs); + else + __zs = __move_sequence_y(__ys, __ye, __zs); + break; + } + } + else + { + const auto __i = __zs - __zs_beg; + if (__same_move_val || __i < __nx) + __move_value_x(__xs, __zs); + else + __move_value_y(__xs, __zs); + ++__zs, --__n; + if (++__xs == __xe) + { + const auto __j = __zs - __zs_beg; + if (__same_move_seq || __j < __nx) + __move_sequence_x(__ys, __ye, __zs); + else + __move_sequence_y(__ys, __ye, __zs); + return; + } + else if (__n == 0) + { + const auto __j = __zs - __zs_beg; + if (__same_move_seq || __j < __nx) + { + __zs = __move_sequence_x(__xs, __xe, __zs); + __move_sequence_x(__ys, __ye, __zs); + } + else + { + __zs = __move_sequence_y(__xs, __xe, __zs); + __move_sequence_y(__ys, __ye, __zs); + } + return; + } + } + } + } + __ys = __xs; + __ye = __xe; + } + const auto __i = __zs - __zs_beg; + if (__same_move_seq || __i < __nx) + __move_sequence_x(__ys, __ye, __zs); + else + __move_sequence_y(__ys, __ye, __zs); + } +}; + +template <typename _ForwardIterator1, typename _ForwardIterator2, typename _OutputIterator, typename _Compare, + typename _CopyConstructRange> +_OutputIterator +__set_union_construct(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp, + _CopyConstructRange __cc_range) +{ + using _Tp = typename std::iterator_traits<_OutputIterator>::value_type; + + for (; __first1 != __last1; ++__result) + { + if (__first2 == __last2) + return __cc_range(__first1, __last1, __result); + if (__comp(*__first2, *__first1)) + { + ::new (std::addressof(*__result)) _Tp(*__first2); + ++__first2; + } + else + { + ::new (std::addressof(*__result)) _Tp(*__first1); + if (!__comp(*__first1, *__first2)) + ++__first2; + ++__first1; + } + } + return __cc_range(__first2, __last2, __result); +} + +template <typename _ForwardIterator1, typename _ForwardIterator2, typename _OutputIterator, typename _Compare> +_OutputIterator +__set_intersection_construct(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp) +{ + using _Tp = typename std::iterator_traits<_OutputIterator>::value_type; + + for (; __first1 != __last1 && __first2 != __last2;) + { + if (__comp(*__first1, *__first2)) + ++__first1; + else + { + if (!__comp(*__first2, *__first1)) + { + ::new (std::addressof(*__result)) _Tp(*__first1); + ++__result; + ++__first1; + } + ++__first2; + } + } + return __result; +} + +template <typename _ForwardIterator1, typename _ForwardIterator2, typename _OutputIterator, typename _Compare, + typename _CopyConstructRange> +_OutputIterator +__set_difference_construct(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp, + _CopyConstructRange __cc_range) +{ + using _Tp = typename std::iterator_traits<_OutputIterator>::value_type; + + for (; __first1 != __last1;) + { + if (__first2 == __last2) + return __cc_range(__first1, __last1, __result); + + if (__comp(*__first1, *__first2)) + { + ::new (std::addressof(*__result)) _Tp(*__first1); + ++__result; + ++__first1; + } + else + { + if (!__comp(*__first2, *__first1)) + ++__first1; + ++__first2; + } + } + return __result; +} +template <typename _ForwardIterator1, typename _ForwardIterator2, typename _OutputIterator, typename _Compare, + typename _CopyConstructRange> +_OutputIterator +__set_symmetric_difference_construct(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, + _ForwardIterator2 __last2, _OutputIterator __result, _Compare __comp, + _CopyConstructRange __cc_range) +{ + using _Tp = typename std::iterator_traits<_OutputIterator>::value_type; + + for (; __first1 != __last1;) + { + if (__first2 == __last2) + return __cc_range(__first1, __last1, __result); + + if (__comp(*__first1, *__first2)) + { + ::new (std::addressof(*__result)) _Tp(*__first1); + ++__result; + ++__first1; + } + else + { + if (__comp(*__first2, *__first1)) + { + ::new (std::addressof(*__result)) _Tp(*__first2); + ++__result; + } + else + ++__first1; + ++__first2; + } + } + return __cc_range(__first2, __last2, __result); +} + +} // namespace __utils +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_PARALLEL_BACKEND_UTILS_H */ diff --git a/pstl/include/pstl/internal/parallel_impl.h b/pstl/include/pstl/internal/parallel_impl.h new file mode 100644 index 0000000..76b3f43 --- /dev/null +++ b/pstl/include/pstl/internal/parallel_impl.h @@ -0,0 +1,90 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_PARALLEL_IMPL_H +#define _PSTL_PARALLEL_IMPL_H + +#include "pstl_config.h" + +#include <atomic> +// This header defines the minimum set of parallel routines required to support Parallel STL, +// implemented on top of Intel(R) Threading Building Blocks (Intel(R) TBB) library + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __internal +{ + +//------------------------------------------------------------------------ +// parallel_find +//----------------------------------------------------------------------- +/** Return extremum value returned by brick f[i,j) for subranges [i,j) of [first,last) +Each f[i,j) must return a value in [i,j). */ +template <class _BackendTag, class _ExecutionPolicy, class _Index, class _Brick, class _Compare> +_Index +__parallel_find(_BackendTag __tag, _ExecutionPolicy&& __exec, _Index __first, _Index __last, _Brick __f, + _Compare __comp, bool __b_first) +{ + typedef typename std::iterator_traits<_Index>::difference_type _DifferenceType; + const _DifferenceType __n = __last - __first; + _DifferenceType __initial_dist = __b_first ? __n : -1; + std::atomic<_DifferenceType> __extremum(__initial_dist); + // TODO: find out what is better here: parallel_for or parallel_reduce + __par_backend::__parallel_for(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__comp, __f, __first, &__extremum](_Index __i, _Index __j) + { + // See "Reducing Contention Through Priority Updates", PPoPP '13, for discussion of + // why using a shared variable scales fairly well in this situation. + if (__comp(__i - __first, __extremum)) + { + _Index __res = __f(__i, __j); + // If not '__last' returned then we found what we want so put this to extremum + if (__res != __j) + { + const _DifferenceType __k = __res - __first; + for (_DifferenceType __old = __extremum; __comp(__k, __old); + __old = __extremum) + { + __extremum.compare_exchange_weak(__old, __k); + } + } + } + }); + return __extremum != __initial_dist ? __first + __extremum : __last; +} + +//------------------------------------------------------------------------ +// parallel_or +//------------------------------------------------------------------------ +//! Return true if brick f[i,j) returns true for some subrange [i,j) of [first,last) +template <class _BackendTag, class _ExecutionPolicy, class _Index, class _Brick> +bool +__parallel_or(_BackendTag __tag, _ExecutionPolicy&& __exec, _Index __first, _Index __last, _Brick __f) +{ + std::atomic<bool> __found(false); + __par_backend::__parallel_for(__tag, std::forward<_ExecutionPolicy>(__exec), __first, __last, + [__f, &__found](_Index __i, _Index __j) + { + if (!__found.load(std::memory_order_relaxed) && __f(__i, __j)) + { + __found.store(true, std::memory_order_relaxed); + __par_backend::__cancel_execution(); + } + }); + return __found; +} + +} // namespace __internal +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_PARALLEL_IMPL_H */ diff --git a/pstl/include/pstl/internal/pstl_config.h b/pstl/include/pstl/internal/pstl_config.h new file mode 100644 index 0000000..8303226 --- /dev/null +++ b/pstl/include/pstl/internal/pstl_config.h @@ -0,0 +1,204 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_CONFIG_H +#define _PSTL_CONFIG_H + +#include <__pstl_config_site> + +// The version is XYYZ, where X is major, YY is minor, and Z is patch (i.e. X.YY.Z) +#define _PSTL_VERSION 17000 +#define _PSTL_VERSION_MAJOR (_PSTL_VERSION / 1000) +#define _PSTL_VERSION_MINOR ((_PSTL_VERSION % 1000) / 10) +#define _PSTL_VERSION_PATCH (_PSTL_VERSION % 10) + +#if !defined(_PSTL_PAR_BACKEND_SERIAL) && !defined(_PSTL_PAR_BACKEND_TBB) && !defined(_PSTL_PAR_BACKEND_OPENMP) +# error "A parallel backend must be specified" +#endif + +// Check the user-defined macro for warnings +#if defined(PSTL_USAGE_WARNINGS) +# define _PSTL_USAGE_WARNINGS +#endif + +#if defined(_LIBCPP_VERSION) +# include <__assert> +# define _PSTL_ASSERT(pred) _LIBCPP_ASSERT(pred, "") +#elif defined(__GLIBCXX__) +# define _PSTL_ASSERT(pred) __glibcxx_assert(pred) +#else +# include <cassert> +# define _PSTL_ASSERT(pred) (assert((pred))) +#endif + +// Portability "#pragma" definition +#ifdef _MSC_VER +# define _PSTL_PRAGMA(x) __pragma(x) +#else +# define _PSTL_PRAGMA(x) _Pragma(# x) +#endif + +#define _PSTL_STRING_AUX(x) #x +#define _PSTL_STRING(x) _PSTL_STRING_AUX(x) +#define _PSTL_STRING_CONCAT(x, y) x #y + +#ifdef _PSTL_HIDE_FROM_ABI_PER_TU +# define _PSTL_HIDE_FROM_ABI_PUSH \ + _Pragma("clang attribute push(__attribute__((internal_linkage)), apply_to=any(function,record))") +# define _PSTL_HIDE_FROM_ABI_POP _Pragma("clang attribute pop") +#else +# define _PSTL_HIDE_FROM_ABI_PUSH /* nothing */ +# define _PSTL_HIDE_FROM_ABI_POP /* nothing */ +#endif + +// note that when ICC or Clang is in use, _PSTL_GCC_VERSION might not fully match +// the actual GCC version on the system. +#define _PSTL_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) + +#if defined(__clang__) +// according to clang documentation, version can be vendor specific +# define _PSTL_CLANG_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) +#endif + +// Enable SIMD for compilers that support OpenMP 4.0 +#if (defined(_OPENMP) && _OPENMP >= 201307) || \ + (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1600) || \ + (!defined(__INTEL_COMPILER) && _PSTL_GCC_VERSION >= 40900) || \ + defined(__clang__) +# define _PSTL_PRAGMA_SIMD _PSTL_PRAGMA(omp simd) +# define _PSTL_PRAGMA_DECLARE_SIMD _PSTL_PRAGMA(omp declare simd) +# define _PSTL_PRAGMA_SIMD_REDUCTION(PRM) _PSTL_PRAGMA(omp simd reduction(PRM)) +#elif !defined(_MSC_VER) //#pragma simd +# define _PSTL_PRAGMA_SIMD _PSTL_PRAGMA(simd) +# define _PSTL_PRAGMA_DECLARE_SIMD +# define _PSTL_PRAGMA_SIMD_REDUCTION(PRM) _PSTL_PRAGMA(simd reduction(PRM)) +#else //no simd +# define _PSTL_PRAGMA_SIMD +# define _PSTL_PRAGMA_DECLARE_SIMD +# define _PSTL_PRAGMA_SIMD_REDUCTION(PRM) +#endif //Enable SIMD + +#if defined(__INTEL_COMPILER) +# define _PSTL_PRAGMA_FORCEINLINE _PSTL_PRAGMA(forceinline) +#else +# define _PSTL_PRAGMA_FORCEINLINE +#endif + +#if defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1900 +# define _PSTL_PRAGMA_SIMD_SCAN(PRM) _PSTL_PRAGMA(omp simd reduction(inscan, PRM)) +# define _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(PRM) _PSTL_PRAGMA(omp scan inclusive(PRM)) +# define _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(PRM) _PSTL_PRAGMA(omp scan exclusive(PRM)) +#else +# define _PSTL_PRAGMA_SIMD_SCAN(PRM) +# define _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(PRM) +# define _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(PRM) +#endif + +// Should be defined to 1 for environments with a vendor implementation of C++17 execution policies +#define _PSTL_CPP17_EXECUTION_POLICIES_PRESENT (_MSC_VER >= 1912 && _MSVC_LANG >= 201703L) || \ + (_GLIBCXX_RELEASE >= 9 && __GLIBCXX__ >= 20190503 && __cplusplus >= 201703L) + +#if (defined(_MSC_VER) && _MSC_VER >= 1900) || \ + __cplusplus >= 201300L || \ + __cpp_lib_robust_nonmodifying_seq_ops == 201304 +# define _PSTL_CPP14_2RANGE_MISMATCH_EQUAL_PRESENT +#endif +#if (defined(_MSC_VER) && _MSC_VER >= 1900) || \ + __cplusplus >= 201402L || \ + __cpp_lib_make_reverse_iterator == 201402 +# define _PSTL_CPP14_MAKE_REVERSE_ITERATOR_PRESENT +#endif +#if (defined(_MSC_VER) && _MSC_VER >= 1900) || __cplusplus >= 201402L +# define _PSTL_CPP14_INTEGER_SEQUENCE_PRESENT +#endif +#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1700) || \ + (defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918) || \ + __cplusplus >= 201402L +# define _PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT +#endif + +#if defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1800 +# define _PSTL_EARLYEXIT_PRESENT +# define _PSTL_MONOTONIC_PRESENT +#endif + +#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1900) || \ + (!defined(__INTEL_COMPILER) && _PSTL_GCC_VERSION >= 40900) || \ + (defined(_OPENMP) && _OPENMP >= 201307) +# define _PSTL_UDR_PRESENT +#endif + +#if defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1900 && __INTEL_COMPILER_BUILD_DATE >= 20180626 +# define _PSTL_UDS_PRESENT +#endif + +#if defined(_PSTL_EARLYEXIT_PRESENT) +# define _PSTL_PRAGMA_SIMD_EARLYEXIT _PSTL_PRAGMA(omp simd early_exit) +#else +# define _PSTL_PRAGMA_SIMD_EARLYEXIT +#endif + +#if defined(_PSTL_MONOTONIC_PRESENT) +# define _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC(PRM) _PSTL_PRAGMA(omp ordered simd monotonic(PRM)) +# define _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC_2ARGS(PRM1, PRM2) _PSTL_PRAGMA(omp ordered simd monotonic(PRM1, PRM2)) +#else +# define _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC(PRM) +# define _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC_2ARGS(PRM1, PRM2) +#endif + +// Declaration of reduction functor, where +// NAME - the name of the functor +// OP - type of the callable object with the reduction operation +// omp_in - refers to the local partial result +// omp_out - refers to the final value of the combiner operator +// omp_priv - refers to the private copy of the initial value +// omp_orig - refers to the original variable to be reduced +#define _PSTL_PRAGMA_DECLARE_REDUCTION(NAME, OP) \ + _PSTL_PRAGMA(omp declare reduction(NAME:OP : omp_out(omp_in)) initializer(omp_priv = omp_orig)) + +#if defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1600 +# define _PSTL_PRAGMA_VECTOR_UNALIGNED _PSTL_PRAGMA(vector unaligned) +#else +# define _PSTL_PRAGMA_VECTOR_UNALIGNED +#endif + +// Check the user-defined macro to use non-temporal stores +#if defined(PSTL_USE_NONTEMPORAL_STORES) && (__INTEL_COMPILER >= 1600) +# define _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED _PSTL_PRAGMA(vector nontemporal) +#else +# define _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED +#endif + +#if defined(_MSC_VER) || defined(__INTEL_COMPILER) // the preprocessors don't type a message location +# define _PSTL_PRAGMA_LOCATION __FILE__ ":" _PSTL_STRING(__LINE__) ": [Parallel STL message]: " +#else +# define _PSTL_PRAGMA_LOCATION " [Parallel STL message]: " +#endif + +#define _PSTL_PRAGMA_MESSAGE_IMPL(x) _PSTL_PRAGMA(message(_PSTL_STRING_CONCAT(_PSTL_PRAGMA_LOCATION, x))) + +#if defined(_PSTL_USAGE_WARNINGS) +# define _PSTL_PRAGMA_MESSAGE(x) _PSTL_PRAGMA_MESSAGE_IMPL(x) +# define _PSTL_PRAGMA_MESSAGE_POLICIES(x) _PSTL_PRAGMA_MESSAGE_IMPL(x) +#else +# define _PSTL_PRAGMA_MESSAGE(x) +# define _PSTL_PRAGMA_MESSAGE_POLICIES(x) +#endif + +// broken macros +#if (defined(__GLIBCXX__) && __GLIBCXX__ < 20150716) || \ + (defined(_MSC_VER) && _MSC_VER < 1800) +# define _PSTL_CPP11_STD_ROTATE_BROKEN +#endif + +#if defined(__INTEL_COMPILER) && __INTEL_COMPILER == 1800 +# define _PSTL_ICC_18_OMP_SIMD_BROKEN +#endif + +#endif /* _PSTL_CONFIG_H */ diff --git a/pstl/include/pstl/internal/unseq_backend_simd.h b/pstl/include/pstl/internal/unseq_backend_simd.h new file mode 100644 index 0000000..9521a42 --- /dev/null +++ b/pstl/include/pstl/internal/unseq_backend_simd.h @@ -0,0 +1,862 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_UNSEQ_BACKEND_SIMD_H +#define _PSTL_UNSEQ_BACKEND_SIMD_H + +#include <type_traits> + +#include "pstl_config.h" +#include "utils.h" + +// This header defines the minimum set of vector routines required +// to support parallel STL. + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __unseq_backend +{ + +// Expect vector width up to 64 (or 512 bit) +const std::size_t __lane_size = 64; + +template <class _Iterator, class _DifferenceType, class _Function> +_Iterator +__simd_walk_1(_Iterator __first, _DifferenceType __n, _Function __f) noexcept +{ + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + __f(__first[__i]); + + return __first + __n; +} + +template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Function> +_Iterator2 +__simd_walk_2(_Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Function __f) noexcept +{ + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + __f(__first1[__i], __first2[__i]); + return __first2 + __n; +} + +template <class _Iterator1, class _DifferenceType, class _Iterator2, class _Iterator3, class _Function> +_Iterator3 +__simd_walk_3(_Iterator1 __first1, _DifferenceType __n, _Iterator2 __first2, _Iterator3 __first3, + _Function __f) noexcept +{ + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + __f(__first1[__i], __first2[__i], __first3[__i]); + return __first3 + __n; +} + +// TODO: check whether __simd_first() can be used here +template <class _Index, class _DifferenceType, class _Pred> +bool +__simd_or(_Index __first, _DifferenceType __n, _Pred __pred) noexcept +{ +#if defined(_PSTL_EARLYEXIT_PRESENT) + _DifferenceType __i; + _PSTL_PRAGMA_VECTOR_UNALIGNED + _PSTL_PRAGMA_SIMD_EARLYEXIT + for (__i = 0; __i < __n; ++__i) + if (__pred(__first[__i])) + break; + return __i < __n; +#else + _DifferenceType __block_size = 4 < __n ? 4 : __n; + const _Index __last = __first + __n; + while (__last != __first) + { + int32_t __flag = 1; + _PSTL_PRAGMA_SIMD_REDUCTION(& : __flag) + for (_DifferenceType __i = 0; __i < __block_size; ++__i) + if (__pred(*(__first + __i))) + __flag = 0; + if (!__flag) + return true; + + __first += __block_size; + if (__last - __first >= __block_size << 1) + { + // Double the block _Size. Any unnecessary iterations can be amortized against work done so far. + __block_size <<= 1; + } + else + { + __block_size = __last - __first; + } + } + return false; +#endif +} + +template <class _Index, class _DifferenceType, class _Compare> +_Index +__simd_first(_Index __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp) noexcept +{ +#if defined(_PSTL_EARLYEXIT_PRESENT) + _DifferenceType __i = __begin; + _PSTL_PRAGMA_VECTOR_UNALIGNED // Do not generate peel loop part + _PSTL_PRAGMA_SIMD_EARLYEXIT for (; __i < __end; ++__i) + { + if (__comp(__first, __i)) + { + break; + } + } + return __first + __i; +#else + // Experiments show good block sizes like this + const _DifferenceType __block_size = 8; + alignas(__lane_size) _DifferenceType __lane[__block_size] = {0}; + while (__end - __begin >= __block_size) + { + _DifferenceType __found = 0; + _PSTL_PRAGMA_VECTOR_UNALIGNED // Do not generate peel loop part + _PSTL_PRAGMA_SIMD_REDUCTION(| + : __found) for (_DifferenceType __i = __begin; __i < __begin + __block_size; + ++__i) + { + const _DifferenceType __t = __comp(__first, __i); + __lane[__i - __begin] = __t; + __found |= __t; + } + if (__found) + { + _DifferenceType __i; + // This will vectorize + for (__i = 0; __i < __block_size; ++__i) + { + if (__lane[__i]) + { + break; + } + } + return __first + __begin + __i; + } + __begin += __block_size; + } + + //Keep remainder scalar + while (__begin != __end) + { + if (__comp(__first, __begin)) + { + return __first + __begin; + } + ++__begin; + } + return __first + __end; +#endif //_PSTL_EARLYEXIT_PRESENT +} + +template <class _Index1, class _DifferenceType, class _Index2, class _Pred> +std::pair<_Index1, _Index2> +__simd_first(_Index1 __first1, _DifferenceType __n, _Index2 __first2, _Pred __pred) noexcept +{ +#if defined(_PSTL_EARLYEXIT_PRESENT) + _DifferenceType __i = 0; + _PSTL_PRAGMA_VECTOR_UNALIGNED + _PSTL_PRAGMA_SIMD_EARLYEXIT + for (; __i < __n; ++__i) + if (__pred(__first1[__i], __first2[__i])) + break; + return std::make_pair(__first1 + __i, __first2 + __i); +#else + const _Index1 __last1 = __first1 + __n; + const _Index2 __last2 = __first2 + __n; + // Experiments show good block sizes like this + const _DifferenceType __block_size = 8; + alignas(__lane_size) _DifferenceType __lane[__block_size] = {0}; + while (__last1 - __first1 >= __block_size) + { + _DifferenceType __found = 0; + _DifferenceType __i; + _PSTL_PRAGMA_VECTOR_UNALIGNED // Do not generate peel loop part + _PSTL_PRAGMA_SIMD_REDUCTION(| + : __found) for (__i = 0; __i < __block_size; ++__i) + { + const _DifferenceType __t = __pred(__first1[__i], __first2[__i]); + __lane[__i] = __t; + __found |= __t; + } + if (__found) + { + _DifferenceType __i2; + // This will vectorize + for (__i2 = 0; __i2 < __block_size; ++__i2) + { + if (__lane[__i2]) + break; + } + return std::make_pair(__first1 + __i2, __first2 + __i2); + } + __first1 += __block_size; + __first2 += __block_size; + } + + //Keep remainder scalar + for (; __last1 != __first1; ++__first1, ++__first2) + if (__pred(*(__first1), *(__first2))) + return std::make_pair(__first1, __first2); + + return std::make_pair(__last1, __last2); +#endif //_PSTL_EARLYEXIT_PRESENT +} + +template <class _Index, class _DifferenceType, class _Pred> +_DifferenceType +__simd_count(_Index __index, _DifferenceType __n, _Pred __pred) noexcept +{ + _DifferenceType __count = 0; + _PSTL_PRAGMA_SIMD_REDUCTION(+ : __count) + for (_DifferenceType __i = 0; __i < __n; ++__i) + if (__pred(*(__index + __i))) + ++__count; + + return __count; +} + +template <class _InputIterator, class _DifferenceType, class _OutputIterator, class _BinaryPredicate> +_OutputIterator +__simd_unique_copy(_InputIterator __first, _DifferenceType __n, _OutputIterator __result, + _BinaryPredicate __pred) noexcept +{ + if (__n == 0) + return __result; + + _DifferenceType __cnt = 1; + __result[0] = __first[0]; + + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 1; __i < __n; ++__i) + { + _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC(__cnt : 1) + if (!__pred(__first[__i], __first[__i - 1])) + { + __result[__cnt] = __first[__i]; + ++__cnt; + } + } + return __result + __cnt; +} + +template <class _InputIterator, class _DifferenceType, class _OutputIterator, class _Assigner> +_OutputIterator +__simd_assign(_InputIterator __first, _DifferenceType __n, _OutputIterator __result, _Assigner __assigner) noexcept +{ + _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + __assigner(__first + __i, __result + __i); + return __result + __n; +} + +template <class _InputIterator, class _DifferenceType, class _OutputIterator, class _UnaryPredicate> +_OutputIterator +__simd_copy_if(_InputIterator __first, _DifferenceType __n, _OutputIterator __result, _UnaryPredicate __pred) noexcept +{ + _DifferenceType __cnt = 0; + + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + { + _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC(__cnt : 1) + if (__pred(__first[__i])) + { + __result[__cnt] = __first[__i]; + ++__cnt; + } + } + return __result + __cnt; +} + +template <class _InputIterator, class _DifferenceType, class _BinaryPredicate> +_DifferenceType +__simd_calc_mask_2(_InputIterator __first, _DifferenceType __n, bool* __mask, _BinaryPredicate __pred) noexcept +{ + _DifferenceType __count = 0; + + _PSTL_PRAGMA_SIMD_REDUCTION(+ : __count) + for (_DifferenceType __i = 0; __i < __n; ++__i) + { + __mask[__i] = !__pred(__first[__i], __first[__i - 1]); + __count += __mask[__i]; + } + return __count; +} + +template <class _InputIterator, class _DifferenceType, class _UnaryPredicate> +_DifferenceType +__simd_calc_mask_1(_InputIterator __first, _DifferenceType __n, bool* __mask, _UnaryPredicate __pred) noexcept +{ + _DifferenceType __count = 0; + + _PSTL_PRAGMA_SIMD_REDUCTION(+ : __count) + for (_DifferenceType __i = 0; __i < __n; ++__i) + { + __mask[__i] = __pred(__first[__i]); + __count += __mask[__i]; + } + return __count; +} + +template <class _InputIterator, class _DifferenceType, class _OutputIterator, class _Assigner> +void +__simd_copy_by_mask(_InputIterator __first, _DifferenceType __n, _OutputIterator __result, bool* __mask, + _Assigner __assigner) noexcept +{ + _DifferenceType __cnt = 0; + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + { + if (__mask[__i]) + { + _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC(__cnt : 1) + { + __assigner(__first + __i, __result + __cnt); + ++__cnt; + } + } + } +} + +template <class _InputIterator, class _DifferenceType, class _OutputIterator1, class _OutputIterator2> +void +__simd_partition_by_mask(_InputIterator __first, _DifferenceType __n, _OutputIterator1 __out_true, + _OutputIterator2 __out_false, bool* __mask) noexcept +{ + _DifferenceType __cnt_true = 0, __cnt_false = 0; + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + { + _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC_2ARGS(__cnt_true : 1, __cnt_false : 1) + if (__mask[__i]) + { + __out_true[__cnt_true] = __first[__i]; + ++__cnt_true; + } + else + { + __out_false[__cnt_false] = __first[__i]; + ++__cnt_false; + } + } +} + +template <class _Index, class _DifferenceType, class _Tp> +_Index +__simd_fill_n(_Index __first, _DifferenceType __n, const _Tp& __value) noexcept +{ + _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + __first[__i] = __value; + return __first + __n; +} + +template <class _Index, class _DifferenceType, class _Generator> +_Index +__simd_generate_n(_Index __first, _DifferenceType __size, _Generator __g) noexcept +{ + _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __size; ++__i) + __first[__i] = __g(); + return __first + __size; +} + +template <class _Index, class _BinaryPredicate> +_Index +__simd_adjacent_find(_Index __first, _Index __last, _BinaryPredicate __pred, bool __or_semantic) noexcept +{ + if (__last - __first < 2) + return __last; + + typedef typename std::iterator_traits<_Index>::difference_type _DifferenceType; + _DifferenceType __i = 0; + +#if defined(_PSTL_EARLYEXIT_PRESENT) + //Some compiler versions fail to compile the following loop when iterators are used. Indices are used instead + const _DifferenceType __n = __last - __first - 1; + _PSTL_PRAGMA_VECTOR_UNALIGNED + _PSTL_PRAGMA_SIMD_EARLYEXIT + for (; __i < __n; ++__i) + if (__pred(__first[__i], __first[__i + 1])) + break; + + return __i < __n ? __first + __i : __last; +#else + // Experiments show good block sizes like this + //TODO: to consider tuning block_size for various data types + const _DifferenceType __block_size = 8; + alignas(__lane_size) _DifferenceType __lane[__block_size] = {0}; + while (__last - __first >= __block_size) + { + _DifferenceType __found = 0; + _PSTL_PRAGMA_VECTOR_UNALIGNED // Do not generate peel loop part + _PSTL_PRAGMA_SIMD_REDUCTION(| + : __found) for (__i = 0; __i < __block_size - 1; ++__i) + { + //TODO: to improve SIMD vectorization + const _DifferenceType __t = __pred(*(__first + __i), *(__first + __i + 1)); + __lane[__i] = __t; + __found |= __t; + } + + //Process a pair of elements on a boundary of a data block + if (__first + __block_size < __last && __pred(*(__first + __i), *(__first + __i + 1))) + __lane[__i] = __found = 1; + + if (__found) + { + if (__or_semantic) + return __first; + + // This will vectorize + for (__i = 0; __i < __block_size; ++__i) + if (__lane[__i]) + break; + return __first + __i; //As far as found is true a __result (__lane[__i] is true) is guaranteed + } + __first += __block_size; + } + //Process the rest elements + for (; __last - __first > 1; ++__first) + if (__pred(*__first, *(__first + 1))) + return __first; + + return __last; +#endif +} + +// It was created to reduce the code inside std::enable_if +template <typename _Tp, typename _BinaryOperation> +using is_arithmetic_plus = std::integral_constant<bool, std::is_arithmetic<_Tp>::value && + std::is_same<_BinaryOperation, std::plus<_Tp>>::value>; + +template <typename _DifferenceType, typename _Tp, typename _BinaryOperation, typename _UnaryOperation> +typename std::enable_if<is_arithmetic_plus<_Tp, _BinaryOperation>::value, _Tp>::type +__simd_transform_reduce(_DifferenceType __n, _Tp __init, _BinaryOperation, _UnaryOperation __f) noexcept +{ + _PSTL_PRAGMA_SIMD_REDUCTION(+ : __init) + for (_DifferenceType __i = 0; __i < __n; ++__i) + __init += __f(__i); + return __init; +} + +template <typename _Size, typename _Tp, typename _BinaryOperation, typename _UnaryOperation> +typename std::enable_if<!is_arithmetic_plus<_Tp, _BinaryOperation>::value, _Tp>::type +__simd_transform_reduce(_Size __n, _Tp __init, _BinaryOperation __binary_op, _UnaryOperation __f) noexcept +{ + const _Size __block_size = __lane_size / sizeof(_Tp); + if (__n > 2 * __block_size && __block_size > 1) + { + alignas(__lane_size) char __lane_[__lane_size]; + _Tp* __lane = reinterpret_cast<_Tp*>(__lane_); + + // initializer + _PSTL_PRAGMA_SIMD + for (_Size __i = 0; __i < __block_size; ++__i) + { + ::new (__lane + __i) _Tp(__binary_op(__f(__i), __f(__block_size + __i))); + } + // main loop + _Size __i = 2 * __block_size; + const _Size last_iteration = __block_size * (__n / __block_size); + for (; __i < last_iteration; __i += __block_size) + { + _PSTL_PRAGMA_SIMD + for (_Size __j = 0; __j < __block_size; ++__j) + { + __lane[__j] = __binary_op(__lane[__j], __f(__i + __j)); + } + } + // remainder + _PSTL_PRAGMA_SIMD + for (_Size __j = 0; __j < __n - last_iteration; ++__j) + { + __lane[__j] = __binary_op(__lane[__j], __f(last_iteration + __j)); + } + // combiner + for (_Size __j = 0; __j < __block_size; ++__j) + { + __init = __binary_op(__init, __lane[__j]); + } + // destroyer + _PSTL_PRAGMA_SIMD + for (_Size __j = 0; __j < __block_size; ++__j) + { + __lane[__j].~_Tp(); + } + } + else + { + for (_Size __i = 0; __i < __n; ++__i) + { + __init = __binary_op(__init, __f(__i)); + } + } + return __init; +} + +// Exclusive scan for "+" and arithmetic types +template <class _InputIterator, class _Size, class _OutputIterator, class _UnaryOperation, class _Tp, + class _BinaryOperation> +typename std::enable_if<is_arithmetic_plus<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type +__simd_scan(_InputIterator __first, _Size __n, _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, + _BinaryOperation, /*Inclusive*/ std::false_type) +{ + _PSTL_PRAGMA_SIMD_SCAN(+ : __init) + for (_Size __i = 0; __i < __n; ++__i) + { + __result[__i] = __init; + _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(__init) + __init += __unary_op(__first[__i]); + } + return std::make_pair(__result + __n, __init); +} + +// As soon as we cannot call __binary_op in "combiner" we create a wrapper over _Tp to encapsulate __binary_op +template <typename _Tp, typename _BinaryOp> +struct _Combiner +{ + _Tp __value_; + _BinaryOp* __bin_op; // Here is a pointer to function because of default ctor + + _Combiner() : __value_{}, __bin_op(nullptr) {} + _Combiner(const _Tp& value, const _BinaryOp* bin_op) : __value_(value), __bin_op(const_cast<_BinaryOp*>(bin_op)) {} + _Combiner(const _Combiner& __obj) : __value_{}, __bin_op(__obj.__bin_op) {} + + void + operator()(const _Combiner& __obj) + { + __value_ = (*__bin_op)(__value_, __obj.__value); + } +}; + +// Exclusive scan for other binary operations and types +template <class _InputIterator, class _Size, class _OutputIterator, class _UnaryOperation, class _Tp, + class _BinaryOperation> +typename std::enable_if<!is_arithmetic_plus<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type +__simd_scan(_InputIterator __first, _Size __n, _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, + _BinaryOperation __binary_op, /*Inclusive*/ std::false_type) +{ + typedef _Combiner<_Tp, _BinaryOperation> _CombinerType; + _CombinerType __init_{__init, &__binary_op}; + + _PSTL_PRAGMA_DECLARE_REDUCTION(__bin_op, _CombinerType) + + _PSTL_PRAGMA_SIMD_SCAN(__bin_op : __init_) + for (_Size __i = 0; __i < __n; ++__i) + { + __result[__i] = __init_.__value_; + _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(__init_) + _PSTL_PRAGMA_FORCEINLINE + __init_.__value_ = __binary_op(__init_.__value_, __unary_op(__first[__i])); + } + return std::make_pair(__result + __n, __init_.__value_); +} + +// Inclusive scan for "+" and arithmetic types +template <class _InputIterator, class _Size, class _OutputIterator, class _UnaryOperation, class _Tp, + class _BinaryOperation> +typename std::enable_if<is_arithmetic_plus<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type +__simd_scan(_InputIterator __first, _Size __n, _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, + _BinaryOperation, /*Inclusive*/ std::true_type) +{ + _PSTL_PRAGMA_SIMD_SCAN(+ : __init) + for (_Size __i = 0; __i < __n; ++__i) + { + __init += __unary_op(__first[__i]); + _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(__init) + __result[__i] = __init; + } + return std::make_pair(__result + __n, __init); +} + +// Inclusive scan for other binary operations and types +template <class _InputIterator, class _Size, class _OutputIterator, class _UnaryOperation, class _Tp, + class _BinaryOperation> +typename std::enable_if<!is_arithmetic_plus<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type +__simd_scan(_InputIterator __first, _Size __n, _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, + _BinaryOperation __binary_op, std::true_type) +{ + typedef _Combiner<_Tp, _BinaryOperation> _CombinerType; + _CombinerType __init_{__init, &__binary_op}; + + _PSTL_PRAGMA_DECLARE_REDUCTION(__bin_op, _CombinerType) + + _PSTL_PRAGMA_SIMD_SCAN(__bin_op : __init_) + for (_Size __i = 0; __i < __n; ++__i) + { + _PSTL_PRAGMA_FORCEINLINE + __init_.__value_ = __binary_op(__init_.__value_, __unary_op(__first[__i])); + _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(__init_) + __result[__i] = __init_.__value_; + } + return std::make_pair(__result + __n, __init_.__value_); +} + +// [restriction] - std::iterator_traits<_ForwardIterator>::value_type should be DefaultConstructible. +// complexity [violation] - We will have at most (__n-1 + number_of_lanes) comparisons instead of at most __n-1. +template <typename _ForwardIterator, typename _Size, typename _Compare> +_ForwardIterator +__simd_min_element(_ForwardIterator __first, _Size __n, _Compare __comp) noexcept +{ + if (__n == 0) + { + return __first; + } + + typedef typename std::iterator_traits<_ForwardIterator>::value_type _ValueType; + struct _ComplexType + { + _ValueType __min_val; + _Size __min_ind; + _Compare* __min_comp; + + _ComplexType() : __min_val{}, __min_ind{}, __min_comp(nullptr) {} + _ComplexType(const _ValueType& val, const _Compare* comp) + : __min_val(val), __min_ind(0), __min_comp(const_cast<_Compare*>(comp)) + { + } + _ComplexType(const _ComplexType& __obj) + : __min_val(__obj.__min_val), __min_ind(__obj.__min_ind), __min_comp(__obj.__min_comp) + { + } + + _PSTL_PRAGMA_DECLARE_SIMD + void + operator()(const _ComplexType& __obj) + { + if (!(*__min_comp)(__min_val, __obj.__min_val) && + ((*__min_comp)(__obj.__min_val, __min_val) || __obj.__min_ind - __min_ind < 0)) + { + __min_val = __obj.__min_val; + __min_ind = __obj.__min_ind; + } + } + }; + + _ComplexType __init{*__first, &__comp}; + + _PSTL_PRAGMA_DECLARE_REDUCTION(__min_func, _ComplexType) + + _PSTL_PRAGMA_SIMD_REDUCTION(__min_func : __init) + for (_Size __i = 1; __i < __n; ++__i) + { + const _ValueType __min_val = __init.__min_val; + const _ValueType __current = __first[__i]; + if (__comp(__current, __min_val)) + { + __init.__min_val = __current; + __init.__min_ind = __i; + } + } + return __first + __init.__min_ind; +} + +// [restriction] - std::iterator_traits<_ForwardIterator>::value_type should be DefaultConstructible. +// complexity [violation] - We will have at most (2*(__n-1) + 4*number_of_lanes) comparisons instead of at most [1.5*(__n-1)]. +template <typename _ForwardIterator, typename _Size, typename _Compare> +std::pair<_ForwardIterator, _ForwardIterator> +__simd_minmax_element(_ForwardIterator __first, _Size __n, _Compare __comp) noexcept +{ + if (__n == 0) + { + return std::make_pair(__first, __first); + } + typedef typename std::iterator_traits<_ForwardIterator>::value_type _ValueType; + + struct _ComplexType + { + _ValueType __min_val; + _ValueType __max_val; + _Size __min_ind; + _Size __max_ind; + _Compare* __minmax_comp; + + _ComplexType() : __min_val{}, __max_val{}, __min_ind{}, __max_ind{}, __minmax_comp(nullptr) {} + _ComplexType(const _ValueType& min_val, const _ValueType& max_val, const _Compare* comp) + : __min_val(min_val), __max_val(max_val), __min_ind(0), __max_ind(0), + __minmax_comp(const_cast<_Compare*>(comp)) + { + } + _ComplexType(const _ComplexType& __obj) + : __min_val(__obj.__min_val), __max_val(__obj.__max_val), __min_ind(__obj.__min_ind), + __max_ind(__obj.__max_ind), __minmax_comp(__obj.__minmax_comp) + { + } + + void + operator()(const _ComplexType& __obj) + { + // min + if ((*__minmax_comp)(__obj.__min_val, __min_val)) + { + __min_val = __obj.__min_val; + __min_ind = __obj.__min_ind; + } + else if (!(*__minmax_comp)(__min_val, __obj.__min_val)) + { + __min_val = __obj.__min_val; + __min_ind = (__min_ind - __obj.__min_ind < 0) ? __min_ind : __obj.__min_ind; + } + + // max + if ((*__minmax_comp)(__max_val, __obj.__max_val)) + { + __max_val = __obj.__max_val; + __max_ind = __obj.__max_ind; + } + else if (!(*__minmax_comp)(__obj.__max_val, __max_val)) + { + __max_val = __obj.__max_val; + __max_ind = (__max_ind - __obj.__max_ind < 0) ? __obj.__max_ind : __max_ind; + } + } + }; + + _ComplexType __init{*__first, *__first, &__comp}; + + _PSTL_PRAGMA_DECLARE_REDUCTION(__min_func, _ComplexType); + + _PSTL_PRAGMA_SIMD_REDUCTION(__min_func : __init) + for (_Size __i = 1; __i < __n; ++__i) + { + auto __min_val = __init.__min_val; + auto __max_val = __init.__max_val; + auto __current = __first + __i; + if (__comp(*__current, __min_val)) + { + __init.__min_val = *__current; + __init.__min_ind = __i; + } + else if (!__comp(*__current, __max_val)) + { + __init.__max_val = *__current; + __init.__max_ind = __i; + } + } + return std::make_pair(__first + __init.__min_ind, __first + __init.__max_ind); +} + +template <class _InputIterator, class _DifferenceType, class _OutputIterator1, class _OutputIterator2, + class _UnaryPredicate> +std::pair<_OutputIterator1, _OutputIterator2> +__simd_partition_copy(_InputIterator __first, _DifferenceType __n, _OutputIterator1 __out_true, + _OutputIterator2 __out_false, _UnaryPredicate __pred) noexcept +{ + _DifferenceType __cnt_true = 0, __cnt_false = 0; + + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 0; __i < __n; ++__i) + { + _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC_2ARGS(__cnt_true : 1, __cnt_false : 1) + if (__pred(__first[__i])) + { + __out_true[__cnt_true] = __first[__i]; + ++__cnt_true; + } + else + { + __out_false[__cnt_false] = __first[__i]; + ++__cnt_false; + } + } + return std::make_pair(__out_true + __cnt_true, __out_false + __cnt_false); +} + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +__simd_find_first_of(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __s_first, + _ForwardIterator2 __s_last, _BinaryPredicate __pred) noexcept +{ + typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferencType; + + const _DifferencType __n1 = __last - __first; + const _DifferencType __n2 = __s_last - __s_first; + if (__n1 == 0 || __n2 == 0) + { + return __last; // according to the standard + } + + // Common case + // If first sequence larger than second then we'll run simd_first with parameters of first sequence. + // Otherwise, vice versa. + if (__n1 < __n2) + { + for (; __first != __last; ++__first) + { + if (__unseq_backend::__simd_or( + __s_first, __n2, + __internal::__equal_value_by_pred<decltype(*__first), _BinaryPredicate>(*__first, __pred))) + { + return __first; + } + } + } + else + { + for (; __s_first != __s_last; ++__s_first) + { + const auto __result = __unseq_backend::__simd_first( + __first, _DifferencType(0), __n1, [__s_first, &__pred](_ForwardIterator1 __it, _DifferencType __i) { + return __pred(__it[__i], *__s_first); + }); + if (__result != __last) + { + return __result; + } + } + } + return __last; +} + +template <class _RandomAccessIterator, class _DifferenceType, class _UnaryPredicate> +_RandomAccessIterator +__simd_remove_if(_RandomAccessIterator __first, _DifferenceType __n, _UnaryPredicate __pred) noexcept +{ + // find first element we need to remove + auto __current = __unseq_backend::__simd_first( + __first, _DifferenceType(0), __n, + [&__pred](_RandomAccessIterator __it, _DifferenceType __i) { return __pred(__it[__i]); }); + __n -= __current - __first; + + // if we have in sequence only one element that pred(__current[1]) != false we can exit the function + if (__n < 2) + { + return __current; + } + + _DifferenceType __cnt = 0; + _PSTL_PRAGMA_SIMD + for (_DifferenceType __i = 1; __i < __n; ++__i) + { + _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC(__cnt : 1) + if (!__pred(__current[__i])) + { + __current[__cnt] = std::move(__current[__i]); + ++__cnt; + } + } + return __current + __cnt; +} +} // namespace __unseq_backend +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_UNSEQ_BACKEND_SIMD_H */ diff --git a/pstl/include/pstl/internal/utils.h b/pstl/include/pstl/internal/utils.h new file mode 100644 index 0000000..ec9d467 --- /dev/null +++ b/pstl/include/pstl/internal/utils.h @@ -0,0 +1,177 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 _PSTL_UTILS_H +#define _PSTL_UTILS_H + +#include <new> +#include <iterator> + +_PSTL_HIDE_FROM_ABI_PUSH + +namespace __pstl +{ +namespace __internal +{ + +template <typename _Fp> +auto +__except_handler(_Fp __f) -> decltype(__f()) +{ + try + { + return __f(); + } + catch (const std::bad_alloc&) + { + throw; // re-throw bad_alloc according to the standard [algorithms.parallel.exceptions] + } + catch (...) + { + std::terminate(); // Good bye according to the standard [algorithms.parallel.exceptions] + } +} + +template <typename _Fp> +void +__invoke_if(std::true_type, _Fp __f) +{ + __f(); +} + +template <typename _Fp> +void __invoke_if(std::false_type, _Fp) +{ +} + +template <typename _Fp> +void +__invoke_if_not(std::false_type, _Fp __f) +{ + __f(); +} + +template <typename _Fp> +void __invoke_if_not(std::true_type, _Fp) +{ +} + +template <typename _F1, typename _F2> +auto +__invoke_if_else(std::true_type, _F1 __f1, _F2) -> decltype(__f1()) +{ + return __f1(); +} + +template <typename _F1, typename _F2> +auto +__invoke_if_else(std::false_type, _F1, _F2 __f2) -> decltype(__f2()) +{ + return __f2(); +} + +//! Unary operator that returns reference to its argument. +struct __no_op +{ + template <typename _Tp> + _Tp&& + operator()(_Tp&& __a) const + { + return std::forward<_Tp>(__a); + } +}; + +template <typename _Pred> +class __reorder_pred +{ + _Pred _M_pred; + + public: + explicit __reorder_pred(_Pred __pred) : _M_pred(__pred) {} + + template <typename _FTp, typename _STp> + bool + operator()(_FTp&& __a, _STp&& __b) + { + return _M_pred(std::forward<_STp>(__b), std::forward<_FTp>(__a)); + } +}; + +//! Like a polymorphic lambda for pred(...,value) +template <typename _Tp, typename _Predicate> +class __equal_value_by_pred +{ + const _Tp& _M_value; + _Predicate _M_pred; + + public: + __equal_value_by_pred(const _Tp& __value, _Predicate __pred) : _M_value(__value), _M_pred(__pred) {} + + template <typename _Arg> + bool + operator()(_Arg&& __arg) + { + return _M_pred(std::forward<_Arg>(__arg), _M_value); + } +}; + +//! Like a polymorphic lambda for ==value +template <typename _Tp> +class __equal_value +{ + const _Tp& _M_value; + + public: + explicit __equal_value(const _Tp& __value) : _M_value(__value) {} + + template <typename _Arg> + bool + operator()(_Arg&& __arg) const + { + return std::forward<_Arg>(__arg) == _M_value; + } +}; + +//! Logical negation of ==value +template <typename _Tp> +class __not_equal_value +{ + const _Tp& _M_value; + + public: + explicit __not_equal_value(const _Tp& __value) : _M_value(__value) {} + + template <typename _Arg> + bool + operator()(_Arg&& __arg) const + { + return !(std::forward<_Arg>(__arg) == _M_value); + } +}; + +template <typename _ForwardIterator, typename _Compare> +_ForwardIterator +__cmp_iterators_by_values(_ForwardIterator __a, _ForwardIterator __b, _Compare __comp) +{ + if (__a < __b) + { // we should return closer iterator + return __comp(*__b, *__a) ? __b : __a; + } + else + { + return __comp(*__a, *__b) ? __a : __b; + } +} + +} // namespace __internal +} // namespace __pstl + +_PSTL_HIDE_FROM_ABI_POP + +#endif /* _PSTL_UTILS_H */ diff --git a/pstl/test/CMakeLists.txt b/pstl/test/CMakeLists.txt new file mode 100644 index 0000000..bc027c3 --- /dev/null +++ b/pstl/test/CMakeLists.txt @@ -0,0 +1,41 @@ +#===-- CMakeLists.txt ----------------------------------------------------===## +# +# 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 +# +#===----------------------------------------------------------------------===## + +# TODO(ldionne): This CMake testing infrastructure should be replaced with a +# llvm-lit test suite. + +add_custom_target(pstl-build-tests + COMMENT "Build all the pstl tests.") + +add_custom_target(check-pstl + COMMAND "${CMAKE_CTEST_COMMAND}" --output-on-failure + USES_TERMINAL + DEPENDS pstl-build-tests + COMMENT "Build and run all the unit tests.") + +add_library(test_stdlib INTERFACE) +target_include_directories(test_stdlib INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/support/stdlib") +target_link_libraries(test_stdlib INTERFACE pstl::ParallelSTL) +target_compile_options(test_stdlib INTERFACE -Wno-gnu-include-next) + +file(GLOB_RECURSE UNIT_TESTS "*.pass.cpp") +foreach(_file IN LISTS UNIT_TESTS) + file(RELATIVE_PATH _target "${CMAKE_CURRENT_SOURCE_DIR}" "${_file}") + string(REPLACE ".cpp" "" _target "${_target}") + string(REPLACE "/" "-" _target "${_target}") + set(_target "pstl-${_target}") + + add_executable(${_target} EXCLUDE_FROM_ALL "${_file}") + target_include_directories(${_target} PRIVATE "${CMAKE_CURRENT_LIST_DIR}") + target_compile_options(${_target} PRIVATE -Wno-unused-local-typedef -Wno-unused-variable) + target_link_libraries(${_target} PRIVATE test_stdlib) + set_target_properties(${_target} PROPERTIES CXX_EXTENSIONS NO + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") + add_test(${_target} "${CMAKE_CURRENT_BINARY_DIR}/${_target}") + add_dependencies(pstl-build-tests ${_target}) +endforeach() diff --git a/pstl/test/pstl/header_inclusion_order_algorithm_0.pass.cpp b/pstl/test/pstl/header_inclusion_order_algorithm_0.pass.cpp new file mode 100644 index 0000000..8cdeb18 --- /dev/null +++ b/pstl/test/pstl/header_inclusion_order_algorithm_0.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===-- header_inclusion_order_algorithm_0.pass.cpp -----------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +int +main() +{ + return 0; +} diff --git a/pstl/test/pstl/header_inclusion_order_algorithm_1.pass.cpp b/pstl/test/pstl/header_inclusion_order_algorithm_1.pass.cpp new file mode 100644 index 0000000..ea6c4f4 --- /dev/null +++ b/pstl/test/pstl/header_inclusion_order_algorithm_1.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===-- header_inclusion_order_algorithm_1.pass.cpp -----------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <algorithm> +#include <execution> + +int +main() +{ + return 0; +} diff --git a/pstl/test/pstl/header_inclusion_order_memory_0.pass.cpp b/pstl/test/pstl/header_inclusion_order_memory_0.pass.cpp new file mode 100644 index 0000000..dbb70c5 --- /dev/null +++ b/pstl/test/pstl/header_inclusion_order_memory_0.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===-- header_inclusion_order_memory_0.pass.cpp --------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <memory> + +int +main() +{ + return 0; +} diff --git a/pstl/test/pstl/header_inclusion_order_memory_1.pass.cpp b/pstl/test/pstl/header_inclusion_order_memory_1.pass.cpp new file mode 100644 index 0000000..99f059c --- /dev/null +++ b/pstl/test/pstl/header_inclusion_order_memory_1.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===-- header_inclusion_order_memory_1.pass.cpp --------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <memory> +#include <execution> + +int +main() +{ + return 0; +} diff --git a/pstl/test/pstl/header_inclusion_order_numeric_0.pass.cpp b/pstl/test/pstl/header_inclusion_order_numeric_0.pass.cpp new file mode 100644 index 0000000..4b18657 --- /dev/null +++ b/pstl/test/pstl/header_inclusion_order_numeric_0.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===-- header_inclusion_order_numeric_0.pass.cpp -------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <numeric> + +int +main() +{ + return 0; +} diff --git a/pstl/test/pstl/header_inclusion_order_numeric_1.pass.cpp b/pstl/test/pstl/header_inclusion_order_numeric_1.pass.cpp new file mode 100644 index 0000000..a0bb24e --- /dev/null +++ b/pstl/test/pstl/header_inclusion_order_numeric_1.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===-- header_inclusion_order_numeric_0.pass.cpp -------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <numeric> +#include <execution> + +int +main() +{ + return 0; +} diff --git a/pstl/test/pstl/version.pass.cpp b/pstl/test/pstl/version.pass.cpp new file mode 100644 index 0000000..820dabc --- /dev/null +++ b/pstl/test/pstl/version.pass.cpp @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include <pstl/internal/pstl_config.h> + +static_assert(_PSTL_VERSION == 17000); +static_assert(_PSTL_VERSION_MAJOR == 17); +static_assert(_PSTL_VERSION_MINOR == 00); +static_assert(_PSTL_VERSION_PATCH == 0); + +int +main() +{ + return 0; +} diff --git a/pstl/test/std/algorithms/alg.merge/inplace_merge.pass.cpp b/pstl/test/std/algorithms/alg.merge/inplace_merge.pass.cpp new file mode 100644 index 0000000..3446d95 --- /dev/null +++ b/pstl/test/std/algorithms/alg.merge/inplace_merge.pass.cpp @@ -0,0 +1,159 @@ +// -*- C++ -*- +//===-- inplace_merge.pass.cpp --------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_one_policy +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration + template <typename BiDirIt1, typename Size, typename Generator1, typename Generator2, typename Compare> + void + operator()(pstl::execution::unsequenced_policy, BiDirIt1 first1, BiDirIt1 last1, BiDirIt1 first2, BiDirIt1 last2, + Size n, Size m, Generator1 generator1, Generator2 generator2, Compare comp) + { + } + + template <typename BiDirIt1, typename Size, typename Generator1, typename Generator2, typename Compare> + void + operator()(pstl::execution::parallel_unsequenced_policy, BiDirIt1 first1, BiDirIt1 last1, BiDirIt1 first2, + BiDirIt1 last2, Size n, Size m, Generator1 generator1, Generator2 generator2, Compare comp) + { + } +#endif + + // inplace_merge works with bidirectional iterators at least + template <typename Policy, typename BiDirIt1, typename Size, typename Generator1, typename Generator2, + typename Compare> + typename std::enable_if<!is_same_iterator_category<BiDirIt1, std::forward_iterator_tag>::value, void>::type + operator()(Policy&& exec, BiDirIt1 first1, BiDirIt1 last1, BiDirIt1 first2, BiDirIt1 last2, Size n, Size m, + Generator1 generator1, Generator2 generator2, Compare comp) + { + const BiDirIt1 mid1 = std::next(first1, m); + fill_data(first1, mid1, generator1); + fill_data(mid1, last1, generator2); + + const BiDirIt1 mid2 = std::next(first2, m); + fill_data(first2, mid2, generator1); + fill_data(mid2, last2, generator2); + + std::inplace_merge(first1, mid1, last1, comp); + std::inplace_merge(exec, first2, mid2, last2, comp); + EXPECT_EQ_N(first1, first2, n, "wrong effect from inplace_merge with predicate"); + } + + template <typename Policy, typename BiDirIt1, typename Size, typename Generator1, typename Generator2, + typename Compare> + typename std::enable_if<is_same_iterator_category<BiDirIt1, std::forward_iterator_tag>::value, void>::type + operator()(Policy&&, BiDirIt1, BiDirIt1, BiDirIt1, BiDirIt1, Size, Size, Generator1, Generator2, Compare) + { + } +}; + +template <typename T, typename Generator1, typename Generator2, typename Compare> +void +test_by_type(Generator1 generator1, Generator2 generator2, Compare comp) +{ + using namespace std; + size_t max_size = 100000; + Sequence<T> in1(max_size, [](size_t v) { return T(v); }); + Sequence<T> exp(max_size, [](size_t v) { return T(v); }); + size_t m; + + for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + m = 0; + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n, exp.begin(), exp.begin() + n, n, m, + generator1, generator2, comp); + + m = n / 3; + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n, exp.begin(), exp.begin() + n, n, m, + generator1, generator2, comp); + + m = 2 * n / 3; + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n, exp.begin(), exp.begin() + n, n, m, + generator1, generator2, comp); + } +} + +template <typename T> +struct LocalWrapper +{ + explicit LocalWrapper(int32_t k) : my_val(k) {} + LocalWrapper(LocalWrapper&& input) { my_val = std::move(input.my_val); } + LocalWrapper& + operator=(LocalWrapper&& input) + { + my_val = std::move(input.my_val); + return *this; + } + bool + operator<(const LocalWrapper<T>& w) const + { + return my_val < w.my_val; + } + friend bool + operator==(const LocalWrapper<T>& x, const LocalWrapper<T>& y) + { + return x.my_val == y.my_val; + } + friend std::ostream& + operator<<(std::ostream& stream, const LocalWrapper<T>& input) + { + return stream << input.my_val; + } + + private: + T my_val; +}; + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + invoke_if(exec, [&]() { inplace_merge(exec, iter, iter, iter, non_const(std::less<T>())); }); + } +}; + +int +main() +{ + test_by_type<float64_t>([](int32_t i) { return -2 * i; }, [](int32_t i) { return -(2 * i + 1); }, + [](const float64_t x, const float64_t y) { return x > y; }); + + test_by_type<int32_t>([](int32_t i) { return 10 * i; }, [](int32_t i) { return i + 1; }, std::less<int32_t>()); + + test_by_type<LocalWrapper<float32_t>>([](int32_t i) { return LocalWrapper<float32_t>(2 * i + 1); }, + [](int32_t i) { return LocalWrapper<float32_t>(2 * i); }, + std::less<LocalWrapper<float32_t>>()); + + test_algo_basic_single<int32_t>(run_for_rnd_bi<test_non_const<int32_t>>()); + + test_by_type<MemoryChecker>( + [](std::size_t idx){ return MemoryChecker{std::int32_t(idx * 2)}; }, + [](std::size_t idx){ return MemoryChecker{std::int32_t(idx * 2 + 1)}; }, + [](const MemoryChecker& val1, const MemoryChecker& val2){ return val1.value() == val2.value(); }); + EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from inplace_merge: number of ctors calls < num of dtors calls"); + EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from inplace_merge: number of ctors calls > num of dtors calls"); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.merge/merge.pass.cpp b/pstl/test/std/algorithms/alg.merge/merge.pass.cpp new file mode 100644 index 0000000..a09ef42 --- /dev/null +++ b/pstl/test/std/algorithms/alg.merge/merge.pass.cpp @@ -0,0 +1,113 @@ +// -*- C++ -*- +//===-- merge.pass.cpp ----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> +#include <functional> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_merge +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename OutputIterator, + typename Compare> + void + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, + OutputIterator out_first, OutputIterator out_last, Compare comp) + { + using namespace std; + { + const auto res = merge(exec, first1, last1, first2, last2, out_first, comp); + EXPECT_TRUE(res == out_last, "wrong return result from merge with predicate"); + EXPECT_TRUE(is_sorted(out_first, res, comp), "wrong result from merge with predicate"); + EXPECT_TRUE(includes(out_first, res, first1, last1, comp), "first sequence is not a part of result"); + EXPECT_TRUE(includes(out_first, res, first2, last2, comp), "second sequence is not a part of result"); + } + { + const auto res = merge(exec, first1, last1, first2, last2, out_first); + EXPECT_TRUE(res == out_last, "wrong return result from merge"); + EXPECT_TRUE(is_sorted(out_first, res), "wrong result from merge"); + } + } + + // for reverse iterators + template <typename Policy, typename InputIterator1, typename InputIterator2, typename OutputIterator, + typename Compare> + void + operator()(Policy&& exec, std::reverse_iterator<InputIterator1> first1, std::reverse_iterator<InputIterator1> last1, + std::reverse_iterator<InputIterator2> first2, std::reverse_iterator<InputIterator2> last2, + std::reverse_iterator<OutputIterator> out_first, std::reverse_iterator<OutputIterator> out_last, Compare) + { + using namespace std; + typedef typename std::iterator_traits<std::reverse_iterator<InputIterator1>>::value_type T; + const auto res = merge(exec, first1, last1, first2, last2, out_first, std::greater<T>()); + + EXPECT_TRUE(res == out_last, "wrong return result from merge with predicate"); + EXPECT_TRUE(is_sorted(out_first, res, std::greater<T>()), "wrong result from merge with predicate"); + EXPECT_TRUE(includes(out_first, res, first1, last1, std::greater<T>()), + "first sequence is not a part of result"); + EXPECT_TRUE(includes(out_first, res, first2, last2, std::greater<T>()), + "second sequence is not a part of result"); + } +}; + +template <typename T, typename Generator1, typename Generator2> +void +test_merge_by_type(Generator1 generator1, Generator2 generator2) +{ + using namespace std; + size_t max_size = 100000; + Sequence<T> in1(max_size, generator1); + Sequence<T> in2(max_size / 2, generator2); + Sequence<T> out(in1.size() + in2.size()); + std::sort(in1.begin(), in1.end()); + std::sort(in2.begin(), in2.end()); + + for (size_t size = 0; size <= max_size; size = size <= 16 ? size + 1 : size_t(3.1415 * size)) + { + invoke_on_all_policies(test_merge(), in1.cbegin(), in1.cbegin() + size, in2.data(), in2.data() + size / 2, + out.begin(), out.begin() + 1.5 * size, std::less<T>()); + invoke_on_all_policies(test_merge(), in1.data(), in1.data() + size, in2.cbegin(), in2.cbegin() + size / 2, + out.begin(), out.begin() + 3 * size / 2, std::less<T>()); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputIterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputIterator out_iter) + { + merge(exec, input_iter, input_iter, input_iter, input_iter, out_iter, non_const(std::less<T>())); + } +}; + +int +main() +{ + test_merge_by_type<int32_t>([](size_t v) { return (v % 2 == 0 ? v : -v) * 3; }, [](size_t v) { return v * 2; }); + test_merge_by_type<float64_t>([](size_t v) { return float64_t(v); }, [](size_t v) { return float64_t(v - 100); }); + +#if !defined(_PSTL_ICC_16_17_TEST_64_TIMEOUT) + test_merge_by_type<Wrapper<int16_t>>([](size_t v) { return Wrapper<int16_t>(v % 100); }, + [](size_t v) { return Wrapper<int16_t>(v % 10); }); +#endif + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/alg.copy/copy_if.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/alg.copy/copy_if.pass.cpp new file mode 100644 index 0000000..692907e --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/alg.copy/copy_if.pass.cpp @@ -0,0 +1,147 @@ +// -*- C++ -*- +//===-- copy_if.pass.cpp --------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for copy_if and remove_copy_if +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct run_copy_if +{ +#if defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) // dummy specializations to skip testing in case of broken configuration + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Predicate, typename T> + void + operator()(pstl::execution::parallel_policy, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n, + Predicate pred, T trash) + { + } + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Predicate, typename T> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, + OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, + OutputIterator2 expected_last, Size n, Predicate pred, T trash) + { + } +#endif + + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Predicate, typename T> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, Predicate pred, + T trash) + { + // Cleaning + std::fill_n(expected_first, n, trash); + std::fill_n(out_first, n, trash); + + // Run copy_if + auto i = copy_if(first, last, expected_first, pred); + auto k = copy_if(exec, first, last, out_first, pred); + EXPECT_EQ_N(expected_first, out_first, n, "wrong copy_if effect"); + for (size_t j = 0; j < GuardSize; ++j) + { + ++k; + } + EXPECT_TRUE(out_last == k, "wrong return value from copy_if"); + + // Cleaning + std::fill_n(expected_first, n, trash); + std::fill_n(out_first, n, trash); + // Run remove_copy_if + i = remove_copy_if(first, last, expected_first, [=](const T& x) { return !pred(x); }); + k = remove_copy_if(exec, first, last, out_first, [=](const T& x) { return !pred(x); }); + EXPECT_EQ_N(expected_first, out_first, n, "wrong remove_copy_if effect"); + for (size_t j = 0; j < GuardSize; ++j) + { + ++k; + } + EXPECT_TRUE(out_last == k, "wrong return value from remove_copy_if"); + } +}; + +template <typename T, typename Predicate, typename Convert> +void +test(T trash, Predicate pred, Convert convert, bool check_weakness = true) +{ + // Try sequences of various lengths. + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + // count is number of output elements, plus a handful + // more for sake of detecting buffer overruns. + size_t count = GuardSize; + Sequence<T> in(n, [&](size_t k) -> T { + T val = convert(n ^ k); + count += pred(val) ? 1 : 0; + return val; + }); + + Sequence<T> out(count, [=](size_t) { return trash; }); + Sequence<T> expected(count, [=](size_t) { return trash; }); + if (check_weakness) + { + auto expected_result = copy_if(in.cfbegin(), in.cfend(), expected.begin(), pred); + size_t m = expected_result - expected.begin(); + EXPECT_TRUE(n / 4 <= m && m <= 3 * (n + 1) / 4, "weak test for copy_if"); + } + invoke_on_all_policies(run_copy_if(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), count, pred, trash); + invoke_on_all_policies(run_copy_if(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), + expected.end(), count, pred, trash); + } +} + +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even)); + + invoke_if(exec, [&]() { remove_copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even)); }); + } +}; + +int +main() +{ + test<float64_t>(-666.0, [](const float64_t& x) { return x * x <= 1024; }, + [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? float64_t(j % 32) : float64_t(j % 33 + 34); }); + + test<int32_t>(-666, [](const int32_t& x) { return x != 42; }, + [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? int32_t(j + 1) : 42; }); + +#if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN) + test<Number>(Number(42, OddTag()), IsMultiple(3, OddTag()), [](int32_t j) { return Number(j, OddTag()); }); +#endif + +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN) + test<int32_t>(-666, [](const int32_t&) { return true; }, [](size_t j) { return j; }, false); +#endif + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/is_partitioned.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/is_partitioned.pass.cpp new file mode 100644 index 0000000..bd6b259 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/is_partitioned.pass.cpp @@ -0,0 +1,101 @@ +// -*- C++ -*- +//===-- is_partitioned.pass.cpp -------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_one_policy +{ + //dummy specialization by policy type, in case of broken configuration +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) + + template <typename Iterator1, typename Predicate> + void + operator()(pstl::execution::unsequenced_policy, Iterator1 begin1, Iterator1 end1, Predicate pred) + { + } + template <typename Iterator1, typename Predicate> + void + operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 begin1, Iterator1 end1, Predicate pred) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator1, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Predicate pred) + { + const bool expected = std::is_partitioned(begin1, end1, pred); + const bool actual = std::is_partitioned(exec, begin1, end1, pred); + EXPECT_TRUE(actual == expected, "wrong return result from is_partitioned"); + } +}; + +template <typename T, typename Predicate> +void +test(Predicate pred) +{ + + const std::size_t max_n = 1000000; + Sequence<T> in(max_n, [](std::size_t k) { return T(k); }); + + for (std::size_t n1 = 0; n1 <= max_n; n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1)) + { + invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, pred); + std::partition(in.begin(), in.begin() + n1, pred); + invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, pred); + } +} + +template <typename T> +struct LocalWrapper +{ + explicit LocalWrapper(std::size_t k) : my_val(k) {} + + private: + T my_val; +}; + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + invoke_if(exec, [&]() { is_partitioned(exec, iter, iter, non_const(is_even)); }); + } +}; + +int +main() +{ + test<float64_t>([](const float64_t x) { return x < 0; }); + test<int32_t>([](const int32_t x) { return x > 1000; }); + test<uint16_t>([](const uint16_t x) { return x % 5 < 3; }); +#if !defined(_PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN) && !defined(_PSTL_ICC_19_TEST_IS_PARTITIONED_RELEASE_BROKEN) + test<LocalWrapper<float64_t>>([](const LocalWrapper<float64_t>&) { return true; }); +#endif + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp new file mode 100644 index 0000000..724f0ba --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp @@ -0,0 +1,178 @@ +// -*- C++ -*- +//===-- partition.pass.cpp ------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for stable_partition and partition +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> +#include <iterator> +#include <type_traits> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct DataType +{ + explicit DataType(int32_t k) : my_val(k) {} + DataType(DataType&& input) { my_val = std::move(input.my_val); } + DataType& + operator=(DataType&& input) + { + my_val = std::move(input.my_val); + return *this; + } + T + get_val() const + { + return my_val; + } + + friend std::ostream& + operator<<(std::ostream& stream, const DataType<T>& input) + { + return stream << input.my_val; + } + + private: + T my_val; +}; + +template <typename Iterator> +typename std::enable_if<std::is_trivial<typename std::iterator_traits<Iterator>::value_type>::value, bool>::type +is_equal(Iterator first, Iterator last, Iterator d_first) +{ + return std::equal(first, last, d_first); +} + +template <typename Iterator> +typename std::enable_if<!std::is_trivial<typename std::iterator_traits<Iterator>::value_type>::value, bool>::type + is_equal(Iterator, Iterator, Iterator) +{ + return true; +} + +struct test_one_policy +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specializations to skip testing in case of broken configuration + template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator> + void + operator()(pstl::execution::unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last, + Size n, UnaryOp unary_op, Generator generator) + { + } + + template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator> + void + operator()(pstl::execution::parallel_unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, + BiDirIt exp_last, Size n, UnaryOp unary_op, Generator generator) + { + } +#elif defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) //dummy specializations to skip testing in case of broken configuration + template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator> + void + operator()(pstl::execution::parallel_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last, + Size n, UnaryOp unary_op, Generator generator) + { + } + + template <typename BiDirIt, typename Size, typename UnaryOp, typename Generator> + void + operator()(pstl::execution::parallel_unsequenced_policy, BiDirIt first, BiDirIt last, BiDirIt exp_first, + BiDirIt exp_last, Size n, UnaryOp unary_op, Generator generator) + { + } +#endif + + template <typename Policy, typename BiDirIt, typename Size, typename UnaryOp, typename Generator> + typename std::enable_if<!is_same_iterator_category<BiDirIt, std::forward_iterator_tag>::value, void>::type + operator()(Policy&& exec, BiDirIt first, BiDirIt last, BiDirIt exp_first, BiDirIt exp_last, Size, UnaryOp unary_op, + Generator generator) + { + // partition + { + fill_data(first, last, generator); + BiDirIt actual_ret = std::partition(exec, first, last, unary_op); + EXPECT_TRUE(std::all_of(first, actual_ret, unary_op) && !std::any_of(actual_ret, last, unary_op), + "wrong effect from partition"); + } + // stable_partition + { + fill_data(exp_first, exp_last, generator); + BiDirIt exp_ret = std::stable_partition(exp_first, exp_last, unary_op); + fill_data(first, last, generator); + BiDirIt actual_ret = std::stable_partition(exec, first, last, unary_op); + + EXPECT_TRUE(std::distance(first, actual_ret) == std::distance(exp_first, exp_ret), + "wrong result from stable_partition"); + EXPECT_TRUE((is_equal<BiDirIt>(exp_first, exp_last, first)), "wrong effect from stable_partition"); + } + } + template <typename Policy, typename BiDirIt, typename Size, typename UnaryOp, typename Generator> + typename std::enable_if<is_same_iterator_category<BiDirIt, std::forward_iterator_tag>::value, void>::type + operator()(Policy&&, BiDirIt, BiDirIt, BiDirIt, BiDirIt, Size, UnaryOp, Generator) + { + } +}; + +template <typename T, typename Generator, typename UnaryPred> +void +test_by_type(Generator generator, UnaryPred pred) +{ + + using namespace std; + size_t max_size = 100000; + Sequence<T> in(max_size, [](size_t v) { return T(v); }); + Sequence<T> exp(max_size, [](size_t v) { return T(v); }); + + for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n, exp.begin(), exp.begin() + n, n, pred, + generator); + } +} + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + invoke_if(exec, [&]() { + partition(exec, iter, iter, non_const(is_even)); + stable_partition(exec, iter, iter, non_const(is_even)); + }); + } +}; + +int +main() +{ +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN) + test_by_type<int32_t>([](int32_t i) { return i; }, [](int32_t) { return true; }); +#endif + test_by_type<float64_t>([](int32_t i) { return -i; }, [](const float64_t x) { return x < 0; }); + test_by_type<int64_t>([](int32_t i) { return i + 1; }, [](int64_t x) { return x % 3 == 0; }); + test_by_type<DataType<float32_t>>([](int32_t i) { return DataType<float32_t>(2 * i + 1); }, + [](const DataType<float32_t>& x) { return x.get_val() < 0; }); + + test_algo_basic_single<int32_t>(run_for_rnd_bi<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp new file mode 100644 index 0000000..c621f83 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp @@ -0,0 +1,116 @@ +// -*- C++ -*- +//===-- partition_copy.pass.cpp -------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for stable_partition and partition_copy +#include "support/pstl_test_config.h" + +#include <algorithm> +#include <cstdlib> +#include <execution> +#include <functional> +#include <iterator> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_partition_copy +{ + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, + typename UnaryOp> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator true_first, OutputIterator, + OutputIterator2 false_first, OutputIterator2, UnaryOp unary_op) + { + + auto actual_ret = std::partition_copy(exec, first, last, true_first, false_first, unary_op); + + EXPECT_TRUE(std::distance(true_first, actual_ret.first) == std::count_if(first, last, unary_op), + "partition_copy has wrong effect from true sequence"); + EXPECT_TRUE(std::distance(false_first, actual_ret.second) == std::count_if(first, last, std::not_fn(unary_op)), + "partition_copy has wrong effect from false sequence"); + } + + //dummy specialization by iterator type and policy type, in case of broken configuration +#if defined(_PSTL_ICC_1800_TEST_MONOTONIC_RELEASE_64_BROKEN) + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename UnaryOp> + void + operator()(pstl::execution::unsequenced_policy, std::reverse_iterator<InputIterator> first, + std::reverse_iterator<InputIterator> last, std::reverse_iterator<OutputIterator> true_first, + std::reverse_iterator<OutputIterator> true_last, std::reverse_iterator<OutputIterator2> false_first, + OutputIterator2 false_last, UnaryOp unary_op) + { + } + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename UnaryOp> + void + operator()(pstl::execution::parallel_unsequenced_policy, std::reverse_iterator<InputIterator> first, + std::reverse_iterator<InputIterator> last, std::reverse_iterator<OutputIterator> true_first, + std::reverse_iterator<OutputIterator> true_last, std::reverse_iterator<OutputIterator2> false_first, + OutputIterator2 false_last, UnaryOp unary_op) + { + } +#endif +}; + +template <typename T, typename UnaryPred> +void +test(UnaryPred pred) +{ + + const std::size_t max_size = 100000; + Sequence<T> in(max_size, [](std::size_t v) -> T { return T(v); }); + Sequence<T> actual_true(max_size); + Sequence<T> actual_false(max_size); + for (std::size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : std::size_t(3.1415 * n)) + { + + // for non-const input iterators + invoke_on_all_policies(test_partition_copy(), in.begin(), in.begin() + n, actual_true.begin(), + actual_true.begin() + n, actual_false.begin(), actual_false.begin() + n, pred); + + // for const input iterators + invoke_on_all_policies(test_partition_copy(), in.cbegin(), in.cbegin() + n, actual_true.begin(), + actual_true.begin() + n, actual_false.begin(), actual_false.begin() + n, pred); + } +} + +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + + partition_copy(exec, input_iter, input_iter, out_iter, out_iter, non_const(is_even)); + } +}; + +int +main() +{ + test<int32_t>([](const int32_t value) { return value % 2; }); + +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN) + test<int32_t>([](const int32_t) { return true; }); +#endif + + test<float64_t>([](const float64_t value) { return value > 2 << 6; }); + test<Wrapper<float64_t>>([](const Wrapper<float64_t>& value) -> bool { return value.get_my_field() != nullptr; }); + + test_algo_basic_double<int32_t>(run_for_rnd_bi<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp new file mode 100644 index 0000000..126454f --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp @@ -0,0 +1,104 @@ +// -*- C++ -*- +//===-- reverse.pass.cpp --------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <iterator> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_one_policy +{ +#if defined(_PSTL_ICC_18_VC141_TEST_SIMD_LAMBDA_RELEASE_BROKEN) || defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration + template <typename Iterator1, typename Iterator2> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, + Iterator2 actual_e) + { + } + template <typename Iterator1, typename Iterator2> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, + Iterator2 actual_e) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> + typename std::enable_if<!is_same_iterator_category<Iterator1, std::forward_iterator_tag>::value>::type + operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e) + { + using namespace std; + + copy(data_b, data_e, actual_b); + + reverse(exec, actual_b, actual_e); + + bool check = equal(data_b, data_e, reverse_iterator<Iterator2>(actual_e)); + + EXPECT_TRUE(check, "wrong result of reverse"); + } + + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> + typename std::enable_if<is_same_iterator_category<Iterator1, std::forward_iterator_tag>::value>::type + operator()(ExecutionPolicy&&, Iterator1, Iterator1, Iterator2, Iterator2) + { + } +}; + +template <typename T> +void +test() +{ + const std::size_t max_len = 100000; + + Sequence<T> actual(max_len); + + Sequence<T> data(max_len, [](std::size_t i) { return T(i); }); + + for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) + { + invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), + actual.begin() + len); + } +} + +template <typename T> +struct wrapper +{ + T t; + wrapper() {} + explicit wrapper(T t_) : t(t_) {} + bool + operator==(const wrapper<T>& a) const + { + return t == a.t; + } +}; + +int +main() +{ + test<int32_t>(); + test<uint16_t>(); + test<float64_t>(); +#if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN) + test<wrapper<float64_t>>(); +#endif + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp new file mode 100644 index 0000000..5334438 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp @@ -0,0 +1,130 @@ +// -*- C++ -*- +//===-- reverse_copy.pass.cpp ---------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <iterator> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct wrapper +{ + T t; + wrapper() {} + explicit wrapper(T t_) : t(t_) {} + wrapper& + operator=(const T& t_) + { + t = t_; + return *this; + } + bool + operator==(const wrapper& t_) const + { + return t == t_.t; + } +}; + +template <typename T1, typename T2> +bool +eq(const wrapper<T1>& a, const wrapper<T2>& b) +{ + return a.t == b.t; +} + +template <typename T1, typename T2> +bool +eq(const T1& a, const T2& b) +{ + return a == b; +} + +// we need to save state here, because we need to test with different types of iterators +// due to the caller invoke_on_all_policies does forcing modification passed iterator type to cover additional usage cases. +template <typename Iterator> +struct test_one_policy +{ + Iterator data_b; + Iterator data_e; + test_one_policy(Iterator b, Iterator e) : data_b(b), data_e(e) {} + +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration + template <typename Iterator1> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::unsequenced_policy, Iterator1 actual_b, Iterator1 actual_e) + { + } + template <typename Iterator1> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 actual_b, Iterator1 actual_e) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator1> + void + operator()(ExecutionPolicy&& exec, Iterator1 actual_b, Iterator1 actual_e) + { + using namespace std; + using T = typename iterator_traits<Iterator1>::value_type; + + fill(actual_b, actual_e, T(-123)); + Iterator1 actual_return = reverse_copy(exec, data_b, data_e, actual_b); + + EXPECT_TRUE(actual_return == actual_e, "wrong result of reverse_copy"); + + const auto n = std::distance(data_b, data_e); + Sequence<T> res(n); + std::copy(std::reverse_iterator<Iterator>(data_e), std::reverse_iterator<Iterator>(data_b), res.begin()); + + EXPECT_EQ_N(res.begin(), actual_b, n, "wrong effect of reverse_copy"); + } +}; + +template <typename T1, typename T2> +void +test() +{ + typedef typename Sequence<T1>::iterator iterator_type; + typedef typename Sequence<T1>::const_bidirectional_iterator cbi_iterator_type; + + const std::size_t max_len = 100000; + + Sequence<T2> actual(max_len); + + Sequence<T1> data(max_len, [](std::size_t i) { return T1(i); }); + + for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) + { + invoke_on_all_policies(test_one_policy<iterator_type>(data.begin(), data.begin() + len), actual.begin(), + actual.begin() + len); + invoke_on_all_policies(test_one_policy<cbi_iterator_type>(data.cbibegin(), std::next(data.cbibegin(), len)), + actual.begin(), actual.begin() + len); + } +} + +int +main() +{ + test<int16_t, int8_t>(); + test<uint16_t, float32_t>(); + test<float64_t, int64_t>(); + test<wrapper<float64_t>, wrapper<float64_t>>(); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/copy_move.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/copy_move.pass.cpp new file mode 100644 index 0000000..24b7843 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/copy_move.pass.cpp @@ -0,0 +1,197 @@ +// -*- C++ -*- +//===-- copy_move.pass.cpp ------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for copy, move and copy_n +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct run_copy +{ + +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, typename T> + void + operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size size, + Size n, T trash) + { + } + + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, typename T> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, + OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, + OutputIterator2 expected_last, Size size, Size n, T trash) + { + } +#endif + + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename T> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size size, Size n, T trash) + { + // Cleaning + std::fill_n(expected_first, size, trash); + std::fill_n(out_first, size, trash); + + // Run copy + copy(first, last, expected_first); + auto k = copy(exec, first, last, out_first); + for (size_t j = 0; j < GuardSize; ++j) + ++k; + EXPECT_EQ_N(expected_first, out_first, size, "wrong effect from copy"); + EXPECT_TRUE(out_last == k, "wrong return value from copy"); + + // Cleaning + std::fill_n(out_first, size, trash); + // Run copy_n + k = copy_n(exec, first, n, out_first); + for (size_t j = 0; j < GuardSize; ++j) + ++k; + EXPECT_EQ_N(expected_first, out_first, size, "wrong effect from copy_n"); + EXPECT_TRUE(out_last == k, "wrong return value from copy_n"); + } +}; + +template <typename T> +struct run_move +{ + +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> + void + operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size size, + Size n, T trash) + { + } + + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, + OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, + OutputIterator2 expected_last, Size size, Size n, T trash) + { + } +#endif + + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size size, Size, T trash) + { + // Cleaning + std::fill_n(expected_first, size, trash); + std::fill_n(out_first, size, trash); + + // Run move + move(first, last, expected_first); + auto k = move(exec, first, last, out_first); + for (size_t j = 0; j < GuardSize; ++j) + ++k; + EXPECT_EQ_N(expected_first, out_first, size, "wrong effect from move"); + EXPECT_TRUE(out_last == k, "wrong return value from move"); + } +}; + +template <typename T> +struct run_move<Wrapper<T>> +{ + +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> + void + operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size size, + Size n, Wrapper<T> trash) + { + } + + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, + OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, + OutputIterator2 expected_last, Size size, Size n, Wrapper<T> trash) + { + } +#endif + + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2, OutputIterator2, Size size, Size, Wrapper<T> trash) + { + // Cleaning + std::fill_n(out_first, size, trash); + Wrapper<T>::SetMoveCount(0); + + // Run move + auto k = move(exec, first, last, out_first); + for (size_t j = 0; j < GuardSize; ++j) + ++k; + EXPECT_TRUE(Wrapper<T>::MoveCount() == size, "wrong effect from move"); + EXPECT_TRUE(out_last == k, "wrong return value from move"); + } +}; + +template <typename T, typename Convert> +void +test(T trash, Convert convert) +{ + // Try sequences of various lengths. + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + // count is number of output elements, plus a handful + // more for sake of detecting buffer overruns. + Sequence<T> in(n, [&](size_t k) -> T { + T val = convert(n ^ k); + return val; + }); + + const size_t outN = n + GuardSize; + Sequence<T> out(outN, [=](size_t) { return trash; }); + Sequence<T> expected(outN, [=](size_t) { return trash; }); + invoke_on_all_policies(run_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), outN, n, trash); + invoke_on_all_policies(run_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), + expected.end(), outN, n, trash); + invoke_on_all_policies(run_move<T>(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), n, n, trash); + + // For this test const iterator isn't suitable + // because const rvalue-reference call copy assignment operator + } +} + +int +main() +{ + test<int32_t>(-666, [](size_t j) { return int32_t(j); }); + test<Wrapper<float64_t>>(Wrapper<float64_t>(-666.0), [](int32_t j) { return Wrapper<float64_t>(j); }); + +#if !defined(_PSTL_ICC_16_17_TEST_64_TIMEOUT) + test<float64_t>(-666.0, [](size_t j) { return float64_t(j); }); + test<Number>(Number(42, OddTag()), [](int32_t j) { return Number(j, OddTag()); }); +#endif + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/fill.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/fill.pass.cpp new file mode 100644 index 0000000..d44a1a4 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/fill.pass.cpp @@ -0,0 +1,100 @@ +// -*- C++ -*- +//===-- fill.pass.cpp -----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_fill +{ + template <typename It, typename T> + bool + check(It first, It last, const T& value) + { + for (; first != last; ++first) + if (*first != value) + return false; + return true; + } + + template <typename Policy, typename Iterator, typename T> + void + operator()(Policy&& exec, Iterator first, Iterator last, const T& value) + { + fill(first, last, T(value + 1)); // initialize memory with different value + + fill(exec, first, last, value); + EXPECT_TRUE(check(first, last, value), "fill wrong result"); + } +}; + +struct test_fill_n +{ + template <typename It, typename Size, typename T> + bool + check(It first, Size n, const T& value) + { + for (Size i = 0; i < n; ++i, ++first) + if (*first != value) + return false; + return true; + } + + template <typename Policy, typename Iterator, typename Size, typename T> + void + operator()(Policy&& exec, Iterator first, Size n, const T& value) + { + fill_n(first, n, T(value + 1)); // initialize memory with different value + + const Iterator one_past_last = fill_n(exec, first, n, value); + const Iterator expected_return = std::next(first, n); + + EXPECT_TRUE(expected_return == one_past_last, "fill_n should return Iterator to one past the element assigned"); + EXPECT_TRUE(check(first, n, value), "fill_n wrong result"); + + //n == -1 + const Iterator res = fill_n(exec, first, -1, value); + EXPECT_TRUE(res == first, "fill_n wrong result for n == -1"); + } +}; + +template <typename T> +void +test_fill_by_type(std::size_t n) +{ + Sequence<T> in(n, [](std::size_t) -> T { return T(0); }); //fill with zeros + T value = -1; + + invoke_on_all_policies(test_fill(), in.begin(), in.end(), value); + invoke_on_all_policies(test_fill_n(), in.begin(), n, value); +} + +int +main() +{ + + const std::size_t N = 100000; + + for (std::size_t n = 0; n < N; n = n < 16 ? n + 1 : size_t(3.1415 * n)) + { + test_fill_by_type<int32_t>(n); + test_fill_by_type<float64_t>(n); + } + + std::cout << done() << std::endl; + + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/generate.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/generate.pass.cpp new file mode 100644 index 0000000..92d87ea --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/generate.pass.cpp @@ -0,0 +1,104 @@ +// -*- C++ -*- +//===-- generate.pass.cpp -------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <atomic> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct Generator_count +{ + const T def_val = T(-1); + T + operator()() + { + return def_val; + } + T + default_value() const + { + return def_val; + } +}; + +struct test_generate +{ + template <typename Policy, typename Iterator, typename Size> + void + operator()(Policy&& exec, Iterator first, Iterator last, Size n) + { + using namespace std; + typedef typename std::iterator_traits<Iterator>::value_type T; + + // Try random-access iterator + { + Generator_count<T> g; + generate(exec, first, last, g); + Size count = std::count(first, last, g.default_value()); + EXPECT_TRUE(count == n, "generate wrong result for generate"); + std::fill(first, last, T(0)); + } + + { + Generator_count<T> g; + const auto m = n / 2; + auto actual_last = generate_n(exec, first, m, g); + Size count = std::count(first, actual_last, g.default_value()); + EXPECT_TRUE(count == m && actual_last == std::next(first, m), "generate_n wrong result for generate_n"); + std::fill(first, actual_last, T(0)); + } + } +}; + +template <typename T> +void +test_generate_by_type() +{ + for (size_t n = 0; n <= 100000; n = n < 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> in(n, [](size_t) -> T { return T(0); }); //fill by zero + + invoke_on_all_policies(test_generate(), in.begin(), in.end(), in.size()); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto gen = []() { return T(0); }; + + generate(exec, iter, iter, non_const(gen)); + generate_n(exec, iter, 0, non_const(gen)); + } +}; + +int +main() +{ + + test_generate_by_type<int32_t>(); + test_generate_by_type<float64_t>(); + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/remove.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/remove.pass.cpp new file mode 100644 index 0000000..872b0d2 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/remove.pass.cpp @@ -0,0 +1,161 @@ +// -*- C++ -*- +//===-- remove.pass.cpp ---------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Test for remove, remove_if +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct run_remove +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename InputIterator, typename OutputIterator, typename Size, typename T> + void + operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size n, + const T& value) + { + } + template <typename InputIterator, typename OutputIterator, typename Size, typename T> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, + OutputIterator out_first, OutputIterator out_last, OutputIterator expected_first, + OutputIterator expected_last, Size n, const T& value) + { + } +#endif + + template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename T> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size, + const T& value) + { + // Cleaning + std::copy(first, last, expected_first); + std::copy(first, last, out_first); + + // Run remove + OutputIterator i = remove(expected_first, expected_last, value); + OutputIterator k = remove(exec, out_first, out_last, value); + EXPECT_TRUE(std::distance(expected_first, i) == std::distance(out_first, k), "wrong return value from remove"); + EXPECT_EQ_N(expected_first, out_first, std::distance(expected_first, i), "wrong remove effect"); + } +}; + +struct run_remove_if +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename InputIterator, typename OutputIterator, typename Size, typename Predicate> + void + operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size n, + Predicate pred) + { + } + template <typename InputIterator, typename OutputIterator, typename Size, typename Predicate> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, + OutputIterator out_first, OutputIterator out_last, OutputIterator expected_first, + OutputIterator expected_last, Size n, Predicate pred) + { + } +#endif + + template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename Predicate> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator expected_first, OutputIterator expected_last, Size, + Predicate pred) + { + // Cleaning + std::copy(first, last, expected_first); + std::copy(first, last, out_first); + + // Run remove_if + OutputIterator i = remove_if(expected_first, expected_last, pred); + OutputIterator k = remove_if(exec, out_first, out_last, pred); + EXPECT_TRUE(std::distance(expected_first, i) == std::distance(out_first, k), + "wrong return value from remove_if"); + EXPECT_EQ_N(expected_first, out_first, std::distance(expected_first, i), "wrong remove_if effect"); + } +}; + +template <typename T, typename Predicate, typename Convert> +void +test(T trash, const T& value, Predicate pred, Convert convert) +{ + const std::size_t max_size = 100000; + Sequence<T> out(max_size, [trash](size_t) { return trash; }); + Sequence<T> expected(max_size, [trash](size_t) { return trash; }); + + for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> data(n, [&](size_t k) -> T { return convert(k); }); + + invoke_on_all_policies(run_remove(), data.begin(), data.end(), out.begin(), out.begin() + n, expected.begin(), + expected.begin() + n, n, value); + invoke_on_all_policies(run_remove_if(), data.begin(), data.end(), out.begin(), out.begin() + n, + expected.begin(), expected.begin() + n, n, pred); + } +} + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + + invoke_if(exec, [&]() { remove_if(exec, iter, iter, non_const(is_even)); }); + } +}; + +int +main() +{ +#if !defined(_PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN) + test<int32_t>(666, 42, [](int32_t) { return true; }, [](size_t j) { return j; }); +#endif + + test<int32_t>(666, 2001, [](const int32_t& val) { return val != 2001; }, + [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 2001 : -1 - int32_t(j); }); + test<float64_t>(-666.0, 8.5, [](const float64_t& val) { return val != 8.5; }, + [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); }); + +#if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN) + test<Number>(Number(-666, OddTag()), Number(42, OddTag()), IsMultiple(3, OddTag()), + [](int32_t j) { return Number(j, OddTag()); }); +#endif + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); + + test<MemoryChecker>(MemoryChecker{0}, MemoryChecker{1}, + [](const MemoryChecker& val){ return val.value() == 1; }, + [](std::size_t idx){ return MemoryChecker{std::int32_t(idx % 3 == 0)}; } + ); + EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from remove,remove_if: number of ctors calls < num of dtors calls"); + EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from remove,remove_if: number of ctors calls > num of dtors calls"); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/remove_copy.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/remove_copy.pass.cpp new file mode 100644 index 0000000..9cdd7d6 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/remove_copy.pass.cpp @@ -0,0 +1,91 @@ +// -*- C++ -*- +//===-- remove_copy.pass.cpp ----------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct run_remove_copy +{ + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename T> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, const T& value, + T trash) + { + // Cleaning + std::fill_n(expected_first, n, trash); + std::fill_n(out_first, n, trash); + + // Run copy_if + auto i = std::remove_copy(first, last, expected_first, value); + (void)i; + auto k = std::remove_copy(exec, first, last, out_first, value); + EXPECT_EQ_N(expected_first, out_first, n, "wrong remove_copy effect"); + for (size_t j = 0; j < GuardSize; ++j) + { + ++k; + } + EXPECT_TRUE(out_last == k, "wrong return value from remove_copy"); + } +}; + +template <typename T, typename Convert> +void +test(T trash, const T& value, Convert convert, bool check_weakness = true) +{ + // Try sequences of various lengths. + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + // count is number of output elements, plus a handful + // more for sake of detecting buffer overruns. + size_t count = GuardSize; + Sequence<T> in(n, [&](size_t k) -> T { + T x = convert(n ^ k); + count += !(x == value) ? 1 : 0; + return x; + }); + using namespace std; + + Sequence<T> out(count, [=](size_t) { return trash; }); + Sequence<T> expected(count, [=](size_t) { return trash; }); + if (check_weakness) + { + auto expected_result = remove_copy(in.cfbegin(), in.cfend(), expected.begin(), value); + size_t m = expected_result - expected.begin(); + EXPECT_TRUE(n / 4 <= m && m <= 3 * (n + 1) / 4, "weak test for remove_copy"); + } + invoke_on_all_policies(run_remove_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), count, value, trash); + invoke_on_all_policies(run_remove_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), + expected.end(), count, value, trash); + } +} + +int +main() +{ + + test<float64_t>(-666.0, 8.5, [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); }); + + test<int32_t>(-666, 42, [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 42 : -1 - int32_t(j); }); + + test<Number>(Number(42, OddTag()), Number(2001, OddTag()), + [](int32_t j) { return ((j + 1) % 3 & 2) != 0 ? Number(2001, OddTag()) : Number(j, OddTag()); }); + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/replace.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/replace.pass.cpp new file mode 100644 index 0000000..9c6ca1ea --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/replace.pass.cpp @@ -0,0 +1,160 @@ +// -*- C++ -*- +//===-- replace.pass.cpp --------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +// This class is needed to check the self-copying +struct copy_int +{ + int32_t value; + int32_t copied_times = 0; + constexpr explicit copy_int(int32_t val = 0) : value(val) {} + constexpr copy_int(copy_int const& other) : value(other.value), copied_times(other.copied_times) { } + + constexpr copy_int& + operator=(const copy_int& other) + { + if (&other == this) + copied_times++; + else + { + value = other.value; + copied_times = other.copied_times; + } + return *this; + } + + constexpr bool + operator==(const copy_int& other) const + { + return (value == other.value); + } +}; + +template <typename Iterator> +struct test_one_policy +{ + std::size_t len; + Iterator data_b; + Iterator data_e; + test_one_policy(Iterator data_, std::size_t len_) + { + len = len_; + data_b = data_; + data_e = std::next(data_b, len); + } + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename T, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator1 expected_b, Iterator1 expected_e, Iterator2 actual_b, + Iterator2 actual_e, Predicate pred, const T& value, const T& old_value) + { + using namespace std; + + copy(data_b, data_e, expected_b); + copy(data_b, data_e, actual_b); + + replace(expected_b, expected_e, old_value, value); + replace(exec, actual_b, actual_e, old_value, value); + + EXPECT_TRUE((check<T, Iterator2>(actual_b, actual_e)), "wrong result of self assignment check"); + EXPECT_TRUE(equal(expected_b, expected_e, actual_b), "wrong result of replace"); + + copy(data_b, data_e, expected_b); + copy(data_b, data_e, actual_b); + + replace_if(expected_b, expected_e, pred, value); + replace_if(exec, actual_b, actual_e, pred, value); + EXPECT_TRUE(equal(expected_b, expected_e, actual_b), "wrong result of replace_if"); + } + + template <typename T, typename Iterator1> + bool check(Iterator1, Iterator1) + { + return true; + } + + template <typename T, typename Iterator1> + typename std::enable_if<std::is_same<T, copy_int>::value, bool>::type_t + check(Iterator1 b, Iterator1 e) + { + return std::all_of(b, e, [](const copy_int& elem) { return elem.copied_times == 0; }); + } +}; + +template <typename T1, typename T2, typename Pred> +void +test(Pred pred) +{ + typedef typename Sequence<T2>::iterator iterator_type; + + const std::size_t max_len = 100000; + + static constexpr T1 value = T1(0); + static constexpr T1 new_value = T1(666); + + Sequence<T2> expected(max_len); + Sequence<T2> actual(max_len); + + Sequence<T2> data(max_len, [](std::size_t i) { + if (i % 3 == 2) + { + return T1(i); + } + else + { + return value; + } + }); + + for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) + { + test_one_policy<iterator_type> temp(data.begin(), len); + + invoke_on_all_policies(temp, expected.begin(), expected.begin() + len, actual.begin(), actual.begin() + len, + pred, new_value, value); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + invoke_if(exec, [&]() { replace_if(exec, iter, iter, non_const(is_even), T(0)); }); + } +}; + +int +main() +{ + test<int32_t, float32_t>(__pstl::__internal::__equal_value<int32_t>(666)); + test<uint16_t, uint8_t>([](const uint16_t& elem) { return elem % 3 < 2; }); + test<float64_t, int64_t>([](const float64_t& elem) { return elem * elem - 3.5 * elem > 10; }); + test<copy_int, copy_int>([](const copy_int& val) { return val.value / 5 > 2; }); + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/replace_copy.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/replace_copy.pass.cpp new file mode 100644 index 0000000..94d725f --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/replace_copy.pass.cpp @@ -0,0 +1,105 @@ +// -*- C++ -*- +//===-- replace_copy.pass.cpp ---------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for replace_copy and replace_copy_if + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_replace_copy +{ + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Predicate, typename T> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, Predicate pred, + const T& old_value, const T& new_value, T trash) + { + // Cleaning + std::fill_n(expected_first, n, trash); + std::fill_n(out_first, n, trash); + // Run replace_copy + auto i = std::replace_copy(first, last, expected_first, old_value, new_value); + auto k = std::replace_copy(exec, first, last, out_first, old_value, new_value); + EXPECT_EQ_N(expected_first, out_first, n, "wrong replace_copy effect"); + EXPECT_TRUE(out_last == k, "wrong return value from replace_copy"); + + // Cleaning + std::fill_n(expected_first, n, trash); + std::fill_n(out_first, n, trash); + // Run replace_copy_if + i = replace_copy_if(first, last, expected_first, pred, new_value); + k = replace_copy_if(exec, first, last, out_first, pred, new_value); + EXPECT_EQ_N(expected_first, out_first, n, "wrong replace_copy_if effect"); + EXPECT_TRUE(out_last == k, "wrong return value from replace_copy_if"); + } +}; + +template <typename T, typename Convert, typename Predicate> +void +test(T trash, const T& old_value, const T& new_value, Predicate pred, Convert convert) +{ + // Try sequences of various lengths. + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> in(n, [&](size_t k) -> T { return convert(n ^ k); }); + Sequence<T> out(n, [=](size_t) { return trash; }); + Sequence<T> expected(n, [=](size_t) { return trash; }); + + invoke_on_all_policies(test_replace_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), out.size(), pred, old_value, new_value, trash); + invoke_on_all_policies(test_replace_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), + expected.end(), out.size(), pred, old_value, new_value, trash); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + + invoke_if(exec, [&]() { replace_copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even), T(0)); }); + } +}; + +int +main() +{ + + test<float64_t>(-666.0, 8.5, 0.33, [](const float64_t& x) { return x * x <= 1024; }, + [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); }); + + test<int32_t>(-666, 42, 99, [](const int32_t& x) { return x != 42; }, + [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 42 : -1 - int32_t(j); }); + +#if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN) + test<Number>(Number(42, OddTag()), Number(2001, OddTag()), Number(2017, OddTag()), IsMultiple(3, OddTag()), + [](int32_t j) { return ((j + 1) % 3 & 2) != 0 ? Number(2001, OddTag()) : Number(j, OddTag()); }); +#endif + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/rotate.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/rotate.pass.cpp new file mode 100644 index 0000000..0d1cfeb --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/rotate.pass.cpp @@ -0,0 +1,176 @@ +// -*- C++ -*- +//===-- rotate.pass.cpp ---------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <iterator> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct wrapper +{ + T t; + int move_count; + explicit wrapper(T t_) : t(t_), move_count(0) {} + wrapper& + operator=(const T& t_) + { + t = t_; + return *this; + } + + wrapper(const wrapper<T>& a) : move_count(0) { t = a.t; } + + wrapper<T>& + operator=(wrapper<T>& a) + { + t = a.t; + return *this; + } + + wrapper<T>& + operator=(wrapper<T>&& a) + { + t = a.t; + move_count += 1; + return *this; + } +}; + +template <typename T> +struct compare +{ + bool + operator()(const T& a, const T& b) + { + return a == b; + } +}; + +template <typename T> +struct compare<wrapper<T>> +{ + bool + operator()(const wrapper<T>& a, const wrapper<T>& b) + { + return a.t == b.t; + } +}; +#include <typeinfo> + +struct test_one_policy +{ + +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specializations to skip testing in case of broken configuration + template <typename Iterator, typename Size> + void + operator()(pstl::execution::unsequenced_policy, Iterator data_b, Iterator data_e, Iterator actual_b, + Iterator actual_e, Size shift) + { + } + template <typename Iterator, typename Size> + void + operator()(pstl::execution::parallel_unsequenced_policy, Iterator data_b, Iterator data_e, Iterator actual_b, + Iterator actual_e, Size shift) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator, typename Size> + void + operator()(ExecutionPolicy&& exec, Iterator data_b, Iterator data_e, Iterator actual_b, Iterator actual_e, + Size shift) + { + using namespace std; + using T = typename iterator_traits<Iterator>::value_type; + Iterator actual_m = std::next(actual_b, shift); + + copy(data_b, data_e, actual_b); + Iterator actual_return = rotate(exec, actual_b, actual_m, actual_e); + + EXPECT_TRUE(actual_return == std::next(actual_b, std::distance(actual_m, actual_e)), "wrong result of rotate"); + auto comparator = compare<T>(); + bool check = std::equal(actual_return, actual_e, data_b, comparator); + check = check && std::equal(actual_b, actual_return, std::next(data_b, shift), comparator); + + EXPECT_TRUE(check, "wrong effect of rotate"); + EXPECT_TRUE(check_move(exec, actual_b, actual_e, shift), "wrong move test of rotate"); + } + + template <typename ExecutionPolicy, typename Iterator, typename Size> + typename std::enable_if< + is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value && + !std::is_same<ExecutionPolicy, std::execution::sequenced_policy>::value && + std::is_same<typename std::iterator_traits<Iterator>::value_type, wrapper<float32_t>>::value, + bool>::type + check_move(ExecutionPolicy&&, Iterator b, Iterator e, Size shift) + { + bool result = all_of(b, e, [](wrapper<float32_t>& a) { + bool temp = a.move_count > 0; + a.move_count = 0; + return temp; + }); + return shift == 0 || result; + } + + template <typename ExecutionPolicy, typename Iterator, typename Size> + typename std::enable_if< + !(is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value && + !std::is_same<ExecutionPolicy, std::execution::sequenced_policy>::value && + std::is_same<typename std::iterator_traits<Iterator>::value_type, wrapper<float32_t>>::value), + bool>::type + check_move(ExecutionPolicy&&, Iterator, Iterator, Size) + { + return true; + } +}; + +template <typename T> +void +test() +{ + const int32_t max_len = 100000; + + Sequence<T> actual(max_len, [](std::size_t i) { return T(i); }); + Sequence<T> data(max_len, [](std::size_t i) { return T(i); }); + + for (int32_t len = 0; len < max_len; len = len <= 16 ? len + 1 : int32_t(3.1415 * len)) + { + int32_t shifts[] = {0, 1, 2, len / 3, (2 * len) / 3, len - 1}; + for (auto shift : shifts) + { + if (shift >= 0 && shift < len) + { + invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), + actual.begin() + len, shift); + } + } + } +} + +int +main() +{ + test<int32_t>(); + test<wrapper<float64_t>>(); + test<MemoryChecker>(); + EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from rotate: number of ctors calls < num of dtors calls"); + EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from rotate: number of ctors calls > num of dtors calls"); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/rotate_copy.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/rotate_copy.pass.cpp new file mode 100644 index 0000000..539cefc --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/rotate_copy.pass.cpp @@ -0,0 +1,146 @@ +// -*- C++ -*- +//===-- rotate_copy.pass.cpp ----------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <iterator> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct wrapper; + +template <typename T> +bool +compare(const wrapper<T>& a, const wrapper<T>& b) +{ + return a.t == b.t; +} + +template <typename T> +bool +compare(const T& a, const T& b) +{ + return a == b; +} + +template <typename T> +struct wrapper +{ + explicit wrapper(T t_) : t(t_) {} + wrapper& + operator=(const T& t_) + { + t = t_; + return *this; + } + friend bool + compare<T>(const wrapper<T>& a, const wrapper<T>& b); + + private: + T t; +}; + +template <typename T, typename It1, typename It2> +struct comparator +{ + using T1 = typename std::iterator_traits<It1>::value_type; + using T2 = typename std::iterator_traits<It2>::value_type; + bool + operator()(T1 a, T2 b) + { + T temp = a; + return compare(temp, b); + } +}; + +struct test_one_policy +{ + +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration + template <typename Iterator1, typename Iterator2> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, + Iterator2 actual_e, std::size_t shift) + { + } + template <typename Iterator1, typename Iterator2> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, + Iterator2 actual_e, std::size_t shift) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> + void + operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e, + std::size_t shift) + { + using namespace std; + using T = typename iterator_traits<Iterator2>::value_type; + Iterator1 data_m = std::next(data_b, shift); + + fill(actual_b, actual_e, T(-123)); + Iterator2 actual_return = rotate_copy(exec, data_b, data_m, data_e, actual_b); + + EXPECT_TRUE(actual_return == actual_e, "wrong result of rotate_copy"); + auto comparer = comparator<T, Iterator1, Iterator2>(); + bool check = std::equal(data_m, data_e, actual_b, comparer); + check = check && std::equal(data_b, data_m, std::next(actual_b, std::distance(data_m, data_e)), comparer); + + EXPECT_TRUE(check, "wrong effect of rotate_copy"); + } +}; + +template <typename T1, typename T2> +void +test() +{ + + const std::size_t max_len = 100000; + + Sequence<T2> actual(max_len, [](std::size_t i) { return T1(i); }); + + Sequence<T1> data(max_len, [](std::size_t i) { return T1(i); }); + + for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) + { + std::size_t shifts[] = {0, 1, 2, len / 3, (2 * len) / 3, len - 1}; + for (std::size_t shift : shifts) + { + if (shift > 0 && shift < len) + { + invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), + actual.begin() + len, shift); + invoke_on_all_policies(test_one_policy(), data.cbegin(), data.cbegin() + len, actual.begin(), + actual.begin() + len, shift); + } + } + } +} + +int +main() +{ + test<int32_t, int8_t>(); + test<uint16_t, float32_t>(); + test<float64_t, int64_t>(); + test<wrapper<float64_t>, wrapper<float64_t>>(); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/swap_ranges.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/swap_ranges.pass.cpp new file mode 100644 index 0000000..c3c3693 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/swap_ranges.pass.cpp @@ -0,0 +1,133 @@ +// -*- C++ -*- +//===-- swap_ranges.pass.cpp ----------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <iterator> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct wrapper +{ + T t; + std::size_t number_of_swaps = 0; + wrapper() {} + explicit wrapper(T t_) : t(t_) {} + template <typename U> + void + operator=(const U& b) + { + t = b; + } + bool + operator==(const wrapper<T>& a) const + { + return t == a.t; + } +}; + +template <typename T> +void +swap(wrapper<T>& a, wrapper<T>& b) +{ + std::swap(a.t, b.t); + a.number_of_swaps++; + b.number_of_swaps++; +} + +template <typename T> +struct check_swap +{ + bool + operator()(T&) + { + return true; + } +}; + +template <typename T> +struct check_swap<wrapper<T>> +{ + bool + operator()(wrapper<T>& a) + { + bool temp = (a.number_of_swaps == 1); + a.number_of_swaps = 0; + return temp; + } +}; + +struct test_one_policy +{ + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> + void + operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e) + { + using namespace std; + using T_ref = typename iterator_traits<Iterator1>::reference; + using T = typename iterator_traits<Iterator1>::value_type; + + iota(data_b, data_e, 0); + iota(actual_b, actual_e, std::distance(data_b, data_e)); + + Iterator2 actual_return = swap_ranges(exec, data_b, data_e, actual_b); + bool check_return = (actual_return == actual_e); + EXPECT_TRUE(check_return, "wrong result of swap_ranges"); + if (check_return) + { + std::size_t i = 0; + bool check = all_of(actual_b, actual_e, [&i](T_ref a) { return a == T(i++); }) && + all_of(data_b, data_e, [&i](T_ref a) { return a == T(i++); }); + + EXPECT_TRUE(check, "wrong effect of swap_ranges"); + + if (check) + { + bool swap_check = + all_of(data_b, data_e, check_swap<T>()) && all_of(actual_b, actual_e, check_swap<T>()); + EXPECT_TRUE(swap_check, "wrong effect of swap_ranges swap check"); + } + } + } +}; + +template <typename T> +void +test() +{ + const std::size_t max_len = 100000; + + Sequence<T> data(max_len); + Sequence<T> actual(max_len); + + for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) + { + invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), + actual.begin() + len); + } +} + +int +main() +{ + test<wrapper<uint16_t>>(); + test<wrapper<float64_t>>(); + test<int32_t>(); + test<float32_t>(); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp new file mode 100644 index 0000000..949e6f5 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp @@ -0,0 +1,122 @@ +// -*- C++ -*- +//===-- transform_binary.pass.cpp -----------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename In1, typename In2, typename Out> +class TheOperation +{ + Out val; + + public: + TheOperation(Out v) : val(v) {} + Out + operator()(const In1& x, const In2& y) const + { + return Out(val + x - y); + } +}; + +template <typename InputIterator1, typename InputIterator2, typename OutputIterator> +void +check_and_reset(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator out_first) +{ + typedef typename std::iterator_traits<OutputIterator>::value_type Out; + typename std::iterator_traits<OutputIterator>::difference_type k = 0; + for (; first1 != last1; ++first1, ++first2, ++out_first, ++k) + { + // check + Out expected = Out(1.5) + *first1 - *first2; + Out actual = *out_first; + if (std::is_floating_point<Out>::value) + { + EXPECT_TRUE((expected > actual ? expected - actual : actual - expected) < 1e7, + "wrong value in output sequence"); + } + else + { + EXPECT_EQ(expected, actual, "wrong value in output sequence"); + } + // reset + *out_first = k % 7 != 4 ? 7 * k - 5 : 0; + } +} + +struct test_one_policy +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename OutputIterator, + typename BinaryOp> + void + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2, + OutputIterator out_first, OutputIterator, BinaryOp op) + { + auto result = std::transform(exec, first1, last1, first2, out_first, op); + (void)result; + check_and_reset(first1, last1, first2, out_first); + } +}; + +template <typename In1, typename In2, typename Out, typename Predicate> +void +test(Predicate pred) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<In1> in1(n, [](size_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; }); + Sequence<In2> in2(n, [](size_t k) { return k % 7 != 2 ? 5 * k - 5 : 0; }); + + Sequence<Out> out(n, [](size_t) { return -1; }); + + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.begin(), in2.end(), out.begin(), + out.end(), pred); + invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cend(), in2.cbegin(), in2.cend(), out.begin(), + out.end(), pred); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + invoke_if(exec, [&]() { + InputIterator input_iter2 = input_iter; + transform(exec, input_iter, input_iter, input_iter2, out_iter, non_const(std::plus<T>())); + }); + } +}; + +int +main() +{ + //const operator() + test<int32_t, int32_t, int32_t>(TheOperation<int32_t, int32_t, int32_t>(1)); + test<float32_t, float32_t, float32_t>(TheOperation<float32_t, float32_t, float32_t>(1.5)); + //non-const operator() + test<int32_t, float32_t, float32_t>(non_const(TheOperation<int32_t, float32_t, float32_t>(1.5))); + test<int64_t, float64_t, float32_t>(non_const(TheOperation<int64_t, float64_t, float32_t>(1.5))); + //lambda + test<int8_t, float64_t, int8_t>([](const int8_t& x, const float64_t& y) { return int8_t(int8_t(1.5) + x - y); }); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/transform_unary.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/transform_unary.pass.cpp new file mode 100644 index 0000000..69a410c --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/transform_unary.pass.cpp @@ -0,0 +1,91 @@ +// -*- C++ -*- +//===-- transform_unary.pass.cpp ------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename InputIterator, typename OutputIterator> +void +check_and_reset(InputIterator first, InputIterator last, OutputIterator out_first) +{ + typedef typename std::iterator_traits<OutputIterator>::value_type Out; + typename std::iterator_traits<OutputIterator>::difference_type k = 0; + for (; first != last; ++first, ++out_first, ++k) + { + // check + Out expected = 1 - *first; + Out actual = *out_first; + EXPECT_EQ(expected, actual, "wrong value in output sequence"); + // reset + *out_first = k % 7 != 4 ? 7 * k - 5 : 0; + } +} + +struct test_one_policy +{ + template <typename Policy, typename InputIterator, typename OutputIterator, typename UnaryOp> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, UnaryOp op) + { + auto orr = std::transform(exec, first, last, out_first, op); + EXPECT_TRUE(out_last == orr, "transform returned wrong iterator"); + check_and_reset(first, last, out_first); + } +}; + +template <typename Tin, typename Tout> +void +test() +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<Tin> in(n, [](int32_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; }); + + Sequence<Tout> out(n); + + const auto flip = Complement<Tin, Tout>(1); + invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), out.begin(), out.end(), flip); + invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), out.begin(), out.end(), flip); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + invoke_if(exec, [&]() { transform(exec, input_iter, input_iter, out_iter, non_const(std::negate<T>())); }); + } +}; + +int +main() +{ + test<int32_t, int32_t>(); + test<int32_t, float32_t>(); + test<uint16_t, float32_t>(); + test<float32_t, float64_t>(); + test<float64_t, float64_t>(); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/unique.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/unique.pass.cpp new file mode 100644 index 0000000..fbd4742 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/unique.pass.cpp @@ -0,0 +1,163 @@ +// -*- C++ -*- +//===-- unique.pass.cpp ---------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Test for unique +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct run_unique +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename ForwardIt, typename Generator> + void + operator()(pstl::execution::unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, + ForwardIt last2, Generator generator) + { + } + + template <typename ForwardIt, typename Generator> + void + operator()(pstl::execution::parallel_unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, + ForwardIt last2, Generator generator) + { + } + + template <typename ForwardIt, typename BinaryPred, typename Generator> + void + operator()(pstl::execution::unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, + ForwardIt last2, BinaryPred pred, Generator generator) + { + } + + template <typename ForwardIt, typename BinaryPred, typename Generator> + void + operator()(pstl::execution::parallel_unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, + ForwardIt last2, BinaryPred pred, Generator generator) + { + } +#endif + + template <typename Policy, typename ForwardIt, typename Generator> + void + operator()(Policy&& exec, ForwardIt first1, ForwardIt last1, ForwardIt first2, ForwardIt last2, Generator generator) + { + using namespace std; + + // Preparation + fill_data(first1, last1, generator); + fill_data(first2, last2, generator); + + ForwardIt i = unique(first1, last1); + ForwardIt k = unique(exec, first2, last2); + + auto n = std::distance(first1, i); + EXPECT_TRUE(std::distance(first2, k) == n, "wrong return value from unique without predicate"); + EXPECT_EQ_N(first1, first2, n, "wrong effect from unique without predicate"); + } + + template <typename Policy, typename ForwardIt, typename BinaryPred, typename Generator> + void + operator()(Policy&& exec, ForwardIt first1, ForwardIt last1, ForwardIt first2, ForwardIt last2, BinaryPred pred, + Generator generator) + { + using namespace std; + + // Preparation + fill_data(first1, last1, generator); + fill_data(first2, last2, generator); + + ForwardIt i = unique(first1, last1, pred); + ForwardIt k = unique(exec, first2, last2, pred); + + auto n = std::distance(first1, i); + EXPECT_TRUE(std::distance(first2, k) == n, "wrong return value from unique with predicate"); + EXPECT_EQ_N(first1, first2, n, "wrong effect from unique with predicate"); + } +}; + +template <typename T, typename Generator, typename Predicate> +void +test(Generator generator, Predicate pred) +{ + const std::size_t max_size = 1000000; + Sequence<T> in(max_size, [](size_t v) { return T(v); }); + Sequence<T> exp(max_size, [](size_t v) { return T(v); }); + + for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + invoke_on_all_policies(run_unique(), exp.begin(), exp.begin() + n, in.begin(), in.begin() + n, generator); + invoke_on_all_policies(run_unique(), exp.begin(), exp.begin() + n, in.begin(), in.begin() + n, pred, generator); + } +} + +template <typename T> +struct LocalWrapper +{ + T my_val; + + explicit LocalWrapper(T k) : my_val(k) {} + LocalWrapper(LocalWrapper&& input) : my_val(std::move(input.my_val)) {} + LocalWrapper& + operator=(LocalWrapper&& input) + { + my_val = std::move(input.my_val); + return *this; + } + friend bool + operator==(const LocalWrapper<T>& x, const LocalWrapper<T>& y) + { + return x.my_val == y.my_val; + } +}; + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + invoke_if(exec, [&]() { unique(exec, iter, iter, non_const(std::equal_to<T>())); }); + } +}; + +int +main() +{ +#if !defined(_PSTL_ICC_16_17_18_TEST_UNIQUE_MASK_RELEASE_BROKEN) + test<int32_t>([](size_t j) { return j / 3; }, + [](const int32_t& val1, const int32_t& val2) { return val1 * val1 == val2 * val2; }); + test<float64_t>([](size_t) { return float64_t(1); }, + [](const float64_t& val1, const float64_t& val2) { return val1 != val2; }); +#endif + test<LocalWrapper<uint32_t>>([](size_t j) { return LocalWrapper<uint32_t>(j); }, + [](const LocalWrapper<uint32_t>& val1, const LocalWrapper<uint32_t>& val2) { + return val1.my_val != val2.my_val; + }); + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + test<MemoryChecker>( + [](std::size_t idx){ return MemoryChecker{std::int32_t(idx / 3)}; }, + [](const MemoryChecker& val1, const MemoryChecker& val2){ return val1.value() == val2.value(); }); + EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from unique: number of ctors calls < num of dtors calls"); + EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from unique: number of ctors calls > num of dtors calls"); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.modifying.operations/unique_copy_equal.pass.cpp b/pstl/test/std/algorithms/alg.modifying.operations/unique_copy_equal.pass.cpp new file mode 100644 index 0000000..356d4d2 --- /dev/null +++ b/pstl/test/std/algorithms/alg.modifying.operations/unique_copy_equal.pass.cpp @@ -0,0 +1,135 @@ +// -*- C++ -*- +//===-- unique_copy_equal.pass.cpp ----------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for unique_copy +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct run_unique_copy +{ +#if defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) // dummy specializations to skip testing in case of broken configuration + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Predicate, typename T> + void + operator()(pstl::execution::parallel_policy, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n, + Predicate pred, T trash) + { + } + + template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Predicate, typename T> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, + OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, + OutputIterator2 expected_last, Size n, Predicate pred, T trash) + { + } +#endif + + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Predicate, typename T> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, Predicate pred, + T trash) + { + // Cleaning + std::fill_n(expected_first, n, trash); + std::fill_n(out_first, n, trash); + + // Run unique_copy + auto i = unique_copy(first, last, expected_first); + auto k = unique_copy(exec, first, last, out_first); + EXPECT_EQ_N(expected_first, out_first, n, "wrong unique_copy effect"); + for (size_t j = 0; j < GuardSize; ++j) + { + ++k; + } + EXPECT_TRUE(out_last == k, "wrong return value from unique_copy"); + + // Cleaning + std::fill_n(expected_first, n, trash); + std::fill_n(out_first, n, trash); + // Run unique_copy with predicate + i = unique_copy(first, last, expected_first, pred); + k = unique_copy(exec, first, last, out_first, pred); + EXPECT_EQ_N(expected_first, out_first, n, "wrong unique_copy with predicate effect"); + for (size_t j = 0; j < GuardSize; ++j) + { + ++k; + } + EXPECT_TRUE(out_last == k, "wrong return value from unique_copy with predicate"); + } +}; + +template <typename T, typename BinaryPredicate, typename Convert> +void +test(T trash, BinaryPredicate pred, Convert convert, bool check_weakness = true) +{ + // Try sequences of various lengths. + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + // count is number of output elements, plus a handful + // more for sake of detecting buffer overruns. + Sequence<T> in(n, [&](size_t k) -> T { return convert(k ^ n); }); + using namespace std; + size_t count = GuardSize; + for (size_t k = 0; k < in.size(); ++k) + count += k == 0 || !pred(in[k], in[k - 1]) ? 1 : 0; + Sequence<T> out(count, [=](size_t) { return trash; }); + Sequence<T> expected(count, [=](size_t) { return trash; }); + if (check_weakness) + { + auto expected_result = unique_copy(in.begin(), in.end(), expected.begin(), pred); + size_t m = expected_result - expected.begin(); + EXPECT_TRUE(n / (n < 10000 ? 4 : 6) <= m && m <= (3 * n + 1) / 4, "weak test for unique_copy"); + } + invoke_on_all_policies(run_unique_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), count, pred, trash); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + unique_copy(exec, input_iter, input_iter, out_iter, non_const(std::equal_to<T>())); + } +}; + +int +main() +{ + test<Number>(Number(42, OddTag()), std::equal_to<Number>(), + [](int32_t j) { return Number(3 * j / 13 ^ (j & 8), OddTag()); }); + + test<float32_t>(float32_t(42), std::equal_to<float32_t>(), + [](int32_t j) { return float32_t(5 * j / 23 ^ (j / 7)); }); +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN) + test<float32_t>(float32_t(42), [](float32_t, float32_t) { return false; }, [](int32_t j) { return float32_t(j); }, + false); +#endif + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/adjacent_find.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/adjacent_find.pass.cpp new file mode 100644 index 0000000..36a623b --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/adjacent_find.pass.cpp @@ -0,0 +1,114 @@ +// -*- C++ -*- +//===-- adjacent_find.pass.cpp --------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_adjacent_find +{ + template <typename Policy, typename Iterator, typename Pred> + void + operator()(Policy&& exec, Iterator first, Iterator last, Pred pred) + { + using namespace std; + + auto k = std::adjacent_find(first, last, pred); + auto i = adjacent_find(exec, first, last, pred); + EXPECT_TRUE(i == k, "wrong return value from adjacent_find with predicate"); + + i = adjacent_find(exec, first, last); + EXPECT_TRUE(i == k, "wrong return value from adjacent_find without predicate"); + } +}; + +template <typename T> +void +test_adjacent_find_by_type() +{ + + size_t counts[] = {2, 3, 500}; + for (size_t c = 0; c < const_size(counts); ++c) + { + + for (size_t e = 0; e < (counts[c] >= 64 ? 64 : (counts[c] == 2 ? 1 : 2)); ++e) + { + Sequence<T> in(counts[c], [](size_t v) -> T { return T(v); }); //fill 0...n + in[e] = in[e + 1] = -1; //make an adjacent pair + + auto i = std::adjacent_find(in.cbegin(), in.cend(), std::equal_to<T>()); + EXPECT_TRUE(i == in.cbegin() + e, "std::adjacent_find returned wrong result"); + + invoke_on_all_policies(test_adjacent_find(), in.begin(), in.end(), std::equal_to<T>()); + invoke_on_all_policies(test_adjacent_find(), in.cbegin(), in.cend(), std::equal_to<T>()); + } + } + + //special cases: size=0, size=1; + for (size_t expect = 0; expect < 1; ++expect) + { + Sequence<T> in(expect, [](size_t v) -> T { return T(v); }); //fill 0...n + auto i = std::adjacent_find(in.cbegin(), in.cend(), std::equal_to<T>()); + EXPECT_TRUE(i == in.cbegin() + expect, "std::adjacent_find returned wrong result"); + + invoke_on_all_policies(test_adjacent_find(), in.begin(), in.end(), std::equal_to<T>()); + invoke_on_all_policies(test_adjacent_find(), in.cbegin(), in.cend(), std::equal_to<T>()); + } + + //special cases: + Sequence<T> a1 = {5, 5, 5, 6, 7, 8, 9}; + invoke_on_all_policies(test_adjacent_find(), a1.begin(), a1.end(), std::equal_to<T>()); + invoke_on_all_policies(test_adjacent_find(), a1.begin() + 1, a1.end(), std::equal_to<T>()); + + invoke_on_all_policies(test_adjacent_find(), a1.cbegin(), a1.cend(), std::equal_to<T>()); + invoke_on_all_policies(test_adjacent_find(), a1.cbegin() + 1, a1.cend(), std::equal_to<T>()); + + Sequence<T> a2 = {5, 6, 7, 8, 9, 9}; + invoke_on_all_policies(test_adjacent_find(), a2.begin(), a2.end(), std::equal_to<T>()); + invoke_on_all_policies(test_adjacent_find(), a2.begin(), a2.end() - 1, std::equal_to<T>()); + + invoke_on_all_policies(test_adjacent_find(), a2.cbegin(), a2.cend(), std::equal_to<T>()); + invoke_on_all_policies(test_adjacent_find(), a2.cbegin(), a2.cend() - 1, std::equal_to<T>()); + + Sequence<T> a3 = {5, 6, 6, 6, 7, 9, 9, 9, 9}; + invoke_on_all_policies(test_adjacent_find(), a3.begin(), a3.end(), std::equal_to<T>()); + + invoke_on_all_policies(test_adjacent_find(), a3.cbegin(), a3.cend(), std::equal_to<T>()); +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + adjacent_find(exec, iter, iter, non_const(std::equal_to<T>())); + } +}; + +int +main() +{ + + test_adjacent_find_by_type<int32_t>(); + test_adjacent_find_by_type<float64_t>(); + + test_algo_basic_single<int32_t>(run_for_rnd_bi<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/all_of.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/all_of.pass.cpp new file mode 100644 index 0000000..ec23dc4 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/all_of.pass.cpp @@ -0,0 +1,117 @@ +// -*- C++ -*- +//===-- all_of.pass.cpp ---------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +/* + TODO: consider implementing the following tests for a better code coverage + - correctness + - bad input argument (if applicable) + - data corruption around/of input and output + - correctly work with nested parallelism + - check that algorithm does not require anything more than is described in its requirements section +*/ + +using namespace TestUtils; + +struct test_all_of +{ + template <typename ExecutionPolicy, typename Iterator, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected) + { + + auto actualr = std::all_of(exec, begin, end, pred); + EXPECT_EQ(expected, actualr, "result for all_of"); + } +}; + +template <typename T> +struct Parity +{ + bool parity; + + public: + Parity(bool parity_) : parity(parity_) {} + bool + operator()(T value) const + { + return (size_t(value) ^ parity) % 2 == 0; + } +}; + +template <typename T> +void +test(size_t bits) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + + // Sequence of odd values + Sequence<T> in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); }); + + // Even value, or false when T is bool. + T spike(2 * HashBits(n, bits - 1)); + Sequence<T> inCopy(in); + + invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), true); + invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), true); + EXPECT_EQ(in, inCopy, "all_of modified input sequence"); + if (n > 0) + { + // Sprinkle in a miss + in[2 * n / 3] = spike; + invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false); + invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false); + + // Sprinkle in a few more misses + in[n / 2] = spike; + in[n / 3] = spike; + invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false); + invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false); + } + } +} + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + all_of(exec, iter, iter, non_const(is_even)); + } +}; + +int +main() +{ + test<int32_t>(8 * sizeof(int32_t)); + test<uint16_t>(8 * sizeof(uint16_t)); + test<float64_t>(53); +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN) + test<bool>(1); +#endif + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/any_of.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/any_of.pass.cpp new file mode 100644 index 0000000..97d1691 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/any_of.pass.cpp @@ -0,0 +1,103 @@ +// -*- C++ -*- +//===-- any_of.pass.cpp ---------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +/* + TODO: consider implementing the following tests for a better code coverage + - correctness + - bad input argument (if applicable) + - data corruption around/of input and output + - correctly work with nested parallelism + - check that algorithm does not require anything more than is described in its requirements section +*/ + +using namespace TestUtils; + +struct test_any_of +{ + template <typename ExecutionPolicy, typename Iterator, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected) + { + + auto actualr = std::any_of(exec, begin, end, pred); + EXPECT_EQ(expected, actualr, "result for any_of"); + } +}; + +template <typename T> +void +test(size_t bits) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + + // Sequence of odd values + Sequence<T> in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); }); + + // Even value, or false when T is bool. + T spike(2 * HashBits(n, bits - 1)); + Sequence<T> inCopy(in); + + invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), false); + invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), false); + EXPECT_EQ(in, inCopy, "any_of modified input sequence"); + if (n > 0) + { + // Sprinkle in a hit + in[2 * n / 3] = spike; + invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), true); + invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), true); + + // Sprinkle in a few more hits + in[n / 2] = spike; + in[n / 3] = spike; + invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), true); + invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), true); + } + } +} + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + any_of(exec, iter, iter, non_const(is_even)); + } +}; + +int +main() +{ + test<int32_t>(8 * sizeof(int32_t)); + test<uint16_t>(8 * sizeof(uint16_t)); + test<float64_t>(53); +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN) + test<bool>(1); +#endif + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/count.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/count.pass.cpp new file mode 100644 index 0000000..e8eca02 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/count.pass.cpp @@ -0,0 +1,108 @@ +// -*- C++ -*- +//===-- count.pass.cpp ----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for count and count_if +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_count +{ + template <typename Policy, typename Iterator, typename T> + void + operator()(Policy&& exec, Iterator first, Iterator last, T needle) + { + auto expected = std::count(first, last, needle); + auto result = std::count(exec, first, last, needle); + EXPECT_EQ(expected, result, "wrong count result"); + } +}; + +struct test_count_if +{ + template <typename Policy, typename Iterator, typename Predicate> + void + operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred) + { + auto expected = std::count_if(first, last, pred); + auto result = std::count_if(exec, first, last, pred); + EXPECT_EQ(expected, result, "wrong count_if result"); + } +}; + +template <typename T> +class IsEqual +{ + T value; + + public: + IsEqual(T value_, OddTag) : value(value_) {} + bool + operator()(const T& x) const + { + return x == value; + } +}; + +template <typename In, typename T, typename Predicate, typename Convert> +void +test(T needle, Predicate pred, Convert convert) +{ + // Try sequences of various lengths. + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<In> in(n, [=](size_t k) -> In { + // Sprinkle "42" and "50" early, so that short sequences have non-zero count. + return convert((n - k - 1) % 3 == 0 ? 42 : (n - k - 2) % 5 == 0 ? 50 : 3 * (int(k) % 1000 - 500)); + }); + invoke_on_all_policies(test_count(), in.begin(), in.end(), needle); + invoke_on_all_policies(test_count_if(), in.begin(), in.end(), pred); + + invoke_on_all_policies(test_count(), in.cbegin(), in.cend(), needle); + invoke_on_all_policies(test_count_if(), in.cbegin(), in.cend(), pred); + } +} + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + count_if(exec, iter, iter, non_const(is_even)); + } +}; + +int +main() +{ + test<int32_t>(42, IsEqual<int32_t>(50, OddTag()), [](int32_t j) { return j; }); +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN) + test<int32_t>(42, [](const int32_t&) { return true; }, [](int32_t j) { return j; }); +#endif + test<float64_t>(42, IsEqual<float64_t>(50, OddTag()), [](int32_t j) { return float64_t(j); }); + test<Number>(Number(42, OddTag()), IsEqual<Number>(Number(50, OddTag()), OddTag()), + [](int32_t j) { return Number(j, OddTag()); }); + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/equal.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/equal.pass.cpp new file mode 100644 index 0000000..a6983ea --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/equal.pass.cpp @@ -0,0 +1,168 @@ +// -*- C++ -*- +//===-- equal.pass.cpp ----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +#define CPP14_ENABLED 0 + +struct UserType +{ + size_t key; + float32_t f; + float64_t d; + int32_t i; + + bool + operator()(UserType a, UserType b) + { + return a.key < b.key; + } + bool + operator<(UserType a) + { + return a.key < key; + } + bool + operator>=(UserType a) + { + return a.key <= key; + } + bool + operator<=(UserType a) + { + return a.key >= key; + } + bool + operator==(UserType a) + { + return a.key == key; + } + bool + operator==(UserType a) const + { + return a.key == key; + } + bool + operator!=(UserType a) + { + return a.key != key; + } + UserType operator!() + { + UserType tmp; + tmp.key = !key; + return tmp; + } + friend std::ostream& + operator<<(std::ostream& stream, const UserType a) + { + stream << a.key; + return stream; + } + + UserType() : key(-1), f(0.0f), d(0.0), i(0) {} + UserType(size_t Number) : key(Number), f(0.0f), d(0.0), i(0) {} + UserType& + operator=(const UserType& other) + { + key = other.key; + return *this; + } + UserType(const UserType& other) : key(other.key), f(other.f), d(other.d), i(other.i) {} + UserType(UserType&& other) : key(other.key), f(other.f), d(other.d), i(other.i) + { + other.key = -1; + other.f = 0.0f; + other.d = 0.0; + other.i = 0; + } +}; + +struct test_one_policy +{ + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> + void + operator()(ExecutionPolicy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2, bool is_true_equal) + { + using namespace std; + + auto expected = equal(first1, last1, first2); + auto actual = equal(exec, first1, last1, first2); + EXPECT_EQ(expected, actual, "result for equal for random-access iterator, checking against std::equal()"); + + // testing bool + EXPECT_TRUE(is_true_equal == actual, "result for equal for random-access iterator, bool"); + +//add C++14 equal symantics tests +//add more cases for inCopy size less than in +#if CPP14_ENABLED + auto actualr14 = std::equal(in.cbegin(), in.cend(), inCopy.cbegin(), inCopy.cend()); + EXPECT_EQ(expected, actualr14, "result for equal for random-access iterator"); +#endif + } +}; + +template <typename T> +void +test(size_t bits) +{ + for (size_t n = 1; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + + // Sequence of odd values + Sequence<T> in(n, [bits](size_t k) { return T(2 * HashBits(k, bits - 1) ^ 1); }); + Sequence<T> inCopy(in); + + invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), inCopy.begin(), true); + invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), inCopy.cbegin(), true); + + // testing bool !equal() + inCopy[0] = !inCopy[0]; + invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), inCopy.begin(), false); + invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), inCopy.cbegin(), false); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename FirstIterator, typename SecondInterator> + void + operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) + { + equal(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>())); + } +}; + +int +main() +{ + + test<int32_t>(8 * sizeof(int32_t)); + test<uint16_t>(8 * sizeof(uint16_t)); + test<float64_t>(53); +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN) + test<bool>(1); +#endif + test<UserType>(256); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/find.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/find.pass.cpp new file mode 100644 index 0000000..54b25c27 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/find.pass.cpp @@ -0,0 +1,96 @@ +// -*- C++ -*- +//===-- find.pass.cpp -----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for find +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_find +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename Iterator, typename Value> + void + operator()(pstl::execution::unsequenced_policy, Iterator first, Iterator last, Value value) + { + } + template <typename Iterator, typename Value> + void + operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Value value) + { + } +#endif + + template <typename Policy, typename Iterator, typename Value> + void + operator()(Policy&& exec, Iterator first, Iterator last, Value value) + { + auto i = std::find(first, last, value); + auto j = find(exec, first, last, value); + EXPECT_TRUE(i == j, "wrong return value from find"); + } +}; + +template <typename T, typename Value, typename Hit, typename Miss> +void +test(Value value, Hit hit, Miss miss) +{ + // Try sequences of various lengths. + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> in(n, [&](size_t k) -> T { return miss(n ^ k); }); + // Try different find positions, including not found. + // By going backwards, we can add extra matches that are *not* supposed to be found. + // The decreasing exponential gives us O(n) total work for the loop since each find takes O(m) time. + for (size_t m = n; m > 0; m *= 0.6) + { + if (m < n) + in[m] = hit(n ^ m); + invoke_on_all_policies(test_find(), in.begin(), in.end(), value); + invoke_on_all_policies(test_find(), in.cbegin(), in.cend(), value); + } + } +} + +// Type defined for sake of checking that std::find works with asymmetric ==. +class Weird +{ + Number value; + + public: + friend bool + operator==(Number x, Weird y) + { + return x == y.value; + } + Weird(int32_t val, OddTag) : value(val, OddTag()) {} +}; + +int +main() +{ + // Note that the "hit" and "miss" functions here avoid overflow issues. + test<Number>(Weird(42, OddTag()), [](int32_t) { return Number(42, OddTag()); }, // hit + [](int32_t j) { return Number(j == 42 ? 0 : j, OddTag()); }); // miss + + // Test with value that is equal to two different bit patterns (-0.0 and 0.0) + test<float32_t>(-0.0, [](int32_t j) { return j & 1 ? 0.0 : -0.0; }, // hit + [](int32_t j) { return j == 0 ? ~j : j; }); // miss + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/find_end.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/find_end.pass.cpp new file mode 100644 index 0000000..ed0185f --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/find_end.pass.cpp @@ -0,0 +1,123 @@ +// -*- C++ -*- +//===-- find_end.pass.cpp -------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_one_policy +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename Iterator1, typename Iterator2, typename Predicate> + void + operator()(pstl::execution::unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, + Predicate pred) + { + } + template <typename Iterator1, typename Iterator2, typename Predicate> + void + operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, + Predicate pred) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, Predicate pred) + { + using namespace std; + // For find_end + { + auto expected = find_end(b, e, bsub, esub, pred); + auto actual = find_end(exec, b, e, bsub, esub); + EXPECT_TRUE(actual == expected, "wrong return result from find_end"); + + actual = find_end(exec, b, e, bsub, esub, pred); + EXPECT_TRUE(actual == expected, "wrong return result from find_end with a predicate"); + } + + // For search + { + auto expected = search(b, e, bsub, esub, pred); + auto actual = search(exec, b, e, bsub, esub); + EXPECT_TRUE(actual == expected, "wrong return result from search"); + + actual = search(exec, b, e, bsub, esub, pred); + EXPECT_TRUE(actual == expected, "wrong return result from search with a predicate"); + } + } +}; + +template <typename T> +void +test(const std::size_t bits) +{ + + const std::size_t max_n1 = 1000; + const std::size_t max_n2 = (max_n1 * 10) / 8; + Sequence<T> in(max_n1, [bits](std::size_t) { return T(2 * HashBits(max_n1, bits - 1) ^ 1); }); + Sequence<T> sub(max_n2, [bits](std::size_t) { return T(2 * HashBits(max_n1, bits - 1)); }); + for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1)) + { + std::size_t sub_n[] = {0, 1, 3, n1, (n1 * 10) / 8}; + std::size_t res[] = {0, 1, n1 / 2, n1}; + for (auto n2 : sub_n) + { + for (auto r : res) + { + std::size_t i = r, isub = 0; + for (; i < n1 && isub < n2; ++i, ++isub) + in[i] = sub[isub]; + invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, sub.begin(), sub.begin() + n2, + std::equal_to<T>()); + invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, sub.cbegin(), + sub.cbegin() + n2, std::equal_to<T>()); + } + } + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename FirstIterator, typename SecondInterator> + void + operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) + { + invoke_if(exec, [&]() { + find_end(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>())); + search(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>())); + }); + } +}; + +int +main() +{ + test<int32_t>(8 * sizeof(int32_t)); + test<uint16_t>(8 * sizeof(uint16_t)); + test<float64_t>(53); +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN) + test<bool>(1); +#endif + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/find_first_of.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/find_first_of.pass.cpp new file mode 100644 index 0000000..5b4801e --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/find_first_of.pass.cpp @@ -0,0 +1,112 @@ +// -*- C++ -*- +//===-- find_first_of.pass.cpp --------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_one_policy +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename Iterator1, typename Iterator2, typename Predicate> + void + operator()(pstl::execution::unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, + Predicate pred) + { + } + template <typename Iterator1, typename Iterator2, typename Predicate> + void + operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, + Predicate pred) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, Predicate pred) + { + using namespace std; + Iterator1 expected = find_first_of(b, e, bsub, esub, pred); + Iterator1 actual = find_first_of(exec, b, e, bsub, esub, pred); + EXPECT_TRUE(actual == expected, "wrong return result from find_first_of with a predicate"); + + expected = find_first_of(b, e, bsub, esub); + actual = find_first_of(exec, b, e, bsub, esub); + EXPECT_TRUE(actual == expected, "wrong return result from find_first_of"); + } +}; + +template <typename T, typename Predicate> +void +test(Predicate pred) +{ + + const std::size_t max_n1 = 1000; + const std::size_t max_n2 = (max_n1 * 10) / 8; + Sequence<T> in1(max_n1, [](std::size_t) { return T(1); }); + Sequence<T> in2(max_n2, [](std::size_t) { return T(0); }); + for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1)) + { + std::size_t sub_n[] = {0, 1, n1 / 3, n1, (n1 * 10) / 8}; + for (const auto n2 : sub_n) + { + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.data(), in2.data() + n2, pred); + + in2[n2 / 2] = T(1); + invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + n1, in2.data(), in2.data() + n2, + pred); + + if (n2 >= 3) + { + in2[2 * n2 / 3] = T(1); + invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + n1, in2.begin(), + in2.begin() + n2, pred); + in2[2 * n2 / 3] = T(0); + } + in2[n2 / 2] = T(0); + } + } + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n1 / 10, in1.data(), + in1.data() + max_n1 / 10, pred); +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename FirstIterator, typename SecondInterator> + void + operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) + { + invoke_if(exec, [&]() { + find_first_of(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>())); + }); + } +}; + +int +main() +{ + test<int32_t>(std::equal_to<int32_t>()); + test<uint16_t>(std::not_equal_to<uint16_t>()); + test<float64_t>([](const float64_t x, const float64_t y) { return x * x == y * y; }); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/find_if.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/find_if.pass.cpp new file mode 100644 index 0000000..180d003 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/find_if.pass.cpp @@ -0,0 +1,109 @@ +// -*- C++ -*- +//===-- find_if.pass.cpp --------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for find_if and find_if_not +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_find_if +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename Iterator, typename Predicate, typename NotPredicate> + void + operator()(pstl::execution::unsequenced_policy, Iterator first, Iterator last, Predicate pred, + NotPredicate not_pred) + { + } + template <typename Iterator, typename Predicate, typename NotPredicate> + void + operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Predicate pred, + NotPredicate not_pred) + { + } +#endif + + template <typename Policy, typename Iterator, typename Predicate, typename NotPredicate> + void + operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred, NotPredicate not_pred) + { + auto i = std::find_if(first, last, pred); + auto j = find_if(exec, first, last, pred); + EXPECT_TRUE(i == j, "wrong return value from find_if"); + auto i_not = find_if_not(exec, first, last, not_pred); + EXPECT_TRUE(i_not == i, "wrong return value from find_if_not"); + } +}; + +template <typename T, typename Predicate, typename Hit, typename Miss> +void +test(Predicate pred, Hit hit, Miss miss) +{ + auto not_pred = [pred](T x) { return !pred(x); }; + // Try sequences of various lengths. + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> in(n, [&](size_t k) -> T { return miss(n ^ k); }); + // Try different find positions, including not found. + // By going backwards, we can add extra matches that are *not* supposed to be found. + // The decreasing exponential gives us O(n) total work for the loop since each find takes O(m) time. + for (size_t m = n; m > 0; m *= 0.6) + { + if (m < n) + in[m] = hit(n ^ m); + invoke_on_all_policies(test_find_if(), in.begin(), in.end(), pred, not_pred); + invoke_on_all_policies(test_find_if(), in.cbegin(), in.cend(), pred, not_pred); + } + } +} + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + + invoke_if(exec, [&]() { + find_if(exec, iter, iter, non_const(is_even)); + find_if_not(exec, iter, iter, non_const(is_even)); + }); + } +}; + +int +main() +{ +#if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN) + // Note that the "hit" and "miss" functions here avoid overflow issues. + test<Number>(IsMultiple(5, OddTag()), [](int32_t j) { return Number(j - j % 5, OddTag()); }, // hit + [](int32_t j) { return Number(j % 5 == 0 ? j ^ 1 : j, OddTag()); }); // miss +#endif + + // Try type for which algorithm can really be vectorized. + test<float32_t>([](float32_t x) { return x >= 0; }, [](float32_t j) { return j * j; }, + [](float32_t j) { return -1 - j * j; }); + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/for_each.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/for_each.pass.cpp new file mode 100644 index 0000000..14a0ad4 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/for_each.pass.cpp @@ -0,0 +1,102 @@ +// -*- C++ -*- +//===-- for_each.pass.cpp -------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename Type> +struct Gen +{ + Type + operator()(std::size_t k) + { + return Type(k % 5 != 1 ? 3 * k - 7 : 0); + }; +}; + +template <typename T> +struct Flip +{ + int32_t val; + Flip(int32_t y) : val(y) {} + T + operator()(T& x) const + { + return x = val - x; + } +}; + +struct test_one_policy +{ + template <typename Policy, typename Iterator, typename Size> + void + operator()(Policy&& exec, Iterator first, Iterator last, Iterator expected_first, Iterator expected_last, Size n) + { + typedef typename std::iterator_traits<Iterator>::value_type T; + + // Try for_each + std::for_each(expected_first, expected_last, Flip<T>(1)); + for_each(exec, first, last, Flip<T>(1)); + EXPECT_EQ_N(expected_first, first, n, "wrong effect from for_each"); + + // Try for_each_n + std::for_each_n(std::execution::seq, expected_first, n, Flip<T>(1)); + for_each_n(exec, first, n, Flip<T>(1)); + EXPECT_EQ_N(expected_first, first, n, "wrong effect from for_each_n"); + } +}; + +template <typename T> +void +test() +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> inout(n, Gen<T>()); + Sequence<T> expected(n, Gen<T>()); + invoke_on_all_policies(test_one_policy(), inout.begin(), inout.end(), expected.begin(), expected.end(), + inout.size()); + } +} + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + invoke_if(exec, [&]() { + auto f = [](typename std::iterator_traits<Iterator>::reference x) { x = x + 1; }; + + for_each(exec, iter, iter, non_const(f)); + for_each_n(exec, iter, 0, non_const(f)); + }); + } +}; + +int +main() +{ + test<int32_t>(); + test<uint16_t>(); + test<float64_t>(); + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/mismatch.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/mismatch.pass.cpp new file mode 100644 index 0000000..4d83ad6 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/mismatch.pass.cpp @@ -0,0 +1,132 @@ +// -*- C++ -*- +//===-- mismatch.pass.cpp -------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_mismatch +{ + template <typename Policy, typename Iterator1, typename Iterator2> + void + operator()(Policy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2) + { + using namespace std; + typedef typename iterator_traits<Iterator1>::value_type T; + { + const auto expected = std::mismatch(first1, last1, first2, std::equal_to<T>()); + const auto res3 = mismatch(exec, first1, last1, first2, std::equal_to<T>()); + EXPECT_TRUE(expected == res3, "wrong return result from mismatch"); + const auto res4 = mismatch(exec, first1, last1, first2); + EXPECT_TRUE(expected == res4, "wrong return result from mismatch"); + } + } + template <typename Policy, typename Iterator1, typename Iterator2> + void + operator()(Policy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2) + { + using namespace std; + typedef typename iterator_traits<Iterator1>::value_type T; + { + const auto expected = mismatch(std::execution::seq, first1, last1, first2, last2, std::equal_to<T>()); + const auto res1 = mismatch(exec, first1, last1, first2, last2, std::equal_to<T>()); + EXPECT_TRUE(expected == res1, "wrong return result from mismatch"); + const auto res2 = mismatch(exec, first1, last1, first2, last2); + EXPECT_TRUE(expected == res2, "wrong return result from mismatch"); + } + } +}; + +template <typename T> +void +test_mismatch_by_type() +{ + using namespace std; + for (size_t size = 0; size <= 100000; size = size <= 16 ? size + 1 : size_t(3.1415 * size)) + { + const T val = T(-1); + Sequence<T> in(size, [](size_t v) -> T { return T(v % 100); }); + { + Sequence<T> in2(in); + invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin(), in2.end()); + invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin()); + + const size_t min_size = 3; + if (size > min_size) + { + const size_t idx_for_1 = size / min_size; + in[idx_for_1] = val, in[idx_for_1 + 1] = val, in[idx_for_1 + 2] = val; + invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin(), in2.end()); + invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin()); + } + + const size_t idx_for_2 = 500; + if (size >= idx_for_2 - 1) + { + in2[size / idx_for_2] = val; + invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend()); + invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin()); + } + } + { + Sequence<T> in2(100, [](size_t v) -> T { return T(v); }); + invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin(), in.end()); + // We can't call std::mismatch with semantic below when size of second sequence less than size of first sequence + if (in2.size() <= in.size()) + invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin()); + + const size_t idx = 97; + in2[idx] = val; + in2[idx + 1] = val; + invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend()); + if (in.size() <= in2.size()) + invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin()); + } + { + Sequence<T> in2({}); + invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin(), in.end()); + + invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend()); + if (in.size() == 0) + invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin()); + } + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename FirstIterator, typename SecondInterator> + void + operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) + { + mismatch(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::less<T>())); + } +}; + +int +main() +{ + + test_mismatch_by_type<int32_t>(); + test_mismatch_by_type<float64_t>(); + test_mismatch_by_type<Wrapper<int32_t>>(); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/none_of.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/none_of.pass.cpp new file mode 100644 index 0000000..dbdcd54 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/none_of.pass.cpp @@ -0,0 +1,101 @@ +// -*- C++ -*- +//===-- none_of.pass.cpp --------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +/* + TODO: consider implementing the following tests for a better code coverage + - correctness + - bad input argument (if applicable) + - data corruption around/of input and output + - correctly work with nested parallelism + - check that algorithm does not require anything more than is described in its requirements section +*/ + +using namespace TestUtils; + +struct test_none_of +{ + template <typename ExecutionPolicy, typename Iterator, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected) + { + + auto actualr = std::none_of(exec, begin, end, pred); + EXPECT_EQ(expected, actualr, "result for none_of"); + } +}; + +template <typename T> +void +test(size_t bits) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + + // Sequence of odd values + Sequence<T> in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); }); + + // Even value, or false when T is bool. + T spike(2 * HashBits(n, bits - 1)); + + invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to<T>(spike), true); + invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), true); + if (n > 0) + { + // Sprinkle in a hit + in[2 * n / 3] = spike; + invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to<T>(spike), false); + invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), false); + + // Sprinkle in a few more hits + in[n / 3] = spike; + in[n / 2] = spike; + invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to<T>(spike), false); + invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), false); + } + } +} + +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + auto is_even = [&](float64_t v) { + uint32_t i = (uint32_t)v; + return i % 2 == 0; + }; + none_of(exec, iter, iter, non_const(is_even)); + } +}; + +int +main() +{ + test<int32_t>(8 * sizeof(int32_t)); + test<uint16_t>(8 * sizeof(uint16_t)); + test<float64_t>(53); +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN) + test<bool>(1); +#endif + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/nth_element.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/nth_element.pass.cpp new file mode 100644 index 0000000..f3e43da --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/nth_element.pass.cpp @@ -0,0 +1,175 @@ +// -*- C++ -*- +//===-- nth_element.pass.cpp ----------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <iostream> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +// User defined type with minimal requirements +template <typename T> +struct DataType +{ + explicit DataType(int32_t k) : my_val(k) {} + DataType(DataType&& input) + { + my_val = std::move(input.my_val); + input.my_val = T(0); + } + DataType& + operator=(DataType&& input) + { + my_val = std::move(input.my_val); + input.my_val = T(0); + return *this; + } + T + get_val() const + { + return my_val; + } + + friend std::ostream& + operator<<(std::ostream& stream, const DataType<T>& input) + { + return stream << input.my_val; + } + + private: + T my_val; +}; + +template <typename T> +bool +is_equal(const DataType<T>& x, const DataType<T>& y) +{ + return x.get_val() == y.get_val(); +} + +template <typename T> +bool +is_equal(const T& x, const T& y) +{ + return x == y; +} + +struct test_one_policy +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration + template <typename Iterator1, typename Size, typename Generator1, typename Generator2, typename Compare> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::unsequenced_policy, Iterator1 first1, Iterator1 last1, Iterator1 first2, + Iterator1 last2, Size n, Size m, Generator1 generator1, Generator2 generator2, Compare comp) + { + } + template <typename Iterator1, typename Size, typename Generator1, typename Generator2, typename Compare> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 first1, Iterator1 last1, Iterator1 first2, + Iterator1 last2, Size n, Size m, Generator1 generator1, Generator2 generator2, Compare comp) + { + } +#endif + + // nth_element works only with random access iterators + template <typename Policy, typename Iterator1, typename Size, typename Generator1, typename Generator2, + typename Compare> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(Policy&& exec, Iterator1 first1, Iterator1 last1, Iterator1 first2, Iterator1 last2, Size n, Size m, + Generator1 generator1, Generator2 generator2, Compare comp) + { + + using T = typename std::iterator_traits<Iterator1>::value_type; + const Iterator1 mid1 = std::next(first1, m); + const Iterator1 mid2 = std::next(first2, m); + + fill_data(first1, mid1, generator1); + fill_data(mid1, last1, generator2); + fill_data(first2, mid2, generator1); + fill_data(mid2, last2, generator2); + std::nth_element(first1, mid1, last1, comp); + std::nth_element(exec, first2, mid2, last2, comp); + if (m > 0 && m < n) + { + EXPECT_TRUE(is_equal(*mid1, *mid2), "wrong result from nth_element with predicate"); + } + EXPECT_TRUE(std::find_first_of(first2, mid2, mid2, last2, [comp](T& x, T& y) { return comp(y, x); }) == mid2, + "wrong effect from nth_element with predicate"); + } + + template <typename Policy, typename Iterator1, typename Size, typename Generator1, typename Generator2, + typename Compare> + typename std::enable_if<!is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(Policy&&, Iterator1, Iterator1, Iterator1, Iterator1, Size, Size, Generator1, Generator2, Compare) + { + } +}; + +template <typename T, typename Generator1, typename Generator2, typename Compare> +void +test_by_type(Generator1 generator1, Generator2 generator2, Compare comp) +{ + using namespace std; + size_t max_size = 10000; + Sequence<T> in1(max_size, [](size_t v) { return T(v); }); + Sequence<T> exp(max_size, [](size_t v) { return T(v); }); + size_t m; + + for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + m = 0; + invoke_on_all_policies(test_one_policy(), exp.begin(), exp.begin() + n, in1.begin(), in1.begin() + n, n, m, + generator1, generator2, comp); + m = n / 7; + invoke_on_all_policies(test_one_policy(), exp.begin(), exp.begin() + n, in1.begin(), in1.begin() + n, n, m, + generator1, generator2, comp); + m = 3 * n / 5; + invoke_on_all_policies(test_one_policy(), exp.begin(), exp.begin() + n, in1.begin(), in1.begin() + n, n, m, + generator1, generator2, comp); + } + invoke_on_all_policies(test_one_policy(), exp.begin(), exp.begin() + max_size, in1.begin(), in1.begin() + max_size, + max_size, max_size, generator1, generator2, comp); +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + invoke_if(exec, [&]() { nth_element(exec, iter, iter, iter, non_const(std::less<T>())); }); + } +}; + +int +main() +{ + test_by_type<int32_t>([](int32_t i) { return 10 * i; }, [](int32_t i) { return i + 1; }, std::less<int32_t>()); + test_by_type<int32_t>([](int32_t) { return 0; }, [](int32_t) { return 0; }, std::less<int32_t>()); + + test_by_type<float64_t>([](int32_t i) { return -2 * i; }, [](int32_t i) { return -(2 * i + 1); }, + [](const float64_t x, const float64_t y) { return x > y; }); + + test_by_type<DataType<float32_t>>( + [](int32_t i) { return DataType<float32_t>(2 * i + 1); }, [](int32_t i) { return DataType<float32_t>(2 * i); }, + [](const DataType<float32_t>& x, const DataType<float32_t>& y) { return x.get_val() < y.get_val(); }); + + test_algo_basic_single<int32_t>(run_for_rnd<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.nonmodifying/search_n.pass.cpp b/pstl/test/std/algorithms/alg.nonmodifying/search_n.pass.cpp new file mode 100644 index 0000000..573f364 --- /dev/null +++ b/pstl/test/std/algorithms/alg.nonmodifying/search_n.pass.cpp @@ -0,0 +1,109 @@ +// -*- C++ -*- +//===-- search_n.pass.cpp -------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_one_policy +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename Iterator, typename Size, typename T, typename Predicate> + void + operator()(pstl::execution::unsequenced_policy, Iterator b, Iterator e, Size count, const T& value, Predicate pred) + { + } + template <typename Iterator, typename Size, typename T, typename Predicate> + void + operator()(pstl::execution::parallel_unsequenced_policy, Iterator b, Iterator e, Size count, const T& value, + Predicate pred) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator, typename Size, typename T, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator b, Iterator e, Size count, const T& value, Predicate pred) + { + using namespace std; + auto expected = search_n(b, e, count, value, pred); + auto actual = search_n(exec, b, e, count, value); + EXPECT_TRUE(actual == expected, "wrong return result from search_n"); + + actual = search_n(exec, b, e, count, value, pred); + EXPECT_TRUE(actual == expected, "wrong return result from search_n with a predicate"); + } +}; + +template <typename T> +void +test() +{ + + const std::size_t max_n1 = 100000; + const T value = T(1); + for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1)) + { + std::size_t sub_n[] = {0, 1, 3, n1, (n1 * 10) / 8}; + std::size_t res[] = {0, 1, n1 / 2, n1}; + for (auto n2 : sub_n) + { + // Some of standard libraries return "first" in this case. We return "last" according to the standard + if (n2 == 0) + { + continue; + } + for (auto r : res) + { + Sequence<T> in(n1, [](std::size_t) { return T(0); }); + std::size_t i = r, isub = 0; + for (; i < n1 && isub < n2; ++i, ++isub) + in[i] = value; + + invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, n2, value, std::equal_to<T>()); + invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, n2, value, std::equal_to<T>()); + } + } + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + invoke_if(exec, [&]() { search_n(exec, iter, iter, 0, T(0), non_const(std::equal_to<T>())); }); + } +}; + +int +main() +{ + test<int32_t>(); + test<uint16_t>(); + test<float64_t>(); +#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN) + test<bool>(); +#endif + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/alg.heap.operations/is_heap.pass.cpp b/pstl/test/std/algorithms/alg.sorting/alg.heap.operations/is_heap.pass.cpp new file mode 100644 index 0000000..08eca8e --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/alg.heap.operations/is_heap.pass.cpp @@ -0,0 +1,146 @@ +// -*- C++ -*- +//===-- is_heap.pass.cpp --------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for is_heap, is_heap_until +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" +#include <iostream> + +using namespace TestUtils; + +struct WithCmpOp +{ + int32_t _first; + int32_t _second; + WithCmpOp() : _first(0), _second(0){}; + explicit WithCmpOp(int32_t x) : _first(x), _second(x){}; + bool + operator<(const WithCmpOp& rhs) const + { + return this->_first < rhs._first; + } +}; + +struct test_is_heap +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename Iterator, typename Predicate> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::unsequenced_policy, Iterator first, Iterator last, Predicate pred) + { + } + template <typename Iterator, typename Predicate> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Predicate pred) + { + } +#endif + + template <typename Policy, typename Iterator, typename Predicate> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value, void>::type + operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred) + { + using namespace std; + // is_heap + { + bool expected = is_heap(first, last); + bool actual = is_heap(exec, first, last); + EXPECT_TRUE(expected == actual, "wrong return value from is_heap"); + } + // is_heap with predicate + { + bool expected = is_heap(first, last, pred); + bool actual = is_heap(exec, first, last, pred); + EXPECT_TRUE(expected == actual, "wrong return value from is_heap with predicate"); + } + // is_heap_until + { + Iterator expected = is_heap_until(first, last); + Iterator actual = is_heap_until(exec, first, last); + EXPECT_TRUE(expected == actual, "wrong return value from is_heap_until"); + } + // is_heap_until with predicate + { + const Iterator expected = is_heap_until(first, last, pred); + const auto y = std::distance(first, expected); + const Iterator actual = is_heap_until(exec, first, last, pred); + const auto x = std::distance(first, actual); + EXPECT_TRUE(expected == actual, "wrong return value from is_heap_until with predicate"); + EXPECT_EQ(x, y, "both iterators should be the same distance away from 'first'"); + } + } + + // is_heap, is_heap_until works only with random access iterators + template <typename Policy, typename Iterator, typename Predicate> + typename std::enable_if<!is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value, void>::type + operator()(Policy&&, Iterator, Iterator, Predicate) + { + } +}; + +template <typename T, typename Comp> +void +test_is_heap_by_type(Comp comp) +{ + using namespace std; + + const size_t max_size = 100000; + for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> in(n, [](size_t v) -> T { return T(v); }); + + invoke_on_all_policies(test_is_heap(), in.begin(), in.end(), comp); + + std::make_heap(in.begin(), in.begin() + n / 4, comp); + invoke_on_all_policies(test_is_heap(), in.cbegin(), in.cend(), comp); + + std::make_heap(in.begin(), in.begin() + n / 3, comp); + invoke_on_all_policies(test_is_heap(), in.begin(), in.end(), comp); + + std::make_heap(in.begin(), in.end(), comp); + invoke_on_all_policies(test_is_heap(), in.cbegin(), in.cend(), comp); + } + + Sequence<T> in(max_size / 10, [](size_t) -> T { return T(1); }); + invoke_on_all_policies(test_is_heap(), in.begin(), in.end(), comp); +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + invoke_if(exec, [&]() { + is_heap(exec, iter, iter, non_const(std::less<T>())); + is_heap_until(exec, iter, iter, non_const(std::less<T>())); + }); + } +}; + +int +main() +{ + test_is_heap_by_type<float32_t>(std::greater<float32_t>()); + test_is_heap_by_type<WithCmpOp>(std::less<WithCmpOp>()); + test_is_heap_by_type<uint64_t>([](uint64_t x, uint64_t y) { return x % 100 < y % 100; }); + + test_algo_basic_single<int32_t>(run_for_rnd<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp b/pstl/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp new file mode 100644 index 0000000..cb92057 --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp @@ -0,0 +1,175 @@ +// -*- C++ -*- +//===-- lexicographical_compare.pass.cpp ----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <iostream> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_one_policy +{ + + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate> + void + operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Iterator2 begin2, Iterator2 end2, + Predicate pred) + { + const bool expected = std::lexicographical_compare(begin1, end1, begin2, end2, pred); + const bool actual = std::lexicographical_compare(exec, begin1, end1, begin2, end2, pred); + EXPECT_TRUE(actual == expected, "wrong return result from lexicographical compare with predicate"); + } + + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> + void + operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Iterator2 begin2, Iterator2 end2) + { + const bool expected = std::lexicographical_compare(begin1, end1, begin2, end2); + const bool actual = std::lexicographical_compare(exec, begin1, end1, begin2, end2); + EXPECT_TRUE(actual == expected, "wrong return result from lexicographical compare without predicate"); + } +}; + +template <typename T1, typename T2, typename Predicate> +void +test(Predicate pred) +{ + + const std::size_t max_n = 1000000; + Sequence<T1> in1(max_n, [](std::size_t k) { return T1(k); }); + Sequence<T2> in2(2 * max_n, [](std::size_t k) { return T2(k); }); + + std::size_t n2; + + // Test case: Call algorithm's version without predicate. + invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + max_n, in2.cbegin() + 3 * max_n / 10, + in2.cbegin() + 5 * max_n / 10); + + // Test case: If one range is a prefix of another, the shorter range is lexicographically less than the other. + std::size_t max_n2 = max_n / 10; + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n, in2.cbegin(), in2.cbegin() + max_n2, + pred); + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n, in2.begin() + max_n2, + in2.begin() + 3 * max_n2, pred); + + // Test case: If one range is a prefix of another, the shorter range is lexicographically less than the other. + max_n2 = 2 * max_n; + invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + max_n, in2.begin(), in2.begin() + max_n2, + pred); + + for (std::size_t n1 = 0; n1 <= max_n; n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1)) + { + // Test case: If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal. + n2 = n1; + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred); + + n2 = n1; + // Test case: two ranges have different elements and are of the same length (second sequence less than first) + std::size_t ind = n1 / 2; + in2[ind] = T2(-1); + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred); + in2[ind] = T2(ind); + + // Test case: two ranges have different elements and are of the same length (first sequence less than second) + ind = n1 / 5; + in1[ind] = T1(-1); + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.cbegin(), in2.cbegin() + n2, pred); + in1[ind] = T1(ind); + } +} + +template <typename Predicate> +void +test_string(Predicate pred) +{ + + const std::size_t max_n = 1000000; + std::string in1 = ""; + std::string in2 = ""; + for (std::size_t n1 = 0; n1 <= max_n; ++n1) + { + in1 += n1; + } + + for (std::size_t n1 = 0; n1 <= 2 * max_n; ++n1) + { + in2 += n1; + } + + std::size_t n2; + + for (std::size_t n1 = 0; n1 < in1.size(); n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1)) + { + // Test case: If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal. + n2 = n1; + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred); + + n2 = n1; + // Test case: two ranges have different elements and are of the same length (second sequence less than first) + in2[n1 / 2] = 'a'; + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred); + + // Test case: two ranges have different elements and are of the same length (first sequence less than second) + in1[n1 / 5] = 'a'; + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.cbegin(), in2.cbegin() + n2, pred); + } + invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + max_n, in2.cbegin() + 3 * max_n / 10, + in2.cbegin() + 5 * max_n / 10); +} + +template <typename T> +struct LocalWrapper +{ + explicit LocalWrapper(std::size_t k) : my_val(k) {} + bool + operator<(const LocalWrapper<T>& w) const + { + return my_val < w.my_val; + } + + private: + T my_val; +}; + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename FirstIterator, typename SecondInterator> + void + operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) + { + invoke_if(exec, [&]() { + lexicographical_compare(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::less<T>())); + }); + } +}; + +int +main() +{ + test<uint16_t, float64_t>(std::less<float64_t>()); + test<float32_t, int32_t>(std::greater<float32_t>()); +#if !defined(_PSTL_ICC_18_TEST_EARLY_EXIT_AVX_RELEASE_BROKEN) + test<float64_t, int32_t>([](const float64_t x, const int32_t y) { return x * x < y * y; }); +#endif + test<LocalWrapper<int32_t>, LocalWrapper<int32_t>>( + [](const LocalWrapper<int32_t>& x, const LocalWrapper<int32_t>& y) { return x < y; }); + test_string([](const char x, const char y) { return x < y; }); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/alg.min.max/minmax_element.pass.cpp b/pstl/test/std/algorithms/alg.sorting/alg.min.max/minmax_element.pass.cpp new file mode 100644 index 0000000..715b250 --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/alg.min.max/minmax_element.pass.cpp @@ -0,0 +1,192 @@ +// -*- C++ -*- +//===-- minmax_element.pass.cpp -------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> +#include <set> +#include <cmath> + +#include "support/utils.h" + +using namespace TestUtils; + +struct check_minelement +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator begin, Iterator end) + { + typedef typename std::iterator_traits<Iterator>::value_type T; + const Iterator expect = std::min_element(begin, end); + const Iterator result = std::min_element(exec, begin, end); + const Iterator result_pred = std::min_element(exec, begin, end, std::less<T>()); + EXPECT_TRUE(expect == result, "wrong return result from min_element"); + EXPECT_TRUE(expect == result_pred, "wrong return result from min_element"); + } +}; + +struct check_maxelement +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator begin, Iterator end) + { + typedef typename std::iterator_traits<Iterator>::value_type T; + const Iterator expect = std::max_element(begin, end); + const Iterator result = std::max_element(exec, begin, end); + const Iterator result_pred = std::max_element(exec, begin, end, std::less<T>()); + EXPECT_TRUE(expect == result, "wrong return result from max_element"); + EXPECT_TRUE(expect == result_pred, "wrong return result from max_element"); + } +}; + +struct check_minmaxelement +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator begin, Iterator end) + { + typedef typename std::iterator_traits<Iterator>::value_type T; + const std::pair<Iterator, Iterator> expect = std::minmax_element(begin, end); + const std::pair<Iterator, Iterator> got = std::minmax_element(exec, begin, end); + const std::pair<Iterator, Iterator> got_pred = std::minmax_element(exec, begin, end, std::less<T>()); + EXPECT_TRUE(expect.first == got.first, "wrong return result from minmax_element (min part)"); + EXPECT_TRUE(expect.second == got.second, "wrong return result from minmax_element (max part)"); + EXPECT_TRUE(expect == got_pred, "wrong return result from minmax_element"); + } +}; + +template <typename T> +struct sequence_wrapper +{ + TestUtils::Sequence<T> seq; + const T min_value; + const T max_value; + static const std::size_t bits = 30; // We assume that T can handle signed 2^bits+1 value + + // TestUtils::HashBits returns value between 0 and (1<<bits)-1, + // therefore we could threat 1<<bits as maximum and -(1<<bits) as a minimum + sequence_wrapper(std::size_t n) : seq(n), min_value(-(1 << bits)), max_value(1 << bits) {} + + void + pattern_fill() + { + seq.fill([](std::size_t i) -> T { return T(TestUtils::HashBits(i, bits)); }); + } + + // sets first one at position `at` and bunch of them farther + void + set_desired_value(std::size_t at, T value) + { + if (seq.size() == 0) + return; + seq[at] = value; + + //Producing serveral red herrings + for (std::size_t i = at + 1; i < seq.size(); i += 1 + TestUtils::HashBits(i, 5)) + seq[i] = value; + } +}; + +template <typename T> +void +test_by_type(std::size_t n) +{ + sequence_wrapper<T> wseq(n); + + // to avoid overtesing we use std::set to leave only unique indexes + std::set<std::size_t> targets{0}; + if (n > 1) + { + targets.insert(1); + targets.insert(2.718282 * n / 3); + targets.insert(n / 2); + targets.insert(n / 7.389056); + targets.insert(n - 1); // last + } + + for (std::set<std::size_t>::iterator it = targets.begin(); it != targets.end(); ++it) + { + wseq.pattern_fill(); + wseq.set_desired_value(*it, wseq.min_value); + TestUtils::invoke_on_all_policies(check_minelement(), wseq.seq.cbegin(), wseq.seq.cend()); + TestUtils::invoke_on_all_policies(check_minelement(), wseq.seq.begin(), wseq.seq.end()); + + wseq.set_desired_value(*it, wseq.max_value); + TestUtils::invoke_on_all_policies(check_maxelement(), wseq.seq.cbegin(), wseq.seq.cend()); + TestUtils::invoke_on_all_policies(check_maxelement(), wseq.seq.begin(), wseq.seq.end()); + + if (targets.size() > 1) + { + for (std::set<std::size_t>::reverse_iterator rit = targets.rbegin(); rit != targets.rend(); ++rit) + { + if (*rit == *it) // we requires at least 2 unique indexes in targets + break; + wseq.pattern_fill(); + wseq.set_desired_value(*it, wseq.min_value); // setting minimum element + wseq.set_desired_value(*rit, wseq.max_value); // setting maximum element + TestUtils::invoke_on_all_policies(check_minmaxelement(), wseq.seq.cbegin(), wseq.seq.cend()); + TestUtils::invoke_on_all_policies(check_minmaxelement(), wseq.seq.begin(), wseq.seq.end()); + } + } + else + { // we must check this corner case; it can not be tested in loop above + TestUtils::invoke_on_all_policies(check_minmaxelement(), wseq.seq.cbegin(), wseq.seq.cend()); + TestUtils::invoke_on_all_policies(check_minmaxelement(), wseq.seq.begin(), wseq.seq.end()); + } + } +} + +// should provide minimal requirements only +struct OnlyLessCompare +{ + int32_t val; + OnlyLessCompare() : val(0) {} + OnlyLessCompare(int32_t val_) : val(val_) {} + bool + operator<(const OnlyLessCompare& other) const + { + return val < other.val; + } +}; + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + max_element(exec, iter, iter, non_const(std::less<T>())); + min_element(exec, iter, iter, non_const(std::less<T>())); + minmax_element(exec, iter, iter, non_const(std::less<T>())); + } +}; + +int +main() +{ + using TestUtils::float64_t; + const std::size_t N = 100000; + + for (std::size_t n = 0; n < N; n = n < 16 ? n + 1 : size_t(3.14159 * n)) + { + test_by_type<float64_t>(n); + test_by_type<OnlyLessCompare>(n); + } + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << TestUtils::done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/alg.set.operations/includes.pass.cpp b/pstl/test/std/algorithms/alg.sorting/alg.set.operations/includes.pass.cpp new file mode 100644 index 0000000..778d265 --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/alg.set.operations/includes.pass.cpp @@ -0,0 +1,106 @@ +// -*- C++ -*- +//===-- includes.pass.cpp -------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <algorithm> +#include <cmath> +#include <execution> +#include <functional> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct Num +{ + T val; + explicit Num(const T& v) : val(v) {} + + //for "includes" checks + template <typename T1> + bool + operator<(const Num<T1>& v1) const + { + return val < v1.val; + } + + //The types Type1 and Type2 must be such that an object of type InputIt can be dereferenced and then implicitly converted to both of them + template <typename T1> + operator Num<T1>() const + { + return Num<T1>((T1)val); + } +}; + +struct test_one_policy +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<!TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, + Compare comp) + { + + auto expect_res = std::includes(first1, last1, first2, last2, comp); + auto res = std::includes(exec, first1, last1, first2, last2, comp); + + EXPECT_TRUE(expect_res == res, "wrong result for includes"); + } + + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare) + { + } +}; + +template <typename T1, typename T2, typename Compare> +void +test_includes(Compare compare) +{ + + const std::size_t n_max = 1000000; + + // The rand()%(2*n+1) encourages generation of some duplicates. + std::srand(42); + + for (std::size_t n = 0; n < n_max; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + for (std::size_t m = 0; m < n_max; m = m <= 16 ? m + 1 : size_t(2.71828 * m)) + { + //prepare the input ranges + Sequence<T1> in1(n, [](std::size_t k) { return rand() % (2 * k + 1); }); + Sequence<T2> in2(m, [](std::size_t k) { return rand() % (k + 1); }); + + std::sort(in1.begin(), in1.end(), compare); + std::sort(in2.begin(), in2.end(), compare); + + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), compare); + + //test w/ non constant predicate + if (n < 5 && m < 5) + invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), + non_const(compare)); + } + } +} + +int +main() +{ + + test_includes<float64_t, float64_t>(std::less<>()); + test_includes<Num<int64_t>, Num<int32_t>>([](const Num<int64_t>& x, const Num<int32_t>& y) { return x < y; }); + std::cout << done() << std::endl; + + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/alg.set.operations/set.pass.cpp b/pstl/test/std/algorithms/alg.sorting/alg.set.operations/set.pass.cpp new file mode 100644 index 0000000..40ae3d9 --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/alg.set.operations/set.pass.cpp @@ -0,0 +1,280 @@ +// -*- C++ -*- +//===-- set.pass.cpp ------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <algorithm> +#include <chrono> +#include <cmath> +#include <execution> +#include <functional> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct Num +{ + T val; + + Num() : val{} {} + Num(const T& v) : val(v) {} + + //for "includes" checks + template <typename T1> + bool + operator<(const Num<T1>& v1) const + { + return val < v1.val; + } + + //The types Type1 and Type2 must be such that an object of type InputIt can be dereferenced and then implicitly converted to both of them + template <typename T1> + operator Num<T1>() const + { + return Num<T1>((T1)val); + } + + friend bool + operator==(const Num& v1, const Num& v2) + { + return v1.val == v2.val; + } +}; + +template <typename Type> +struct test_set_union +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<!TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, + Compare comp) + { + using T1 = typename std::iterator_traits<InputIterator1>::value_type; + + auto n1 = std::distance(first1, last1); + auto n2 = std::distance(first2, last2); + auto n = n1 + n2; + Sequence<T1> expect(n); + Sequence<T1> out(n); + + auto expect_res = std::set_union(first1, last1, first2, last2, expect.begin(), comp); + auto res = std::set_union(exec, first1, last1, first2, last2, out.begin(), comp); + + EXPECT_TRUE(expect_res - expect.begin() == res - out.begin(), "wrong result for set_union"); + EXPECT_EQ_N(expect.begin(), out.begin(), std::distance(out.begin(), res), "wrong set_union effect"); + } + + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare) + { + } +}; + +template <typename Type> +struct test_set_intersection +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<!TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, + Compare comp) + { + using T1 = typename std::iterator_traits<InputIterator1>::value_type; + + auto n1 = std::distance(first1, last1); + auto n2 = std::distance(first2, last2); + auto n = n1 + n2; + Sequence<T1> expect(n); + Sequence<T1> out(n); + + auto expect_res = std::set_intersection(first1, last1, first2, last2, expect.begin(), comp); + auto res = std::set_intersection(exec, first1, last1, first2, last2, out.begin(), comp); + + EXPECT_TRUE(expect_res - expect.begin() == res - out.begin(), "wrong result for set_intersection"); + EXPECT_EQ_N(expect.begin(), out.begin(), std::distance(out.begin(), res), "wrong set_intersection effect"); + } + + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare) + { + } +}; + +template <typename Type> +struct test_set_difference +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<!TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, + Compare comp) + { + using T1 = typename std::iterator_traits<InputIterator1>::value_type; + + auto n1 = std::distance(first1, last1); + auto n2 = std::distance(first2, last2); + auto n = n1 + n2; + Sequence<T1> expect(n); + Sequence<T1> out(n); + + auto expect_res = std::set_difference(first1, last1, first2, last2, expect.begin(), comp); + auto res = std::set_difference(exec, first1, last1, first2, last2, out.begin(), comp); + + EXPECT_TRUE(expect_res - expect.begin() == res - out.begin(), "wrong result for set_difference"); + EXPECT_EQ_N(expect.begin(), out.begin(), std::distance(out.begin(), res), "wrong set_difference effect"); + } + + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare) + { + } +}; + +template <typename Type> +struct test_set_symmetric_difference +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<!TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, + Compare comp) + { + using T1 = typename std::iterator_traits<InputIterator1>::value_type; + + auto n1 = std::distance(first1, last1); + auto n2 = std::distance(first2, last2); + auto n = n1 + n2; + Sequence<T1> expect(n); + Sequence<T1> out(n); + + auto expect_res = std::set_symmetric_difference(first1, last1, first2, last2, expect.begin(), comp); + auto res = std::set_symmetric_difference(exec, first1, last1, first2, last2, out.begin(), comp); + + EXPECT_TRUE(expect_res - expect.begin() == res - out.begin(), "wrong result for set_symmetric_difference"); + EXPECT_EQ_N(expect.begin(), out.begin(), std::distance(out.begin(), res), + "wrong set_symmetric_difference effect"); + } + + template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare> + typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type + operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare) + { + } +}; + +template <typename T1, typename T2, typename Compare> +void +test_set(Compare compare) +{ + + const std::size_t n_max = 100000; + + // The rand()%(2*n+1) encourages generation of some duplicates. + std::srand(4200); + + for (std::size_t n = 0; n < n_max; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + for (std::size_t m = 0; m < n_max; m = m <= 16 ? m + 1 : size_t(2.71828 * m)) + { + //prepare the input ranges + Sequence<T1> in1(n, [](std::size_t k) { return rand() % (2 * k + 1); }); + Sequence<T2> in2(m, [m](std::size_t k) { return (m % 2) * rand() + rand() % (k + 1); }); + + std::sort(in1.begin(), in1.end(), compare); + std::sort(in2.begin(), in2.end(), compare); + + invoke_on_all_policies(test_set_union<T1>(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), + compare); + + invoke_on_all_policies(test_set_intersection<T1>(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), + compare); + + invoke_on_all_policies(test_set_difference<T1>(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), + compare); + + invoke_on_all_policies(test_set_symmetric_difference<T1>(), in1.begin(), in1.end(), in2.cbegin(), + in2.cend(), compare); + } + } +} + +template <typename T> +struct test_non_const_set_difference +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + set_difference(exec, input_iter, input_iter, input_iter, input_iter, out_iter, non_const(std::less<T>())); + } +}; + +template <typename T> +struct test_non_const_set_intersection +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + set_intersection(exec, input_iter, input_iter, input_iter, input_iter, out_iter, non_const(std::less<T>())); + } +}; + +template <typename T> +struct test_non_const_set_symmetric_difference +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + set_symmetric_difference(exec, input_iter, input_iter, input_iter, input_iter, out_iter, + non_const(std::less<T>())); + } +}; + +template <typename T> +struct test_non_const_set_union +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + set_union(exec, input_iter, input_iter, input_iter, input_iter, out_iter, non_const(std::less<T>())); + } +}; + +int +main() +{ + + test_set<float64_t, float64_t>(std::less<>()); + test_set<Num<int64_t>, Num<int32_t>>([](const Num<int64_t>& x, const Num<int32_t>& y) { return x < y; }); + + test_set<MemoryChecker, MemoryChecker>([](const MemoryChecker& val1, const MemoryChecker& val2) -> bool { + return val1.value() < val2.value(); + }); + EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from set algorithms: number of ctors calls < num of dtors calls"); + EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from set algorithms: number of ctors calls > num of dtors calls"); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const_set_difference<int32_t>>()); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const_set_intersection<int32_t>>()); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const_set_symmetric_difference<int32_t>>()); + + test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const_set_union<int32_t>>()); + + std::cout << done() << std::endl; + + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/is_sorted.pass.cpp b/pstl/test/std/algorithms/alg.sorting/is_sorted.pass.cpp new file mode 100644 index 0000000..d4c6fe2 --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/is_sorted.pass.cpp @@ -0,0 +1,100 @@ +// -*- C++ -*- +//===-- is_sorted.pass.cpp ------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_is_sorted +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator first, Iterator last, bool exam) + { + using namespace std; + typedef typename std::iterator_traits<Iterator>::value_type T; + + //try random-access iterator + bool res = is_sorted(exec, first, last); + EXPECT_TRUE(exam == res, "is_sorted wrong result for random-access iterator"); + auto iexam = is_sorted_until(first, last); + auto ires = is_sorted_until(exec, first, last); + EXPECT_TRUE(iexam == ires, "is_sorted_until wrong result for random-access iterator"); + + //try random-access iterator with a predicate + res = is_sorted(exec, first, last, std::less<T>()); + EXPECT_TRUE(exam == res, "is_sorted wrong result for random-access iterator"); + iexam = is_sorted_until(first, last, std::less<T>()); + ires = is_sorted_until(exec, first, last, std::less<T>()); + EXPECT_TRUE(iexam == ires, "is_sorted_until wrong result for random-access iterator"); + } +}; + +template <typename T> +void +test_is_sorted_by_type() +{ + + Sequence<T> in(99999, [](size_t v) -> T { return T(v); }); //fill 0..n + + invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); + invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); + + in[in.size() / 2] = -1; + invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); + invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); + + in[1] = -1; + invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); + invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); + + //an empty container + Sequence<T> in0(0); + invoke_on_all_policies(test_is_sorted(), in0.begin(), in0.end(), std::is_sorted(in0.begin(), in0.end())); + invoke_on_all_policies(test_is_sorted(), in0.cbegin(), in0.cend(), std::is_sorted(in0.begin(), in0.end())); + + //non-descending order + Sequence<T> in1(9, [](size_t) -> T { return T(0); }); + invoke_on_all_policies(test_is_sorted(), in1.begin(), in1.end(), std::is_sorted(in1.begin(), in1.end())); + invoke_on_all_policies(test_is_sorted(), in1.cbegin(), in1.cend(), std::is_sorted(in1.begin(), in1.end())); +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + is_sorted(exec, iter, iter, std::less<T>()); + is_sorted_until(exec, iter, iter, std::less<T>()); + } +}; + +int +main() +{ + + test_is_sorted_by_type<int32_t>(); + test_is_sorted_by_type<float64_t>(); + + test_is_sorted_by_type<Wrapper<int32_t>>(); + + test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/partial_sort.pass.cpp b/pstl/test/std/algorithms/alg.sorting/partial_sort.pass.cpp new file mode 100644 index 0000000..bf6e68b --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/partial_sort.pass.cpp @@ -0,0 +1,149 @@ +// -*- C++ -*- +//===-- partial_sort.pass.cpp ---------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <cmath> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +static std::atomic<int32_t> count_val; +static std::atomic<int32_t> count_comp; + +template <typename T> +struct Num +{ + T val; + + Num() { ++count_val; } + Num(T v) : val(v) { ++count_val; } + Num(const Num<T>& v) : val(v.val) { ++count_val; } + Num(Num<T>&& v) : val(v.val) { ++count_val; } + ~Num() { --count_val; } + Num<T>& + operator=(const Num<T>& v) + { + val = v.val; + return *this; + } + operator T() const { return val; } + bool + operator<(const Num<T>& v) const + { + ++count_comp; + return val < v.val; + } +}; + +struct test_brick_partial_sort +{ + template <typename Policy, typename InputIterator, typename Compare> + typename std::enable_if<is_same_iterator_category<InputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&& exec, InputIterator first, InputIterator last, InputIterator exp_first, InputIterator exp_last, + Compare compare) + { + + typedef typename std::iterator_traits<InputIterator>::value_type T; + + // The rand()%(2*n+1) encourages generation of some duplicates. + std::srand(42); + const std::size_t n = last - first; + for (std::size_t k = 0; k < n; ++k) + { + first[k] = T(rand() % (2 * n + 1)); + } + std::copy(first, last, exp_first); + + for (std::size_t p = 0; p < n; p = p <= 16 ? p + 1 : std::size_t(31.415 * p)) + { + auto m1 = first + p; + auto m2 = exp_first + p; + + std::partial_sort(exp_first, m2, exp_last, compare); + count_comp = 0; + std::partial_sort(exec, first, m1, last, compare); + EXPECT_EQ_N(exp_first, first, p, "wrong effect from partial_sort"); + + //checking upper bound number of comparisons; O(p*(last-first)log(middle-first)); where p - number of threads; + if (m1 - first > 1) + { +#ifdef _DEBUG +# if defined(_PSTL_PAR_BACKEND_TBB) + auto p = tbb::this_task_arena::max_concurrency(); +# else + auto p = 1; +# endif + auto complex = std::ceil(n * std::log(float32_t(m1 - first))); + if (count_comp > complex * p) + { + std::cout << "complexity exceeded" << std::endl; + } +#endif // _DEBUG + } + } + } + + template <typename Policy, typename InputIterator, typename Compare> + typename std::enable_if<!is_same_iterator_category<InputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&&, InputIterator, InputIterator, InputIterator, InputIterator, Compare) + { + } +}; + +template <typename T, typename Compare> +void +test_partial_sort(Compare compare) +{ + + const std::size_t n_max = 100000; + Sequence<T> in(n_max); + Sequence<T> exp(n_max); + for (std::size_t n = 0; n < n_max; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + invoke_on_all_policies(test_brick_partial_sort(), in.begin(), in.begin() + n, exp.begin(), exp.begin() + n, + compare); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + partial_sort(exec, iter, iter, iter, non_const(std::less<T>())); + } +}; + +int +main() +{ + count_val = 0; + + test_partial_sort<Num<float32_t>>([](Num<float32_t> x, Num<float32_t> y) { return x < y; }); + + EXPECT_TRUE(count_val == 0, "cleanup error"); + + test_partial_sort<int32_t>( + [](int32_t x, int32_t y) { return x > y; }); // Reversed so accidental use of < will be detected. + + test_algo_basic_single<int32_t>(run_for_rnd<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/partial_sort_copy.pass.cpp b/pstl/test/std/algorithms/alg.sorting/partial_sort_copy.pass.cpp new file mode 100644 index 0000000..9090f89 --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/partial_sort_copy.pass.cpp @@ -0,0 +1,196 @@ +// -*- C++ -*- +//===-- partial_sort_copy.pass.cpp ----------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for partial_sort_copy +#include "support/pstl_test_config.h" + +#include <cmath> +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct Num +{ + T val; + + Num() : val(0) {} + Num(T v) : val(v) {} + Num(const Num<T>& v) : val(v.val) {} + Num(Num<T>&& v) : val(v.val) {} + Num<T>& + operator=(const Num<T>& v) + { + val = v.val; + return *this; + } + operator T() const { return val; } + bool + operator<(const Num<T>& v) const + { + return val < v.val; + } +}; + +template <typename RandomAccessIterator> +struct test_one_policy +{ + RandomAccessIterator d_first; + RandomAccessIterator d_last; + RandomAccessIterator exp_first; + RandomAccessIterator exp_last; + // This ctor is needed because output shouldn't be transformed to any iterator type (only random access iterators are allowed) + test_one_policy(RandomAccessIterator b1, RandomAccessIterator e1, RandomAccessIterator b2, RandomAccessIterator e2) + : d_first(b1), d_last(e1), exp_first(b2), exp_last(e2) + { + } +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration + template <typename InputIterator, typename Size, typename T, typename Compare> + void + operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, Size n1, Size n2, + const T& trash, Compare compare) + { + } + + template <typename InputIterator, typename Size, typename T, typename Compare> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, Size n1, Size n2, + const T& trash, Compare compare) + { + } + + template <typename InputIterator, typename Size, typename T> + void + operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, Size n1, Size n2, + const T& trash) + { + } + + template <typename InputIterator, typename Size, typename T> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, Size n1, Size n2, + const T& trash) + { + } +#endif + + template <typename Policy, typename InputIterator, typename Size, typename T, typename Compare> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, Size n1, Size n2, const T& trash, + Compare compare) + { + prepare_data(first, last, n1, trash); + RandomAccessIterator exp = std::partial_sort_copy(first, last, exp_first, exp_last, compare); + RandomAccessIterator res = std::partial_sort_copy(exec, first, last, d_first, d_last, compare); + + EXPECT_TRUE((exp - exp_first) == (res - d_first), "wrong result from partial_sort_copy with predicate"); + EXPECT_EQ_N(exp_first, d_first, n2, "wrong effect from partial_sort_copy with predicate"); + } + + template <typename Policy, typename InputIterator, typename Size, typename T> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, Size n1, Size n2, const T& trash) + { + prepare_data(first, last, n1, trash); + RandomAccessIterator exp = std::partial_sort_copy(first, last, exp_first, exp_last); + RandomAccessIterator res = std::partial_sort_copy(exec, first, last, d_first, d_last); + + EXPECT_TRUE((exp - exp_first) == (res - d_first), "wrong result from partial_sort_copy without predicate"); + EXPECT_EQ_N(exp_first, d_first, n2, "wrong effect from partial_sort_copy without predicate"); + } + + private: + template <typename InputIterator, typename Size, typename T> + void + prepare_data(InputIterator first, InputIterator last, Size n1, const T& trash) + { + // The rand()%(2*n+1) encourages generation of some duplicates. + std::srand(42); + std::generate(first, last, [n1]() { return T(rand() % (2 * n1 + 1)); }); + + std::fill(exp_first, exp_last, trash); + std::fill(d_first, d_last, trash); + } +}; + +template <typename T, typename Compare> +void +test_partial_sort_copy(Compare compare) +{ + + typedef typename Sequence<T>::iterator iterator_type; + const std::size_t n_max = 100000; + Sequence<T> in(n_max); + Sequence<T> out(2 * n_max); + Sequence<T> exp(2 * n_max); + std::size_t n1 = 0; + std::size_t n2; + T trash = T(-666); + for (; n1 < n_max; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1)) + { + // If both sequences are equal + n2 = n1; + invoke_on_all_policies( + test_one_policy<iterator_type>(out.begin(), out.begin() + n2, exp.begin(), exp.begin() + n2), in.begin(), + in.begin() + n1, n1, n2, trash, compare); + + // If first sequence is greater than second + n2 = n1 / 3; + invoke_on_all_policies( + test_one_policy<iterator_type>(out.begin(), out.begin() + n2, exp.begin(), exp.begin() + n2), in.begin(), + in.begin() + n1, n1, n2, trash, compare); + + // If first sequence is less than second + n2 = 2 * n1; + invoke_on_all_policies( + test_one_policy<iterator_type>(out.begin(), out.begin() + n2, exp.begin(), exp.begin() + n2), in.begin(), + in.begin() + n1, n1, n2, trash, compare); + } + // Test partial_sort_copy without predicate + n1 = n_max; + n2 = 2 * n1; + invoke_on_all_policies(test_one_policy<iterator_type>(out.begin(), out.begin() + n2, exp.begin(), exp.begin() + n2), + in.begin(), in.begin() + n1, n1, n2, trash); +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename InputIterator, typename OutputInterator> + void + operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) + { + invoke_if(exec, [&]() { + partial_sort_copy(exec, input_iter, input_iter, out_iter, out_iter, non_const(std::less<T>())); + }); + } +}; + +int +main() +{ + test_partial_sort_copy<Num<float32_t>>([](Num<float32_t> x, Num<float32_t> y) { return x < y; }); + test_partial_sort_copy<int32_t>([](int32_t x, int32_t y) { return x > y; }); + + test_algo_basic_double<int32_t>(run_for_rnd<test_non_const<int32_t>>()); + + test_partial_sort_copy<MemoryChecker>( + [](const MemoryChecker& val1, const MemoryChecker& val2){ return val1.value() < val2.value(); }); + EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from partial_sort_copy: number of ctors calls < num of dtors calls"); + EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from partial_sort_copy: number of ctors calls > num of dtors calls"); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/algorithms/alg.sorting/sort.pass.cpp b/pstl/test/std/algorithms/alg.sorting/sort.pass.cpp new file mode 100644 index 0000000..5617a60 --- /dev/null +++ b/pstl/test/std/algorithms/alg.sorting/sort.pass.cpp @@ -0,0 +1,247 @@ +// -*- C++ -*- +//===-- sort.pass.cpp -----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <algorithm> + +#include "support/utils.h" + +using namespace TestUtils; +#define _CRT_SECURE_NO_WARNINGS + +#include <atomic> + +static bool Stable; + +//! Number of extant keys +static std::atomic<int32_t> KeyCount; + +//! One more than highest index in array to be sorted. +static uint32_t LastIndex; + +//! Keeping Equal() static and a friend of ParanoidKey class (C++, paragraphs 3.5/7.1.1) +class ParanoidKey; +static bool +Equal(const ParanoidKey& x, const ParanoidKey& y); + +//! A key to be sorted, with lots of checking. +class ParanoidKey +{ + //! Value used by comparator + int32_t value; + //! Original position or special value (Empty or Dead) + int32_t index; + //! Special value used to mark object without a comparable value, e.g. after being moved from. + static const int32_t Empty = -1; + //! Special value used to mark destroyed objects. + static const int32_t Dead = -2; + // True if key object has comparable value + bool + isLive() const + { + return (uint32_t)(index) < LastIndex; + } + // True if key object has been constructed. + bool + isConstructed() const + { + return isLive() || index == Empty; + } + + public: + ParanoidKey() + { + ++KeyCount; + index = Empty; + value = Empty; + } + ParanoidKey(const ParanoidKey& k) : value(k.value), index(k.index) + { + EXPECT_TRUE(k.isLive(), "source for copy-constructor is dead"); + ++KeyCount; + } + ~ParanoidKey() + { + EXPECT_TRUE(isConstructed(), "double destruction"); + index = Dead; + --KeyCount; + } + ParanoidKey& + operator=(const ParanoidKey& k) + { + EXPECT_TRUE(k.isLive(), "source for copy-assignment is dead"); + EXPECT_TRUE(isConstructed(), "destination for copy-assignment is dead"); + value = k.value; + index = k.index; + return *this; + } + ParanoidKey(int32_t index, int32_t value, OddTag) : value(value), index(index) {} + ParanoidKey(ParanoidKey&& k) : value(k.value), index(k.index) + { + EXPECT_TRUE(k.isConstructed(), "source for move-construction is dead"); +// std::stable_sort() fails in move semantics on paranoid test before VS2015 +#if !defined(_MSC_VER) || _MSC_VER >= 1900 + k.index = Empty; +#endif + ++KeyCount; + } + ParanoidKey& + operator=(ParanoidKey&& k) + { + EXPECT_TRUE(k.isConstructed(), "source for move-assignment is dead"); + EXPECT_TRUE(isConstructed(), "destination for move-assignment is dead"); + value = k.value; + index = k.index; +// std::stable_sort() fails in move semantics on paranoid test before VS2015 +#if !defined(_MSC_VER) || _MSC_VER >= 1900 + k.index = Empty; +#endif + return *this; + } + friend class KeyCompare; + friend bool + Equal(const ParanoidKey& x, const ParanoidKey& y); +}; + +class KeyCompare +{ + enum statusType + { + //! Special value used to mark defined object. + Live = 0xabcd, + //! Special value used to mark destroyed objects. + Dead = -1 + } status; + + public: + KeyCompare(OddTag) : status(Live) {} + ~KeyCompare() { status = Dead; } + bool + operator()(const ParanoidKey& j, const ParanoidKey& k) const + { + EXPECT_TRUE(status == Live, "key comparison object not defined"); + EXPECT_TRUE(j.isLive(), "first key to operator() is not live"); + EXPECT_TRUE(k.isLive(), "second key to operator() is not live"); + return j.value < k.value; + } +}; + +// Equal is equality comparison used for checking result of sort against expected result. +static bool +Equal(const ParanoidKey& x, const ParanoidKey& y) +{ + return (x.value == y.value && !Stable) || (x.index == y.index); +} + +static bool +Equal(float32_t x, float32_t y) +{ + return x == y; +} + +static bool +Equal(int32_t x, int32_t y) +{ + return x == y; +} + +struct test_sort_with_compare +{ + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Compare> + typename std::enable_if<is_same_iterator_category<InputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&& exec, OutputIterator tmp_first, OutputIterator tmp_last, OutputIterator2 expected_first, + OutputIterator2 expected_last, InputIterator first, InputIterator, Size n, Compare compare) + { + using namespace std; + copy_n(first, n, expected_first); + copy_n(first, n, tmp_first); + if (Stable) + std::stable_sort(expected_first + 1, expected_last - 1, compare); + else + std::sort(expected_first + 1, expected_last - 1, compare); + int32_t count0 = KeyCount; + if (Stable) + stable_sort(exec, tmp_first + 1, tmp_last - 1, compare); + else + sort(exec, tmp_first + 1, tmp_last - 1, compare); + + for (size_t i = 0; i < n; ++i, ++expected_first, ++tmp_first) + { + // Check that expected[i] is equal to tmp[i] + EXPECT_TRUE(Equal(*expected_first, *tmp_first), "bad sort"); + } + int32_t count1 = KeyCount; + EXPECT_EQ(count0, count1, "key cleanup error"); + } + template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, + typename Compare> + typename std::enable_if<!is_same_iterator_category<InputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&&, OutputIterator, OutputIterator, OutputIterator2, OutputIterator2, InputIterator, InputIterator, + Size, Compare) + { + } +}; + +template <typename T, typename Compare, typename Convert> +void +test_sort(Compare compare, Convert convert) +{ + for (size_t n = 0; n < 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + LastIndex = n + 2; + // The rand()%(2*n+1) encourages generation of some duplicates. + // Sequence is padded with an extra element at front and back, to detect overwrite bugs. + Sequence<T> in(n + 2, [=](size_t k) { return convert(k, rand() % (2 * n + 1)); }); + Sequence<T> expected(in); + Sequence<T> tmp(in); + invoke_on_all_policies(test_sort_with_compare(), tmp.begin(), tmp.end(), expected.begin(), expected.end(), + in.begin(), in.end(), in.size(), compare); + } +} + +template <typename T> +struct test_non_const +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator iter) + { + sort(exec, iter, iter, non_const(std::less<T>())); + stable_sort(exec, iter, iter, non_const(std::less<T>())); + } +}; + +int +main() +{ + std::srand(42); + for (int32_t kind = 0; kind < 2; ++kind) + { + Stable = kind != 0; + test_sort<ParanoidKey>(KeyCompare(OddTag()), + [](size_t k, size_t val) { return ParanoidKey(k, val, OddTag()); }); + test_sort<float32_t>([](float32_t x, float32_t y) { return x < y; }, + [](size_t, size_t val) { return float32_t(val); }); + test_sort<int32_t>( + [](int32_t x, int32_t y) { return x > y; }, // Reversed so accidental use of < will be detected. + [](size_t, size_t val) { return int32_t(val); }); + } + + test_algo_basic_single<int32_t>(run_for_rnd<test_non_const<int32_t>>()); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/lit.local.cfg b/pstl/test/std/lit.local.cfg new file mode 100644 index 0000000..f259d8f --- /dev/null +++ b/pstl/test/std/lit.local.cfg @@ -0,0 +1,2 @@ +if "parallel-algorithms" not in config.available_features: + config.unsupported = True diff --git a/pstl/test/std/numerics/numeric.ops/adjacent_difference.pass.cpp b/pstl/test/std/numerics/numeric.ops/adjacent_difference.pass.cpp new file mode 100644 index 0000000..8e56f61 --- /dev/null +++ b/pstl/test/std/numerics/numeric.ops/adjacent_difference.pass.cpp @@ -0,0 +1,170 @@ +// -*- C++ -*- +//===-- adjacent_difference.pass.cpp --------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <iterator> +#include <execution> +#include <numeric> + +#include "support/utils.h" + +using namespace TestUtils; + +template <typename T> +struct wrapper +{ + T t; + constexpr explicit wrapper(T t_) : t(t_) {} + template <typename T2> + constexpr wrapper(const wrapper<T2>& a) + { + t = a.t; + } + template <typename T2> + constexpr void + operator=(const wrapper<T2>& a) + { + t = a.t; + } + constexpr wrapper<T> + operator-(const wrapper<T>& a) const + { + return wrapper<T>(t - a.t); + } +}; + +template <typename T> +bool +compare(const T& a, const T& b) +{ + return a == b; +} + +template <typename T> +bool +compare(const wrapper<T>& a, const wrapper<T>& b) +{ + return a.t == b.t; +} + +template <typename Iterator1, typename Iterator2, typename T, typename Function> +typename std::enable_if<!std::is_floating_point<T>::value, bool>::type +compute_and_check(Iterator1 first, Iterator1 last, Iterator2 d_first, T, Function f) +{ + using T2 = typename std::iterator_traits<Iterator2>::value_type; + + if (first == last) + return true; + + { + T2 temp(*first); + if (!compare(temp, *d_first)) + return false; + } + Iterator1 second = std::next(first); + + ++d_first; + for (; second != last; ++first, ++second, ++d_first) + { + T2 temp(f(*second, *first)); + if (!compare(temp, *d_first)) + return false; + } + + return true; +} + +// we don't want to check equality here +// because we can't be sure it will be strictly equal for floating point types +template <typename Iterator1, typename Iterator2, typename T, typename Function> +typename std::enable_if<std::is_floating_point<T>::value, bool>::type compute_and_check(Iterator1, Iterator1, Iterator2, + T, Function) +{ + return true; +} + +struct test_one_policy +{ +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ + defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) // dummy specialization by policy type, in case of broken configuration + template <typename Iterator1, typename Iterator2, typename T, typename Function> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, + Iterator2 actual_e, T trash, Function f) + { + } + template <typename Iterator1, typename Iterator2, typename T, typename Function> + typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type + operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, + Iterator2 actual_e, T trash, Function f) + { + } +#endif + + template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename T, typename Function> + void + operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e, + T trash, Function f) + { + using namespace std; + using T2 = typename std::iterator_traits<Iterator1>::value_type; + + fill(actual_b, actual_e, trash); + + Iterator2 actual_return = adjacent_difference(exec, data_b, data_e, actual_b); + EXPECT_TRUE(compute_and_check(data_b, data_e, actual_b, T2(0), std::minus<T2>()), + "wrong effect of adjacent_difference"); + EXPECT_TRUE(actual_return == actual_e, "wrong result of adjacent_difference"); + + fill(actual_b, actual_e, trash); + + actual_return = adjacent_difference(exec, data_b, data_e, actual_b, f); + EXPECT_TRUE(compute_and_check(data_b, data_e, actual_b, T2(0), f), + "wrong effect of adjacent_difference with functor"); + EXPECT_TRUE(actual_return == actual_e, "wrong result of adjacent_difference with functor"); + } +}; + +template <typename T1, typename T2, typename Pred> +void +test(Pred pred) +{ + const std::size_t max_len = 100000; + + static constexpr T2 value = T2(77); + static constexpr T1 trash = T1(31); + + Sequence<T1> actual(max_len, [](std::size_t i) { return T1(i); }); + + Sequence<T2> data(max_len, [](std::size_t i) { return i % 3 == 2 ? T2(i * i) : value; }); + + for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) + { + invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), + actual.begin() + len, trash, pred); + invoke_on_all_policies(test_one_policy(), data.cbegin(), data.cbegin() + len, actual.begin(), + actual.begin() + len, trash, pred); + } +} + +int +main() +{ + test<uint8_t, uint32_t>([](uint32_t a, uint32_t b) { return a - b; }); + test<int32_t, int64_t>([](int64_t a, int64_t b) { return a / (b + 1); }); + test<int64_t, float32_t>([](float32_t a, float32_t b) { return (a + b) / 2; }); + test<wrapper<int32_t>, wrapper<int64_t>>( + [](const wrapper<int64_t>& a, const wrapper<int64_t>& b) { return a - b; }); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/numerics/numeric.ops/reduce.pass.cpp b/pstl/test/std/numerics/numeric.ops/reduce.pass.cpp new file mode 100644 index 0000000..b2144b9 --- /dev/null +++ b/pstl/test/std/numerics/numeric.ops/reduce.pass.cpp @@ -0,0 +1,114 @@ +// -*- C++ -*- +//===-- reduce.pass.cpp ---------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <numeric> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_long_forms_for_one_policy +{ + template <typename Policy, typename Iterator, typename T, typename BinaryOp> + void + operator()(Policy&& exec, Iterator first, Iterator last, T init, BinaryOp binary, T expected) + { + T result_r = std::reduce(exec, first, last, init, binary); + EXPECT_EQ(expected, result_r, "bad result from reduce(exec, first, last, init, binary_op)"); + } +}; + +template <typename T, typename BinaryOp, typename F> +void +test_long_form(T init, BinaryOp binary_op, F f) +{ + // Try sequences of various lengths + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + T expected(init); + Sequence<T> in(n, [n, f](size_t k) { return f((int32_t(k ^ n) % 1000 - 500)); }); + for (size_t k = 0; k < n; ++k) + expected = binary_op(expected, in[k]); + + using namespace std; + + T result = transform_reduce_serial(in.cfbegin(), in.cfend(), init, binary_op, [](const T& t) { return t; }); + EXPECT_EQ(expected, result, "bad result from reduce(first, last, init, binary_op_op)"); + + invoke_on_all_policies(test_long_forms_for_one_policy(), in.begin(), in.end(), init, binary_op, expected); + invoke_on_all_policies(test_long_forms_for_one_policy(), in.cbegin(), in.cend(), init, binary_op, expected); + } +} + +struct test_two_short_forms +{ + +#if defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) //dummy specialization by policy type, in case of broken configuration + template <typename Iterator> + void + operator()(pstl::execution::parallel_policy, Iterator first, Iterator last, Sum init, Sum expected) + { + } + template <typename Iterator> + void + operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Sum init, Sum expected) + { + } +#endif + + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator first, Iterator last, Sum init, Sum expected) + { + using namespace std; + + Sum r0 = init + reduce(exec, first, last); + EXPECT_EQ(expected, r0, "bad result from reduce(exec, first, last)"); + + Sum r1 = reduce(exec, first, last, init); + EXPECT_EQ(expected, r1, "bad result from reduce(exec, first, last, init)"); + } +}; + +// Test forms of reduce(...) that omit the binary_op or init operands. +void +test_short_forms() +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sum init(42, OddTag()); + Sum expected(init); + Sequence<Sum> in(n, [n](size_t k) { return Sum((int32_t(k ^ n) % 1000 - 500), OddTag()); }); + for (size_t k = 0; k < n; ++k) + expected = expected + in[k]; + invoke_on_all_policies(test_two_short_forms(), in.begin(), in.end(), init, expected); + invoke_on_all_policies(test_two_short_forms(), in.cbegin(), in.cend(), init, expected); + } +} + +int +main() +{ + // Test for popular types + test_long_form(42, std::plus<int32_t>(), [](int32_t x) { return x; }); + test_long_form(42.0, std::plus<float64_t>(), [](float64_t x) { return x; }); + + // Test for strict types + test_long_form<Number>(Number(42, OddTag()), Add(OddTag()), [](int32_t x) { return Number(x, OddTag()); }); + + // Short forms are just facade for long forms, so just test with a single type. + test_short_forms(); + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/numerics/numeric.ops/scan.fail.cpp b/pstl/test/std/numerics/numeric.ops/scan.fail.cpp new file mode 100644 index 0000000..b17944a --- /dev/null +++ b/pstl/test/std/numerics/numeric.ops/scan.fail.cpp @@ -0,0 +1,28 @@ +// -*- C++ -*- +//===-- scan.fail.cpp -----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include <execution> +#include <numeric> + +struct CustomPolicy +{ +} policy; + +int32_t +main() +{ + int *first = nullptr, *last = nullptr, *result = nullptr; + + std::exclusive_scan(policy, first, last, result, 0); // expected-error {{no matching function for call to 'exclusive_scan'}} + std::exclusive_scan(policy, first, last, result, 0, std::plus<int>()); // expected-error {{no matching function for call to 'exclusive_scan'}} + + return 0; +} diff --git a/pstl/test/std/numerics/numeric.ops/scan.pass.cpp b/pstl/test/std/numerics/numeric.ops/scan.pass.cpp new file mode 100644 index 0000000..e89edc7 --- /dev/null +++ b/pstl/test/std/numerics/numeric.ops/scan.pass.cpp @@ -0,0 +1,201 @@ +// -*- C++ -*- +//===-- scan.pass.cpp -----------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <numeric> + +#include "support/utils.h" + +using namespace TestUtils; + +// We provide the no execution policy versions of the exclusive_scan and inclusive_scan due checking correctness result of the versions with execution policies. +//TODO: to add a macro for availability of ver implementations +template <class InputIterator, class OutputIterator, class T> +OutputIterator +exclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, T init) +{ + for (; first != last; ++first, ++result) + { + *result = init; + init = init + *first; + } + return result; +} + +template <class InputIterator, class OutputIterator, class T, class BinaryOperation> +OutputIterator +exclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, T init, BinaryOperation binary_op) +{ + for (; first != last; ++first, ++result) + { + *result = init; + init = binary_op(init, *first); + } + return result; +} + +// Note: N4582 is missing the ", class T". Issue was reported 2016-Apr-11 to cxxeditor@gmail.com +template <class InputIterator, class OutputIterator, class BinaryOperation, class T> +OutputIterator +inclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op, T init) +{ + for (; first != last; ++first, ++result) + { + init = binary_op(init, *first); + *result = init; + } + return result; +} + +template <class InputIterator, class OutputIterator, class BinaryOperation> +OutputIterator +inclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op) +{ + if (first != last) + { + auto tmp = *first; + *result = tmp; + return inclusive_scan_serial(++first, last, ++result, binary_op, tmp); + } + else + { + return result; + } +} + +template <class InputIterator, class OutputIterator> +OutputIterator +inclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result) +{ + typedef typename std::iterator_traits<InputIterator>::value_type input_type; + return inclusive_scan_serial(first, last, result, std::plus<input_type>()); +} + +// Most of the framework required for testing inclusive and exclusive scan is identical, +// so the tests for both are in this file. Which is being tested is controlled by the global +// flag inclusive, which is set to each alternative by main(). +static bool inclusive; + +template <typename Iterator, typename Size, typename T> +void +check_and_reset(Iterator expected_first, Iterator out_first, Size n, T trash) +{ + EXPECT_EQ_N(expected_first, out_first, n, + inclusive ? "wrong result from inclusive_scan" : "wrong result from exclusive_scan"); + std::fill_n(out_first, n, trash); +} + +struct test_scan_with_plus +{ + template <typename Policy, typename Iterator1, typename Iterator2, typename Iterator3, typename Size, typename T> + void + operator()(Policy&& exec, Iterator1 in_first, Iterator1 in_last, Iterator2 out_first, Iterator2 out_last, + Iterator3 expected_first, Iterator3, Size n, T init, T trash) + { + using namespace std; + + auto orr1 = inclusive ? inclusive_scan_serial(in_first, in_last, expected_first) + : exclusive_scan_serial(in_first, in_last, expected_first, init); + (void)orr1; + auto orr = inclusive ? inclusive_scan(exec, in_first, in_last, out_first) + : exclusive_scan(exec, in_first, in_last, out_first, init); + EXPECT_TRUE(out_last == orr, + inclusive ? "inclusive_scan returned wrong iterator" : "exclusive_scan returned wrong iterator"); + + check_and_reset(expected_first, out_first, n, trash); + fill(out_first, out_last, trash); + } +}; + +template <typename T, typename Convert> +void +test_with_plus(T init, T trash, Convert convert) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> in(n, convert); + Sequence<T> expected(in); + Sequence<T> out(n, [&](int32_t) { return trash; }); + + invoke_on_all_policies(test_scan_with_plus(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), in.size(), init, trash); + invoke_on_all_policies(test_scan_with_plus(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), + expected.end(), in.size(), init, trash); + } +} +struct test_scan_with_binary_op +{ + template <typename Policy, typename Iterator1, typename Iterator2, typename Iterator3, typename Size, typename T, + typename BinaryOp> + typename std::enable_if<!TestUtils::isReverse<Iterator1>::value, void>::type + operator()(Policy&& exec, Iterator1 in_first, Iterator1 in_last, Iterator2 out_first, Iterator2 out_last, + Iterator3 expected_first, Iterator3, Size n, T init, BinaryOp binary_op, T trash) + { + using namespace std; + + auto orr1 = inclusive ? inclusive_scan_serial(in_first, in_last, expected_first, binary_op, init) + : exclusive_scan_serial(in_first, in_last, expected_first, init, binary_op); + (void)orr1; + auto orr = inclusive ? inclusive_scan(exec, in_first, in_last, out_first, binary_op, init) + : exclusive_scan(exec, in_first, in_last, out_first, init, binary_op); + + EXPECT_TRUE(out_last == orr, "scan returned wrong iterator"); + check_and_reset(expected_first, out_first, n, trash); + } + + template <typename Policy, typename Iterator1, typename Iterator2, typename Iterator3, typename Size, typename T, + typename BinaryOp> + typename std::enable_if<TestUtils::isReverse<Iterator1>::value, void>::type + operator()(Policy&&, Iterator1, Iterator1, Iterator2, Iterator2, Iterator3, Iterator3, Size, T, BinaryOp, T) + { + } +}; + +template <typename In, typename Out, typename BinaryOp> +void +test_matrix(Out init, BinaryOp binary_op, Out trash) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<In> in(n, [](size_t k) { return In(k, k + 1); }); + + Sequence<Out> out(n, [&](size_t) { return trash; }); + Sequence<Out> expected(n, [&](size_t) { return trash; }); + + invoke_on_all_policies(test_scan_with_binary_op(), in.begin(), in.end(), out.begin(), out.end(), + expected.begin(), expected.end(), in.size(), init, binary_op, trash); + invoke_on_all_policies(test_scan_with_binary_op(), in.cbegin(), in.cend(), out.begin(), out.end(), + expected.begin(), expected.end(), in.size(), init, binary_op, trash); + } +} + +int +main() +{ + for (int32_t mode = 0; mode < 2; ++mode) + { + inclusive = mode != 0; +#if !defined(_PSTL_ICC_19_TEST_SIMD_UDS_WINDOWS_RELEASE_BROKEN) + // Test with highly restricted type and associative but not commutative operation + test_matrix<Matrix2x2<int32_t>, Matrix2x2<int32_t>>(Matrix2x2<int32_t>(), multiply_matrix<int32_t>, + Matrix2x2<int32_t>(-666, 666)); +#endif + + // Since the implict "+" forms of the scan delegate to the generic forms, + // there's little point in using a highly restricted type, so just use double. + test_with_plus<float64_t>(inclusive ? 0.0 : -1.0, -666.0, + [](uint32_t k) { return float64_t((k % 991 + 1) ^ (k % 997 + 2)); }); + } + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/numerics/numeric.ops/transform_reduce.pass.cpp b/pstl/test/std/numerics/numeric.ops/transform_reduce.pass.cpp new file mode 100644 index 0000000..f9c4538 --- /dev/null +++ b/pstl/test/std/numerics/numeric.ops/transform_reduce.pass.cpp @@ -0,0 +1,129 @@ +// -*- C++ -*- +//===-- transform_reduce.pass.cpp -----------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <numeric> + +#include "support/utils.h" + +using namespace TestUtils; + +// Functor for xor-operation for modeling binary operations in inner_product +class XOR +{ + public: + template <typename T> + T + operator()(const T& left, const T& right) const + { + return left ^ right; + } +}; + +// Model of User-defined class +class MyClass +{ + public: + int32_t my_field; + MyClass() { my_field = 0; } + MyClass(int32_t in) { my_field = in; } + MyClass(const MyClass& in) = default; + + friend MyClass + operator+(const MyClass& x, const MyClass& y) + { + return MyClass(x.my_field + y.my_field); + } + friend MyClass + operator-(const MyClass& x) + { + return MyClass(-x.my_field); + } + friend MyClass operator*(const MyClass& x, const MyClass& y) + { + return MyClass(x.my_field * y.my_field); + } + friend bool operator==(const MyClass& x, const MyClass& y) + { + return x.my_field == y.my_field; + } +}; + +template <typename T> +void +CheckResults(const T& expected, const T& in) +{ + EXPECT_TRUE(expected == in, "wrong result of transform_reduce"); +} + +// We need to check correctness only for "int" (for example) except cases +// if we have "floating-point type"-specialization +void +CheckResults(const float32_t&, const float32_t&) +{ +} + +// Test for different types and operations with different iterators +struct test_transform_reduce +{ + template <typename Policy, typename InputIterator1, typename InputIterator2, typename T, typename BinaryOperation1, + typename BinaryOperation2, typename UnaryOp> + void + operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2, + T init, BinaryOperation1 opB1, BinaryOperation2 opB2, UnaryOp opU) + { + + auto expectedB = std::inner_product(first1, last1, first2, init, opB1, opB2); + auto expectedU = transform_reduce_serial(first1, last1, init, opB1, opU); + T resRA = std::transform_reduce(exec, first1, last1, first2, init, opB1, opB2); + CheckResults(expectedB, resRA); + resRA = std::transform_reduce(exec, first1, last1, init, opB1, opU); + CheckResults(expectedU, resRA); + } +}; + +template <typename T, typename BinaryOperation1, typename BinaryOperation2, typename UnaryOp, typename Initializer> +void +test_by_type(T init, BinaryOperation1 opB1, BinaryOperation2 opB2, UnaryOp opU, Initializer initObj) +{ + + std::size_t maxSize = 100000; + Sequence<T> in1(maxSize, initObj); + Sequence<T> in2(maxSize, initObj); + + for (std::size_t n = 0; n < maxSize; n = n < 16 ? n + 1 : size_t(3.1415 * n)) + { + invoke_on_all_policies(test_transform_reduce(), in1.begin(), in1.begin() + n, in2.begin(), in2.begin() + n, + init, opB1, opB2, opU); + invoke_on_all_policies(test_transform_reduce(), in1.cbegin(), in1.cbegin() + n, in2.cbegin(), in2.cbegin() + n, + init, opB1, opB2, opU); + } +} + +int +main() +{ + test_by_type<int32_t>(42, std::plus<int32_t>(), std::multiplies<int32_t>(), std::negate<int32_t>(), + [](std::size_t) -> int32_t { return int32_t(rand() % 1000); }); + test_by_type<int64_t>(0, [](const int64_t& a, const int64_t& b) -> int64_t { return a | b; }, XOR(), + [](const int64_t& x) -> int64_t { return x * 2; }, + [](std::size_t) -> int64_t { return int64_t(rand() % 1000); }); + test_by_type<float32_t>( + 1.0f, std::multiplies<float32_t>(), [](const float32_t& a, const float32_t& b) -> float32_t { return a + b; }, + [](const float32_t& x) -> float32_t { return x + 2; }, [](std::size_t) -> float32_t { return rand() % 1000; }); + test_by_type<MyClass>(MyClass(), std::plus<MyClass>(), std::multiplies<MyClass>(), std::negate<MyClass>(), + [](std::size_t) -> MyClass { return MyClass(rand() % 1000); }); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/numerics/numeric.ops/transform_scan.pass.cpp b/pstl/test/std/numerics/numeric.ops/transform_scan.pass.cpp new file mode 100644 index 0000000..95294e4 --- /dev/null +++ b/pstl/test/std/numerics/numeric.ops/transform_scan.pass.cpp @@ -0,0 +1,177 @@ +// -*- C++ -*- +//===-- transform_scan.pass.cpp -------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <numeric> + +#include "support/utils.h" + +using namespace TestUtils; + +// Most of the framework required for testing inclusive and exclusive transform-scans is identical, +// so the tests for both are in this file. Which is being tested is controlled by the global +// flag inclusive, which is set to each alternative by main(). +static bool inclusive; + +template <typename Iterator, typename Size, typename T> +void +check_and_reset(Iterator expected_first, Iterator out_first, Size n, T trash) +{ + EXPECT_EQ_N(expected_first, out_first, n, + inclusive ? "wrong result from transform_inclusive_scan" + : "wrong result from transform_exclusive_scan"); + std::fill_n(out_first, n, trash); +} + +struct test_transform_scan +{ + template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename UnaryOp, + typename T, typename BinaryOp> + typename std::enable_if<!TestUtils::isReverse<InputIterator>::value, void>::type + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, + OutputIterator out_last, OutputIterator expected_first, OutputIterator, Size n, UnaryOp unary_op, T init, + BinaryOp binary_op, T trash) + { + using namespace std; + + auto orr1 = + inclusive + ? transform_inclusive_scan(std::execution::seq, first, last, expected_first, binary_op, unary_op, init) + : transform_exclusive_scan(std::execution::seq, first, last, expected_first, init, binary_op, unary_op); + auto orr2 = inclusive ? transform_inclusive_scan(exec, first, last, out_first, binary_op, unary_op, init) + : transform_exclusive_scan(exec, first, last, out_first, init, binary_op, unary_op); + EXPECT_TRUE(out_last == orr2, "transform...scan returned wrong iterator"); + check_and_reset(expected_first, out_first, n, trash); + + // Checks inclusive scan if init is not provided + if (inclusive && n > 0) + { + orr1 = transform_inclusive_scan(std::execution::seq, first, last, expected_first, binary_op, unary_op); + orr2 = transform_inclusive_scan(exec, first, last, out_first, binary_op, unary_op); + EXPECT_TRUE(out_last == orr2, "transform...scan returned wrong iterator"); + check_and_reset(expected_first, out_first, n, trash); + } + } + + template <typename Policy, typename InputIterator, typename OutputIterator, typename Size, typename UnaryOp, + typename T, typename BinaryOp> + typename std::enable_if<TestUtils::isReverse<InputIterator>::value, void>::type + operator()(Policy&&, InputIterator, InputIterator, OutputIterator, OutputIterator, OutputIterator, OutputIterator, + Size, UnaryOp, T, BinaryOp, T) + { + } +}; + +const uint32_t encryption_mask = 0x314; + +template <typename InputIterator, typename OutputIterator, typename UnaryOperation, typename T, + typename BinaryOperation> +std::pair<OutputIterator, T> +transform_inclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op, + T init, BinaryOperation binary_op) noexcept +{ + for (; first != last; ++first, ++result) + { + init = binary_op(init, unary_op(*first)); + *result = init; + } + return std::make_pair(result, init); +} + +template <typename InputIterator, typename OutputIterator, typename UnaryOperation, typename T, + typename BinaryOperation> +std::pair<OutputIterator, T> +transform_exclusive_scan_serial(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation unary_op, + T init, BinaryOperation binary_op) noexcept +{ + for (; first != last; ++first, ++result) + { + *result = init; + init = binary_op(init, unary_op(*first)); + } + return std::make_pair(result, init); +} + +template <typename In, typename Out, typename UnaryOp, typename BinaryOp> +void +test(UnaryOp unary_op, Out init, BinaryOp binary_op, Out trash) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<In> in(n, [](size_t k) { return In(k ^ encryption_mask); }); + + Out tmp = init; + Sequence<Out> expected(n, [&](size_t k) -> Out { + if (inclusive) + { + tmp = binary_op(tmp, unary_op(in[k])); + return tmp; + } + else + { + Out val = tmp; + tmp = binary_op(tmp, unary_op(in[k])); + return val; + } + }); + + Sequence<Out> out(n, [&](size_t) { return trash; }); + + auto result = + inclusive + ? transform_inclusive_scan_serial(in.cbegin(), in.cend(), out.fbegin(), unary_op, init, binary_op) + : transform_exclusive_scan_serial(in.cbegin(), in.cend(), out.fbegin(), unary_op, init, binary_op); + (void)result; + check_and_reset(expected.begin(), out.begin(), out.size(), trash); + + invoke_on_all_policies(test_transform_scan(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), in.size(), unary_op, init, binary_op, trash); + invoke_on_all_policies(test_transform_scan(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), + expected.end(), in.size(), unary_op, init, binary_op, trash); + } +} + +template <typename In, typename Out, typename UnaryOp, typename BinaryOp> +void +test_matrix(UnaryOp unary_op, Out init, BinaryOp binary_op, Out trash) +{ + for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<In> in(n, [](size_t k) { return In(k, k + 1); }); + + Sequence<Out> out(n, [&](size_t) { return trash; }); + Sequence<Out> expected(n, [&](size_t) { return trash; }); + + invoke_on_all_policies(test_transform_scan(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), + expected.end(), in.size(), unary_op, init, binary_op, trash); + invoke_on_all_policies(test_transform_scan(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), + expected.end(), in.size(), unary_op, init, binary_op, trash); + } +} + +int +main() +{ + for (int32_t mode = 0; mode < 2; ++mode) + { + inclusive = mode != 0; +#if !defined(_PSTL_ICC_19_TEST_SIMD_UDS_WINDOWS_RELEASE_BROKEN) + test_matrix<Matrix2x2<int32_t>, Matrix2x2<int32_t>>([](const Matrix2x2<int32_t> x) { return x; }, + Matrix2x2<int32_t>(), multiply_matrix<int32_t>, + Matrix2x2<int32_t>(-666, 666)); +#endif + test<int32_t, uint32_t>([](int32_t x) { return x++; }, -123, [](int32_t x, int32_t y) { return x + y; }, 666); + } + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_construct.pass.cpp b/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_construct.pass.cpp new file mode 100644 index 0000000..a5bdbb3 --- /dev/null +++ b/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_construct.pass.cpp @@ -0,0 +1,123 @@ +// -*- C++ -*- +//===-- uninitialized_construct.pass.cpp ----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for uninitialized_default_construct, uninitialized_default_construct_n, +// uninitialized_value_construct, uninitialized_value_construct_n + +#include "support/pstl_test_config.h" + +#include <execution> +#include <memory> + +#include "support/utils.h" + +using namespace TestUtils; + +// function of checking correctness for uninitialized.construct.value +template <typename T, typename Iterator> +bool +IsCheckValueCorrectness(Iterator begin, Iterator end) +{ + for (; begin != end; ++begin) + { + if (*begin != T()) + { + return false; + } + } + return true; +} + +struct test_uninit_construct +{ + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial<T>=*/std::false_type) + { + typedef typename std::iterator_traits<Iterator>::value_type T; + // it needs for cleaning memory that was filled by default constructors in unique_ptr<T[]> p(new T[n]) + // and for cleaning memory after last calling of uninitialized_value_construct_n. + // It is important for non-trivial types + std::destroy_n(exec, begin, n); + + // reset counter of constructors + T::SetCount(0); + // run algorithm + std::uninitialized_default_construct(exec, begin, end); + // compare counter of constructors to length of container + EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct"); + // destroy objects for testing new algorithms on same memory + std::destroy(exec, begin, end); + + std::uninitialized_default_construct_n(exec, begin, n); + EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct_n"); + std::destroy_n(exec, begin, n); + + std::uninitialized_value_construct(exec, begin, end); + EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct"); + std::destroy(exec, begin, end); + + std::uninitialized_value_construct_n(exec, begin, n); + EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct_n"); + } + + template <typename Policy, typename Iterator> + void + operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial<T>=*/std::true_type) + { + typedef typename std::iterator_traits<Iterator>::value_type T; + + std::uninitialized_default_construct(exec, begin, end); + std::destroy(exec, begin, end); + + std::uninitialized_default_construct_n(exec, begin, n); + std::destroy_n(exec, begin, n); + + std::uninitialized_value_construct(exec, begin, end); + // check correctness for uninitialized.construct.value + EXPECT_TRUE(IsCheckValueCorrectness<T>(begin, end), "wrong uninitialized_value_construct"); + std::destroy(exec, begin, end); + + std::uninitialized_value_construct_n(exec, begin, n); + EXPECT_TRUE(IsCheckValueCorrectness<T>(begin, end), "wrong uninitialized_value_construct_n"); + std::destroy_n(exec, begin, n); + } +}; + +template <typename T> +void +test_uninit_construct_by_type() +{ + std::size_t N = 100000; + for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + std::unique_ptr<T[]> p(new T[n]); + invoke_on_all_policies(test_uninit_construct(), p.get(), std::next(p.get(), n), n, std::is_trivial<T>()); + } +} + +int +main() +{ + + // for user-defined types +#if !defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) + test_uninit_construct_by_type<Wrapper<int32_t>>(); + test_uninit_construct_by_type<Wrapper<std::vector<std::string>>>(); +#endif + + // for trivial types + test_uninit_construct_by_type<int8_t>(); + test_uninit_construct_by_type<float64_t>(); + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_copy_move.pass.cpp b/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_copy_move.pass.cpp new file mode 100644 index 0000000..21186b4 --- /dev/null +++ b/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_copy_move.pass.cpp @@ -0,0 +1,143 @@ +// -*- C++ -*- +//===-- uninitialized_copy_move.pass.cpp ----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// Tests for uninitialized_copy, uninitialized_copy_n, uninitialized_move, uninitialized_move_n + +#include "support/pstl_test_config.h" + +#include <execution> +#include <memory> + +#include "support/utils.h" + +using namespace TestUtils; + +// function of checking correctness for uninitialized.construct.value +template <typename InputIterator, typename OutputIterator, typename Size> +bool +IsCheckValueCorrectness(InputIterator first1, OutputIterator first2, Size n) +{ + for (Size i = 0; i < n; ++i, ++first1, ++first2) + { + if (*first1 != *first2) + { + return false; + } + } + return true; +} + +struct test_uninitialized_copy_move +{ + template <typename Policy, typename InputIterator, typename OutputIterator> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, size_t n, + /*is_trivial<T>=*/std::false_type) + { + typedef typename std::iterator_traits<InputIterator>::value_type T; + // it needs for cleaning memory that was filled by default constructors in unique_ptr<T[]> p(new T[n]) + // and for cleaning memory after last calling of uninitialized_value_construct_n. + // It is important for non-trivial types + std::destroy_n(exec, out_first, n); + + // reset counter of constructors + T::SetCount(0); + // run algorithm + std::uninitialized_copy(exec, first, last, out_first); + // compare counter of constructors to length of container + EXPECT_TRUE(T::Count() == n, "wrong uninitialized_copy"); + // destroy objects for testing new algorithms on same memory + std::destroy_n(exec, out_first, n); + + std::uninitialized_copy_n(exec, first, n, out_first); + EXPECT_TRUE(T::Count() == n, "wrong uninitialized_copy_n"); + std::destroy_n(exec, out_first, n); + + // For move + std::uninitialized_move(exec, first, last, out_first); + // compare counter of constructors to length of container + EXPECT_TRUE(T::MoveCount() == n, "wrong uninitialized_move"); + // destroy objects for testing new algorithms on same memory + std::destroy_n(exec, out_first, n); + + std::uninitialized_move_n(exec, first, n, out_first); + EXPECT_TRUE(T::MoveCount() == n, "wrong uninitialized_move_n"); + std::destroy_n(exec, out_first, n); + } + +#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) + template <typename InputIterator, typename OutputIterator> + void + operator()(pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first, + size_t n, /*is_trivial<T>=*/std::true_type) + { + } + template <typename InputIterator, typename OutputIterator> + void + operator()(pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, + OutputIterator out_first, size_t n, /*is_trivial<T>=*/std::true_type) + { + } +#endif + + template <typename Policy, typename InputIterator, typename OutputIterator> + void + operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, size_t n, + /*is_trivial<T>=*/std::true_type) + { + std::uninitialized_copy(exec, first, last, out_first); + EXPECT_TRUE(IsCheckValueCorrectness(first, out_first, n), "wrong uninitialized_copy"); + std::destroy_n(exec, out_first, n); + + std::uninitialized_copy_n(exec, first, n, out_first); + EXPECT_TRUE(IsCheckValueCorrectness(first, out_first, n), "wrong uninitialized_copy_n"); + std::destroy_n(exec, out_first, n); + + std::uninitialized_move(exec, first, last, out_first); + EXPECT_TRUE(IsCheckValueCorrectness(first, out_first, n), "wrong uninitialized_move"); + std::destroy_n(exec, out_first, n); + + std::uninitialized_move_n(exec, first, n, out_first); + EXPECT_TRUE(IsCheckValueCorrectness(first, out_first, n), "wrong uninitialized_move_n"); + std::destroy_n(exec, out_first, n); + } +}; + +template <typename T> +void +test_uninitialized_copy_move_by_type() +{ + std::size_t N = 100000; + for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + Sequence<T> in(n, [=](size_t k) -> T { return T(k); }); + std::unique_ptr<T[]> p(new T[n]); + invoke_on_all_policies(test_uninitialized_copy_move(), in.begin(), in.end(), p.get(), n, std::is_trivial<T>()); + } +} + +int +main() +{ + + // for trivial types + test_uninitialized_copy_move_by_type<int16_t>(); + test_uninitialized_copy_move_by_type<float64_t>(); + + // for user-defined types +#if !defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) && !defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) && \ + !defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) + test_uninitialized_copy_move_by_type<Wrapper<int8_t>>(); +#endif + + std::cout << done() << std::endl; + return 0; +} diff --git a/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_fill_destroy.pass.cpp b/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_fill_destroy.pass.cpp new file mode 100644 index 0000000..d579ea5 --- /dev/null +++ b/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_fill_destroy.pass.cpp @@ -0,0 +1,93 @@ +// -*- C++ -*- +//===-- uninitialized_fill_destroy.pass.cpp -------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +#include "support/pstl_test_config.h" + +#include <execution> +#include <memory> + +#include "support/utils.h" + +using namespace TestUtils; + +struct test_uninitialized_fill_destroy +{ + template <typename Policy, typename Iterator, typename T> + void + operator()(Policy&& exec, Iterator first, Iterator last, const T& in, std::size_t n, std::false_type) + { + using namespace std; + { + T::SetCount(0); + uninitialized_fill(exec, first, last, in); + size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); + EXPECT_TRUE(n == count, "wrong work of uninitialized_fill"); + destroy(exec, first, last); + EXPECT_TRUE(T::Count() == 0, "wrong work of destroy"); + } + + { + auto res = uninitialized_fill_n(exec, first, n, in); + EXPECT_TRUE(res == last, "wrong result of uninitialized_fill_n"); + size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); + EXPECT_TRUE(n == count, "wrong work of uninitialized_fill_n"); + destroy_n(exec, first, n); + EXPECT_TRUE(T::Count() == 0, "wrong work of destroy_n"); + } + } + template <typename Policy, typename Iterator, typename T> + void + operator()(Policy&& exec, Iterator first, Iterator last, const T& in, std::size_t n, std::true_type) + { + using namespace std; + { + destroy(exec, first, last); + uninitialized_fill(exec, first, last, in); + size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); + EXPECT_EQ(n, count, "wrong work of uninitialized:_fill"); + } + { + destroy_n(exec, first, n); + auto res = uninitialized_fill_n(exec, first, n, in); + size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); + EXPECT_EQ(n, count, "wrong work of uninitialized_fill_n"); + EXPECT_TRUE(res == last, "wrong result of uninitialized_fill_n"); + } + } +}; + +template <typename T> +void +test_uninitialized_fill_destroy_by_type() +{ + std::size_t N = 100000; + for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) + { + std::unique_ptr<T[]> p(new T[n]); + invoke_on_all_policies(test_uninitialized_fill_destroy(), p.get(), std::next(p.get(), n), T(), n, + std::is_trivial<T>()); + } +} + +int +main() +{ + // for trivial types + test_uninitialized_fill_destroy_by_type<int32_t>(); + test_uninitialized_fill_destroy_by_type<float64_t>(); + + // for user-defined types + test_uninitialized_fill_destroy_by_type<Wrapper<std::string>>(); + test_uninitialized_fill_destroy_by_type<Wrapper<int8_t*>>(); + std::cout << done() << std::endl; + + return 0; +} diff --git a/pstl/test/support/pstl_test_config.h b/pstl/test/support/pstl_test_config.h new file mode 100644 index 0000000..daf5625 --- /dev/null +++ b/pstl/test/support/pstl_test_config.h @@ -0,0 +1,52 @@ +// -*- C++ -*- +//===-- pstl_test_config.h ------------------------------------------------===// +// +// 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 _PSTL_TEST_config_H +#define _PSTL_TEST_config_H + +#if defined(_MSC_VER) && defined(_DEBUG) +# define _SCL_SECURE_NO_WARNINGS //to prevent the compilation warning. Microsoft STL implementation has specific checking of an iterator range in DEBUG mode for the containers from the standard library. +#endif + +#ifndef __clang__ +# define _PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN \ + (__x86_64 && !_DEBUG && __INTEL_COMPILER && __INTEL_COMPILER <= 1700 && !__APPLE__) +# define _PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN \ + (!_DEBUG && __INTEL_COMPILER && \ + (__INTEL_COMPILER < 1800 || (__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE < 1))) +# define _PSTL_ICC_1800_TEST_MONOTONIC_RELEASE_64_BROKEN \ + (__x86_64 && !_DEBUG && __INTEL_COMPILER && __INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE < 1) +# define _PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN \ + (__i386__ && !_DEBUG && __INTEL_COMPILER >= 1700 && __INTEL_COMPILER < 1800 && __APPLE__) +# define _PSTL_ICC_18_VC141_TEST_SIMD_LAMBDA_RELEASE_BROKEN \ + (!_DEBUG && __INTEL_COMPILER >= 1800 && __INTEL_COMPILER < 1900 && _MSC_VER == 1910) +# define _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN \ + (_M_IX86 && _DEBUG && __INTEL_COMPILER >= 1700 && __INTEL_COMPILER < 1800 && _MSC_VER >= 1900) +# define _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN \ + (_M_IX86 && _DEBUG && __INTEL_COMPILER >= 1600 && __INTEL_COMPILER < 1700 && _MSC_VER == 1900) +# define _PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN \ + (((_M_X64 && _MSC_VER == 1900) || __x86_64) && !_DEBUG && __INTEL_COMPILER < 1700) +# define _PSTL_ICC_16_17_TEST_64_TIMEOUT (__x86_64 && __INTEL_COMPILER && __INTEL_COMPILER < 1800 && !__APPLE__) +# define _PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN \ + (!_DEBUG && __INTEL_COMPILER && __INTEL_COMPILER == 1800) +# define _PSTL_CLANG_TEST_BIG_OBJ_DEBUG_32_BROKEN \ + (__i386__ && PSTL_USE_DEBUG && __clang__ && _PSTL_CLANG_VERSION <= 90000) +# define _PSTL_ICC_16_17_18_TEST_UNIQUE_MASK_RELEASE_BROKEN \ + (!_DEBUG && __INTEL_COMPILER && \ + (__INTEL_COMPILER < 1800 || (__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE < 3))) +# define _PSTL_ICC_18_TEST_EARLY_EXIT_AVX_RELEASE_BROKEN \ + (!_DEBUG && __INTEL_COMPILER == 1800 && __AVX__ && !__AVX2__ && !__AVX512__) +# define _PSTL_ICC_19_TEST_IS_PARTITIONED_RELEASE_BROKEN \ + (!PSTL_USE_DEBUG && (__linux__ || __APPLE__) && __INTEL_COMPILER == 1900) +# define _PSTL_ICL_19_VC14_VC141_TEST_SCAN_RELEASE_BROKEN \ + (__INTEL_COMPILER == 1900 && _MSC_VER >= 1900 && _MSC_VER <= 1910) +# define _PSTL_ICC_19_TEST_SIMD_UDS_WINDOWS_RELEASE_BROKEN (__INTEL_COMPILER == 1900 && _MSC_VER && !_DEBUG) +#endif // !__clang__ + +#endif /* _PSTL_TEST_config_H */ diff --git a/pstl/test/support/stdlib/algorithm b/pstl/test/support/stdlib/algorithm new file mode 100644 index 0000000..39e6f74 --- /dev/null +++ b/pstl/test/support/stdlib/algorithm @@ -0,0 +1,26 @@ +// -*- C++ -*- +//===-- algorithm ---------------------------------------------------------===// +// +// 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 _TEST_SUPPORT_STDLIB_ALGORITHM +#define _TEST_SUPPORT_STDLIB_ALGORITHM + +#include_next <algorithm> + +#include <pstl/internal/pstl_config.h> + +#if _PSTL_EXECUTION_POLICIES_DEFINED +// If <execution> has already been included, pull in implementations +# include <pstl/internal/glue_algorithm_impl.h> +#else +// Otherwise just pull in forward declarations +# include <pstl/internal/glue_algorithm_defs.h> +# define _PSTL_ALGORITHM_FORWARD_DECLARED 1 +#endif + +#endif /* _TEST_SUPPORT_STDLIB_ALGORITHM */ diff --git a/pstl/test/support/stdlib/execution b/pstl/test/support/stdlib/execution new file mode 100644 index 0000000..ad18c45 --- /dev/null +++ b/pstl/test/support/stdlib/execution @@ -0,0 +1,50 @@ +// -*- C++ -*- +//===-- execution ---------------------------------------------------------===// +// +// 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 _TEST_SUPPORT_STDLIB_EXECUTION +#define _TEST_SUPPORT_STDLIB_EXECUTION + +// #include_next <execution> // None of the standard libraries PSTL is built on top have the <execution> header yet. + +#include <pstl/internal/pstl_config.h> +#include <pstl/internal/execution_defs.h> + +#define _PSTL_EXECUTION_POLICIES_DEFINED 1 + +#if _PSTL_ALGORITHM_FORWARD_DECLARED +# include <pstl/internal/glue_algorithm_impl.h> +#endif + +#if _PSTL_MEMORY_FORWARD_DECLARED +# include <pstl/internal/glue_memory_impl.h> +#endif + +#if _PSTL_NUMERIC_FORWARD_DECLARED +# include <pstl/internal/glue_numeric_impl.h> +#endif + +#if _PSTL_CPP17_EXECUTION_POLICIES_PRESENT +_PSTL_PRAGMA_MESSAGE_POLICIES("The <Parallel STL> execution policies are defined in the namespace __pstl::execution") +#else +# include <pstl/internal/glue_execution_defs.h> +_PSTL_PRAGMA_MESSAGE_POLICIES( + "The <Parallel STL> execution policies are injected into the standard namespace std::execution") +#endif + +//TODO: __pstl::execution namespace is injected into the pstl::execution namespace when the implementation is not a part of +// standard C++ library +namespace pstl +{ +namespace execution +{ +using namespace __pstl::execution; +} +} // namespace pstl + +#endif /* _TEST_SUPPORT_STDLIB_EXECUTION */ diff --git a/pstl/test/support/stdlib/memory b/pstl/test/support/stdlib/memory new file mode 100644 index 0000000..7d110912 --- /dev/null +++ b/pstl/test/support/stdlib/memory @@ -0,0 +1,26 @@ +// -*- C++ -*- +//===-- memory ------------------------------------------------------------===// +// +// 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 _TEST_SUPPORT_STDLIB_MEMORY +#define _TEST_SUPPORT_STDLIB_MEMORY + +#include_next <memory> + +#include <pstl/internal/pstl_config.h> + +#if _PSTL_EXECUTION_POLICIES_DEFINED +// If <execution> has already been included, pull in implementations +# include <pstl/internal/glue_memory_impl.h> +#else +// Otherwise just pull in forward declarations +# include <pstl/internal/glue_memory_defs.h> +# define _PSTL_MEMORY_FORWARD_DECLARED 1 +#endif + +#endif /* _TEST_SUPPORT_STDLIB_MEMORY */ diff --git a/pstl/test/support/stdlib/numeric b/pstl/test/support/stdlib/numeric new file mode 100644 index 0000000..8f98bc7 --- /dev/null +++ b/pstl/test/support/stdlib/numeric @@ -0,0 +1,26 @@ +// -*- C++ -*- +//===-- numeric -----------------------------------------------------------===// +// +// 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 _TEST_SUPPORT_STDLIB_NUMERIC +#define _TEST_SUPPORT_STDLIB_NUMERIC + +#include_next <numeric> + +#include <pstl/internal/pstl_config.h> + +#if _PSTL_EXECUTION_POLICIES_DEFINED +// If <execution> has already been included, pull in implementations +# include <pstl/internal/glue_numeric_impl.h> +#else +// Otherwise just pull in forward declarations +# include <pstl/internal/glue_numeric_defs.h> +# define _PSTL_NUMERIC_FORWARD_DECLARED 1 +#endif + +#endif /* _TEST_SUPPORT_STDLIB_NUMERIC */ diff --git a/pstl/test/support/utils.h b/pstl/test/support/utils.h new file mode 100644 index 0000000..ed6d48b --- /dev/null +++ b/pstl/test/support/utils.h @@ -0,0 +1,1318 @@ +// -*- C++ -*- +//===-- utils.h -----------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// File contains common utilities that tests rely on + +// Do not #include <algorithm>, because if we do we will not detect accidental dependencies. +#include <atomic> +#include <cstdint> +#include <cstdlib> +#include <cstring> +#include <iostream> +#include <iterator> +#include <memory> +#include <sstream> +#include <vector> + +#include "pstl_test_config.h" + +namespace TestUtils +{ + +typedef double float64_t; +typedef float float32_t; + +template <class T, std::size_t N> +constexpr size_t +const_size(const T (&)[N]) noexcept +{ + return N; +} + +template <typename T> +class Sequence; + +// Handy macros for error reporting +#define EXPECT_TRUE(condition, message) ::TestUtils::expect(true, condition, __FILE__, __LINE__, message) +#define EXPECT_FALSE(condition, message) ::TestUtils::expect(false, condition, __FILE__, __LINE__, message) + +// Check that expected and actual are equal and have the same type. +#define EXPECT_EQ(expected, actual, message) ::TestUtils::expect_equal(expected, actual, __FILE__, __LINE__, message) + +// Check that sequences started with expected and actual and have had size n are equal and have the same type. +#define EXPECT_EQ_N(expected, actual, n, message) \ + ::TestUtils::expect_equal(expected, actual, n, __FILE__, __LINE__, message) + +// Issue error message from outstr, adding a newline. +// Real purpose of this routine is to have a place to hang a breakpoint. +inline void +issue_error_message(std::stringstream& outstr) +{ + outstr << std::endl; + std::cerr << outstr.str(); + std::exit(EXIT_FAILURE); +} + +inline void +expect(bool expected, bool condition, const char* file, int32_t line, const char* message) +{ + if (condition != expected) + { + std::stringstream outstr; + outstr << "error at " << file << ":" << line << " - " << message; + issue_error_message(outstr); + } +} + +// Do not change signature to const T&. +// Function must be able to detect const differences between expected and actual. +template <typename T> +void +expect_equal(T& expected, T& actual, const char* file, int32_t line, const char* message) +{ + if (!(expected == actual)) + { + std::stringstream outstr; + outstr << "error at " << file << ":" << line << " - " << message << ", expected " << expected << " got " + << actual; + issue_error_message(outstr); + } +} + +template <typename T> +void +expect_equal(Sequence<T>& expected, Sequence<T>& actual, const char* file, int32_t line, const char* message) +{ + size_t n = expected.size(); + size_t m = actual.size(); + if (n != m) + { + std::stringstream outstr; + outstr << "error at " << file << ":" << line << " - " << message << ", expected sequence of size " << n + << " got sequence of size " << m; + issue_error_message(outstr); + return; + } + size_t error_count = 0; + for (size_t k = 0; k < n && error_count < 10; ++k) + { + if (!(expected[k] == actual[k])) + { + std::stringstream outstr; + outstr << "error at " << file << ":" << line << " - " << message << ", at index " << k << " expected " + << expected[k] << " got " << actual[k]; + issue_error_message(outstr); + ++error_count; + } + } +} + +template <typename Iterator1, typename Iterator2, typename Size> +void +expect_equal(Iterator1 expected_first, Iterator2 actual_first, Size n, const char* file, int32_t line, + const char* message) +{ + size_t error_count = 0; + for (Size k = 0; k < n && error_count < 10; ++k, ++expected_first, ++actual_first) + { + if (!(*expected_first == *actual_first)) + { + std::stringstream outstr; + outstr << "error at " << file << ":" << line << " - " << message << ", at index " << k; + issue_error_message(outstr); + ++error_count; + } + } +} + +// ForwardIterator is like type Iterator, but restricted to be a forward iterator. +// Only the forward iterator signatures that are necessary for tests are present. +// Post-increment in particular is deliberatly omitted since our templates should avoid using it +// because of efficiency considerations. +template <typename Iterator, typename IteratorTag> +class ForwardIterator +{ + public: + typedef IteratorTag iterator_category; + typedef typename std::iterator_traits<Iterator>::value_type value_type; + typedef typename std::iterator_traits<Iterator>::difference_type difference_type; + typedef typename std::iterator_traits<Iterator>::pointer pointer; + typedef typename std::iterator_traits<Iterator>::reference reference; + + protected: + Iterator my_iterator; + typedef value_type element_type; + + public: + ForwardIterator() = default; + explicit ForwardIterator(Iterator i) : my_iterator(i) {} + reference operator*() const { return *my_iterator; } + Iterator operator->() const { return my_iterator; } + ForwardIterator + operator++() + { + ++my_iterator; + return *this; + } + ForwardIterator operator++(int32_t) + { + auto retval = *this; + my_iterator++; + return retval; + } + friend bool + operator==(const ForwardIterator& i, const ForwardIterator& j) + { + return i.my_iterator == j.my_iterator; + } + friend bool + operator!=(const ForwardIterator& i, const ForwardIterator& j) + { + return i.my_iterator != j.my_iterator; + } + + Iterator + iterator() const + { + return my_iterator; + } +}; + +template <typename Iterator, typename IteratorTag> +class BidirectionalIterator : public ForwardIterator<Iterator, IteratorTag> +{ + typedef ForwardIterator<Iterator, IteratorTag> base_type; + + public: + BidirectionalIterator() = default; + explicit BidirectionalIterator(Iterator i) : base_type(i) {} + BidirectionalIterator(const base_type& i) : base_type(i.iterator()) {} + + BidirectionalIterator + operator++() + { + ++base_type::my_iterator; + return *this; + } + BidirectionalIterator + operator--() + { + --base_type::my_iterator; + return *this; + } + BidirectionalIterator operator++(int32_t) + { + auto retval = *this; + base_type::my_iterator++; + return retval; + } + BidirectionalIterator operator--(int32_t) + { + auto retval = *this; + base_type::my_iterator--; + return retval; + } +}; + +template <typename Iterator, typename F> +void +fill_data(Iterator first, Iterator last, F f) +{ + typedef typename std::iterator_traits<Iterator>::value_type T; + for (std::size_t i = 0; first != last; ++first, ++i) + { + *first = T(f(i)); + } +} + +struct MemoryChecker { + // static counters and state tags + static std::atomic<std::int64_t> alive_object_counter; // initialized outside + static constexpr std::int64_t alive_state = 0xAAAAAAAAAAAAAAAA; + static constexpr std::int32_t dead_state = 0; // only used as a set value to cancel alive_state + + std::int32_t _value; // object value used for algorithms + std::int64_t _state; // state tag used for checks + + // ctors, dtors, assign ops + explicit MemoryChecker(std::int32_t value = 0) : _value(value) { + // check for EXPECT_TRUE(state() != alive_state, ...) has not been done since we cannot guarantee that + // raw memory for object being constructed does not have a bit sequence being equal to alive_state + + // set constructed state and increment counter for living object + inc_alive_objects(); + _state = alive_state; + } + MemoryChecker(MemoryChecker&& other) : _value(other.value()) { + // check for EXPECT_TRUE(state() != alive_state, ...) has not been done since + // compiler can optimize out the move ctor call that results in false positive failure + EXPECT_TRUE(other.state() == alive_state, "wrong effect from MemoryChecker(MemoryChecker&&): attemp to construct an object from non-existing object"); + // set constructed state and increment counter for living object + inc_alive_objects(); + _state = alive_state; + } + MemoryChecker(const MemoryChecker& other) : _value(other.value()) { + // check for EXPECT_TRUE(state() != alive_state, ...) has not been done since + // compiler can optimize out the copy ctor call that results in false positive failure + EXPECT_TRUE(other.state() == alive_state, "wrong effect from MemoryChecker(const MemoryChecker&): attemp to construct an object from non-existing object"); + // set constructed state and increment counter for living object + inc_alive_objects(); + _state = alive_state; + } + MemoryChecker& operator=(MemoryChecker&& other) { + // check if we do not assign over uninitialized memory + EXPECT_TRUE(state() == alive_state, "wrong effect from MemoryChecker::operator=(MemoryChecker&& other): attemp to assign to non-existing object"); + EXPECT_TRUE(other.state() == alive_state, "wrong effect from MemoryChecker::operator=(MemoryChecker&& other): attemp to assign from non-existing object"); + // just assign new value, counter is the same, state is the same + _value = other.value(); + + return *this; + } + MemoryChecker& operator=(const MemoryChecker& other) { + // check if we do not assign over uninitialized memory + EXPECT_TRUE(state() == alive_state, "wrong effect from MemoryChecker::operator=(const MemoryChecker& other): attemp to assign to non-existing object"); + EXPECT_TRUE(other.state() == alive_state, "wrong effect from MemoryChecker::operator=(const MemoryChecker& other): attemp to assign from non-existing object"); + // just assign new value, counter is the same, state is the same + _value = other.value(); + + return *this; + } + ~MemoryChecker() { + // check if we do not double destruct the object + EXPECT_TRUE(state() == alive_state, "wrong effect from ~MemoryChecker(): attemp to destroy non-existing object"); + // set destructed state and decrement counter for living object + static_cast<volatile std::int64_t&>(_state) = dead_state; + dec_alive_objects(); + } + + // getters + std::int32_t value() const { return _value; } + std::int64_t state() const { return _state; } + static std::int32_t alive_objects() { return alive_object_counter.load(); } +private: + // setters + void inc_alive_objects() { alive_object_counter.fetch_add(1); } + void dec_alive_objects() { alive_object_counter.fetch_sub(1); } +}; + +std::atomic<std::int64_t> MemoryChecker::alive_object_counter{0}; + +std::ostream& operator<<(std::ostream& os, const MemoryChecker& val) { return (os << val.value()); } +bool operator==(const MemoryChecker& v1, const MemoryChecker& v2) { return v1.value() == v2.value(); } +bool operator<(const MemoryChecker& v1, const MemoryChecker& v2) { return v1.value() < v2.value(); } + +// Sequence<T> is a container of a sequence of T with lots of kinds of iterators. +// Prefixes on begin/end mean: +// c = "const" +// f = "forward" +// No prefix indicates non-const random-access iterator. +template <typename T> +class Sequence +{ + std::vector<T> m_storage; + + public: + typedef typename std::vector<T>::iterator iterator; + typedef typename std::vector<T>::const_iterator const_iterator; + typedef ForwardIterator<iterator, std::forward_iterator_tag> forward_iterator; + typedef ForwardIterator<const_iterator, std::forward_iterator_tag> const_forward_iterator; + + typedef BidirectionalIterator<iterator, std::bidirectional_iterator_tag> bidirectional_iterator; + typedef BidirectionalIterator<const_iterator, std::bidirectional_iterator_tag> const_bidirectional_iterator; + + typedef T value_type; + explicit Sequence(size_t size) : m_storage(size) {} + + // Construct sequence [f(0), f(1), ... f(size-1)] + // f can rely on its invocations being sequential from 0 to size-1. + template <typename Func> + Sequence(size_t size, Func f) + { + m_storage.reserve(size); + // Use push_back because T might not have a default constructor + for (size_t k = 0; k < size; ++k) + m_storage.push_back(T(f(k))); + } + Sequence(const std::initializer_list<T>& data) : m_storage(data) {} + + const_iterator + begin() const + { + return m_storage.begin(); + } + const_iterator + end() const + { + return m_storage.end(); + } + iterator + begin() + { + return m_storage.begin(); + } + iterator + end() + { + return m_storage.end(); + } + const_iterator + cbegin() const + { + return m_storage.cbegin(); + } + const_iterator + cend() const + { + return m_storage.cend(); + } + forward_iterator + fbegin() + { + return forward_iterator(m_storage.begin()); + } + forward_iterator + fend() + { + return forward_iterator(m_storage.end()); + } + const_forward_iterator + cfbegin() const + { + return const_forward_iterator(m_storage.cbegin()); + } + const_forward_iterator + cfend() const + { + return const_forward_iterator(m_storage.cend()); + } + const_forward_iterator + fbegin() const + { + return const_forward_iterator(m_storage.cbegin()); + } + const_forward_iterator + fend() const + { + return const_forward_iterator(m_storage.cend()); + } + + const_bidirectional_iterator + cbibegin() const + { + return const_bidirectional_iterator(m_storage.cbegin()); + } + const_bidirectional_iterator + cbiend() const + { + return const_bidirectional_iterator(m_storage.cend()); + } + + bidirectional_iterator + bibegin() + { + return bidirectional_iterator(m_storage.begin()); + } + bidirectional_iterator + biend() + { + return bidirectional_iterator(m_storage.end()); + } + + std::size_t + size() const + { + return m_storage.size(); + } + const T* + data() const + { + return m_storage.data(); + } + typename std::vector<T>::reference operator[](size_t j) { return m_storage[j]; } + const T& operator[](size_t j) const { return m_storage[j]; } + + // Fill with given value + void + fill(const T& value) + { + for (size_t i = 0; i < m_storage.size(); i++) + m_storage[i] = value; + } + + void + print() const; + + template <typename Func> + void + fill(Func f) + { + fill_data(m_storage.begin(), m_storage.end(), f); + } +}; + +template <typename T> +void +Sequence<T>::print() const +{ + std::cout << "size = " << size() << ": { "; + std::copy(begin(), end(), std::ostream_iterator<T>(std::cout, " ")); + std::cout << " } " << std::endl; +} + +// Predicates for algorithms +template <typename DataType> +struct is_equal_to +{ + is_equal_to(const DataType& expected) : m_expected(expected) {} + bool + operator()(const DataType& actual) const + { + return actual == m_expected; + } + + private: + DataType m_expected; +}; + +// Low-quality hash function, returns value between 0 and (1<<bits)-1 +// Warning: low-order bits are quite predictable. +inline size_t +HashBits(size_t i, size_t bits) +{ + size_t mask = bits >= 8 * sizeof(size_t) ? ~size_t(0) : (size_t(1) << bits) - 1; + return (424157 * i ^ 0x24aFa) & mask; +} + +// Stateful unary op +template <typename T, typename U> +class Complement +{ + int32_t val; + + public: + Complement(T v) : val(v) {} + U + operator()(const T& x) const + { + return U(val - x); + } +}; + +// Tag used to prevent accidental use of converting constructor, even if use is explicit. +struct OddTag +{ +}; + +class Sum; + +// Type with limited set of operations. Not default-constructible. +// Only available operator is "==". +// Typically used as value type in tests. +class Number +{ + int32_t value; + friend class Add; + friend class Sum; + friend class IsMultiple; + friend class Congruent; + friend Sum + operator+(const Sum& x, const Sum& y); + + public: + Number(int32_t val, OddTag) : value(val) {} + friend bool + operator==(const Number& x, const Number& y) + { + return x.value == y.value; + } + friend std::ostream& + operator<<(std::ostream& o, const Number& d) + { + return o << d.value; + } +}; + +// Stateful predicate for Number. Not default-constructible. +class IsMultiple +{ + long modulus; + + public: + // True if x is multiple of modulus + bool + operator()(Number x) const + { + return x.value % modulus == 0; + } + IsMultiple(long modulus_, OddTag) : modulus(modulus_) {} +}; + +// Stateful equivalence-class predicate for Number. Not default-constructible. +class Congruent +{ + long modulus; + + public: + // True if x and y have same remainder for the given modulus. + // Note: this is not quite the same as "equivalent modulo modulus" when x and y have different + // sign, but nonetheless AreCongruent is still an equivalence relationship, which is all + // we need for testing. + bool + operator()(Number x, Number y) const + { + return x.value % modulus == y.value % modulus; + } + Congruent(long modulus_, OddTag) : modulus(modulus_) {} +}; + +// Stateful reduction operation for Number +class Add +{ + long bias; + + public: + explicit Add(OddTag) : bias(1) {} + Number + operator()(Number x, const Number& y) + { + return Number(x.value + y.value + (bias - 1), OddTag()); + } +}; + +// Class similar to Number, but has default constructor and +. +class Sum : public Number +{ + public: + Sum() : Number(0, OddTag()) {} + Sum(long x, OddTag) : Number(x, OddTag()) {} + friend Sum + operator+(const Sum& x, const Sum& y) + { + return Sum(x.value + y.value, OddTag()); + } +}; + +// Type with limited set of operations, which includes an associative but not commutative operation. +// Not default-constructible. +// Typically used as value type in tests involving "GENERALIZED_NONCOMMUTATIVE_SUM". +class MonoidElement +{ + size_t a, b; + + public: + MonoidElement(size_t a_, size_t b_, OddTag) : a(a_), b(b_) {} + friend bool + operator==(const MonoidElement& x, const MonoidElement& y) + { + return x.a == y.a && x.b == y.b; + } + friend std::ostream& + operator<<(std::ostream& o, const MonoidElement& x) + { + return o << "[" << x.a << ".." << x.b << ")"; + } + friend class AssocOp; +}; + +// Stateful associative op for MonoidElement +// It's not really a monoid since the operation is not allowed for any two elements. +// But it's good enough for testing. +class AssocOp +{ + unsigned c; + + public: + explicit AssocOp(OddTag) : c(5) {} + MonoidElement + operator()(const MonoidElement& x, const MonoidElement& y) + { + unsigned d = 5; + EXPECT_EQ(d, c, "state lost"); + EXPECT_EQ(x.b, y.a, "commuted?"); + + return MonoidElement(x.a, y.b, OddTag()); + } +}; + +// Multiplication of matrix is an associative but not commutative operation +// Typically used as value type in tests involving "GENERALIZED_NONCOMMUTATIVE_SUM". +template <typename T> +struct Matrix2x2 +{ + T a[2][2]; + Matrix2x2() : a{{1, 0}, {0, 1}} {} + Matrix2x2(T x, T y) : a{{0, x}, {x, y}} {} +#if !defined(_PSTL_ICL_19_VC14_VC141_TEST_SCAN_RELEASE_BROKEN) + Matrix2x2(const Matrix2x2& m) : a{{m.a[0][0], m.a[0][1]}, {m.a[1][0], m.a[1][1]}} {} + Matrix2x2& + operator=(const Matrix2x2& m) + { + a[0][0] = m.a[0][0], a[0][1] = m.a[0][1], a[1][0] = m.a[1][0], a[1][1] = m.a[1][1]; + return *this; + } +#endif +}; + +template <typename T> +bool +operator==(const Matrix2x2<T>& left, const Matrix2x2<T>& right) +{ + return left.a[0][0] == right.a[0][0] && left.a[0][1] == right.a[0][1] && left.a[1][0] == right.a[1][0] && + left.a[1][1] == right.a[1][1]; +} + +template <typename T> +Matrix2x2<T> +multiply_matrix(const Matrix2x2<T>& left, const Matrix2x2<T>& right) +{ + Matrix2x2<T> result; + for (int32_t i = 0; i < 2; ++i) + { + for (int32_t j = 0; j < 2; ++j) + { + result.a[i][j] = left.a[i][0] * right.a[0][j] + left.a[i][1] * right.a[1][j]; + } + } + return result; +} + +//============================================================================ +// Adapters for creating different types of iterators. +// +// In this block we implemented some adapters for creating differnet types of iterators. +// It's needed for extending the unit testing of Parallel STL algorithms. +// We have adapters for iterators with different tags (forward_iterator_tag, bidirectional_iterator_tag), reverse iterators. +// The input iterator should be const or non-const, non-reverse random access iterator. +// Iterator creates in "MakeIterator": +// firstly, iterator is "packed" by "IteratorTypeAdapter" (creating forward or bidirectional iterator) +// then iterator is "packed" by "ReverseAdapter" (if it's possible) +// So, from input iterator we may create, for example, reverse bidirectional iterator. +// "Main" functor for testing iterators is named "invoke_on_all_iterator_types". + +// Base adapter +template <typename Iterator> +struct BaseAdapter +{ + typedef Iterator iterator_type; + iterator_type + operator()(Iterator it) + { + return it; + } +}; + +// Check if the iterator is reverse iterator +// Note: it works only for iterators that created by std::reverse_iterator +template <typename NotReverseIterator> +struct isReverse : std::false_type +{ +}; + +template <typename Iterator> +struct isReverse<std::reverse_iterator<Iterator>> : std::true_type +{ +}; + +// Reverse adapter +template <typename Iterator, typename IsReverse> +struct ReverseAdapter +{ + typedef std::reverse_iterator<Iterator> iterator_type; + iterator_type + operator()(Iterator it) + { +#if defined(_PSTL_CPP14_MAKE_REVERSE_ITERATOR_PRESENT) + return std::make_reverse_iterator(it); +#else + return iterator_type(it); +#endif + } +}; + +// Non-reverse adapter +template <typename Iterator> +struct ReverseAdapter<Iterator, std::false_type> : BaseAdapter<Iterator> +{ +}; + +// Iterator adapter by type (by default std::random_access_iterator_tag) +template <typename Iterator, typename IteratorTag> +struct IteratorTypeAdapter : BaseAdapter<Iterator> +{ +}; + +// Iterator adapter for forward iterator +template <typename Iterator> +struct IteratorTypeAdapter<Iterator, std::forward_iterator_tag> +{ + typedef ForwardIterator<Iterator, std::forward_iterator_tag> iterator_type; + iterator_type + operator()(Iterator it) + { + return iterator_type(it); + } +}; + +// Iterator adapter for bidirectional iterator +template <typename Iterator> +struct IteratorTypeAdapter<Iterator, std::bidirectional_iterator_tag> +{ + typedef BidirectionalIterator<Iterator, std::bidirectional_iterator_tag> iterator_type; + iterator_type + operator()(Iterator it) + { + return iterator_type(it); + } +}; + +//For creating iterator with new type +template <typename InputIterator, typename IteratorTag, typename IsReverse> +struct MakeIterator +{ + typedef IteratorTypeAdapter<InputIterator, IteratorTag> IterByType; + typedef ReverseAdapter<typename IterByType::iterator_type, IsReverse> ReverseIter; + + typename ReverseIter::iterator_type + operator()(InputIterator it) + { + return ReverseIter()(IterByType()(it)); + } +}; + +// Useful constant variables +constexpr std::size_t GuardSize = 5; +constexpr std::ptrdiff_t sizeLimit = 1000; + +template <typename Iter, typename Void = void> // local iterator_traits for non-iterators +struct iterator_traits_ +{ +}; + +template <typename Iter> // For iterators +struct iterator_traits_<Iter, + typename std::enable_if<!std::is_void<typename Iter::iterator_category>::value, void>::type> +{ + typedef typename Iter::iterator_category iterator_category; +}; + +template <typename T> // For pointers +struct iterator_traits_<T*> +{ + typedef std::random_access_iterator_tag iterator_category; +}; + +// is iterator Iter has tag Tag +template <typename Iter, typename Tag> +using is_same_iterator_category = std::is_same<typename iterator_traits_<Iter>::iterator_category, Tag>; + +// if we run with reverse or const iterators we shouldn't test the large range +template <typename IsReverse, typename IsConst> +struct invoke_if_ +{ + template <typename Op, typename... Rest> + void + operator()(bool is_allow, Op op, Rest&&... rest) + { + if (is_allow) + op(std::forward<Rest>(rest)...); + } +}; +template <> +struct invoke_if_<std::false_type, std::false_type> +{ + template <typename Op, typename... Rest> + void + operator()(bool, Op op, Rest&&... rest) + { + op(std::forward<Rest>(rest)...); + } +}; + +// Base non_const_wrapper struct. It is used to distinguish non_const testcases +// from a regular one. For non_const testcases only compilation is checked. +struct non_const_wrapper +{ +}; + +// Generic wrapper to specify iterator type to execute callable Op on. +// The condition can be either positive(Op is executed only with IteratorTag) +// or negative(Op is executed with every type of iterators except IteratorTag) +template <typename Op, typename IteratorTag, bool IsPositiveCondition = true> +struct non_const_wrapper_tagged : non_const_wrapper +{ + template <typename Policy, typename Iterator> + typename std::enable_if<IsPositiveCondition == is_same_iterator_category<Iterator, IteratorTag>::value, void>::type + operator()(Policy&& exec, Iterator iter) + { + Op()(exec, iter); + } + + template <typename Policy, typename InputIterator, typename OutputIterator> + typename std::enable_if<IsPositiveCondition == is_same_iterator_category<OutputIterator, IteratorTag>::value, + void>::type + operator()(Policy&& exec, InputIterator input_iter, OutputIterator out_iter) + { + Op()(exec, input_iter, out_iter); + } + + template <typename Policy, typename Iterator> + typename std::enable_if<IsPositiveCondition != is_same_iterator_category<Iterator, IteratorTag>::value, void>::type + operator()(Policy&&, Iterator) + { + } + + template <typename Policy, typename InputIterator, typename OutputIterator> + typename std::enable_if<IsPositiveCondition != is_same_iterator_category<OutputIterator, IteratorTag>::value, + void>::type + operator()(Policy&&, InputIterator, OutputIterator) + { + } +}; + +// These run_for_* structures specify with which types of iterators callable object Op +// should be executed. +template <typename Op> +struct run_for_rnd : non_const_wrapper_tagged<Op, std::random_access_iterator_tag> +{ +}; + +template <typename Op> +struct run_for_rnd_bi : non_const_wrapper_tagged<Op, std::forward_iterator_tag, false> +{ +}; + +template <typename Op> +struct run_for_rnd_fw : non_const_wrapper_tagged<Op, std::bidirectional_iterator_tag, false> +{ +}; + +// Invoker for different types of iterators. +template <typename IteratorTag, typename IsReverse> +struct iterator_invoker +{ + template <typename Iterator> + using make_iterator = MakeIterator<Iterator, IteratorTag, IsReverse>; + template <typename Iterator> + using IsConst = typename std::is_const< + typename std::remove_pointer<typename std::iterator_traits<Iterator>::pointer>::type>::type; + template <typename Iterator> + using invoke_if = invoke_if_<IsReverse, IsConst<Iterator>>; + + // A single iterator version which is used for non_const testcases + template <typename Policy, typename Op, typename Iterator> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value && + std::is_base_of<non_const_wrapper, Op>::value, + void>::type + operator()(Policy&& exec, Op op, Iterator iter) + { + op(std::forward<Policy>(exec), make_iterator<Iterator>()(iter)); + } + + // A version with 2 iterators which is used for non_const testcases + template <typename Policy, typename Op, typename InputIterator, typename OutputIterator> + typename std::enable_if<is_same_iterator_category<OutputIterator, std::random_access_iterator_tag>::value && + std::is_base_of<non_const_wrapper, Op>::value, + void>::type + operator()(Policy&& exec, Op op, InputIterator input_iter, OutputIterator out_iter) + { + op(std::forward<Policy>(exec), make_iterator<InputIterator>()(input_iter), + make_iterator<OutputIterator>()(out_iter)); + } + + template <typename Policy, typename Op, typename Iterator, typename Size, typename... Rest> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value, void>::type + operator()(Policy&& exec, Op op, Iterator begin, Size n, Rest&&... rest) + { + invoke_if<Iterator>()(n <= sizeLimit, op, exec, make_iterator<Iterator>()(begin), n, + std::forward<Rest>(rest)...); + } + + template <typename Policy, typename Op, typename Iterator, typename... Rest> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value && + !std::is_base_of<non_const_wrapper, Op>::value, + void>::type + operator()(Policy&& exec, Op op, Iterator inputBegin, Iterator inputEnd, Rest&&... rest) + { + invoke_if<Iterator>()(std::distance(inputBegin, inputEnd) <= sizeLimit, op, exec, + make_iterator<Iterator>()(inputBegin), make_iterator<Iterator>()(inputEnd), + std::forward<Rest>(rest)...); + } + + template <typename Policy, typename Op, typename InputIterator, typename OutputIterator, typename... Rest> + typename std::enable_if<is_same_iterator_category<OutputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&& exec, Op op, InputIterator inputBegin, InputIterator inputEnd, OutputIterator outputBegin, + Rest&&... rest) + { + invoke_if<InputIterator>()(std::distance(inputBegin, inputEnd) <= sizeLimit, op, exec, + make_iterator<InputIterator>()(inputBegin), make_iterator<InputIterator>()(inputEnd), + make_iterator<OutputIterator>()(outputBegin), std::forward<Rest>(rest)...); + } + + template <typename Policy, typename Op, typename InputIterator, typename OutputIterator, typename... Rest> + typename std::enable_if<is_same_iterator_category<OutputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&& exec, Op op, InputIterator inputBegin, InputIterator inputEnd, OutputIterator outputBegin, + OutputIterator outputEnd, Rest&&... rest) + { + invoke_if<InputIterator>()(std::distance(inputBegin, inputEnd) <= sizeLimit, op, exec, + make_iterator<InputIterator>()(inputBegin), make_iterator<InputIterator>()(inputEnd), + make_iterator<OutputIterator>()(outputBegin), + make_iterator<OutputIterator>()(outputEnd), std::forward<Rest>(rest)...); + } + + template <typename Policy, typename Op, typename InputIterator1, typename InputIterator2, typename OutputIterator, + typename... Rest> + typename std::enable_if<is_same_iterator_category<OutputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&& exec, Op op, InputIterator1 inputBegin1, InputIterator1 inputEnd1, InputIterator2 inputBegin2, + InputIterator2 inputEnd2, OutputIterator outputBegin, OutputIterator outputEnd, Rest&&... rest) + { + invoke_if<InputIterator1>()( + std::distance(inputBegin1, inputEnd1) <= sizeLimit, op, exec, make_iterator<InputIterator1>()(inputBegin1), + make_iterator<InputIterator1>()(inputEnd1), make_iterator<InputIterator2>()(inputBegin2), + make_iterator<InputIterator2>()(inputEnd2), make_iterator<OutputIterator>()(outputBegin), + make_iterator<OutputIterator>()(outputEnd), std::forward<Rest>(rest)...); + } +}; + +// Invoker for reverse iterators only +// Note: if we run with reverse iterators we shouldn't test the large range +template <typename IteratorTag> +struct iterator_invoker<IteratorTag, /* IsReverse = */ std::true_type> +{ + + template <typename Iterator> + using make_iterator = MakeIterator<Iterator, IteratorTag, std::true_type>; + + // A single iterator version which is used for non_const testcases + template <typename Policy, typename Op, typename Iterator> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value && + std::is_base_of<non_const_wrapper, Op>::value, + void>::type + operator()(Policy&& exec, Op op, Iterator iter) + { + op(std::forward<Policy>(exec), make_iterator<Iterator>()(iter)); + } + + // A version with 2 iterators which is used for non_const testcases + template <typename Policy, typename Op, typename InputIterator, typename OutputIterator> + typename std::enable_if<is_same_iterator_category<OutputIterator, std::random_access_iterator_tag>::value && + std::is_base_of<non_const_wrapper, Op>::value, + void>::type + operator()(Policy&& exec, Op op, InputIterator input_iter, OutputIterator out_iter) + { + op(std::forward<Policy>(exec), make_iterator<InputIterator>()(input_iter), + make_iterator<OutputIterator>()(out_iter)); + } + + template <typename Policy, typename Op, typename Iterator, typename Size, typename... Rest> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value, void>::type + operator()(Policy&& exec, Op op, Iterator begin, Size n, Rest&&... rest) + { + if (n <= sizeLimit) + op(exec, make_iterator<Iterator>()(begin + n), n, std::forward<Rest>(rest)...); + } + + template <typename Policy, typename Op, typename Iterator, typename... Rest> + typename std::enable_if<is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value && + !std::is_base_of<non_const_wrapper, Op>::value, + void>::type + operator()(Policy&& exec, Op op, Iterator inputBegin, Iterator inputEnd, Rest&&... rest) + { + if (std::distance(inputBegin, inputEnd) <= sizeLimit) + op(exec, make_iterator<Iterator>()(inputEnd), make_iterator<Iterator>()(inputBegin), + std::forward<Rest>(rest)...); + } + + template <typename Policy, typename Op, typename InputIterator, typename OutputIterator, typename... Rest> + typename std::enable_if<is_same_iterator_category<OutputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&& exec, Op op, InputIterator inputBegin, InputIterator inputEnd, OutputIterator outputBegin, + Rest&&... rest) + { + if (std::distance(inputBegin, inputEnd) <= sizeLimit) + op(exec, make_iterator<InputIterator>()(inputEnd), make_iterator<InputIterator>()(inputBegin), + make_iterator<OutputIterator>()(outputBegin + (inputEnd - inputBegin)), std::forward<Rest>(rest)...); + } + + template <typename Policy, typename Op, typename InputIterator, typename OutputIterator, typename... Rest> + typename std::enable_if<is_same_iterator_category<OutputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&& exec, Op op, InputIterator inputBegin, InputIterator inputEnd, OutputIterator outputBegin, + OutputIterator outputEnd, Rest&&... rest) + { + if (std::distance(inputBegin, inputEnd) <= sizeLimit) + op(exec, make_iterator<InputIterator>()(inputEnd), make_iterator<InputIterator>()(inputBegin), + make_iterator<OutputIterator>()(outputEnd), make_iterator<OutputIterator>()(outputBegin), + std::forward<Rest>(rest)...); + } + + template <typename Policy, typename Op, typename InputIterator1, typename InputIterator2, typename OutputIterator, + typename... Rest> + typename std::enable_if<is_same_iterator_category<OutputIterator, std::random_access_iterator_tag>::value, + void>::type + operator()(Policy&& exec, Op op, InputIterator1 inputBegin1, InputIterator1 inputEnd1, InputIterator2 inputBegin2, + InputIterator2 inputEnd2, OutputIterator outputBegin, OutputIterator outputEnd, Rest&&... rest) + { + if (std::distance(inputBegin1, inputEnd1) <= sizeLimit) + op(exec, make_iterator<InputIterator1>()(inputEnd1), make_iterator<InputIterator1>()(inputBegin1), + make_iterator<InputIterator2>()(inputEnd2), make_iterator<InputIterator2>()(inputBegin2), + make_iterator<OutputIterator>()(outputEnd), make_iterator<OutputIterator>()(outputBegin), + std::forward<Rest>(rest)...); + } +}; + +// We can't create reverse iterator from forward iterator +template <> +struct iterator_invoker<std::forward_iterator_tag, /*isReverse=*/std::true_type> +{ + template <typename... Rest> + void + operator()(Rest&&...) + { + } +}; + +template <typename IsReverse> +struct reverse_invoker +{ + template <typename... Rest> + void + operator()(Rest&&... rest) + { + // Random-access iterator + iterator_invoker<std::random_access_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...); + + // Forward iterator + iterator_invoker<std::forward_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...); + + // Bidirectional iterator + iterator_invoker<std::bidirectional_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...); + } +}; + +struct invoke_on_all_iterator_types +{ + template <typename... Rest> + void + operator()(Rest&&... rest) + { + reverse_invoker</* IsReverse = */ std::false_type>()(std::forward<Rest>(rest)...); + reverse_invoker</* IsReverse = */ std::true_type>()(std::forward<Rest>(rest)...); + } +}; +//============================================================================ + +// Invoke op(policy,rest...) for each possible policy. +template <typename Op, typename... T> +void +invoke_on_all_policies(Op op, T&&... rest) +{ + using namespace __pstl::execution; + + // Try static execution policies + invoke_on_all_iterator_types()(seq, op, std::forward<T>(rest)...); + invoke_on_all_iterator_types()(unseq, op, std::forward<T>(rest)...); + invoke_on_all_iterator_types()(par, op, std::forward<T>(rest)...); + invoke_on_all_iterator_types()(par_unseq, op, std::forward<T>(rest)...); +} + +template <typename F> +struct NonConstAdapter +{ + F my_f; + NonConstAdapter(const F& f) : my_f(f) {} + + template <typename... Types> + auto + operator()(Types&&... args) -> decltype(std::declval<F>(). + operator()(std::forward<Types>(args)...)) + { + return my_f(std::forward<Types>(args)...); + } +}; + +template <typename F> +NonConstAdapter<F> +non_const(const F& f) +{ + return NonConstAdapter<F>(f); +} + +// Wrapper for types. It's need for counting of constructing and destructing objects +template <typename T> +class Wrapper +{ + public: + Wrapper() + { + my_field = std::shared_ptr<T>(new T()); + ++my_count; + } + Wrapper(const T& input) + { + my_field = std::shared_ptr<T>(new T(input)); + ++my_count; + } + Wrapper(const Wrapper& input) + { + my_field = input.my_field; + ++my_count; + } + Wrapper(Wrapper&& input) + { + my_field = input.my_field; + input.my_field = nullptr; + ++move_count; + } + Wrapper& + operator=(const Wrapper& input) + { + my_field = input.my_field; + return *this; + } + Wrapper& + operator=(Wrapper&& input) + { + my_field = input.my_field; + input.my_field = nullptr; + ++move_count; + return *this; + } + bool + operator==(const Wrapper& input) const + { + return my_field == input.my_field; + } + bool + operator<(const Wrapper& input) const + { + return *my_field < *input.my_field; + } + bool + operator>(const Wrapper& input) const + { + return *my_field > *input.my_field; + } + friend std::ostream& + operator<<(std::ostream& stream, const Wrapper& input) + { + return stream << *(input.my_field); + } + ~Wrapper() + { + --my_count; + if (move_count > 0) + { + --move_count; + } + } + T* + get_my_field() const + { + return my_field.get(); + }; + static size_t + Count() + { + return my_count; + } + static size_t + MoveCount() + { + return move_count; + } + static void + SetCount(const size_t& n) + { + my_count = n; + } + static void + SetMoveCount(const size_t& n) + { + move_count = n; + } + + private: + static std::atomic<size_t> my_count; + static std::atomic<size_t> move_count; + std::shared_ptr<T> my_field; +}; + +template <typename T> +std::atomic<size_t> Wrapper<T>::my_count = {0}; + +template <typename T> +std::atomic<size_t> Wrapper<T>::move_count = {0}; + +template <typename InputIterator, typename T, typename BinaryOperation, typename UnaryOperation> +T +transform_reduce_serial(InputIterator first, InputIterator last, T init, BinaryOperation binary_op, + UnaryOperation unary_op) noexcept +{ + for (; first != last; ++first) + { + init = binary_op(init, unary_op(*first)); + } + return init; +} + +static const char* +done() +{ +#if defined(_PSTL_TEST_SUCCESSFUL_KEYWORD) + return "done"; +#else + return "passed"; +#endif +} + +// test_algo_basic_* functions are used to execute +// f on a very basic sequence of elements of type T. + +// Should be used with unary predicate +template <typename T, typename F> +static void +test_algo_basic_single(F&& f) +{ + size_t N = 10; + Sequence<T> in(N, [](size_t v) -> T { return T(v); }); + + invoke_on_all_policies(f, in.begin()); +} + +// Should be used with binary predicate +template <typename T, typename F> +static void +test_algo_basic_double(F&& f) +{ + size_t N = 10; + Sequence<T> in(N, [](size_t v) -> T { return T(v); }); + Sequence<T> out(N, [](size_t v) -> T { return T(v); }); + + invoke_on_all_policies(f, in.begin(), out.begin()); +} + +template <typename Policy, typename F> +static void +invoke_if(Policy&&, F f) +{ +#if defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) + using decay_policy = typename std::decay<Policy>::type; + using allow_unsequenced = + std::integral_constant<bool, (std::is_same<decay_policy, std::execution::unsequenced_policy>::value || + std::is_same<decay_policy, std::execution::parallel_unsequenced_policy>::value)>; + __pstl::__internal::__invoke_if_not(allow_unsequenced{}, f); +#else + f(); +#endif +} + +} /* namespace TestUtils */ |