OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TableFunctionsCommon.cpp File Reference
#include "TableFunctionsCommon.hpp"
#include <filesystem>
#include <memory>
#include <regex>
#include <string>
#include <tbb/parallel_for.h>
#include <tbb/task_arena.h>
+ Include dependency graph for TableFunctionsCommon.cpp:

Go to the source code of this file.

Namespaces

 FileUtilities
 

Macros

#define NANOSECONDS_PER_SECOND   1000000000
 

Functions

template<typename T >
NEVER_INLINE HOST std::pair< T, T > get_column_min_max (const Column< T > &col)
 
template NEVER_INLINE HOST
std::pair< int8_t, int8_t > 
get_column_min_max (const Column< int8_t > &col)
 
template NEVER_INLINE HOST
std::pair< int16_t, int16_t > 
get_column_min_max (const Column< int16_t > &col)
 
template NEVER_INLINE HOST
std::pair< int32_t, int32_t > 
get_column_min_max (const Column< int32_t > &col)
 
template NEVER_INLINE HOST
std::pair< int64_t, int64_t > 
get_column_min_max (const Column< int64_t > &col)
 
template NEVER_INLINE HOST
std::pair< float, float > 
get_column_min_max (const Column< float > &col)
 
template NEVER_INLINE HOST
std::pair< double, double > 
get_column_min_max (const Column< double > &col)
 
std::pair< int32_t, int32_t > get_column_min_max (const Column< TextEncodingDict > &col)
 
template<typename T >
NEVER_INLINE HOST double get_column_mean (const T *data, const int64_t num_rows)
 
template NEVER_INLINE HOST double get_column_mean (const int8_t *data, const int64_t num_rows)
 
template NEVER_INLINE HOST double get_column_mean (const int16_t *data, const int64_t num_rows)
 
template NEVER_INLINE HOST double get_column_mean (const int32_t *data, const int64_t num_rows)
 
template NEVER_INLINE HOST double get_column_mean (const int64_t *data, const int64_t num_rows)
 
template NEVER_INLINE HOST double get_column_mean (const float *data, const int64_t num_rows)
 
template NEVER_INLINE HOST double get_column_mean (const double *data, const int64_t num_rows)
 
template<typename T >
NEVER_INLINE HOST double get_column_mean (const Column< T > &col)
 
template NEVER_INLINE HOST double get_column_mean (const Column< int8_t > &col)
 
template NEVER_INLINE HOST double get_column_mean (const Column< int16_t > &col)
 
template NEVER_INLINE HOST double get_column_mean (const Column< int32_t > &col)
 
template NEVER_INLINE HOST double get_column_mean (const Column< int64_t > &col)
 
template NEVER_INLINE HOST double get_column_mean (const Column< float > &col)
 
template NEVER_INLINE HOST double get_column_mean (const Column< double > &col)
 
template<typename T >
NEVER_INLINE HOST double get_column_std_dev (const Column< T > &col, const double mean)
 
template NEVER_INLINE HOST double get_column_std_dev (const Column< int32_t > &col, const double mean)
 
template NEVER_INLINE HOST double get_column_std_dev (const Column< int64_t > &col, const double mean)
 
template NEVER_INLINE HOST double get_column_std_dev (const Column< float > &col, const double mean)
 
template NEVER_INLINE HOST double get_column_std_dev (const Column< double > &col, const double mean)
 
template<typename T >
NEVER_INLINE HOST double get_column_std_dev (const T *data, const int64_t num_rows, const double mean)
 
template NEVER_INLINE HOST double get_column_std_dev (const int32_t *data, const int64_t num_rows, const double mean)
 
template NEVER_INLINE HOST double get_column_std_dev (const int64_t *data, const int64_t num_rows, const double mean)
 
template NEVER_INLINE HOST double get_column_std_dev (const float *data, const int64_t num_rows, const double mean)
 
template NEVER_INLINE HOST double get_column_std_dev (const double *data, const int64_t num_rows, const double mean)
 
