From 37fc983f0684d1414daeb141ba38f04c875cb683 Mon Sep 17 00:00:00 2001 From: Matteo Cicuttin <datafl4sh@toxicnet.eu> Date: Sun, 29 Mar 2020 14:57:06 +0200 Subject: [PATCH] Spinlock without lock_guard. --- kokkos-testing/fd_catalog/fd_wave_cpu.hpp | 55 +++++++++++------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/kokkos-testing/fd_catalog/fd_wave_cpu.hpp b/kokkos-testing/fd_catalog/fd_wave_cpu.hpp index 88a5f76..120620b 100644 --- a/kokkos-testing/fd_catalog/fd_wave_cpu.hpp +++ b/kokkos-testing/fd_catalog/fd_wave_cpu.hpp @@ -543,7 +543,7 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) /* Multithreading stuff */ #ifdef USE_SPINLOCK spin_lock splock; - #define GUARDED_BLOCK std::lock_guard<spin_lock> lg(splock); + //#define GUARDED_BLOCK std::lock_guard<spin_lock> lg(splock); #else std::mutex cv_mtx; std::condition_variable prod_cv; @@ -561,15 +561,15 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) while (1) { #ifdef USE_SPINLOCK - { - GUARDED_BLOCK; - if (thread_done[thread_id]) - continue; + splock.lock(); + int done = thread_done[thread_id]; + splock.unlock(); + + if (done) + continue; - if (iteration_finished) - return; - } - + if (iteration_finished) + return; #else /* Wait for the producer to notify that there's something to do */ { @@ -590,15 +590,14 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) /* Work for this thread finished, notify producer */ #ifdef USE_SPINLOCK - { - GUARDED_BLOCK; - thread_done[thread_id] = true; - times[thread_id] += ms.count(); - } + splock.lock(); + thread_done[thread_id] = 1; + times[thread_id] += ms.count(); + splock.unlock(); #else std::unique_lock<std::mutex> lck(cv_mtx); prod_cv.notify_one(); - thread_done[thread_id] = true; + thread_done[thread_id] = 1; times[thread_id] += ms.count(); #endif /* USE_SPINLOCK */ } @@ -618,18 +617,19 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) auto start = std::chrono::high_resolution_clock::now(); #ifdef USE_SPINLOCK - { - GUARDED_BLOCK; - for (auto& td : thread_done) - td = 0; - } + splock.lock(); + for (auto& td : thread_done) + td = 0; + splock.unlock(); while(1) { int ttd = 0; - GUARDED_BLOCK; + + splock.lock(); for (auto& td : thread_done) ttd += td; + splock.unlock(); if (ttd == nths) break; @@ -666,17 +666,16 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) /* Tell all the threads to stop */ #ifdef USE_SPINLOCK - { - GUARDED_BLOCK; - for (size_t i = 0; i < nths; i++) - thread_done[i] = false; - iteration_finished = true; - } + splock.lock(); + for (size_t i = 0; i < nths; i++) + thread_done[i] = 0; + iteration_finished = true; + splock.unlock(); #else { std::unique_lock<std::mutex> lck(cv_mtx); for (size_t i = 0; i < nths; i++) - thread_done[i] = false; + thread_done[i] = 0; iteration_finished = true; cons_cv.notify_all(); } -- GitLab