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