template<typename T >
NEVER_INLINE HOST std::tuple
< T, T, bool > 
get_column_metadata (const Column< T > &col)
 
template NEVER_INLINE HOST
std::tuple< int8_t, int8_t,
bool > 
get_column_metadata (const Column< int8_t > &col)
 
template NEVER_INLINE HOST
std::tuple< int16_t, int16_t,
bool > 
get_column_metadata (const Column< int16_t > &col)
 
template NEVER_INLINE HOST
std::tuple< int32_t, int32_t,
bool > 
get_column_metadata (const Column< int32_t > &col)
 
template NEVER_INLINE HOST
std::tuple< int64_t, int64_t,
bool > 
get_column_metadata (const Column< int64_t > &col)
 
template NEVER_INLINE HOST
std::tuple< float, float, bool > 
get_column_metadata (const Column< float > &col)
 
template NEVER_INLINE HOST
std::tuple< double, double,
bool > 
get_column_metadata (const Column< double > &col)
 
std::tuple< int32_t, int32_t,
bool > 
get_column_metadata (const Column< TextEncodingDict > &col)
 
template<typename T >
void z_std_normalize_col (const T *input_data, T *output_data, const int64_t num_rows, const double mean, const double std_dev)
 
template void z_std_normalize_col (const float *input_data, float *output_data, const int64_t num_rows, const double mean, const double std_dev)
 
template void z_std_normalize_col (const double *input_data, double *output_data, const int64_t num_rows, const double mean, const double std_dev)
 
template<typename T >
std::vector< std::vector< T > > z_std_normalize_data (const std::vector< T * > &input_data, const int64_t num_rows)
 
template std::vector
< std::vector< float > > 
z_std_normalize_data (const std::vector< float * > &input_data, const int64_t num_rows)
 
template std::vector
< std::vector< double > > 
z_std_normalize_data (const std::vector< double * > &input_data, const int64_t num_rows)
 
template<typename T1 , typename T2 >
NEVER_INLINE HOST T1 distance_in_meters (const T1 fromlon, const T1 fromlat, const T2 tolon, const T2 tolat)
 
template NEVER_INLINE HOST float distance_in_meters (const float fromlon, const float fromlat, const float tolon, const float tolat)
 
template NEVER_INLINE HOST float distance_in_meters (const float fromlon, const float fromlat, const double tolon, const double tolat)
 
template NEVER_INLINE HOST double distance_in_meters (const double fromlon, const double fromlat, const float tolon, const float tolat)
 
template NEVER_INLINE HOST double distance_in_meters (const double fromlon, const double fromlat, const double tolon, const double tolat)
 Computes the distance, in meters, between two WGS-84 positions. More...
 
std::regex FileUtilities::glob_to_regex (const std::string &glob, bool case_sensitive=false)
 
std::vector
< std::filesystem::path > 
FileUtilities::get_fs_paths (const std::string &file_or_directory)
 
template<typename T >
NEVER_INLINE HOST bool is_valid_tf_input (const T input, const T bounds_val, const BoundsType bounds_type, const IntervalType interval_type)
 
template NEVER_INLINE HOST bool is_valid_tf_input (const int32_t input, const int32_t bounds_val, const BoundsType bounds_type, const IntervalType interval_type)
 
template NEVER_INLINE HOST bool is_valid_tf_input (const int64_t input, const int64_t bounds_val, const BoundsType bounds_type, const IntervalType interval_type)
 
template NEVER_INLINE HOST bool is_valid_tf_input (const float input, const float bounds_val, const BoundsType bounds_type, const IntervalType interval_type)
 
template NEVER_INLINE HOST bool is_valid_tf_input (const double input, const double bounds_val, const BoundsType bounds_type, const IntervalType interval_type)
 

Macro Definition Documentation

#define NANOSECONDS_PER_SECOND   1000000000

Definition at line 29 of file TableFunctionsCommon.cpp.

Function Documentation

template<typename T1 , typename T2 >
NEVER_INLINE HOST T1 distance_in_meters ( const T1  fromlon,
const T1  fromlat,
const T2  tolon,
const T2  tolat 
)

