OmniSciDB  16c4e035a1
 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 2021 OmniSci, 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 #endif
24 
25 #include "UtilityTableFunctions.h"
26 
28 #ifdef _WIN32
29 #pragma comment(linker "/INCLUDE:generate_series_parallel")
30 #else
31 __attribute__((__used__))
32 #endif
33 int32_t generate_series_parallel(const int64_t start,
34  const int64_t stop,
35  const int64_t step,
36  Column<int64_t>& series_output) {
37  const int64_t num_rows = ((stop - start) / step) + 1;
38 
39  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_rows),
40  [&](const tbb::blocked_range<int64_t>& r) {
41  const int64_t start_out_idx = r.begin();
42  const int64_t end_out_idx = r.end();
43  for (int64_t out_idx = start_out_idx; out_idx != end_out_idx;
44  ++out_idx) {
45  series_output[out_idx] = start + out_idx * step;
46  }
47  });
48  return num_rows;
49 }
50 
52 #ifdef _WIN32
53 #pragma comment(linker "/INCLUDE:generate_series__cpu_1")
54 #else
55 __attribute__((__used__))
56 #endif
58  const int64_t start,
59  const int64_t stop,
60  const int64_t step,
61  Column<int64_t>& series_output) {
62  const int64_t MAX_ROWS{1L << 30};
63  const int64_t PARALLEL_THRESHOLD{10000L};
64  const int64_t num_rows = ((stop - start) / step) + 1;
65  if (num_rows <= 0) {
66  mgr.set_output_row_size(0);
67  return 0;
68  }
69  mgr.set_output_row_size(num_rows);
70 
71  if (num_rows > MAX_ROWS) {
72  return mgr.ERROR_MESSAGE(
73  "Invocation of generate_series would result in " + std::to_string(num_rows) +
74  " rows, which exceeds the max limit of " + std::to_string(MAX_ROWS) + " rows.");
75  }
76 
77 #ifdef HAVE_TBB
78  if (num_rows > PARALLEL_THRESHOLD) {
79  return generate_series_parallel(start, stop, step, series_output);
80  }
81 #endif
82 
83  for (int64_t out_idx = 0; out_idx != num_rows; ++out_idx) {
84  series_output[out_idx] = start + out_idx * step;
85  }
86  return num_rows;
87 }
88 
90 #ifdef _WIN32
91 #pragma comment(linker "/INCLUDE:generate_series__cpu_2")
92 #else
93 __attribute__((__used__))
94 #endif
96  const int64_t start,
97  const int64_t stop,
98  Column<int64_t>& series_output) {
99  return generate_series__cpu_1(mgr, start, stop, 1, series_output);
100 }
101 
102 #endif //__CUDACC__
void set_output_row_size(int64_t num_rows)
Definition: OmniSciTypes.h:329
EXTENSION_NOINLINE int32_t generate_series_parallel(const int64_t start, const int64_t stop, const int64_t step, Column< int64_t > &series_output)
std::string to_string(char const *&&v)
#define EXTENSION_NOINLINE
Definition: OmniSciTypes.h:34
FORCE_INLINE T __attribute__((__may_alias__))*may_alias_ptr(T *ptr)
Definition: TypePunning.h:32
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
EXTENSION_NOINLINE int32_t generate_series__cpu_1(TableFunctionManager &mgr, const int64_t start, const int64_t stop, const int64_t step, Column< int64_t > &series_output)
EXTENSION_NOINLINE int32_t generate_series__cpu_2(TableFunctionManager &mgr, const int64_t start, const int64_t stop, Column< int64_t > &series_output)