Skip to content

Commit

Permalink
Simplify fence functions in the Threads backend (kokkos#6571)
Browse files Browse the repository at this point in the history
* Simplify fence functions in the Threads backend

* Remove extra blank space
  • Loading branch information
Rombur committed Nov 7, 2023
1 parent 8d9400e commit a453e9f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 49 deletions.
8 changes: 0 additions & 8 deletions core/src/Threads/Kokkos_Threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ static_assert(false,

/*--------------------------------------------------------------------------*/

namespace Kokkos {
namespace Impl {
enum class fence_is_static { yes, no };
} // namespace Impl
} // namespace Kokkos

/*--------------------------------------------------------------------------*/

namespace Kokkos {

/** \brief Execution space for a pool of C++11 threads on a CPU. */
Expand Down
53 changes: 18 additions & 35 deletions core/src/Threads/Kokkos_Threads_Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,46 +253,29 @@ int ThreadsInternal::in_parallel() {
return s_current_function && (&s_threads_process != s_current_function_arg) &&
(s_threads_process.m_pool_base || !is_process());
}
void ThreadsInternal::fence() { internal_fence(Impl::fence_is_static::yes); }
void ThreadsInternal::fence(const std::string &name) {
internal_fence(name, Impl::fence_is_static::yes);
void ThreadsInternal::fence() {
fence("Kokkos::ThreadsInternal::fence: Unnamed Instance Fence");
}

void ThreadsInternal::internal_fence(Impl::fence_is_static is_static) {
internal_fence((is_static == Impl::fence_is_static::no)
? "Kokkos::ThreadsInternal::fence: Unnamed Instance Fence"
: "Kokkos::ThreadsInternal::fence: Unnamed Static Fence",
is_static);
void ThreadsInternal::fence(const std::string &name) {
Kokkos::Tools::Experimental::Impl::profile_fence_event<Kokkos::Threads>(
name, Kokkos::Tools::Experimental::Impl::DirectFenceIDHandle{1},
internal_fence);
}

// Wait for root thread to become inactive
void ThreadsInternal::internal_fence(const std::string &name,
Impl::fence_is_static is_static) {
const auto &fence_lam = [&]() {
if (s_thread_pool_size[0]) {
// Wait for the root thread to complete:
Impl::spinwait_while_equal(s_threads_exec[0]->m_pool_state,
ThreadState::Active);
}
void ThreadsInternal::internal_fence() {
if (s_thread_pool_size[0]) {
// Wait for the root thread to complete:
Impl::spinwait_while_equal(s_threads_exec[0]->m_pool_state,
ThreadState::Active);
}

s_current_function = nullptr;
s_current_function_arg = nullptr;
s_current_function = nullptr;
s_current_function_arg = nullptr;

// Make sure function and arguments are cleared before
// potentially re-activating threads with a subsequent launch.
memory_fence();
};
if (is_static == Impl::fence_is_static::yes) {
Kokkos::Tools::Experimental::Impl::profile_fence_event<Kokkos::Threads>(
name,
Kokkos::Tools::Experimental::SpecialSynchronizationCases::
GlobalDeviceSynchronization,
fence_lam);
} else {
Kokkos::Tools::Experimental::Impl::profile_fence_event<Kokkos::Threads>(
name, Kokkos::Tools::Experimental::Impl::DirectFenceIDHandle{1},
fence_lam);
}
// Make sure function and arguments are cleared before
// potentially re-activating threads with a subsequent launch.
memory_fence();
}

/** \brief Begin execution of the asynchronous functor */
Expand Down Expand Up @@ -710,7 +693,7 @@ int Threads::concurrency() const { return impl_thread_pool_size(0); }
#endif

void Threads::fence(const std::string &name) const {
Impl::ThreadsInternal::internal_fence(name, Impl::fence_is_static::no);
Impl::ThreadsInternal::fence(name);
}

Threads &Threads::impl_instance(int) {
Expand Down
12 changes: 6 additions & 6 deletions core/src/Threads/Kokkos_Threads_Instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,7 @@ class ThreadsInternal {
static int in_parallel();
static void fence();
static void fence(const std::string &);
static void internal_fence(
Impl::fence_is_static is_static = Impl::fence_is_static::yes);
static void internal_fence(
const std::string &,
Impl::fence_is_static is_static = Impl::fence_is_static::yes);
static void internal_fence();

/* Dynamic Scheduling related functionality */
// Initialize the work range for this thread
Expand Down Expand Up @@ -572,7 +568,11 @@ inline void Threads::print_configuration(std::ostream &os, bool verbose) const {
}

inline void Threads::impl_static_fence(const std::string &name) {
Impl::ThreadsInternal::internal_fence(name, Impl::fence_is_static::yes);
Kokkos::Tools::Experimental::Impl::profile_fence_event<Kokkos::Threads>(
name,
Kokkos::Tools::Experimental::SpecialSynchronizationCases::
GlobalDeviceSynchronization,
Impl::ThreadsInternal::internal_fence);
}
} /* namespace Kokkos */

Expand Down

0 comments on commit a453e9f

Please sign in to comment.