OmniSciDB  b28c0d5765
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UtilityTableFunctions.cpp File Reference
#include <string>
#include "Shared/ThreadInfo.h"
#include "UtilityTableFunctions.h"
#include <chrono>
#include <random>
#include <thread>
+ Include dependency graph for UtilityTableFunctions.cpp:

Go to the source code of this file.

Functions

int64_t numStepsBetween (int64_t start, int64_t stop, int64_t step)
 
template<typename T >
int64_t numStepsBetween (Timestamp start, Timestamp stop, T step)
 
template<typename T , typename K >
int32_t generate_series_parallel (const T start, const T stop, const K step, Column< T > &series_output)
 
template<typename T , typename K >
NEVER_INLINE HOST int32_t generate_series__cpu_template (TableFunctionManager &mgr, const T start, const T stop, const K step, Column< T > &series_output)
 
template<typename T >
NEVER_INLINE HOST int32_t generate_series__cpu_template (TableFunctionManager &mgr, const T start, const T stop, Column< T > &series_output)
 
HOST std::string gen_random_str (std::mt19937 &generator, const int64_t str_len)
 
EXTENSION_NOINLINE_HOST int32_t generate_random_strings__cpu_ (TableFunctionManager &mgr, const int64_t num_strings, const int64_t string_length, Column< int64_t > &output_id, Column< TextEncodingDict > &output_strings)
 
template int64_t numStepsBetween (Timestamp, Timestamp, DayTimeInterval)
 
template int64_t numStepsBetween (Timestamp, Timestamp, YearMonthTimeInterval)
 
template int32_t generate_series_parallel (int64_t, int64_t, int64_t, Column< int64_t > &)
 
template int32_t generate_series_parallel (Timestamp, Timestamp, DayTimeInterval, Column< Timestamp > &)
 
template int32_t generate_series_parallel (Timestamp, Timestamp, YearMonthTimeInterval, Column< Timestamp > &)
 
template int32_t generate_series__cpu_template (TableFunctionManager &, int64_t, int64_t, int64_t, Column< int64_t > &)
 
template int32_t generate_series__cpu_template (TableFunctionManager &, Timestamp, Timestamp, DayTimeInterval, Column< Timestamp > &)
 
template int32_t generate_series__cpu_template (TableFunctionManager &, Timestamp, Timestamp, YearMonthTimeInterval, Column< Timestamp > &)
 
template int32_t generate_series__cpu_template (TableFunctionManager &, int64_t, int64_t, Column< int64_t > &)
 

Function Documentation

HOST std::string gen_random_str ( std::mt19937 &  generator,
const int64_t  str_len 
)

Definition at line 103 of file UtilityTableFunctions.cpp.

Referenced by generate_random_strings__cpu_().

103  {
104  constexpr char alphanum_lookup_table[] =
105  "0123456789"
106  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
107  "abcdefghijklmnopqrstuvwxyz";
108  constexpr size_t char_mod = sizeof(alphanum_lookup_table) - 1;
109  std::uniform_int_distribution<int32_t> rand_distribution(0, char_mod);
110 
111  std::string tmp_s;
112  tmp_s.reserve(str_len);
113  for (int i = 0; i < str_len; ++i) {
114  tmp_s += alphanum_lookup_table[rand_distribution(generator)];
115  }
116  return tmp_s;
117 }

+ Here is the caller graph for this function:

EXTENSION_NOINLINE_HOST int32_t generate_random_strings__cpu_ ( TableFunctionManager mgr,
const int64_t  num_strings,
const int64_t  string_length,
Column< int64_t > &  output_id,
Column< TextEncodingDict > &  output_strings 
)

Definition at line 125 of file UtilityTableFunctions.cpp.

References CHECK_LE, DEBUG_TIMER, gen_random_str(), ThreadInfo::num_elems_per_thread, ThreadInfo::num_threads, threading_serial::parallel_for(), and TableFunctionManager::set_output_row_size().

