25 from daal
import step1Local, step2Master, step3Local
26 from daal.algorithms
import svd
27 from daal.data_management
import FileDataSource, DataSourceIface
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
34 DAAL_PREFIX = os.path.join(
'..',
'data')
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')
46 dataFromStep1ForStep2 = [0] * nBlocks
47 dataFromStep1ForStep3 = [0] * nBlocks
48 dataFromStep2ForStep3 = [0] * nBlocks
54 def computestep1Local(block):
55 global dataFromStep1ForStep2, dataFromStep1ForStep3
58 dataSource = FileDataSource(
59 datasetFileNames[block],
60 DataSourceIface.doAllocateNumericTable,
61 DataSourceIface.doDictionaryFromContext
65 dataSource.loadDataBlock()
68 algorithm = svd.Distributed(step1Local,fptype=np.float64)
70 algorithm.input.set(svd.data, dataSource.getNumericTable())
73 pres = algorithm.compute()
75 dataFromStep1ForStep2[block] = pres.get(svd.outputOfStep1ForStep2)
76 dataFromStep1ForStep3[block] = pres.get(svd.outputOfStep1ForStep3)
79 def computeOnMasterNode():
80 global Sigma, V, dataFromStep2ForStep3
83 algorithm = svd.Distributed(step2Master,fptype=np.float64)
85 for i
in range(nBlocks):
86 algorithm.input.add(svd.inputOfStep2FromStep1, i, dataFromStep1ForStep2[i])
89 pres = algorithm.compute()
91 for i
in range(nBlocks):
92 dataFromStep2ForStep3[i] = pres.getCollection(svd.outputOfStep2ForStep3, i)
94 res = algorithm.finalizeCompute()
96 Sigma = res.get(svd.singularValues)
97 V = res.get(svd.rightSingularMatrix)
100 def finalizeComputestep1Local(block):
104 algorithm = svd.Distributed(step3Local,fptype=np.float64)
106 algorithm.input.set(svd.inputOfStep3FromStep1, dataFromStep1ForStep3[block])
107 algorithm.input.set(svd.inputOfStep3FromStep2, dataFromStep2ForStep3[block])
111 res = algorithm.finalizeCompute()
113 Ui[block] = res.get(svd.leftSingularMatrix)
115 if __name__ ==
"__main__":
117 for i
in range(nBlocks):
120 computeOnMasterNode()
122 for i
in range(nBlocks):
123 finalizeComputestep1Local(i)
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)