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

svd_dense_distr.py

1 # file: svd_dense_distr.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 
17 
18 
19 import os
20 import sys
21 import numpy as np
22 
23 from daal import step1Local, step2Master, step3Local
24 from daal.algorithms import svd
25 from daal.data_management import FileDataSource, DataSourceIface
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 
32 DAAL_PREFIX = os.path.join('..', 'data')
33 
34 # Input data set parameters
35 nBlocks = 4
36 
37 datasetFileNames = [
38  os.path.join(DAAL_PREFIX, 'distributed', 'svd_1.csv'),
39  os.path.join(DAAL_PREFIX, 'distributed', 'svd_2.csv'),
40  os.path.join(DAAL_PREFIX, 'distributed', 'svd_3.csv'),
41  os.path.join(DAAL_PREFIX, 'distributed', 'svd_4.csv')
42 ]
43 
44 dataFromStep1ForStep2 = [0] * nBlocks
45 dataFromStep1ForStep3 = [0] * nBlocks
46 dataFromStep2ForStep3 = [0] * nBlocks
47 Sigma = None
48 V = None
49 Ui = [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 SVD on the local node
66  algorithm = svd.Distributed(step1Local,fptype=np.float64)
67 
68  algorithm.input.set(svd.data, dataSource.getNumericTable())
69 
70  # Compute SVD and get OnlinePartialResult class from daal.algorithms.svd
71  pres = algorithm.compute()
72 
73  dataFromStep1ForStep2[block] = pres.get(svd.outputOfStep1ForStep2)
74  dataFromStep1ForStep3[block] = pres.get(svd.outputOfStep1ForStep3)
75 
76 
77 def computeOnMasterNode():
78  global Sigma, V, dataFromStep2ForStep3
79 
80  # Create an algorithm to compute SVD on the master node
81  algorithm = svd.Distributed(step2Master,fptype=np.float64)
82 
83  for i in range(nBlocks):
84  algorithm.input.add(svd.inputOfStep2FromStep1, i, dataFromStep1ForStep2[i])
85 
86  # Compute SVD and get DistributedPartialResult class from daal.algorithms.svd
87  pres = algorithm.compute()
88 
89  for i in range(nBlocks):
90  dataFromStep2ForStep3[i] = pres.getCollection(svd.outputOfStep2ForStep3, i)
91 
92  res = algorithm.finalizeCompute()
93 
94  Sigma = res.get(svd.singularValues)
95  V = res.get(svd.rightSingularMatrix)
96 
97 
98 def finalizeComputestep1Local(block):
99  global Ui
100 
101  # Create an algorithm to compute SVD on the master node
102  algorithm = svd.Distributed(step3Local,fptype=np.float64)
103 
104  algorithm.input.set(svd.inputOfStep3FromStep1, dataFromStep1ForStep3[block])
105  algorithm.input.set(svd.inputOfStep3FromStep2, dataFromStep2ForStep3[block])
106 
107  # Compute SVD
108  algorithm.compute()
109  res = algorithm.finalizeCompute()
110 
111  Ui[block] = res.get(svd.leftSingularMatrix)
112 
113 if __name__ == "__main__":
114 
115  for i in range(nBlocks):
116  computestep1Local(i)
117 
118  computeOnMasterNode()
119 
120  for i in range(nBlocks):
121  finalizeComputestep1Local(i)
122 
123  # Print the results
124  printNumericTable(Sigma, "Singular values:")
125  printNumericTable(V, "Right orthogonal matrix V:")
126  printNumericTable(Ui[0], "Part of left orthogonal matrix U from 1st node:", 10)

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