From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 0ECB43858D28; Tue, 17 Jan 2023 14:15:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0ECB43858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673964923; bh=ZlqHdmsGqq+Bjn+XlzDg/xZnmEIQ7izB3cRNAxJk8fM=; h=From:To:Subject:Date:From; b=ogkp6z4FZ1O5lmJZ3XGtwBmkORY+5QScs/ezy13P0+ubYGAJD0ajQalzR8jOGWi8Z uGUbjzFRyv7iraR0u53BJrYcX9i0KAgXm+O1tjHKezcfEXy/PT8uddIl9b/t3TWSVF xFBGTkCtnFp65hMJ3axIXx2xlsCo73+odW8pyRzE= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb/gdb-13-branch] Avoid submitting empty tasks in parallel_for_each X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/gdb-13-branch X-Git-Oldrev: 2ec9694617bc3a84ef89588d849eaef8893c0ec4 X-Git-Newrev: 6107546876df070688d75d8cd87d7c7a5f246091 Message-Id: <20230117141523.0ECB43858D28@sourceware.org> Date: Tue, 17 Jan 2023 14:15:23 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D6107546876df= 070688d75d8cd87d7c7a5f246091 commit 6107546876df070688d75d8cd87d7c7a5f246091 Author: Tom Tromey Date: Tue Dec 13 12:03:34 2022 -0700 Avoid submitting empty tasks in parallel_for_each =20 I found that parallel_for_each would submit empty tasks to the thread pool. For example, this can happen if the number of tasks is smaller than the number of available threads. In the DWARF reader, this resulted in the cooked index containing empty sub-indices. This patch arranges to instead shrink the result vector and process the trailing entries in the calling thread. =20 (cherry picked from commit 63078a04984b73e1fdeb4571a63605ee5c13f929) Diff: --- gdb/unittests/parallel-for-selftests.c | 39 ++++++++++++++++++++++++++++++= ++++ gdbsupport/parallel-for.h | 30 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/gdb/unittests/parallel-for-selftests.c b/gdb/unittests/paralle= l-for-selftests.c index 3162db18df1..15a095ae62b 100644 --- a/gdb/unittests/parallel-for-selftests.c +++ b/gdb/unittests/parallel-for-selftests.c @@ -149,6 +149,45 @@ TEST (int n_threads) SELF_CHECK (counter =3D=3D NUMBER); =20 #undef NUMBER + + /* Check that if there are fewer tasks than threads, then we won't + end up with a null result. */ + std::vector> intresults; + std::atomic any_empty_tasks (false); + + FOR_EACH (1, 0, 1, + [&] (int start, int end) + { + if (start =3D=3D end) + any_empty_tasks =3D true; + return std::unique_ptr (new int (end - start)); + }); + SELF_CHECK (!any_empty_tasks); + SELF_CHECK (std::all_of (intresults.begin (), + intresults.end (), + [] (const std::unique_ptr &entry) + { + return entry !=3D nullptr; + })); + + /* The same but using the task size parameter. */ + intresults.clear (); + any_empty_tasks =3D false; + FOR_EACH (1, 0, 1, + [&] (int start, int end) + { + if (start =3D=3D end) + any_empty_tasks =3D true; + return std::unique_ptr (new int (end - start)); + }, + task_size_one); + SELF_CHECK (!any_empty_tasks); + SELF_CHECK (std::all_of (intresults.begin (), + intresults.end (), + [] (const std::unique_ptr &entry) + { + return entry !=3D nullptr; + })); } =20 #endif /* FOR_EACH */ diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h index b565676a0d0..de9ebb15746 100644 --- a/gdbsupport/parallel-for.h +++ b/gdbsupport/parallel-for.h @@ -70,6 +70,12 @@ public: return result; } =20 + /* Resize the results to N. */ + void resize (size_t n) + { + m_futures.resize (n); + } + private: =20 /* A vector of futures coming from the tasks run in the @@ -108,6 +114,12 @@ public: } } =20 + /* Resize the results to N. */ + void resize (size_t n) + { + m_futures.resize (n); + } + private: =20 std::vector> m_futures; @@ -232,6 +244,24 @@ parallel_for_each (unsigned n, RandomIt first, RandomI= t last, end =3D j; remaining_size -=3D chunk_size; } + + /* This case means we don't have enough elements to really + distribute them. Rather than ever submit a task that does + nothing, we short-circuit here. */ + if (first =3D=3D end) + end =3D last; + + if (end =3D=3D last) + { + /* We're about to dispatch the last batch of elements, which + we normally process in the main thread. So just truncate + the result list here. This avoids submitting empty tasks + to the thread pool. */ + count =3D i; + results.resize (count); + break; + } + if (parallel_for_each_debug) { debug_printf (_("Parallel for: elements on worker thread %i\t: %zu"),