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

qr_dense_distr.py

1 # file: qr_dense_distr.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 import sys
23 
24 from daal import step1Local, step2Master, step3Local
25 from daal.algorithms import qr
26 from daal.data_management import FileDataSource, DataSourceIface
27 
28 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
29 if utils_folder not in sys.path:
30  sys.path.insert(0, utils_folder)
31 from utils import printNumericTable
32 
33 DAAL_PREFIX = os.path.join('..', 'data')
34 
35 # Input data set parameters
36 nBlocks = 4
37 
38 datasetFileNames = [
39  os.path.join(DAAL_PREFIX, 'distributed', 'qr_1.csv'),
40  os.path.join(DAAL_PREFIX, 'distributed', 'qr_2.csv'),
41  os.path.join(DAAL_PREFIX, 'distributed', 'qr_3.csv'),
42  os.path.join(DAAL_PREFIX, 'distributed', 'qr_4.csv')
43 ]
44 
45 dataFromStep1ForStep2 = [0] * nBlocks
46 dataFromStep1ForStep3 = [0] * nBlocks
47 dataFromStep2ForStep3 = [0] * nBlocks
48 R = None
49 Qi = [0] * nBlocks
50 
51 
52 def computestep1Local(block):
53  global dataFromStep1ForStep2, dataFromStep1ForStep3
54 
55  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
56  dataSource = FileDataSource(
57  datasetFileNames[block],
58  DataSourceIface.doAllocateNumericTable,
59  DataSourceIface.doDictionaryFromContext
60  )
61 
62  # Retrieve the input data
63  dataSource.loadDataBlock()
64 
65  # Create an algorithm to compute QR decomposition on the local node
66  algorithm = qr.Distributed(step1Local)
67 
68  algorithm.input.set(qr.data, dataSource.getNumericTable())
69 
70  # Compute QR decomposition and get OnlinePartialResult class from daal.algorithms.qr
71  pres = algorithm.compute()
72 
73  dataFromStep1ForStep2[block] = pres.get(qr.outputOfStep1ForStep2)
74  dataFromStep1ForStep3[block] = pres.get(qr.outputOfStep1ForStep3)
75 
76 
77 def computeOnMasterNode():
78  global R, dataFromStep2ForStep3
79 
80  # Create an algorithm to compute QR decomposition on the master node
81  algorithm = qr.Distributed(step2Master)
82 
83  for i in range(nBlocks):
84  algorithm.input.add(qr.inputOfStep2FromStep1, i, dataFromStep1ForStep2[i])
85 
86  # Compute QR decomposition and get DistributedPartialResult class from daal.algorithms.qr
87  pres = algorithm.compute()
88 
89  for i in range(nBlocks):
90  dataFromStep2ForStep3[i] = pres.getCollection(qr.outputOfStep2ForStep3, i)
91 
92  res = algorithm.finalizeCompute()
93  R = res.get(qr.matrixR)
94 
95 
96 def finalizeComputestep1Local(block):
97  global Qi
98 
99  # Create an algorithm to compute QR decomposition on the master node
100  algorithm = qr.Distributed(step3Local)
101 
102  algorithm.input.set(qr.inputOfStep3FromStep1, dataFromStep1ForStep3[block])
103  algorithm.input.set(qr.inputOfStep3FromStep2, dataFromStep2ForStep3[block])
104 
105  # Compute QR decomposition
106  algorithm.compute()
107 
108  res = algorithm.finalizeCompute()
109 
110  Qi[block] = res.get(qr.matrixQ)
111 
112 if __name__ == "__main__":
113 
114  for i in range(nBlocks):
115  computestep1Local(i)
116 
117  computeOnMasterNode()
118 
119  for i in range(nBlocks):
120  finalizeComputestep1Local(i)
121 
122  # Print the results
123  printNumericTable(Qi[0], "Part of orthogonal matrix Q from 1st node:", 10)
124  printNumericTable(R, "Triangular matrix R:")

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