Definition at line 400 of file TableFunctionsCommon.cpp.

400  {
401  T1 latitudeArc = (fromlat - tolat) * 0.017453292519943295769236907684886;
402  T1 longitudeArc = (fromlon - tolon) * 0.017453292519943295769236907684886;
403  T1 latitudeH = sin(latitudeArc * 0.5);
404  latitudeH *= latitudeH;
405  T1 lontitudeH = sin(longitudeArc * 0.5);
406  lontitudeH *= lontitudeH;
407  T1 tmp = cos(fromlat * 0.017453292519943295769236907684886) *
408  cos(tolat * 0.017453292519943295769236907684886);
409  return 6372797.560856 * (2.0 * asin(sqrt(latitudeH + tmp * lontitudeH)));
410 }
template NEVER_INLINE HOST float distance_in_meters ( const float  fromlon,
const float  fromlat,
const float  tolon,
const float  tolat 
)
template NEVER_INLINE HOST float distance_in_meters ( const float  fromlon,
const float  fromlat,
const double  tolon,
const double  tolat 
)
template NEVER_INLINE HOST double distance_in_meters ( const double  fromlon,
const double  fromlat,
const float  tolon,
const float  tolat 
)
template NEVER_INLINE HOST double distance_in_meters ( const double  fromlon,
const double  fromlat,
const double  tolon,
const double  tolat 
)

Computes the distance, in meters, between two WGS-84 positions.

The result is equal to EARTH_RADIUS_IN_METERS*ArcInRadians(from,to)

ArcInRadians is equal to Distance(from,to)/EARTH_RADIUS_IN_METERS = 2*asin(sqrt(h(d/EARTH_RADIUS_IN_METERS )))

where:

  • d is the distance in meters between 'from' and 'to' positions.
  • h is the haversine function: h(x)=sinĀ²(x/2)

code attribution: http://blog.julien.cayzac.name/2008/10/arc-and-distance-between-two-points-on.html

The haversine formula gives: h(d/R) = h(from.lat-to.lat)+h(from.lon-to.lon)+cos(from.lat)*cos(to.lat)

See Also
http://en.wikipedia.org/wiki/Law_of_haversines

Definition at line 423 of file ExtensionFunctions.hpp.

Referenced by GeoRaster< T, Z >::calculate_bins_and_scales(), length_linestring(), ST_Distance_Point_LineString_Geodesic(), and ST_Distance_Point_Point_Geodesic().

426  {
427  double latitudeArc = (fromlat - tolat) * 0.017453292519943295769236907684886;
428  double longitudeArc = (fromlon - tolon) * 0.017453292519943295769236907684886;
429  double latitudeH = sin(latitudeArc * 0.5);
430  latitudeH *= latitudeH;
431  double lontitudeH = sin(longitudeArc * 0.5);
432  lontitudeH *= lontitudeH;
433  double tmp = cos(fromlat * 0.017453292519943295769236907684886) *
434  cos(tolat * 0.017453292519943295769236907684886);
435  return 6372797.560856 * (2.0 * asin(sqrt(latitudeH + tmp * lontitudeH)));
436 }

+ Here is the caller graph for this function:

template<typename T >
NEVER_INLINE HOST double get_column_mean ( const T *  data,
const int64_t  num_rows 
)

Definition at line 110 of file TableFunctionsCommon.cpp.

References max_inputs_per_thread, threading_serial::parallel_for(), and heavydb.dtypes::T.

Referenced by get_column_mean(), and z_std_normalize_data().

