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

svd_dense_distr.py

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

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