From 54a32a4ed27f1fdae913065079f652191cb80ef0 Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Fri, 28 Jul 2023 08:07:43 +0100 Subject: [PATCH 1/4] include `accumulate_block()` struct - opted for version that returns type _Tp - - void version in book takes three arguments - - _Tp version in book takes two --- listings/listing_9.3.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/listings/listing_9.3.cpp b/listings/listing_9.3.cpp index 47278c2..f5d59bb 100644 --- a/listings/listing_9.3.cpp +++ b/listings/listing_9.3.cpp @@ -2,6 +2,14 @@ #include #include +template +struct accumulate_block { + _Tp operator() (_ForwardIt __first, _ForwardIt __last) + { + return std::accumulate(__first, __last, __Tp{}); + } +}; + template T parallel_accumulate(Iterator first,Iterator last,T init) { From 773b41d69b8a1a7219d07c40c148516ce98a88df Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Fri, 28 Jul 2023 08:13:36 +0100 Subject: [PATCH 2/4] complete call to `accumulate_block()` - struct called without arguments - add arguments according to earlier operator() definition --- listings/listing_9.3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/listing_9.3.cpp b/listings/listing_9.3.cpp index f5d59bb..f3b91c8 100644 --- a/listings/listing_9.3.cpp +++ b/listings/listing_9.3.cpp @@ -29,7 +29,7 @@ T parallel_accumulate(Iterator first,Iterator last,T init) { Iterator block_end=block_start; std::advance(block_end,block_size); - futures[i]=pool.submit(accumulate_block()); + futures[i]=pool.submit(accumulate_block()(block_start,block_end)); block_start=block_end; } T last_result=accumulate_block()(block_start,last); From 687189faefae849f33a96586fc6aef5b78c414ac Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Fri, 28 Jul 2023 08:16:06 +0100 Subject: [PATCH 3/4] mirror example from book - book passes accumulate_block as part of a lambda function --- listings/listing_9.3.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/listings/listing_9.3.cpp b/listings/listing_9.3.cpp index f3b91c8..5f3d631 100644 --- a/listings/listing_9.3.cpp +++ b/listings/listing_9.3.cpp @@ -29,7 +29,9 @@ T parallel_accumulate(Iterator first,Iterator last,T init) { Iterator block_end=block_start; std::advance(block_end,block_size); - futures[i]=pool.submit(accumulate_block()(block_start,block_end)); + futures[i]=pool.submit([=] { + accumulate_block()(block_start,block_end); + }); block_start=block_end; } T last_result=accumulate_block()(block_start,last); From e78bb109fea51810982969596829d7f9e402b8c5 Mon Sep 17 00:00:00 2001 From: ITHelpDec <34002836+ITHelpDec@users.noreply.github.com> Date: Fri, 28 Jul 2023 08:20:54 +0100 Subject: [PATCH 4/4] correct lambda function - accumulate_block::operator() returns type _Tp - we must call `return` on the struct's call inside the lambda to capture this return type - without this, the code will fail to compile, and provide the following error: - - "No viable overloaded '='" --- listings/listing_9.3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listings/listing_9.3.cpp b/listings/listing_9.3.cpp index 5f3d631..7a963dc 100644 --- a/listings/listing_9.3.cpp +++ b/listings/listing_9.3.cpp @@ -30,7 +30,7 @@ T parallel_accumulate(Iterator first,Iterator last,T init) Iterator block_end=block_start; std::advance(block_end,block_size); futures[i]=pool.submit([=] { - accumulate_block()(block_start,block_end); + return accumulate_block()(block_start,block_end); }); block_start=block_end; }