110  {
111  // const int64_t num_rows = col.size();
112  const size_t max_thread_count = std::thread::hardware_concurrency();
113  const size_t max_inputs_per_thread = 20000;
114  const size_t num_threads = std::min(
115  max_thread_count, ((num_rows + max_inputs_per_thread - 1) / max_inputs_per_thread));
116 
117  std::vector<double> local_col_sums(num_threads, 0.);
118  std::vector<int64_t> local_col_non_null_counts(num_threads, 0L);
119  tbb::task_arena limited_arena(num_threads);
120  limited_arena.execute([&] {
121  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_rows),
122  [&](const tbb::blocked_range<int64_t>& r) {
123  const int64_t start_idx = r.begin();
124  const int64_t end_idx = r.end();
125  double local_col_sum = 0.;
126  int64_t local_col_non_null_count = 0;
127  for (int64_t r = start_idx; r < end_idx; ++r) {
128  const T val = data[r];
129  if (val == inline_null_value<T>()) {
130  continue;
131  }
132  local_col_sum += data[r];
133  local_col_non_null_count++;
134  }
135  size_t thread_idx = tbb::this_task_arena::current_thread_index();
136  local_col_sums[thread_idx] += local_col_sum;
137  local_col_non_null_counts[thread_idx] += local_col_non_null_count;
138  });
139  });
140 
141  double col_sum = 0.0;
142  int64_t col_non_null_count = 0L;
143 
144  for (size_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
145  col_sum += local_col_sums[thread_idx];
146  col_non_null_count += local_col_non_null_counts[thread_idx];
147  }
148 
149  return col_non_null_count == 0 ? 0 : col_sum / col_non_null_count;
150 }
const size_t max_inputs_per_thread
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template NEVER_INLINE HOST double get_column_mean ( const int8_t *  data,
const int64_t  num_rows 
)
template NEVER_INLINE HOST double get_column_mean ( const int16_t *  data,
const int64_t  num_rows 
)
template NEVER_INLINE HOST double get_column_mean ( const int32_t *  data,
const int64_t  num_rows 
)
template NEVER_INLINE HOST double get_column_mean ( const int64_t *  data,
const int64_t  num_rows 
)
template NEVER_INLINE HOST double get_column_mean ( const float *  data,
const int64_t  num_rows 
)
template NEVER_INLINE HOST double get_column_mean ( const double *  data,
const int64_t  num_rows 
)
template<typename T >
NEVER_INLINE HOST double get_column_mean ( const Column< T > &  col)

Definition at line 171 of file TableFunctionsCommon.cpp.

References get_column_mean(), Column< T >::getPtr(), and Column< T >::size().

171  {
172  return get_column_mean(col.getPtr(), col.size());
173 }
DEVICE int64_t size() const
Definition: heavydbTypes.h:726
DEVICE T * getPtr() const
Definition: heavydbTypes.h:725
NEVER_INLINE HOST double get_column_mean(const T *data, const int64_t num_rows)

+ Here is the call graph for this function:

template NEVER_INLINE HOST double get_column_mean ( const Column< int8_t > &  col)
template NEVER_INLINE HOST double get_column_mean ( const Column< int16_t > &  col)
template NEVER_INLINE HOST double get_column_mean ( const Column< int32_t > &  col)
template NEVER_INLINE HOST double get_column_mean ( const Column< int64_t > &  col)
template NEVER_INLINE HOST double get_column_mean ( const Column< float > &  col)
template NEVER_INLINE HOST double get_column_mean ( const Column< double > &  col)
template<typename T >
NEVER_INLINE HOST std::tuple<T, T, bool> get_column_metadata ( const Column< T > &  col)

Definition at line 259 of file TableFunctionsCommon.cpp.

References Column< T >::isNull(), max_inputs_per_thread, threading_serial::parallel_for(), Column< T >::size(), and heavydb.dtypes::T.

Referenced by get_column_metadata().

