Skip to content

Commit

Permalink
pull() instead of pull(queue)
Browse files Browse the repository at this point in the history
  • Loading branch information
minmingzhu committed Aug 23, 2024
1 parent 5d08ac0 commit e9abe0a
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public class ColumnAccessor {
private long cObject;
private Common.ComputeDevice cDevice;

public ColumnAccessor(long cObject) {
this.cObject = cObject;
this.cDevice = Common.ComputeDevice.HOST;
}

public ColumnAccessor(long cObject, Common.ComputeDevice device) {
this.cObject = cObject;
this.cDevice = device;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
public class RowAccessor {
private long cObject;
private Common.ComputeDevice cDevice;
public RowAccessor(long cObject) {
this.cObject = cObject;
this.cDevice = Common.ComputeDevice.HOST;
}

public RowAccessor(long cObject, Common.ComputeDevice device) {
this.cObject = cObject;
this.cDevice = device;
Expand Down
20 changes: 10 additions & 10 deletions mllib-dal/src/main/scala/com/intel/oap/mllib/OneDAL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ object OneDAL {
matrix
}

def homogenTableToMatrix(table: HomogenTable, device: Common.ComputeDevice): Matrix = {
def homogenTableToMatrix(table: HomogenTable): Matrix = {
val numRows = table.getRowCount.toInt
val numCols = table.getColumnCount.toInt

val accessor = new RowAccessor(table.getcObejct(), device)
val accessor = new RowAccessor(table.getcObejct())
val arrayDouble: Array[Double] = accessor.pullDouble(0, numRows)

// Transpose as DAL numeric table is row-major and DenseMatrix is column major
Expand All @@ -82,11 +82,11 @@ object OneDAL {
matrix
}

def homogenTableToOldMatrix(table: HomogenTable, device: Common.ComputeDevice): OldMatrix = {
def homogenTableToOldMatrix(table: HomogenTable): OldMatrix = {
val numRows = table.getRowCount.toInt
val numCols = table.getColumnCount.toInt

val accessor = new RowAccessor(table.getcObejct(), device)
val accessor = new RowAccessor(table.getcObejct())
val arrayDouble: Array[Double] = accessor.pullDouble(0, numRows)

// Transpose as DAL numeric table is row-major and DenseMatrix is column major
Expand Down Expand Up @@ -115,8 +115,8 @@ object OneDAL {
Vectors.dense(arrayDouble)
}

def homogenTableNx1ToVector(cTable: Long, device: Common.ComputeDevice ): Vector = {
val columnAcc = new ColumnAccessor(cTable, device)
def homogenTableNx1ToVector(cTable: Long): Vector = {
val columnAcc = new ColumnAccessor(cTable)
val arrayDouble = columnAcc.pullDouble(0)
Vectors.dense(arrayDouble)
}
Expand All @@ -135,8 +135,8 @@ object OneDAL {
Vectors.dense(arrayDouble)
}

def homogenTable1xNToVector(table: HomogenTable, device: Common.ComputeDevice): Vector = {
val rowAcc = new RowAccessor(table.getcObejct, device)
def homogenTable1xNToVector(table: HomogenTable): Vector = {
val rowAcc = new RowAccessor(table.getcObejct)
val arrayDouble = rowAcc.pullDouble(0, 1)
Vectors.dense(arrayDouble)
}
Expand All @@ -159,10 +159,10 @@ object OneDAL {
resArray
}

def homogenTableToVectors(table: HomogenTable, device: Common.ComputeDevice): Array[Vector] = {
def homogenTableToVectors(table: HomogenTable): Array[Vector] = {
val numRows = table.getRowCount.toInt

val rowAcc = new RowAccessor(table.getcObejct(), device)
val rowAcc = new RowAccessor(table.getcObejct())

val resArray = new Array[Vector](numRows.toInt)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ class KMeansDALImpl(var nClusters: Int,
val ret = if (rank == 0) {
assert(cCentroids != 0)
val centerVectors = if (useDevice == "GPU") {
OneDAL.homogenTableToVectors(OneDAL.makeHomogenTable(cCentroids),
computeDevice)
OneDAL.homogenTableToVectors(OneDAL.makeHomogenTable(cCentroids))
} else {
OneDAL.numericTableToVectors(OneDAL.makeNumericTable(cCentroids))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class PCADALImpl(val k: Int,
val ret = if (rank == 0) {
val principleComponents = if (useDevice == "GPU") {
val pcNumericTable = OneDAL.makeHomogenTable(result.getPcNumericTable)
getPrincipleComponentsFromOneAPI(pcNumericTable, k, computeDevice)
getPrincipleComponentsFromOneAPI(pcNumericTable, k)
} else {
val pcNumericTable = OneDAL.makeNumericTable(result.getPcNumericTable)
getPrincipleComponentsFromDAL(pcNumericTable, k)
Expand All @@ -102,7 +102,7 @@ class PCADALImpl(val k: Int,
val explainedVarianceNumericTable = OneDAL.makeHomogenTable(
result.getExplainedVarianceNumericTable)
getExplainedVarianceFromOneAPI(
explainedVarianceNumericTable, k, computeDevice)
explainedVarianceNumericTable, k)
} else {
val explainedVarianceNumericTable = OneDAL.makeNumericTable(
result.getExplainedVarianceNumericTable)
Expand Down Expand Up @@ -141,21 +141,20 @@ class PCADALImpl(val k: Int,
}

private[mllib] def getPrincipleComponentsFromOneAPI(table: HomogenTable,
k: Int,
device: Common.ComputeDevice): DenseMatrix = {
k: Int): DenseMatrix = {
val numRows = table.getRowCount.toInt
val numCols = table.getColumnCount.toInt
require(k <= numRows, "k should be less or equal to row number")

val arrayDouble = getDoubleBufferDataFromOneAPI(table, numRows, device)
val arrayDouble = getDoubleBufferDataFromOneAPI(table, numRows)

// Column-major, transpose of top K rows of NumericTable
new DenseMatrix(numCols, k, arrayDouble.slice(0, numCols * k), false)
}

private[mllib] def getExplainedVarianceFromOneAPI(table_1xn: HomogenTable, k: Int,
device: Common.ComputeDevice): DenseVector = {
val arrayDouble = getDoubleBufferDataFromOneAPI(table_1xn, 1, device)
private[mllib] def getExplainedVarianceFromOneAPI(table_1xn: HomogenTable,
k: Int): DenseVector = {
val arrayDouble = getDoubleBufferDataFromOneAPI(table_1xn, 1)
val sum = arrayDouble.sum
val topK = Arrays.copyOfRange(arrayDouble, 0, k)
for (i <- 0 until k)
Expand All @@ -166,11 +165,10 @@ class PCADALImpl(val k: Int,
// table.asInstanceOf[HomogenNumericTable].getDoubleArray() would error on GPU,
// so use table.getBlockOfRows instead of it.
private[mllib] def getDoubleBufferDataFromOneAPI(table: HomogenTable,
numRows: Int,
device: Common.ComputeDevice): Array[Double] = {
numRows: Int): Array[Double] = {

// returned DoubleBuffer is ByteByffer, need to copy as double array
val accessor = new RowAccessor(table.getcObejct(), device)
val accessor = new RowAccessor(table.getcObejct())
val arrayDouble: Array[Double] = accessor.pullDouble(0, numRows)

arrayDouble
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ class LinearRegressionDALImpl( val fitIntercept: Boolean,

val ret = if (rank == 0) {
val coefficientArray = if (useDevice == "GPU") {
OneDAL.homogenTableToVectors(OneDAL.makeHomogenTable(cbeta),
computeDevice)
OneDAL.homogenTableToVectors(OneDAL.makeHomogenTable(cbeta))
} else {
OneDAL.numericTableToVectors(OneDAL.makeNumericTable(cbeta))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ class RandomForestRegressorDALImpl(val uid: String,
val ret = if (rank == 0) {
val convResultStartTime = System.nanoTime()
val predictionNumericTable = OneDAL.homogenTableToMatrix(
OneDAL.makeHomogenTable(result.getPredictionNumericTable),
computeDevice)
OneDAL.makeHomogenTable(result.getPredictionNumericTable))
val convResultEndTime = System.nanoTime()

val durationCovResult = (convResultEndTime - convResultStartTime).toDouble / 1E9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ class CorrelationDALImpl(
val ret = if (rank == 0) {
val convResultStartTime = System.nanoTime()
val correlationNumericTable = if (useDevice == "GPU") {
OneDAL.homogenTableToMatrix(OneDAL.makeHomogenTable(result.getCorrelationNumericTable),
computeDevice)
OneDAL.homogenTableToMatrix(OneDAL.makeHomogenTable(result.getCorrelationNumericTable))
} else {
OneDAL.numericTableToMatrix(OneDAL.makeNumericTable(result.getCorrelationNumericTable))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,28 @@ class SummarizerDALImpl(val executorNum: Int,
val convResultStartTime = System.nanoTime()
val meanVector = if (useDevice == "GPU") {
OneDAL.homogenTable1xNToVector(
OneDAL.makeHomogenTable(result.getMeanNumericTable), computeDevice)
OneDAL.makeHomogenTable(result.getMeanNumericTable))
} else {
OneDAL.numericTable1xNToVector(
OneDAL.makeNumericTable(result.getMeanNumericTable))
}
val varianceVector = if (useDevice == "GPU") {
OneDAL.homogenTable1xNToVector(
OneDAL.makeHomogenTable(result.getVarianceNumericTable), computeDevice)
OneDAL.makeHomogenTable(result.getVarianceNumericTable))
} else {
OneDAL.numericTable1xNToVector(
OneDAL.makeNumericTable(result.getVarianceNumericTable))
}
val maxVector = if (useDevice == "GPU") {
OneDAL.homogenTable1xNToVector(
OneDAL.makeHomogenTable(result.getMaximumNumericTable), computeDevice)
OneDAL.makeHomogenTable(result.getMaximumNumericTable))
} else {
OneDAL.numericTable1xNToVector(
OneDAL.makeNumericTable(result.getMaximumNumericTable))
}
val minVector = if (useDevice == "GPU") {
OneDAL.homogenTable1xNToVector(
OneDAL.makeHomogenTable(result.getMinimumNumericTable), computeDevice)
OneDAL.makeHomogenTable(result.getMinimumNumericTable))
} else {
OneDAL.numericTable1xNToVector(
OneDAL.makeNumericTable(result.getMinimumNumericTable))
Expand Down

0 comments on commit e9abe0a

Please sign in to comment.