[ASR/IP] Revise getnumNPU/ASRdevice() to use common function
authorDongju Chae <dongju.chae@samsung.com>
Mon, 25 Nov 2019 07:12:27 +0000 (16:12 +0900)
committer송욱/On-Device Lab(SR)/Staff Engineer/삼성전자 <wook16.song@samsung.com>
Wed, 27 Nov 2019 04:48:35 +0000 (13:48 +0900)
This commit revises getnumNPU/ASRdevice() to use common function.
Also, getASRdevice() is implemented in a similar way to the NPU device.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
src/core/ip/plugin-comm-ip.c

index dfc0b93..905146c 100644 (file)
@@ -3,6 +3,7 @@
  * Copyright (C) 2019 Samsung Electronics
  * Copyright (C) 2019 MyungJoo Ham <myungjoo.ham@samsung.com>
  * Copyright (C) 2019 Parichay Kapoor <pk.kapoor@samsung.com>
+ * Copyright (C) 2019 Dongju Chae <dongju.chae@samsung.com>
  */
 /**
  * @file plugin-comm-ip.c
@@ -11,6 +12,7 @@
  * @see http://suprem.sec.samsung.net/confluence/display/ODLC/Software+Stack
  * @author MyungJoo Ham <myungjoo.ham@samsung.com>
  * @author Parichay Kapoor <pk.kapoor@samsung.com>
+ * @author Dongju Chae <dongju.chae@samsung.com>
  * @bug No known bugs except for NYI items
  *
  * @note This version does not support multi-model.
 #include <ne-model.h>
 #include <ne-conf.h>
 #include <ne-inputservice.h>
-#include <asr/asr.h>
 
 #define TAG _N11
 
 #define DEV_BASE_PATH "/dev"
-#define DEV_NAME "srnpu"
-#define DEV_NAME_SIZE 100
+#define DEV_NAME_NPU "srnpu"
+#define DEV_NAME_ASR "srasr"
+#define DEV_NAME_SIZE 128
+#define DEV_MAX_OFFSET 16
 #define DEFAULT_FORCE_STOP true
 
 #define MAX_NUM_MODELS 1
@@ -72,7 +75,6 @@ typedef struct {
 typedef struct {
   dev_type device_type;                   /**< NPU or ASR / Connection Type
                                                The first word MUST be this. */
-
   int device_id;                          /**< id of the device */
   int num_models;                         /**< num of models loaded */
   model_private *models[MAX_NUM_MODELS];  /**< private for each model */