259  {
260  T col_min = std::numeric_limits<T>::max();
261  T col_max = std::numeric_limits<T>::lowest();
262  bool has_nulls = false;
263  const int64_t num_rows = col.size();
264  const size_t max_thread_count = std::thread::hardware_concurrency();
265  const size_t max_inputs_per_thread = 200000;
266  const size_t num_threads = std::min(
267  max_thread_count, ((num_rows + max_inputs_per_thread - 1) / max_inputs_per_thread));
268 
269  std::vector<T> local_col_mins(num_threads, std::numeric_limits<T>::max());
270  std::vector<T> local_col_maxes(num_threads, std::numeric_limits<T>::lowest());
271  std::vector<bool> local_col_has_nulls(num_threads, false);
272  tbb::task_arena limited_arena(num_threads);
273 
274  limited_arena.execute([&] {
275  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_rows),
276  [&](const tbb::blocked_range<int64_t>& r) {
277  const int64_t start_idx = r.begin();
278  const int64_t end_idx = r.end();
279  T local_col_min = std::numeric_limits<T>::max();
280  T local_col_max = std::numeric_limits<T>::lowest();
281  bool local_has_nulls = false;
282  for (int64_t r = start_idx; r < end_idx; ++r) {
283  if (col.isNull(r)) {
284  local_has_nulls = true;
285  continue;
286  }
287  if (col[r] < local_col_min) {
288  local_col_min = col[r];
289  }
290  if (col[r] > local_col_max) {
291  local_col_max = col[r];
292  }
293  }
294  const size_t thread_idx =
295  tbb::this_task_arena::current_thread_index();
296  if (local_has_nulls) {
297  local_col_has_nulls[thread_idx] = true;
298  }
299  if (local_col_min < local_col_mins[thread_idx]) {
300  local_col_mins[thread_idx] = local_col_min;
301  }
302  if (local_col_max > local_col_maxes[thread_idx]) {
303  local_col_maxes[thread_idx] = local_col_max;
304  }
305  });
306  });
307 
308  for (size_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
309  if (local_col_has_nulls[thread_idx]) {
310  has_nulls = true;
311  }
312  if (local_col_mins[thread_idx] < col_min) {
313  col_min = local_col_mins[thread_idx];
314  }
315  if (local_col_maxes[thread_idx] > col_max) {
316  col_max = local_col_maxes[thread_idx];
317  }
318  }
319  return {col_min, col_max, has_nulls};
320 }
DEVICE int64_t size() const
Definition: heavydbTypes.h:726
const size_t max_inputs_per_thread
DEVICE bool isNull(int64_t index) const
Definition: heavydbTypes.h:729
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template NEVER_INLINE HOST std::tuple<int8_t, int8_t, bool> get_column_metadata ( const Column< int8_t > &  col)
template NEVER_INLINE HOST std::tuple<int16_t, int16_t, bool> get_column_metadata ( const Column< int16_t > &  col)
template NEVER_INLINE HOST std::tuple<int32_t, int32_t, bool> get_column_metadata ( const Column< int32_t > &  col)
template NEVER_INLINE HOST std::tuple<int64_t, int64_t, bool> get_column_metadata ( const Column< int64_t > &  col)
template NEVER_INLINE HOST std::tuple<float, float, bool> get_column_metadata ( const Column< float > &  col)
template NEVER_INLINE HOST std::tuple<double, double, bool> get_column_metadata ( const Column< double > &  col)
std::tuple<int32_t, int32_t, bool> get_column_metadata ( const Column< TextEncodingDict > &  col)

Definition at line 335 of file TableFunctionsCommon.cpp.

References get_column_metadata(), Column< TextEncodingDict >::getPtr(), and Column< TextEncodingDict >::size().

336  {
337  Column<int32_t> int_alias_col(reinterpret_cast<int32_t*>(col.getPtr()), col.size());
338  return get_column_metadata(int_alias_col);
339 }
DEVICE TextEncodingDict * getPtr() const
Definition: heavydbTypes.h:915
NEVER_INLINE HOST std::tuple< T, T, bool > get_column_metadata(const Column< T > &col)
DEVICE int64_t size() const
Definition: heavydbTypes.h:916

+ Here is the call graph for this function:

template<typename T >
NEVER_INLINE HOST std::pair<T, T> get_column_min_max ( const Column< T > &  col)

Definition at line 32 of file TableFunctionsCommon.cpp.

References max_inputs_per_thread, threading_serial::parallel_for(), Column< T >::size(), and heavydb.dtypes::T.

Referenced by ct_union_pushdown_stats__cpu_template(), GeoRaster< T, Z >::GeoRaster(), get_column_min_max(), get_min_or_max(), and get_min_or_max_union().

