[petsc-dev] use of hash table vs array in various places in PETSc

Barry Smith bsmith at mcs.anl.gov
Sun Sep 18 20:55:23 CDT 2011


   Currently in several places in PETSc we have code like

#if defined(PETSC_USE_CTABLE)
  ierr = PetscTableFind(aij->colmap,i,....,&col);CHKERRQ;
#else
  col = aij->colmap[i];
#endif

   so when compiled without ctable it gives no hashtable overhead, but with ctable it will be memory scalable when most of the entries are zero.

   The problem is that this assumes that it can be a compile-time decision whether to use the hash table or not. This is not always true  so I propose the following change: we make the decision hash table or array at run time (in PetscTableCreate()) and store the hash table/array in the same data structure and have PetscTableFind() and PetscTableAdd() be in-line functions sort of like

    PetscTableFind(hashtable, index, *col) 
    {
       if (hashtable->isarray) {*col = hashtable->array[index]; PetscFunctionReturn(0);}
       /* else get value from hashtable. */

  The only drawback I see is each access would requires the check if (hashtable->isarray)  which will slow down the array version "slightly" but the alternative of have two separate versions of many methods one that uses arrays and one that uses hash tables seems absurd. 

   Any objections/suggestions of alternatives?

   The end result will actually be simpler code in all the places that currently have #if (ctable) and a slightly more involved set of PetscTableXXX routines. All in all an improvement I would say.


    Barry





More information about the petsc-dev mailing list