From 291a039f6e5a274f3b838d454512993f865ccea6 Mon Sep 17 00:00:00 2001 From: minmingzhu Date: Tue, 8 Aug 2023 07:59:38 +0000 Subject: [PATCH] testing Signed-off-by: minmingzhu --- mllib-dal/src/main/native/KMeansImpl.cpp | 2 +- mllib-dal/src/main/native/OneDAL.cpp | 57 ++++++++++++++++--- .../javah/com_intel_oap_mllib_OneDAL__.h | 23 ++++++-- ..._intel_oneapi_dal_table_HomogenTableImpl.h | 9 +++ .../native/oneapi/dal/HomogenTableImpl.cpp | 43 +++++++++++++- .../scala/com/intel/oap/mllib/OneDAL.scala | 20 ++++--- 6 files changed, 133 insertions(+), 21 deletions(-) diff --git a/mllib-dal/src/main/native/KMeansImpl.cpp b/mllib-dal/src/main/native/KMeansImpl.cpp index a7a40a90f..0133e2056 100644 --- a/mllib-dal/src/main/native/KMeansImpl.cpp +++ b/mllib-dal/src/main/native/KMeansImpl.cpp @@ -251,7 +251,7 @@ static jlong doKMeansOneAPICompute( *reinterpret_cast(pNumTabData); homogen_table centroids = *reinterpret_cast(pNumTabCenters); - const auto kmeans_desc = kmeans_gpu::descriptor<>() + const auto kmeans_desc = kmeans_gpu::descriptor() .set_cluster_count(clusterNum) .set_max_iteration_count(iterationNum) .set_accuracy_threshold(tolerance); diff --git a/mllib-dal/src/main/native/OneDAL.cpp b/mllib-dal/src/main/native/OneDAL.cpp index 87451a0ea..6e4983f7f 100644 --- a/mllib-dal/src/main/native/OneDAL.cpp +++ b/mllib-dal/src/main/native/OneDAL.cpp @@ -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 NativeArrayPtr; +typedef std::shared_ptr NativeFloatArrayPtr; +typedef std::shared_ptr NativeDoubleArrayPtr; + std::mutex g_amtx; -std::vector g_NativeArrayPtrVector; +std::vector g_NativeFloatArrayPtrVector; +std::vector 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(); } @@ -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(nativeArrayPtr); @@ -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(nativeArrayPtr); + jsize sourceLength = env->GetArrayLength(sourceArray); + jdouble *source = static_cast( + env->GetPrimitiveArrayCritical(sourceArray, NULL)); + std::copy(source, source + sourceLength, nativeArray + index); + env->ReleasePrimitiveArrayCritical(sourceArray, source, 0); +} \ No newline at end of file diff --git a/mllib-dal/src/main/native/javah/com_intel_oap_mllib_OneDAL__.h b/mllib-dal/src/main/native/javah/com_intel_oap_mllib_OneDAL__.h index 9c8e4c1b0..8eaafec0f 100644 --- a/mllib-dal/src/main/native/javah/com_intel_oap_mllib_OneDAL__.h +++ b/mllib-dal/src/main/native/javah/com_intel_oap_mllib_OneDAL__.h @@ -31,7 +31,6 @@ JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cSetDouble JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cSetDoubleBatch (JNIEnv *, jobject, jlong, jint, jdoubleArray, jint, jint); - /* * Class: com_intel_oap_mllib_OneDAL__ * Method: cFreeDataMemory @@ -74,12 +73,28 @@ JNIEXPORT jlong JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cNewFloatArray /* * Class: com_intel_oap_mllib_OneDAL__ - * Method: cCopyArrayToNative - * Signature: (J[DJ)V + * Method: cNewDoubleArray + * Signature: (J)J */ -JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cCopyArrayToNative +JNIEXPORT jlong JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cNewDoubleArray + (JNIEnv *, jobject, jlong); + +/* + * Class: com_intel_oap_mllib_OneDAL__ + * Method: cCopyFloatArrayToNative + * Signature: (J[FJ)V + */ +JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cCopyFloatArrayToNative (JNIEnv *, jobject, jlong, jfloatArray, jlong); +/* + * Class: com_intel_oap_mllib_OneDAL__ + * Method: cCopyDoubleArrayToNative + * Signature: (J[DJ)V + */ +JNIEXPORT void JNICALL Java_com_intel_oap_mllib_OneDAL_00024_cCopyDoubleArrayToNative + (JNIEnv *, jobject, jlong, jdoubleArray, jlong); + #ifdef __cplusplus } #endif diff --git a/mllib-dal/src/main/native/javah/com_intel_oneapi_dal_table_HomogenTableImpl.h b/mllib-dal/src/main/native/javah/com_intel_oneapi_dal_table_HomogenTableImpl.h index 641c0a05d..87ecb5b55 100644 --- a/mllib-dal/src/main/native/javah/com_intel_oneapi_dal_table_HomogenTableImpl.h +++ b/mllib-dal/src/main/native/javah/com_intel_oneapi_dal_table_HomogenTableImpl.h @@ -39,6 +39,14 @@ JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_dInit JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_lInit (JNIEnv *, jobject, jlong, jlong, jlongArray, jint, jint); +/* + * Class: com_intel_oneapi_dal_table_HomogenTableImpl + * Method: dPtrInit + * Signature: (JJJII)J + */ +JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_dPtrInit + (JNIEnv *, jobject, jlong, jlong, jlong, jint, jint); + /* * Class: com_intel_oneapi_dal_table_HomogenTableImpl * Method: fPtrInit @@ -46,6 +54,7 @@ JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_lInit */ JNIEXPORT jlong JNICALL Java_com_intel_oneapi_dal_table_HomogenTableImpl_fPtrInit (JNIEnv *, jobject, jlong, jlong, jlong, jint, jint); + /* * Class: com_intel_oneapi_dal_table_HomogenTableImpl * Method: cGetColumnCount diff --git a/mllib-dal/src/main/native/oneapi/dal/HomogenTableImpl.cpp b/mllib-dal/src/main/native/oneapi/dal/HomogenTableImpl.cpp index 816bfc31b..b6e4c82a1 100644 --- a/mllib-dal/src/main/native/oneapi/dal/HomogenTableImpl.cpp +++ b/mllib-dal/src/main/native/oneapi/dal/HomogenTableImpl.cpp @@ -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(dataPtr); + if (fData == NULL) { + std::cout << "Error: unable to obtain critical array" << std::endl; + exit(-1); + } + const std::vector dependencies = {}; + HomogenTablePtr tablePtr; + ComputeDevice device = getComputeDeviceByOrdinal(computeDeviceOrdinal); + switch(device) { + case ComputeDevice::host:{ + tablePtr = std::make_shared(fData, cRowCount, cColCount, + detail::empty_delete(), + getDataLayout(cLayout)); + break; + } + case ComputeDevice::cpu: + case ComputeDevice::gpu:{ + auto queue = getQueue(device); + auto data = sycl::malloc_shared(cRowCount * cColCount, queue); + queue.memcpy(data, fData, sizeof(double) * cRowCount * cColCount).wait(); + tablePtr = std::make_shared(queue, data, cRowCount, cColCount, + detail::make_default_delete(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(dataPtr); if (fData == NULL) { std::cout << "Error: unable to obtain critical array" << std::endl; diff --git a/mllib-dal/src/main/scala/com/intel/oap/mllib/OneDAL.scala b/mllib-dal/src/main/scala/com/intel/oap/mllib/OneDAL.scala index 63a40c08d..b887312d0 100644 --- a/mllib-dal/src/main/scala/com/intel/oap/mllib/OneDAL.scala +++ b/mllib-dal/src/main/scala/com/intel/oap/mllib/OneDAL.scala @@ -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 { @@ -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) } @@ -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 } @@ -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 }