37#include <gtest/gtest.h>
82 auto result =
pmaps(pool, small_vec, [](
int x) {
return x * x; });
83 ASSERT_EQ(result.size(), 5u);
84 EXPECT_EQ(result, (std::vector<int>{1, 4, 9, 16, 25}));
89 auto result = pmaps<double>(pool, small_vec, [](
int x) {
return x * 1.5; });
90 ASSERT_EQ(result.size(), 5u);
91 EXPECT_NEAR(result[0], 1.5, 0.001);
92 EXPECT_NEAR(result[4], 7.5, 0.001);
97 auto result =
pmaps(pool, small_vec, [](
int x) {
return std::to_string(x); });
98 ASSERT_EQ(result.size(), 5u);
99 EXPECT_EQ(result[0],
"1");
100 EXPECT_EQ(result[4],
"5");
105 auto result =
pmaps(pool, empty_vec, [](
int x) {
return x * 2; });
106 EXPECT_TRUE(result.empty());
111 auto result =
pmaps(pool, single_vec, [](
int x) {
return x * 2; });
112 ASSERT_EQ(result.size(), 1u);
113 EXPECT_EQ(result[0], 84);
118 auto result =
pmaps(pool, large_vec, [](
int x) {
return x * 2; });
119 ASSERT_EQ(result.size(), 10000u);
120 for (
size_t i = 0; i < result.size(); ++i)
121 EXPECT_EQ(result[i],
static_cast<int>((i + 1) * 2));
126 std::list<int> lst = {1, 2, 3, 4, 5};
127 auto result =
pmaps(pool, lst, [](
int x) {
return x * x; });
128 EXPECT_EQ(result, (std::vector<int>{1, 4, 9, 16, 25}));
133 auto result =
pmaps(pool, large_vec, [](
int x) {
return x; });
134 for (
size_t i = 0; i < result.size(); ++i)
135 EXPECT_EQ(result[i],
static_cast<int>(i + 1));
142 options.min_size = large_vec.size() + 1;
146 auto result =
pmaps(large_vec, [](
int x) {
return x + 1; },
options);
147 ASSERT_EQ(result.size(), large_vec.size());
148 EXPECT_EQ(result.front(), 2);
149 EXPECT_EQ(result.back(), 10001);
158 auto result =
pfilter(pool, small_vec, [](
int x) {
return x % 2 == 0; });
159 EXPECT_EQ(result, (std::vector<int>{2, 4}));
164 auto result =
pfilter(pool, small_vec, [](
int x) {
return x % 2 != 0; });
165 EXPECT_EQ(result, (std::vector<int>{1, 3, 5}));
170 auto result =
pfilter(pool, small_vec, [](
int x) {
return x > 100; });
171 EXPECT_TRUE(result.empty());
176 auto result =
pfilter(pool, small_vec, [](
int x) {
return x > 0; });
177 EXPECT_EQ(result.size(), 5u);
182 auto result =
pfilter(pool, empty_vec, [](
int) {
return true; });
183 EXPECT_TRUE(result.empty());
188 std::vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
189 auto result =
pfilter(pool, nums, [](
int x) {
return x % 3 == 0; });
190 EXPECT_EQ(result, (std::vector<int>{3, 6, 9}));
195 auto result =
pfilter(pool, large_vec, [](
int x) {
return x % 100 == 0; });
196 EXPECT_EQ(result.size(), 100u);
197 EXPECT_EQ(result[0], 100);
198 EXPECT_EQ(result[99], 10000);
207 options.cancel_token = source.token();
209 EXPECT_THROW(
pmaps(large_vec,
212 source.request_cancel();
225 int sum =
pfoldl(pool, small_vec, 0, std::plus<int>());
231 int product =
pfoldl(pool, small_vec, 1, std::multiplies<int>());
237 int max_val =
pfoldl(pool, small_vec, INT_MIN,
238 [](
int a,
int b) {
return std::max(a, b); });
239 EXPECT_EQ(max_val, 5);
244 int result =
pfoldl(pool, empty_vec, 42, std::plus<int>());
245 EXPECT_EQ(result, 42);
251 [](
long long a,
int b) {
return a + b; });
253 EXPECT_EQ(
sum, 50005000LL);
262 std::vector<int> data = {1, 2, 3, 4, 5};
263 pfor_each(pool, data, [](
int& x) { x *= 2; });
264 EXPECT_EQ(data, (std::vector<int>{2, 4, 6, 8, 10}));
269 std::atomic<int>
count{0};
271 if (x % 2 == 0) ++
count;
273 EXPECT_EQ(
count.load(), 5000);
278 std::atomic<int>
count{0};
280 EXPECT_EQ(
count.load(), 0);
285 std::vector<int> data = {1, 2, 3, 4};
291 EXPECT_EQ(data, (std::vector<int>{6, 7, 8, 9}));
300 bool result =
pall(pool, small_vec, [](
int x) {
return x > 0; });
306 bool result =
pall(pool, small_vec, [](
int x) {
return x > 3; });
307 EXPECT_FALSE(result);
312 bool result =
pall(pool, empty_vec, [](
int) {
return false; });
318 bool result =
pexists(pool, small_vec, [](
int x) {
return x == 3; });
324 bool result =
pexists(pool, small_vec, [](
int x) {
return x > 100; });
325 EXPECT_FALSE(result);
330 bool result =
pexists(pool, empty_vec, [](
int) {
return true; });
331 EXPECT_FALSE(result);
336 bool result =
pnone(pool, small_vec, [](
int x) {
return x < 0; });
342 bool result =
pnone(pool, small_vec, [](
int x) {
return x == 3; });
343 EXPECT_FALSE(result);
352 size_t count =
pcount_if(pool, small_vec, [](
int x) {
return x % 2 == 0; });
353 EXPECT_EQ(
count, 2u);
358 size_t count =
pcount_if(pool, large_vec, [](
int x) {
return x % 100 == 0; });
359 EXPECT_EQ(
count, 100u);
364 size_t count =
pcount_if(pool, small_vec, [](
int x) {
return x > 0; });
365 EXPECT_EQ(
count, 5u);
370 size_t count =
pcount_if(pool, small_vec, [](
int x) {
return x > 100; });
371 EXPECT_EQ(
count, 0u);
376 size_t count =
pcount_if(pool, large_vec, [](
int x) {
return x % 7 == 0; });
377 EXPECT_EQ(
count, 1428u);
386 auto idx =
pfind(pool, small_vec, [](
int x) {
return x == 3; });
387 ASSERT_TRUE(idx.has_value());
393 auto idx =
pfind(pool, small_vec, [](
int x) {
return x == 100; });
394 EXPECT_FALSE(idx.has_value());
399 std::vector<int> data = {1, 2, 3, 3, 3, 4, 5};
400 auto idx =
pfind(pool, data, [](
int x) {
return x == 3; });
401 ASSERT_TRUE(idx.has_value());
407 auto idx =
pfind(pool, empty_vec, [](
int) {
return true; });
408 EXPECT_FALSE(idx.has_value());
418 auto idx =
pfind(large_vec, [](
int x) {
return x == 7777; },
options);
419 ASSERT_TRUE(idx.has_value());
420 EXPECT_EQ(*idx, 7776u);
425 std::vector<std::string> words = {
"apple",
"banana",
"cherry"};
427 [](
const std::string& s) {
return s.length() > 5; });
428 ASSERT_TRUE(found.has_value());
429 EXPECT_EQ(*found,
"banana");
438 auto sum =
psum(pool, small_vec);
444 auto sum =
psum(pool, small_vec, 100);
450 auto sum =
psum(pool, empty_vec);
456 auto sum = psum<std::vector<int>,
long long>(pool, large_vec, 0
LL);
457 EXPECT_EQ(
sum, 50005000LL);
478 auto min_val =
pmin(pool, small_vec);
479 ASSERT_TRUE(min_val.has_value());
480 EXPECT_EQ(*min_val, 1);
485 auto max_val =
pmax(pool, small_vec);
486 ASSERT_TRUE(max_val.has_value());
487 EXPECT_EQ(*max_val, 5);
492 auto min_val =
pmin(pool, empty_vec);
493 EXPECT_FALSE(min_val.has_value());
498 auto max_val =
pmax(pool, empty_vec);
499 EXPECT_FALSE(max_val.has_value());
504 auto result =
pminmax(pool, small_vec);
505 ASSERT_TRUE(result.has_value());
506 EXPECT_EQ(result->first, 1);
507 EXPECT_EQ(result->second, 5);
512 std::vector<int> data = {5, 1, 3, 2, 4};
513 auto result =
pminmax(pool, data);
514 ASSERT_TRUE(result.has_value());
515 EXPECT_EQ(result->first, 1);
516 EXPECT_EQ(result->second, 5);
521 auto result =
pminmax(pool, large_vec);
522 ASSERT_TRUE(result.has_value());
523 EXPECT_EQ(result->first, 1);
524 EXPECT_EQ(result->second, 10000);
533 std::vector<int> data = {5, 2, 8, 1, 9, 3, 7, 4, 6};
535 EXPECT_EQ(data, (std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9}));
540 std::vector<int> data = {5, 2, 8, 1, 9, 3, 7, 4, 6};
541 psort(pool, data, std::greater<int>());
542 EXPECT_EQ(data, (std::vector<int>{9, 8, 7, 6, 5, 4, 3, 2, 1}));
547 std::vector<int> data;
549 EXPECT_TRUE(data.empty());
554 std::vector<int> data = {42};
556 EXPECT_EQ(data[0], 42);
561 std::vector<int> data = {1, 2, 3, 4, 5};
563 EXPECT_EQ(data, (std::vector<int>{1, 2, 3, 4, 5}));
568 std::vector<int> data = large_vec;
569 std::mt19937
rng(42);
570 std::shuffle(data.begin(), data.end(),
rng);
572 for (
size_t i = 0; i < data.size(); ++i)
573 EXPECT_EQ(data[i],
static_cast<int>(i + 1));
578 std::vector<int> data = {7, 1, 5, 9, 3, 2};
584 EXPECT_EQ(data, (std::vector<int>{1, 2, 3, 5, 7, 9}));
589 std::vector<int> data(4096);
590 std::iota(data.begin(), data.end(), 0);
591 std::mt19937
rng(77);
592 std::shuffle(data.begin(), data.end(),
rng);
599 options.cancel_token = source.token();
600 source.request_cancel();
611 std::vector<int> a = {1, 2, 3};
612 std::vector<int> b = {4, 5, 6};
613 std::atomic<int>
sum{0};
616 EXPECT_EQ(
sum.load(), 32);
621 std::vector<int> a = {1, 2, 3, 4, 5};
622 std::vector<int> b = {10, 20, 30};
623 std::atomic<int>
sum{0};
626 EXPECT_EQ(
sum.load(), 66);
632 std::vector<int> b = {1, 2, 3};
633 std::atomic<int>
count{0};
636 EXPECT_EQ(
count.load(), 0);
645 std::vector<int> a = {1, 2, 3};
646 std::vector<int> b = {4, 5, 6};
648 auto result =
pzip_maps(pool, a, b, [](
int x,
int y) {
return x *
y; });
649 EXPECT_EQ(result, (std::vector<int>{4, 10, 18}));
654 std::vector<int> a = {1, 2, 3};
655 std::vector<double> b = {1.5, 2.5, 3.5};
657 auto result =
pzip_maps(pool, a, b, [](
int x,
double y) {
return x +
y; });
658 EXPECT_NEAR(result[0], 2.5, 0.001);
659 EXPECT_NEAR(result[1], 4.5, 0.001);
660 EXPECT_NEAR(result[2], 6.5, 0.001);
665 std::vector<int> a = {1, 2, 3, 4};
666 std::vector<int> b = {10, 20, 30, 40};
672 EXPECT_EQ(mapped, (std::vector<int>{11, 22, 33, 44}));
674 auto folded =
pzip_foldl(a, b, 0, [](
int acc,
int x,
int y) {
677 EXPECT_EQ(folded, 300);
679 std::atomic<int> visits{0};
681 EXPECT_EQ(visits.load(), 4);
690 auto [evens, odds] =
ppartition(pool, small_vec, [](
int x) {
return x % 2 == 0; });
691 EXPECT_EQ(evens, (std::vector<int>{2, 4}));
692 EXPECT_EQ(odds, (std::vector<int>{1, 3, 5}));
697 auto [yes, no] =
ppartition(pool, small_vec, [](
int x) {
return x > 0; });
698 EXPECT_EQ(yes.size(), 5u);
699 EXPECT_TRUE(no.empty());
704 auto [yes, no] =
ppartition(pool, small_vec, [](
int x) {
return x < 0; });
705 EXPECT_TRUE(yes.empty());
706 EXPECT_EQ(no.size(), 5u);
711 auto [yes, no] =
ppartition(pool, empty_vec, [](
int) {
return true; });
712 EXPECT_TRUE(yes.empty());
713 EXPECT_TRUE(no.empty());
718 std::vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
719 auto [evens, odds] =
ppartition(pool, data, [](
int x) {
return x % 2 == 0; });
720 EXPECT_EQ(evens, (std::vector<int>{2, 4, 6, 8, 10}));
721 EXPECT_EQ(odds, (std::vector<int>{1, 3, 5, 7, 9}));
730 std::vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8};
731 auto [evens, odds] =
ppartition(data, [](
int x) {
return x % 2 == 0; },
options);
732 EXPECT_EQ(evens, (std::vector<int>{2, 4, 6, 8}));
733 EXPECT_EQ(odds, (std::vector<int>{1, 3, 5, 7}));
742 NoDefault() =
delete;
743 explicit NoDefault(
int v) :
value(v) {}
744 NoDefault(
const NoDefault &) =
default;
745 NoDefault(NoDefault &&)
noexcept =
default;
746 NoDefault & operator=(
const NoDefault &) =
default;
747 NoDefault & operator=(NoDefault &&)
noexcept =
default;
750 std::vector<NoDefault> data;
751 for (
int i = 1; i <= 6; ++i)
752 data.emplace_back(i);
754 auto [small, large] =
ppartition(pool, data, [](
const NoDefault & x) {
758 ASSERT_EQ(small.size(), 3u);
759 ASSERT_EQ(large.size(), 3u);
760 EXPECT_EQ(small[0].
value, 1);
761 EXPECT_EQ(small[1].
value, 2);
762 EXPECT_EQ(small[2].
value, 3);
763 EXPECT_EQ(large[0].
value, 4);
764 EXPECT_EQ(large[1].
value, 5);
765 EXPECT_EQ(large[2].
value, 6);
774 options.cancel_token = source.token();
775 source.request_cancel();
777 std::vector<int> data(512, 1);
788 NoDefault() =
delete;
789 explicit NoDefault(
int v) :
value(v) {}
790 NoDefault(
const NoDefault &) =
default;
791 NoDefault(NoDefault &&)
noexcept =
default;
792 NoDefault & operator=(
const NoDefault &) =
default;
793 NoDefault & operator=(NoDefault &&)
noexcept =
default;
796 std::vector<NoDefault> data;
797 for (
int i = 1; i <= 8; ++i)
798 data.emplace_back(i);
800 auto result =
pfilter(pool, data, [](
const NoDefault & x) {
801 return x.value % 2 == 0;
804 ASSERT_EQ(result.size(), 4u);
805 EXPECT_EQ(result[0].
value, 2);
806 EXPECT_EQ(result[1].
value, 4);
807 EXPECT_EQ(result[2].
value, 6);
808 EXPECT_EQ(result[3].
value, 8);
817 std::vector<int> data(256);
818 std::iota(data.begin(), data.end(), 1);
819 std::vector<int>
expected(data.size());
820 std::partial_sum(data.begin(), data.end(),
expected.begin());
822 auto result =
pscan(pool, data, std::plus<int>{});
828 std::vector<int> data = {1, 2, 3, 4, 5};
830 EXPECT_EQ(result, (std::vector<int>{0, 1, 3, 6, 10}));
839 std::vector<int> data = {3, 1, 4, 1, 5, 9};
840 auto inclusive =
pscan(data, std::plus<int>{},
options);
841 EXPECT_EQ(inclusive, (std::vector<int>{3, 4, 8, 9, 14, 23}));
844 EXPECT_EQ(exclusive, (std::vector<int>{10, 13, 14, 18, 19, 24}));
846 std::vector<int> left = {1, 3, 5, 7};
847 std::vector<int> right = {2, 4, 6, 8};
848 auto merged =
pmerge(left, right, std::less<int>{},
options);
849 EXPECT_EQ(merged, (std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8}));
858 options.cancel_token = source.token();
859 source.request_cancel();
861 std::vector<int> data(512, 1);
866 std::vector<int> left(256), right(256);
867 std::iota(left.begin(), left.end(), 0);
868 std::iota(right.begin(), right.end(), 0);
869 EXPECT_THROW((
void)
pmerge(left, right, std::less<int>{},
options),
879 NoDefault() =
delete;
880 explicit NoDefault(
int v) :
value(v) {}
881 NoDefault(
const NoDefault &) =
default;
882 NoDefault(NoDefault &&)
noexcept =
default;
883 NoDefault & operator=(
const NoDefault &) =
default;
884 NoDefault & operator=(NoDefault &&)
noexcept =
default;
887 std::vector<NoDefault> data;
888 for (
int i = 1; i <= 4; ++i)
889 data.emplace_back(i);
891 auto inclusive =
pscan(data,
892 [](
const NoDefault & a,
const NoDefault & b) {
893 return NoDefault(a.value + b.value);
895 ASSERT_EQ(inclusive.size(), 4u);
896 EXPECT_EQ(inclusive[0].
value, 1);
897 EXPECT_EQ(inclusive[1].
value, 3);
898 EXPECT_EQ(inclusive[2].
value, 6);
899 EXPECT_EQ(inclusive[3].
value, 10);
902 [](
const NoDefault & a,
const NoDefault & b) {
903 return NoDefault(a.value + b.value);
905 ASSERT_EQ(exclusive.size(), 4u);
906 EXPECT_EQ(exclusive[0].
value, 0);
907 EXPECT_EQ(exclusive[1].
value, 1);
908 EXPECT_EQ(exclusive[2].
value, 3);
909 EXPECT_EQ(exclusive[3].
value, 6);
911 std::vector<NoDefault> left, right;
912 left.emplace_back(1);
913 left.emplace_back(3);
914 left.emplace_back(5);
915 right.emplace_back(2);
916 right.emplace_back(4);
917 right.emplace_back(6);
919 auto merged =
pmerge(left, right,
920 [](
const NoDefault & a,
const NoDefault & b) {
921 return a.value < b.value;
923 ASSERT_EQ(merged.size(), 6u);
924 for (
size_t i = 0; i < merged.size(); ++i)
925 EXPECT_EQ(merged[i].
value,
static_cast<int>(i + 1));
935 std::vector<int> seq_result;
936 for (
int x : large_vec)
937 seq_result.push_back(x * 2 + 1);
940 auto par_result =
pmaps(pool, large_vec, [](
int x) {
return x * 2 + 1; });
942 EXPECT_EQ(seq_result, par_result);
948 std::vector<int> seq_result;
949 for (
int x : large_vec)
951 seq_result.push_back(x);
954 auto par_result =
pfilter(pool, large_vec, [](
int x) {
return x % 17 == 0; });
956 EXPECT_EQ(seq_result, par_result);
962 long long seq_sum = 0;
963 for (
int x : large_vec)
967 long long par_sum =
pfoldl(pool, large_vec, 0
LL,
968 [](
long long a,
int b) {
return a + b; });
970 EXPECT_EQ(seq_sum, par_sum);
979 [[nodiscard]]
bool parallel_benchmarks_enabled()
981 return std::getenv(
"ALEPH_RUN_PARALLEL_BENCHMARKS") !=
nullptr;
987 if (not parallel_benchmarks_enabled())
988 GTEST_SKIP() <<
"Skipping timing-sensitive parallel benchmarks; "
989 <<
"set ALEPH_RUN_PARALLEL_BENCHMARKS=1 to enable";
991 std::vector<int> data(1000000);
992 std::iota(data.begin(), data.end(), 0);
995 auto seq_start = std::chrono::high_resolution_clock::now();
996 std::vector<int> seq_result;
997 seq_result.reserve(data.size());
999 seq_result.push_back(x * x + 2 * x + 1);
1000 auto seq_end = std::chrono::high_resolution_clock::now();
1001 auto seq_duration = std::chrono::duration_cast<std::chrono::microseconds>(
1002 seq_end - seq_start).count();
1005 auto par_start = std::chrono::high_resolution_clock::now();
1006 auto par_result =
pmaps(pool, data, [](
int x) {
return x * x + 2 * x + 1; });
1007 auto par_end = std::chrono::high_resolution_clock::now();
1008 auto par_duration = std::chrono::duration_cast<std::chrono::microseconds>(
1009 par_end - par_start).count();
1011 EXPECT_EQ(seq_result, par_result);
1013 double speedup =
static_cast<double>(seq_duration) / par_duration;
1014 std::cout <<
"\n=== Benchmark: pmaps ===\n";
1015 std::cout <<
"Data size: 1M elements\n";
1016 std::cout <<
"Sequential: " << seq_duration <<
" μs\n";
1017 std::cout <<
"Parallel: " << par_duration <<
" μs\n";
1018 std::cout <<
"Speedup: " << speedup <<
"x\n";
1019 std::cout <<
"========================\n\n";
1025 if (pool.num_threads() > 1)
1026 EXPECT_GT(speedup, 0.3);
1031 if (not parallel_benchmarks_enabled())
1032 GTEST_SKIP() <<
"Skipping timing-sensitive parallel benchmarks; "
1033 <<
"set ALEPH_RUN_PARALLEL_BENCHMARKS=1 to enable";
1035 std::vector<int> data(1000000);
1036 std::iota(data.begin(), data.end(), 0);
1038 auto pred = [](
int x) {
return x % 7 == 0 || x % 11 == 0; };
1041 auto seq_start = std::chrono::high_resolution_clock::now();
1042 std::vector<int> seq_result;
1045 seq_result.push_back(x);
1046 auto seq_end = std::chrono::high_resolution_clock::now();
1047 auto seq_duration = std::chrono::duration_cast<std::chrono::microseconds>(
1048 seq_end - seq_start).count();
1051 auto par_start = std::chrono::high_resolution_clock::now();
1053 auto par_end = std::chrono::high_resolution_clock::now();
1054 auto par_duration = std::chrono::duration_cast<std::chrono::microseconds>(
1055 par_end - par_start).count();
1057 EXPECT_EQ(seq_result, par_result);
1059 double speedup =
static_cast<double>(seq_duration) / par_duration;
1060 std::cout <<
"\n=== Benchmark: pfilter ===\n";
1061 std::cout <<
"Data size: 1M elements\n";
1062 std::cout <<
"Sequential: " << seq_duration <<
" μs\n";
1063 std::cout <<
"Parallel: " << par_duration <<
" μs\n";
1064 std::cout <<
"Speedup: " << speedup <<
"x\n";
1065 std::cout <<
"==========================\n\n";
1070 if (not parallel_benchmarks_enabled())
1071 GTEST_SKIP() <<
"Skipping timing-sensitive parallel benchmarks; "
1072 <<
"set ALEPH_RUN_PARALLEL_BENCHMARKS=1 to enable";
1074 std::vector<int> data(100000);
1075 std::iota(data.begin(), data.end(), 0);
1076 std::mt19937
rng(123);
1077 std::shuffle(data.begin(), data.end(),
rng);
1079 std::vector<int> data_copy = data;
1082 auto seq_start = std::chrono::high_resolution_clock::now();
1083 std::sort(data.begin(), data.end());
1084 auto seq_end = std::chrono::high_resolution_clock::now();
1085 auto seq_duration = std::chrono::duration_cast<std::chrono::microseconds>(
1086 seq_end - seq_start).count();
1089 auto par_start = std::chrono::high_resolution_clock::now();
1090 psort(pool, data_copy);
1091 auto par_end = std::chrono::high_resolution_clock::now();
1092 auto par_duration = std::chrono::duration_cast<std::chrono::microseconds>(
1093 par_end - par_start).count();
1095 EXPECT_EQ(data, data_copy);
1097 double speedup =
static_cast<double>(seq_duration) / par_duration;
1098 std::cout <<
"\n=== Benchmark: psort ===\n";
1099 std::cout <<
"Data size: 100K elements\n";
1100 std::cout <<
"std::sort: " << seq_duration <<
" μs\n";
1101 std::cout <<
"psort: " << par_duration <<
" μs\n";
1102 std::cout <<
"Speedup: " << speedup <<
"x\n";
1103 std::cout <<
"========================\n\n";
1113 auto future1 = std::async(std::launch::async, [
this]() {
1114 return pmaps(pool, large_vec, [](
int x) {
return x * 2; });
1117 auto future2 = std::async(std::launch::async, [
this]() {
1118 return pfilter(pool, large_vec, [](
int x) {
return x % 2 == 0; });
1121 auto future3 = std::async(std::launch::async, [
this]() {
1122 return psum<std::vector<int>,
long long>(pool, large_vec, 0
LL);
1125 auto result1 = future1.get();
1126 auto result2 = future2.get();
1127 auto result3 = future3.get();
1129 EXPECT_EQ(result1.size(), 10000u);
1130 EXPECT_EQ(result2.size(), 5000u);
1131 EXPECT_EQ(result3, 50005000LL);
1142 auto result =
pmaps(single_pool, small_vec, [](
int x) {
return x * x; });
1143 EXPECT_EQ(result, (std::vector<int>{1, 4, 9, 16, 25}));
1150 auto result =
pmaps(many_pool, small_vec, [](
int x) {
return x * x; });
1151 EXPECT_EQ(result, (std::vector<int>{1, 4, 9, 16, 25}));
1156 std::vector<int> huge(100000);
1157 std::iota(huge.begin(), huge.end(), 0);
1159 auto sum = psum<std::vector<int>,
long long>(pool, huge, 0
LL);
1161 EXPECT_EQ(
sum, 4999950000LL);
1170 std::vector<int> a = {1, 2, 3};
1171 std::vector<int> b = {4, 5, 6};
1172 std::vector<int> c = {7, 8, 9};
1173 std::atomic<int>
sum{0};
1179 EXPECT_EQ(
sum.load(), 45);
1184 std::vector<int> a = {1, 2};
1185 std::vector<int> b = {3, 4};
1186 std::vector<int> c = {5, 6};
1187 std::vector<int> d = {7, 8};
1188 std::atomic<int>
sum{0};
1191 sum +=
w * x *
y * z;
1194 EXPECT_EQ(
sum.load(), 1*3*5*7 + 2*4*6*8);
1199 std::vector<int> a = {1, 2, 3, 4, 5};
1200 std::vector<int> b = {10, 20, 30};
1201 std::vector<int> c = {100, 200, 300, 400};
1202 std::atomic<int>
count{0};
1208 EXPECT_EQ(
count.load(), 3);
1213 std::vector<int> a = {1, 2, 3};
1214 std::vector<int> b = {4, 5, 6};
1215 std::vector<int> c = {7, 8, 9};
1217 auto result =
pzip_maps_n(pool, [](
int x,
int y,
int z) {
1221 EXPECT_EQ(result, (std::vector<int>{12, 15, 18}));
1227 std::vector<int> b = {1, 2, 3};
1233 EXPECT_TRUE(result.empty());
1238 std::vector<int> a = {1, 2, 3};
1239 std::vector<int> b = {4, 5, 6};
1243 [](
int acc,
int x,
int y) {
return acc + x *
y; },
1252 std::vector<int> a = {1, 2, 3};
1253 std::vector<int> b = {2, 3, 4};
1254 std::vector<int> c = {3, 4, 5};
1256 bool result =
pzip_all_n(pool, [](
int x,
int y,
int z) {
1257 return x <
y &&
y < z;
1260 EXPECT_TRUE(result);
1265 std::vector<int> a = {1, 2, 3};
1266 std::vector<int> b = {2, 3, 4};
1267 std::vector<int> c = {3, 2, 5};
1269 bool result =
pzip_all_n(pool, [](
int x,
int y,
int z) {
1270 return x <
y &&
y < z;
1273 EXPECT_FALSE(result);
1278 std::vector<int> a = {1, 2, 3};
1279 std::vector<int> b = {4, 2, 6};
1285 EXPECT_TRUE(result);
1290 std::vector<int> a = {1, 2, 3};
1291 std::vector<int> b = {4, 5, 6};
1297 EXPECT_FALSE(result);
1302 std::vector<int> a = {1, 2, 3, 4, 5};
1303 std::vector<int> b = {5, 4, 3, 2, 1};
1309 EXPECT_EQ(
count, 5u);
1314 std::vector<int> a = {1, 2, 3, 4, 5};
1315 std::vector<int> b = {1, 2, 3, 4, 5};
1318 return x ==
y && x % 2 == 0;
1321 EXPECT_EQ(
count, 2u);
1326 std::vector<int> a = {1, 2, 3};
1327 std::vector<int> b = {4, 5, 6};
1328 std::vector<int> c = {7, 8, 9};
1333 auto mapped =
pzip_maps_n([](
int x,
int y,
int z) {
return x +
y + z; },
1335 EXPECT_EQ(mapped, (std::vector<int>{12, 15, 18}));
1338 [](
int acc,
int x,
int y,
int z) {
1339 return acc + x +
y + z;
1343 EXPECT_EQ(folded, 45);
1345 EXPECT_TRUE(
pzip_all_n([](
int x,
int y,
int z) {
return x <
y &&
y < z; },
1347 EXPECT_TRUE(
pzip_exists_n([](
int x,
int y,
int z) {
return x +
y + z == 15; },
1360 std::vector<int> data(100, 0);
1363 x =
static_cast<int>(i * 2);
1366 for (
size_t i = 0; i < data.size(); ++i)
1367 EXPECT_EQ(data[i],
static_cast<int>(i * 2));
1372 const std::vector<int> data = {10, 20, 30, 40, 50};
1373 std::atomic<int> weighted_sum{0};
1376 weighted_sum +=
static_cast<int>(i) * x;
1380 EXPECT_EQ(weighted_sum.load(), 400);
1385 std::vector<std::string> words = {
"a",
"bb",
"ccc"};
1388 [](
size_t i,
const std::string& s) {
1389 return std::to_string(i) +
":" + s;
1392 EXPECT_EQ(result[0],
"0:a");
1393 EXPECT_EQ(result[1],
"1:bb");
1394 EXPECT_EQ(result[2],
"2:ccc");
1399 std::vector<int> empty;
1402 [](
size_t i,
int x) {
return static_cast<int>(i + x); });
1404 EXPECT_TRUE(result.empty());
1409 std::vector<int> data(6, 0);
1415 x =
static_cast<int>(i + 10);
1417 EXPECT_EQ(data, (std::vector<int>{10, 11, 12, 13, 14, 15}));
1420 return std::to_string(i) +
"=" + std::to_string(x);
1422 EXPECT_EQ(labels[0],
"0=10");
1423 EXPECT_EQ(labels[5],
"5=15");
1428 std::vector<int> a(256, 1), b(256, 2), c(256, 3);
1433 options.cancel_token = source.token();
1434 source.request_cancel();
1438 EXPECT_THROW((
void)
pzip_maps_n([](
int x,
int y,
int z) {
return x +
y + z; },
1442 return static_cast<int>(i) + x;
1453 std::vector<int> a(10000), b(10000), c(10000);
1454 std::iota(a.begin(), a.end(), 0);
1455 std::iota(b.begin(), b.end(), 10000);
1456 std::iota(c.begin(), c.end(), 20000);
1458 std::atomic<long long>
sum{0};
1468 EXPECT_EQ(
sum.load(), 449985000LL);
1473 std::vector<int> a(10000), b(10000);
1474 std::iota(a.begin(), a.end(), 0);
1475 std::iota(b.begin(), b.end(), 0);
1481 for (
size_t i = 0; i < result.size(); ++i)
1482 EXPECT_EQ(result[i],
static_cast<int>(i * i));
1487 std::vector<int> a(1000), b(1000), c(1000);
1488 std::iota(a.begin(), a.end(), 0);
1489 std::iota(b.begin(), b.end(), 1000);
1490 std::iota(c.begin(), c.end(), 2000);
1493 std::vector<int> seq_result;
1494 for (
size_t i = 0; i < a.size(); ++i)
1495 seq_result.push_back(a[i] + b[i] + c[i]);
1498 auto par_result =
pzip_maps_n(pool, [](
int x,
int y,
int z) {
1502 EXPECT_EQ(seq_result, par_result);
1507 ::testing::InitGoogleTest(&argc, argv);
1508 return RUN_ALL_TESTS();
Parallel functional programming operations using ThreadPool.
TEST_F(ParallelTest, PmapsSquare)
size_t size_t int32_t value
Cooperative cancellation source paired with CancellationToken.
A reusable thread pool for efficient parallel task execution.
Minimal std::expected-style result type for C++20.
Exception thrown when cooperative cancellation is observed.
std::vector< int > single_vec
std::vector< int > empty_vec
std::vector< int > small_vec
std::vector< int > large_vec
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
bool pall(ThreadPool &pool, const Container &c, Pred pred, size_t chunk_size=0)
Parallel all predicate (short-circuit).
bool pnone(ThreadPool &pool, const Container &c, Pred pred, size_t chunk_size=0)
Parallel none predicate.
auto pmaps(ThreadPool &pool, const Container &c, Op op, size_t chunk_size=0)
Parallel map operation.
auto pmin(ThreadPool &pool, const Container &c, size_t chunk_size=0)
Parallel minimum element.
T pzip_foldl_n(ThreadPool &pool, T init, Op op, Combiner combiner, const Containers &... cs)
Parallel fold/reduce over N zipped containers (variadic).
T pzip_foldl(ThreadPool &pool, const Container1 &c1, const Container2 &c2, T init, Op op, size_t chunk_size=0)
Parallel zip + fold.
void pzip_for_each(ThreadPool &pool, const Container1 &c1, const Container2 &c2, Op op, size_t chunk_size=0)
Parallel zip + for_each.
auto penumerate_maps(ThreadPool &pool, const Container &c, Op op, size_t chunk_size=0)
Parallel enumerate with map.
void penumerate_for_each(ThreadPool &pool, Container &c, Op op, size_t chunk_size=0)
Parallel for_each with index (enumerate).
bool pzip_all_n(ThreadPool &pool, Pred pred, const Containers &... cs)
Parallel all predicate over N zipped containers (variadic).
size_t pcount_if(ThreadPool &pool, const Container &c, Pred pred, size_t chunk_size=0)
Parallel count_if operation.
auto pexclusive_scan(ThreadPool &pool, const Container &c, T init, BinaryOp op, size_t chunk_size=0)
Parallel exclusive scan over a container.
std::optional< size_t > pfind(ThreadPool &pool, const Container &c, Pred pred, size_t chunk_size=0)
Parallel find operation (returns index).
bool pzip_exists_n(ThreadPool &pool, Pred pred, const Containers &... cs)
Parallel exists predicate over N zipped containers (variadic).
void psort(ThreadPool &pool, Container &c, Compare cmp=Compare{}, const size_t min_parallel_size=1024)
Parallel sort (in-place).
void pfor_each(ThreadPool &pool, Container &c, Op op, size_t chunk_size=0)
Parallel for_each operation.
auto pfilter(ThreadPool &pool, const Container &c, Pred pred, size_t chunk_size=0)
Parallel filter operation.
auto ppartition(ThreadPool &pool, const Container &c, Pred pred, size_t chunk_size=0)
Parallel partition (stable).
auto pmerge(ThreadPool &pool, const Container1 &c1, const Container2 &c2, Compare comp=Compare{}, size_t chunk_size=0)
Parallel merge of two sorted containers.
T pproduct(ThreadPool &pool, const Container &c, T init=T{1}, size_t chunk_size=0)
Parallel product of elements.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
auto pmax(ThreadPool &pool, const Container &c, size_t chunk_size=0)
Parallel maximum element.
void pzip_for_each_n(ThreadPool &pool, Op op, const Containers &... cs)
Parallel for_each over N zipped containers (variadic).
auto pzip_maps_n(ThreadPool &pool, Op op, const Containers &... cs)
Parallel map over N zipped containers (variadic).
auto pscan(ThreadPool &pool, const Container &c, BinaryOp op, size_t chunk_size=0)
Parallel inclusive scan over a container.
T pfoldl(ThreadPool &pool, const Container &c, T init, BinaryOp op, size_t chunk_size=0)
Parallel left fold (reduce).
auto pfind_value(ThreadPool &pool, const Container &c, Pred pred, size_t chunk_size=0)
Parallel find with value return.
auto pzip_maps(ThreadPool &pool, const Container1 &c1, const Container2 &c2, Op op, size_t chunk_size=0)
Parallel zip + map.
T psum(ThreadPool &pool, const Container &c, T init=T{}, size_t chunk_size=0)
Parallel sum of elements.
auto pminmax(ThreadPool &pool, const Container &c, size_t chunk_size=0)
Parallel min and max elements.
size_t pzip_count_if_n(ThreadPool &pool, Pred pred, const Containers &... cs)
Parallel count over N zipped containers (variadic).
bool pexists(ThreadPool &pool, const Container &c, Pred pred, size_t chunk_size=0)
Parallel exists predicate (short-circuit).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
static struct argp_option options[]
Common configuration object for parallel algorithms.
size_t chunk_size
Elements per chunk (0 = auto).
ThreadPool * pool
Executor to use (nullptr = default_pool()).