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

kmeans_distributed.h
1 /* file: kmeans_distributed.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 the interface for the K-Means algorithm in the distributed
45 // processing mode
46 //--
47 */
48 
49 #ifndef __KMEANS_DISTRIBITED_H__
50 #define __KMEANS_DISTRIBITED_H__
51 
52 #include "algorithms/algorithm.h"
53 #include "data_management/data/numeric_table.h"
54 #include "services/daal_defines.h"
55 #include "algorithms/kmeans/kmeans_types.h"
56 
57 namespace daal
58 {
59 namespace algorithms
60 {
61 namespace kmeans
62 {
63 
64 namespace interface1
65 {
80 template<ComputeStep step, typename algorithmFPType, Method method, CpuType cpu>
81 class DAAL_EXPORT DistributedContainer;
82 
87 template<typename algorithmFPType, Method method, CpuType cpu>
88 class DAAL_EXPORT DistributedContainer<step1Local, algorithmFPType, method, cpu> : public
89  daal::algorithms::AnalysisContainerIface<distributed>
90 {
91 public:
97  DistributedContainer(daal::services::Environment::env *daalEnv);
99  virtual ~DistributedContainer();
104  virtual services::Status compute() DAAL_C11_OVERRIDE;
109  virtual services::Status finalizeCompute() DAAL_C11_OVERRIDE;
110 };
111 
116 template<typename algorithmFPType, Method method, CpuType cpu>
117 class DAAL_EXPORT DistributedContainer<step2Master, algorithmFPType, method, cpu> : public
118  daal::algorithms::AnalysisContainerIface<distributed>
119 {
120 public:
126  DistributedContainer(daal::services::Environment::env *daalEnv);
128  virtual ~DistributedContainer();
133  virtual services::Status compute() DAAL_C11_OVERRIDE;
138  virtual services::Status finalizeCompute() DAAL_C11_OVERRIDE;
139 };
140 
158 template<ComputeStep step, typename algorithmFPType = DAAL_ALGORITHM_FP_TYPE, Method method = lloydDense>
159 class DAAL_EXPORT Distributed {};
160 
174 template<typename algorithmFPType, Method method>
175 class DAAL_EXPORT Distributed<step1Local, algorithmFPType, method> : public daal::algorithms::Analysis<distributed>
176 {
177 public:
178  typedef algorithms::kmeans::Input InputType;
179  typedef algorithms::kmeans::Parameter ParameterType;
180  typedef algorithms::kmeans::Result ResultType;
181  typedef algorithms::kmeans::PartialResult PartialResultType;
182 
188  Distributed(size_t nClusters, bool assignFlag = false) : parameter(nClusters, 1)
189  {
190  initialize();
191  parameter.assignFlag = assignFlag;
192  }
193 
200  Distributed(const Distributed<step1Local, algorithmFPType, method> &other) : parameter(other.parameter)
201  {
202  initialize();
203  input.set(data, other.input.get(data));
204  input.set(inputCentroids, other.input.get(inputCentroids));
205  }
206 
211  virtual int getMethod() const DAAL_C11_OVERRIDE { return(int) method; }
212 
217  ResultPtr getResult()
218  {
219  return _result;
220  }
221 
226  services::Status setResult(const ResultPtr& result)
227  {
228  DAAL_CHECK(result, services::ErrorNullResult)
229  _result = result;
230  _res = _result.get();
231  return services::Status();
232  }
233 
238  PartialResultPtr getPartialResult()
239  {
240  return _partialResult;
241  }
242 
246  services::Status setPartialResult(const PartialResultPtr& partialRes)
247  {
248  DAAL_CHECK(partialRes, services::ErrorNullPartialResult);
249  _partialResult = partialRes;
250  _pres = _partialResult.get();
251  return services::Status();
252  }
253 
257  services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
258  {
259  return services::Status();
260  }
261 
267  services::SharedPtr<Distributed<step1Local, algorithmFPType, method> > clone() const
268  {
269  return services::SharedPtr<Distributed<step1Local, algorithmFPType, method> >(cloneImpl());
270  }
271 
272 protected:
273  virtual Distributed<step1Local, algorithmFPType, method> * cloneImpl() const DAAL_C11_OVERRIDE
274  {
275  return new Distributed<step1Local, algorithmFPType, method>(*this);
276  }
277 
278  virtual services::Status allocateResult() DAAL_C11_OVERRIDE
279  {
280  _result.reset(new ResultType());
281  services::Status s = _result->allocate<algorithmFPType>(_in, _par, (int) method);
282  _res = _result.get();
283  return s;
284  }
285 
286  virtual services::Status allocatePartialResult() DAAL_C11_OVERRIDE
287  {
288  _partialResult.reset(new PartialResultType());
289  services::Status s = _partialResult->allocate<algorithmFPType>(&input, _par, (int) method);
290  _pres = _partialResult.get();
291  return s;
292  }
293 
294  virtual services::Status initializePartialResult() DAAL_C11_OVERRIDE
295  {
296  return services::Status();
297  }
298 
299  void initialize()
300  {
301  Analysis<distributed>::_ac = new __DAAL_ALGORITHM_CONTAINER(distributed, DistributedContainer, step1Local, algorithmFPType, method)(&_env);
302  _in = &input;
303  _par = &parameter;
304  }
305 
306 public:
307  InputType input;
308  ParameterType parameter;
310 private:
311  PartialResultPtr _partialResult;
312  ResultPtr _result;
313 };
314 
331 template<typename algorithmFPType, Method method>
332 class DAAL_EXPORT Distributed<step2Master, algorithmFPType, method>: public daal::algorithms::Analysis<distributed>
333 {
334 public:
335  typedef algorithms::kmeans::DistributedStep2MasterInput InputType;
336  typedef algorithms::kmeans::Parameter ParameterType;
337  typedef algorithms::kmeans::Result ResultType;
338  typedef algorithms::kmeans::PartialResult PartialResultType;
339 
345  Distributed(size_t nClusters, size_t nIterations = 1) : parameter(nClusters, nIterations)
346  {
347  initialize();
348  parameter.assignFlag = false;
349  }
350 
357  Distributed(const Distributed<step2Master, algorithmFPType, method> &other) : parameter(other.parameter)
358  {
359  initialize();
360  input.set(partialResults, other.input.get(partialResults));
361  }
362 
367  virtual int getMethod() const DAAL_C11_OVERRIDE { return(int) method; }
368 
373  ResultPtr getResult()
374  {
375  return _result;
376  }
377 
382  services::Status setResult(const ResultPtr& result)
383  {
384  DAAL_CHECK(result, services::ErrorNullResult)
385  _result = result;
386  _res = _result.get();
387  return services::Status();
388  }
389 
394  PartialResultPtr getPartialResult()
395  {
396  return _partialResult;
397  }
398 
402  services::Status setPartialResult(const PartialResultPtr& partialRes)
403  {
404  DAAL_CHECK(partialRes, services::ErrorNullPartialResult);
405  _partialResult = partialRes;
406  _pres = _partialResult.get();
407  return services::Status();
408  }
409 
413  services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
414  {
415  services::Status s;
416  if(_partialResult)
417  {
418  s |= _partialResult->check(_par, method);
419  if (!s) { return s; }
420  }
421  else
422  {
423  return services::Status(services::ErrorNullResult);
424  }
425 
426  if(_result)
427  {
428  s |= _result->check(_partialResult.get(), _par, method);
429  }
430  else
431  {
432  return services::Status(services::ErrorNullResult);
433  }
434  return s;
435  }
436 
442  services::SharedPtr<Distributed<step2Master, algorithmFPType, method> > clone() const
443  {
444  return services::SharedPtr<Distributed<step2Master, algorithmFPType, method> >(cloneImpl());
445  }
446 
447 protected:
448  virtual Distributed<step2Master, algorithmFPType, method> * cloneImpl() const DAAL_C11_OVERRIDE
449  {
450  return new Distributed<step2Master, algorithmFPType, method>(*this);
451  }
452 
453  virtual services::Status allocateResult() DAAL_C11_OVERRIDE
454  {
455  _result.reset(new ResultType());
456  services::Status s = _result->allocate<algorithmFPType>(_pres, _par, (int) method);
457  _res = _result.get();
458  return s;
459  }
460 
461  virtual services::Status allocatePartialResult() DAAL_C11_OVERRIDE
462  {
463  _partialResult.reset(new PartialResultType());
464  services::Status s = _partialResult->allocate<algorithmFPType>(&input, _par, (int) method);
465  _pres = _partialResult.get();
466  return s;
467  }
468 
469  virtual services::Status initializePartialResult() DAAL_C11_OVERRIDE
470  {
471  return services::Status();
472  }
473 
474  void initialize()
475  {
476  Analysis<distributed>::_ac = new __DAAL_ALGORITHM_CONTAINER(distributed, DistributedContainer, step2Master, algorithmFPType, method)(&_env);
477  _in = &input;
478  _par = &parameter;
479  }
480 
481 public:
482  InputType input;
483  ParameterType parameter;
485 private:
486  PartialResultPtr _partialResult;
487  ResultPtr _result;
488 };
490 } // namespace interface1
491 using interface1::DistributedContainer;
492 using interface1::Distributed;
493 } // namespace daal::algorithms::kmeans
494 } // namespace daal::algorithms
495 } // namespace daal
496 #endif
daal::algorithms::kmeans::interface1::Distributed
Computes the results of the K-Means algorithm in the distributed processing mode. ...
Definition: kmeans_distributed.h:159
daal::algorithms::kmeans::interface1::Parameter
Parameters for the K-Means algorithm.
Definition: kmeans_types.h:155
daal::algorithms::kmeans::interface1::Result
Results obtained with the compute() method of the K-Means algorithm in the batch processing mode...
Definition: kmeans_types.h:303
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::Distributed
Distributed(const Distributed< step1Local, algorithmFPType, method > &other)
Definition: kmeans_distributed.h:200
daal::services::interface1::Environment::_envStruct
The environment structure.
Definition: env_detect.h:95
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::AnalysisContainerIface
Abstract interface class that provides virtual methods to access and run implementations of the analy...
Definition: analysis.h:79
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::setResult
services::Status setResult(const ResultPtr &result)
Definition: kmeans_distributed.h:226
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getPartialResult
PartialResultPtr getPartialResult()
Definition: kmeans_distributed.h:238
daal::algorithms::kmeans::interface1::PartialResult
Partial results obtained with the compute() method of the K-Means algorithm in the batch processing m...
Definition: kmeans_types.h:236
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getMethod
virtual int getMethod() const DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:211
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::input
InputType input
Definition: kmeans_distributed.h:482
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:131
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getResult
ResultPtr getResult()
Definition: kmeans_distributed.h:217
daal::algorithms::kmeans::data
Definition: kmeans_types.h:96
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getPartialResult
PartialResultPtr getPartialResult()
Definition: kmeans_distributed.h:394
daal::algorithms::kmeans::interface1::DistributedStep2MasterInput
Input objects for the K-Means algorithm in the distributed processing mode
Definition: kmeans_types.h:373
daal::algorithms::kmeans::interface1::Input
Input objects for the K-Means algorithm
Definition: kmeans_types.h:197
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::setPartialResult
services::Status setPartialResult(const PartialResultPtr &partialRes)
Definition: kmeans_distributed.h:402
daal_defines.h
daal::algorithms::kmeans::interface1::DistributedStep2MasterInput::get
data_management::DataCollectionPtr get(MasterInputId id) const
daal::algorithms::kmeans::nIterations
Definition: kmeans_types.h:137
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getResult
ResultPtr getResult()
Definition: kmeans_distributed.h:373
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::clone
services::SharedPtr< Distributed< step1Local, algorithmFPType, method > > clone() const
Definition: kmeans_distributed.h:267
daal::algorithms::kmeans::init::interface1::Distributed
class DAAL_EXPORT Distributed
Computes initial clusters for the K-Means algorithm in the distributed processing mode...
Definition: kmeans_init_distributed.h:281
daal::distributed
Definition: daal_defines.h:133
daal::services::interface1::SharedPtr
Shared pointer that retains shared ownership of an object through a pointer. Several SharedPtr object...
Definition: daal_shared_ptr.h:187
daal::services::interface1::SharedPtr::get
T * get() const
Definition: daal_shared_ptr.h:332
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::clone
services::SharedPtr< Distributed< step2Master, algorithmFPType, method > > clone() const
Definition: kmeans_distributed.h:442
daal::algorithms::kmeans::inputCentroids
Definition: kmeans_types.h:97
daal::algorithms::kmeans::partialResults
Definition: kmeans_types.h:107
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::checkFinalizeComputeParams
services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:413
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::Distributed
Distributed(const Distributed< step2Master, algorithmFPType, method > &other)
Definition: kmeans_distributed.h:357
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::parameter
ParameterType parameter
Definition: kmeans_distributed.h:308
daal::algorithms::Analysis
Provides methods for execution of operations over data, such as computation of Summary Statistics est...
Definition: analysis.h:94
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::parameter
ParameterType parameter
Definition: kmeans_distributed.h:483
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::checkFinalizeComputeParams
services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:257
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >
Computes the results of the K-Means algorithm in the second step of the distributed processing mode...
Definition: kmeans_distributed.h:332
daal::algorithms::kmeans::interface1::DistributedContainer
class DAAL_EXPORT DistributedContainer
Provides methods to run implementations of the K-Means algorithm. This class is associated with the d...
Definition: kmeans_distributed.h:81
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::input
InputType input
Definition: kmeans_distributed.h:307
daal::step1Local
Definition: daal_defines.h:143
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getMethod
virtual int getMethod() const DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:367
daal::step2Master
Definition: daal_defines.h:144
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::setResult
services::Status setResult(const ResultPtr &result)
Definition: kmeans_distributed.h:382
daal::algorithms::kmeans::interface1::Input::get
data_management::NumericTablePtr get(InputId id) const
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::Distributed
Distributed(size_t nClusters, bool assignFlag=false)
Definition: kmeans_distributed.h:188
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >
Computes the results of the K-Means algorithm in the first step of the distributed processing mode...
Definition: kmeans_distributed.h:175
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::Distributed
Distributed(size_t nClusters, size_t nIterations=1)
Definition: kmeans_distributed.h:345
daal::services::ErrorNullResult
Definition: error_indexes.h:122
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::setPartialResult
services::Status setPartialResult(const PartialResultPtr &partialRes)
Definition: kmeans_distributed.h:246

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