47 from daal
import step1Local, step2Master, step3Local
48 from daal.algorithms
import svd
49 from daal.data_management
import FileDataSource, DataSourceIface
51 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
52 if utils_folder
not in sys.path:
53 sys.path.insert(0, utils_folder)
54 from utils
import printNumericTable
56 DAAL_PREFIX = os.path.join(
'..',
'data')
62 os.path.join(DAAL_PREFIX,
'distributed',
'svd_1.csv'),
63 os.path.join(DAAL_PREFIX,
'distributed',
'svd_2.csv'),
64 os.path.join(DAAL_PREFIX,
'distributed',
'svd_3.csv'),
65 os.path.join(DAAL_PREFIX,
'distributed',
'svd_4.csv')
68 dataFromStep1ForStep2 = [0] * nBlocks
69 dataFromStep1ForStep3 = [0] * nBlocks
70 dataFromStep2ForStep3 = [0] * nBlocks
76 def computestep1Local(block):
77 global dataFromStep1ForStep2, dataFromStep1ForStep3
80 dataSource = FileDataSource(
81 datasetFileNames[block],
82 DataSourceIface.doAllocateNumericTable,
83 DataSourceIface.doDictionaryFromContext
87 dataSource.loadDataBlock()
90 algorithm = svd.Distributed(step1Local)
92 algorithm.input.set(svd.data, dataSource.getNumericTable())
95 pres = algorithm.compute()
97 dataFromStep1ForStep2[block] = pres.get(svd.outputOfStep1ForStep2)
98 dataFromStep1ForStep3[block] = pres.get(svd.outputOfStep1ForStep3)
101 def computeOnMasterNode():
102 global Sigma, V, dataFromStep2ForStep3
105 algorithm = svd.Distributed(step2Master)
107 for i
in range(nBlocks):
108 algorithm.input.add(svd.inputOfStep2FromStep1, i, dataFromStep1ForStep2[i])
111 pres = algorithm.compute()
113 for i
in range(nBlocks):
114 dataFromStep2ForStep3[i] = pres.getCollection(svd.outputOfStep2ForStep3, i)
116 res = algorithm.finalizeCompute()
118 Sigma = res.get(svd.singularValues)
119 V = res.get(svd.rightSingularMatrix)
122 def finalizeComputestep1Local(block):
126 algorithm = svd.Distributed(step3Local)
128 algorithm.input.set(svd.inputOfStep3FromStep1, dataFromStep1ForStep3[block])
129 algorithm.input.set(svd.inputOfStep3FromStep2, dataFromStep2ForStep3[block])
133 res = algorithm.finalizeCompute()
135 Ui[block] = res.get(svd.leftSingularMatrix)
137 if __name__ ==
"__main__":
139 for i
in range(nBlocks):
142 computeOnMasterNode()
144 for i
in range(nBlocks):
145 finalizeComputestep1Local(i)
148 printNumericTable(Sigma,
"Singular values:")
149 printNumericTable(V,
"Right orthogonal matrix V:")
150 printNumericTable(Ui[0],
"Part of left orthogonal matrix U from 1st node:", 10)