[Filter] add a hash table for shared key
authorHyoung Joo Ahn <hello.ahn@samsung.com>
Tue, 29 Jun 2021 15:15:29 +0000 (00:15 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Sun, 29 Aug 2021 13:04:31 +0000 (22:04 +0900)
[Filter] add a hash table for shared key

Signed-off-by: Hyoung Joo Ahn <hello.ahn@samsung.com>
gst/nnstreamer/include/nnstreamer_plugin_api_filter.h
gst/nnstreamer/tensor_filter/tensor_filter_common.c
gst/nnstreamer/tensor_filter/tensor_filter_common.h

index 8e94f5b..da534dc 100644 (file)
@@ -567,6 +567,54 @@ extern accl_hw parse_accl_hw_fill (parse_accl_args accl_args);
  */
 #define parse_accl_hw(...) parse_accl_hw_fill((parse_accl_args){__VA_ARGS__})
 
+/* extern functions for shared model representation table */
+/**
+ * @brief Insert the new shared model representation at the table.
+ * @param[in] key The new key to store at hash table.
+ * @param[in] value The new value to store at hash table.
+ * @param[in] instance The instance that is sharing the model representation. It will be registered at the referred list.
+ * @return 0 if the new key and value are inserted. Others are failed to insert it.
+ */
+extern int
+nnstreamer_filter_shared_table_insert (char *key, void *value, void *instance);
+
+/* extern functions for shared model representation table */
+/**
+ * @brief Remove the shared model representation at the table.
+ * @param[in] key The key to remove the matched value of hash table.
+ * @return TRUE if the new key and value are removed. FALSE if failed to remove it.
+ */
+extern int
+nnstreamer_filter_shared_table_remove (const char *key);
+
+/* extern functions for shared model representation table */
+/**
+ * @brief Remove the instance registered at the referred list of shared model table.
+ * @param[in] key The key to find the matched list of hash table.
+ * @param[in] instance The instance that should be removed from the referred list.
+ * @return TRUE if the new key and value are removed. FALSE if failed to remove it.
+ */
+extern int
+nnstreamer_filter_shared_table_remove_this (const char *key, const void *instance);
+
+/* extern functions for shared model representation table */
+/**
+ * @brief Get the length of the referred list.
+ * @param[in] key The key to find the matched list of hash table.
+ * @return The length of the list. If it is NULL, it returns -1
+ */
+extern int
+nnstreamer_filter_referred_list_length (const char *key);
+
+/* extern functions for shared model representation table */
+/**
+ * @brief Get the shared model representation that is already shared and has the same key.
+ * @param[in] key The key to find the matched shared representation.
+ * @return The model representation. NULL if it is failed.
+ */
+extern void *
+nnstreamer_filter_get_shared_model_representation (const char *key);
+
 #ifdef __cplusplus
 }
 #endif
index 300d91c..8e52ffc 100644 (file)
@@ -92,6 +92,8 @@ static gint _gtfc_setprop_IS_UPDATABLE (GstTensorFilterPrivate * priv,
 static gint _gtfc_setprop_ACCELERATOR (GstTensorFilterPrivate * priv,
     GstTensorFilterProperties * prop, const GValue * value);
 
+static GHashTable *shared_model_table = NULL;
+
 /**
  * @brief GstTensorFilter properties.
  */
@@ -1051,6 +1053,11 @@ gst_tensor_filter_common_free_property (GstTensorFilterPrivate * priv)
       g_free (latency);
     g_queue_free (queue);
   }
