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

kmeans_distributed.h
1 /* file: kmeans_distributed.h */
2 /*******************************************************************************
3 * Copyright 2014-2019 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 the interface for the K-Means algorithm in the distributed
19 // processing mode
20 //--
21 */
22 
23 #ifndef __KMEANS_DISTRIBITED_H__
24 #define __KMEANS_DISTRIBITED_H__
25 
26 #include "algorithms/algorithm.h"
27 #include "data_management/data/numeric_table.h"
28 #include "services/daal_defines.h"
29 #include "algorithms/kmeans/kmeans_types.h"
30 
31 namespace daal
32 {
33 namespace algorithms
34 {
35 namespace kmeans
36 {
37 
38 namespace interface1
39 {
54 template<ComputeStep step, typename algorithmFPType, Method method, CpuType cpu>
55 class DAAL_EXPORT DistributedContainer;
56 
61 template<typename algorithmFPType, Method method, CpuType cpu>
62 class DAAL_EXPORT DistributedContainer<step1Local, algorithmFPType, method, cpu> : public
63  daal::algorithms::AnalysisContainerIface<distributed>
64 {
65 public:
71  DistributedContainer(daal::services::Environment::env *daalEnv);
73  virtual ~DistributedContainer();
78  virtual services::Status compute() DAAL_C11_OVERRIDE;
83  virtual services::Status finalizeCompute() DAAL_C11_OVERRIDE;
84 };
85 
90 template<typename algorithmFPType, Method method, CpuType cpu>
91 class DAAL_EXPORT DistributedContainer<step2Master, algorithmFPType, method, cpu> : public
92  daal::algorithms::AnalysisContainerIface<distributed>
93 {
94 public:
100  DistributedContainer(daal::services::Environment::env *daalEnv);
102  virtual ~DistributedContainer();
107  virtual services::Status compute() DAAL_C11_OVERRIDE;
112  virtual services::Status finalizeCompute() DAAL_C11_OVERRIDE;
113 };
114 
132 template<ComputeStep step, typename algorithmFPType = DAAL_ALGORITHM_FP_TYPE, Method method = lloydDense>
133 class DAAL_EXPORT Distributed {};
134 
148 template<typename algorithmFPType, Method method>
149 class DAAL_EXPORT Distributed<step1Local, algorithmFPType, method> : public daal::algorithms::Analysis<distributed>
150 {
151 public:
152  typedef algorithms::kmeans::Input InputType;
153  typedef algorithms::kmeans::Parameter ParameterType;
154  typedef algorithms::kmeans::Result ResultType;
155  typedef algorithms::kmeans::PartialResult PartialResultType;
156 
162  Distributed(size_t nClusters, bool assignFlag = false) : parameter(nClusters, 1)
163  {
164  initialize();
165  parameter.assignFlag = assignFlag;
166  }
167 
174  Distributed(const Distributed<step1Local, algorithmFPType, method> &other) : parameter(other.parameter)
175  {
176  initialize();
177  input.set(data, other.input.get(data));
178  input.set(inputCentroids, other.input.get(inputCentroids));
179  }
180 
185  virtual int getMethod() const DAAL_C11_OVERRIDE { return(int) method; }
186 
191  ResultPtr getResult()
192  {
193  return _result;
194  }
195 
200  services::Status setResult(const ResultPtr& result)
201  {
202  DAAL_CHECK(result, services::ErrorNullResult)
203  _result = result;
204  _res = _result.get();
205  return services::Status();
206  }
207 
212  PartialResultPtr getPartialResult()
213  {
214  return _partialResult;
215  }
216 
220  services::Status setPartialResult(const PartialResultPtr& partialRes)
221  {
222  DAAL_CHECK(partialRes, services::ErrorNullPartialResult);
223  _partialResult = partialRes;
224  _pres = _partialResult.get();
225  return services::Status();
226  }
227 
231  services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
232  {
233  return services::Status();
234  }
235 
241  services::SharedPtr<Distributed<step1Local, algorithmFPType, method> > clone() const
242  {
243  return services::SharedPtr<Distributed<step1Local, algorithmFPType, method> >(cloneImpl());
244  }
245 
246 protected:
247  virtual Distributed<step1Local, algorithmFPType, method> * cloneImpl() const DAAL_C11_OVERRIDE
248  {
249  return new Distributed<step1Local, algorithmFPType, method>(*this);
250  }
251 
252  virtual services::Status allocateResult() DAAL_C11_OVERRIDE
253  {
254  _result.reset(new ResultType());
255  services::Status s = _result->allocate<algorithmFPType>(_in, _par, (int) method);
256  _res = _result.get();
257  return s;
258  }
259 
260  virtual services::Status allocatePartialResult() DAAL_C11_OVERRIDE
261  {
262  _partialResult.reset(new PartialResultType());
263  services::Status s = _partialResult->allocate<algorithmFPType>(&input, _par, (int) method);
264  _pres = _partialResult.get();
265  return s;
266  }
267 
268  virtual services::Status initializePartialResult() DAAL_C11_OVERRIDE
269  {
270  return services::Status();
271  }
272 
273  void initialize()
274  {
275  Analysis<distributed>::_ac = new __DAAL_ALGORITHM_CONTAINER(distributed, DistributedContainer, step1Local, algorithmFPType, method)(&_env);
276  _in = &input;
277  _par = &parameter;
278  }
279 
280 public:
281  InputType input;
282  ParameterType parameter;
284 private:
285  PartialResultPtr _partialResult;
286  ResultPtr _result;
287 };
288 
305 template<typename algorithmFPType, Method method>
306 class DAAL_EXPORT Distributed<step2Master, algorithmFPType, method>: public daal::algorithms::Analysis<distributed>
307 {
308 public:
309  typedef algorithms::kmeans::DistributedStep2MasterInput InputType;
310  typedef algorithms::kmeans::Parameter ParameterType;
311  typedef algorithms::kmeans::Result ResultType;
312  typedef algorithms::kmeans::PartialResult PartialResultType;
313 
319  Distributed(size_t nClusters, size_t nIterations = 1) : parameter(nClusters, nIterations)
320  {
321  initialize();
322  parameter.assignFlag = false;
323  }
324 
331  Distributed(const Distributed<step2Master, algorithmFPType, method> &other) : parameter(other.parameter)
332  {
333  initialize();
334  input.set(partialResults, other.input.get(partialResults));
335  }
336 
341  virtual int getMethod() const DAAL_C11_OVERRIDE { return(int) method; }
342 
347  ResultPtr getResult()
348  {
349  return _result;
350  }
351 
356  services::Status setResult(const ResultPtr& result)
357  {
358  DAAL_CHECK(result, services::ErrorNullResult)
359  _result = result;
360  _res = _result.get();
361  return services::Status();
362  }
363 
368  PartialResultPtr getPartialResult()
369  {
370  return _partialResult;
371  }
372 
376  services::Status setPartialResult(const PartialResultPtr& partialRes)
377  {
378  DAAL_CHECK(partialRes, services::ErrorNullPartialResult);
379  _partialResult = partialRes;
380  _pres = _partialResult.get();
381  return services::Status();
382  }
383 
387  services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
388  {
389  services::Status s;
390  if(_partialResult)
391  {
392  s |= _partialResult->check(_par, method);
393  if (!s) { return s; }
394  }
395  else
396  {
397  return services::Status(services::ErrorNullResult);
398  }
399 
400  if(_result)
401  {
402  s |= _result->check(_partialResult.get(), _par, method);
403  }
404  else
405  {
406  return services::Status(services::ErrorNullResult);
407  }
408  return s;
409  }
410 
416  services::SharedPtr<Distributed<step2Master, algorithmFPType, method> > clone() const
417  {
418  return services::SharedPtr<Distributed<step2Master, algorithmFPType, method> >(cloneImpl());
419  }
420 
421 protected:
422  virtual Distributed<step2Master, algorithmFPType, method> * cloneImpl() const DAAL_C11_OVERRIDE
423  {
424  return new Distributed<step2Master, algorithmFPType, method>(*this);
425  }
426 
427  virtual services::Status allocateResult() DAAL_C11_OVERRIDE
428  {
429  _result.reset(new ResultType());
430  services::Status s = _result->allocate<algorithmFPType>(_pres, _par, (int) method);
431  _res = _result.get();
432  return s;
433  }
434 
435  virtual services::Status allocatePartialResult() DAAL_C11_OVERRIDE
436  {
437  _partialResult.reset(new PartialResultType());
438  services::Status s = _partialResult->allocate<algorithmFPType>(&input, _par, (int) method);
439  _pres = _partialResult.get();
440  return s;
441  }
442 
443  virtual services::Status initializePartialResult() DAAL_C11_OVERRIDE
444  {
445  return services::Status();
446  }
447 
448  void initialize()
449  {
450  Analysis<distributed>::_ac = new __DAAL_ALGORITHM_CONTAINER(distributed, DistributedContainer, step2Master, algorithmFPType, method)(&_env);
451  _in = &input;
452  _par = &parameter;
453  }
454 
455 public:
456  InputType input;
457  ParameterType parameter;
459 private:
460  PartialResultPtr _partialResult;
461  ResultPtr _result;
462 };
464 } // namespace interface1
465 using interface1::DistributedContainer;
466 using interface1::Distributed;
467 } // namespace daal::algorithms::kmeans
468 } // namespace daal::algorithms
469 } // namespace daal
470 #endif
daal::algorithms::kmeans::interface1::Distributed
Computes the results of the K-Means algorithm in the distributed processing mode. ...
Definition: kmeans_distributed.h:133
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::Distributed
Distributed(const Distributed< step1Local, algorithmFPType, method > &other)
Definition: kmeans_distributed.h:174
daal
Definition: algorithm_base_common.h:31
daal::algorithms::AnalysisContainerIface
Abstract interface class that provides virtual methods to access and run implementations of the analy...
Definition: analysis.h:53
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::setResult
services::Status setResult(const ResultPtr &result)
Definition: kmeans_distributed.h:200
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getPartialResult
PartialResultPtr getPartialResult()
Definition: kmeans_distributed.h:212
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getMethod
virtual int getMethod() const DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:185
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::input
InputType input
Definition: kmeans_distributed.h:456
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:105
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getResult
ResultPtr getResult()
Definition: kmeans_distributed.h:191
daal::algorithms::kmeans::nIterations
Definition: kmeans_types.h:111
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getPartialResult
PartialResultPtr getPartialResult()
Definition: kmeans_distributed.h:368
daal::algorithms::kmeans::inputCentroids
Definition: kmeans_types.h:71
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::setPartialResult
services::Status setPartialResult(const PartialResultPtr &partialRes)
Definition: kmeans_distributed.h:376
daal_defines.h
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getResult
ResultPtr getResult()
Definition: kmeans_distributed.h:347
daal::algorithms::kmeans::init::interface2::Distributed
class DAAL_EXPORT Distributed
Computes initial clusters for the K-Means algorithm in the distributed processing mode...
Definition: kmeans_init_distributed.h:275
daal::distributed
Definition: daal_defines.h:111
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::clone
services::SharedPtr< Distributed< step2Master, algorithmFPType, method > > clone() const
Definition: kmeans_distributed.h:416
daal::algorithms::kmeans::data
Definition: kmeans_types.h:70
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::clone
services::SharedPtr< Distributed< step1Local, algorithmFPType, method > > clone() const
Definition: kmeans_distributed.h:241
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::checkFinalizeComputeParams
services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:387
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::Distributed
Distributed(const Distributed< step2Master, algorithmFPType, method > &other)
Definition: kmeans_distributed.h:331
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::parameter
ParameterType parameter
Definition: kmeans_distributed.h:282
daal::algorithms::Analysis
Provides methods for execution of operations over data, such as computation of Summary Statistics est...
Definition: analysis.h:68
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::parameter
ParameterType parameter
Definition: kmeans_distributed.h:457
daal::algorithms::kmeans::partialResults
Definition: kmeans_types.h:81
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::checkFinalizeComputeParams
services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:231
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:306
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:55
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::input
InputType input
Definition: kmeans_distributed.h:281
daal::step1Local
Definition: daal_defines.h:121
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getMethod
virtual int getMethod() const DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:341
daal::step2Master
Definition: daal_defines.h:122
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::setResult
services::Status setResult(const ResultPtr &result)
Definition: kmeans_distributed.h:356
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::Distributed
Distributed(size_t nClusters, bool assignFlag=false)
Definition: kmeans_distributed.h:162
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:149
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::Distributed
Distributed(size_t nClusters, size_t nIterations=1)
Definition: kmeans_distributed.h:319
daal::services::ErrorNullResult
Definition: error_indexes.h:96
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::setPartialResult
services::Status setPartialResult(const PartialResultPtr &partialRes)
Definition: kmeans_distributed.h:220

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