C++ API Reference for Intel® Data Analytics Acceleration Library 2019 Update 5

algorithm_base_mode_impl.h
1 /* file: algorithm_base_mode_impl.h */
2 /*******************************************************************************
3 * Copyright 2014-2019 Intel Corporation.
4 *
5 * This software and the related documents are Intel copyrighted materials, and
6 * your use of them is governed by the express license under which they were
7 * provided to you (License). Unless the License provides otherwise, you may not
8 * use, modify, copy, publish, distribute, disclose or transmit this software or
9 * the related documents without Intel's prior written permission.
10 *
11 * This software and the related documents are provided as is, with no express
12 * or implied warranties, other than those that are expressly stated in the
13 * License.
14 *******************************************************************************/
15 
16 /*
17 //++
18 // Implementation of base classes defining algorithm interface.
19 //--
20 */
21 
22 #ifndef __ALGORITHM_BASE_MODE_IMPL_H__
23 #define __ALGORITHM_BASE_MODE_IMPL_H__
24 
25 #include "services/daal_defines.h"
26 #include "algorithms/algorithm_base_common.h"
27 #include "services/daal_memory.h"
28 #include "services/daal_kernel_defines.h"
29 
30 #include "services/host_app.h"
31 
32 namespace daal
33 {
34 namespace algorithms
35 {
36 
40 namespace interface1
41 {
42 
49 template<ComputeMode mode>
50 class DAAL_EXPORT AlgorithmImpl : public Algorithm<mode>
51 {
52 public:
54  AlgorithmImpl() : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
55 
56  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
57 
58  virtual ~AlgorithmImpl()
59  {
60  resetCompute();
61  resetFinalizeCompute();
62  }
63 
68  services::Status computeNoThrow();
69 
74  services::Status compute()
75  {
76  this->_status = computeNoThrow();
77  return services::throwIfPossible(this->_status);
78  }
79 
83  services::Status finalizeComputeNoThrow()
84  {
85  if(this->isChecksEnabled())
86  {
87  services::Status s = this->checkPartialResult();
88  if(!s)
89  return s;
90  }
91 
92  services::Status s = this->allocateResultMemory();
93  if(!s)
94  return s.add(services::ErrorMemoryAllocationFailed);
95 
96  this->_ac->setPartialResult(this->_pres);
97  this->_ac->setResult(this->_res);
98 
99  if(this->isChecksEnabled())
100  {
101  s = this->checkFinalizeComputeParams();
102  if(!s)
103  return s;
104  }
105 
106  s = setupFinalizeCompute();
107  if(s)
108  s |= this->_ac->finalizeCompute();
109  if(resetFinalizeFlag)
110  s |= resetFinalizeCompute();
111  return s;
112  }
113 
117  services::Status finalizeCompute()
118  {
119  this->_status = finalizeComputeNoThrow();
120  return services::throwIfPossible(this->_status);
121  }
122 
126  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
127  {
128  services::Status s;
129  if (this->_par)
130  s = this->_par->check();
131  return s.add(this->_in->check(this->_par, this->getMethod()));
132  }
133 
137  virtual services::Status checkResult() DAAL_C11_OVERRIDE
138  {
139  return this->_pres ? this->_pres->check(this->_in, this->_par, this->getMethod()) :
140  services::Status(services::ErrorNullPartialResult);
141  }
142 
146  virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
147  {
148  return this->_pres ? this->_pres->check(this->_par, this->getMethod()) :
149  services::Status(services::ErrorNullPartialResult);
150  }
151 
155  virtual services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
156  {
157  return this->_res ? this->_res->check(this->_pres, this->_par, this->getMethod()) : services::Status();
158  }
159 
160  services::Status setupCompute()
161  {
162  services::Status s;
163  if(!wasSetup)
164  {
165  s = this->_ac->setupCompute();
166  wasSetup = true;
167  }
168  return s;
169  }
170 
171  services::Status resetCompute()
172  {
173  services::Status s;
174  if(wasSetup)
175  {
176  s = this->_ac->resetCompute();
177  wasSetup = false;
178  }
179  return s;
180  }
181 
182  void enableResetOnCompute(bool flag)
183  {
184  resetFlag = flag;
185  }
186 
187  services::Status setupFinalizeCompute()
188  {
189  services::Status s;
190  if(!wasFinalizeSetup)
191  {
192  s = this->_ac->setupFinalizeCompute();
193  wasFinalizeSetup = true;
194  }
195  return s;
196  }
197 
198  services::Status resetFinalizeCompute()
199  {
200  services::Status s;
201  if(wasFinalizeSetup)
202  {
203  s = this->_ac->resetFinalizeCompute();
204  wasFinalizeSetup = false;
205  }
206  return s;
207  }
208 
209  void enableResetOnFinalizeCompute(bool flag)
210  {
211  resetFinalizeFlag = flag;
212  }
217  services::HostAppIfacePtr hostApp();
218 
223  void setHostApp(const services::HostAppIfacePtr& pHost);
224 
225 private:
226  bool wasSetup;
227  bool resetFlag;
228  bool wasFinalizeSetup;
229  bool resetFinalizeFlag;
230 };
231 
236 template<>
237 class DAAL_EXPORT AlgorithmImpl<batch> : public Algorithm<batch>
238 {
239 public:
241  AlgorithmImpl() : wasSetup(false), resetFlag(true) {}
242 
243  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true) {}
244 
245  virtual ~AlgorithmImpl()
246  {
247  resetCompute();
248  }
249 
253  services::Status computeNoThrow();
254 
258  services::Status compute()
259  {
260  this->_status = computeNoThrow();
261  return services::throwIfPossible(this->_status);
262  }
263 
267  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
268  {
269  services::Status s;
270  if (_par)
271  {
272  s = _par->check();
273  if(!s)
274  return s;
275  }
276 
277  return _in->check(_par, getMethod());
278  }
279 
283  virtual services::Status checkResult() DAAL_C11_OVERRIDE
284  {
285  if (_res)
286  return _res->check(_in, _par, getMethod());
287  return services::Status(services::ErrorNullResult);
288  }
289 
290  services::Status setupCompute()
291  {
292  services::Status s;
293  if(!wasSetup)
294  {
295  s = this->_ac->setupCompute();
296  wasSetup = true;
297  }
298  return s;
299  }
300 
301  services::Status resetCompute()
302  {
303  services::Status s;
304  if(wasSetup)
305  {
306  s = this->_ac->resetCompute();
307  wasSetup = false;
308  }
309  return s;
310  }
311 
312  void enableResetOnCompute(bool flag)
313  {
314  resetFlag = flag;
315  }
316 
321  services::HostAppIfacePtr hostApp();
322 
327  void setHostApp(const services::HostAppIfacePtr& pHost);
328 
329 private:
330  bool wasSetup;
331  bool resetFlag;
332 };
334 } // namespace interface1
335 using interface1::AlgorithmImpl;
336 
337 }
338 }
339 #endif
daal::algorithms::interface1::AlgorithmImpl< batch >::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:241
daal
Definition: algorithm_base_common.h:31
daal::algorithms::interface1::AlgorithmImpl::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:54
daal::algorithms::interface1::AlgorithmImpl< batch >::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:283
daal::algorithms::interface1::AlgorithmImpl::checkPartialResult
virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:146
daal::algorithms::interface1::AlgorithmImpl< batch >::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:258
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:105
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:147
daal::algorithms::interface1::AlgorithmImpl< batch >::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:267
daal::algorithms::interface1::AlgorithmImpl::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:137
daal_defines.h
daal::algorithms::interface1::AlgorithmImpl::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:126
daal::batch
Definition: daal_defines.h:110
daal::algorithms::interface1::AlgorithmImpl::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:74
daal::algorithms::interface1::AlgorithmImpl::finalizeCompute
services::Status finalizeCompute()
Definition: algorithm_base_mode_impl.h:117
daal::algorithms::interface1::AlgorithmImpl
Provides implementations of the compute and finalizeCompute methods of the Algorithm class...
Definition: algorithm_base_mode_impl.h:50
daal::algorithms::interface1::Algorithm
Implements the abstract interface AlgorithmIface. Algorithm is, in turn, the base class for the class...
Definition: algorithm_base_mode.h:50
daal::algorithms::interface1::AlgorithmImpl::finalizeComputeNoThrow
services::Status finalizeComputeNoThrow()
Definition: algorithm_base_mode_impl.h:83
daal_kernel_defines.h
daal::services::ErrorNullResult
Definition: error_indexes.h:96
daal::algorithms::interface1::AlgorithmImpl::checkFinalizeComputeParams
virtual services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:155

For more complete information about compiler optimizations, see our Optimization Notice.