32  {
33  T col_min = std::numeric_limits<T>::max();
34  T col_max = std::numeric_limits<T>::lowest();
35  const int64_t num_rows = col.size();
36  const size_t max_thread_count = std::thread::hardware_concurrency();
37  const size_t max_inputs_per_thread = 20000;
38  const size_t num_threads = std::min(
39  max_thread_count, ((num_rows + max_inputs_per_thread - 1) / max_inputs_per_thread));
40 
41  std::vector<T> local_col_mins(num_threads, std::numeric_limits<T>::max());
42  std::vector<T> local_col_maxes(num_threads, std::numeric_limits<T>::lowest());
43  tbb::task_arena limited_arena(num_threads);
44 
45  limited_arena.execute([&] {
46  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_rows),
47  [&](const tbb::blocked_range<int64_t>& r) {
48  const int64_t start_idx = r.begin();
49  const int64_t end_idx = r.end();
50  T local_col_min = std::numeric_limits<T>::max();
51  T local_col_max = std::numeric_limits<T>::lowest();
52  for (int64_t r = start_idx; r < end_idx; ++r) {
53  const T val = col[r];
54  if (val == inline_null_value<T>()) {
55  continue;
56  }
57  if (val < local_col_min) {
58  local_col_min = val;
59  }
60  if (val > local_col_max) {
61  local_col_max = val;
62  }
63  }
64  size_t thread_idx = tbb::this_task_arena::current_thread_index();
65  if (local_col_min < local_col_mins[thread_idx]) {
66  local_col_mins[thread_idx] = local_col_min;
67  }
68  if (local_col_max > local_col_maxes[thread_idx]) {
69  local_col_maxes[thread_idx] = local_col_max;
70  }
71  });
72  });
73 
74  for (size_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
75  if (local_col_mins[thread_idx] < col_min) {
76  col_min = local_col_mins[thread_idx];
77  }
78  if (local_col_maxes[thread_idx] > col_max) {
79  col_max = local_col_maxes[thread_idx];
80  }
81  }
82  return std::make_pair(col_min, col_max);
83 }
DEVICE int64_t size() const
Definition: heavydbTypes.h:726
const size_t max_inputs_per_thread
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template NEVER_INLINE HOST std::pair<int8_t, int8_t> get_column_min_max ( const Column< int8_t > &  col)
template NEVER_INLINE HOST std::pair<int16_t, int16_t> get_column_min_max ( const Column< int16_t > &  col)
template NEVER_INLINE HOST std::pair<int32_t, int32_t> get_column_min_max ( const Column< int32_t > &  col)
template NEVER_INLINE HOST std::pair<int64_t, int64_t> get_column_min_max ( const Column< int64_t > &  col)
template NEVER_INLINE HOST std::pair<float, float> get_column_min_max ( const Column< float > &  col)
template NEVER_INLINE HOST std::pair<double, double> get_column_min_max ( const Column< double > &  col)
std::pair<int32_t, int32_t> get_column_min_max ( const Column< TextEncodingDict > &  col)

Definition at line 98 of file TableFunctionsCommon.cpp.

References get_column_min_max(), Column< TextEncodingDict >::getPtr(), and Column< TextEncodingDict >::size().

98  {
99  Column<int32_t> int_alias_col(reinterpret_cast<int32_t*>(col.getPtr()), col.size());
100  return get_column_min_max(int_alias_col);
101 }
NEVER_INLINE HOST std::pair< T, T > get_column_min_max(const Column< T > &col)
DEVICE TextEncodingDict * getPtr() const
Definition: heavydbTypes.h:915
DEVICE int64_t size() const
Definition: heavydbTypes.h:916

+ Here is the call graph for this function:

template<typename T >
NEVER_INLINE HOST double get_column_std_dev ( const Column< T > &  col,
const double  mean 
)

Definition at line 183 of file TableFunctionsCommon.cpp.

References get_column_std_dev(), Column< T >::getPtr(), and Column< T >::size().

Referenced by get_column_std_dev(), and z_std_normalize_data().

