Python* API Reference for Intel® Data Analytics Acceleration Library 2019

enable_thread_pinning.py

1 # file: enable_thread_pinning.py
2 #===============================================================================
3 # Copyright 2014-2018 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 
19 import os
20 import sys
21 
22 import daal.algorithms.kmeans as kmeans
23 import daal.algorithms.kmeans.init as init
24 from daal.data_management import FileDataSource, DataSourceIface
25 from daal.services import Environment
26 
27 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
28 if utils_folder not in sys.path:
29  sys.path.insert(0, utils_folder)
30 from utils import printNumericTable
31 # Input data set parameters
32 datasetFileName = os.path.join('..', 'data', 'batch', 'kmeans_dense.csv')
33 
34 # K-Means algorithm parameters
35 nClusters = 20
36 nIterations = 5
37 nThreads = 2
38 nThreadsInit = None
39 nThreadsNew = None
40 
41 if __name__ == "__main__":
42 
43  # Initialize FileDataSource to retrieve the input data from a .csv file
44  dataSource = FileDataSource(
45  datasetFileName, DataSourceIface.doAllocateNumericTable,
46  DataSourceIface.doDictionaryFromContext
47  )
48 
49  # Retrieve the data from the input file
50  dataSource.loadDataBlock()
51 
52  # Get initial clusters for the K-Means algorithm
53  initAlg = kmeans.init.Batch(nClusters)
54 
55  initAlg.input.set(kmeans.init.data, dataSource.getNumericTable())
56 
57  # Enables thread pinning for next algorithm runs
58  Environment.getInstance().enableThreadPinning(True)
59 
60  res = initAlg.compute()
61 
62  # Disables thread pinning for next algorithm runs
63  Environment.getInstance().enableThreadPinning(False)
64 
65  centroids = res.get(kmeans.init.centroids)
66 
67  # Create an algorithm object for the K-Means algorithm
68  algorithm = kmeans.Batch(nClusters, nIterations)
69 
70  algorithm.input.set(kmeans.data, dataSource.getNumericTable())
71  algorithm.input.set(kmeans.inputCentroids, centroids)
72 
73  # Run computations
74  unused_result = algorithm.compute()
75 
76  printNumericTable(unused_result.get(kmeans.assignments), "First 10 cluster assignments:", 10);
77  printNumericTable(unused_result.get(kmeans.centroids), "First 10 dimensions of centroids:", 20, 10);
78  printNumericTable(unused_result.get(kmeans.objectiveFunction), "Objective function value:");

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