Skip to content

Commit

Permalink
Compare exact values when data type is integer [Fixes #17]
Browse files Browse the repository at this point in the history
  • Loading branch information
definelicht committed Aug 13, 2020
1 parent 4cce901 commit 62c4e44
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
12 changes: 9 additions & 3 deletions host/RunHardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,18 @@ int main(int argc, char **argv) {
// Convert to single element vector
const auto cTest = Unpack<kMemoryWidthM>(cMem);

for (int i = 0; i < size_n; ++i) {
for (int j = 0; j < size_m; ++j) {
for (size_t i = 0; i < size_n; ++i) {
for (size_t j = 0; j < size_m; ++j) {
const auto testVal = make_signed<Data_t>(cTest[i * size_m + j]);
const auto refVal = make_signed<Data_t>(cRef[i * size_m + j]);
const Data_t diff = std::abs(testVal - refVal);
if (diff / refVal > static_cast<Data_t>(1e-3)) {
bool mismatch;
if (std::is_floating_point<Data_t>::value) {
mismatch = diff / refVal > static_cast<Data_t>(1e-3);
} else {
mismatch = diff != 0;
}
if (mismatch) {
std::cerr << "Mismatch at (" << i << ", " << j << "): " << testVal
<< " vs. " << refVal << "\n";
return 1;
Expand Down
7 changes: 3 additions & 4 deletions include/Utility.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// @author Johannes de Fine Licht ([email protected])
/// @date June 2017
/// @copyright This software is copyrighted under the BSD 3-Clause License.
/// @copyright This software is copyrighted under the BSD 3-Clause License.

#pragma once

Expand All @@ -10,8 +9,8 @@
#include <iterator>
#include <sstream>
#include <vector>
#include "hlslib/xilinx/SDAccel.h"
#include "MatrixMultiplication.h"
#include "hlslib/xilinx/SDAccel.h"
#ifdef MM_HAS_BLAS
#include "cblas.h"
#endif
Expand All @@ -37,7 +36,7 @@ void Naive(IteratorRead aBegin, IteratorRead bBegin, IteratorWrite cBegin,
const auto elemB = bBegin[k * sizeM + m];
acc = OperatorReduce::Apply(acc, OperatorMap::Apply(elemA, elemB));
}
cBegin[n * sizeM + m] = acc;
cBegin[n * sizeM + m] = acc;
}
}
}
Expand Down
27 changes: 16 additions & 11 deletions test/TestSimulation.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/// @author Johannes de Fine Licht ([email protected])
/// @date June 2017
/// @copyright This software is copyrighted under the BSD 3-Clause License.
/// @copyright This software is copyrighted under the BSD 3-Clause License.

#include "Utility.h"
#include "MatrixMultiplication.h"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <random>
#include <type_traits>
#include <vector>
#include "MatrixMultiplication.h"
#include "Utility.h"

int main(int argc, char **argv) {

#ifdef MM_DYNAMIC_SIZES
if (argc < 4 || argc > 4) {
std::cerr << "Usage: ./TestSimulation N K M" << std::endl;
Expand Down Expand Up @@ -46,9 +44,10 @@ int main(int argc, char **argv) {
std::vector<Data_t> cReference(size_n * size_m, 0);

std::default_random_engine rng(kSeed);
typename std::conditional<
std::is_integral<Data_t>::value, std::uniform_int_distribution<unsigned long>,
std::uniform_real_distribution<double>>::type dist(1, 10);
typename std::conditional<std::is_integral<Data_t>::value,
std::uniform_int_distribution<unsigned long>,
std::uniform_real_distribution<double>>::type
dist(1, 10);

std::for_each(a.begin(), a.end(),
[&dist, &rng](Data_t &in) { in = Data_t(dist(rng)); });
Expand Down Expand Up @@ -78,9 +77,15 @@ int main(int argc, char **argv) {
const auto testVal = make_signed<Data_t>(cTest[i * size_m + j]);
const auto refVal = make_signed<Data_t>(cReference[i * size_m + j]);
const Data_t diff = std::abs(testVal - refVal);
if (diff / refVal > static_cast<Data_t>(1e-3)) {
std::cerr << "Mismatch detected at (" << i << ", " << j
<< "): " << testVal << " vs. " << refVal << "\n";
bool mismatch;
if (std::is_floating_point<Data_t>::value) {
mismatch = diff / refVal > static_cast<Data_t>(1e-3);
} else {
mismatch = diff != 0;
}
if (mismatch) {
std::cerr << "Mismatch at (" << i << ", " << j << "): " << testVal
<< " vs. " << refVal << "\n";
return 1;
}
}
Expand Down

0 comments on commit 62c4e44

Please sign in to comment.