183  {
184  return get_column_std_dev(col.getPtr(), col.size(), mean);
185 }
DEVICE int64_t size() const
Definition: heavydbTypes.h:726
DEVICE T * getPtr() const
Definition: heavydbTypes.h:725
NEVER_INLINE HOST double get_column_std_dev(const Column< T > &col, const double mean)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template NEVER_INLINE HOST double get_column_std_dev ( const Column< int32_t > &  col,
const double  mean 
)
template NEVER_INLINE HOST double get_column_std_dev ( const Column< int64_t > &  col,
const double  mean 
)
template NEVER_INLINE HOST double get_column_std_dev ( const Column< float > &  col,
const double  mean 
)
template NEVER_INLINE HOST double get_column_std_dev ( const Column< double > &  col,
const double  mean 
)
template<typename T >
NEVER_INLINE HOST double get_column_std_dev ( const T *  data,
const int64_t  num_rows,
const double  mean 
)

Definition at line 197 of file TableFunctionsCommon.cpp.

References max_inputs_per_thread, threading_serial::parallel_for(), and heavydb.dtypes::T.

199  {
200  // const int64_t num_rows = col.size();
201  const size_t max_thread_count = std::thread::hardware_concurrency();
202  const size_t max_inputs_per_thread = 200000;
203  const size_t num_threads = std::min(
204  max_thread_count, ((num_rows + max_inputs_per_thread - 1) / max_inputs_per_thread));
205 
206  std::vector<double> local_col_squared_residuals(num_threads, 0.);
207  std::vector<int64_t> local_col_non_null_counts(num_threads, 0L);
208  tbb::task_arena limited_arena(num_threads);
209 
210  limited_arena.execute([&] {
211  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_rows),
212  [&](const tbb::blocked_range<int64_t>& r) {
213  const int64_t start_idx = r.begin();
214  const int64_t end_idx = r.end();
215  double local_col_squared_residual = 0.;
216  int64_t local_col_non_null_count = 0;
217  for (int64_t r = start_idx; r < end_idx; ++r) {
218  const T val = data[r];
219  if (val == inline_null_value<T>()) {
220  continue;
221  }
222  const double residual = val - mean;
223  local_col_squared_residual += (residual * residual);
224  local_col_non_null_count++;
225  }
226  size_t thread_idx = tbb::this_task_arena::current_thread_index();
227  local_col_squared_residuals[thread_idx] +=
228  local_col_squared_residual;
229  local_col_non_null_counts[thread_idx] += local_col_non_null_count;
230  });
231  });
232 
233  double col_sum_squared_residual = 0.0;
234  int64_t col_non_null_count = 0;
235 
236  for (size_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
237  col_sum_squared_residual += local_col_squared_residuals[thread_idx];
238  col_non_null_count += local_col_non_null_counts[thread_idx];
239  }
240 
241  return col_non_null_count == 0 ? 0
242  : sqrt(col_sum_squared_residual / col_non_null_count);
243 }
const size_t max_inputs_per_thread
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())

+ Here is the call graph for this function:

template NEVER_INLINE HOST double get_column_std_dev ( const int32_t *  data,
const int64_t  num_rows,
const double  mean 
)
template NEVER_INLINE HOST double get_column_std_dev ( const int64_t *  data,
const int64_t  num_rows,
const double  mean 
)
template NEVER_INLINE HOST double get_column_std_dev ( const float *  data,
const int64_t  num_rows,
const double  mean 
)
template NEVER_INLINE HOST double get_column_std_dev ( const double *  data,
const int64_t  num_rows,
const double  mean 
)
template<typename T >
NEVER_INLINE HOST bool is_valid_tf_input ( const T  input,
const T  bounds_val,
const BoundsType  bounds_type,
const IntervalType  interval_type 
)

Definition at line 504 of file TableFunctionsCommon.cpp.

References Exclusive, Inclusive, Max, Min, and UNREACHABLE.

