[petsc-dev] Make PetscHash publicly accessible

Jed Brown jed at jedbrown.org
Fri Apr 22 09:52:38 CDT 2016


Matthew Knepley <knepley at gmail.com> writes:
> You are wrong in this case. The HashIJ is not trivial, and cannot be done
> easily inline. What is the problem here?

$ git grep -l 'PetscHashIJ[^K]'
src/sys/examples/tests/ex26.c
src/sys/utils/hash.h

$ git grep -l 'PetscHashJK'
src/dm/impls/plex/plexfem.c
src/mat/impls/preallocator/matpreallocator.c
src/sys/utils/hash.h

But matpreallocator.c doesn't actually use the linked list aspects of
PetscHashJK, so it's memory overhead (two unused pointers plus padding
per entry) for nothing.

$ git grep -l 'PetscHashIJKL'
src/dm/impls/plex/plexinterpolate.c
src/sys/utils/hash.h

This is actually used, but I think the implementation (below) accurately
fits my description of wrapping khash primitives with imaginary error
checking.  Lawrence, do you just want a general-purpose hash table or do
you specifically want Matt's hash/linked-list structure?


#undef  __FUNCT__
#define __FUNCT__ "PetscHashIJKLCreate"
PETSC_STATIC_INLINE PetscErrorCode PetscHashIJKLCreate(PetscHashIJKL *h)
{
  PetscErrorCode ierr;

  PetscFunctionBegin;
  PetscValidPointer(h, 1);
  ierr = PetscNew((h));CHKERRQ(ierr);
  (*h)->ht = kh_init(HASHIJKL);
  PetscFunctionReturn(0);
}

#undef  __FUNCT__
#define __FUNCT__ "PetscHashIJKLResize"
PETSC_STATIC_INLINE PetscErrorCode PetscHashIJKLResize(PetscHashIJKL h, PetscInt n)
{
  PetscFunctionBegin;
  (kh_resize(HASHIJKL, (h)->ht, (n)));
  PetscFunctionReturn(0);
}

#undef  __FUNCT__
#define __FUNCT__ "PetscHashIJKLKeySize"
PETSC_STATIC_INLINE PetscErrorCode PetscHashIJKLKeySize(PetscHashIJKL h, PetscInt *n)
{
  PetscFunctionBegin;
  ((*n) = kh_size((h)->ht));
  PetscFunctionReturn(0);
}

#undef  __FUNCT__
#define __FUNCT__ "PetscHashIJKLPut"
/*
  PetscHashIJKLPut - Insert key in the hash table

  Input Parameters:
+ h - The hash table
- key - The key to insert

  Output Parameter:
+ missing - 0 if the key is present in the hash table, 1 if the bucket is empty (never used), 2 if the element in the bucket has been deleted
- iter - Iterator into table

  Level: developer

.seealso: PetscHashIJKLCreate(), PetscHashIJKLSet()
*/
PETSC_STATIC_INLINE PetscErrorCode PetscHashIJKLPut(PetscHashIJKL h, PetscHashIJKLKey key, PetscHashIJKLIter *missing, PetscHashIJKLIter *iter)
{
  PetscFunctionBeginHot;
  *iter = kh_put(HASHIJKL, (h)->ht, (key), missing);
  PetscFunctionReturn(0);
}

#undef  __FUNCT__
#define __FUNCT__ "PetscHashIJKLSet"
/*
  PetscHashIJKLSet - Set the value for an iterator in the hash table

  Input Parameters:
+ h - The hash table
. iter - An iterator into the table
- value - The value to set

  Level: developer

.seealso: PetscHashIJKLCreate(), PetscHashIJKLPut(), PetscHashIJKLGet()
*/
PETSC_STATIC_INLINE PetscErrorCode PetscHashIJKLSet(PetscHashIJKL h, PetscHashIJKLIter iter, PetscInt value)
{
  PetscFunctionBeginHot;
  kh_val((h)->ht, iter).n = value;
  PetscFunctionReturn(0);
}

#undef  __FUNCT__
#define __FUNCT__ "PetscHashIJKLGet"
/*
  PetscHashIJKLGet - Get the value for an iterator in the hash table

  Input Parameters:
+ h - The hash table
. iter - An iterator into the table

  Output Parameters:
. value - The value to get

  Level: developer

.seealso: PetscHashIJKLCreate(), PetscHashIJKLPut(), PetscHashIJKLSet()
*/
PETSC_STATIC_INLINE PetscErrorCode PetscHashIJKLGet(PetscHashIJKL h, PetscHashIJKLIter iter, PetscInt *value)
{
  PetscFunctionBeginHot;
  *value = kh_val((h)->ht, iter).n;
  PetscFunctionReturn(0);
}

#undef  __FUNCT__
#define __FUNCT__ "PetscHashIJKLClear"
PETSC_STATIC_INLINE PetscErrorCode PetscHashIJKLClear(PetscHashIJKL h)
{
  PetscFunctionBegin;
  kh_clear(HASHIJKL, (h)->ht);
  PetscFunctionReturn(0);
}

#undef  __FUNCT__
#define __FUNCT__ "PetscHashIJKLDestroy"
PETSC_STATIC_INLINE PetscErrorCode PetscHashIJKLDestroy(PetscHashIJKL *h)
{
  PetscFunctionBegin;
  PetscValidPointer(h, 1);
  if ((*h)) {
    PetscErrorCode ierr;

    if ((*h)->ht) {
      kh_destroy(HASHIJKL, (*h)->ht);
      (*h)->ht = NULL;
    }
    ierr = PetscFree((*h));CHKERRQ(ierr);
  }
  PetscFunctionReturn(0);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20160422/7a82a1db/attachment.sig>


More information about the petsc-dev mailing list