From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gproxy2-pub.mail.unifiedlayer.com (gproxy2-pub.mail.unifiedlayer.com [69.89.18.3]) by sourceware.org (Postfix) with ESMTPS id 3F0D43858C53 for ; Mon, 4 Apr 2022 19:53:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3F0D43858C53 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw13.mail.unifiedlayer.com (unknown [10.0.90.128]) by progateway4.mail.pro1.eigbox.com (Postfix) with ESMTP id A288F100471CD for ; Mon, 4 Apr 2022 19:53:42 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id bSlinVkkdY8ycbSlin7ZkN; Mon, 04 Apr 2022 19:53:42 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=e+XD9Yl/ c=1 sm=1 tr=0 ts=624b4cc6 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=z0gMJWrwH1QA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=lAAMn_4K_BDjz6QeHLAA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=KZQIB9n+1HqJbOo+ObkXI65MWzW0KiqqF3eqllByNwQ=; b=O5UdGwCuIq+NoOJ61mVL+yNVkP qOQU7V517KMUNLAeajccWQX7sfzIR03jduRcHGIwbP2Ufztpr286mCG6C9gUovAzWssYoXv6YRiyB pWC8lEpIDiXXFEQPoJj1okfnX; Received: from 71-211-176-135.hlrn.qwest.net ([71.211.176.135]:34800 helo=prentzel.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nbSli-003uD2-0C; Mon, 04 Apr 2022 13:53:42 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v4 12/34] Return vector of results from parallel_for_each Date: Mon, 4 Apr 2022 13:53:13 -0600 Message-Id: <20220404195335.2111906-13-tom@tromey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404195335.2111906-1-tom@tromey.com> References: <20220404195335.2111906-1-tom@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 71.211.176.135 X-Source-L: No X-Exim-ID: 1nbSli-003uD2-0C X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-176-135.hlrn.qwest.net (prentzel.Home) [71.211.176.135]:34800 X-Source-Auth: tom+tromey.com X-Email-Count: 20 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3030.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Apr 2022 19:53:45 -0000 This changes gdb::parallel_for_each to return a vector of the results. However, if the passed-in function returns void, the return type remains 'void'. This functionality is used later, to parallelize the new indexer. --- gdbsupport/parallel-for.h | 142 +++++++++++++++++++++++++++++++------- gdbsupport/thread-pool.cc | 6 +- gdbsupport/thread-pool.h | 23 +++++- 3 files changed, 141 insertions(+), 30 deletions(-) diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h index 811ffd61bda..44303abb716 100644 --- a/gdbsupport/parallel-for.h +++ b/gdbsupport/parallel-for.h @@ -21,11 +21,98 @@ #define GDBSUPPORT_PARALLEL_FOR_H #include +#include #include "gdbsupport/thread-pool.h" namespace gdb { +namespace detail +{ + +/* This is a helper class that is used to accumulate results for + parallel_for. There is a specialization for 'void', below. */ +template +struct par_for_accumulator +{ +public: + + explicit par_for_accumulator (size_t n_threads) + : m_futures (n_threads) + { + } + + /* The result type that is accumulated. */ + typedef std::vector result_type; + + /* Post the Ith task to a background thread, and store a future for + later. */ + void post (size_t i, std::function task) + { + m_futures[i] + = gdb::thread_pool::g_thread_pool->post_task (std::move (task)); + } + + /* Invoke TASK in the current thread, then compute all the results + from all background tasks and put them into a result vector, + which is returned. */ + result_type finish (gdb::function_view task) + { + result_type result (m_futures.size () + 1); + + result.back () = task (); + + for (size_t i = 0; i < m_futures.size (); ++i) + result[i] = m_futures[i].get (); + + return result; + } + +private: + + /* A vector of futures coming from the tasks run in the + background. */ + std::vector> m_futures; +}; + +/* See the generic template. */ +template<> +struct par_for_accumulator +{ +public: + + explicit par_for_accumulator (size_t n_threads) + : m_futures (n_threads) + { + } + + /* This specialization does not compute results. */ + typedef void result_type; + + void post (size_t i, std::function task) + { + m_futures[i] + = gdb::thread_pool::g_thread_pool->post_task (std::move (task)); + } + + result_type finish (gdb::function_view task) + { + task (); + + for (auto &future : m_futures) + { + /* Use 'get' and not 'wait', to propagate any exception. */ + future.get (); + } + } + +private: + + std::vector> m_futures; +}; + +} + /* A very simple "parallel for". This splits the range of iterators into subranges, and then passes each subrange to the callback. The work may or may not be done in separate threads. @@ -36,21 +123,25 @@ namespace gdb The parameter N says how batching ought to be done -- there will be at least N elements processed per thread. Setting N to 0 is not - allowed. */ + allowed. + + If the function returns a non-void type, then a vector of the + results is returned. The size of the resulting vector depends on + the number of threads that were used. */ template -void +typename gdb::detail::par_for_accumulator< + std::result_of_t + >::result_type parallel_for_each (unsigned n, RandomIt first, RandomIt last, RangeFunction callback) { - /* So we can use a local array below. */ - const size_t local_max = 16; - size_t n_threads = std::min (thread_pool::g_thread_pool->thread_count (), - local_max); - size_t n_actual_threads = 0; - std::future futures[local_max]; + typedef typename std::result_of_t + result_type; + size_t n_threads = thread_pool::g_thread_pool->thread_count (); size_t n_elements = last - first; + size_t elts_per_thread = 0; if (n_threads > 1) { /* Require that there should be at least N elements in a @@ -58,26 +149,27 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last, gdb_assert (n > 0); if (n_elements / n_threads < n) n_threads = std::max (n_elements / n, (size_t) 1); - size_t elts_per_thread = n_elements / n_threads; - n_actual_threads = n_threads - 1; - for (int i = 0; i < n_actual_threads; ++i) - { - RandomIt end = first + elts_per_thread; - auto task = [=] () - { - callback (first, end); - }; - - futures[i] = gdb::thread_pool::g_thread_pool->post_task (task); - first = end; - } + elts_per_thread = n_elements / n_threads; } - /* Process all the remaining elements in the main thread. */ - callback (first, last); + size_t count = n_threads == 0 ? 0 : n_threads - 1; + gdb::detail::par_for_accumulator results (count); - for (int i = 0; i < n_actual_threads; ++i) - futures[i].wait (); + for (int i = 0; i < count; ++i) + { + RandomIt end = first + elts_per_thread; + results.post (i, [=] () + { + return callback (first, end); + }); + first = end; + } + + /* Process all the remaining elements in the main thread. */ + return results.finish ([=] () + { + return callback (first, last); + }); } } diff --git a/gdbsupport/thread-pool.cc b/gdbsupport/thread-pool.cc index 7d446952cc7..efd8b799713 100644 --- a/gdbsupport/thread-pool.cc +++ b/gdbsupport/thread-pool.cc @@ -134,11 +134,10 @@ thread_pool::set_thread_count (size_t num_threads) #endif /* CXX_STD_THREAD */ } -std::future -thread_pool::post_task (std::function &&func) +void +thread_pool::do_post_task (std::packaged_task &&func) { std::packaged_task t (std::move (func)); - std::future f = t.get_future (); #if CXX_STD_THREAD if (m_thread_count != 0) @@ -153,7 +152,6 @@ thread_pool::post_task (std::function &&func) /* Just execute it now. */ t (); } - return f; } #if CXX_STD_THREAD diff --git a/gdbsupport/thread-pool.h b/gdbsupport/thread-pool.h index c5d5426d209..fa2ad13f4f4 100644 --- a/gdbsupport/thread-pool.h +++ b/gdbsupport/thread-pool.h @@ -64,7 +64,24 @@ class thread_pool /* Post a task to the thread pool. A future is returned, which can be used to wait for the result. */ - std::future post_task (std::function &&func); + std::future post_task (std::function &&func) + { + std::packaged_task task (std::move (func)); + std::future result = task.get_future (); + do_post_task (std::packaged_task (std::move (task))); + return result; + } + + /* Post a task to the thread pool. A future is returned, which can + be used to wait for the result. */ + template + std::future post_task (std::function &&func) + { + std::packaged_task task (std::move (func)); + std::future result = task.get_future (); + do_post_task (std::packaged_task (std::move (task))); + return result; + } private: @@ -74,6 +91,10 @@ class thread_pool /* The callback for each worker thread. */ void thread_function (); + /* Post a task to the thread pool. A future is returned, which can + be used to wait for the result. */ + void do_post_task (std::packaged_task &&func); + /* The current thread count. */ size_t m_thread_count = 0; -- 2.34.1