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

DataStructuresSOA.java

/* file: DataStructuresSOA.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 a structure of arrays (SOA)
*/
package com.intel.daal.examples.datasource;
import java.nio.DoubleBuffer;
import java.nio.IntBuffer;
import com.intel.daal.data_management.data.*;
import com.intel.daal.examples.utils.Service;
import com.intel.daal.services.DaalContext;
class DataStructuresSOA {
private static final long firstReadRow = 0;
private static final long nRead = 3;
private static final long nVectorsSOA = 10;
private static final long nFeaturesSOA = 4;
private static DaalContext context = new DaalContext();
public static void main(String[] args) {
System.out.println("Structure of array (SOA) numeric table example");
long readFeatureIdx;
int[] iDataSOA = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
double[] dDataSOA = { 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8 };
float[] fDataSOA = { 3.1f, 3.2f, 3.3f, 3.4f, 3.5f, 3.6f, 3.7f, 3.8f, 3.9f, 4.0f };
long[] lDataSOA = { -10, -20, -30, -40, -50, -60, -70, -80, -90, -100 };
/* Construct an SOA numeric table with nVectorsSOA rows and nFeaturesSOA columns */
SOANumericTable dataTable = new SOANumericTable(context, nFeaturesSOA, nVectorsSOA);
dataTable.setArray(iDataSOA, 0);
dataTable.setArray(dDataSOA, 1);
dataTable.setArray(fDataSOA, 2);
dataTable.setArray(lDataSOA, 3);
/* Read a block of rows */
DoubleBuffer dataDouble = DoubleBuffer.allocate((int) (nRead * nFeaturesSOA));
dataDouble = dataTable.getBlockOfRows(firstReadRow, nRead, dataDouble);
printDoubleBuffer(dataDouble, nFeaturesSOA, nRead, "Print SOA data structures as double:");
dataTable.releaseBlockOfRows(firstReadRow, nRead, dataDouble);
/* Read a feature (column) */
IntBuffer dataInt = IntBuffer.allocate((int) nVectorsSOA);
readFeatureIdx = 0;
dataInt = dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nVectorsSOA, dataInt);
printIntBuffer(dataInt, 1, nVectorsSOA, "Print the first feature of SOA:");
dataTable.releaseBlockOfColumnValues(readFeatureIdx, firstReadRow, nVectorsSOA, dataInt);
/* Get the automatically generated dictionary and get the number of features */
DataDictionary dictionary = dataTable.getDictionary();
System.out.println("Number of features in table: " + dictionary.getNumberOfFeatures());
System.out.println("");
System.out.println("Default type in autogenerated dictionary:");
for (int i = 0; i < nFeaturesSOA; i++) {
DataFeature df = dictionary.getFeature(i);
DataFeatureUtils.FeatureType featureType = df.getFeatureType();
System.out.print("Type of feature #" + i + ": ");
System.out.println(featureType.toString());
}
System.out.println("");
/* Modify the dictionary information about data */
DataFeature categoricalFeature = dictionary.getFeature(0);
categoricalFeature.setFeatureType(DataFeatureUtils.FeatureType.DAAL_CATEGORICAL);
System.out.println("Modified type in the dictionary:");
for (int i = 0; i < nFeaturesSOA; i++) {
DataFeature df = dictionary.getFeature(i);
DataFeatureUtils.FeatureType featureType = df.getFeatureType();
System.out.print("Type of feature #" + i + ": ");
System.out.println(featureType.toString());
}
System.out.println("");
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("");
}
}