Line data Source code
1 : //@HEADER 2 : // ************************************************************************ 3 : // 4 : // Kokkos v. 4.0 5 : // Copyright (2022) National Technology & Engineering 6 : // Solutions of Sandia, LLC (NTESS). 7 : // 8 : // Under the terms of Contract DE-NA0003525 with NTESS, 9 : // the U.S. Government retains certain rights in this software. 10 : // 11 : // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. 12 : // See https://kokkos.org/LICENSE for license information. 13 : // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 14 : // 15 : //@HEADER 16 : 17 : #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE 18 : #include <Kokkos_Macros.hpp> 19 : static_assert(false, 20 : "Including non-public Kokkos header files is not allowed."); 21 : #endif 22 : #ifndef KOKKOS_OPENMP_HPP 23 : #define KOKKOS_OPENMP_HPP 24 : 25 : #include <Kokkos_Macros.hpp> 26 : #if defined(KOKKOS_ENABLE_OPENMP) 27 : 28 : #include <Kokkos_Core_fwd.hpp> 29 : 30 : #include <Kokkos_HostSpace.hpp> 31 : #include <Kokkos_ScratchSpace.hpp> 32 : #include <Kokkos_Parallel.hpp> 33 : #include <Kokkos_Layout.hpp> 34 : #include <impl/Kokkos_HostSharedPtr.hpp> 35 : #include <impl/Kokkos_Profiling_Interface.hpp> 36 : #include <impl/Kokkos_InitializationSettings.hpp> 37 : 38 : #include <omp.h> 39 : 40 : #include <cstddef> 41 : #include <iosfwd> 42 : #include <vector> 43 : 44 : /*--------------------------------------------------------------------------*/ 45 : 46 : namespace Kokkos { 47 : 48 : namespace Impl { 49 : class OpenMPInternal; 50 : } // namespace Impl 51 : 52 : /// \class OpenMP 53 : /// \brief Kokkos device for multicore processors in the host memory space. 54 7416 : class OpenMP { 55 : public: 56 : //! Tag this class as a kokkos execution space 57 : using execution_space = OpenMP; 58 : 59 : using memory_space = HostSpace; 60 : 61 : //! This execution space preferred device_type 62 : using device_type = Kokkos::Device<execution_space, memory_space>; 63 : using array_layout = LayoutRight; 64 : using size_type = memory_space::size_type; 65 : using scratch_memory_space = ScratchMemorySpace<OpenMP>; 66 : 67 : OpenMP(); 68 : 69 : explicit OpenMP(int pool_size); 70 : 71 : #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4 72 : template <typename T = void> 73 : KOKKOS_DEPRECATED_WITH_COMMENT( 74 : "OpenMP execution space should be constructed explicitly.") 75 : OpenMP(int pool_size) 76 : : OpenMP(pool_size) {} 77 : #endif 78 : 79 : /// \brief Print configuration information to the given output stream. 80 : void print_configuration(std::ostream& os, bool verbose = false) const; 81 : 82 : #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4 83 : /// \brief is the instance running a parallel algorithm 84 : KOKKOS_DEPRECATED static bool in_parallel(OpenMP const& = OpenMP()) noexcept; 85 : #endif 86 : 87 : /// \brief Wait until all dispatched functors complete on the given instance 88 : /// 89 : /// This is a no-op on OpenMP 90 : static void impl_static_fence(std::string const& name); 91 : 92 : void fence(std::string const& name = 93 : "Kokkos::OpenMP::fence: Unnamed Instance Fence") const; 94 : 95 : #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4 96 : /// \brief Does the given instance return immediately after launching 97 : /// a parallel algorithm 98 : /// 99 : /// This always returns false on OpenMP 100 : KOKKOS_DEPRECATED inline static bool is_asynchronous( 101 : OpenMP const& = OpenMP()) noexcept { 102 : return false; 103 : } 104 : #endif 105 : 106 : #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4 107 : static int concurrency(OpenMP const& = OpenMP()); 108 : #else 109 : int concurrency() const; 110 : #endif 111 : 112 : static void impl_initialize(InitializationSettings const&); 113 : 114 : /// \brief is the default execution space initialized for current 'master' 115 : /// thread 116 : static bool impl_is_initialized() noexcept; 117 : 118 : /// \brief Free any resources being consumed by the default execution space 119 : static void impl_finalize(); 120 : 121 : int impl_thread_pool_size() const noexcept; 122 : 123 : int impl_thread_pool_size(int depth) const; 124 : 125 : /** \brief The rank of the executing thread in this thread pool */ 126 : inline static int impl_thread_pool_rank() noexcept; 127 : 128 : // use UniqueToken 129 : static int impl_max_hardware_threads() noexcept; 130 : 131 : // use UniqueToken 132 : KOKKOS_INLINE_FUNCTION 133 : static int impl_hardware_thread_id() noexcept; 134 : 135 : static int impl_get_current_max_threads() noexcept; 136 : 137 2472 : Impl::OpenMPInternal* impl_internal_space_instance() const { 138 1236 : return m_space_instance.get(); 139 : } 140 : 141 : static constexpr const char* name() noexcept { return "OpenMP"; } 142 : uint32_t impl_instance_id() const noexcept { return 1; } 143 : 144 : private: 145 : friend bool operator==(OpenMP const& lhs, OpenMP const& rhs) { 146 : return lhs.impl_internal_space_instance() == 147 : rhs.impl_internal_space_instance(); 148 : } 149 : friend bool operator!=(OpenMP const& lhs, OpenMP const& rhs) { 150 : return !(lhs == rhs); 151 : } 152 : Kokkos::Impl::HostSharedPtr<Impl::OpenMPInternal> m_space_instance; 153 : }; 154 : 155 : inline int OpenMP::impl_thread_pool_rank() noexcept { 156 : KOKKOS_IF_ON_HOST((return omp_get_thread_num();)) 157 : 158 : KOKKOS_IF_ON_DEVICE((return -1;)) 159 : } 160 : 161 : inline int OpenMP::impl_thread_pool_size(int depth) const { 162 : return depth < 2 ? impl_thread_pool_size() : 1; 163 : } 164 : 165 : KOKKOS_INLINE_FUNCTION 166 : int OpenMP::impl_hardware_thread_id() noexcept { 167 : KOKKOS_IF_ON_HOST((return omp_get_thread_num();)) 168 : 169 : KOKKOS_IF_ON_DEVICE((return -1;)) 170 : } 171 : 172 : namespace Tools { 173 : namespace Experimental { 174 : template <> 175 : struct DeviceTypeTraits<OpenMP> { 176 : static constexpr DeviceType id = DeviceType::OpenMP; 177 : static int device_id(const OpenMP&) { return 0; } 178 : }; 179 : } // namespace Experimental 180 : } // namespace Tools 181 : } // namespace Kokkos 182 : 183 : /*--------------------------------------------------------------------------*/ 184 : /*--------------------------------------------------------------------------*/ 185 : 186 : namespace Kokkos { 187 : namespace Impl { 188 : 189 : template <> 190 : struct MemorySpaceAccess<Kokkos::OpenMP::memory_space, 191 : Kokkos::OpenMP::scratch_memory_space> { 192 : enum : bool { assignable = false }; 193 : enum : bool { accessible = true }; 194 : enum : bool { deepcopy = false }; 195 : }; 196 : 197 : } // namespace Impl 198 : } // namespace Kokkos 199 : 200 : /*--------------------------------------------------------------------------*/ 201 : /*--------------------------------------------------------------------------*/ 202 : 203 : #include <OpenMP/Kokkos_OpenMP_Instance.hpp> 204 : #include <OpenMP/Kokkos_OpenMP_Team.hpp> 205 : #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4 206 : #include <OpenMP/Kokkos_OpenMP_Task.hpp> 207 : #endif 208 : 209 : #include <KokkosExp_MDRangePolicy.hpp> 210 : /*--------------------------------------------------------------------------*/ 211 : 212 : #endif /* #if defined( KOKKOS_ENABLE_OPENMP ) && defined( _OPENMP ) */ 213 : #endif /* #ifndef KOKKOS_OPENMP_HPP */