HEBench
hebench_utilities_harness.cpp
Go to the documentation of this file.
1 
2 // Copyright (C) 2021 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include <algorithm>
6 #include <cctype>
7 #include <fstream>
8 #include <iomanip>
9 #include <sstream>
10 
12 
13 namespace hebench {
14 namespace Utilities {
15 
16 std::mt19937 RandomGenerator::m_rand;
17 
18 std::string convertToDirectoryName(const std::string &s, bool to_lowercase)
19 {
20  std::stringstream ss_retval;
21  std::string s_tmp = s;
22 
23  // convert all non-alnum to underscores and, if requested, alnum to lowercase
24  std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(),
25  [to_lowercase](unsigned char c) { return (std::isalnum(c) || c == '.' ?
26  (to_lowercase ? std::tolower(c) : c) :
27  '_'); });
28  // remove leading and trailing underscores
29  s_tmp.erase(0, s_tmp.find_first_not_of('_'));
30  s_tmp.erase(s_tmp.find_last_not_of('_') + 1);
31 
32  // remove contiguous underscore repetitions
33  std::size_t pos = 0;
34  while (pos < s_tmp.size())
35  {
36  std::size_t i = s_tmp.find_first_of('_', pos);
37  if (i == std::string::npos)
38  ss_retval << s_tmp.substr(pos);
39  else
40  {
41  if (i < s_tmp.size())
42  ++i;
43  ss_retval << s_tmp.substr(pos, i - pos);
44  i = s_tmp.find_first_not_of('_', i);
45  } // end if
46  pos = i;
47  } // end while
48 
49  return ss_retval.str();
50 }
51 
52 void printArraysAsColumns(std::ostream &os,
53  const hebench::APIBridge::NativeDataBuffer **p_buffers, std::size_t count,
55  bool output_row_index,
56  const char *separator)
57 {
58  switch (data_type)
59  {
61  printArraysAsColumns<std::int32_t>(os, p_buffers, count, output_row_index, separator);
62  break;
63 
65  printArraysAsColumns<std::int64_t>(os, p_buffers, count, output_row_index, separator);
66  break;
67 
69  printArraysAsColumns<float>(os, p_buffers, count, output_row_index, separator);
70  break;
71 
73  printArraysAsColumns<double>(os, p_buffers, count, output_row_index, separator);
74  break;
75 
76  default:
77  printArraysAsColumns<char>(os, p_buffers, count, output_row_index, separator);
78  break;
79  } // end switch
80 }
81 
82 //-----------------------
83 // class RandomGenerator
84 //-----------------------
85 
86 void RandomGenerator::setRandomSeed(std::uint64_t seed)
87 {
88  m_rand.seed(seed);
89 }
90 
92 {
93  setRandomSeed(std::chrono::system_clock::now().time_since_epoch().count());
94 }
95 
96 } // namespace Utilities
97 } // namespace hebench
@ Float64
64 bits IEEE 754 standard floating point real numbers.
Definition: types.h:306
@ Int64
64 bits signed integers.
Definition: types.h:304
DataType
Defines data types for a workload.
Definition: types.h:379
@ Float32
32 bits IEEE 754 standard floating point real numbers.
Definition: types.h:382
@ Int32
32 bits signed integers.
Definition: types.h:380
Structure to contain flexible data.
Definition: types.h:552
std::string convertToDirectoryName(const std::string &s, bool to_lowercase=true)
Converts a string to directory name friendly.
void printArraysAsColumns(std::ostream &os, const hebench::APIBridge::NativeDataBuffer **p_buffers, std::size_t count, hebench::APIBridge::DataType data_type, bool output_row_index=false, const char *separator=" ")
Writes the collection of NativeDataBuffer as columns to the specified output stream.