Skip to content

Commit

Permalink
Migrate pca daal to DPC++ (oap-project#223)
Browse files Browse the repository at this point in the history
* 1. add JNI wrapper
2. modify compiler config
3. add unit test

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* add ci test

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* java code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* cpp code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-local-test.sh

* Update ci-local-test.sh

* Update install-build-deps-ubuntu.sh

* Update install-build-deps-ubuntu.sh

* Update ci-local-test.sh

* Update ci-local-test.sh

* Update ci-local-test.sh

* Update install-build-deps-ubuntu.sh

* Update install-build-deps-ubuntu.sh

* Update install-build-deps-ubuntu.sh

* Update install-build-deps-ubuntu.sh

* Update install-build-deps-ubuntu.sh

* Update prepare-build-deps.sh

* add convert array, vector to homogentable

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* 1. rollback ci config
2. remove CI test becaue homogentable only support GPU

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* 1. update prepare-build-deps.sh

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* remove ci GPU test

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* 1. remove GPU CI test

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* fix comments

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update Makefile

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* fix comments

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* 1. fix ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update header

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update header

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-cluster-test.sh

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci conf

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update oneapi to 2022.1.2

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci conf

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* fix comment

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update build-jni.sh

* update Makefile

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update test.sh

* Update test.sh

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update test.sh

* Update test.sh

* update test.sh

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update test.sh

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update test.sh

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* fix link libze_loader.so.1

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci-local-dpc-test.sh

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update oneapi to 2022.1.2

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-local-dpc-test.sh

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update Makefile

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-local-dpc-test.sh

* Update prepare-build-deps-gpu.sh

* Update prepare-build-deps-gpu.sh

* Update prepare-build-deps.sh

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-local-dpc-test.sh

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update install-build-level-zero-deps-ubuntun.sh

* Update install-build-level-zero-deps-ubuntun.sh

* Update install-build-level-zero-deps-ubuntun.sh

* Update ci-local-dpc-test.sh

* Update GPU.cpp

* Update GPU.cpp

* Update ci-build-test.sh

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update HomogenTableImpl.cpp

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-local-dpc-test.sh

* Update ci-local-dpc-test.sh

* Update ci-local-dpc-test.sh

* update HomogenTable API

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update install-build-level-zero-deps-ubuntu.sh

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-local-dpc-test.sh

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-local-dpc-test.sh

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update ci

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update run.sh

* Update build-jni.sh

* 1. Merge branch 'make_homogen_table' into convert_homogentable
2. update ci
3. add a new parameters for HomogenTable class

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update pom.xml

* Update test.sh

* Update Makefile

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ConvertHomogenTableSuite.scala

* Update install-level-zero-ubuntu.sh

* Update install-level-zero-ubuntu.sh

* Update build.sh

* Update test.sh

* Update test.sh

* Update HomogenTableImpl.cpp

* Update install-build-deps-ubuntu.sh

* Update HomogenTableImpl.cpp

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update install-build-deps-ubuntu.sh

* fix comments

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update list-compute-devices.cpp

* Convert homogentable to array/vector/matrix (#4)

* 1. convert homogenTable to array/vector/matrix
2. add unit test

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Merge branch 'make_homogen_table' into convert_homogentable

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* fix comments

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Convert RDD[Vector]/RDD[LabeledPoint] to HomogenTable (#8)

* 1. convert rdd to HomogenTable
* 2. add unit test

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* add empty homogentable

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Mirgate kmeans daal to DPC++ (#7)

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* 1. Mirgate kmeans daal to DPC++
* 2. add unit test

* 1. move local host to distributed host
2. add cluster test
3. code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update ci-cluster-test.sh

* Update ci-cluster-test.sh

* Update build.sh

* Update build.sh

* Update OneDAL.cpp

* Update OneDAL.cpp

* Update OneDAL.cpp

* Update com_intel_oap_mllib_OneDAL__.h

* update code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update NaiveBayesResult.java

* 1. update code style
2. When using the Host/CPU device throws a fail

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* rollback code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* rollback code style

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* rollback checkstyle.xml

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* rollback checkstyle.xml

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update OneDAL.scala

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* revert ALS.scala

* revert checkstyle.xml

* :Revert "revert checkstyle.xml"

This reverts commit 4f74bac.

* disable cluster test for gpu

* disable cluster test for gpu

* nit

* migrate cpa to DPC++

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* Update OutputHelpers.hpp

* update com_intel_oap_mllib_feature_PCADALImpl.h

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update PCADALImpl.scala

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* 1. update function name and variable name

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* update stayHomogenTablePtrToVector to saveHomogenTablePtrToVector

Signed-off-by: minmingzhu <minming.zhu@intel.com>

* change compute_device to ComputeDevice

Signed-off-by: minmingzhu <minming.zhu@intel.com>

Co-authored-by: minmingz <minming.zhu@inte.com>
Co-authored-by: Wu, Xiaochang <xiaochang.wu@intel.com>
  • Loading branch information
3 people committed May 9, 2023
1 parent 7ea290e commit 10818de
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 51 deletions.
58 changes: 21 additions & 37 deletions mllib-dal/src/main/native/KMeansOneAPIImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,32 @@ using namespace std;
using namespace oneapi::dal;
const int ccl_root = 0;

typedef std::shared_ptr<homogen_table> homogenPtr;

std::mutex kmtx;
std::vector<homogenPtr> cVector;

static void saveShareHomogenPtrVector(const homogenPtr &ptr) {
kmtx.lock();
cVector.push_back(ptr);
kmtx.unlock();
}

static jlong doKMeansOneAPICompute(JNIEnv *env, jint rankId, jlong pNumTabData,
jlong pNumTabCenters, jint cluster_num,
jdouble tolerance, jint iteration_num,
jint executor_num, const ccl::string &ipPort,
jint cComputeDevice, jobject resultObj) {
std::cout << "oneDAL (native): OneAPI compute start , rankid %ld " << rankId
jlong pNumTabCenters, jint clusterNum,
jdouble tolerance, jint iterationNum,
jint executorNum, const ccl::string &ipPort,
jint computeDeviceOrdinal,
jobject resultObj) {
std::cout << "oneDAL (native): GPU/CPU compute start , rankid = " << rankId
<< "; device = " << computeDeviceOrdinal << "(0:HOST;1:GPU;2:CPU)"
<< std::endl;
const bool isRoot = (rankId == ccl_root);
compute_device device = getComputeDevice(cComputeDevice);
ComputeDevice device = getComputeDeviceByOrdinal(computeDeviceOrdinal);
homogen_table htable =
*reinterpret_cast<const homogen_table *>(pNumTabData);
homogen_table centroids =
*reinterpret_cast<const homogen_table *>(pNumTabCenters);
const auto kmeans_desc = kmeans::descriptor<>()
.set_cluster_count(cluster_num)
.set_max_iteration_count(iteration_num)
.set_cluster_count(clusterNum)
.set_max_iteration_count(iterationNum)
.set_accuracy_threshold(tolerance);
kmeans::train_input local_input{htable, centroids};
auto queue = getQueue(device);
auto comm = preview::spmd::make_communicator<preview::spmd::backend::ccl>(
queue, executor_num, rankId, ipPort);
queue, executorNum, rankId, ipPort);
kmeans::train_result result_train =
preview::train(comm, kmeans_desc, local_input);
if (isRoot) {
std::cout << "iteration_num: " << iteration_num << std::endl;
std::cout << "Iteration count: " << result_train.get_iteration_count()
<< std::endl;
std::cout << "Objective function value: "
<< result_train.get_objective_function_value() << std::endl;
// Get the class of the input object
jclass clazz = env->GetObjectClass(resultObj);
// Get Field references
Expand All @@ -88,9 +74,9 @@ static jlong doKMeansOneAPICompute(JNIEnv *env, jint rankId, jlong pNumTabData,
env->SetDoubleField(resultObj, totalCostField,
result_train.get_objective_function_value());

homogenPtr centroidsPtr = std::make_shared<homogen_table>(
HomogenTablePtr centroidsPtr = std::make_shared<homogen_table>(
result_train.get_model().get_centroids());
saveShareHomogenPtrVector(centroidsPtr);
saveHomogenTablePtrToVector(centroidsPtr);
return (jlong)centroidsPtr.get();
} else {
return (jlong)0;
Expand All @@ -105,18 +91,16 @@ static jlong doKMeansOneAPICompute(JNIEnv *env, jint rankId, jlong pNumTabData,
JNIEXPORT jlong JNICALL
Java_com_intel_oap_mllib_clustering_KMeansDALImpl_cKMeansOneapiComputeWithInitCenters(
JNIEnv *env, jobject obj, jlong pNumTabData, jlong pNumTabCenters,
jint cluster_num, jdouble tolerance, jint iteration_num, jint executor_num,
jint cComputeDevice, jint rankId, jstring ip_port, jobject resultObj) {
std::cout << "oneDAL (native): use GPU DPC++ kernels with " << std::endl;
const char *ipport = env->GetStringUTFChars(ip_port, 0);
std::string ipPort = std::string(ipport);
jint clusterNum, jdouble tolerance, jint iterationNum, jint executorNum,
jint computeDeviceOrdinal, jint rankId, jstring ipPort, jobject resultObj) {
std::cout << "oneDAL (native): use GPU DPC++ kernels " << std::endl;
const char *ipPortPtr = env->GetStringUTFChars(ipPort, 0);
std::string ipPortStr = std::string(ipPortPtr);
jlong ret = 0L;
printf("oneDAL (native): KMeansOneapiComputeWithInitCenters %d \n",
cComputeDevice);
ret = doKMeansOneAPICompute(
env, rankId, pNumTabData, pNumTabCenters, cluster_num, tolerance,
iteration_num, executor_num, ipPort, cComputeDevice, resultObj);
env->ReleaseStringUTFChars(ip_port, ipport);
env, rankId, pNumTabData, pNumTabCenters, clusterNum, tolerance,
iterationNum, executorNum, ipPortStr, computeDeviceOrdinal, resultObj);
env->ReleaseStringUTFChars(ipPort, ipPortPtr);
return ret;
}
#endif
90 changes: 90 additions & 0 deletions mllib-dal/src/main/native/PCAOneAPIImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
* Copyright 2020 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

#include <chrono>
#include <iostream>

#ifdef CPU_GPU_PROFILE
#include "GPU.h"
#ifndef ONEDAL_DATA_PARALLEL
#define ONEDAL_DATA_PARALLEL
#endif
#include "Communicator.hpp"
#include "OutputHelpers.hpp"
#include "com_intel_oap_mllib_feature_PCADALImpl.h"
#include "oneapi/dal/algo/pca.hpp"
#include "oneapi/dal/table/homogen.hpp"
#include "service.h"

using namespace std;
using namespace oneapi::dal;
const int ccl_root = 0;

static void doPCAOneAPICompute(JNIEnv *env, jint rankId, jlong pNumTabData,
jint executorNum, const ccl::string &ipPort,
jint computeDeviceOrdinal, jobject resultObj) {
const bool isRoot = (rankId == ccl_root);
ComputeDevice device = getComputeDeviceByOrdinal(computeDeviceOrdinal);
std::cout << "oneDAL (native): GPU/CPU compute start , rankid = " << rankId
<< "; device = " << computeDeviceOrdinal << "(0:HOST;1:GPU;2:CPU)"
<< std::endl;
homogen_table htable =
*reinterpret_cast<const homogen_table *>(pNumTabData);

const auto pca_desc = pca::descriptor{};
auto queue = getQueue(device);
auto comm = preview::spmd::make_communicator<preview::spmd::backend::ccl>(
queue, executorNum, rankId, ipPort);
pca::train_input local_input{htable};
const auto result_train = preview::train(comm, pca_desc, local_input);
if (isRoot) {
// Return all eigenvalues & eigenvectors
// Get the class of the input object
jclass clazz = env->GetObjectClass(resultObj);
// Get Field references
jfieldID pcNumericTableField =
env->GetFieldID(clazz, "pcNumericTable", "J");
jfieldID explainedVarianceNumericTableField =
env->GetFieldID(clazz, "explainedVarianceNumericTable", "J");

HomogenTablePtr eigenvectors =
std::make_shared<homogen_table>(result_train.get_eigenvectors());
saveHomogenTablePtrToVector(eigenvectors);

HomogenTablePtr eigenvalues =
std::make_shared<homogen_table>(result_train.get_eigenvalues());
saveHomogenTablePtrToVector(eigenvalues);

env->SetLongField(resultObj, pcNumericTableField,
(jlong)eigenvectors.get());
env->SetLongField(resultObj, explainedVarianceNumericTableField,
(jlong)eigenvalues.get());
}
}

JNIEXPORT jlong JNICALL
Java_com_intel_oap_mllib_feature_PCADALImpl_cPCATrainDAL(
JNIEnv *env, jobject obj, jlong pNumTabData, jint executorNum,
jint computeDeviceOrdinal, jint rankId, jstring ipPort, jobject resultObj) {
std::cout << "oneDAL (native): use GPU DPC++ kernels " << std::endl;
const char *ipPortPtr = env->GetStringUTFChars(ipPort, 0);
std::string ipPortStr = std::string(ipPortPtr);
doPCAOneAPICompute(env, rankId, pNumTabData, executorNum, ipPortStr,
computeDeviceOrdinal, resultObj);
env->ReleaseStringUTFChars(ipPort, ipPortPtr);
return 0;
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
package com.intel.oap.mllib.clustering

import com.intel.oap.mllib.Utils.getOneCCLIPPort
<<<<<<< HEAD
import com.intel.oap.mllib.{OneCCL, OneDAL, Utils}
=======
import com.intel.oap.mllib.{OneCCL, OneDAL}
>>>>>>> Migrate KMeans daal to DPC++ (#209)
import com.intel.oneapi.dal.table.Common
import org.apache.spark.TaskContext
import org.apache.spark.internal.Logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ class PCADALImpl(val k: Int,
val numCols = table.getNumberOfColumns.toInt
require(k <= numRows, "k should be less or equal to row number")

val arrayDouble = getDoubleBufferDataFromDAL(table, numRows, numCols)
val arrayDouble = getDoubleBufferDataFromDAL(table, numRows, device)

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

private def getExplainedVarianceFromDAL(table_1xn: NumericTable, k: Int): DenseVector = {
val dataNumCols = table_1xn.getNumberOfColumns.toInt
val arrayDouble = getDoubleBufferDataFromDAL(table_1xn, 1, dataNumCols)
private[mllib] def getExplainedVarianceFromDAL(table_1xn: HomogenTable, k: Int,
device: Common.ComputeDevice): DenseVector = {
val arrayDouble = getDoubleBufferDataFromDAL(table_1xn, 1, device)
val sum = arrayDouble.sum
val topK = Arrays.copyOfRange(arrayDouble, 0, k)
for (i <- 0 until k)
Expand All @@ -182,15 +182,13 @@ class PCADALImpl(val k: Int,

// table.asInstanceOf[HomogenNumericTable].getDoubleArray() would error on GPU,
// so use table.getBlockOfRows instead of it.
private def getDoubleBufferDataFromDAL(table: NumericTable,
private[mllib] def getDoubleBufferDataFromDAL(table: HomogenTable,
numRows: Int,
numCols: Int): Array[Double] = {
var dataDouble: DoubleBuffer = null
device: Common.ComputeDevice): Array[Double] = {

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

arrayDouble
}
Expand Down

0 comments on commit 10818de

Please sign in to comment.