Python* API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

set_number_of_threads.py

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

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