mnemonic-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject [1/2] incubator-mnemonic git commit: MNEMONIC-94: Durable native sorting service
Date Fri, 04 Nov 2016 20:57:01 GMT
Repository: incubator-mnemonic
Updated Branches:
  refs/heads/master fe50f5f2c -> 5dbb34da6


MNEMONIC-94: Durable native sorting service


Project: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/commit/4ca00c21
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/tree/4ca00c21
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/diff/4ca00c21

Branch: refs/heads/master
Commit: 4ca00c218605119e35e4b82443adbb6f835185d8
Parents: 2cec8e9
Author: Wang, Gang(Gary) <gang1.wang@intel.com>
Authored: Tue Nov 1 12:12:03 2016 -0700
Committer: Wang, Gang(Gary) <gang1.wang@intel.com>
Committed: Fri Nov 4 13:48:51 2016 -0700

----------------------------------------------------------------------
 .../internal/SortServiceImpl.java               |  60 +++++
 .../src/main/native/CMakeLists.txt              |   4 +-
 .../src/main/native/common.c                    |  81 ++++--
 .../src/main/native/common.h                    |   7 +-
 ...computingservice_internal_PrintServiceImpl.c |  11 +-
 ..._computingservice_internal_SortServiceImpl.c | 137 ++++++++++
 ..._computingservice_internal_SortServiceImpl.h |  48 ++++
 ...ice.computingservice.GeneralComputingService |   1 +
 .../DurableSinglyLinkedListNGPrintTest.java     |   8 +-
 .../DurableSinglyLinkedListNGSortTest.java      | 268 +++++++++++++++++++
 10 files changed, 588 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/main/java/org/apache/mnemonic/service/computingservice/internal/SortServiceImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/main/java/org/apache/mnemonic/service/computingservice/internal/SortServiceImpl.java
