From f75b88faa592ce4f3d239975df07cf6909cf5d01 Mon Sep 17 00:00:00 2001 From: Rich Graham Date: Mon, 16 Aug 2004 23:06:33 +0000 Subject: [PATCH] fix a bug, so that the fifo size is rounded up to a power of 2. Add some support code to determine nearest power of 2. This commit was SVN r2170. --- src/class/ompi_circular_buffer_fifo.h | 59 +++++++++++++++++++++++---- src/mca/mpool/mpool.h | 5 ++- src/util/Makefile.am | 2 + src/util/pow2.c | 42 +++++++++++++++++++ src/util/pow2.h | 19 +++++++++ 5 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 src/util/pow2.c create mode 100644 src/util/pow2.h diff --git a/src/class/ompi_circular_buffer_fifo.h b/src/class/ompi_circular_buffer_fifo.h index bb7558a192..2dde8b0e23 100644 --- a/src/class/ompi_circular_buffer_fifo.h +++ b/src/class/ompi_circular_buffer_fifo.h @@ -9,6 +9,7 @@ #include "include/sys/cache.h" #include "os/atomic.h" #include "mca/mpool/mpool.h" +#include "util/pow2.h" /** @file @@ -102,22 +103,21 @@ static inline int ompi_cb_fifo_init(int size_of_fifo, int lazy_free_freq, mca_mpool_base_module_t *memory_allocator) { - int tmp_size, errorCode = OMPI_SUCCESS; - size_t len_to_allocate,i; + int errorCode = OMPI_SUCCESS,i; + size_t len_to_allocate; /* verify that size is power of 2, and greatter that 0 - if not, * round up */ if ( 0 >= size_of_fifo) { return OMPI_ERROR; } - tmp_size/=2; - tmp_size*=2; - if( tmp_size != size_of_fifo ) { - size_of_fifo=2*tmp_size; - } /* set fifo size */ - fifo->size = size_of_fifo; + fifo->size = ompi_round_up_to_nearest_pow2(size_of_fifo); + /* debug */ + fprintf(stderr," BBB lazy %d size %d\n",lazy_free_freq,fifo->size); + fflush(stderr); + /* end debug */ /* set lazy free frequence */ if( ( 0 >= lazy_free_freq ) || @@ -130,6 +130,10 @@ static inline int ompi_cb_fifo_init(int size_of_fifo, int lazy_free_freq, * and use the & operator for the wrap-around */ fifo->mask = (size_of_fifo - 1); + /* debug */ + fprintf(stderr," AAA \n"); + fflush(stderr); + /* end debug */ /* allocate fifo array */ len_to_allocate = sizeof(void *) * size_of_fifo; fifo->queue=memory_allocator->mpool_alloc(len_to_allocate,CACHE_LINE_SIZE); @@ -170,6 +174,45 @@ static inline int ompi_cb_fifo_init(int size_of_fifo, int lazy_free_freq, return errorCode; } +/** + * function to cleanup the fifo + * + * @param fifo Pointer to data structure defining this fifo (IN) + * + * @param memory_allocator Pointer to the memory allocator to use + * to allocate memory for this fifo. (IN) + * + */ +static inline int ompi_cb_fifo_free( ompi_cb_fifo_t *fifo, + mca_mpool_base_module_t *memory_allocator) +{ + + int errorCode = OMPI_SUCCESS; + + /* make sure null fifo is not passed in */ + if ( NULL == fifo) { + return OMPI_ERROR; + } + + /* free fifo array */ + if( NULL != fifo->queue){ + memory_allocator->mpool_free(fifo->queue); + } + + /* free head control structure */ + if( NULL != fifo->head) { + memory_allocator->mpool_free(fifo->head); + } + + /* free tail control structure */ + if( NULL != fifo->tail) { + memory_allocator->mpool_free(fifo->tail); + } + + /* return */ + return errorCode; +} + /** * Write pointer to the specified slot diff --git a/src/mca/mpool/mpool.h b/src/mca/mpool/mpool.h index 3a54ef7612..09405c832c 100644 --- a/src/mca/mpool/mpool.h +++ b/src/mca/mpool/mpool.h @@ -79,8 +79,9 @@ typedef struct mca_mpool_base_component_1_0_0_t mca_mpool_base_component_1_0_0_t typedef struct mca_mpool_base_component_1_0_0_t mca_mpool_base_component_t; /** - * mpool module descriptor. Contains functions exported - * by the component. + * mpool module descriptor. Contains the interface functions exported + * by the component. This does not expose memory management + * details. */ struct mca_mpool_base_module_t { mca_mpool_base_component_t *mpool_component; /**< component stuct */ diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 4abda1dab3..4fcf47cad9 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -26,6 +26,7 @@ headers = \ proc_info.h \ os_path.h \ os_create_dirpath.h \ + pow2.h \ session_dir.h \ strncpy.h @@ -47,6 +48,7 @@ libutil_la_SOURCES = \ sys_info.c \ os_path.c \ os_create_dirpath.c \ + pow2.c \ session_dir.c \ strncpy.c diff --git a/src/util/pow2.c b/src/util/pow2.c new file mode 100644 index 0000000000..dc8b7fb0d0 --- /dev/null +++ b/src/util/pow2.c @@ -0,0 +1,42 @@ +/* + * $HEADER$ + */ + + +/** + * This routine takes in an interger, and returns the nearest + * power of 2 integer, same or greater than the input. + * + * @param input_integer input value + * + * @returnvalue power of 2 integer same or greater than the input + * parameter. + */ +int ompi_round_up_to_nearest_pow2(int input_integer) +{ + int pop_count, highest_used_bit, tmp_input_integer, cnt, + return_value; + + /* init counters */ + pop_count=0; + highest_used_bit=-1; + + /* get population count, and highest non-zero bit */ + tmp_input_integer=input_integer; + while ( tmp_input_integer > 0 ){ + pop_count+=(tmp_input_integer&1); + highest_used_bit++; + tmp_input_integer>>=1; + }; + if( 1 < pop_count ) { + /* "round" up */ + highest_used_bit++; + } + + /* generate return value */ + return_value=1<