message: Added validation to simple example
diff: */
  #include <algorithm>
  +#include <cmath>
  #include <iomanip>
  #include <iostream>
  @@ -61,9 +62,36 @@ struct Parameters {
  int count;
  };
  -static void run(const ISPCRTDeviceType device_type, const unsigned int SIZE) {
  +void simple_CPU_validation(std::vector<float> vin, std::vector<float> &vgold, const unsigned int SIZE) {
  + for (unsigned int i = 0; i < SIZE; i++) {
  + float v = vin[i];
  + if (v < 3.)
  + v = v * v;
  + else
  + v = std::sqrt(v);
  +
  + vgold[i] = v;
  + }
  +}
  +
  +#define EPSILON 0.01f
  +bool validate_result(std::vector<float> vout, std::vector<float> vgold, const unsigned int SIZE) {
  + bool bValid = true;
  + for (unsigned int i = 0; i < SIZE; i++) {
  + float delta = (float)fabs(vgold[i] - vout[i]);
  + if (delta > EPSILON) {
  + std::cout << "Validation failed on i=" << i << ": vout[i] = " << vout[i] << ", but " << vgold[i]
  + << " was expected\n";
  + bValid = false;
  + }
  + }
  + return bValid;
  +}
  +
  +static int run(const ISPCRTDeviceType device_type, const unsigned int SIZE) {
  std::vector<float> vin(SIZE);
  std::vector<float> vout(SIZE);
  + std::vector<float> vgold(SIZE);
  ispcrt::Device device(device_type);
  @@ -91,6 +119,9 @@ static void run(const ISPCRTDeviceType device_type, const unsigned int SIZE) {
  std::generate(vin.begin(), vin.end(), [i = 0]() mutable { return i++; });
  + // Calculate gold result
  + simple_CPU_validation(vin, vgold, SIZE);
  +
  // ispcrt::Array objects which used as inputs for ISPC kernel should be
  // explicitly copied to device from host
  queue.copyToDevice(p_dev);
  @@ -116,9 +147,16 @@ static void run(const ISPCRTDeviceType device_type, const unsigned int SIZE) {
  queue.sync();
  std::cout << "Executed on: " << device_type << '\n' << std::setprecision(6) << std::fixed;
  +
  + // Check and print result
  + bool bValid = validate_result(vout, vgold, SIZE);
  + if (bValid) {
  for (int i = 0; i < SIZE; i++) {
  std::cout << i << ": simple(" << vin[i] << ") = " << vout[i] << '\n';
  }
  + return 0;
  + }
  + return -1;
  }
  void usage(const char *p) {
  @@ -149,7 +187,6 @@ int main(int argc, char *argv[]) {
  }
  }
  - run(device_type, SIZE);
  -
  - return 0;
  + int success = run(device_type, SIZE);
  + return success;
  }
  