call_args.cc 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

#include <embb/base/perf/call_args.h>
#include <embb/base/core_set.h>
#include <embb/base/perf/timer.h>
#include <string>
#include <ostream>
#include <cstdlib>

namespace embb {
namespace base {
namespace perf {

void CallArgs::Parse(int argc, char * argv[]) {
  // Set config from command line arguments:
  for (int paramIndex = 1; paramIndex < argc; paramIndex += 2) {
    // Max. number of threads to resolve speedup:
    if (std::string(argv[paramIndex]) == "-t") {
      size_t threads_param = static_cast<size_t>(
        atoi(argv[paramIndex + 1]));
      if (threads_param > 0 && threads_param < max_threads) {
        max_threads = threads_param;
      }
    }
    // Test vector size:
    if (std::string(argv[paramIndex]) == "-n") {
      size_t vsize_param = static_cast<size_t>(
        atoi(argv[paramIndex + 1]));
      if (vsize_param > 0) {
        vector_size = vsize_param;
      }
    }
    // Performance counter scaling:
    if (std::string(argv[paramIndex]) == "-f") {
      unsigned int scale_param = static_cast<unsigned int>(
        atoi(argv[paramIndex + 1]));
      if (scale_param > 0) {
        counter_scale = scale_param;
      }
    }
    // Element type:
    if (std::string(argv[paramIndex]) == "-e") {
      element_type = UNDEFINED_SCALAR_TYPE;
      ::std::string type = argv[paramIndex + 1];
      if (type == "float") {
        element_type = FLOAT;
      }
      else if (type == "double") {
        element_type = DOUBLE;
      }
    }
    // Stress type:
    if (std::string(argv[paramIndex]) == "-s") {
      stress_type = UNDEFINED_STRESS_TYPE;
      ::std::string type = argv[paramIndex + 1];
      if (type == "cpu") {
        stress_type = CPU_STRESS;
      }
      else if (type == "ram") {
        stress_type = RAM_STRESS;
      }
    }
    // Test load factor:
    if (std::string(argv[paramIndex]) == "-l") {
      load_factor = static_cast<size_t>(
        atoi(argv[paramIndex + 1]));
    }
    // Additional test parameter:
    if (std::string(argv[paramIndex]) == "-p") {
      parallel_base_ref = atoi(argv[paramIndex + 1]);
    }
    // Sanitizing and error handling:
    if (element_type == UNDEFINED_SCALAR_TYPE) {
      throw ::std::runtime_error(
        "Invalid setting for element type (-e int|float|double)");
    }
    if (stress_type == UNDEFINED_STRESS_TYPE) {
      throw ::std::runtime_error(
        "Invalid setting for stress test type (-s ram|cpu)");
    }
  }
  // Calibrate performance time sampling:
  embb::base::perf::Timer::Calibrate(
    embb::base::perf::TimeMeasure::Counter,
    CounterScale());
}

void CallArgs::Print(std::ostream & os) {
  os << "Max. threads:    (-t) " << MaxThreads() << std::endl
     << "Vector size:     (-n) " << VectorSize() << std::endl
     << "Load factor:     (-l) " << LoadFactor() << std::endl
     << "Element type:    (-e) " << ElementTypeName() << std::endl
     << "Stress mode:     (-s) " << StressModeName() << std::endl
     << "Serial base ref: (-p) " << ParallelBaseReference() << std::endl
     << "Time sampling:   (-f) " << embb::base::perf::Timer::TimerName() 
     << std::endl;
}

} // namespace perf
} // namespace base
} // namespace embb