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