RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
computation.hpp
Go to the documentation of this file.
1
11
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 <string>
23#include <vector>
24
25// for python computation
27
28namespace rtctk::exampleDataTask {
29
30using namespace rtctk::componentFramework;
31
32// Constructor not required, all local vars are set by the SetStaticConfig method
33// cppcheck-suppress noConstructor
34class Computation {
35public:
36 enum class Algorithm : uint8_t { SimpleInversion, PythonInversion };
37
38 Computation() : m_logger(GetLogger("app")) {
39 }
40
41 void SetStaticConfig(size_t n_slopes, size_t n_acts) {
42 m_n_slopes = n_slopes;
43 m_n_acts = n_acts;
44
45 // set vectors and matrix to correct size.
46 m_im.resize(m_n_acts, m_n_slopes);
47 m_cm.resize(m_n_slopes, m_n_acts);
48 m_ipiv.resize(m_n_acts);
49
50 // some simple debug output
51 LOG4CPLUS_DEBUG(m_logger, "m_n_slopes: " << m_n_slopes);
52 LOG4CPLUS_DEBUG(m_logger, "m_n_acts: " << m_n_acts);
53 }
54
56 m_im = std::move(data);
57
58 if (m_n_acts != m_im.GetNrows() || m_n_slopes != m_im.GetNcols()) {
59 std::stringstream err_text;
60 err_text << "IM wrong shape, "
61 << "expected:" << m_n_acts << " x " << m_n_slopes
62 << "received: " << m_im.GetNrows() << " x " << m_im.GetNcols();
64 CII_THROW(RtctkException, err_text.str());
65 }
66 }
67
68 struct Result {
70
71 struct {
72 std::chrono::duration<double> elapsed;
74 };
75
77 auto time_start = std::chrono::steady_clock::now();
78
79 if (algorithm == Algorithm::SimpleInversion) {
80 // Invert the matrix in a very simple way.
81 memcpy(m_cm.data(), m_im.data(), m_n_slopes * m_n_acts * sizeof(float));
82 LAPACKE_sgetrf(
83 LAPACK_ROW_MAJOR, m_n_acts, m_n_slopes, m_cm.data(), m_n_slopes, m_ipiv.data());
84 LAPACKE_sgetri(LAPACK_ROW_MAJOR, m_n_slopes, m_cm.data(), m_n_acts, m_ipiv.data());
85 } else {
86 // Always acquire the Python GIL before calling Python code.
87 py::gil_scoped_acquire gil;
88 // Load the Python module and setup the function to call. You can do this earlier in the
89 // constructor to speed things up.
90 // cppcheck-suppress unreadVariable
91 auto py_compute_module = py::module::import("rtctk_example_data_task_py_lib");
92 auto py_inversion = py_compute_module.attr("inversion");
93 // Do the computation.
94 py_inversion(&m_im, &m_cm);
95 }
96
97 auto elapsed = std::chrono::steady_clock::now() - time_start;
98
99 return {.cm = m_cm, .stats = {elapsed}};
100 }
101
102private:
103 log4cplus::Logger& m_logger;
104
105 // static config
106 size_t m_n_slopes = 0;
107 size_t m_n_acts = 0;
108
109 // input data
111
112 // output data
114
115 std::vector<int> m_ipiv;
116};
117
118} // namespace rtctk::exampleDataTask
119
120#endif // RTCTK_EXAMPLEDATATASK_COMPUTATION_HPP
A buffer class representing 2D matrix data.
Definition matrixBuffer.hpp:27
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:220
Definition computation.hpp:35
Result Compute(Algorithm algorithm)
Definition computation.hpp:76
rtctk::componentFramework::MatrixBuffer< T > MatrixBuffer
Definition computation.hpp:40
void SetDynamicConfig(MatrixBuffer< float > &&data)
Definition computation.hpp:55
Computation()
Definition computation.hpp:38
void SetStaticConfig(size_t n_slopes, size_t n_acts)
Definition computation.hpp:41
Algorithm
Definition computation.hpp:36
@ SimpleInversion
Definition computation.hpp:36
@ PythonInversion
Definition computation.hpp:36
Provides macros and utilities for exception handling.
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:191
Logging Support Library based on log4cplus.
Declaration of the embedded Python module for MatrixBuffer classes.
Declaration of the MatrixBuffer template class used in APIs.
Definition businessLogic.cpp:26
Definition computation.hpp:42
struct rtctk::exampleDataTask::Computation::Result::@344315031211130374123213135274146162054312272367 stats
const MatrixBuffer< float > & cm
Definition computation.hpp:69
std::chrono::duration< double > elapsed
Definition computation.hpp:72