C++ API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

pca_svd_distributed_mpi.cpp

/* file: pca_svd_distributed_mpi.cpp */
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation.
*
* This software and the related documents are Intel copyrighted materials, and
* your use of them is governed by the express license under which they were
* provided to you (License). Unless the License provides otherwise, you may not
* use, modify, copy, publish, distribute, disclose or transmit this software or
* the related documents without Intel's prior written permission.
*
* This software and the related documents are provided as is, with no express
* or implied warranties, other than those that are expressly stated in the
* License.
*
* License:
* http://software.intel.com/en-us/articles/intel-sample-source-code-license-agr
* eement/
*******************************************************************************/
/*
! Content:
! C++ sample of principal component analysis (PCA) using the singular value
! decomposition (SVD) method in the distributed processing mode
!
!******************************************************************************/
#include <mpi.h>
#include "daal.h"
#include "service.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms;
typedef float algorithmFPType; /* Algorithm floating-point type */
/* Input data set parameters */
const size_t nBlocks = 4;
size_t nFeatures;
int rankId, comm_size;
#define mpi_root 0
const string datasetFileNames[] =
{
"./data/distributed/pca_normalized_1.csv", "./data/distributed/pca_normalized_2.csv",
"./data/distributed/pca_normalized_3.csv", "./data/distributed/pca_normalized_4.csv"
};
int main(int argc, char *argv[])
{
checkArguments(argc, argv, 4, &datasetFileNames[0], &datasetFileNames[1], &datasetFileNames[2], &datasetFileNames[3]);
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
MPI_Comm_rank(MPI_COMM_WORLD, &rankId);
/* Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file */
FileDataSource<CSVFeatureManager> dataSource(datasetFileNames[rankId], DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);
/* Retrieve the input data */
dataSource.loadDataBlock();
/* Create an algorithm for principal component analysis using the SVD method on local nodes */
pca::Distributed<step1Local, algorithmFPType, pca::svdDense> localAlgorithm;
/* Set the input data set to the algorithm */
localAlgorithm.input.set(pca::data, dataSource.getNumericTable());
/* Compute PCA decomposition */
localAlgorithm.compute();
/* Serialize partial results required by step 2 */
services::SharedPtr<byte> serializedData;
InputDataArchive dataArch;
localAlgorithm.getPartialResult()->serialize( dataArch );
size_t perNodeArchLength = dataArch.getSizeOfArchive();
/* Serialized data is of equal size on each node if each node called compute() equal number of times */
if (rankId == mpi_root)
{
serializedData = services::SharedPtr<byte>( new byte[ perNodeArchLength * nBlocks ] );
}
byte *nodeResults = new byte[ perNodeArchLength ];
dataArch.copyArchiveToArray( nodeResults, perNodeArchLength );
/* Transfer partial results to step 2 on the root node */
MPI_Gather( nodeResults, perNodeArchLength, MPI_CHAR, serializedData.get(), perNodeArchLength, MPI_CHAR, mpi_root,
MPI_COMM_WORLD);
delete[] nodeResults;
if(rankId == mpi_root)
{
/* Create an algorithm for principal component analysis using the SVD method on the master node */
pca::Distributed<step2Master, algorithmFPType, pca::svdDense> masterAlgorithm;
for( size_t i = 0; i < nBlocks ; i++ )
{
/* Deserialize partial results from step 1 */
OutputDataArchive dataArch( serializedData.get() + perNodeArchLength * i, perNodeArchLength );
services::SharedPtr<pca::PartialResult<pca::svdDense> > dataForStep2FromStep1 = services::SharedPtr<pca::PartialResult<pca::svdDense> >(
new pca::PartialResult<pca::svdDense>() );
dataForStep2FromStep1->deserialize(dataArch);
/* Set local partial results as input for the master-node algorithm */
masterAlgorithm.input.add(pca::partialResults, dataForStep2FromStep1 );
}
/* Merge and finalizeCompute PCA decomposition on the master node */
masterAlgorithm.compute();
masterAlgorithm.finalizeCompute();
/* Retrieve the algorithm results */
pca::ResultPtr result = masterAlgorithm.getResult();
/* Print the results */
printNumericTable(result->get(pca::eigenvalues), "Eigenvalues:");
printNumericTable(result->get(pca::eigenvectors), "Eigenvectors:");
}
MPI_Finalize();
return 0;
}

For more complete information about compiler optimizations, see our Optimization Notice.