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

algorithm_base_mode_impl.h
1 /* file: algorithm_base_mode_impl.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 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 "algorithms/algorithm_base_mode_batch.h"
28 #include "services/host_app.h"
29 
30 namespace daal
31 {
32 namespace algorithms
33 {
34 
38 namespace interface1
39 {
40 
47 template<ComputeMode mode>
48 class DAAL_EXPORT AlgorithmImpl : public Algorithm<mode>
49 {
50 public:
52  AlgorithmImpl() : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
53 
54  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
55 
56  virtual ~AlgorithmImpl()
57  {
58  resetCompute();
59  resetFinalizeCompute();
60  }
61 
66  services::Status computeNoThrow();
67 
72  services::Status compute()
73  {
74  this->_status = computeNoThrow();
75  return services::throwIfPossible(this->_status);
76  }
77 
81  services::Status finalizeComputeNoThrow()
82  {
83  if(this->isChecksEnabled())
84  {
85  services::Status s = this->checkPartialResult();
86  if(!s)
87  return s;
88  }
89 
90  services::Status s = this->allocateResultMemory();
91  if(!s)
92  return s.add(services::ErrorMemoryAllocationFailed);
93 
94  this->_ac->setPartialResult(this->_pres);
95  this->_ac->setResult(this->_res);
96 
97  if(this->isChecksEnabled())
98  {
99  s = this->checkFinalizeComputeParams();
100  if(!s)
101  return s;
102  }
103 
104  s = setupFinalizeCompute();
105  if(s)
106  s |= this->_ac->finalizeCompute();
107  if(resetFinalizeFlag)
108  s |= resetFinalizeCompute();
109  return s;
110  }
111 
115  services::Status finalizeCompute()
116  {
117  this->_status = finalizeComputeNoThrow();
118  return services::throwIfPossible(this->_status);
119  }
120 
124  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
125  {
126  services::Status s;
127  if (this->_par)
128  s = this->_par->check();
129  return s.add(this->_in->check(this->_par, this->getMethod()));
130  }
131 
135  virtual services::Status checkResult() DAAL_C11_OVERRIDE
136  {
137  return this->_pres ? this->_pres->check(this->_in, this->_par, this->getMethod()) :
138  services::Status(services::ErrorNullPartialResult);
139  }
140 
144  virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
145  {
146  return this->_pres ? this->_pres->check(this->_par, this->getMethod()) :
147  services::Status(services::ErrorNullPartialResult);
148  }
149 
153  virtual services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
154  {
155  return this->_res ? this->_res->check(this->_pres, this->_par, this->getMethod()) : services::Status();
156  }
157 
158  services::Status setupCompute()
159  {
160  services::Status s;
161  if(!wasSetup)
162  {
163  s = this->_ac->setupCompute();
164  wasSetup = true;
165  }
166  return s;
167  }
168 
169  services::Status resetCompute()
170  {
171  services::Status s;
172  if(wasSetup)
173  {
174  s = this->_ac->resetCompute();
175  wasSetup = false;
176  }
177  return s;
178  }
179 
180  void enableResetOnCompute(bool flag)
181  {
182  resetFlag = flag;
183  }
184 
185  services::Status setupFinalizeCompute()
186  {
187  services::Status s;
188  if(!wasFinalizeSetup)
189  {
190  s = this->_ac->setupFinalizeCompute();
191  wasFinalizeSetup = true;
192  }
193  return s;
194  }
195 
196  services::Status resetFinalizeCompute()
197  {
198  services::Status s;
199  if(wasFinalizeSetup)
200  {
201  s = this->_ac->resetFinalizeCompute();
202  wasFinalizeSetup = false;
203  }
204  return s;
205  }
206 
207  void enableResetOnFinalizeCompute(bool flag)
208  {
209  resetFinalizeFlag = flag;
210  }
215  services::HostAppIfacePtr hostApp();
216 
221  void setHostApp(const services::HostAppIfacePtr& pHost);
222 
223 private:
224  bool wasSetup;
225  bool resetFlag;
226  bool wasFinalizeSetup;
227  bool resetFinalizeFlag;
228 };
229 
234 template<>
235 class DAAL_EXPORT AlgorithmImpl<batch> : public Algorithm<batch>
236 {
237 public:
239  AlgorithmImpl() : wasSetup(false), resetFlag(true) {}
240 
241  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true) {}
242 
243  virtual ~AlgorithmImpl()
244  {
245  resetCompute();
246  }
247 
251  services::Status computeNoThrow();
252 
256  services::Status compute()
257  {
258  this->_status = computeNoThrow();
259  return services::throwIfPossible(this->_status);
260  }
261 
265  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
266  {
267  services::Status s;
268  if (_par)
269  {
270  s = _par->check();
271  if(!s)
272  return s;
273  }
274 
275  return _in->check(_par, getMethod());
276  }
277 
281  virtual services::Status checkResult() DAAL_C11_OVERRIDE
282  {
283  if (_res)
284  return _res->check(_in, _par, getMethod());
285  return services::Status(services::ErrorNullResult);
286  }
287 
288  services::Status setupCompute()
289  {
290  services::Status s;
291  if(!wasSetup)
292  {
293  s = this->_ac->setupCompute();
294  wasSetup = true;
295  }
296  return s;
297  }
298 
299  services::Status resetCompute()
300  {
301  services::Status s;
302  if(wasSetup)
303  {
304  s = this->_ac->resetCompute();
305  wasSetup = false;
306  }
307  return s;
308  }
309 
310  void enableResetOnCompute(bool flag)
311  {
312  resetFlag = flag;
313  }
314 
319  services::HostAppIfacePtr hostApp();
320 
325  void setHostApp(const services::HostAppIfacePtr& pHost);
326 
327 private:
328  bool wasSetup;
329  bool resetFlag;
330 };
332 } // namespace interface1
333 using interface1::AlgorithmImpl;
334 
335 }
336 }
337 #endif
daal::algorithms::interface1::AlgorithmImpl< batch >::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:239
daal
Definition: algorithm_base_common.h:31
daal::algorithms::interface1::AlgorithmImpl::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:52
daal::algorithms::interface1::AlgorithmImpl< batch >::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:281
daal::algorithms::interface1::AlgorithmImpl::checkPartialResult
virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:144
daal::algorithms::interface1::AlgorithmImpl< batch >::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:256
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:105
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:146
daal::algorithms::interface1::AlgorithmImpl< batch >::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:265
daal::algorithms::interface1::AlgorithmImpl::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:135
daal_defines.h
daal::algorithms::interface1::AlgorithmImpl::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:124
daal::batch
Definition: daal_defines.h:106
daal::algorithms::interface1::AlgorithmImpl::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:72
daal::algorithms::interface1::AlgorithmImpl::finalizeCompute
services::Status finalizeCompute()
Definition: algorithm_base_mode_impl.h:115
daal::algorithms::interface1::AlgorithmImpl
Provides implementations of the compute and finalizeCompute methods of the Algorithm class...
Definition: algorithm_base_mode_impl.h:48
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:81
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:153

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