From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway31.websitewelcome.com (gateway31.websitewelcome.com [192.185.144.91]) by sourceware.org (Postfix) with ESMTPS id B14783858D39 for ; Sat, 4 Dec 2021 20:38:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B14783858D39 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tromey.com Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway31.websitewelcome.com (Postfix) with ESMTP id 2FDBB107313 for ; Sat, 4 Dec 2021 14:38:50 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id tbo2mrOMHQOwQtbo2mwtHS; Sat, 04 Dec 2021 14:38:50 -0600 X-Authority-Reason: nr=8 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=jqIocYB7BrjrWt05q5bdKkdnL9QLFUmchIj/XsFvIpc=; b=q/0Hdwly9rFu9wZoonxKojMEoI PRc8t+Bipw8dxAIna5YlmjnEotHQyC5HoXZnohgkoXekX+8J+bZlOwu3cb4sFdxT29XrFdOoWjTzO E269+2Wn+3g+utPdYSFrrB3YW; Received: from 97-122-84-67.hlrn.qwest.net ([97.122.84.67]:51988 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mtbo1-000ewu-TA; Sat, 04 Dec 2021 13:38:49 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v3 11/33] Return vector of results from parallel_for_each Date: Sat, 4 Dec 2021 13:38:22 -0700 Message-Id: <20211204203844.1188999-12-tom@tromey.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211204203844.1188999-1-tom@tromey.com> References: <20211204203844.1188999-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: 97.122.84.67 X-Source-L: No X-Exim-ID: 1mtbo1-000ewu-TA X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-84-67.hlrn.qwest.net (localhost.localdomain) [97.122.84.67]:51988 X-Source-Auth: tom+tromey.com X-Email-Count: 12 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3031.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NEUTRAL, TXREP 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: Sat, 04 Dec 2021 20:38:52 -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 | 146 +++++++++++++++++++++++++++++++------- gdbsupport/thread-pool.cc | 6 +- gdbsupport/thread-pool.h | 23 +++++- 3 files changed, 144 insertions(+), 31 deletions(-) diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h index 54027e69402..f8a2687c965 100644 --- a/gdbsupport/parallel-for.h +++ b/gdbsupport/parallel-for.h @@ -21,6 +21,7 @@ #define GDBSUPPORT_PARALLEL_FOR_H #include +#include #if CXX_STD_THREAD #include #include "gdbsupport/thread-pool.h" @@ -29,6 +30,92 @@ 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 (std::function 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 (std::function 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. @@ -39,22 +126,28 @@ 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) { -#if CXX_STD_THREAD - /* 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 = 1; +#if CXX_STD_THREAD + 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 @@ -62,29 +155,30 @@ 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; } #endif /* CXX_STD_THREAD */ - /* 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); #if CXX_STD_THREAD - 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; + } #endif /* CXX_STD_THREAD */ + + /* 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 2bb75cc9cef..f2252ffc9fd 100644 --- a/gdbsupport/thread-pool.cc +++ b/gdbsupport/thread-pool.cc @@ -129,11 +129,10 @@ thread_pool::set_thread_count (size_t num_threads) m_thread_count = num_threads; } -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 (m_thread_count == 0) { @@ -146,7 +145,6 @@ thread_pool::post_task (std::function &&func) m_tasks.emplace (std::move (t)); m_tasks_cv.notify_one (); } - return f; } void diff --git a/gdbsupport/thread-pool.h b/gdbsupport/thread-pool.h index 9bddaa9eaae..bf0e03f005a 100644 --- a/gdbsupport/thread-pool.h +++ b/gdbsupport/thread-pool.h @@ -58,7 +58,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: @@ -67,6 +84,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.31.1