OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UtilityTableFunctions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __CUDACC__
18 
19 #include <string>
20 
21 #ifdef HAVE_TBB
22 #include <tbb/parallel_for.h>
23 #include <tbb/task_arena.h>
24 #endif
25 
26 #include "Shared/ThreadInfo.h"
27 #include "UtilityTableFunctions.h"
28 
29 int64_t numStepsBetween(int64_t start, int64_t stop, int64_t step) {
30  return (stop - start) / step;
31 }
32 
33 template <typename T>
34 int64_t numStepsBetween(Timestamp start, Timestamp stop, T step) {
35  return step.numStepsBetween(start, stop);
36 }
37 
38 template <typename T, typename K>
39 int32_t generate_series_parallel(const T start,
40  const T stop,
41  const K step,
42  Column<T>& series_output) {
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 SUCCESS;
55 }
56 
57 template <typename T, typename K>
59  const T start,
60  const T stop,
61  const K step,
62  Column<T>& series_output) {
63  const int64_t PARALLEL_THRESHOLD{10000L};
64  const int64_t num_rows = numStepsBetween(start, stop, step) + 1;
65  if (num_rows <= 0) {
66  mgr.set_output_row_size(0);
67  return 0;
68  }
69  // set_output_row_size ensures that the output buffer size will be
70  // in a reasonable range (up to 16 TiB), and if it is not, an
71  // OutOfHostMemory exception will be thrown.
72  mgr.set_output_row_size(num_rows);
73 
74 #ifdef HAVE_TBB
75  if (num_rows > PARALLEL_THRESHOLD) {
76  return generate_series_parallel(start, stop, step, series_output);
77  }
78 #endif
79 
80  for (int64_t out_idx = 0; out_idx != num_rows; ++out_idx) {
81  series_output[out_idx] = start + (step * out_idx);
82  }
83  return SUCCESS;
84 }
85 
86 template <typename T>
88  const T start,
89  const T stop,
90  Column<T>& series_output) {
92  mgr, start, stop, static_cast<int64_t>(1), series_output);
93 }
94 
95 #include <chrono>
96 #include <random>
97 #include <thread>
98 
99 HOST std::string gen_random_str(std::mt19937& generator, const int64_t str_len) {
100  constexpr char alphanum_lookup_table[] =
101  "0123456789"
102  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
103  "abcdefghijklmnopqrstuvwxyz";
104  constexpr size_t char_mod = sizeof(alphanum_lookup_table) - 1;
105  std::uniform_int_distribution<int32_t> rand_distribution(0, char_mod);
106 
107  std::string tmp_s;
108  tmp_s.reserve(str_len);
109  for (int i = 0; i < str_len; ++i) {
110  tmp_s += alphanum_lookup_table[rand_distribution(generator)];
111  }
112  return tmp_s;
113 }
114 
116 #ifdef _WIN32
117 #pragma comment(linker "/INCLUDE:generate_random_strings__cpu_")
118 #else
119 __attribute__((__used__))
120 #endif
122  const int64_t num_strings,
123  const int64_t string_length,
124  Column<int64_t>& output_id,
125  Column<TextEncodingDict>& output_strings) {
126  auto timer = DEBUG_TIMER(__func__);
127  // Check for out-of-range errors for the input parameters
128  // in the function instead of with require due to issue encountered
129  // with require over multiple variables
130  constexpr int64_t max_strings{10000000L};
131  constexpr int64_t max_str_len{10000L};
132  if (num_strings > max_strings) {
133  return mgr.ERROR_MESSAGE(
134  "generate_random_strings: num_strings must be between 0 and 10,000,000.");
135  }
136  if (string_length > max_str_len) {
137  return mgr.ERROR_MESSAGE(
138  "generate_random_strings: string_length must be between 1 and 10,000.");
139  }
140  if (num_strings == 0L) {
141  // Bail early as there is no work to be done
142  return 0;
143  }
144 
145  mgr.set_output_row_size(num_strings);
146  constexpr int64_t target_strings_per_thread{5000};
147  const ThreadInfo thread_info(
148  std::thread::hardware_concurrency(), num_strings, target_strings_per_thread);
149  std::vector<std::mt19937> per_thread_rand_generators;
150  per_thread_rand_generators.reserve(thread_info.num_threads);
151  for (int64_t thread_idx = 0; thread_idx < thread_info.num_threads; ++thread_idx) {
152  const uint64_t seed = std::chrono::duration_cast<std::chrono::nanoseconds>(
153  std::chrono::system_clock::now().time_since_epoch())
154  .count() +
155  thread_idx * 971;
156  per_thread_rand_generators.emplace_back(seed);
157  }
158  std::vector<std::string> rand_strings(num_strings);
159  tbb::task_arena limited_arena(thread_info.num_threads);
160  limited_arena.execute([&] {
161  CHECK_LE(tbb::this_task_arena::max_concurrency(), thread_info.num_threads);
163  tbb::blocked_range<int64_t>(0, num_strings, thread_info.num_elems_per_thread),
164  [&](const tbb::blocked_range<int64_t>& r) {
165  const int64_t tbb_thread_idx = tbb::this_task_arena::current_thread_index();
166  const int64_t start_out_idx = r.begin();
167  const int64_t end_out_idx = r.end();
168  for (int64_t out_idx = start_out_idx; out_idx != end_out_idx; ++out_idx) {
169  rand_strings[out_idx] =
170  gen_random_str(per_thread_rand_generators[tbb_thread_idx], string_length);
171  }
172  },
173  tbb::simple_partitioner());
174  });
175  const std::vector<int32_t> rand_string_ids =
176  output_strings.string_dict_proxy_->getOrAddTransientBulk(rand_strings);
177  for (int64_t row_idx = 0; row_idx < num_strings; row_idx++) {
178  output_id[row_idx] = row_idx;
179  output_strings[row_idx] = rand_string_ids[row_idx];
180  }
181  return num_strings;
182 }
183 
184 // Explicit template instantiations
185 
186 // wrappers for step calculation
189 
190 // parallel implementations
191 template int32_t generate_series_parallel(int64_t, int64_t, int64_t, Column<int64_t>&);
192 template int32_t generate_series_parallel(Timestamp,
193  Timestamp,
196 template int32_t generate_series_parallel(Timestamp,
197  Timestamp,
200 
201 // non-default step implementations
203  int64_t,
204  int64_t,
205  int64_t,
206  Column<int64_t>&);
208  Timestamp,
209  Timestamp,
213  Timestamp,
214  Timestamp,
217 
218 // default step implementations
220  int64_t,
221  int64_t,
222  Column<int64_t>&);
223 
224 #endif //__CUDACC__
void set_output_row_size(int64_t num_rows)
Definition: heavydbTypes.h:373
int64_t num_elems_per_thread
Definition: ThreadInfo.h:23
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)
#define SUCCESS
Definition: heavydbTypes.h:76
#define HOST
int64_t num_threads
Definition: ThreadInfo.h:22
#define EXTENSION_NOINLINE_HOST
Definition: heavydbTypes.h:55
__attribute__((__used__)) ModelInfo get_model_info_from_file(const std
#define CHECK_LE(x, y)
Definition: Logger.h:304
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)
#define NEVER_INLINE
#define DEBUG_TIMER(name)
Definition: Logger.h:412
HOST std::string gen_random_str(std::mt19937 &generator, const int64_t str_len)
NEVER_INLINE HOST int32_t generate_series__cpu_template(TableFunctionManager &mgr, const T start, const T stop, const K step, Column< T > &series_output)
int32_t generate_series_parallel(const T start, const T stop, const K step, Column< T > &series_output)