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

kmeans_distributed.h
1 /* file: kmeans_distributed.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 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:
183  Distributed(size_t nClusters, bool assignFlag = false) : parameter(nClusters, 1)
184  {
185  initialize();
186  parameter.assignFlag = assignFlag;
187  }
188 
195  Distributed(const Distributed<step1Local, algorithmFPType, method> &other) : parameter(other.parameter)
196  {
197  initialize();
198  input.set(data, other.input.get(data));
199  input.set(inputCentroids, other.input.get(inputCentroids));
200  }
201 
206  virtual int getMethod() const DAAL_C11_OVERRIDE { return(int) method; }
207 
212  ResultPtr getResult()
213  {
214  return _result;
215  }
216 
221  services::Status setResult(const ResultPtr& result)
222  {
223  DAAL_CHECK(result, services::ErrorNullResult)
224  _result = result;
225  _res = _result.get();
226  return services::Status();
227  }
228 
233  PartialResultPtr getPartialResult()
234  {
235  return _partialResult;
236  }
237 
241  services::Status setPartialResult(const PartialResultPtr& partialRes)
242  {
243  DAAL_CHECK(partialRes, services::ErrorNullPartialResult);
244  _partialResult = partialRes;
245  _pres = _partialResult.get();
246  return services::Status();
247  }
248 
252  services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
253  {
254  return services::Status();
255  }
256 
262  services::SharedPtr<Distributed<step1Local, algorithmFPType, method> > clone() const
263  {
264  return services::SharedPtr<Distributed<step1Local, algorithmFPType, method> >(cloneImpl());
265  }
266 
267 protected:
268  virtual Distributed<step1Local, algorithmFPType, method> * cloneImpl() const DAAL_C11_OVERRIDE
269  {
270  return new Distributed<step1Local, algorithmFPType, method>(*this);
271  }
272 
273  virtual services::Status allocateResult() DAAL_C11_OVERRIDE
274  {
275  _result.reset(new Result());
276  services::Status s = _result->allocate<algorithmFPType>(_in, _par, (int) method);
277  _res = _result.get();
278  return s;
279  }
280 
281  virtual services::Status allocatePartialResult() DAAL_C11_OVERRIDE
282  {
283  _partialResult.reset(new PartialResult());
284  services::Status s = _partialResult->allocate<algorithmFPType>(&input, _par, (int) method);
285  _pres = _partialResult.get();
286  return s;
287  }
288 
289  virtual services::Status initializePartialResult() DAAL_C11_OVERRIDE
290  {
291  return services::Status();
292  }
293 
294  void initialize()
295  {
296  Analysis<distributed>::_ac = new __DAAL_ALGORITHM_CONTAINER(distributed, DistributedContainer, step1Local, algorithmFPType, method)(&_env);
297  _in = &input;
298  _par = &parameter;
299  }
300 
301 public:
302  Input input;
303  Parameter parameter;
305 private:
306  PartialResultPtr _partialResult;
307  ResultPtr _result;
308 };
309 
326 template<typename algorithmFPType, Method method>
327 class DAAL_EXPORT Distributed<step2Master, algorithmFPType, method>: public daal::algorithms::Analysis<distributed>
328 {
329 public:
335  Distributed(size_t nClusters, size_t nIterations = 1) : parameter(nClusters, nIterations)
336  {
337  initialize();
338  parameter.assignFlag = false;
339  }
340 
347  Distributed(const Distributed<step2Master, algorithmFPType, method> &other) : parameter(other.parameter)
348  {
349  initialize();
350  input.set(partialResults, other.input.get(partialResults));
351  }
352 
357  virtual int getMethod() const DAAL_C11_OVERRIDE { return(int) method; }
358 
363  ResultPtr getResult()
364  {
365  return _result;
366  }
367 
372  services::Status setResult(const ResultPtr& result)
373  {
374  DAAL_CHECK(result, services::ErrorNullResult)
375  _result = result;
376  _res = _result.get();
377  return services::Status();
378  }
379 
384  PartialResultPtr getPartialResult()
385  {
386  return _partialResult;
387  }
388 
392  services::Status setPartialResult(const PartialResultPtr& partialRes)
393  {
394  DAAL_CHECK(partialRes, services::ErrorNullPartialResult);
395  _partialResult = partialRes;
396  _pres = _partialResult.get();
397  return services::Status();
398  }
399 
403  services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
404  {
405  services::Status s;
406  if(_partialResult)
407  {
408  s |= _partialResult->check(_par, method);
409  if (!s) { return s; }
410  }
411  else
412  {
413  return services::Status(services::ErrorNullResult);
414  }
415 
416  if(_result)
417  {
418  s |= _result->check(_partialResult.get(), _par, method);
419  }
420  else
421  {
422  return services::Status(services::ErrorNullResult);
423  }
424  return s;
425  }
426 
432  services::SharedPtr<Distributed<step2Master, algorithmFPType, method> > clone() const
433  {
434  return services::SharedPtr<Distributed<step2Master, algorithmFPType, method> >(cloneImpl());
435  }
436 
437 protected:
438  virtual Distributed<step2Master, algorithmFPType, method> * cloneImpl() const DAAL_C11_OVERRIDE
439  {
440  return new Distributed<step2Master, algorithmFPType, method>(*this);
441  }
442 
443  virtual services::Status allocateResult() DAAL_C11_OVERRIDE
444  {
445  _result.reset(new Result());
446  services::Status s = _result->allocate<algorithmFPType>(_pres, _par, (int) method);
447  _res = _result.get();
448  return s;
449  }
450 
451  virtual services::Status allocatePartialResult() DAAL_C11_OVERRIDE
452  {
453  _partialResult.reset(new PartialResult());
454  services::Status s = _partialResult->allocate<algorithmFPType>(&input, _par, (int) method);
455  _pres = _partialResult.get();
456  return s;
457  }
458 
459  virtual services::Status initializePartialResult() DAAL_C11_OVERRIDE
460  {
461  return services::Status();
462  }
463 
464  void initialize()
465  {
466  Analysis<distributed>::_ac = new __DAAL_ALGORITHM_CONTAINER(distributed, DistributedContainer, step2Master, algorithmFPType, method)(&_env);
467  _in = &input;
468  _par = &parameter;
469  }
470 
471 public:
472  DistributedStep2MasterInput input;
473  Parameter parameter;
475 private:
476  PartialResultPtr _partialResult;
477  ResultPtr _result;
478 };
480 } // namespace interface1
481 using interface1::DistributedContainer;
482 using interface1::Distributed;
483 } // namespace daal::algorithms::kmeans
484 } // namespace daal::algorithms
485 } // namespace daal
486 #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:153
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::Distributed
Distributed(const Distributed< step1Local, algorithmFPType, method > &other)
Definition: kmeans_distributed.h:195
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:221
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getPartialResult
PartialResultPtr getPartialResult()
Definition: kmeans_distributed.h:233
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getMethod
virtual int getMethod() const DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:206
daal::services::ErrorNullPartialResult
Definition: error_indexes.h:131
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::getResult
ResultPtr getResult()
Definition: kmeans_distributed.h:212
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::input
DistributedStep2MasterInput input
Definition: kmeans_distributed.h:472
daal::algorithms::kmeans::nIterations
Definition: kmeans_types.h:135
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getPartialResult
PartialResultPtr getPartialResult()
Definition: kmeans_distributed.h:384
daal::algorithms::kmeans::inputCentroids
Definition: kmeans_types.h:97
daal::algorithms::kmeans::interface1::DistributedStep2MasterInput
Input objects for the K-Means algorithm in the distributed processing mode
Definition: kmeans_types.h:371
daal::algorithms::kmeans::interface1::Input
Input objects for the K-Means algorithm
Definition: kmeans_types.h:195
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::setPartialResult
services::Status setPartialResult(const PartialResultPtr &partialRes)
Definition: kmeans_distributed.h:392
daal_defines.h
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getResult
ResultPtr getResult()
Definition: kmeans_distributed.h:363
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:132
daal::algorithms::kmeans::interface1::Input::get
data_management::NumericTablePtr get(InputId id) const
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::clone
services::SharedPtr< Distributed< step2Master, algorithmFPType, method > > clone() const
Definition: kmeans_distributed.h:432
daal::algorithms::kmeans::data
Definition: kmeans_types.h:96
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::clone
services::SharedPtr< Distributed< step1Local, algorithmFPType, method > > clone() const
Definition: kmeans_distributed.h:262
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::parameter
Parameter parameter
Definition: kmeans_distributed.h:303
daal::algorithms::kmeans::interface1::DistributedStep2MasterInput::get
data_management::DataCollectionPtr get(MasterInputId id) const
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::checkFinalizeComputeParams
services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:403
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::Distributed
Distributed(const Distributed< step2Master, algorithmFPType, method > &other)
Definition: kmeans_distributed.h:347
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::partialResults
Definition: kmeans_types.h:107
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::checkFinalizeComputeParams
services::Status checkFinalizeComputeParams() DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:252
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:327
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::step1Local
Definition: daal_defines.h:142
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::getMethod
virtual int getMethod() const DAAL_C11_OVERRIDE
Definition: kmeans_distributed.h:357
daal::step2Master
Definition: daal_defines.h:143
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::input
Input input
Definition: kmeans_distributed.h:302
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::setResult
services::Status setResult(const ResultPtr &result)
Definition: kmeans_distributed.h:372
daal::algorithms::kmeans::interface1::Distributed< step2Master, algorithmFPType, method >::parameter
Parameter parameter
Definition: kmeans_distributed.h:473
daal::algorithms::kmeans::interface1::Distributed< step1Local, algorithmFPType, method >::Distributed
Distributed(size_t nClusters, bool assignFlag=false)
Definition: kmeans_distributed.h:183
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:335
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:241

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