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

kmeans_distributed.h
1 /* file: kmeans_distributed.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 the interface for K-Means algorithm in the distributed
21 // processing mode
22 //--
23 */
24 
25 #ifndef __KMEANS_DISTRIBITED_H__
26 #define __KMEANS_DISTRIBITED_H__
27 
28 #include "algorithms/algorithm.h"
29 #include "data_management/data/numeric_table.h"
30 #include "services/daal_defines.h"
31 #include "algorithms/kmeans/kmeans_types.h"
32 
33 namespace daal
34 {
35 namespace algorithms
36 {
37 namespace kmeans
38 {
39 
40 namespace interface1
41 {
56 template<ComputeStep step, typename algorithmFPType, Method method, CpuType cpu>
57 class DistributedContainer;
58 
63 template<typename algorithmFPType, Method method, CpuType cpu>
64 class DistributedContainer<step1Local, algorithmFPType, method, cpu> : public
65  daal::algorithms::AnalysisContainerIface<distributed>
66 {
67 public:
73  DistributedContainer(daal::services::Environment::env *daalEnv);
75  virtual ~DistributedContainer();
80  virtual services::Status compute() DAAL_C11_OVERRIDE;
85  virtual services::Status finalizeCompute() DAAL_C11_OVERRIDE;
86 };
87 
92 template<typename algorithmFPType, Method method, CpuType cpu>
93 class DistributedContainer<step2Master, algorithmFPType, method, cpu> : public
94  daal::algorithms::AnalysisContainerIface<distributed>
95 {
96 public:
102  DistributedContainer(daal::services::Environment::env *daalEnv);
104  virtual ~DistributedContainer();
109  virtual services::Status compute() DAAL_C11_OVERRIDE;
114  virtual services::Status finalizeCompute() DAAL_C11_OVERRIDE;
115 };
116 
134 template<ComputeStep step, typename algorithmFPType = DAAL_ALGORITHM_FP_TYPE, Method method = lloydDense>
135 class DAAL_EXPORT Distributed {};
136 
150 template<typename algorithmFPType, Method method>
151 class DAAL_EXPORT Distributed<step1Local, algorithmFPType, method> : public daal::algorithms::Analysis<distributed>
152 {
153 public:
154  typedef algorithms::kmeans::Input InputType;
155  typedef algorithms::kmeans::Parameter ParameterType;
156  typedef algorithms::kmeans::Result ResultType;
157  typedef algorithms::kmeans::PartialResult PartialResultType;
158 
164  Distributed(size_t nClusters, bool assignFlag = false) : parameter(nClusters, 1)
165  {
166  initialize();
167  parameter.assignFlag = assignFlag;
168  }
169 
176  Distributed(const Distributed<step1Local, algorithmFPType, method> &other) : parameter(other.parameter)
177  {
178  initialize();
179  input.set(data, other.input.get(data));
180  input.set(inputCentroids, other.input.get(inputCentroids));
181  }
182 
187  virtual int getMethod() const DAAL_C11_OVERRIDE { return(int) method; }
188 
193  ResultPtr getResult()
194  {
195  return _result;
196  }
197 
202  services::Status setResult(const ResultPtr& result)
203  {
204  DAAL_CHECK(result, services::ErrorNullResult)
205  _result = result;
206  _res = _result.get();
207  return services::Status();
208  }
209 
214  PartialResultPtr getPartialResult()
215  {
216  return _partialResult;
217  }
218 
222  services::Status setPartialResult(const PartialResultPtr& partialRes)
223  {
224  DAAL_CHECK(partialRes, services::ErrorNullPartialResult);
225  _partialResult = partialRes;
226  _pres = _partialResult.get();
227  return services::Status();
228  }
229 
233  services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
234  {
235  return services::Status();
236  }
237 
243  services::SharedPtr<Distributed<step1Local, algorithmFPType, method> > clone() const
244  {
245  return services::SharedPtr<Distributed<step1Local, algorithmFPType, method> >(cloneImpl());
246  }
247 
248 protected:
249  virtual Distributed<step1Local, algorithmFPType, method> * cloneImpl() const DAAL_C11_OVERRIDE
250  {
251  return new Distributed<step1Local, algorithmFPType, method>(*this);
252  }
253 
254  virtual services::Status allocateResult() DAAL_C11_OVERRIDE
255  {
256  _result.reset(new ResultType());
257  services::Status s = _result->allocate<algorithmFPType>(_in, _par, (int) method);
258  _res = _result.get();
259  return s;
260  }
261 
262  virtual services::Status allocatePartialResult() DAAL_C11_OVERRIDE
263  {
264  _partialResult.reset(new PartialResultType());
265  services::Status s = _partialResult->allocate<algorithmFPType>(&input, _par, (int) method);
266  _pres = _partialResult.get();
267  return s;
268  }
269 
270  virtual services::Status initializePartialResult() DAAL_C11_OVERRIDE
271  {
272  return services::Status();
273  }
274 
275  void initialize()
276  {
277  Analysis<distributed>::_ac = new __DAAL_ALGORITHM_CONTAINER(distributed, DistributedContainer, step1Local, algorithmFPType, method)(&_env);
278  _in = &input;
279  _par = &parameter;
280  }
281 
282 public:
283  InputType input;
284  ParameterType parameter;
286 private:
287  PartialResultPtr _partialResult;
288  ResultPtr _result;
289 };
290 
307 template<typename algorithmFPType, Method method>
308 class DAAL_EXPORT Distributed<step2Master, algorithmFPType, method>: public daal::algorithms::Analysis<distributed>
309 {
310 public:
311  typedef algorithms::kmeans::DistributedStep2MasterInput InputType;
312  typedef algorithms::kmeans::Parameter ParameterType;
313  typedef algorithms::kmeans::Result ResultType;
314  typedef algorithms::kmeans::PartialResult PartialResultType;
315 
321  Distributed(size_t nClusters, size_t nIterations = 1) : parameter(nClusters, nIterations)
322  {
323  initialize();
324  parameter.assignFlag = false;
325  }
326 
333  Distributed(const Distributed<step2Master, algorithmFPType, method> &other) : parameter(other.parameter)
334  {
335  initialize();
336  input.set(partialResults, other.input.get(partialResults));
337  }
338 
343  virtual int getMethod() const DAAL_C11_OVERRIDE { return(int) method; }
344 
349  ResultPtr getResult()
350  {
351  return _result;
352  }
353 
358  services::Status setResult(const ResultPtr& result)
359  {
360  DAAL_CHECK(result, services::ErrorNullResult)
361  _result = result;
362  _res = _result.get();
363  return services::Status();
364  }
365 
370  PartialResultPtr getPartialResult()
371  {
372  return _partialResult;
373  }
374 
378  services::Status setPartialResult(const PartialResultPtr& partialRes)
379  {
380  DAAL_CHECK(partialRes, services::ErrorNullPartialResult);
381  _partialResult = partialRes;
382  _pres = _partialResult.get();
383  return services::Status();
384  }
385 
389  services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
390  {
391  services::Status s;
392  if(_partialResult)
393  {
394  s |= _partialResult->check(_par, method);
395  if (!s) { return s; }
396  }
397  else
398  {
399  return services::Status(services::ErrorNullResult);
400  }
401 
402  if(_result)
403  {
404  s |= _result->check(_partialResult.get(), _par, method);
405  }
406  else
407  {
408  return services::Status(services::ErrorNullResult);
409  }
410  return s;
411  }
412 
418  services::SharedPtr<Distributed<step2Master, algorithmFPType, method> > clone() const
419  {
420  return services::SharedPtr<Distributed<step2Master, algorithmFPType, method> >(cloneImpl());
421  }
422 
423 protected:
424  virtual Distributed<step2Master, algorithmFPType, method> * cloneImpl() const DAAL_C11_OVERRIDE
425  {
426  return new Distributed<step2Master, algorithmFPType, method>(*this);
427  }
428 
429  virtual services::Status allocateResult() DAAL_C11_OVERRIDE
430  {
431  _result.reset(new ResultType());
432  services::Status s = _result->allocate<algorithmFPType>(_pres, _par, (int) method);
433  _res = _result.get();
434  return s;
435  }
436 
437  virtual services::Status allocatePartialResult() DAAL_C11_OVERRIDE
438  {
439  _partialResult.reset(new PartialResultType());
440  services::Status s = _partialResult->allocate<algorithmFPType>(&input, _par, (int) method);
441  _pres = _partialResult.get();
442  return s;
443  }
444 
445  virtual services::Status initializePartialResult() DAAL_C11_OVERRIDE
446  {
447  return services::Status();
448  }
449 
450  void initialize()
451  {
452  Analysis<distributed>::_ac = new __DAAL_ALGORITHM_CONTAINER(distributed, DistributedContainer, step2Master, algorithmFPType, method)(&_env);
453  _in = &input;
454  _par = &parameter;
455  }
456 
457 public:
458  InputType input;
459  ParameterType parameter;
461 private:
462  PartialResultPtr _partialResult;
463  ResultPtr _result;
464 };
466 } // namespace interface1
467 using interface1::DistributedContainer;
468 using interface1::Distributed;
469 } // namespace daal::algorithms::kmeans
470 } // namespace daal::algorithms
471 } // namespace daal
472 #endif
daal::step1Local
Definition: daal_defines.h:123
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::clone
services::SharedPtr< Distributed< step2Master, algorithmFPType, method > > clone() const
Definition: kmeans_distributed.h:418
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >
Computes the results of K-Means algorithm in the second step of the distributed processing mode...
Definition: kmeans_distributed.h:308
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >
Computes the results of K-Means algorithm in the first step of the distributed processing mode...
Definition: kmeans_distributed.h:151
daal::algorithms::kmeans::nIterations
Definition: kmeans_types.h:113
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getMethod
virtual int getMethod() const DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:343
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::input
InputType input
Definition: kmeans_distributed.h:458
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::Distributed
Distributed(size_t nClusters, size_t nIterations=1)
Definition: kmeans_distributed.h:321
daal::step2Master
Definition: daal_defines.h:124
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::setPartialResult
services::Status setPartialResult(const PartialResultPtr &partialRes)
Definition: kmeans_distributed.h:378
daal::algorithms::kmeans::data
Definition: kmeans_types.h:72
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::checkFinalizeComputeParams
services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:389
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::parameter
ParameterType parameter
Definition: kmeans_distributed.h:284
daal::services::ErrorNullResult
Definition: error_indexes.h:98
daal::algorithms::kmeans::interface1::DistributedContainer
Provides methods to run implementations of K-Means algorithm. This class is associated with the daal:...
Definition: kmeans_distributed.h:57
daal::algorithms::AnalysisContainerIface
Abstract interface class that provides virtual methods to access and run implementations of the analy...
Definition: analysis.h:55
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::setResult
services::Status setResult(const ResultPtr &result)
Definition: kmeans_distributed.h:358
daal::algorithms::kmeans::init::interface2::Distributed
class DAAL_EXPORT Distributed
Computes initial clusters for K-Means algorithm in the distributed processing mode.
Definition: kmeans_init_distributed.h:277
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getResult
ResultPtr getResult()
Definition: kmeans_distributed.h:349
daal_defines.h
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::setPartialResult
services::Status setPartialResult(const PartialResultPtr &partialRes)
Definition: kmeans_distributed.h:222
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::Distributed
Distributed(const Distributed< step1Local, algorithmFPType, method > &other)
Definition: kmeans_distributed.h:176
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::clone
services::SharedPtr< Distributed< step1Local, algorithmFPType, method > > clone() const
Definition: kmeans_distributed.h:243
daal::algorithms::kmeans::partialResults
Definition: kmeans_types.h:83
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::Distributed
Distributed(const Distributed< step2Master, algorithmFPType, method > &other)
Definition: kmeans_distributed.h:333
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getPartialResult
PartialResultPtr getPartialResult()
Definition: kmeans_distributed.h:370
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getMethod
virtual int getMethod() const DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:187
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getPartialResult
PartialResultPtr getPartialResult()
Definition: kmeans_distributed.h:214
daal::algorithms::kmeans::inputCentroids
Definition: kmeans_types.h:73
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::input
InputType input
Definition: kmeans_distributed.h:283
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getResult
ResultPtr getResult()
Definition: kmeans_distributed.h:193
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::Distributed
Distributed(size_t nClusters, bool assignFlag=false)
Definition: kmeans_distributed.h:164
daal::algorithms::Analysis
Provides methods for execution of operations over data, such as computation of Summary Statistics est...
Definition: analysis.h:70
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:107
daal::distributed
Definition: daal_defines.h:113
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::parameter
ParameterType parameter
Definition: kmeans_distributed.h:459
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::checkFinalizeComputeParams
services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:233
daal::algorithms::kmeans::interface1::Distributed
Computes the results of K-Means algorithm in the distributed processing mode.
Definition: kmeans_distributed.h:135
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::setResult
services::Status setResult(const ResultPtr &result)
Definition: kmeans_distributed.h:202

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