|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | +#include <iostream> |
| 6 | +#include <chrono> |
| 7 | + |
| 8 | +#include "CLI11.hpp" |
| 9 | +#include "daal.h" |
| 10 | + |
| 11 | +namespace dm = daal::data_management; |
| 12 | +namespace ds = daal::services; |
| 13 | +namespace da = daal::algorithms; |
| 14 | + |
| 15 | +/* |
| 16 | + * Parse a size argument as given to the command-line options. |
| 17 | + * This consists of each element of the size, delimited by |
| 18 | + * a 'x' or ','. |
| 19 | + * |
| 20 | + * Parameters |
| 21 | + * ---------- |
| 22 | + * in : std::string |
| 23 | + * String to parse |
| 24 | + * size : std::vector<int> &size |
| 25 | + * Vector to which we should output size values |
| 26 | + */ |
| 27 | +void parse_size(std::string in, std::vector<int> &size) { |
| 28 | + |
| 29 | + std::stringstream tmp(in); |
| 30 | + int i; |
| 31 | + |
| 32 | + while (tmp >> i) { |
| 33 | + size.push_back(i); |
| 34 | + char c = tmp.peek(); |
| 35 | + if (c == 'x' || c == ',') |
| 36 | + tmp.ignore(); |
| 37 | + } |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +/* |
| 43 | + * Terminate the program with an error message if the number of dimensions |
| 44 | + * in the given size vector does not match the expected number of dimensions. |
| 45 | + * |
| 46 | + * Parameters |
| 47 | + * ---------- |
| 48 | + * size : std::vector<int> &size |
| 49 | + * Vector to check |
| 50 | + * expected : int |
| 51 | + * Number of dimensions we expect |
| 52 | + */ |
| 53 | +void check_dims(std::vector<int> &size, int expected) { |
| 54 | + |
| 55 | + if (size.size() != expected) { |
| 56 | + std::cerr << "error: expected " << expected << " dimensions in size " |
| 57 | + << "but got " << size.size(); |
| 58 | + std::exit(1); |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +/* |
| 65 | + * Try to set DAAL's number of threads to the given number of threads, |
| 66 | + * and returns the actual number of threads DAAL will use. |
| 67 | + * |
| 68 | + * Parameters |
| 69 | + * ---------- |
| 70 | + * num_threads : int |
| 71 | + * Number of threads we ask DAAL to use |
| 72 | + * |
| 73 | + * Returns |
| 74 | + * ------- |
| 75 | + * int |
| 76 | + * Number of threads DAAL says it will use |
| 77 | + */ |
| 78 | +int set_threads(int num_threads) { |
| 79 | + |
| 80 | + if (num_threads > 0) |
| 81 | + ds::Environment::getInstance()->setNumberOfThreads(num_threads); |
| 82 | + |
| 83 | + return ds::Environment::getInstance()->getNumberOfThreads(); |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +/* |
| 89 | + * Time the given function for the specified number of repetitions, |
| 90 | + * returning a pair of a vector of durations and the LAST result. |
| 91 | + */ |
| 92 | +template <typename T> |
| 93 | +std::pair<std::vector<std::chrono::duration<double>>, T> |
| 94 | +time_vec(std::function<T()> func, int reps) { |
| 95 | + |
| 96 | + std::vector<std::chrono::duration<double>> vec; |
| 97 | + T result; |
| 98 | + for (int i = 0; i < reps; i++) { |
| 99 | + auto t0 = std::chrono::high_resolution_clock::now(); |
| 100 | + result = func(); |
| 101 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 102 | + vec.push_back(t1 - t0); |
| 103 | + } |
| 104 | + return std::make_pair(vec, result); |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +/* |
| 110 | + * Time the given function for the specified number of repetitions, |
| 111 | + * returning a pair of the minimum duration and the LAST result. |
| 112 | + */ |
| 113 | +template <typename T> |
| 114 | +std::pair<double, T> time_min(std::function<T()> func, int reps) { |
| 115 | + |
| 116 | + auto pair = time_vec(func, reps); |
| 117 | + auto times = pair.first; |
| 118 | + double time = std::min_element(times.begin(), times.end())->count(); |
| 119 | + |
| 120 | + return std::make_pair(time, pair.second); |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +/* |
| 126 | + * Create a DAAL HomogenNumericTable from an array in memory. |
| 127 | + */ |
| 128 | +template <typename T> |
| 129 | +dm::NumericTablePtr make_table(T *data, size_t rows, size_t cols) { |
| 130 | + |
| 131 | + return dm::HomogenNumericTable<T>::create(data, cols, rows); |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +/* |
| 137 | + * Generate an array of random numbers. |
| 138 | + */ |
| 139 | +double *gen_random(size_t n) { |
| 140 | + |
| 141 | + double *x = new double[n]; |
| 142 | + for (size_t i = 0; i < n; i++) |
| 143 | + x[i] = (double) rand() / RAND_MAX; |
| 144 | + return x; |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +void add_common_args(CLI::App &app, |
| 150 | + std::string &batch, std::string &arch, |
| 151 | + std::string &prefix, int &num_threads, |
| 152 | + bool &header, bool &verbose) { |
| 153 | + |
| 154 | + batch = "?"; |
| 155 | + app.add_option("-b,--batch", batch, "Batch ID, for bookkeeping"); |
| 156 | + |
| 157 | + arch = "?"; |
| 158 | + app.add_option("-a,--arch", arch, "Machine architecture, for bookkeeping"); |
| 159 | + |
| 160 | + prefix = "Native-C"; |
| 161 | + app.add_option("-p,--prefix", prefix, "Prefix string, for bookkeeping"); |
| 162 | + |
| 163 | + num_threads = 0; |
| 164 | + app.add_option("-n,--num-threads", num_threads, "Number of threads for DAAL to use", true); |
| 165 | + |
| 166 | + header = false; |
| 167 | + app.add_flag("--header", header, "Output CSV header"); |
| 168 | + |
| 169 | + verbose = false; |
| 170 | + app.add_flag("--verbose", header, "Output extra debug messages"); |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | + |
0 commit comments