23 from daal
import step1Local, step2Master, step3Local
24 from daal.algorithms
import svd
25 from daal.data_management
import FileDataSource, DataSourceIface
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
32 DAAL_PREFIX = os.path.join(
'..',
'data')
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')
44 dataFromStep1ForStep2 = [0] * nBlocks
45 dataFromStep1ForStep3 = [0] * nBlocks
46 dataFromStep2ForStep3 = [0] * nBlocks
52 def computestep1Local(block):
53 global dataFromStep1ForStep2, dataFromStep1ForStep3
56 dataSource = FileDataSource(
57 datasetFileNames[block],
58 DataSourceIface.doAllocateNumericTable,
59 DataSourceIface.doDictionaryFromContext
63 dataSource.loadDataBlock()
66 algorithm = svd.Distributed(step1Local,fptype=np.float64)
68 algorithm.input.set(svd.data, dataSource.getNumericTable())
71 pres = algorithm.compute()
73 dataFromStep1ForStep2[block] = pres.get(svd.outputOfStep1ForStep2)
74 dataFromStep1ForStep3[block] = pres.get(svd.outputOfStep1ForStep3)
77 def computeOnMasterNode():
78 global Sigma, V, dataFromStep2ForStep3
81 algorithm = svd.Distributed(step2Master,fptype=np.float64)
83 for i
in range(nBlocks):
84 algorithm.input.add(svd.inputOfStep2FromStep1, i, dataFromStep1ForStep2[i])
87 pres = algorithm.compute()
89 for i
in range(nBlocks):
90 dataFromStep2ForStep3[i] = pres.getCollection(svd.outputOfStep2ForStep3, i)
92 res = algorithm.finalizeCompute()
94 Sigma = res.get(svd.singularValues)
95 V = res.get(svd.rightSingularMatrix)
98 def finalizeComputestep1Local(block):
102 algorithm = svd.Distributed(step3Local,fptype=np.float64)
104 algorithm.input.set(svd.inputOfStep3FromStep1, dataFromStep1ForStep3[block])
105 algorithm.input.set(svd.inputOfStep3FromStep2, dataFromStep2ForStep3[block])
109 res = algorithm.finalizeCompute()
111 Ui[block] = res.get(svd.leftSingularMatrix)
113 if __name__ ==
"__main__":
115 for i
in range(nBlocks):
118 computeOnMasterNode()
120 for i
in range(nBlocks):
121 finalizeComputestep1Local(i)
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)