@@ -85,7 +87,8 @@ typedef struct {
 
 /** @brief private data structure for the lib npu host */
 struct lib_npu_private {
-  npu_device *npu_dev;                    /**< device handle */
+  npu_device *npu_dev;         /**< npu device handle */
+  npu_device *asr_dev;         /**< asr device handle */
   int init_status;             /**< status of init ne handler */
   const hostHandlerInfo *host_handle;     /**< host handler */
 };
@@ -101,6 +104,7 @@ void fini_lib_npu (void) __attribute__ ((destructor(PLUGIN_PRIORITY)));
 void init_lib_npu (void)
 {
   libnpupriv.npu_dev = NULL;
+  libnpupriv.asr_dev = NULL;
   /** TODO: set conf path of npu-engine.ini file?? */
   libnpupriv.init_status = load_conf (NULL);
   if (libnpupriv.init_status == 0) {
@@ -143,65 +147,97 @@ static int model_dev_validity_check (npudev_h dev, uint32_t modelid)
 {
   npu_device *npu_dev;
 
-  if (dev == NULL || modelid >= libnpupriv.npu_dev->num_models) {
+  if (dev == NULL) {
     return -EINVAL;
   }
 
   npu_dev = dev;
-  if (npu_dev != libnpupriv.npu_dev) {
-    return -EINVAL;
-  }
+  if (npu_dev->device_type & DEVICETYPE_ASR) {
+     if (npu_dev != libnpupriv.asr_dev)
+      return -EINVAL;
 
-  if (npu_dev->device_type != NPUCOND_CONN_SOCIP)
-    return -EINVAL;
+    if (npu_dev->device_type != NPUCOND_ASR_CONN_SOCIP)
+      return -EINVAL;
+  } else {
+    if (modelid >= libnpupriv.npu_dev->num_models)
+      return -EINVAL;
+
+    if (npu_dev != libnpupriv.npu_dev)
+      return -EINVAL;
+
+    if (npu_dev->device_type != NPUCOND_CONN_SOCIP)
+      return -EINVAL;
+  }
 
   return 0;
 }
 
 /**
+ * @brief return the number of devices with the requested device name
+ * @param[in] base the base path of device
+ * @param[in] name the device name
+ * @return @c the number of devices
+ */
+static int getnumdevice(const char *base, const char *name)
+{
+  char filename[DEV_NAME_SIZE + 1];
+  int offset, ret;
+  int num_devices = 0;
+
+  /** assume that device has a suffix offset if there are multiple devices */
+  for (offset = 0; offset < DEV_MAX_OFFSET; offset++) {
+    ret = snprintf (filename, DEV_NAME_SIZE, "%s/%s%d", base, name, offset);
+    if (ret < 0)
+      return ret;
+
+    filename[ret] = '\x00';
+    if (access(filename, F_OK) == 0)
+      num_devices++;
+    else
+      break;
+  }
+
+  /** check a device wiht no-suffix name as well */
+  ret = snprintf (filename, DEV_NAME_SIZE, "%s/%s", base, name);
+  if (ret < 0)
+    return ret;
+
+  filename[ret] = '\x00';
+  if (access(filename, F_OK) == 0)
+    num_devices++;
+
+  return num_devices;
+}
+
+
+/**
  * @brief Returns the list of NPU devices.
  * @return @c The number of NPU devices.
  * @retval 0 if no NPU devices available. if positive (number of NPUs) if NPU devices available. otherwise, a negative error value.
  */
 int getnumNPUdevice(void)
 {
-  char filename[DEV_NAME_SIZE + 1];
-  char filename_wnum[DEV_NAME_SIZE + 1];
-  int status, status_wnum;
-  int err;
-
-  /** emul mode deos not need device */
 #if defined(ENABLE_EMUL)
+  /** test mode deos not need device */
   return 1;
 #endif
 
-  err = snprintf (filename, DEV_NAME_SIZE, "%s/%s", DEV_BASE_PATH, DEV_NAME);
-  if (err < 0) {
-    return err;
-  } else {
-    filename[DEV_NAME_SIZE - 1] = 0;
-  }
-
-  err = snprintf (filename_wnum, DEV_NAME_SIZE, "%s/%s0", DEV_BASE_PATH,
-      DEV_NAME);
-  if (err < 0) {
-    return err;
-  } else {
-    filename_wnum[DEV_NAME_SIZE - 1] = 0;
-  }
+  return getnumdevice(DEV_BASE_PATH, DEV_NAME_NPU);
+}
 
-  /* check if file exist for this device */
-  status = access(filename, F_OK);
-  status_wnum = access(filename_wnum, F_OK);
-  if (status == 0 || status_wnum == 0) {
-    err = 1;
-  } else if (status == -ENOENT && status_wnum == -ENOENT) {
-    err = 0;
-  } else {
-    err = status;
-  }
+/**
+ * @brief Returns the list of ASR devices.
+ * @return @c The number of ASR devices.
+ * @retval 0 if no ASR devices available. if positive (number of ASRs) if NPU devices available. otherwise, a negative error value.
+ */
+int getnumASRdevice(void)
+{
+#if defined(ENABLE_EMUL)
+  /** test mode deos not need device */
+  return 1;
+#endif
 
-  return err;
+  return getnumdevice(DEV_BASE_PATH, DEV_NAME_ASR);
 }
 
 /**
@@ -220,8 +256,9 @@ int getNPUdevice(npudev_h *dev, uint32_t id)
   if (num_devices < 0) {
     return num_devices;
   }
-  if (id < 0 || id >= num_devices) {
-    return -EINVAL;
+
+  if (id >= num_devices) {
+    return -ERANGE;
   }
 
   /** Current implementation only supports one NPU device at once */
@@ -256,6 +293,51 @@ int getNPUdevice(npudev_h *dev, uint32_t id)
 }
 
 /**
+ * @brief Returns the handle of the chosen ASR devices.
+ * @param[out] dev The ASR device handle
+ * @param[in] id The ASR id to get the handle. 0 <= id < getnumASRdevice().
+ * @return @c 0 if no error. otherwise a negative error value
+ */
+int getASRdevice(npudev_h *dev, uint32_t id)
+{
+  npu_device *asr_dev;
+  int num_devices;
+
+  num_devices = getnumASRdevice();
+  if (num_devices < 0) {
+    return num_devices;
+  }
+
+  if (id >= num_devices) {
+    return -ERANGE;
+  }
+
+  /** Current implementation only supports one ASR device at once */
+  if (libnpupriv.asr_dev != NULL) {
+    if (libnpupriv.asr_dev->device_id == id) {
+      *dev = (npudev_h) libnpupriv.asr_dev;
+      return 0;
+    } else {
+      return -EINVAL;
+    }
+  } else {
+    asr_dev = (npu_device *) malloc (sizeof (npu_device));
+    if (asr_dev == NULL) {
+      return -errno;
+    }
+    asr_dev->device_id = id;
+    asr_dev->sequence_in = 0;
+    asr_dev->sequence_out = 0;
+    asr_dev->device_type = NPUCOND_ASR_CONN_SOCIP;
+
+    libnpupriv.asr_dev = asr_dev;
+    *dev = (npudev_h) libnpupriv.asr_dev;
+  }
+
+  return 0;
+}
+
+/**
  * @brief Returns the handle of an NPU device meeting the condition
  * @param[out] dev The NPU device handle
  * @param[in] cond The condition for device search.
@@ -911,20 +993,17 @@ int runNPU_sync(npudev_h dev, uint32_t modelid, const input_buffers *input,
   int err;
   uint64_t sequence;
   npu_device *npu_dev;
-
   sync_cb_data cb_data = {
     .npu_dev = dev,
     .user_output = output,
   };
+
   npu_dev = dev;
 
   if (output == NULL || input == NULL || input->num_buffers == 0) {
     return -EINVAL;
   }
 
-  if (npu_dev->device_type & DEVICETYPE_ASR)
-    return runASR_sync(dev, input, output);
-
   err = runNPU_async_util(dev, modelid, input, sync_callback, &sequence, &cb_data,
       NPUASYNC_DROP_OLD);
   if (err < 0) {
@@ -959,7 +1038,6 @@ int runNPU_async(npudev_h dev, uint32_t modelid, const input_buffers *input,
     npu_async_mode mode)
 {
   int err;
-  npu_device *npu_dev = dev;
 
   if (sequence == NULL || input == NULL || input->num_buffers == 0) {
     return -EINVAL;
@@ -969,9 +1047,6 @@ int runNPU_async(npudev_h dev, uint32_t modelid, const input_buffers *input,
     return -EINVAL;
   }
 
-  if (npu_dev->device_type & DEVICETYPE_ASR)
-    return runASR_async(dev, input, cb, sequence, data, mode);
-
   err = runNPU_async_util (dev, modelid, input, cb, sequence, data, mode);
   if (err < 0) {
     return err;