diff --git a/VectorTest/DR3_tests.cpp b/VectorTest/DR3_tests.cpp index 0a8f294..b1833bf 100644 --- a/VectorTest/DR3_tests.cpp +++ b/VectorTest/DR3_tests.cpp @@ -2241,7 +2241,6 @@ void testReduce_VecM_22(int SZ) EXPECT_NUMERIC_EQ(MAX_M, asNumber(asNumber(SZ - 1))); - //auto resMulti_3 = reduceM(testVec, SUM, MAX, MIN); auto resMulti_3 = reduceM2(testVec, SUM, MAX, MIN); auto res_lambda_0 = std::get<0>(resMulti_3); auto res_lambda_1 = std::get<1>(resMulti_3); @@ -2269,6 +2268,80 @@ void testReduce_VecM_22(int SZ) +void testReduce_GeneralVariadic(int SZ) +{ + + + std::vector input(SZ, asNumber(0.0)); + std::iota(begin(input), end(input), asNumber(0.0)); + + std::random_device rd; + std::mt19937 g(rd()); + std::shuffle(begin(input), end(input), g); + + + auto SUM = [](auto x, auto y) { return x + y; }; + auto SUM2 = [](auto x, auto y) { return x*x + y; }; + auto SUM_SQR = [](auto x, auto y) { return x + y * y; }; + + double smSqr = 0.0; + double sm = 0.0; + double sm2 = 0.0; + + smSqr = std::reduce(input.begin(), input.end(), 0.0, SUM_SQR); + sm = std::reduce(input.begin(), input.end(), 0.0, SUM); + sm2 = std::reduce(input.begin(), input.end(), 0.0, SUM2); + + + auto lambdas_3 = std::make_tuple(SUM, SUM2, SUM_SQR); //tupple of lambdas + + //create initial value tuples + double nill = 0.0 ; + auto init_values_3 = std::make_tuple(nill, nill, nill); + + auto resMulti_3 = reduceM(input.begin(), input.end(), lambdas_3, init_values_3); + + + double SUM_RES = std::get<0>(resMulti_3); + auto SUM2_RES = std::get<1>(resMulti_3); + auto SUM_SQR_RES = std::get<2>(resMulti_3); + auto expectedSum = asNumber((SZ - (long)(1)) * (SZ / 2.0)); + + EXPECT_NUMERIC_EQ(SUM_RES, sm); + EXPECT_NUMERIC_EQ(SUM2_RES, sm2); + EXPECT_NUMERIC_EQ(SUM_SQR_RES, smSqr); + +} + + + + +/* +test gteneral multi reduce +*/ +TEST(TestDR3, testReduce_GeneralVariadic) +{ + + testReduce_GeneralVariadic(2); + testReduce_GeneralVariadic(3); + testReduce_GeneralVariadic(4); + + + for (int SZ = 3; SZ < 33; SZ++) + { + testReduce_GeneralVariadic(SZ); + } + + + testReduce_GeneralVariadic(34); + testReduce_GeneralVariadic(63); + testReduce_GeneralVariadic(64); + testReduce_GeneralVariadic(65); + + +} + + /* tests mult with x4 unroll diff --git a/Vectorisation/VecX/dr3.h b/Vectorisation/VecX/dr3.h index 80becc2..3f47143 100644 --- a/Vectorisation/VecX/dr3.h +++ b/Vectorisation/VecX/dr3.h @@ -959,3 +959,35 @@ Vec scan(const Vec& rhs1, OP& oper) { return ApplyScan(rhs1, oper); } + +///* +//////////////// variadic reduction +// Helper function to call a single lambda with the current state and value +template +auto applyLambda(const Tuple& lambdas, State state, Value value) { + return std::get(lambdas)(state, value); +} + +// Recursive case: apply the lambdas to each value in the sequence +template +auto applyLambdasImpl(const Tuple& lambdas, InIt first, InIt last, std::tuple states, std::index_sequence) +{ + for (; first != last; ++first) + { + states = std::make_tuple(applyLambda(lambdas, std::get(states), *first)...); + } + return states; +} + +// Main function to apply multiple lambdas to each value in the sequence +template +auto reduceM(InIt first, InIt last, const std::tuple& lambdas, std::tuple initStates) +{ + if (first == last) + { + throw std::invalid_argument("Input range cannot be empty"); + } + return applyLambdasImpl(lambdas, first, last, initStates, std::index_sequence_for{}); +} + +//*/ \ No newline at end of file diff --git a/scratch/scratch.cpp b/scratch/scratch.cpp index 5eec9ff..c5cf65f 100644 --- a/scratch/scratch.cpp +++ b/scratch/scratch.cpp @@ -201,18 +201,9 @@ int main() // reduce with binned accumulator auto scale = 1.0;// pow(1024.0, 2); using BINNED_ACCUMULATOR = BinsT; - BINNED_ACCUMULATOR Bin(0.0, scale);// / BINNED_ACCUMULATOR.BIG_C[0]); + BINNED_ACCUMULATOR Bin(0.0, scale);// - //auto mult = 1.0;// pow(1024, -1); - //auto mixedBig = mixed* mult;// *scale;// 1024.; - //scale - double len = static_cast(mixed.size()); - auto maxSize = [](auto lhs, auto rhs) { return max(abs(lhs), abs(rhs)); }; - //auto minSize = [](auto lhs, auto rhs) { return min(abs(lhs), abs(rhs)); }; - auto range = reduce(mixed, maxSize); - - //Bin.m_scaleFactor = Bin.BIG_C;// / (range * len); auto binned_Sum = reduceWithAccumulator(Bin, mixed, BinnedAdd);