49 from daal
import step1Local, step2Master, step3Local
50 from daal.algorithms
import svd
51 from daal.data_management
import FileDataSource, DataSourceIface
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
58 DAAL_PREFIX = os.path.join(
'..',
'data')
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')
70 dataFromStep1ForStep2 = [0] * nBlocks
71 dataFromStep1ForStep3 = [0] * nBlocks
72 dataFromStep2ForStep3 = [0] * nBlocks
78 def computestep1Local(block):
79 global dataFromStep1ForStep2, dataFromStep1ForStep3
82 dataSource = FileDataSource(
83 datasetFileNames[block],
84 DataSourceIface.doAllocateNumericTable,
85 DataSourceIface.doDictionaryFromContext
89 dataSource.loadDataBlock()
92 algorithm = svd.Distributed(step1Local,fptype=np.float64)
94 algorithm.input.set(svd.data, dataSource.getNumericTable())
97 pres = algorithm.compute()
99 dataFromStep1ForStep2[block] = pres.get(svd.outputOfStep1ForStep2)
100 dataFromStep1ForStep3[block] = pres.get(svd.outputOfStep1ForStep3)
103 def computeOnMasterNode():
104 global Sigma, V, dataFromStep2ForStep3
107 algorithm = svd.Distributed(step2Master,fptype=np.float64)
109 for i
in range(nBlocks):
110 algorithm.input.add(svd.inputOfStep2FromStep1, i, dataFromStep1ForStep2[i])
113 pres = algorithm.compute()
115 for i
in range(nBlocks):
116 dataFromStep2ForStep3[i] = pres.getCollection(svd.outputOfStep2ForStep3, i)
118 res = algorithm.finalizeCompute()
120 Sigma = res.get(svd.singularValues)
121 V = res.get(svd.rightSingularMatrix)
124 def finalizeComputestep1Local(block):
128 algorithm = svd.Distributed(step3Local,fptype=np.float64)
130 algorithm.input.set(svd.inputOfStep3FromStep1, dataFromStep1ForStep3[block])
131 algorithm.input.set(svd.inputOfStep3FromStep2, dataFromStep2ForStep3[block])
135 res = algorithm.finalizeCompute()
137 Ui[block] = res.get(svd.leftSingularMatrix)
139 if __name__ ==
"__main__":
141 for i
in range(nBlocks):
144 computeOnMasterNode()
146 for i
in range(nBlocks):
147 finalizeComputestep1Local(i)
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)