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

set_number_of_threads.py

Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.

Note: To find daal4py examples, refer to daal4py documentation or browse github repository.

1 # file: set_number_of_threads.py
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 ## <a name="DAAL-EXAMPLE-PY-SET_NUMBER_OF_THREADS"></a>
17 ## \example set_number_of_threads.py
18 
19 import os
20 
21 import daal.algorithms.kmeans as kmeans
22 import daal.algorithms.kmeans.init as init
23 from daal.data_management import FileDataSource, DataSourceIface
24 from daal.services import Environment
25 
26 # Input data set parameters
27 datasetFileName = os.path.join('..', 'data', 'batch', 'kmeans_dense.csv')
28 
29 # K-Means algorithm parameters
30 nClusters = 20
31 nIterations = 5
32 nThreads = 2
33 nThreadsInit = None
34 nThreadsNew = None
35 
36 if __name__ == "__main__":
37 
38  # Get the number of threads that is used by the library by default
39  nThreadsInit = Environment.getInstance().getNumberOfThreads()
40 
41  # Set the maximum number of threads to be used by the library
42  Environment.getInstance().setNumberOfThreads(nThreads)
43 
44  # Get the number of threads that is used by the library after changing
45  nThreadsNew = Environment.getInstance().getNumberOfThreads()
46 
47  # Initialize FileDataSource to retrieve the input data from a .csv file
48  dataSource = FileDataSource(
49  datasetFileName, DataSourceIface.doAllocateNumericTable,
50  DataSourceIface.doDictionaryFromContext
51  )
52 
53  # Retrieve the data from the input file
54  dataSource.loadDataBlock()
55 
56  # Get initial clusters for the K-Means algorithm
57  initAlg = init.Batch(nClusters)
58 
59  initAlg.input.set(init.data, dataSource.getNumericTable())
60  res = initAlg.compute()
61  centroids = res.get(init.centroids)
62 
63  # Create an algorithm object for the K-Means algorithm
64  algorithm = kmeans.Batch(nClusters, nIterations)
65 
66  algorithm.input.set(kmeans.data, dataSource.getNumericTable())
67  algorithm.input.set(kmeans.inputCentroids, centroids)
68 
69  # Run computations
70  unused_result = algorithm.compute()
71 
72  print("Initial number of threads: {}".format(nThreadsInit))
73  print("Number of threads to set: {}".format(nThreads))
74  print("Number of threads after setting: {}".format(nThreadsNew))

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