RTC Toolkit  2.0.0
computation.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_EXAMPLEDATATASK_GPU_COMPUTATION_HPP
13 #define RTCTK_EXAMPLEDATATASK_GPU_COMPUTATION_HPP
14 
20 
21 #include <chrono>
22 #include <numeric>
23 #include <string>
24 #include <vector>
25 
26 using namespace rtctk::componentFramework;
27 
28 class Computation {
29 public:
31 
33  m_last_sample_id = 0;
34  m_sample_idx = 0;
35  m_iteration_idx = 0;
36  }
37 
38  void SetStaticConfig(unsigned to_read, unsigned n_slopes, unsigned n_modes, unsigned gpu) {
39  // read anything that is unlikely to change.
40  m_n_samples_to_read = to_read;
41  m_n_slopes = n_slopes;
42  m_n_modes = n_modes;
43 
44  // set vectors and matrix to correct size.
45  m_s2m_matrix.resize(m_n_modes, m_n_slopes);
46 
47  // some simple debug output
48  LOG4CPLUS_DEBUG(GetLogger(), "m_n_slopes: " << m_n_slopes);
49  LOG4CPLUS_DEBUG(GetLogger(), "m_n_modes: " << m_n_modes);
50  LOG4CPLUS_DEBUG(GetLogger(), "m_n_samples_to_read: " << m_n_samples_to_read);
51  LOG4CPLUS_DEBUG(GetLogger(), "m_gpu: " << gpu);
52 
53  // call the constructor for the
54  m_gpu_comp = std::make_unique<GpuLib>(m_n_slopes, m_n_modes, gpu);
55 
56  // set avg array to zero
57  m_gpu_comp->ResetAvgSlopes();
58  }
59 
61  m_s2m_matrix = std::move(s2m_matrix);
62 
63  if (m_n_modes != m_s2m_matrix.get_nrows() || m_n_slopes != m_s2m_matrix.get_ncols()) {
64  std::stringstream err_text;
65  err_text << "s2m wrong shape: ";
66  err_text << " "
67  << "expected : " << m_n_modes << " x " << m_n_slopes;
68  err_text << " "
69  << "received : " << m_s2m_matrix.get_nrows() << " x "
70  << m_s2m_matrix.get_ncols();
72  CII_THROW(RtctkException, err_text.str());
73  }
74 
75  // upload to GPU
76  m_gpu_comp->SetMatrix(m_s2m_matrix.data());
77  }
78 
79  void Reset() {
80  // This is invoked to clean up in case of stopped during buffering of data.
81  m_last_sample_id = 0;
82  m_sample_idx = 0;
83  m_iteration_idx = 0;
84  m_gpu_comp->ResetAvgSlopes();
85  }
86 
87  void ThreadInit() {
88  m_gpu_comp->InitReaderThread();
89  }
90 
91  void OnDataAvailable(TopicType const& sample) {
92  m_last_sample_id = sample.sample_id;
93  m_gpu_comp->NewSample(sample.wfs.slopes.data(), m_sample_idx);
94  m_sample_idx++;
95  }
96 
97  struct Result {
98  std::vector<float> const& avg_slopes;
99  std::vector<float> const& avg_modes;
100 
101  struct {
102  unsigned last_sample_id;
103  unsigned iteration_idx;
104  std::chrono::duration<double> elapsed;
105  } stats;
106  };
107 
111 
112  if (m_sample_idx != m_n_samples_to_read) {
113  CII_THROW(RtctkException, "not all data available");
114  }
115 
116  LOG4CPLUS_INFO(GetLogger(), "Computation::Compute() - samples: " << m_sample_idx);
117 
118  auto time_start = std::chrono::system_clock::now();
119 
120  m_gpu_comp->Compute();
121 
122  auto elapsed = std::chrono::system_clock::now() - time_start;
123 
124  m_avg_slopes = std::move(m_gpu_comp->GetAvgSlopes());
125  m_avg_modes = std::move(m_gpu_comp->GetResults());
126 
127  m_iteration_idx++;
128  m_sample_idx = 0;
129 
130  m_gpu_comp->ResetAvgSlopes();
131 
132  return {m_avg_slopes, m_avg_modes, m_last_sample_id, m_iteration_idx, elapsed};
133  }
134 
135 private:
136  unsigned m_last_sample_id;
137  unsigned m_iteration_idx;
138  unsigned m_sample_idx;
139 
140  unsigned m_n_slopes;
141  unsigned m_n_modes;
142  unsigned m_n_samples_to_read;
143 
144  std::vector<float> m_avg_slopes;
145  std::vector<float> m_avg_modes;
146  MatrixBuffer<float> m_s2m_matrix;
147 
148  std::unique_ptr<GpuLib> m_gpu_comp;
149 };
150 
151 #endif // RTCTK_EXAMPLEDATATASK_GPU_COMPUTATION_HPP
Computation::Reset
void Reset()
Definition: computation.hpp:79
rtctk::exampleTopic::ScaoLoopTopic::wfs
WfsLoopBaseTopic< N_SUBAPS > wfs
Definition: topics.hpp:30
exceptions.hpp
Provides macros and utilities for exception handling.
Computation::Result::avg_modes
std::vector< float > const & avg_modes
Definition: computation.hpp:99
rtctk::componentFramework
Definition: commandReplier.cpp:20
Computation::Result::last_sample_id
unsigned last_sample_id
Definition: computation.hpp:102
Computation::SetDynamicConfig
void SetDynamicConfig(MatrixBuffer< float > &&s2m_matrix)
Definition: computation.hpp:60
matrixBuffer.hpp
Declaration of the MatrixBuffer template class used in APIs.
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
Computation::Result
Definition: computation.hpp:97
rtctk::exampleTopic::WfsLoopBaseTopic::slopes
std::array< float, 2 *NSUBAPS > slopes
Definition: topics.hpp:24
Computation::Result::iteration_idx
unsigned iteration_idx
Definition: computation.hpp:103
Computation::Result::avg_slopes
std::vector< float > const & avg_slopes
Definition: computation.hpp:98
Computation::ThreadInit
void ThreadInit()
Definition: computation.hpp:87
rtctk::exampleTopic::ScaoLoopTopic::sample_id
uint64_t sample_id
Definition: topics.hpp:29
rtctk::exampleTopic::ScaoLoopTopic
Definition: topics.hpp:28
gpuLib.hpp
GPU library for computation.
Computation::OnDataAvailable
void OnDataAvailable(TopicType const &sample)
Definition: computation.hpp:91
rtctk::componentFramework::GetLogger
log4cplus::Logger & GetLogger(const std::string &name="")
Get handle to a specific logger (used with logging macros)
Computation::Result::elapsed
std::chrono::duration< double > elapsed
Definition: computation.hpp:104
Computation::SetStaticConfig
void SetStaticConfig(unsigned to_read, unsigned n_slopes, unsigned n_modes, unsigned gpu)
Definition: computation.hpp:38
logger.hpp
Logging Support Library based on log4cplus.
Computation
Definition: computation.hpp:28
Computation::Compute
Result Compute()
Definition: computation.hpp:108
Computation::Computation
Computation()
Definition: computation.hpp:32
rtctk::componentFramework::MatrixBuffer< float >
topics.hpp
Common topic definitions used in various examples.