Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_AcquireUniqueTokenImpl.hpp
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_ACQUIRE_UNIQUE_TOKEN_IMPL_HPP
23 #define KOKKOS_ACQUIRE_UNIQUE_TOKEN_IMPL_HPP
24 
25 #include <Kokkos_Core.hpp>
26 #include <Kokkos_UniqueToken.hpp>
27 namespace Kokkos {
28 namespace Experimental {
29 
30 template <typename TeamPolicy>
31 KOKKOS_FUNCTION AcquireTeamUniqueToken<TeamPolicy>::AcquireTeamUniqueToken(
32  AcquireTeamUniqueToken<TeamPolicy>::token_type t, team_member_type team)
33  : my_token(t), my_team_acquired_val(team.team_scratch(0)), my_team(team) {
34  Kokkos::single(Kokkos::PerTeam(my_team),
35  [&]() { my_team_acquired_val() = my_token.acquire(); });
36  my_team.team_barrier();
37 
38  my_acquired_val = my_team_acquired_val();
39 }
40 
41 template <typename TeamPolicy>
42 KOKKOS_FUNCTION AcquireTeamUniqueToken<TeamPolicy>::~AcquireTeamUniqueToken() {
43  my_team.team_barrier();
44  Kokkos::single(Kokkos::PerTeam(my_team),
45  [&]() { my_token.release(my_acquired_val); });
46  my_team.team_barrier();
47 }
48 
49 } // namespace Experimental
50 } // namespace Kokkos
51 
52 #endif // KOKKOS_UNIQUE_TOKEN_HPP