129  {
130  auto timer = DEBUG_TIMER(__func__);
131  // Check for out-of-range errors for the input parameters
132  // in the function instead of with require due to issue encountered
133  // with require over multiple variables
134  constexpr int64_t max_strings{10000000L};
135  constexpr int64_t max_str_len{10000L};
136  if (num_strings > max_strings) {
137  return mgr.ERROR_MESSAGE(
138  "generate_random_strings: num_strings must be between 0 and 10,000,000.");
139  }
140  if (string_length > max_str_len) {
141  return mgr.ERROR_MESSAGE(
142  "generate_random_strings: string_length must be between 1 and 10,000.");
143  }
144  if (num_strings == 0L) {
145  // Bail early as there is no work to be done
146  return 0;
147  }
148 
149  mgr.set_output_row_size(num_strings);
150  constexpr int64_t target_strings_per_thread{5000};
151  const ThreadInfo thread_info(
152  std::thread::hardware_concurrency(), num_strings, target_strings_per_thread);
153  std::vector<std::mt19937> per_thread_rand_generators;
154  per_thread_rand_generators.reserve(thread_info.num_threads);
155  for (int64_t thread_idx = 0; thread_idx < thread_info.num_threads; ++thread_idx) {
156  const uint64_t seed = std::chrono::duration_cast<std::chrono::nanoseconds>(
157  std::chrono::system_clock::now().time_since_epoch())
158  .count() +
159  thread_idx * 971;
160  per_thread_rand_generators.emplace_back(seed);
161  }
162  std::vector<std::string> rand_strings(num_strings);
163  tbb::task_arena limited_arena(thread_info.num_threads);
164  limited_arena.execute([&] {
165  CHECK_LE(tbb::this_task_arena::max_concurrency(), thread_info.num_threads);
167  tbb::blocked_range<int64_t>(0, num_strings, thread_info.num_elems_per_thread),
168  [&](const tbb::blocked_range<int64_t>& r) {
169  const int64_t tbb_thread_idx = tbb::this_task_arena::current_thread_index();
170  const int64_t start_out_idx = r.begin();
171  const int64_t end_out_idx = r.end();
172  for (int64_t out_idx = start_out_idx; out_idx != end_out_idx; ++out_idx) {
173  rand_strings[out_idx] =
174  gen_random_str(per_thread_rand_generators[tbb_thread_idx], string_length);
175  }
176  },
177  tbb::simple_partitioner());
178  });
179  const std::vector<int32_t> rand_string_ids =
180  output_strings.string_dict_proxy_->getOrAddTransientBulk(rand_strings);
181  for (int64_t row_idx = 0; row_idx < num_strings; row_idx++) {
182  output_id[row_idx] = row_idx;
183  output_strings[row_idx] = rand_string_ids[row_idx];
184  }
185  return num_strings;
186 }
void set_output_row_size(int64_t num_rows)
StringDictionaryProxy * string_dict_proxy_
Definition: heavydbTypes.h:875
#define CHECK_LE(x, y)
Definition: Logger.h:233
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
std::vector< int32_t > getOrAddTransientBulk(const std::vector< std::string > &strings)
#define DEBUG_TIMER(name)
Definition: Logger.h:374
HOST std::string gen_random_str(std::mt19937 &generator, const int64_t str_len)

+ Here is the call graph for this function:

template<typename T , typename K >
NEVER_INLINE HOST int32_t generate_series__cpu_template ( TableFunctionManager mgr,
const T  start,
const T  stop,
const K  step,
Column< T > &  series_output 
)

Definition at line 58 of file UtilityTableFunctions.cpp.

References generate_series_parallel(), numStepsBetween(), TableFunctionManager::set_output_row_size(), and to_string().

Referenced by generate_series__cpu_template().

