RTC Toolkit  0.1.0-alpha
businessLogicGpu.hpp
Go to the documentation of this file.
1 #ifndef RTCTK_EXAMPLEDATATASK_BUSINESSLOGIC_GPU_HPP
2 #define RTCTK_EXAMPLEDATATASK_BUSINESSLOGIC_GPU_HPP
3 
10 
11 #include <ipcq/reader.hpp>
12 
13 #include <chrono>
14 #include <iostream>
15 #include <vector>
16 #include <thread>
17 
18 using namespace rtctk::componentFramework;
19 using namespace rtctk::dataTask;
20 using namespace std::chrono_literals;
21 
22 namespace rtctk::exampleDataTask {
23 
24 // TODO: consider removing template arguments here, since users need to edit BusinessLogicGpu anyway
25 template <class ComputationType, class TopicType>
27 {
28  using ReaderType = ipcq::Reader<TopicType>;
29 
30  public:
32  : RunnableStateMachineLogic(name, services)
33  , m_rtr(m_services.Get<RuntimeRepoApiIf>())
34  , m_oldb(m_services.Get<OldbApiIf>())
35  {
36  LOG4CPLUS_DEBUG(GetLogger(), "ExampleDataTask c'tor");
37  }
38 
40  {
41  LOG4CPLUS_INFO(GetLogger(), "ExampleDataTask d'tor");
42  }
43 
44  // activities
45  void Starting(StopToken st) override
46  {
47  m_reader.reset();
48  m_computation.reset();
49  }
50 
51  void Recovering(StopToken st) override
52  {
53  }
54 
55  void Initialising(StopToken st) override
56  {
57  // create the user computation object
58  m_computation = std::make_unique<ComputationType>(m_rtr, m_oldb, m_name);
59  m_reader = std::make_unique<ReaderThread<TopicType,ReaderType>>();
60 
61  // register callbacks
62  m_reader->RegisterOnDataCallback( [this] (const TopicType& sample) {m_computation->OnDataAvailable(sample);});
63  m_reader->RegisterInitThreadCallback([this](){m_computation->ThreadInit();});
64 
65  // static
66  m_reader->SetQueueName(GetParam<std::string>("/common/static/loop_1/queue_name"));
67  m_reader->SetCpuAffinity(GetParam<int32_t>("/" + m_name + "/static/reader_thread/cpu_affinity"));
68  m_reader->SetLoopFrequency(GetParam<int32_t>("/common/static/loop_1/frequency"));
69 
70  // dynamic
71  m_reader->SetSamplesToRead(GetParam<int32_t>("/" + m_name + "/static/common/samples_to_read"));
72  m_reader->SetSamplesToSkip(GetParam<int32_t>("/" + m_name + "/dynamic/reader_thread/samples_to_skip"));
73  }
74 
75  void Enabling(StopToken st) override
76  {
77  m_reader->Spawn();
78  }
79 
80  void Disabling(StopToken st) override
81  {
82  m_reader->Join();
83  }
84 
85  void GoingRunning(StopToken st) override
86  {
87  m_computation->Reset();
88  m_reader->Run();
89  }
90 
91  void GoingIdle(StopToken st) override
92  {
93  m_reader->Idle();
94  }
95 
96  void Running(StopToken st) override
97  {
98  // wait for signal from read thread to trigger computation
99  while(true)
100  {
101  if(st.StopRequested()){break;}
102 
103  m_reader->WaitUntilComputationAllowed();
104 
105  if(st.StopRequested()){break;} // TODO: do we need this?
106 
107  m_computation->Compute();
108 
109  m_reader->SignalComputationDone();
110  }
111 
112  m_reader->SignalComputationDone();
113  }
114 
115  void Updating(StopToken st, Payload args) override
116  {
117  m_reader->SetSamplesToSkip(GetParam<int32_t>("/" + m_name + "/dynamic/reader_thread/samples_to_skip"));
118  m_computation->DynamicLoad();
119  }
120 
121  // guards
122  bool IsUpdatingAllowed(Payload args) override
123  {
124  if(m_computation->isComputing())
125  {
126  return false;
127  }
128  else
129  {
130  return true;
131  }
132  }
133 
134  protected:
135 
136  template<typename T>
137  T GetParam(std::string const& path)
138  {
139  DataPointPath dp_uri(path);
140  auto value = m_rtr.GetDataPoint<T>(dp_uri);
141  return value;
142  }
143 
146 
147  std::unique_ptr<ComputationType> m_computation;
148  std::unique_ptr<ReaderThread<TopicType,ReaderType>> m_reader;
149 };
150 
151 } //namespace
152 
153 #endif
serviceContainer.hpp
A container that can hold any type of service.
runnableStateMachineLogic.hpp
Empty implementation for the RunnableStateMachineLogicIf.
rtctk::exampleDataTask::BusinessLogicGpu::m_computation
std::unique_ptr< ComputationType > m_computation
Definition: businessLogicGpu.hpp:147
rtctk::exampleDataTask::BusinessLogicGpu::BusinessLogicGpu
BusinessLogicGpu(const std::string &name, rtctk::componentFramework::ServiceContainer &services)
Definition: businessLogicGpu.hpp:31
rtctk::exampleDataTask::BusinessLogicGpu
Definition: businessLogicGpu.hpp:27
rtctk::exampleDataTask::BusinessLogicGpu::~BusinessLogicGpu
virtual ~BusinessLogicGpu()
Definition: businessLogicGpu.hpp:39
rtctk::exampleDataTask::BusinessLogicGpu::m_rtr
RuntimeRepoApiIf & m_rtr
Definition: businessLogicGpu.hpp:144
rtctk::exampleDataTask::BusinessLogicGpu::Initialising
void Initialising(StopToken st) override
Definition: businessLogicGpu.hpp:55
rtctk::componentFramework
Definition: rtcComponent.hpp:17
oldbApiIf.hpp
Header file for OldbApiIf, which defines the API for OldbAdapters.
rtctkExampleDataTaskRobotTest.path
string path
Definition: rtctkExampleDataTaskRobotTest.py:228
rtctk::exampleDataTask::BusinessLogicGpu::IsUpdatingAllowed
bool IsUpdatingAllowed(Payload args) override
Definition: businessLogicGpu.hpp:122
rtctk::componentFramework::StopToken
rad::StopToken StopToken
Definition: stopToken.hpp:16
rtctk::exampleDataTask::BusinessLogicGpu::Disabling
void Disabling(StopToken st) override
Definition: businessLogicGpu.hpp:80
rtctk::exampleDataTask::BusinessLogicGpu::Starting
void Starting(StopToken st) override
Definition: businessLogicGpu.hpp:45
rtctk::componentFramework::GetLogger
log4cplus::Logger & GetLogger(const std::string &name="")
rtctk::componentFramework::OldbApiIf
Definition: oldbApiIf.hpp:18
rtctk::componentFramework::Payload
std::string Payload
Definition: runnableStateMachineLogicIf.hpp:17
rtctk::componentFramework::ServiceContainer
Definition: serviceContainer.hpp:31
rtctk::exampleDataTask
Definition: businessLogic.hpp:22
rtctk::exampleDataTask::BusinessLogicGpu::Recovering
void Recovering(StopToken st) override
Definition: businessLogicGpu.hpp:51
rtctk::componentFramework::RuntimeRepoApiIf
Definition: runtimeRepoApiIf.hpp:59
rtctk::exampleDataTask::BusinessLogicGpu::GoingRunning
void GoingRunning(StopToken st) override
Definition: businessLogicGpu.hpp:85
readerThread.hpp
Header file needed to.
rtctk::exampleDataTask::BusinessLogicGpu::m_oldb
OldbApiIf & m_oldb
Definition: businessLogicGpu.hpp:145
rtctk::dataTask
Definition: messageQueue.hpp:17
runtimeRepoApiIf.hpp
Header file for RuntimeRepoApiIf, which defines the API for RuntimeRepoAdapters.
logger.hpp
Logging Support Library based on log4cplus.
rtctk::exampleDataTask::BusinessLogicGpu::GoingIdle
void GoingIdle(StopToken st) override
Definition: businessLogicGpu.hpp:91
rtctk::componentFramework::RunnableStateMachineLogic
Definition: runnableStateMachineLogic.hpp:28
rtctk::componentFramework::DataPointPath
Definition: dataPointPath.hpp:30
rtctk::exampleDataTask::BusinessLogicGpu::Enabling
void Enabling(StopToken st) override
Definition: businessLogicGpu.hpp:75
rtctk::exampleDataTask::BusinessLogicGpu::Running
void Running(StopToken st) override
Definition: businessLogicGpu.hpp:96
rtctk::exampleDataTask::BusinessLogicGpu::GetParam
T GetParam(std::string const &path)
Definition: businessLogicGpu.hpp:137
rtctk::exampleDataTask::BusinessLogicGpu::Updating
void Updating(StopToken st, Payload args) override
Definition: businessLogicGpu.hpp:115
rtctk::exampleDataTask::BusinessLogicGpu::m_reader
std::unique_ptr< ReaderThread< TopicType, ReaderType > > m_reader
Definition: businessLogicGpu.hpp:148