[NPUdrvAPI/test] Bug fix for npu open name
authorParichay Kapoor <pk.kapoor@samsung.com>
Thu, 22 Aug 2019 12:29:27 +0000 (21:29 +0900)
committer함명주/On-Device Lab(SR)/Principal Engineer/삼성전자 <myungjoo.ham@samsung.com>
Thu, 29 Aug 2019 02:34:41 +0000 (11:34 +0900)
Added bug fix for opening npu device name
Added check for npu test cases based on testmode or not
This fix makes npu dev_open_close work with npu fast model simulator

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
src/core/npu/NPUdrvAPI.c
src/test/unittests/ne_core_npu_test.cpp

index 5df0e72..99c57d0 100644 (file)
@@ -72,7 +72,6 @@ typedef struct
 
 /**
  * global values maintain all the opened npus
- * currently only 1 NPU is supported
  * extened npu_fd_table to a table to support multiple devices
  */
 static npu_api_priv napi_priv;
@@ -185,6 +184,35 @@ static int verify_size32 (uint64_t value)
 }
 
 /**
+ * @brief open npu device with prefix
+ * @param[in] dev_id index of the device to be opened
+ * @param[in] prefix the prefix of the npu driver
+ * @return file descriptor is not error, otherwise -1 with errno set
+ */
+static int _npu_open (const unsigned int dev_id, const char *prefix)
+{
+  /**
+   * 0th device can be srnpu0 or srnpu (srnpu has higher preference in search)
+   * However the next device will have to start from srnpu1 to srnpuN
+   */
+  int fd;
+  char name[64];
+
+  /** open srnpu and return for 0th device */
+  if (dev_id == 0) {
+    if ((fd = open(prefix, O_RDWR)) >= 0) {
+      return fd;
+    }
+  }
+
+  /** open srnpu${dev_id} for other dev_id */
+  snprintf(name, sizeof(name), "%s%u", prefix, dev_id);
+  fd = open(name, O_RDWR);
+
+  return fd;
+}
+
+/**
  * @brief open the npu device
  * @param[in] dev_id index of the device to be opened
  * @return file descriptor is not error. otherwise errno
@@ -192,14 +220,14 @@ static int verify_size32 (uint64_t value)
 int npu_open (unsigned int dev_id)
 {
   int fd;
-  char name[64];
+  char prefix[64];
   int status;
   errno = 0;
 
-  snprintf(name, sizeof(name), "%s/%s%u", DEV_BASE_PATH, DEV_NAME, dev_id);
+  snprintf(prefix, sizeof(prefix), "%s/%s", DEV_BASE_PATH, DEV_NAME);
 
   if (conf->test_mode == 0) {
-    fd = open(name, O_RDWR);
+    fd = _npu_open (dev_id, prefix);
   } else {
     fd = dev_id;
   }
index e3c9c08..a4f3c12 100644 (file)
@@ -45,36 +45,50 @@ TEST (ne_core_npu_test, dev_open_close)
   /** open/close multiple devices at once */
   fd = npu_open (DEV_FD);
   EXPECT_EQ (fd, DEV_FD);
-  fd = npu_open (1);
-  EXPECT_EQ (fd, 1);
-  fd = npu_open (2);
-  EXPECT_EQ (fd, 2);
-  fd = npu_open (3);
-  EXPECT_EQ (fd, 3);
-  /** open more than allowed devices */
-  fd = npu_open (16);
-  EXPECT_LT (fd, RET_SUCCESS);
-  /** close unopened devices */
-  ret = npu_close (17);
-  EXPECT_LT (ret, RET_SUCCESS);
-
-  /** close device twice */
-  ret = npu_close (1);
-  EXPECT_EQ (ret, RET_SUCCESS);
-  ret = npu_close (1);
-  EXPECT_LT (ret, RET_SUCCESS);
-
-  ret = npu_close (3);
-  EXPECT_EQ (ret, RET_SUCCESS);
-  ret = npu_close (2);
-  EXPECT_EQ (ret, RET_SUCCESS);
-  ret = npu_close (DEV_FD);
-  EXPECT_EQ (ret, RET_SUCCESS);
-
-  /** reopen closed device */
-  fd = npu_open (1);
-  EXPECT_EQ (fd, 1);
-  ret = npu_close (fd);
+  if (conf->test_mode == 1) {
+    /**
+     * Opening non-existing devices only passes in test mode to test
+     * functionality of NPUdrvAPI
+     */
+    fd = npu_open (1);
+    EXPECT_EQ (fd, 1);
+    fd = npu_open (2);
+    EXPECT_EQ (fd, 2);
+    fd = npu_open (3);
+    EXPECT_EQ (fd, 3);
+    /** open more than allowed devices */
+    fd = npu_open (16);
+    EXPECT_LT (fd, RET_SUCCESS);
+    /** close unopened devices */
+    ret = npu_close (17);
+    EXPECT_LT (ret, RET_SUCCESS);
+
+    /** close device twice */
+    ret = npu_close (1);
+    EXPECT_EQ (ret, RET_SUCCESS);
+    ret = npu_close (1);
+    EXPECT_LT (ret, RET_SUCCESS);
+
+    ret = npu_close (3);
+    EXPECT_EQ (ret, RET_SUCCESS);
+    ret = npu_close (2);
+    EXPECT_EQ (ret, RET_SUCCESS);
+    ret = npu_close (DEV_FD);
+    EXPECT_EQ (ret, RET_SUCCESS);
+
+    /** reopen closed device */
+    fd = npu_open (1);
+    EXPECT_EQ (fd, 1);
+    ret = npu_close (fd);
+  } else {
+    ret = npu_close (DEV_FD);
+    EXPECT_EQ (ret, RET_SUCCESS);
+    fd = npu_open (1);
+    EXPECT_LT (fd, RET_SUCCESS);
+    /** open more than allowed devices */
+    fd = npu_open (16);
+    EXPECT_LT (fd, RET_SUCCESS);
+  }
   EXPECT_EQ (ret, RET_SUCCESS);
 }