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

algorithm_base_mode_impl.h
1 /* file: algorithm_base_mode_impl.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 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 #include "services/host_app.h"
55 
56 namespace daal
57 {
58 namespace algorithms
59 {
60 
64 namespace interface1
65 {
66 
73 template<ComputeMode mode>
74 class DAAL_EXPORT AlgorithmImpl : public Algorithm<mode>
75 {
76 public:
78  AlgorithmImpl() : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
79 
80  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true), wasFinalizeSetup(false), resetFinalizeFlag(true) {}
81 
82  virtual ~AlgorithmImpl()
83  {
84  resetCompute();
85  resetFinalizeCompute();
86  }
87 
92  services::Status computeNoThrow();
93 
98  services::Status compute()
99  {
100  this->_status = computeNoThrow();
101  return services::throwIfPossible(this->_status);
102  }
103 
107  services::Status finalizeComputeNoThrow()
108  {
109  if(this->isChecksEnabled())
110  {
111  services::Status s = this->checkPartialResult();
112  if(!s)
113  return s;
114  }
115 
116  services::Status s = this->allocateResultMemory();
117  if(!s)
118  return s.add(services::ErrorMemoryAllocationFailed);
119 
120  this->_ac->setPartialResult(this->_pres);
121  this->_ac->setResult(this->_res);
122 
123  if(this->isChecksEnabled())
124  {
125  s = this->checkFinalizeComputeParams();
126  if(!s)
127  return s;
128  }
129 
130  s = setupFinalizeCompute();
131  if(s)
132  s |= this->_ac->finalizeCompute();
133  if(resetFinalizeFlag)
134  s |= resetFinalizeCompute();
135  return s;
136  }
137 
141  services::Status finalizeCompute()
142  {
143  this->_status = finalizeComputeNoThrow();
144  return services::throwIfPossible(this->_status);
145  }
146 
150  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
151  {
152  services::Status s;
153  if (this->_par)
154  s = this->_par->check();
155  return s.add(this->_in->check(this->_par, this->getMethod()));
156  }
157 
161  virtual services::Status checkResult() DAAL_C11_OVERRIDE
162  {
163  return this->_pres ? this->_pres->check(this->_in, this->_par, this->getMethod()) :
164  services::Status(services::ErrorNullPartialResult);
165  }
166 
170  virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
171  {
172  return this->_pres ? this->_pres->check(this->_par, this->getMethod()) :
173  services::Status(services::ErrorNullPartialResult);
174  }
175 
179  virtual services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
180  {
181  return this->_res ? this->_res->check(this->_pres, this->_par, this->getMethod()) : services::Status();
182  }
183 
184  services::Status setupCompute()
185  {
186  services::Status s;
187  if(!wasSetup)
188  {
189  s = this->_ac->setupCompute();
190  wasSetup = true;
191  }
192  return s;
193  }
194 
195  services::Status resetCompute()
196  {
197  services::Status s;
198  if(wasSetup)
199  {
200  s = this->_ac->resetCompute();
201  wasSetup = false;
202  }
203  return s;
204  }
205 
206  void enableResetOnCompute(bool flag)
207  {
208  resetFlag = flag;
209  }
210 
211  services::Status setupFinalizeCompute()
212  {
213  services::Status s;
214  if(!wasFinalizeSetup)
215  {
216  s = this->_ac->setupFinalizeCompute();
217  wasFinalizeSetup = true;
218  }
219  return s;
220  }
221 
222  services::Status resetFinalizeCompute()
223  {
224  services::Status s;
225  if(wasFinalizeSetup)
226  {
227  s = this->_ac->resetFinalizeCompute();
228  wasFinalizeSetup = false;
229  }
230  return s;
231  }
232 
233  void enableResetOnFinalizeCompute(bool flag)
234  {
235  resetFinalizeFlag = flag;
236  }
241  services::HostAppIfacePtr hostApp();
242 
247  void setHostApp(const services::HostAppIfacePtr& pHost);
248 
249 private:
250  bool wasSetup;
251  bool resetFlag;
252  bool wasFinalizeSetup;
253  bool resetFinalizeFlag;
254 };
255 
260 template<>
261 class DAAL_EXPORT AlgorithmImpl<batch> : public Algorithm<batch>
262 {
263 public:
265  AlgorithmImpl() : wasSetup(false), resetFlag(true) {}
266 
267  AlgorithmImpl(const AlgorithmImpl& other) : wasSetup(false), resetFlag(true) {}
268 
269  virtual ~AlgorithmImpl()
270  {
271  resetCompute();
272  }
273 
277  services::Status computeNoThrow();
278 
282  services::Status compute()
283  {
284  this->_status = computeNoThrow();
285  return services::throwIfPossible(this->_status);
286  }
287 
291  virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
292  {
293  services::Status s;
294  if (_par)
295  {
296  s = _par->check();
297  if(!s)
298  return s;
299  }
300 
301  return _in->check(_par, getMethod());
302  }
303 
307  virtual services::Status checkResult() DAAL_C11_OVERRIDE
308  {
309  if (_res)
310  return _res->check(_in, _par, getMethod());
311  return services::Status(services::ErrorNullResult);
312  }
313 
314  services::Status setupCompute()
315  {
316  services::Status s;
317  if(!wasSetup)
318  {
319  s = this->_ac->setupCompute();
320  wasSetup = true;
321  }
322  return s;
323  }
324 
325  services::Status resetCompute()
326  {
327  services::Status s;
328  if(wasSetup)
329  {
330  s = this->_ac->resetCompute();
331  wasSetup = false;
332  }
333  return s;
334  }
335 
336  void enableResetOnCompute(bool flag)
337  {
338  resetFlag = flag;
339  }
340 
345  services::HostAppIfacePtr hostApp();
346 
351  void setHostApp(const services::HostAppIfacePtr& pHost);
352 
353 private:
354  bool wasSetup;
355  bool resetFlag;
356 };
358 } // namespace interface1
359 using interface1::AlgorithmImpl;
360 
361 }
362 }
363 #endif
daal::algorithms::interface1::AlgorithmImpl< batch >::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:265
daal::services::interface1::Status
Class that holds the results of API calls. In case of API routine failure it contains the list of err...
Definition: error_handling.h:491
daal
Definition: algorithm_base_common.h:57
daal::algorithms::interface1::AlgorithmImpl::AlgorithmImpl
AlgorithmImpl()
Definition: algorithm_base_mode_impl.h:78
daal::algorithms::interface1::AlgorithmImpl< batch >::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:307
daal::services::interface1::Status::add
Status & add(ErrorID id)
daal::algorithms::interface1::AlgorithmImpl::checkPartialResult
virtual services::Status checkPartialResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:170
daal::algorithms::interface1::AlgorithmImpl< batch >::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:282
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:131
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:172
daal::algorithms::interface1::AlgorithmImpl< batch >::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:291
daal::algorithms::interface1::AlgorithmImpl::checkResult
virtual services::Status checkResult() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:161
daal_defines.h
daal::algorithms::interface1::AlgorithmImpl::checkComputeParams
virtual services::Status checkComputeParams() DAAL_C11_OVERRIDE
Definition: algorithm_base_mode_impl.h:150
daal::batch
Definition: daal_defines.h:132
daal::algorithms::interface1::AlgorithmImpl::compute
services::Status compute()
Definition: algorithm_base_mode_impl.h:98
daal::algorithms::interface1::AlgorithmImpl::finalizeCompute
services::Status finalizeCompute()
Definition: algorithm_base_mode_impl.h:141
daal::algorithms::interface1::AlgorithmImpl
Provides implementations of the compute and finalizeCompute methods of the Algorithm class...
Definition: algorithm_base_mode_impl.h:74
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:107
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:179

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