Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
Signed-off-by: minmingzhu <minming.zhu@intel.com>
  • Loading branch information
minmingzhu committed Aug 8, 2023
1 parent 5bba1fb commit 291a039
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 21 deletions.
2 changes: 1 addition & 1 deletion mllib-dal/src/main/native/KMeansImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ static jlong doKMeansOneAPICompute(
*reinterpret_cast<const homogen_table *>(pNumTabData);
homogen_table centroids =
*reinterpret_cast<const homogen_table *>(pNumTabCenters);
const auto kmeans_desc = kmeans_gpu::descriptor<>()
const auto kmeans_desc = kmeans_gpu::descriptor<double>()
.set_cluster_count(clusterNum)
.set_max_iteration_count(iterationNum)
.set_accuracy_threshold(tolerance);
Expand Down
57 changes: 49 additions & 8 deletions mllib-dal/src/main/native/OneDAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ using namespace daal::data_management;
extern bool daal_check_is_intel_cpu();

// Define a global native array
typedef std::shared_ptr<float[]> NativeArrayPtr;
typedef std::shared_ptr<float[]> NativeFloatArrayPtr;
typedef std::shared_ptr<double[]> NativeDoubleArrayPtr;

std::mutex g_amtx;
std::vector<NativeArrayPtr> g_NativeArrayPtrVector;
std::vector<NativeFloatArrayPtr> g_NativeFloatArrayPtrVector;
std::vector<NativeDoubleArrayPtr> g_NativeDoubleArrayPtrVector;


void saveArrayPtrToVector(const NativeArrayPtr &ptr) {
void saveFloatArrayPtrToVector(const NativeFloatArrayPtr &ptr) {
g_amtx.lock();
g_NativeArrayPtrVector.push_back(ptr);
g_NativeFloatArrayPtrVector.push_back(ptr);
g_amtx.unlock();
}

void saveDoubleArrayPtrToVector(const NativeDoubleArrayPtr &ptr) {
g_amtx.lock();
g_NativeDoubleArrayPtrVector.push_back(ptr);
g_amtx.unlock();
}

Expand Down Expand Up @@ -175,17 +185,17 @@ Java_com_intel_oap_mllib_OneDAL_00024_cNewCSRNumericTableDouble(
JNIEXPORT jlong JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cNewFloatArray(
JNIEnv *env, jobject, jlong size) {
std::cout << "create new native array size : " << size << std::endl;
NativeArrayPtr arrayPtr(new float[size], [](float *ptr) { delete[] ptr; });
saveArrayPtrToVector(arrayPtr);
NativeFloatArrayPtr arrayPtr(new float[size], [](float *ptr) { delete[] ptr; });
saveFloatArrayPtrToVector(arrayPtr);
return (jlong)arrayPtr.get();
}

/*
* Class: com_intel_oap_mllib_OneDAL__
* Method: cCopyArrayToNative
* Method: cCopyFloatArrayToNative
* Signature: (J[DJ)V
*/
JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cCopyArrayToNative(
JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cCopyFloatArrayToNative(
JNIEnv *env, jobject, jlong nativeArrayPtr, jfloatArray sourceArray,
jlong index) {
float *nativeArray = reinterpret_cast<float *>(nativeArrayPtr);
Expand All @@ -195,3 +205,34 @@ JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cCopyArrayToNative(
std::copy(source, source + sourceLength, nativeArray + index);
env->ReleasePrimitiveArrayCritical(sourceArray, source, 0);
}



/*
* Class: com_intel_oap_mllib_OneDAL__
* Method: cNewDoubleArray
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cNewDoubleArray(
JNIEnv *env, jobject, jlong size) {
std::cout << "create new native array size : " << size << std::endl;
NativeDoubleArrayPtr arrayPtr(new double[size], [](double *ptr) { delete[] ptr; });
saveDoubleArrayPtrToVector(arrayPtr);
return (jlong)arrayPtr.get();
}

/*
* Class: com_intel_oap_mllib_OneDAL__
* Method: cCopyDoubleArrayToNative
* Signature: (J[DJ)V
*/
JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cCopyDoubleArrayToNative(
JNIEnv *env, jobject, jlong nativeArrayPtr, jfloatArray sourceArray,
jlong index) {
double *nativeArray = reinterpret_cast<double *>(nativeArrayPtr);
jsize sourceLength = env->GetArrayLength(sourceArray);
jdouble *source = static_cast<jdouble *>(
env->GetPrimitiveArrayCritical(sourceArray, NULL));
std::copy(source, source + sourceLength, nativeArray + index);
env->ReleasePrimitiveArrayCritical(sourceArray, source, 0);
}
23 changes: 19 additions & 4 deletions mllib-dal/src/main/native/javah/com_intel_oap_mllib_OneDAL__.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 42 additions & 1 deletion mllib-dal/src/main/native/oneapi/dal/HomogenTableImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,50 @@ JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_lInit(
* Method: Init
* Signature: (JJJIII)J
*/
JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_fPtrInit(
JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_dPtrInit(
JNIEnv *env, jobject, jlong cRowCount, jlong cColCount, jlong dataPtr, jint cLayout, jint computeDeviceOrdinal) {
printf("HomogenTable dPtrInit\n");
double *fData = reinterpret_cast<double *>(dataPtr);
if (fData == NULL) {
std::cout << "Error: unable to obtain critical array" << std::endl;
exit(-1);
}
const std::vector<sycl::event> dependencies = {};
HomogenTablePtr tablePtr;
ComputeDevice device = getComputeDeviceByOrdinal(computeDeviceOrdinal);
switch(device) {
case ComputeDevice::host:{
tablePtr = std::make_shared<homogen_table>(fData, cRowCount, cColCount,
detail::empty_delete<const double>(),
getDataLayout(cLayout));
break;
}
case ComputeDevice::cpu:
case ComputeDevice::gpu:{
auto queue = getQueue(device);
auto data = sycl::malloc_shared<double>(cRowCount * cColCount, queue);
queue.memcpy(data, fData, sizeof(double) * cRowCount * cColCount).wait();
tablePtr = std::make_shared<homogen_table>(queue, data, cRowCount, cColCount,
detail::make_default_delete<const double>(queue),
dependencies, getDataLayout(cLayout));
break;
}
default: {
deviceError();
}
}
saveHomogenTablePtrToVector(tablePtr);
return (jlong)tablePtr.get();
}

/*
* Class: com_intel_oneapi_dal_table_HomogenTableImpl
* Method: Init
* Signature: (JJJIII)J
*/
JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_fPtrInit(
JNIEnv *env, jobject, jlong cRowCount, jlong cColCount, jlong dataPtr, jint cLayout, jint computeDeviceOrdinal) {
printf("HomogenTable fPtrInit\n");
float *fData = reinterpret_cast<float *>(dataPtr);
if (fData == NULL) {
std::cout << "Error: unable to obtain critical array" << std::endl;
Expand Down
20 changes: 13 additions & 7 deletions mllib-dal/src/main/scala/com/intel/oap/mllib/OneDAL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,12 @@ object OneDAL {
val coalescedTables = coalescedRdd.mapPartitionsWithIndex { (index: Int, it: Iterator[Row]) =>
val list = it.toList
val subRowCount: Int = list.size / numberCores
val labeledPointsList: ListBuffer[Future[(Array[Float], Long)]] =
new ListBuffer[Future[(Array[Float], Long)]]()
val labeledPointsList: ListBuffer[Future[(Array[Double], Long)]] =
new ListBuffer[Future[(Array[Double], Long)]]()
val numRows = list.size
val numCols = list(0).getAs[Vector](1).toArray.size

val labelsArray = new Array[Float](numRows)
val labelsArray = new Array[Double](numRows)
val featuresAddress= OneDAL.cNewFloatArray(numRows.toLong * numCols)
for ( i <- 0 until numberCores) {
val f = Future {
Expand All @@ -464,8 +464,8 @@ object OneDAL {
// oneapi only support float type to fix DAALL-6046, so convert data to float
slice.toArray.zipWithIndex.map { case (row, index) =>
val length = row.getAs[Vector](1).toArray.length
OneDAL.cCopyArrayToNative(featuresAddress, row.getAs[Vector](1).toArray.map(_.toFloat), subRowCount.toLong * numCols * i + length * index)
labelsArray(subRowCount * i + index) = row.getAs[Float](0)
OneDAL.cCopyDoubleArrayToNative(featuresAddress, row.getAs[Vector](1).toArray, subRowCount.toLong * numCols * i + length * index)
labelsArray(subRowCount * i + index) = row.getAs[Double](0)
}
(labelsArray, featuresAddress)
}
Expand Down Expand Up @@ -638,7 +638,7 @@ object OneDAL {
// oneapi only support float type to fix DAALL-6046, so convert data to float
slice.toArray.zipWithIndex.map { case (vector, index) =>
val length = vector.toArray.length
OneDAL.cCopyArrayToNative(targetArrayAddress, vector.toArray.map(_.toFloat), subRowCount.toLong * numCols * i + length * index)
OneDAL.cCopyDoubleArrayToNative(targetArrayAddress, vector.toArray, subRowCount.toLong * numCols * i + length * index)
}
targetArrayAddress
}
Expand Down Expand Up @@ -759,7 +759,13 @@ object OneDAL {

@native def cNewFloatArray(size: Long): Long

@native def cCopyArrayToNative(arrayAddr: Long,
@native def cNewDoubleArray(size: Long): Long

@native def cCopyFloatArrayToNative(arrayAddr: Long,
data: Array[Float],
index: Long): Unit

@native def cCopyDoubleArrayToNative(arrayAddr: Long,
data: Array[Double],
index: Long): Unit
}

0 comments on commit 291a039

Please sign in to comment.