Skip to content

Commit

Permalink
fix up variacic reduce tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andyD123 committed Feb 1, 2025
1 parent 2fb1e21 commit b9280aa
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 11 deletions.
75 changes: 74 additions & 1 deletion VectorTest/DR3_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2269,6 +2268,80 @@ void testReduce_VecM_22(int SZ)



void testReduce_GeneralVariadic(int SZ)
{


std::vector<Numeric> 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
Expand Down
32 changes: 32 additions & 0 deletions Vectorisation/VecX/dr3.h
Original file line number Diff line number Diff line change
Expand Up @@ -959,3 +959,35 @@ Vec<INS_VEC> scan(const Vec<INS_VEC>& rhs1, OP& oper)
{
return ApplyScan(rhs1, oper);
}

///*
//////////////// variadic reduction
// Helper function to call a single lambda with the current state and value
template<std::size_t Index, typename Tuple, typename State, typename Value>
auto applyLambda(const Tuple& lambdas, State state, Value value) {
return std::get<Index>(lambdas)(state, value);
}

// Recursive case: apply the lambdas to each value in the sequence
template<typename Tuple, typename InIt, typename... States, std::size_t... Indices>
auto applyLambdasImpl(const Tuple& lambdas, InIt first, InIt last, std::tuple<States...> states, std::index_sequence<Indices...>)
{
for (; first != last; ++first)
{
states = std::make_tuple(applyLambda<Indices>(lambdas, std::get<Indices>(states), *first)...);
}
return states;
}

// Main function to apply multiple lambdas to each value in the sequence
template<typename InIt, typename... Lambdas, typename... States>
auto reduceM(InIt first, InIt last, const std::tuple<Lambdas...>& lambdas, std::tuple<States...> initStates)
{
if (first == last)
{
throw std::invalid_argument("Input range cannot be empty");
}
return applyLambdasImpl(lambdas, first, last, initStates, std::index_sequence_for<Lambdas...>{});
}

//*/
11 changes: 1 addition & 10 deletions scratch/scratch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,9 @@ int main()
// reduce with binned accumulator
auto scale = 1.0;// pow(1024.0, 2);
using BINNED_ACCUMULATOR = BinsT<VecXX::INS>;
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<double>(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);

Expand Down

0 comments on commit b9280aa

Please sign in to comment.