b/mnemonic-computing-services/mnemonic-utilities-service/src/main/java/org/apache/mnemonic/service/computingservice/internal/SortServiceImpl.java
new file mode 100644
index 0000000..ace277c
--- /dev/null
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/main/java/org/apache/mnemonic/service/computingservice/internal/SortServiceImpl.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.mnemonic.service.computingservice.internal;
+
+import org.apache.mnemonic.service.computingservice.GeneralComputingService;
+import org.apache.mnemonic.service.computingservice.ValueInfo;
+import org.flowcomputing.commons.primitives.NativeLibraryLoader;
+
+public class SortServiceImpl implements GeneralComputingService {
+  static {
+    try {
+      NativeLibraryLoader.loadFromJar("utilitiescomputing");
+    } catch (Exception e) {
+      throw new Error(e);
+    }
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public String getServiceId() {
+    return "sort";
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public long[] perform(ValueInfo[] valinfos) {
+    long[] ret = null;
+    if (null != valinfos) {
+      ret = nperformBubbleSort(valinfos);
+    }
+    return ret;
+  }
+
+  /**
+   * A native function to fulfill the action of print in native level.
+   * @param valinfos an array of value info, some of them could be set as NULL
+   * @return an array of handler returned by native level
+   */
+  protected native long[] nperformBubbleSort(ValueInfo[] valinfos);
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/CMakeLists.txt
b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/CMakeLists.txt
index afe1eda..fc93968 100644
--- a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/CMakeLists.txt
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/CMakeLists.txt
@@ -36,7 +36,9 @@ include_directories(${JNI_INCLUDE_DIRS})
 find_package(Threads REQUIRED)
 include_directories(${CMAKE_THREAD_LIBS_INIT})
 
-add_library(utilitiescomputing SHARED common.c org_apache_mnemonic_service_computingservice_internal_PrintServiceImpl.c)
+add_library(utilitiescomputing SHARED common.c
+  org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.c
+  org_apache_mnemonic_service_computingservice_internal_PrintServiceImpl.c)
 target_include_directories(utilitiescomputing PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
 
 include (InstallRequiredSystemLibraries)

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.c
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.c
b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.c
index cdc0df0..06d30c7 100644
--- a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.c
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.c
@@ -303,56 +303,85 @@ inline long to_p(JNIEnv* env, struct NValueInfo *nvinfo, void *e) {
   return -1L;
 }
 
-void iterMatrix(JNIEnv* env, struct NValueInfo *nvinfo,
-    size_t dims[], long *itmaddrs[], long hdls[],
+void iterTensor(JNIEnv* env, struct NValueInfo *nvinfo,
+    size_t dims[], long *itmaddrs[], long (* const nxtfitmaddrs)[],
+    long *(* const pendings)[], long hdls[],
     size_t dimidx, valueHandler valhandler) {
-  void *addr = NULL;
+  if (++dimidx >= nvinfo->framessz) {
+    return;
+  }
+  long *iatmp;
   long curoff = (nvinfo->frames + dimidx)->nextoff;
   long curnloff = (nvinfo->frames + dimidx)->nlvloff;
   long curnlsz = (nvinfo->frames + dimidx)->nlvlsz;
+  void *addr = NULL;
   if (dimidx < nvinfo->framessz - 1) {
-    while(0L != hdls[dimidx]) {
-      itmaddrs[dimidx + 1] = (long*)(to_e(env, nvinfo, hdls[dimidx]) + curnloff);
-      hdls[dimidx + 1] = *itmaddrs[dimidx + 1];
-      iterMatrix(env, nvinfo, dims, itmaddrs, hdls, dimidx + 1, valhandler);
-      itmaddrs[dimidx] = (long*)(to_e(env, nvinfo, hdls[dimidx]) + curoff);
-      hdls[dimidx] = *itmaddrs[dimidx];
-      ++dims[dimidx];
-    }
-    dims[dimidx] = 0;
-  } else {
-    if (-1L != curoff) {
+    iatmp = itmaddrs[dimidx];
+    while (1) {
+      (*pendings)[dimidx] = 0L;
       while(0L != hdls[dimidx]) {
-        addr = to_e(env, nvinfo, hdls[dimidx]) + curnloff;
-        valhandler(env, dims, dimidx, itmaddrs, addr, curnlsz, nvinfo->dtype);
-        itmaddrs[dimidx] = (long*)(to_e(env, nvinfo, hdls[dimidx]) + curoff);
+        itmaddrs[dimidx + 1] = (long*)(to_e(env, nvinfo, hdls[dimidx]) + curnloff);
+        hdls[dimidx + 1] = *itmaddrs[dimidx + 1];
+        (*nxtfitmaddrs)[dimidx] = (long*)(to_e(env, nvinfo, hdls[dimidx]) + curoff);
+        iterTensor(env, nvinfo, dims, itmaddrs, nxtfitmaddrs, pendings, hdls, dimidx, valhandler);
+        itmaddrs[dimidx] = (*nxtfitmaddrs)[dimidx];
         hdls[dimidx] = *itmaddrs[dimidx];
         ++dims[dimidx];
       }
       dims[dimidx] = 0;
+      if (0L == (*pendings)[dimidx]) {
+        break;
+      }
+      itmaddrs[dimidx] = iatmp;
+      hdls[dimidx] = *iatmp;
+    }
+  } else {
+    if (-1L != curoff) {
+      iatmp = itmaddrs[dimidx];
+      while (1) {
+        (*pendings)[dimidx] = 0L;
+        while(0L != hdls[dimidx]) {
+          addr = to_e(env, nvinfo, hdls[dimidx]) + curnloff;
+          (*nxtfitmaddrs)[dimidx] = (long*)(to_e(env, nvinfo, hdls[dimidx]) + curoff);
+          valhandler(env, dims, dimidx, itmaddrs, nxtfitmaddrs, pendings, addr, curnlsz,
nvinfo->dtype);
+          itmaddrs[dimidx] = (*nxtfitmaddrs)[dimidx];
+          hdls[dimidx] = *itmaddrs[dimidx];
+          ++dims[dimidx];
+        }
+        dims[dimidx] = 0;
+        if (0L == (*pendings)[dimidx]) {
+          break;
+        }
+        itmaddrs[dimidx] = iatmp;
+        hdls[dimidx] = *iatmp;
+      }
     } else {
       addr = to_e(env, nvinfo, hdls[dimidx]) + curnloff;
-      valhandler(env, dims, dimidx, itmaddrs, addr, curnlsz, nvinfo->dtype);
+      valhandler(env, dims, dimidx - 1, itmaddrs, nxtfitmaddrs, pendings, addr, curnlsz,
nvinfo->dtype);
     }
   }
 }
 
-int handleValueInfo(JNIEnv* env, struct NValueInfo *nvinfo, valueHandler valhandler) {
-  if (NULL == nvinfo->frames || 0 == nvinfo->framessz) {
-    return -1;
+long handleValueInfo(JNIEnv* env, struct NValueInfo *nvinfo, valueHandler valhandler) {
+  if (NULL == nvinfo->frames || 0 >= nvinfo->framessz) {
+    return 0L;
   }
   size_t dims[nvinfo->framessz];
   long *itmaddrs[nvinfo->framessz];
+  long *nxtfitmaddrs[nvinfo->framessz];
+  long pendings[nvinfo->framessz];
   long hdls[nvinfo->framessz];
-  size_t i, di = 0;
+  size_t i;
   for (i = 0; i < nvinfo->framessz; ++i) {
     dims[i] = 0;
     itmaddrs[i] = NULL;
+    nxtfitmaddrs[i] = NULL;
+    pendings[i] = 0L;
     hdls[i] = 0L;
   }
-  itmaddrs[di] = &nvinfo->handler;
-  hdls[di] = nvinfo->handler;
-  iterMatrix(env, nvinfo, dims, itmaddrs, hdls, 0, valhandler);
-  return 0;
+  hdls[0] = nvinfo->handler;
+  itmaddrs[0] = &nvinfo->handler;
+  iterTensor(env, nvinfo, dims, itmaddrs, &nxtfitmaddrs, &pendings, hdls, -1, valhandler);
+  return nvinfo->handler;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.h
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.h
b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.h
index be3149a..df69d36 100644
--- a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.h
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/common.h
@@ -34,6 +34,8 @@ extern "C" {
 #include <stdint.h>
 #include <assert.h>
 #include <pthread.h>
+#include <float.h>
+#include <math.h>
 #include <jni.h>
 
 #define DURABLE_BOOLEAN 1
@@ -81,7 +83,8 @@ struct NValueInfo {
  * a value handler to be callback for each value of a matrix
  */
 typedef void (*valueHandler)(JNIEnv* env, size_t dims[], size_t dimidx,
-    long *itmaddrs[], void *addr, size_t sz, int dtype);
+    long *itmaddrs[], long *(* const nxtfitmaddrs)[], long (* const pendings)[],
+    void *addr, size_t sz, int dtype);
 
 /**
  * construct a list of native value info structure from Java array object.
@@ -118,7 +121,7 @@ inline long to_p(JNIEnv* env, struct NValueInfo *nvinfo, void *e);
  * handle one native value info.
  * call-back value handler for each values of a value matrix.
  */
-int handleValueInfo(JNIEnv* env, struct NValueInfo *nvinfo, valueHandler valhandler);
+long handleValueInfo(JNIEnv* env, struct NValueInfo *nvinfo, valueHandler valhandler);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_PrintServiceImpl.c
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_PrintServiceImpl.c
b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_PrintServiceImpl.c
index 0209210..b1c687b 100644
--- a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_PrintServiceImpl.c
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_PrintServiceImpl.c
@@ -26,8 +26,9 @@
  * a customized handler as value handler.
  * It handles would be used to iteratively callback for each value of a value matrix
  */
-void valHandler(JNIEnv* env, size_t dims[], size_t dimidx,
-    long *itmaddrs[], void *addr, size_t sz, int dtype) {
+void valPrintHandler(JNIEnv* env, size_t dims[], size_t dimidx,
+    long *itmaddrs[], long *(* const nxtfitmaddrs)[], long (* const pendings)[],
+    void *addr, size_t sz, int dtype) {
   size_t i;
   if (0 == dims[dimidx]) {
     printf("\n");
@@ -35,7 +36,7 @@ void valHandler(JNIEnv* env, size_t dims[], size_t dimidx,
   for (i = 0; i <= dimidx; ++i) {
     printf("[%zu]", dims[i]);
   }
-  printf(" <%p> ", itmaddrs[dimidx]);
+  printf(" <%p, %p> ", itmaddrs[dimidx], (*nxtfitmaddrs)[dimidx]);
   switch(dtype) {
   case DURABLE_BOOLEAN:
     printf(" %s, ", (0 == *(char*)addr ? "T" : "F"));
@@ -88,7 +89,7 @@ jlongArray JNICALL Java_org_apache_mnemonic_service_computingservice_internal_Pr
   for(idx = 0; idx < visz; ++idx) {
     printf("-- Value Matrix #%u --\n", idx);
     if (NULL != nvinfos + idx) {
-      handleValueInfo(env, *(nvinfos + idx), valHandler);
+      handleValueInfo(env, *(nvinfos + idx), valPrintHandler);
     } else {
       printf("NULL\n");
     }
@@ -99,5 +100,7 @@ jlongArray JNICALL Java_org_apache_mnemonic_service_computingservice_internal_Pr
   return ret;
 }
 
+/*
 __attribute__((destructor)) void fini(void) {
 }
+*/

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.c
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.c
b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.c
new file mode 100644
index 0000000..40fda19
--- /dev/null
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.c
@@ -0,0 +1,137 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 "org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.h"
+
+/******************************************************************************
+ ** JNI implementations
+ *****************************************************************************/
+
+int compareLongValue(long val1, long val2) {
+  if (val1 == val2) {
+    return 0;
+  }
+  return val1 < val2 ? -1 : 1;
+}
+
+int compareDoubleValue(double val1, double val2) {
+  if (fabs(val1 - val2) <= FLT_EPSILON) {
+    return 0;
+  }
+  return val1 < val2 ? -1 : 1;
+}
+
+int compareValues(int dtype, void *val1ptr, void *val2ptr) {
+  int ret = 0;
+  if (NULL != val1ptr && NULL != val2ptr) {
+    switch(dtype) {
+    case DURABLE_BOOLEAN:
+    case DURABLE_CHARACTER:
+    case DURABLE_BYTE:
+      ret = memcmp(val1ptr, val2ptr, 1);
+      break;
+    case DURABLE_SHORT:
+      ret = compareLongValue(*(short*)val1ptr, *(short*)val2ptr);
+      break;
+    case DURABLE_INTEGER:
+      ret = compareLongValue(*(int*)val1ptr, *(int*)val2ptr);
+      break;
+    case DURABLE_LONG:
+      ret = compareLongValue(*(long*)val1ptr, *(long*)val2ptr);
+      break;
+    case DURABLE_FLOAT:
+      ret = compareDoubleValue(*(float*)val1ptr, *(float*)val2ptr);
+      break;
+    case DURABLE_DOUBLE:
+      ret = compareDoubleValue(*(double*)val1ptr, *(double*)val2ptr);
+      break;
+    }
+  }
+  return ret;
+}
+
+/**
+ * a customized handler as value handler.
+ * It handles would be used to iteratively callback for each value of a value matrix
+ */
+void valSimpleSortHandler(JNIEnv* env, size_t dims[], size_t dimidx,
+    long *itmaddrs[], long *(* const nxtfitmaddrs)[], long (* const pendings)[],
+    void *addr, size_t sz, int dtype) {
+  static long *hptr1 = NULL, *hptr2 = NULL;
+  static void *pvaladdr = NULL;
+  long *tmpptr = NULL;
+  long tmpval = 0L;
+  if (dimidx >= 0) {
+    if (NULL == pvaladdr) {
+      pvaladdr = addr;
+      hptr2 = itmaddrs[dimidx];
+    } else {
+      hptr1 = hptr2;
+      hptr2 = itmaddrs[dimidx];
+      if (compareValues(dtype, pvaladdr, addr) > 0) {
+        tmpptr = (*nxtfitmaddrs)[dimidx];
+        tmpval = *tmpptr;
+        *tmpptr = *hptr1;
+        *hptr1 = *hptr2;
+        *hptr2 = tmpval;
+        (*nxtfitmaddrs)[dimidx] = hptr2;
+        hptr2 = tmpptr;
+        ++(*pendings)[dimidx];
+      } else {
+        pvaladdr = addr;
+      }
+    }
+    if (0L == *(*nxtfitmaddrs)[dimidx]) {
+      pvaladdr = NULL;
+    }
+  }
+}
+
+/**
+ * It is invoked by Java side computing service.
+ * a step of computation should be handle in a whole by this function.
+ * param. vinfos contains all relevant value matrixes for this computation.
+ * It could return a set of handlers as results or in-place update the data indicated by
value matrixes.
+ */
+JNIEXPORT
+jlongArray JNICALL Java_org_apache_mnemonic_service_computingservice_internal_SortServiceImpl_nperformBubbleSort(JNIEnv*
env,
+    jobject this, jobjectArray vinfos) {
+  jlongArray ret = NULL;
+  jsize retsz = 1;
+  jsize idx;
+  size_t visz;
+  struct NValueInfo **nvinfos = constructNValueInfos(env, vinfos, &visz);
+  retsz = visz;
+  jlong nret[retsz];
+  for(idx = 0; idx < retsz; ++idx) {
+    nret[idx] = 0L;
+  }
+  printNValueInfos(nvinfos, visz);
+  for(idx = 0; idx < visz; ++idx) {
+    printf("-- Value Matrix #%u --\n", idx);
+    if (NULL != nvinfos + idx) {
+      nret[idx] = handleValueInfo(env, *(nvinfos + idx), valSimpleSortHandler);
+    } else {
+      printf("NULL\n");
+    }
+  }
+
+  ret = constructJLongArray(env, nret, retsz);
+  destructNValueInfos(nvinfos, visz);
+  return ret;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.h
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.h
b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.h
new file mode 100644
index 0000000..9204c98
--- /dev/null
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/main/native/org_apache_mnemonic_service_computingservice_internal_SortServiceImpl.h
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 <jni.h>
+/* Header for class SortServiceImpl */
+
+#include "common.h"
+
+#ifndef _Included_org_apache_mnemonic_service_computingservice_internal_SortServiceImpl
+#define _Included_org_apache_mnemonic_service_computingservice_internal_SortServiceImpl
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class:     org_apache_mnemonic_service_computingservice_internal_SortServiceImpl
+ * Method:    jniInit
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_computingservice_internal_SortServiceImpl_jniInit
+(JNIEnv *, jobject, jint, jint);
+
+/*
+ * Class:     org_apache_mnemonic_service_computingservice_internal_SortServiceImpl
+ * Method:    jniTerm
+ * Signature: ()V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_SortServiceImpl_jniTerm
+(JNIEnv *, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.computingservice.GeneralComputingService
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.computingservice.GeneralComputingService
b/mnemonic-computing-services/mnemonic-utilities-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.computingservice.GeneralComputingService
index 05f8e1c..9f9b4a3 100644
--- a/mnemonic-computing-services/mnemonic-utilities-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.computingservice.GeneralComputingService
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.computingservice.GeneralComputingService
@@ -1 +1,2 @@
 org.apache.mnemonic.service.computingservice.internal.PrintServiceImpl
+org.apache.mnemonic.service.computingservice.internal.SortServiceImpl

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGPrintTest.java
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGPrintTest.java
b/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGPrintTest.java
index e2819cf..e387fff 100644
--- a/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGPrintTest.java
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGPrintTest.java
@@ -67,7 +67,7 @@ public class DurableSinglyLinkedListNGPrintTest {
   }
 
   @Test(enabled = true)
-  public void testLinkedNodeValueWithPerson() {
+  public void testDurableSinglyLinkedListWithPerson() {
 
     int elem_count = 10;
 
@@ -142,7 +142,7 @@ public class DurableSinglyLinkedListNGPrintTest {
   }
 
   @Test(enabled = true)
-  public void testLinkedNodeValueWithLinkedNodeValue() {
+  public void testDurableSinglyLinkedListValue() {
 
     int elem_count = 10;
     long slotKeyId = 10;
@@ -172,7 +172,7 @@ public class DurableSinglyLinkedListNGPrintTest {
 
     Long linkhandler = 0L;
 
-    System.out.printf(" Stage 1 -testLinkedNodeValueWithLinkedNodeValue--> \n");
+    System.out.printf(" --- Stage 1 Generated --- \n");
 
     pre_nextnv = null;
     Double val;
@@ -211,7 +211,7 @@ public class DurableSinglyLinkedListNGPrintTest {
     Iterator<DurableSinglyLinkedList<Double>> iter = linkedvals.iterator();
     Iterator<Double> elemiter = null;
 
-    System.out.printf(" Stage 2 -testLinkedNodeValueWithLinkedNodeValue--> \n");
+    System.out.printf(" --- Stage 2 Restored --- \n");
     while (iter.hasNext()) {
       elemiter = iter.next().iterator();
       while (elemiter.hasNext()) {

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/4ca00c21/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGSortTest.java
----------------------------------------------------------------------
diff --git a/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGSortTest.java
b/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGSortTest.java
new file mode 100644
index 0000000..72a92e4
--- /dev/null
+++ b/mnemonic-computing-services/mnemonic-utilities-service/src/test/java/org/apache/mnemonic/service/computingservice/DurableSinglyLinkedListNGSortTest.java
@@ -0,0 +1,268 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.mnemonic.service.computingservice;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Random;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import org.apache.mnemonic.NonVolatileMemAllocator;
+import org.apache.mnemonic.RestorableAllocator;
+import org.apache.mnemonic.Durable;
+import org.apache.mnemonic.EntityFactoryProxy;
+import org.apache.mnemonic.Utils;
+import org.apache.mnemonic.DurableType;
+import org.apache.mnemonic.collections.DurableSinglyLinkedList;
+import org.apache.mnemonic.collections.DurableSinglyLinkedListFactory;
+
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.testng.Assert;
+
+/**
+ * A Test case suit, verify and demo the durable native computing services (DNCS) infra.
+ *
+ */
+
+public class DurableSinglyLinkedListNGSortTest {
+  public static String uri = "./pobj_NodeValue_sorting.dat";
+  private long cKEYCAPACITY;
+  private Random m_rand;
+  private NonVolatileMemAllocator m_act;
+
+  @BeforeClass
+  public void setUp() throws IOException {
+    m_rand = Utils.createRandom();
+    Files.deleteIfExists(Paths.get(uri));
+    m_act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
1024 * 1024 * 1024,
+        uri, true);
+    cKEYCAPACITY = m_act.handlerCapacity();
+    for (long i = 0; i < cKEYCAPACITY; ++i) {
+      m_act.setHandler(i, 0L);
+    }
+  }
+
+  @AfterClass
+  public void tearDown() {
+    m_act.close();
+  }
+
+  @Test(enabled = true)
+  public void testDurableSinglyLinkedListWithPerson() {
+
+    int elem_count = 20;
+
+    DurableType listgftypes[] = {DurableType.DURABLE};
+    EntityFactoryProxy listefproxies[] = {new EntityFactoryProxy() {
+      @Override
+      public <A extends RestorableAllocator<A>> Durable restore(A allocator,
EntityFactoryProxy[] factoryproxys,
+          DurableType[] gfields, long phandler, boolean autoreclaim) {
+        return PersonFactory.restore(allocator, factoryproxys, gfields, phandler, autoreclaim);
+      }
+    } };
+
+    DurableSinglyLinkedList<Person<Long>> firstnv = DurableSinglyLinkedListFactory.create(m_act,
listefproxies, 
+        listgftypes, false);
+
+    DurableSinglyLinkedList<Person<Long>> nextnv = firstnv;
+
+    Person<Long> person = null;
+    long val;
+    DurableSinglyLinkedList<Person<Long>> newnv;
+    for (int i = 0; i < elem_count; ++i) {
+      person = PersonFactory.create(m_act);
+      person.setAge((short) m_rand.nextInt(50));
+      person.setName(String.format("Name: [%s]", Utils.genRandomString()), true);
+      nextnv.setItem(person, false);
+      if (i + 1 == elem_count) {
+        break;
+      }
+      newnv = DurableSinglyLinkedListFactory.create(m_act, listefproxies, listgftypes, false);
+      nextnv.setNext(newnv, false);
+      nextnv = newnv;
+    }
+
+    Person<Long> eval;
+    DurableSinglyLinkedList<Person<Long>> iternv = firstnv;
+    System.out.printf(" -- Stage 1 Generated---\n");
+    while (null != iternv) {
+      eval = iternv.getItem();
+      if (null != eval) {
+        eval.testOutputAge();
+      }
+      iternv = iternv.getNext();
+    }
+    System.out.printf("\n");
+
+    long handler = firstnv.getHandler();
+
+    DurableSinglyLinkedList<Person<Long>> firstnv2 = DurableSinglyLinkedListFactory.restore(m_act,
listefproxies, 
+        listgftypes, handler, false);
+
+    System.out.printf("--- Stage 2 Restored--- \n");
+    for (Person<Long> eval2 : firstnv2) {
+      if (null != eval2) {
+        eval2.testOutputAge();
+      }
+    }
+    System.out.printf("\n");
+
+    GeneralComputingService gcsvr = Utils.getGeneralComputingService("sort");
+    ValueInfo vinfo = new ValueInfo();
+    List<long[][]> objstack = new ArrayList<long[][]>();
+    objstack.add(firstnv.getNativeFieldInfo());
+    objstack.add(person.getNativeFieldInfo());
+    long[][] fidinfostack = {{2L, 1L}, {0L, 1L}};
+    vinfo.handler = handler;
+    vinfo.transtable = m_act.getTranslateTable();
+    vinfo.dtype = DurableType.SHORT;
+    vinfo.frames = Utils.genNativeParamForm(objstack, fidinfostack);
+    ValueInfo[] vinfos = {vinfo};
+    long[] ret = gcsvr.perform(vinfos);
+
+    Assert.assertEquals(1, ret.length);
+    long handler2 = ret[0];
+    Assert.assertNotEquals(0L, handler2);
+
+    DurableSinglyLinkedList<Person<Long>> firstnv3 = DurableSinglyLinkedListFactory.restore(m_act,
listefproxies, 
+        listgftypes, handler2, false);
+
+    System.out.printf("--- Stage 3 Sorted--- \n");
+    for (Person<Long> eval3 : firstnv3) {
+      if (null != eval3) {
+        eval3.testOutputAge();
+      }
+    }
+    System.out.printf("\n");
+  }
+
+  @Test(enabled = true)
+  public void testDurableSinglyLinkedListValue() {
+
+    int elem_count = 20;
+    long slotKeyId = 10;
+
+    DurableType[] elem_gftypes = {DurableType.DOUBLE};
+    EntityFactoryProxy[] elem_efproxies = null;
+
+    DurableType linkedgftypes[] = {DurableType.DURABLE, DurableType.DOUBLE};
+    EntityFactoryProxy linkedefproxies[] = {new EntityFactoryProxy() {
+      @Override
+      public <A extends RestorableAllocator<A>> Durable restore(A allocator,
EntityFactoryProxy[] factoryproxys,
+          DurableType[] gfields, long phandler, boolean autoreclaim) {
+        EntityFactoryProxy[] val_efproxies = null;
+        DurableType[] val_gftypes = null;
+        if (null != factoryproxys && factoryproxys.length >= 2) {
+          val_efproxies = Arrays.copyOfRange(factoryproxys, 1, factoryproxys.length);
+        }
+        if (null != gfields && gfields.length >= 2) {
+          val_gftypes = Arrays.copyOfRange(gfields, 1, gfields.length);
+        }
+        return DurableSinglyLinkedListFactory.restore(allocator, val_efproxies, val_gftypes,
phandler, autoreclaim);
+      }
+    } };
+
+    DurableSinglyLinkedList<DurableSinglyLinkedList<Double>> nextnv = null, pre_nextnv
= null;
+    DurableSinglyLinkedList<Double> elem = null, pre_elem = null, first_elem = null;
+
+    Long linkhandler = 0L;
+
+    System.out.printf(" --- Stage 1 Generated --- \n");
+
+    pre_nextnv = null;
+    Double val;
+    for (int i = 0; i < elem_count; ++i) {
+      first_elem = null;
+      pre_elem = null;
+      for (int v = 0; v < 12; ++v) {
+        elem = DurableSinglyLinkedListFactory.create(m_act, elem_efproxies, elem_gftypes,
false);
+        val = m_rand.nextDouble();
+        elem.setItem(val, false);
+        if (null == pre_elem) {
+          first_elem = elem;
+        } else {
+          pre_elem.setNext(elem, false);
+        }
+        pre_elem = elem;
+        System.out.printf("%f ", val);
+      }
+
+      nextnv = DurableSinglyLinkedListFactory.create(m_act, linkedefproxies, linkedgftypes,
false);
+      nextnv.setItem(first_elem, false);
+      if (null == pre_nextnv) {
+        linkhandler = nextnv.getHandler();
+      } else {
+        pre_nextnv.setNext(nextnv, false);
+      }
+      pre_nextnv = nextnv;
+      System.out.printf(" generated an item... \n");
+    }
+    m_act.setHandler(slotKeyId, linkhandler);
+
+    long handler = m_act.getHandler(slotKeyId);
+
+    DurableSinglyLinkedList<DurableSinglyLinkedList<Double>> linkedvals = DurableSinglyLinkedListFactory.restore(m_act,
+        linkedefproxies, linkedgftypes, handler, false);
+    Iterator<DurableSinglyLinkedList<Double>> iter = linkedvals.iterator();
+    Iterator<Double> elemiter = null;
+
+    System.out.printf(" --- Stage 2 Restored --- \n");
+    while (iter.hasNext()) {
+      elemiter = iter.next().iterator();
+      while (elemiter.hasNext()) {
+        System.out.printf("%f ", elemiter.next());
+      }
+      System.out.printf(" Fetched an item... \n");
+    }
+
+    GeneralComputingService gcsvr = Utils.getGeneralComputingService("sort");
+    ValueInfo vinfo = new ValueInfo();
+    List<long[][]> objstack = new ArrayList<long[][]>();
+    objstack.add(linkedvals.getNativeFieldInfo());
+    objstack.add(linkedvals.getNativeFieldInfo());
+    long[][] fidinfostack = {{2L, 1L}, {2L, 1L}};
+    vinfo.handler = handler;
+    vinfo.transtable = m_act.getTranslateTable();
+    vinfo.dtype = DurableType.DOUBLE;
+    vinfo.frames = Utils.genNativeParamForm(objstack, fidinfostack);
+    ValueInfo[] vinfos = {vinfo};
+    gcsvr.perform(vinfos);
+
+    // Assert.assert, expected);(plist, plist2);
+    DurableSinglyLinkedList<DurableSinglyLinkedList<Double>> linkedvals3 = DurableSinglyLinkedListFactory.restore(m_act,
+        linkedefproxies, linkedgftypes, handler, false);
+    Iterator<DurableSinglyLinkedList<Double>> iter3 = linkedvals3.iterator();
+    Iterator<Double> elemiter3 = null;
+
+    System.out.printf(" --- Stage 3 Sorted --- \n");
+    while (iter3.hasNext()) {
+      elemiter3 = iter3.next().iterator();
+      while (elemiter3.hasNext()) {
+        System.out.printf("%f ", elemiter3.next());
+      }
+      System.out.printf(" Fetched an item... \n");
+    }
+
+  }
+
+}


Mime
View raw message