SOMHunter Core
async-som.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef asyncsom_h
22 #define asyncsom_h
23 
24 #include <atomic>
25 #include <condition_variable>
26 #include <thread>
27 #include <vector>
28 
29 #include "dataset-features.h"
30 #include "dataset-frames.h"
31 #include "scores.h"
32 
33 namespace sh {
34 class AsyncSom {
35  std::thread worker;
36 
37  size_t _dim{};
38 
39  // worker sync
40  std::condition_variable new_data_wakeup;
41  std::mutex worker_lock;
42 
43  /*
44  * Worker input protocol:
45  *
46  * new_data is set when a new computation is required after
47  * wakeup to process a new dataset. Worker "eats" this flag together
48  * with input data.
49  *
50  * terminate is set when the worker should exit.
51  */
53 
54  // Number of floats in features matrix
55  std::size_t _features_data_len;
56  // Number of floats in scores vector
57  std::size_t _scores_data_len;
58 
59  std::vector<float> points, scores;
60  std::vector<bool> present_mask;
61 
62  /*
63  * Worker output protocol:
64  *
65  * m_ready is set when mapping is filled in AND the
66  * memory is fenced correctly.
67  */
68  bool m_ready;
69  std::vector<std::vector<FrameId>> mapping;
70  std::vector<float> koho;
71 
72  const size_t width;
73  const size_t height;
74 
75  static void async_som_worker(AsyncSom* parent, const Settings& _logger_settings);
76 
77 public:
78  AsyncSom() = delete;
79  ~AsyncSom() noexcept;
80 
81  AsyncSom(AsyncSom&& _logger_settings) = default;
82 
83  AsyncSom(const Settings& _logger_settings, size_t width, size_t height, const PrimaryFrameFeatures& fs,
84  const ScoreModel& sc);
85 
86  void start_work(const PrimaryFrameFeatures& fs, const ScoreModel& sc, const float* scores_orig);
87 
88  std::vector<FrameId> get_display(ScoreModel scores) const;
89 
90  bool map_ready() const {
91  bool t = m_ready;
92  std::atomic_thread_fence(std::memory_order_acquire);
93  return t;
94  }
95  const std::vector<FrameId>& map(size_t i) const { return mapping.at(i); }
96 
97  const float* get_koho(size_t i) const { return koho.data() + i * _dim; }
98 
99  size_t nearest_cluster_with_atleast(const float* vec, const std::vector<size_t>& stolen_count) const {
100  float min = std::numeric_limits<float>::max();
101  size_t res = 0;
102  for (size_t i = 0; i < mapping.size(); ++i) {
103  if (mapping[i].size() > stolen_count[i]) {
104  float tmp = d_sqeucl(koho.data() + _dim * i, vec, _dim);
105  if (min > tmp) {
106  min = tmp;
107  res = i;
108  }
109  }
110  }
111 
112  return res;
113  }
114 };
115 
116 }; // namespace sh
117 
118 #endif
Definition: async-som.h:34
const size_t width
Definition: async-som.h:72
void start_work(const PrimaryFrameFeatures &fs, const ScoreModel &sc, const float *scores_orig)
Definition: async-som.cpp:167
std::condition_variable new_data_wakeup
Definition: async-som.h:40
std::vector< std::vector< FrameId > > mapping
Definition: async-som.h:69
std::vector< float > points
Definition: async-som.h:59
std::vector< FrameId > get_display(ScoreModel scores) const
Definition: async-som.cpp:189
const size_t height
Definition: async-som.h:73
AsyncSom()=delete
std::size_t _features_data_len
Definition: async-som.h:55
std::mutex worker_lock
Definition: async-som.h:41
std::vector< float > scores
Definition: async-som.h:59
bool m_ready
Definition: async-som.h:68
bool new_data
Definition: async-som.h:52
std::thread worker
Definition: async-som.h:35
static void async_som_worker(AsyncSom *parent, const Settings &_logger_settings)
Definition: async-som.cpp:39
std::vector< bool > present_mask
Definition: async-som.h:60
~AsyncSom() noexcept
Definition: async-som.cpp:159
bool map_ready() const
Definition: async-som.h:90
std::size_t _scores_data_len
Definition: async-som.h:57
const float * get_koho(size_t i) const
Definition: async-som.h:97
size_t nearest_cluster_with_atleast(const float *vec, const std::vector< size_t > &stolen_count) const
Definition: async-som.h:99
const std::vector< FrameId > & map(size_t i) const
Definition: async-som.h:95
std::vector< float > koho
Definition: async-som.h:70
bool terminate
Definition: async-som.h:52
size_t _dim
Definition: async-som.h:37
Definition: scores.h:34
Definition: common-types.h:33
unsigned long FrameId
Definition: common-types.h:75
Parsed current config of the core.
Definition: settings.h:190