From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway24.websitewelcome.com (gateway24.websitewelcome.com [192.185.51.61]) by sourceware.org (Postfix) with ESMTPS id 1D4653857809 for ; Thu, 4 Nov 2021 18:11:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1D4653857809 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 cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway24.websitewelcome.com (Postfix) with ESMTP id 05BD09A8F for ; Thu, 4 Nov 2021 13:11:12 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id ihAlmB9DZG0jLihAlmElNu; Thu, 04 Nov 2021 13:09:11 -0500 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=a/2JOCjoxShu6fv7XB5C4XaInzFGJbOIiN/lvM2aimQ=; b=Fo7oT+/ewYzZWLenYUt3ZGb3N7 1grFf7mqK+MZeWM43Hu8TmJtmB62uFJIKt0yysH0Wv2+nL79F6RxyRUHFde5claJaPzKyCjZO+Swd XbJ+aOtoTr4LUR8dC/UVlUB6n; Received: from 75-166-134-234.hlrn.qwest.net ([75.166.134.234]:51958 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 1mihAl-003Gzb-Ls; Thu, 04 Nov 2021 12:09:11 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 11/32] Return vector of results from parallel_for_each Date: Thu, 4 Nov 2021 12:08:46 -0600 Message-Id: <20211104180907.2360627-12-tom@tromey.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211104180907.2360627-1-tom@tromey.com> References: <20211104180907.2360627-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: 75.166.134.234 X-Source-L: No X-Exim-ID: 1mihAl-003Gzb-Ls X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-134-234.hlrn.qwest.net (localhost.localdomain) [75.166.134.234]:51958 X-Source-Auth: tom+tromey.com X-Email-Count: 16 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3032.1 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_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: Thu, 04 Nov 2021 18:11:40 -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 | 143 +++++++++++++++++++++++++++++++------- gdbsupport/thread-pool.cc | 6 +- gdbsupport/thread-pool.h | 23 +++++- 3 files changed, 141 insertions(+), 31 deletions(-) diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h index 54027e69402..ee1f283a0bd 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,89 @@ 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) + future.wait (); + } + +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 +123,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 +152,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