62  {
63  const int64_t MAX_ROWS{1L << 30};
64  const int64_t PARALLEL_THRESHOLD{10000L};
65  const int64_t num_rows = numStepsBetween(start, stop, step) + 1;
66  if (num_rows <= 0) {
67  mgr.set_output_row_size(0);
68  return 0;
69  }
70  mgr.set_output_row_size(num_rows);
71 
72  if (num_rows > MAX_ROWS) {
73  return mgr.ERROR_MESSAGE(
74  "Invocation of generate_series would result in " + std::to_string(num_rows) +
75  " rows, which exceeds the max limit of " + std::to_string(MAX_ROWS) + " rows.");
76  }
77 
78 #ifdef HAVE_TBB
79  if (num_rows > PARALLEL_THRESHOLD) {
80  return generate_series_parallel(start, stop, step, series_output);
81  }
82 #endif
83 
84  for (int64_t out_idx = 0; out_idx != num_rows; ++out_idx) {
85  series_output[out_idx] = start + (step * out_idx);
86  }
87  return num_rows;
88 }
void set_output_row_size(int64_t num_rows)
std::string to_string(char const *&&v)
int64_t numStepsBetween(int64_t start, int64_t stop, int64_t step)
int32_t generate_series_parallel(const T start, const T stop, const K step, Column< T > &series_output)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename T >
NEVER_INLINE HOST int32_t generate_series__cpu_template ( TableFunctionManager mgr,
const T  start,
const T  stop,
Column< T > &  series_output 
)

Definition at line 91 of file UtilityTableFunctions.cpp.

References generate_series__cpu_template().

94  {
96  mgr, start, stop, static_cast<int64_t>(1), series_output);
97 }
NEVER_INLINE HOST int32_t generate_series__cpu_template(TableFunctionManager &mgr, const T start, const T stop, const K step, Column< T > &series_output)

+ Here is the call graph for this function:

template int32_t generate_series__cpu_template ( TableFunctionManager ,
int64_t  ,
int64_t  ,
int64_t  ,
Column< int64_t > &   
)
template int32_t generate_series__cpu_template ( TableFunctionManager ,
Timestamp  ,
Timestamp  ,
DayTimeInterval  ,
Column< Timestamp > &   
)
template int32_t generate_series__cpu_template ( TableFunctionManager ,
Timestamp  ,
Timestamp  ,
YearMonthTimeInterval  ,
Column< Timestamp > &   
)
template int32_t generate_series__cpu_template ( TableFunctionManager ,
int64_t  ,
int64_t  ,
Column< int64_t > &   
)
template<typename T , typename K >
int32_t generate_series_parallel ( const T  start,
const T  stop,
const K  step,
Column< T > &  series_output 
)

Definition at line 39 of file UtilityTableFunctions.cpp.

References numStepsBetween(), and threading_serial::parallel_for().

Referenced by generate_series__cpu_template().

42  {
43  const int64_t num_rows = numStepsBetween(start, stop, step) + 1;
44 
45  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_rows),
46  [&](const tbb::blocked_range<int64_t>& r) {
47  const int64_t start_out_idx = r.begin();
48  const int64_t end_out_idx = r.end();
49  for (int64_t out_idx = start_out_idx; out_idx != end_out_idx;
50  ++out_idx) {
51  series_output[out_idx] = start + (step * out_idx);
52  }
53  });
54  return num_rows;
55 }
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
int64_t numStepsBetween(int64_t start, int64_t stop, int64_t step)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template int32_t generate_series_parallel ( int64_t  ,
int64_t  ,
int64_t  ,
Column< int64_t > &   
)
template int32_t generate_series_parallel ( Timestamp  ,
Timestamp  ,
DayTimeInterval  ,
Column< Timestamp > &   
)
template int32_t generate_series_parallel ( Timestamp  ,
Timestamp  ,
YearMonthTimeInterval  ,
Column< Timestamp > &   
)
int64_t numStepsBetween ( int64_t  start,
int64_t  stop,
int64_t  step 
)

Definition at line 29 of file UtilityTableFunctions.cpp.

Referenced by generate_series__cpu_template(), and generate_series_parallel().

29  {
30  return (stop - start) / step;
31 }

+ Here is the caller graph for this function:

template<typename T >
int64_t numStepsBetween ( Timestamp  start,
Timestamp  stop,
step 
)

Definition at line 34 of file UtilityTableFunctions.cpp.

34  {
35  return step.numStepsBetween(start, stop);
36 }
template int64_t numStepsBetween ( Timestamp  ,
Timestamp  ,
DayTimeInterval   
)
template int64_t numStepsBetween ( Timestamp  ,
Timestamp  ,
YearMonthTimeInterval   
)