Java* API Reference for Intel® Data Analytics Acceleration Library 2016 Update 4

QRDistributed.java

/* file: QRDistributed.java */
/*******************************************************************************
* Copyright 2014-2016 Intel Corporation All Rights Reserved.
*
* The source code, information and material ("Material") contained herein is
* owned by Intel Corporation or its suppliers or licensors, and title to such
* Material remains with Intel Corporation or its suppliers or licensors. The
* Material contains proprietary information of Intel or its suppliers and
* licensors. The Material is protected by worldwide copyright laws and treaty
* provisions. No part of the Material may be used, copied, reproduced,
* modified, published, uploaded, posted, transmitted, distributed or disclosed
* in any way without Intel's prior express written permission. No license under
* any patent, copyright or other intellectual property rights in the Material
* is granted to or conferred upon you, either expressly, by implication,
* inducement, estoppel or otherwise. Any license under such intellectual
* property rights must be express and approved by Intel in writing.
*
* Unless otherwise agreed by Intel in writing, you may not remove or alter this
* notice or any other notice embedded in Materials by Intel or Intel's
* suppliers or licensors in any way.
*******************************************************************************/
/*
// Content:
// Java example of computing QR decomposition in the distributed processing
// mode
*/
package com.intel.daal.examples.qr;
import com.intel.daal.algorithms.qr.*;
import com.intel.daal.data_management.data.DataCollection;
import com.intel.daal.data_management.data.KeyValueDataCollection;
import com.intel.daal.data_management.data.NumericTable;
import com.intel.daal.data_management.data_source.DataSource;
import com.intel.daal.data_management.data_source.FileDataSource;
import com.intel.daal.examples.utils.Service;
import com.intel.daal.services.DaalContext;
class QRDistributed {
/* Input data set parameters */
private static final String[] dataset = { "../data/distributed/qr_1.csv", "../data/distributed/qr_2.csv",
"../data/distributed/qr_3.csv", "../data/distributed/qr_4.csv" };
private static final int nNodes = dataset.length;
private static DataCollection[] dataFromStep1ForStep2 = new DataCollection[nNodes];
private static DataCollection[] dataFromStep1ForStep3 = new DataCollection[nNodes];
private static DataCollection[] dataFromStep2ForStep3 = new DataCollection[nNodes];
private static KeyValueDataCollection inputForStep3FromStep2;
private static NumericTable R;
private static NumericTable[] Qi = new NumericTable[nNodes];
private static DistributedStep1Local qrStep1Local;
private static DistributedStep2Master qrStep2Master;
private static DistributedStep3Local qrStep3Local;
private static DaalContext context = new DaalContext();
public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
for (int iNode = 0; iNode < nNodes; iNode++) {
computeStep1Local(iNode);
}
computeStep2Master();
for (int iNode = 0; iNode < nNodes; iNode++) {
computeStep3Local(iNode);
}
/* Print the results */
printResults();
context.dispose();
}
static void computeStep1Local(int block) {
/* Initialize FileDataSource to retrieve the input data from a .csv file */
FileDataSource dataSource = new FileDataSource(context, dataset[block],
DataSource.DictionaryCreationFlag.DoDictionaryFromContext,
DataSource.NumericTableAllocationFlag.DoAllocateNumericTable);
/* Retrieve the data from the input file */
dataSource.loadDataBlock();
NumericTable input = dataSource.getNumericTable();
/* Create an algorithm to compute QR decomposition on local nodes */
qrStep1Local = new DistributedStep1Local(context, Double.class, Method.defaultDense);
qrStep1Local.input.set(InputId.data, input);
/* Compute QR decomposition */
DistributedStep1LocalPartialResult pres = qrStep1Local.compute();
dataFromStep1ForStep2[block] = pres.get(PartialResultId.outputOfStep1ForStep2);
dataFromStep1ForStep3[block] = pres.get(PartialResultId.outputOfStep1ForStep3);
}
static void computeStep2Master() {
/* Create an algorithm to compute QR decomposition on the master node */
qrStep2Master = new DistributedStep2Master(context, Double.class, Method.defaultDense);
for (int iNode = 0; iNode < nNodes; iNode++) {
qrStep2Master.input.add(DistributedStep2MasterInputId.inputOfStep2FromStep1, iNode,
dataFromStep1ForStep2[iNode]);
}
/* Compute QR decomposition */
DistributedStep2MasterPartialResult pres = qrStep2Master.compute();
inputForStep3FromStep2 = pres.get(DistributedPartialResultCollectionId.outputOfStep2ForStep3);
for (int iNode = 0; iNode < nNodes; iNode++) {
dataFromStep2ForStep3[iNode] = (DataCollection)inputForStep3FromStep2.get(iNode);
}
Result result = qrStep2Master.finalizeCompute();
R = result.get(ResultId.matrixR);
}
private static void computeStep3Local(int block) {
/* Create an algorithm to compute QR decomposition on the master node */
qrStep3Local = new DistributedStep3Local(context, Double.class, Method.defaultDense);
qrStep3Local.input.set(DistributedStep3LocalInputId.inputOfStep3FromStep1, dataFromStep1ForStep3[block]);
qrStep3Local.input.set(DistributedStep3LocalInputId.inputOfStep3FromStep2, dataFromStep2ForStep3[block]);
/* Compute QR decomposition */
qrStep3Local.compute();
Result result = qrStep3Local.finalizeCompute();
Qi[block] = result.get(ResultId.matrixQ);
}
private static void printResults() {
Service.printNumericTable("Orthogonal matrix Q (10 first vectors):", Qi[0], 10);
Service.printNumericTable("Triangular matrix R:", R);
}
}