Skip to content

Commit

Permalink
restructuring specialized posits test infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Dec 17, 2023
1 parent 3aa381a commit cac65dc
Show file tree
Hide file tree
Showing 22 changed files with 735 additions and 453 deletions.
2 changes: 1 addition & 1 deletion benchmark/accuracy/quantization/mpdot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ namespace sw {
if constexpr (bCSV) {
std::cout << stats.stddev << ", " << faEquivalency << ", " << stats.mean ;
for (int i = 0; i < 5; ++i) {
std::cout << ", " << stats.quartiles[i];
std::cout << ", " << stats.quantiles.q[i];
}
std::cout << '\n';
}
Expand Down
18 changes: 9 additions & 9 deletions include/universal/blas/blas_l1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ void strided_print(std::ostream& ostr, size_t n, Vector& x, size_t incx = 1) {
// L1-norm of a vector
template<typename Scalar>
Scalar normL1(const sw::universal::blas::vector<Scalar>& v) {
using namespace sw::universal; // to specialize abs()
Scalar L1Norm{ 0 };
for (auto e : v) {
for (const Scalar& e : v) {
L1Norm += abs(e);
}
return L1Norm;
Expand All @@ -184,7 +183,7 @@ Scalar normL1(const sw::universal::blas::vector<Scalar>& v) {
template<typename Scalar>
Scalar normL2(const sw::universal::blas::vector<Scalar>& v) {
Scalar L2Norm{ 0 };
for (auto e : v) {
for (const Scalar& e : v) {
L2Norm += e * e;
}
return sqrt(L2Norm);
Expand All @@ -196,7 +195,7 @@ Scalar normL3(const sw::universal::blas::vector<Scalar>& v) {
using namespace std;
using namespace sw::universal; // to specialize abs()
Scalar L3Norm{ 0 };
for (auto e : v) {
for (const Scalar& e : v) {
Scalar abse = abs(e);
L3Norm += abse * abse * abse;
}
Expand All @@ -207,7 +206,7 @@ Scalar normL3(const sw::universal::blas::vector<Scalar>& v) {
template<typename Scalar>
Scalar normL4(const sw::universal::blas::vector<Scalar>& v) {
Scalar L4Norm{ 0 };
for (auto e : v) {
for (const Scalar& e : v) {
Scalar esqr = e * e;
L4Norm += esqr * esqr;
}
Expand All @@ -220,7 +219,7 @@ Scalar normLinf(const sw::universal::blas::vector<Scalar>& v) {
using namespace std;
using namespace sw::universal; // to specialize abs()
Scalar LinfNorm{ 0 };
for (auto e : v) {
for (const Scalar& e : v) {
LinfNorm = (abs(e) > LinfNorm) ? abs(e) : LinfNorm;
}
return LinfNorm;
Expand Down Expand Up @@ -251,10 +250,11 @@ Scalar norm(const sw::universal::blas::vector<Scalar>& v, int p) {
break;
default:
{
for (auto e : v) {
norm += pow(abs(e), Scalar( p ));
Scalar sp = Scalar( p );
for (const Scalar& e : v) {
norm += pow(abs(e), sp);
}
norm = pow(norm, Scalar( 1 ) / Scalar( p ));
norm = pow(norm, Scalar( 1 ) / sp);
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions include/universal/blas/solvers/cg_dot_dot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ size_t cg_dot_dot(const Matrix& M, const Matrix& A, const Vector& b, Vector& x,
++itr;
}
if (residual < tolerance) {
std::cout << "solution in " << itr << " iterations\n";
std::cout << "successfully converged in : " << itr << " iterations\n";
}
else {
std::cout << "failed to converge in " << itr << " iterations\n";
std::cout << "failed to converge in : " << itr << " iterations\n";
}

return itr;
Expand Down
4 changes: 2 additions & 2 deletions include/universal/blas/solvers/cg_dot_fdp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ size_t cg_dot_fdp(const Matrix& M, const Matrix& A, const Vector& b, Vector& x,
++itr;
}
if (residual < tolerance) {
std::cout << "solution in " << itr << " iterations\n";
std::cout << "successfully converged in : " << itr << " iterations\n";
}
else {
std::cout << "failed to converge in " << itr << " iterations\n";
std::cout << "failed to converge in : " << itr << " iterations\n";
}

return itr;
Expand Down
161 changes: 81 additions & 80 deletions include/universal/verification/posit_test_randoms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,142 +61,142 @@ namespace sw { namespace universal {

// Execute a binary operator
template<typename TestType>
void executeBinary(int opcode, double da, double db, const TestType& testa, const TestType& testb, TestType& testresult, TestType& testref) {
double reference = 0.0;
void executeBinary(int opcode, double da, double db, double& dc, const TestType& testa, const TestType& testb, TestType& testc, TestType& testref) {
dc = 0.0;
switch (opcode) {
case OPCODE_ADD:
testresult = testa + testb;
reference = da + db;
testc = testa + testb;
dc = da + db;
break;
case OPCODE_SUB:
testresult = testa - testb;
reference = da - db;
testc = testa - testb;
dc = da - db;
break;
case OPCODE_MUL:
testresult = testa * testb;
reference = da * db;
testc = testa * testb;
dc = da * db;
break;
case OPCODE_DIV:
testresult = testa / testb;
reference = da / db;
testc = testa / testb;
dc = da / db;
break;
case OPCODE_IPA:
testresult = testa;
testresult += testb;
reference = da + db;
testc = testa;
testc += testb;
dc = da + db;
break;
case OPCODE_IPS:
testresult = testa;
testresult -= testb;
reference = da - db;
testc = testa;
testc -= testb;
dc = da - db;
break;
case OPCODE_IPM:
testresult = testa;
testresult *= testb;
reference = da * db;
testc = testa;
testc *= testb;
dc = da * db;
break;
case OPCODE_IPD:
testresult = testa;
testresult /= testb;
reference = da / db;
testc = testa;
testc /= testb;
dc = da / db;
break;
case OPCODE_POW:
testresult = sw::universal::pow(testa, testb);
reference = std::pow(da, db);
testc = sw::universal::pow(testa, testb);
dc = std::pow(da, db);
break;
case OPCODE_NOP:
default:
std::cerr << "Unsupported unary operator: operation ignored\n";
std::cerr << "unary operators not supported in executeBinary: operation ignored\n";
break;
}
testref = reference;
testref = dc;
}

// Execute a unary operator
template<typename TestType>
void executeUnary(int opcode, double da, const TestType& testa, TestType& testref, TestType& testresult, double dminpos) {
double reference = 0.0;
void executeUnary(int opcode, double da, double& dc, const TestType& testa, TestType& testc, TestType& testref, double dminpos) {
dc = 0.0;
switch (opcode) {
case OPCODE_SQRT:
testresult = sw::universal::sqrt(testa);
reference = std::sqrt(da);
testc = sw::universal::sqrt(testa);
dc = std::sqrt(da);
break;
case OPCODE_EXP:
testresult = sw::universal::exp(testa);
reference = std::exp(da);
if (0.0 == reference) reference = dminpos;
testc = sw::universal::exp(testa);
dc = std::exp(da);
if (0.0 == dc) dc = dminpos;
break;
case OPCODE_EXP2:
testresult = sw::universal::exp2(testa);
reference = std::exp2(da);
if (0.0 == reference) reference = dminpos;
testc = sw::universal::exp2(testa);
dc = std::exp2(da);
if (0.0 == dc) dc = dminpos;
break;
case OPCODE_LOG:
testresult = sw::universal::log(testa);
reference = std::log(da);
testc = sw::universal::log(testa);
dc = std::log(da);
break;
case OPCODE_LOG2:
testresult = sw::universal::log2(testa);
reference = std::log2(da);
testc = sw::universal::log2(testa);
dc = std::log2(da);
break;
case OPCODE_LOG10:
testresult = sw::universal::log10(testa);
reference = std::log10(da);
testc = sw::universal::log10(testa);
dc = std::log10(da);
break;
case OPCODE_SIN:
testresult = sw::universal::sin(testa);
reference = std::sin(da);
testc = sw::universal::sin(testa);
dc = std::sin(da);
break;
case OPCODE_COS:
testresult = sw::universal::cos(testa);
reference = std::cos(da);
testc = sw::universal::cos(testa);
dc = std::cos(da);
break;
case OPCODE_TAN:
testresult = sw::universal::tan(testa);
reference = std::tan(da);
testc = sw::universal::tan(testa);
dc = std::tan(da);
break;
case OPCODE_ASIN:
testresult = sw::universal::asin(testa);
reference = std::asin(da);
testc = sw::universal::asin(testa);
dc = std::asin(da);
break;
case OPCODE_ACOS:
testresult = sw::universal::acos(testa);
reference = std::acos(da);
testc = sw::universal::acos(testa);
dc = std::acos(da);
break;
case OPCODE_ATAN:
testresult = sw::universal::atan(testa);
reference = std::atan(da);
testc = sw::universal::atan(testa);
dc = std::atan(da);
break;
case OPCODE_SINH:
testresult = sw::universal::sinh(testa);
reference = std::sinh(da);
testc = sw::universal::sinh(testa);
dc = std::sinh(da);
break;
case OPCODE_COSH:
testresult = sw::universal::cosh(testa);
reference = std::cosh(da);
testc = sw::universal::cosh(testa);
dc = std::cosh(da);
break;
case OPCODE_TANH:
testresult = sw::universal::tanh(testa);
reference = std::tanh(da);
testc = sw::universal::tanh(testa);
dc = std::tanh(da);
break;
case OPCODE_ASINH:
testresult = sw::universal::asinh(testa);
reference = std::asinh(da);
testc = sw::universal::asinh(testa);
dc = std::asinh(da);
break;
case OPCODE_ACOSH:
testresult = sw::universal::acosh(testa);
reference = std::acosh(da);
testc = sw::universal::acosh(testa);
dc = std::acosh(da);
break;
case OPCODE_ATANH:
testresult = sw::universal::atanh(testa);
reference = std::atanh(da);
testc = sw::universal::atanh(testa);
dc = std::atanh(da);
break;
case OPCODE_NOP:
default:
std::cerr << "Unsupported binary operator: operation ignored\n";
break;
}
testref = reference;
testref = dc;
}

// generate a random set of operands to test the binary operators for a posit configuration
Expand Down Expand Up @@ -245,18 +245,19 @@ namespace sw { namespace universal {
std::uniform_int_distribution<unsigned long long> distr;
int nrOfFailedTests = 0;
for (unsigned i = 1; i < nrOfRandoms; i++) {
posit<nbits, es> testa, testb, testresult, testref;
posit<nbits, es> testa, testb, testc, testref;
testa.setbits(distr(eng));
testb.setbits(distr(eng));
double da = double(testa);
double db = double(testb);
double dc = 0.0;
// in case you have numeric_limits<long double>::digits trouble... this will show that
//std::cout << "sizeof da: " << sizeof(da) << " bits in significant " << (std::numeric_limits<long double>::digits - 1) << " value da " << da << " at index " << ia << " testa " << testa << std::endl;
//std::cout << "sizeof db: " << sizeof(db) << " bits in significant " << (std::numeric_limits<long double>::digits - 1) << " value db " << db << " at index " << ia << " testa " << testb << std::endl;

#if POSIT_THROW_ARITHMETIC_EXCEPTION
try {
executeBinary(opcode, da, db, testa, testb, testref, testresult);
executeBinary(opcode, da, db, dc, testa, testb, testc, testref);
}
catch (const posit_arithmetic_exception& err) {
if (testa.isnar() || testb.isnar() || ((opcode == OPCODE_DIV || opcode == OPCODE_IPD) && testb.iszero())) {
Expand All @@ -267,16 +268,15 @@ namespace sw { namespace universal {
}
}
#else
executeBinary(opcode, da, db, testa, testb, testref, testresult);
executeBinary(opcode, da, db, dc, testa, testb, testc, testref);
#endif

testresult = testref;
if (testresult != testref) {
if (testc != testref) {
nrOfFailedTests++;
if (reportTestCases) ReportBinaryArithmeticError("FAIL", operation_string, testa, testb, testresult, testref);
if (reportTestCases) ReportBinaryArithmeticError("FAIL", operation_string, testa, testb, testc, testref);
}
else {
//if (reportTestCases) ReportBinaryArithmeticSuccess("PASS", operation_string, testa, testb, testresult, testref);
//if (reportTestCases) ReportBinaryArithmeticSuccess("PASS", operation_string, testa, testb, testc, testref);
}
}
return nrOfFailedTests;
Expand Down Expand Up @@ -337,15 +337,16 @@ namespace sw { namespace universal {
std::uniform_int_distribution<unsigned long long> distr;
int nrOfFailedTests = 0;
for (unsigned i = 1; i < nrOfRandoms; i++) {
TestType testa, testresult, testref;
TestType testa, testc, testref;
testa.setbits(distr(eng));
if (sqrtOperator && testa < 0) testa = -testa;
double da = double(testa);
double dc = 0.0;
// in case you have numeric_limits<long double>::digits trouble... this will show that
//std::cout << "sizeof da: " << sizeof(da) << " bits in significant " << (std::numeric_limits<long double>::digits - 1) << " value da " << da << " at index " << ia << " testa " << testa << std::endl;
#if POSIT_THROW_ARITHMETIC_EXCEPTION
try {
executeUnary(opcode, da, testa, testref, testresult, dminpos);
executeUnary(opcode, da, dc, testa, testc, testref, dminpos);
}
catch (const posit_arithmetic_exception& err) {
if (testa.isnar()) {
Expand All @@ -356,14 +357,14 @@ namespace sw { namespace universal {
}
}
#else
executeUnary(opcode, da, testa, testref, testresult, dminpos);
executeUnary(opcode, da, dc, testa, testc, testref, dminpos);
#endif
if (testresult != testref) {
if (testc != testref) {
nrOfFailedTests++;
if (reportTestCases) ReportUnaryArithmeticError("FAIL", operation_string, testa, testresult, testref);
if (reportTestCases) ReportUnaryArithmeticError("FAIL", operation_string, testa, testc, testref);
}
else {
//if (reportTestCases) ReportUnaryArithmeticSuccess("PASS", operation_string, testa, testresult, testref);
//if (reportTestCases) ReportUnaryArithmeticSuccess("PASS", operation_string, testa, testc, testref);
}
}

Expand Down
Loading

0 comments on commit cac65dc

Please sign in to comment.