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

algorithm_base_mode_impl.h
1 /* file: algorithm_base_mode_impl.h */
2 /*******************************************************************************
3 * Copyright 2014-2017 Intel Corporation
4 * All Rights Reserved.
5 *
6 * If this software was obtained under the Intel Simplified Software License,
7 * the following terms apply:
8 *
9 * The source code, information and material ("Material") contained herein is
10 * owned by Intel Corporation or its suppliers or licensors, and title to such
11 * Material remains with Intel Corporation or its suppliers or licensors. The
12 * Material contains proprietary information of Intel or its suppliers and
13 * licensors. The Material is protected by worldwide copyright laws and treaty
14 * provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed or disclosed
16 * in any way without Intel's prior express written permission. No license under
17 * any patent, copyright or other intellectual property rights in the Material
18 * is granted to or conferred upon you, either expressly, by implication,
19 * inducement, estoppel or otherwise. Any license under such intellectual
20 * property rights must be express and approved by Intel in writing.
21 *
22 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 * notice or any other notice embedded in Materials by Intel or Intel's
24 * suppliers or licensors in any way.
25 *
26 *
27 * If this software was obtained under the Apache License, Version 2.0 (the
28 * "License"), the following terms apply:
29 *
30 * You may not use this file except in compliance with the License. You may
31 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 *
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 *
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41 
42 /*
43 //++
44 // Implementation of base classes defining algorithm interface.
45 //--
46 */
47 
48 #ifndef __ALGORITHM_BASE_MODE_IMPL_H__
49 #define __ALGORITHM_BASE_MODE_IMPL_H__
50 
51 #include "services/daal_defines.h"
52 #include "algorithms/algorithm_base_common.h"
53 #include "algorithms/algorithm_base_mode_batch.h"
54 
55 namespace daal
56 {
57 namespace algorithms
58 {
59 
63 namespace interface1
64 {
71 template<ComputeMode mode>
72 class DAAL_EXPORT AlgorithmImpl : public Algorithm<mode>
73 {
74 public:
76  AlgorithmImpl() : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
77 
78  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
79 
80  virtual ~AlgorithmImpl()
81  {
82  resetCompute();
83  resetFinalizeCompute();
84  }
85 
90  services::Status computeNoThrow();
91 
96  services::Status compute()
97  {
98  this->_status = computeNoThrow();
99  return services::throwIfPossible(this->_status);
100  }
101 
105  services::Status finalizeComputeNoThrow()
106  {
107  if(this->isChecksEnabled())
108  {
109  services::Status s = this->checkPartialResult();
110  if(!s)
111  return s;
112  }
113 
114  services::Status s = this->allocateResultMemory();
115  if(!s)
116  return s.add(services::ErrorMemoryAllocationFailed);
117 
118  this->_ac->setPartialResult(this->_pres);
119  this->_ac->setResult(this->_res);
120 
121  if(this->isChecksEnabled())
122  {
123  s = this->checkFinalizeComputeParams();
124  if(!s)
125  return s;
126  }
127 
128  s = setupFinalizeCompute();
129  if(s)
130  s |= this->_ac->finalizeCompute();
131  if(resetFinalizeFlag)
132  s |= resetFinalizeCompute();
133  return s;
134  }
135 
139  services::Status finalizeCompute()
140  {
141  this->_status = finalizeComputeNoThrow();
142  return services::throwIfPossible(this->_status);
143  }
144 
148  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
149  {
150  services::Status s;
151  if (this->_par)
152  s = this->_par->check();
153  return s.add(this->_in->check(this->_par, this->getMethod()));
154  }
155 
159  virtual services::Status checkResult() DAAL_C11_OVERRIDE
160  {
161  return this->_pres ? this->_pres->check(this->_in, this->_par, this->getMethod()) :
162  services::Status(services::ErrorNullPartialResult);
163  }
164 
168  virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
169  {
170  return this->_pres ? this->_pres->check(this->_par, this->getMethod()) :
171  services::Status(services::ErrorNullPartialResult);
172  }
173 
177  virtual services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
178  {
179  return this->_res ? this->_res->check(this->_pres, this->_par, this->getMethod()) : services::Status();
180  }
181 
182  services::Status setupCompute()
183  {
184  services::Status s;
185  if(!wasSetup)
186  {
187  s = this->_ac->setupCompute();
188  wasSetup = true;
189  }
190  return s;
191  }
192 
193  services::Status resetCompute()
194  {
195  services::Status s;
196  if(wasSetup)
197  {
198  s = this->_ac->resetCompute();
199  wasSetup = false;
200  }
201  return s;
202  }
203 
204  void enableResetOnCompute(bool flag)
205  {
206  resetFlag = flag;
207  }
208 
209  services::Status setupFinalizeCompute()
210  {
211  services::Status s;
212  if(!wasFinalizeSetup)
213  {
214  s = this->_ac->setupFinalizeCompute();
215  wasFinalizeSetup = true;
216  }
217  return s;
218  }
219 
220  services::Status resetFinalizeCompute()
221  {
222  services::Status s;
223  if(wasFinalizeSetup)
224  {
225  s = this->_ac->resetFinalizeCompute();
226  wasFinalizeSetup = false;
227  }
228  return s;
229  }
230 
231  void enableResetOnFinalizeCompute(bool flag)
232  {
233  resetFinalizeFlag = flag;
234  }
235 
236 private:
237  bool wasSetup;
238  bool resetFlag;
239  bool wasFinalizeSetup;
240  bool resetFinalizeFlag;
241 };
242 
247 template<>
248 class DAAL_EXPORT AlgorithmImpl<batch> : public Algorithm<batch>
249 {
250 public:
252  AlgorithmImpl() : wasSetup(false), resetFlag(true) {}
253 
254  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true) {}
255 
256  virtual ~AlgorithmImpl()
257  {
258  resetCompute();
259  }
260 
264  services::Status computeNoThrow();
265 
269  services::Status compute()
270  {
271  this->_status = computeNoThrow();
272  return services::throwIfPossible(this->_status);
273  }
274 
278  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
279  {
280  services::Status s;
281  if (_par)
282  {
283  s = _par->check();
284  if(!s)
285  return s;
286  }
287 
288  return _in->check(_par, getMethod());
289  }
290 
294  virtual services::Status checkResult() DAAL_C11_OVERRIDE
295  {
296  if (_res)
297  return _res->check(_in, _par, getMethod());
298  return services::Status(services::ErrorNullResult);
299  }
300 
301  services::Status setupCompute()
302  {
303  services::Status s;
304  if(!wasSetup)
305  {
306  s = this->_ac->setupCompute();
307  wasSetup = true;
308  }
309  return s;
310  }
311 
312  services::Status resetCompute()
313  {
314  services::Status s;
315  if(wasSetup)
316  {
317  s = this->_ac->resetCompute();
318  wasSetup = false;
319  }
320  return s;
321  }
322 
323  void enableResetOnCompute(bool flag)
324  {
325  resetFlag = flag;
326  }
327 
328 private:
329  bool wasSetup;
330  bool resetFlag;
331 };
333 } // namespace interface1
334 using interface1::AlgorithmImpl;
335 
336 }
337 }
338 #endif
daal::algorithms::interface1::AlgorithmImpl< batch >::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:252
daal
Definition: algorithm_base_common.h:57
daal::algorithms::interface1::AlgorithmImpl::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:76
daal::algorithms::interface1::AlgorithmImpl< batch >::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:294
daal::algorithms::interface1::AlgorithmImpl::checkPartialResult
virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:168
daal::algorithms::interface1::AlgorithmImpl< batch >::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:269
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:131
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:170
daal::algorithms::interface1::AlgorithmImpl< batch >::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:278
daal::algorithms::interface1::AlgorithmImpl::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:159
daal_defines.h
daal::algorithms::interface1::AlgorithmImpl::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:148
daal::batch
Definition: daal_defines.h:131
daal::algorithms::interface1::AlgorithmImpl::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:96
daal::algorithms::interface1::AlgorithmImpl::finalizeCompute
services::Status finalizeCompute()
Definition: algorithm_base_mode_impl.h:139
daal::algorithms::interface1::AlgorithmImpl
Provides implementations of the compute and finalizeCompute methods of the Algorithm class...
Definition: algorithm_base_mode_impl.h:72
daal::algorithms::interface1::Algorithm
Implements the abstract interface AlgorithmIface. Algorithm is, in turn, the base class for the class...
Definition: algorithm_base_mode.h:76
daal::algorithms::interface1::AlgorithmImpl::finalizeComputeNoThrow
services::Status finalizeComputeNoThrow()
Definition: algorithm_base_mode_impl.h:105
daal::services::ErrorNullResult
Definition: error_indexes.h:122
daal::algorithms::interface1::AlgorithmImpl::checkFinalizeComputeParams
virtual services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:177

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