RTC Toolkit  2.0.0
computation.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_EXAMPLEDATATASK_COMPUTATION_HPP
13 #define RTCTK_EXAMPLEDATATASK_COMPUTATION_HPP
14 
18 
19 #include <cblas.h>
20 #include <chrono>
21 #include <lapacke.h>
22 #include <numeric>
23 #include <string>
24 #include <vector>
25 
26 // for python computation
28 
29 namespace rtctk::exampleDataTask {
30 
31 using namespace rtctk::componentFramework;
32 
33 class Computation {
34 public:
35  enum Algorithm { SimpleInversion, PythonInversion };
36 
37  void SetStaticConfig(unsigned n_slopes, unsigned n_acts) {
38  m_n_slopes = n_slopes;
39  m_n_acts = n_acts;
40 
41  // set vectors and matrix to correct size.
42  m_IM.resize(m_n_acts, m_n_slopes);
43  m_CM.resize(m_n_slopes, m_n_acts);
44  m_ipiv.resize(m_n_acts);
45 
46  // some simple debug output
47  LOG4CPLUS_DEBUG(GetLogger(), "m_n_slopes: " << m_n_slopes);
48  LOG4CPLUS_DEBUG(GetLogger(), "m_n_acts: " << m_n_acts);
49  }
50 
52  m_IM = std::move(data);
53 
54  if (m_n_acts != (int)m_IM.GetNrows() || m_n_slopes != (int)m_IM.GetNcols()) {
55  std::stringstream err_text;
56  err_text << "IM wrong shape, "
57  << "expected:" << m_n_acts << " x " << m_n_slopes
58  << "received: " << m_IM.GetNrows() << " x " << m_IM.GetNcols();
60  CII_THROW(RtctkException, err_text.str());
61  }
62  }
63 
64  struct Result {
66 
67  struct {
68  std::chrono::duration<double> elapsed;
69  } stats;
70  };
71 
72  Result Compute(Algorithm algorithm) {
73  auto time_start = std::chrono::system_clock::now();
74 
75  if (algorithm == SimpleInversion) {
76  // invert the matrix in a very dumv way.
77  memcpy((void*)m_CM.data(), (void*)m_IM.data(), m_n_slopes * m_n_acts * sizeof(float));
78  LAPACKE_sgetrf(
79  LAPACK_ROW_MAJOR, m_n_acts, m_n_slopes, m_CM.data(), m_n_slopes, m_ipiv.data());
80  LAPACKE_sgetri(LAPACK_ROW_MAJOR, m_n_slopes, m_CM.data(), m_n_acts, m_ipiv.data());
81  } else {
82  // set up python and load lib (do this earlier to speed up things)
83  py::scoped_interpreter py_guard;
84  auto py_matrix_buffer_import = LoadMatrixBufferPyBinds();
85  auto py_compute_module = py::module::import("rtctkExampleDataTaskPyLib");
86  auto py_inversion = py_compute_module.attr("inversion");
87  // do the computation
88  py_inversion(&m_IM, &m_CM);
89  }
90 
91  auto elapsed = std::chrono::system_clock::now() - time_start;
92 
93  return {m_CM, elapsed};
94  }
95 
96 private:
97  // static config
98  int m_n_slopes;
99  int m_n_acts;
100 
101  // input data
102  MatrixBuffer<float> m_IM;
103 
104  // output data
105  MatrixBuffer<float> m_CM;
106 
107  std::vector<int> m_ipiv;
108 };
109 
110 } // namespace rtctk::exampleDataTask
111 
112 #endif // RTCTK_EXAMPLEDATATASK_COMPUTATION_HPP
exceptions.hpp
Provides macros and utilities for exception handling.
rtctk::exampleDataTask::Computation::Result::elapsed
std::chrono::duration< double > elapsed
Definition: computation.hpp:68
rtctk::exampleDataTask::Computation::Result::cm
MatrixBuffer< float > const & cm
Definition: computation.hpp:65
rtctk::exampleDataTask::Computation::Algorithm
Algorithm
Definition: computation.hpp:26
rtctk::exampleDataTask::Computation::SimpleInversion
@ SimpleInversion
Definition: computation.hpp:35
rtctk::componentFramework
Definition: commandReplier.cpp:20
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
rtctk::exampleDataTask::Computation::SetStaticConfig
void SetStaticConfig(unsigned n_slopes, unsigned n_acts)
Definition: computation.hpp:37
matrixBufferPy.hpp
Declaration of the MatrixBuffer template class used in APIs.
rtctk::componentFramework::GetLogger
log4cplus::Logger & GetLogger(const std::string &name="")
Get handle to a specific logger (used with logging macros)
rtctk::exampleDataTask
Definition: businessLogic.hpp:24
rtctk::exampleDataTask::Computation::Result
Definition: computation.hpp:52
rtctk::componentFramework::LoadMatrixBufferPyBinds
py::module EXPORT_TYPE LoadMatrixBufferPyBinds()
Definition: matrixBufferPy.cpp:51
rtctk::exampleDataTask::Computation::SetDynamicConfig
void SetDynamicConfig(MatrixBuffer< float > &&data)
Definition: computation.hpp:51
logger.hpp
Logging Support Library based on log4cplus.
Computation
Definition: computation.hpp:28
rtctk::exampleDataTask::Computation::Compute
Result Compute(Algorithm algorithm)
Definition: computation.hpp:72
rtctk::componentFramework::MatrixBuffer< float >