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

DataStructuresAOS.java

/* file: DataStructuresAOS.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 using an array of structures (AOS)
*/
package com.intel.daal.examples.datasource;
import java.nio.DoubleBuffer;
import java.nio.IntBuffer;
import com.intel.daal.algorithms.cosdistance.*;
import com.intel.daal.data_management.data.AOSNumericTable;
import com.intel.daal.data_management.data.NumericTable;
import com.intel.daal.examples.utils.Service;
import java.io.Serializable;
import com.intel.daal.services.DaalContext;
class DataStructuresAOS {
private static final int nVectors = 5;
private static final long nFeatures = 5;
private static final long firstReadRow = 0;
public static class PointType implements Serializable {
public double x;
public double y;
public int categ;
public float value;
public PointType(double _x, double _y, int _categ, float _value) {
x = _x;
y = _y;
categ = _categ;
value = _value;
}
}
private static DaalContext context = new DaalContext();
public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
PointType points[] = new PointType[nVectors];
points[0] = new PointType(0.5, -1.3, 1, 1.10f);
points[1] = new PointType(2.5, -3.3, 2, 2.20f);
points[2] = new PointType(4.5, -5.3, 2, 3.35f);
points[3] = new PointType(6.5, -7.3, 0, 4.47f);
points[4] = new PointType(8.5, -9.3, 1, 2.57f);
/*
* Construct AOS numericTable for a data array with nFeaturesAOS fields
* and nPoints elements
*/
AOSNumericTable dataTable = new AOSNumericTable(context, points);
/* Create an algorithm to compute a cosine distance matrix using the defaultDense method */
Batch alg = new Batch(context, Float.class, Method.defaultDense);
alg.input.set(InputId.data, dataTable);
Result result = alg.compute();
NumericTable res = result.get(ResultId.cosineDistance);
Service.printNumericTable("Matrix of Distances:", res);
/* Read a block of rows */
DoubleBuffer dataDouble = DoubleBuffer.allocate((int) (nVectors * nFeatures));
dataDouble = res.getBlockOfRows(firstReadRow, nVectors, dataDouble);
printDoubleBuffer(dataDouble, nFeatures, nVectors, "Print AOS data structures as double:");
res.releaseBlockOfRows(firstReadRow, nVectors, dataDouble);
context.dispose();
}
private static void printDoubleBuffer(DoubleBuffer buf, long nColumns, long nRows, String message) {
int step = (int) nColumns;
System.out.println(message);
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nColumns; j++) {
System.out.format("%6.3f ", buf.get(i * step + j));
}
System.out.println("");
}
System.out.println("");
}
private static void printIntBuffer(IntBuffer buf, long nColumns, long nRows, String message) {
int step = (int) nColumns;
System.out.println(message);
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nColumns; j++) {
System.out.format("%3d ", buf.get(i * step + j));
}
System.out.println("");
}
System.out.println("");
}
}