+
+  if (shared_model_table) {
+    g_hash_table_destroy (shared_model_table);
+    shared_model_table = NULL;
+  }
 }
 
 /**
@@ -1859,6 +1866,9 @@ _gtfc_setprop_SHARED_TENSOR_FILTER_KEY (GstTensorFilterProperties * prop,
   g_free (prop->shared_tensor_filter_key);
   prop->shared_tensor_filter_key = g_value_dup_string (value);
 
+  if (!shared_model_table)
+    shared_model_table = g_hash_table_new (g_str_hash, g_str_equal);
+
   return 0;
 }
 
@@ -2828,3 +2838,162 @@ gst_tensor_filter_check_hw_availability (const gchar * name, const accl_hw hw,
 
   return available;
 }
+
+/* extern functions for shared model representation table */
+/**
+ * @brief Insert the new shared model representation at the table.
+ * @param[in] key The new key to store at hash table.
+ * @param[in] value The new value to store at hash table.
+ * @param[in] instance The instance that is sharing the model representation. It will be registered at the referred list.
+ * @return 0 if the new key and value are inserted. Others are failed to insert it.
+ */
+int
+nnstreamer_filter_shared_table_insert (char *key, void *value, void *instance)
+{
+  GstTensorFilterSharedModelRepresenatation *model_rep;
+  gboolean is_existed = FALSE;
+
+  if (!shared_model_table) {
+    ml_loge ("The shared model representation is not supported properly!");
+    return FALSE;
+  }
+  if (!key) {
+    ml_loge ("The key should NOT be NULL!");
+    return FALSE;
+  }
+  if (!instance) {
+    ml_loge ("The instance should NOT be NULL!");
+    return FALSE;
+  }
+
+  model_rep = g_hash_table_lookup (shared_model_table, key);
+  is_existed = model_rep != NULL;
+
+  if (is_existed) {
+    model_rep->referred_list =
+        g_list_append (model_rep->referred_list, instance);
+    return TRUE;
+  } else {
+    if (!value) {
+      ml_loge ("The value should NOT be NULL!");
+      return FALSE;
+    }
+
+    model_rep = (GstTensorFilterSharedModelRepresenatation *)
+        g_malloc0 (sizeof (GstTensorFilterSharedModelRepresenatation));
+    model_rep->shared_interpreter = value;
+    model_rep->referred_list =
+        g_list_append (model_rep->referred_list, instance);
+
+    return g_hash_table_insert (shared_model_table, key, (gpointer) model_rep);
+  }
+}
+
+/* extern functions for shared model representation table */
+/**
+ * @brief remove the shared model representation at the table.
+ * @param[in] key The key to remove the matched value of hash table.
+ * @return 0 if the new key and value are removed. Others are failed to remove it.
+ */
+int
+nnstreamer_filter_shared_table_remove (const char *key)
+{
+  GstTensorFilterSharedModelRepresenatation *value;
+
+  /* search the table with key */
+  if (!shared_model_table) {
+    ml_loge ("The shared model representation is not supported properly!");
+    return FALSE;
+  }
+
+  value = g_hash_table_lookup (shared_model_table, key);
+  if (!value) {
+    ml_loge ("There is no value of the key: %s", key);
+    return FALSE;
+  }
+
+  g_list_free (value->referred_list);
+  return g_hash_table_remove (shared_model_table, key);
+}
+
+/* extern functions for shared model representation table */
+/**
+ * @brief Remove the instance registered at the referred list of shared model table.
+ * @param[in] key The key to find the matched list of hash table.
+ * @param[in] instance The instance that should be removed from the referred list.
+ * @return TRUE if the new key and value are removed. FALSE if failed to remove it.
+ */
+int
+nnstreamer_filter_shared_table_remove_this (const char *key,
+    const void *instance)
+{
+  GstTensorFilterSharedModelRepresenatation *value;
+
+  /* search the table with key */
+  if (!shared_model_table) {
+    ml_loge ("The shared model representation is not supported properly!");
+    return FALSE;
+  }
+
+  value = g_hash_table_lookup (shared_model_table, key);
+  if (!value) {
+    ml_loge ("There is no value of the key: %s", key);
+    return FALSE;
+  }
+
+  /* remove instance from the list */
+  value->referred_list = g_list_remove (value->referred_list, instance);
+  ml_logd ("The referred instance of sharing key: %s has been removed!", key);
+
+  return TRUE;
+}
+
+/* extern functions for shared model representation table */
+/**
+ * @brief Get the length of the referred list.
+ * @param[in] key The key to find the matched list of hash table.
+ * @return The length of the list. If it is NULL, it returns -1
+ */
+int
+nnstreamer_filter_referred_list_length (const char *key)
+{
+  GstTensorFilterSharedModelRepresenatation *value;
+
+  /* search the table with key */
+  if (!shared_model_table) {
+    ml_loge ("The shared model representation is not supported properly!");
+    return -1;
+  }
+  value = g_hash_table_lookup (shared_model_table, key);
+  if (!value) {
+    ml_loge ("There is no value of the key: %s", key);
+    return -1;
+  }
+
+  return g_list_length (value->referred_list);
+}
+
+/* extern functions for shared model representation table */
+/**
+ * @brief Get the shared model representation that is already shared and has the same key.
+ * @param[in] key The key to find the matched shared representation.
+ * @return The model representation. NULL if it is failed.
+ */
+void *
+nnstreamer_filter_get_shared_model_representation (const char *key)
+{
+  GstTensorFilterSharedModelRepresenatation *value;
+
+  /* search the table with key */
+  if (!shared_model_table) {
+    ml_loge ("The shared model representation is not supported properly!");
+    return NULL;
+  }
+  value = g_hash_table_lookup (shared_model_table, key);
+  if (!value) {
+    ml_logi ("There is no value of the key: %s", key);
+    return NULL;
+  }
+
+  return value->shared_interpreter;
+}
index 4198ac5..d767438 100644 (file)
@@ -102,6 +102,14 @@ typedef struct _GstTensorFilterCombination
 } GstTensorFilterCombination;
 
 /**
+ * @brief Data Structure to store shared table
+ */
+typedef struct {
+  void *shared_interpreter; /**< the model representation for each sub-plugins */
+  GList *referred_list; /**< the referred list about the instances sharing the same key */
+} GstTensorFilterSharedModelRepresenatation;
+
+/**
  * @brief Structure definition for common tensor-filter properties.
  */
 typedef struct _GstTensorFilterPrivate