C++ API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

algorithm_base_mode_impl.h
1 /* file: algorithm_base_mode_impl.h */
2 /*******************************************************************************
3 * Copyright 2014-2020 Intel Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *******************************************************************************/
17 
18 /*
19 //++
20 // Implementation of base classes defining algorithm interface.
21 //--
22 */
23 
24 #ifndef __ALGORITHM_BASE_MODE_IMPL_H__
25 #define __ALGORITHM_BASE_MODE_IMPL_H__
26 
27 #include "services/daal_defines.h"
28 #include "algorithms/algorithm_base_common.h"
29 #include "services/daal_memory.h"
30 #include "services/daal_kernel_defines.h"
31 
32 #include "services/host_app.h"
33 
34 namespace daal
35 {
36 namespace algorithms
37 {
38 
42 namespace interface1
43 {
44 
51 template<ComputeMode mode>
52 class DAAL_EXPORT AlgorithmImpl : public Algorithm<mode>
53 {
54 public:
56  AlgorithmImpl() : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
57 
58  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
59 
60  virtual ~AlgorithmImpl()
61  {
62  resetCompute();
63  resetFinalizeCompute();
64  }
65 
70  services::Status computeNoThrow();
71 
76  services::Status compute()
77  {
78  this->_status = computeNoThrow();
79  return services::throwIfPossible(this->_status);
80  }
81 
85  services::Status finalizeComputeNoThrow()
86  {
87  if(this->isChecksEnabled())
88  {
89  services::Status s = this->checkPartialResult();
90  if(!s)
91  return s;
92  }
93 
94  services::Status s = this->allocateResultMemory();
95  if(!s)
96  return s.add(services::ErrorMemoryAllocationFailed);
97 
98  this->_ac->setPartialResult(this->_pres);
99  this->_ac->setResult(this->_res);
100 
101  if(this->isChecksEnabled())
102  {
103  s = this->checkFinalizeComputeParams();
104  if(!s)
105  return s;
106  }
107 
108  s = setupFinalizeCompute();
109  if(s)
110  s |= this->_ac->finalizeCompute();
111  if(resetFinalizeFlag)
112  s |= resetFinalizeCompute();
113  return s;
114  }
115 
119  services::Status finalizeCompute()
120  {
121  this->_status = finalizeComputeNoThrow();
122  return services::throwIfPossible(this->_status);
123  }
124 
128  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
129  {
130  services::Status s;
131  if (this->_par)
132  s = this->_par->check();
133  return s.add(this->_in->check(this->_par, this->getMethod()));
134  }
135 
139  virtual services::Status checkResult() DAAL_C11_OVERRIDE
140  {
141  return this->_pres ? this->_pres->check(this->_in, this->_par, this->getMethod()) :
142  services::Status(services::ErrorNullPartialResult);
143  }
144 
148  virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
149  {
150  return this->_pres ? this->_pres->check(this->_par, this->getMethod()) :
151  services::Status(services::ErrorNullPartialResult);
152  }
153 
157  virtual services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
158  {
159  return this->_res ? this->_res->check(this->_pres, this->_par, this->getMethod()) : services::Status();
160  }
161 
162  services::Status setupCompute()
163  {
164  services::Status s;
165  if(!wasSetup)
166  {
167  s = this->_ac->setupCompute();
168  wasSetup = true;
169  }
170  return s;
171  }
172 
173  services::Status resetCompute()
174  {
175  services::Status s;
176  if(wasSetup)
177  {
178  s = this->_ac->resetCompute();
179  wasSetup = false;
180  }
181  return s;
182  }
183 
184  void enableResetOnCompute(bool flag)
185  {
186  resetFlag = flag;
187  }
188 
189  services::Status setupFinalizeCompute()
190  {
191  services::Status s;
192  if(!wasFinalizeSetup)
193  {
194  s = this->_ac->setupFinalizeCompute();
195  wasFinalizeSetup = true;
196  }
197  return s;
198  }
199 
200  services::Status resetFinalizeCompute()
201  {
202  services::Status s;
203  if(wasFinalizeSetup)
204  {
205  s = this->_ac->resetFinalizeCompute();
206  wasFinalizeSetup = false;
207  }
208  return s;
209  }
210 
211  void enableResetOnFinalizeCompute(bool flag)
212  {
213  resetFinalizeFlag = flag;
214  }
219  services::HostAppIfacePtr hostApp();
220 
225  void setHostApp(const services::HostAppIfacePtr& pHost);
226 
227 private:
228  bool wasSetup;
229  bool resetFlag;
230  bool wasFinalizeSetup;
231  bool resetFinalizeFlag;
232 };
233 
238 template<>
239 class DAAL_EXPORT AlgorithmImpl<batch> : public Algorithm<batch>
240 {
241 public:
243  AlgorithmImpl() : wasSetup(false), resetFlag(true) {}
244 
245  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true) {}
246 
247  virtual ~AlgorithmImpl()
248  {
249  resetCompute();
250  }
251 
255  services::Status computeNoThrow();
256 
260  services::Status compute()
261  {
262  this->_status = computeNoThrow();
263  return services::throwIfPossible(this->_status);
264  }
265 
269  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
270  {
271  services::Status s;
272  if (_par)
273  {
274  s = _par->check();
275  if(!s)
276  return s;
277  }
278 
279  return _in->check(_par, getMethod());
280  }
281 
285  virtual services::Status checkResult() DAAL_C11_OVERRIDE
286  {
287  if (_res)
288  return _res->check(_in, _par, getMethod());
289  return services::Status(services::ErrorNullResult);
290  }
291 
292  services::Status setupCompute()
293  {
294  services::Status s;
295  if(!wasSetup)
296  {
297  s = this->_ac->setupCompute();
298  wasSetup = true;
299  }
300  return s;
301  }
302 
303  services::Status resetCompute()
304  {
305  services::Status s;
306  if(wasSetup)
307  {
308  s = this->_ac->resetCompute();
309  wasSetup = false;
310  }
311  return s;
312  }
313 
314  void enableResetOnCompute(bool flag)
315  {
316  resetFlag = flag;
317  }
318 
323  services::HostAppIfacePtr hostApp();
324 
329  void setHostApp(const services::HostAppIfacePtr& pHost);
330 
331 private:
332  bool wasSetup;
333  bool resetFlag;
334 };
336 } // namespace interface1
337 using interface1::AlgorithmImpl;
338 
339 }
340 }
341 #endif
daal::algorithms::interface1::AlgorithmImpl::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:139
daal::batch
Definition: daal_defines.h:112
daal::algorithms::interface1::AlgorithmImpl< batch >::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:243
daal::algorithms::interface1::AlgorithmImpl< batch >::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:285
daal::algorithms::interface1::AlgorithmImpl::finalizeCompute
services::Status finalizeCompute()
Definition: algorithm_base_mode_impl.h:119
daal::services::ErrorNullResult
Definition: error_indexes.h:98
daal::algorithms::interface1::Algorithm
Implements the abstract interface AlgorithmIface. Algorithm is, in turn, the base class for the class...
Definition: algorithm_base_mode.h:52
daal::algorithms::interface1::AlgorithmImpl::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:56
daal::algorithms::interface1::AlgorithmImpl
Provides implementations of the compute and finalizeCompute methods of the Algorithm class...
Definition: algorithm_base_mode_impl.h:52
daal::algorithms::interface1::AlgorithmImpl::checkPartialResult
virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:148
daal::algorithms::interface1::AlgorithmImpl< batch >::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:260
daal_defines.h
daal::algorithms::interface1::AlgorithmImpl::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:128
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:150
daal::algorithms::interface1::AlgorithmImpl::finalizeComputeNoThrow
services::Status finalizeComputeNoThrow()
Definition: algorithm_base_mode_impl.h:85
daal_kernel_defines.h
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:107
daal::algorithms::interface1::AlgorithmImpl< batch >::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:269
daal::algorithms::interface1::AlgorithmImpl::checkFinalizeComputeParams
virtual services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:157
daal::algorithms::interface1::AlgorithmImpl::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:76

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