Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 2

svd_dense_distr.py

1 # file: svd_dense_distr.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation
4 # All Rights Reserved.
5 #
6 # If this software was obtained under the Intel Simplified Software License,
7 # the following terms apply:
8 #
9 # The source code, information and material ("Material") contained herein is
10 # owned by Intel Corporation or its suppliers or licensors, and title to such
11 # Material remains with Intel Corporation or its suppliers or licensors. The
12 # Material contains proprietary information of Intel or its suppliers and
13 # licensors. The Material is protected by worldwide copyright laws and treaty
14 # provisions. No part of the Material may be used, copied, reproduced,
15 # modified, published, uploaded, posted, transmitted, distributed or disclosed
16 # in any way without Intel's prior express written permission. No license under
17 # any patent, copyright or other intellectual property rights in the Material
18 # is granted to or conferred upon you, either expressly, by implication,
19 # inducement, estoppel or otherwise. Any license under such intellectual
20 # property rights must be express and approved by Intel in writing.
21 #
22 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 # notice or any other notice embedded in Materials by Intel or Intel's
24 # suppliers or licensors in any way.
25 #
26 #
27 # If this software was obtained under the Apache License, Version 2.0 (the
28 # "License"), the following terms apply:
29 #
30 # You may not use this file except in compliance with the License. You may
31 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 #
33 #
34 # Unless required by applicable law or agreed to in writing, software
35 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 #
38 # See the License for the specific language governing permissions and
39 # limitations under the License.
40 #===============================================================================
41 
42 
44 
45 import os
46 import sys
47 import numpy as np
48 
49 from daal import step1Local, step2Master, step3Local
50 from daal.algorithms import svd
51 from daal.data_management import FileDataSource, DataSourceIface
52 
53 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
54 if utils_folder not in sys.path:
55  sys.path.insert(0, utils_folder)
56 from utils import printNumericTable
57 
58 DAAL_PREFIX = os.path.join('..', 'data')
59 
60 # Input data set parameters
61 nBlocks = 4
62 
63 datasetFileNames = [
64  os.path.join(DAAL_PREFIX, 'distributed', 'svd_1.csv'),
65  os.path.join(DAAL_PREFIX, 'distributed', 'svd_2.csv'),
66  os.path.join(DAAL_PREFIX, 'distributed', 'svd_3.csv'),
67  os.path.join(DAAL_PREFIX, 'distributed', 'svd_4.csv')
68 ]
69 
70 dataFromStep1ForStep2 = [0] * nBlocks
71 dataFromStep1ForStep3 = [0] * nBlocks
72 dataFromStep2ForStep3 = [0] * nBlocks
73 Sigma = None
74 V = None
75 Ui = [0] * nBlocks
76 
77 
78 def computestep1Local(block):
79  global dataFromStep1ForStep2, dataFromStep1ForStep3
80 
81  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
82  dataSource = FileDataSource(
83  datasetFileNames[block],
84  DataSourceIface.doAllocateNumericTable,
85  DataSourceIface.doDictionaryFromContext
86  )
87 
88  # Retrieve the input data
89  dataSource.loadDataBlock()
90 
91  # Create an algorithm to compute SVD on the local node
92  algorithm = svd.Distributed(step1Local,fptype=np.float64)
93 
94  algorithm.input.set(svd.data, dataSource.getNumericTable())
95 
96  # Compute SVD and get OnlinePartialResult class from daal.algorithms.svd
97  pres = algorithm.compute()
98 
99  dataFromStep1ForStep2[block] = pres.get(svd.outputOfStep1ForStep2)
100  dataFromStep1ForStep3[block] = pres.get(svd.outputOfStep1ForStep3)
101 
102 
103 def computeOnMasterNode():
104  global Sigma, V, dataFromStep2ForStep3
105 
106  # Create an algorithm to compute SVD on the master node
107  algorithm = svd.Distributed(step2Master,fptype=np.float64)
108 
109  for i in range(nBlocks):
110  algorithm.input.add(svd.inputOfStep2FromStep1, i, dataFromStep1ForStep2[i])
111 
112  # Compute SVD and get DistributedPartialResult class from daal.algorithms.svd
113  pres = algorithm.compute()
114 
115  for i in range(nBlocks):
116  dataFromStep2ForStep3[i] = pres.getCollection(svd.outputOfStep2ForStep3, i)
117 
118  res = algorithm.finalizeCompute()
119 
120  Sigma = res.get(svd.singularValues)
121  V = res.get(svd.rightSingularMatrix)
122 
123 
124 def finalizeComputestep1Local(block):
125  global Ui
126 
127  # Create an algorithm to compute SVD on the master node
128  algorithm = svd.Distributed(step3Local,fptype=np.float64)
129 
130  algorithm.input.set(svd.inputOfStep3FromStep1, dataFromStep1ForStep3[block])
131  algorithm.input.set(svd.inputOfStep3FromStep2, dataFromStep2ForStep3[block])
132 
133  # Compute SVD
134  algorithm.compute()
135  res = algorithm.finalizeCompute()
136 
137  Ui[block] = res.get(svd.leftSingularMatrix)
138 
139 if __name__ == "__main__":
140 
141  for i in range(nBlocks):
142  computestep1Local(i)
143 
144  computeOnMasterNode()
145 
146  for i in range(nBlocks):
147  finalizeComputestep1Local(i)
148 
149  # Print the results
150  printNumericTable(Sigma, "Singular values:")
151  printNumericTable(V, "Right orthogonal matrix V:")
152  printNumericTable(Ui[0], "Part of left orthogonal matrix U from 1st node:", 10)

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