507  {
508  switch (bounds_type) {
509  case BoundsType::Min:
510  switch (interval_type) {
512  return input >= bounds_val;
514  return input > bounds_val;
515  default:
516  UNREACHABLE();
517  }
518  case BoundsType::Max:
519  switch (interval_type) {
521  return input <= bounds_val;
523  return input < bounds_val;
524  default:
525  UNREACHABLE();
526  }
527  break;
528  default:
529  UNREACHABLE();
530  }
531  UNREACHABLE();
532  return false; // To address compiler warning
533 }
#define UNREACHABLE()
Definition: Logger.h:333
template NEVER_INLINE HOST bool is_valid_tf_input ( const int32_t  input,
const int32_t  bounds_val,
const BoundsType  bounds_type,
const IntervalType  interval_type 
)
template NEVER_INLINE HOST bool is_valid_tf_input ( const int64_t  input,
const int64_t  bounds_val,
const BoundsType  bounds_type,
const IntervalType  interval_type 
)
template NEVER_INLINE HOST bool is_valid_tf_input ( const float  input,
const float  bounds_val,
const BoundsType  bounds_type,
const IntervalType  interval_type 
)
template NEVER_INLINE HOST bool is_valid_tf_input ( const double  input,
const double  bounds_val,
const BoundsType  bounds_type,
const IntervalType  interval_type 
)
template<typename T >
void z_std_normalize_col ( const T *  input_data,
T *  output_data,
const int64_t  num_rows,
const double  mean,
const double  std_dev 
)

Definition at line 342 of file TableFunctionsCommon.cpp.

References threading_serial::parallel_for().

Referenced by z_std_normalize_data().

346  {
347  if (std_dev <= 0.0) {
348  throw std::runtime_error("Standard deviation cannot be <= 0");
349  }
350  const double inv_std_dev = 1.0 / std_dev;
351 
352  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_rows),
353  [&](const tbb::blocked_range<int64_t>& r) {
354  const int64_t start_idx = r.begin();
355  const int64_t end_idx = r.end();
356  for (int64_t row_idx = start_idx; row_idx < end_idx; ++row_idx) {
357  output_data[row_idx] = (input_data[row_idx] - mean) * inv_std_dev;
358  }
359  });
360 }
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template void z_std_normalize_col ( const float *  input_data,
float *  output_data,
const int64_t  num_rows,
const double  mean,
const double  std_dev 
)
template void z_std_normalize_col ( const double *  input_data,
double *  output_data,
const int64_t  num_rows,
const double  mean,
const double  std_dev 
)
template<typename T >
std::vector<std::vector<T> > z_std_normalize_data ( const std::vector< T * > &  input_data,
const int64_t  num_rows 
)

Definition at line 374 of file TableFunctionsCommon.cpp.

References get_column_mean(), get_column_std_dev(), and z_std_normalize_col().

Referenced by dbscan__cpu_template(), and kmeans__cpu_template().

375  {
376  const int64_t num_features = input_data.size();
377  std::vector<std::vector<T>> normalized_data(num_features);
378  for (int64_t feature_idx = 0; feature_idx < num_features; ++feature_idx) {
379  const auto mean = get_column_mean(input_data[feature_idx], num_rows);
380  const auto std_dev = get_column_std_dev(input_data[feature_idx], num_rows, mean);
381  normalized_data[feature_idx].resize(num_rows);
382  z_std_normalize_col(input_data[feature_idx],
383  normalized_data[feature_idx].data(),
384  num_rows,
385  mean,
386  std_dev);
387  }
388  return normalized_data;
389 }
void z_std_normalize_col(const T *input_data, T *output_data, const int64_t num_rows, const double mean, const double std_dev)
NEVER_INLINE HOST double get_column_std_dev(const Column< T > &col, const double mean)
NEVER_INLINE HOST double get_column_mean(const T *data, const int64_t num_rows)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template std::vector<std::vector<float> > z_std_normalize_data ( const std::vector< float * > &  input_data,
const int64_t  num_rows 
)
template std::vector<std::vector<double> > z_std_normalize_data ( const std::vector< double * > &  input_data,
const int64_t  num_rows 
)