From ee20f3c6b0db8685b6f7268c4e589ddbcb06dfd4 Mon Sep 17 00:00:00 2001 From: Tim Woodall Date: Mon, 25 Oct 2004 19:53:44 +0000 Subject: [PATCH] added a hash table indexed by ompi_process_name_t This commit was SVN r3316. --- src/class/Makefile.am | 2 + src/class/ompi_hash_table.h | 3 +- src/class/ompi_proc_table.c | 125 ++++++++++++++++++++++++++++++++++++ src/class/ompi_proc_table.h | 69 ++++++++++++++++++++ 4 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 src/class/ompi_proc_table.c create mode 100644 src/class/ompi_proc_table.h diff --git a/src/class/Makefile.am b/src/class/Makefile.am index 0f6916adc4..aad23b4778 100644 --- a/src/class/Makefile.am +++ b/src/class/Makefile.am @@ -16,6 +16,7 @@ headers = \ ompi_list.h \ ompi_object.h \ ompi_pointer_array.h \ + ompi_proc_table.h \ ompi_value_array.h \ ompi_rb_tree.h @@ -26,6 +27,7 @@ liblfc_la_SOURCES = \ ompi_list.c \ ompi_object.c \ ompi_pointer_array.c \ + ompi_proc_table.c \ ompi_bitmap.c \ ompi_value_array.c \ ompi_rb_tree.c diff --git a/src/class/ompi_hash_table.h b/src/class/ompi_hash_table.h index f9ce29b43d..cd2a45111a 100644 --- a/src/class/ompi_hash_table.h +++ b/src/class/ompi_hash_table.h @@ -29,8 +29,7 @@ struct ompi_hash_table_t { ompi_object_t super; /**< subclass of ompi_object_t */ ompi_list_t ht_nodes; /**< free list of hash nodes */ - ompi_list_t *ht_table; /**< each item is an array of - ompi_fhnode_t nodes */ + ompi_list_t *ht_table; /**< each item is an array of ompi_fhnode_t nodes */ size_t ht_table_size; /**< size of table */ size_t ht_size; /**< number of values on table */ size_t ht_mask; diff --git a/src/class/ompi_proc_table.c b/src/class/ompi_proc_table.c new file mode 100644 index 0000000000..dbca1c4f6f --- /dev/null +++ b/src/class/ompi_proc_table.c @@ -0,0 +1,125 @@ +/* + * $HEADER$ + */ + +#include "ompi_config.h" + +#include +#include + +#include "include/constants.h" +#include "util/output.h" +#include "class/ompi_proc_table.h" + + +/* + * ompi_process_name_hash_node_t + */ + +struct ompi_proc_hash_node_t +{ + ompi_list_item_t super; + ompi_process_name_t hn_key; + void *hn_value; +}; +typedef struct ompi_proc_hash_node_t ompi_proc_hash_node_t; + +static OBJ_CLASS_INSTANCE( + ompi_proc_hash_node_t, + ompi_list_item_t, + NULL, + NULL); + + +void* ompi_hash_table_get_proc(ompi_hash_table_t* ht, + const ompi_process_name_t* proc) +{ + uint32_t key = (proc->cellid << 24) + (proc->jobid << 16) + proc->vpid; + ompi_list_t* list = ht->ht_table + (key & ht->ht_mask); + ompi_proc_hash_node_t *node; + +#if OMPI_ENABLE_DEBUG + if(ht->ht_table_size == 0) { + ompi_output(0, "ompi_hash_table_get_proc:" + "ompi_hash_table_init() has not been called"); + return NULL; + } +#endif + for(node = (ompi_proc_hash_node_t*)ompi_list_get_first(list); + node != (ompi_proc_hash_node_t*)ompi_list_get_end(list); + node = (ompi_proc_hash_node_t*)ompi_list_get_next(node)) { + if (memcmp(&node->hn_key,proc,sizeof(ompi_process_name_t)) == 0) { + return node->hn_value; + } + } + return NULL; +} + + +int ompi_hash_table_set_proc( + ompi_hash_table_t* ht, + const ompi_process_name_t* proc, + void* value) +{ + uint32_t key = (proc->cellid << 24) + (proc->jobid << 16) + proc->vpid; + ompi_list_t* list = ht->ht_table + (key & ht->ht_mask); + ompi_proc_hash_node_t *node; + +#if OMPI_ENABLE_DEBUG + if(ht->ht_table_size == 0) { + ompi_output(0, "ompi_hash_table_set_value_proc:" + "ompi_hash_table_init() has not been called"); + return OMPI_ERR_BAD_PARAM; + } +#endif + for(node = (ompi_proc_hash_node_t*)ompi_list_get_first(list); + node != (ompi_proc_hash_node_t*)ompi_list_get_end(list); + node = (ompi_proc_hash_node_t*)ompi_list_get_next(node)) { + if (memcmp(&node->hn_key,proc,sizeof(ompi_process_name_t)) == 0) { + node->hn_value = value; + return OMPI_SUCCESS; + } + } + + node = (ompi_proc_hash_node_t*)ompi_list_remove_first(&ht->ht_nodes); + if(NULL == node) { + node = OBJ_NEW(ompi_proc_hash_node_t); + if(NULL == node) + return OMPI_ERR_OUT_OF_RESOURCE; + } + node->hn_key = *proc; + node->hn_value = value; + ompi_list_append(list, (ompi_list_item_t*)node); + ht->ht_size++; + return OMPI_SUCCESS; +} + + +int ompi_hash_table_remove_proc( + ompi_hash_table_t* ht, + const ompi_process_name_t* proc) +{ + uint32_t key = (proc->cellid << 24) + (proc->jobid << 16) + proc->vpid; + ompi_list_t* list = ht->ht_table + (key & ht->ht_mask); + ompi_proc_hash_node_t *node; + +#if OMPI_ENABLE_DEBUG + if(ht->ht_table_size == 0) { + ompi_output(0, "ompi_hash_table_remove_value_proc:" + "ompi_hash_table_init() has not been called"); + return OMPI_ERR_BAD_PARAM; + } +#endif + for(node = (ompi_proc_hash_node_t*)ompi_list_get_first(list); + node != (ompi_proc_hash_node_t*)ompi_list_get_end(list); + node = (ompi_proc_hash_node_t*)ompi_list_get_next(node)) { + if (memcmp(&node->hn_key,proc,sizeof(ompi_process_name_t)) == 0) { + ompi_list_remove_item(list, (ompi_list_item_t*)node); + ompi_list_append(&ht->ht_nodes, (ompi_list_item_t*)node); + ht->ht_size--; + return OMPI_SUCCESS; + } + } + return OMPI_ERR_NOT_FOUND; +} + diff --git a/src/class/ompi_proc_table.h b/src/class/ompi_proc_table.h new file mode 100644 index 0000000000..ef717a719e --- /dev/null +++ b/src/class/ompi_proc_table.h @@ -0,0 +1,69 @@ +/* + * $HEADER$ + * + */ + +/** @file + * + * A hash table indexed by ompi_process_name_t. + */ + +#ifndef OMPI_PROC_TABLE_H +#define OMPI_PROC_TABLE_H + +#include "class/ompi_hash_table.h" +#include "mca/ns/ns.h" + +#if defined(c_plusplus) || defined(__cplusplus) +extern "C" { +#endif + + +/** + * Retrieve value via ompi_process_name_t key. + * + * @param table The input hash table (IN). + * @param key The input key (IN). + * @return The value associated with the key or NULL if the item is not found. + * + */ + +OMPI_DECLSPEC void *ompi_hash_table_get_proc( + ompi_hash_table_t* table, + const ompi_process_name_t* key); + +/** + * Set value based on uint32_t key. + * + * @param table The input hash table (IN). + * @param key The input key (IN). + * @param value The value to be associated with the key (IN). + * @return OMPI return code. + * + */ + +OMPI_DECLSPEC int ompi_hash_table_set_proc( + ompi_hash_table_t* table, + const ompi_process_name_t*, + void* value); + +/** + * Remove value based on uint32_t key. + * + * @param table The input hash table (IN). + * @param key The input key (IN). + * @return OMPI return code. + * + */ + +OMPI_DECLSPEC int ompi_hash_table_remove_proc( + ompi_hash_table_t* table, + const ompi_process_name_t* key); + + +#if defined(c_plusplus) || defined(__cplusplus) +} +#endif + + +#endif /* OMPI_HASH_TABLE_H */