OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utilities.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmnSci, 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 #pragma once
18 
19 #ifdef HAVE_SYSTEM_TFS
20 #ifndef __CUDACC__
21 
22 #include <filesystem>
23 #include <mutex>
24 #include <shared_mutex>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
30 
31 template <typename T>
32 TEMPLATE_NOINLINE std::pair<T, T> get_column_min_max(const Column<T>& col);
33 
34 template <typename T1, typename T2>
36 distance_in_meters(const T1 fromlon, const T1 fromlat, const T2 tolon, const T2 tolat);
37 
38 inline int64_t x_y_bin_to_bin_index(const int64_t x_bin,
39  const int64_t y_bin,
40  const int64_t num_x_bins) {
41  return y_bin * num_x_bins + x_bin;
42 }
43 
44 inline std::pair<int64_t, int64_t> bin_to_x_y_bin_indexes(const int64_t bin,
45  const int64_t num_x_bins) {
46  return std::make_pair(bin % num_x_bins, bin / num_x_bins);
47 }
48 
49 struct CacheData {
50  int8_t* data_buffer;
51  size_t num_bytes;
52 
53  CacheData(const size_t num_bytes) : num_bytes(num_bytes) {
54  data_buffer = new int8_t[num_bytes];
55  }
56 
57  ~CacheData() { delete[] data_buffer; }
58 };
59 
60 class DataBufferCache {
61  public:
62  bool isKeyCached(const std::string& key) const;
63 
64  bool isKeyCachedAndSameLength(const std::string& key, const size_t num_bytes) const;
65 
66  // Assumes dest_buffer is already appropriately sized
67  template <typename T>
68  void getDataForKey(const std::string& key, T* dest_buffer) const;
69 
70  template <typename T>
71  const T& getDataRefForKey(const std::string& key) const;
72 
73  template <typename T>
74  const T* getDataPtrForKey(const std::string& key) const;
75 
76  template <typename T>
77  void putDataForKey(const std::string& key,
78  T* const data_buffer,
79  const size_t num_elements);
80 
81  private:
82  const size_t parallel_copy_min_bytes{1 << 20};
83 
84  void copyData(int8_t* dest, const int8_t* source, const size_t num_bytes) const;
85 
86  std::unordered_map<std::string, std::shared_ptr<CacheData>> data_cache_;
87  mutable std::shared_mutex cache_mutex_;
88 };
89 
90 template <typename T>
91 class DataCache {
92  public:
93  bool isKeyCached(const std::string& key) const;
94 
95  std::shared_ptr<T> getDataForKey(const std::string& key) const;
96 
97  void putDataForKey(const std::string& key, std::shared_ptr<T> const data);
98 
99  private:
100  std::unordered_map<std::string, std::shared_ptr<T>> data_cache_;
101  mutable std::shared_mutex cache_mutex_;
102 };
103 
104 namespace FileUtilities {
105 std::vector<std::filesystem::path> get_fs_paths(const std::string& file_or_directory);
106 }
107 
108 enum BoundsType { Min, Max };
109 
110 enum IntervalType { Inclusive, Exclusive };
111 
112 template <typename T>
113 bool is_valid_tf_input(const T input,
114  const T bounds_val,
115  const BoundsType bounds_type,
116  const IntervalType interval_type);
117 
118 #include "Utilities.cpp"
119 
120 #endif //__CUDACC__
121 #endif // HAVE_SYSTEM_TFS
#define TEMPLATE_NOINLINE
Definition: OmniSciTypes.h:30
EXTENSION_NOINLINE 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.