diff --git a/src/sundials/stl/sunstl_vector.h b/src/sundials/stl/sunstl_vector.h index ae068976a2..26a5695205 100644 --- a/src/sundials/stl/sunstl_vector.h +++ b/src/sundials/stl/sunstl_vector.h @@ -38,8 +38,8 @@ typedef struct SUNStlVectorTtype_s* SUNStlVectorTtype; struct SUNStlVectorTtype_s { - size_t size; - size_t capacity; + int64_t size; + int64_t capacity; TTYPE* values; void (*destroyValue)(TTYPE*); }; @@ -52,7 +52,7 @@ struct SUNStlVectorTtype_s #define GROWTH_FACTOR 1.5 static inline SUNStlVectorTtype MAKE_NAME(SUNStlVectorTtype, - New)(size_t init_capacity, + New)(int64_t init_capacity, void (*destroyValue)(TTYPE*)) { SUNStlVectorTtype self = @@ -71,7 +71,7 @@ static inline sunbooleantype MAKE_NAME(SUNStlVectorTtype, } static inline void MAKE_NAME(SUNStlVectorTtype, Resize)(SUNStlVectorTtype self, - size_t new_capacity) + int64_t new_capacity) { if (new_capacity <= self->capacity) return; TTYPE* new_values = (TTYPE*)realloc(self->values, sizeof(TTYPE) * new_capacity); @@ -83,7 +83,8 @@ static inline void MAKE_NAME(SUNStlVectorTtype, Grow)(SUNStlVectorTtype self) { if (self->size == self->capacity) { - size_t new_capacity = (size_t)(ceil(((double)self->capacity) * GROWTH_FACTOR)); + int64_t new_capacity = + (int64_t)(ceil(((double)self->capacity) * GROWTH_FACTOR)); MAKE_NAME(SUNStlVectorTtype, Resize)(self, new_capacity); } } @@ -99,7 +100,7 @@ static inline void MAKE_NAME(SUNStlVectorTtype, } static inline TTYPE* MAKE_NAME(SUNStlVectorTtype, At)(SUNStlVectorTtype self, - size_t index) + int64_t index) { if (index >= self->size) { @@ -110,7 +111,7 @@ static inline TTYPE* MAKE_NAME(SUNStlVectorTtype, At)(SUNStlVectorTtype self, } static inline void MAKE_NAME(SUNStlVectorTtype, Set)(SUNStlVectorTtype self, - size_t index, TTYPE element) + int64_t index, TTYPE element) { if (index >= self->size) { @@ -129,12 +130,12 @@ static inline void MAKE_NAME(SUNStlVectorTtype, PopBack)(SUNStlVectorTtype self) } static inline void MAKE_NAME(SUNStlVectorTtype, Erase)(SUNStlVectorTtype self, - size_t index) + int64_t index) { static TTYPE nullish; if (self->size == 0) return; MAKE_NAME(SUNStlVectorTtype, Set)(self, index, nullish); - for (size_t i = index; i < self->size - 1; i++) + for (int64_t i = index; i < self->size - 1; i++) { self->values[i] = self->values[i + 1]; self->values[i + 1] = nullish; @@ -142,12 +143,13 @@ static inline void MAKE_NAME(SUNStlVectorTtype, Erase)(SUNStlVectorTtype self, self->size -= 1; } -static inline size_t MAKE_NAME(SUNStlVectorTtype, Size)(SUNStlVectorTtype self) +static inline int64_t MAKE_NAME(SUNStlVectorTtype, Size)(SUNStlVectorTtype self) { return self->size; } -static inline size_t MAKE_NAME(SUNStlVectorTtype, Capacity)(SUNStlVectorTtype self) +static inline int64_t MAKE_NAME(SUNStlVectorTtype, + Capacity)(SUNStlVectorTtype self) { return self->capacity; } @@ -161,7 +163,7 @@ static inline void MAKE_NAME(SUNStlVectorTtype, SUNStlVectorTtype self = *self_ptr; - for (size_t i = 0; i < MAKE_NAME(SUNStlVectorTtype, Size)(self); i++) + for (int64_t i = 0; i < MAKE_NAME(SUNStlVectorTtype, Size)(self); i++) { self->destroyValue(&(self->values[i])); self->values[i] = nullish; diff --git a/src/sundials/sundials_hashmap.c b/src/sundials/sundials_hashmap.c index 54d10474c3..94ebc58618 100644 --- a/src/sundials/sundials_hashmap.c +++ b/src/sundials/sundials_hashmap.c @@ -28,6 +28,9 @@ #include "sundials_hashmap_impl.h" #include "sundials_macros.h" +#define ERROR_OCCURRED -99 +#define KEY_NOT_FOUND -1 + static const uint64_t HASH_PRIME = 14695981039346656037U; static const uint64_t HASH_OFFSET_BASIS = 1099511628211U; @@ -46,11 +49,11 @@ static uint64_t fnv1a_hash(const char* str) return hash; } -static inline size_t sunHashMapIdxFromKey(SUNHashMap map, const char* key) +static inline int64_t sunHashMapIdxFromKey(SUNHashMap map, const char* key) { /* We want the index to be in (0, SUNHashMap_Capacity(map)) */ - size_t end = SUNHashMap_Capacity(map) - 1; - size_t idx = end == 0 ? end : (size_t)(fnv1a_hash(key) % end); + int64_t end = SUNHashMap_Capacity(map) - 1; + int64_t idx = end == 0 ? end : (int64_t)(fnv1a_hash(key) % end); return idx; } @@ -66,7 +69,7 @@ static inline size_t sunHashMapIdxFromKey(SUNHashMap map, const char* key) **Returns:** * A SUNErrCode indicating success or a failure */ -SUNErrCode SUNHashMap_New(size_t capacity, +SUNErrCode SUNHashMap_New(int64_t capacity, void (*destroyKeyValue)(SUNHashMapKeyValue* kv_ptr), SUNHashMap* map) { @@ -89,7 +92,7 @@ SUNErrCode SUNHashMap_New(size_t capacity, } /* Initialize all buckets to NULL */ - for (size_t i = 0; i < capacity; i++) + for (int64_t i = 0; i < capacity; i++) { SUNStlVector_SUNHashMapKeyValue_PushBack(buckets, NULL); } @@ -108,7 +111,7 @@ SUNErrCode SUNHashMap_New(size_t capacity, **Returns:** * The capacity of the hashmap */ -size_t SUNHashMap_Capacity(SUNHashMap map) +int64_t SUNHashMap_Capacity(SUNHashMap map) { return SUNStlVector_SUNHashMapKeyValue_Capacity(map->buckets); } @@ -143,61 +146,61 @@ SUNErrCode SUNHashMap_Destroy(SUNHashMap* map) * ``map`` -- the ``SUNHashMap`` object to operate on * ``start`` -- the start of the iteration range * ``yieldfn`` -- the callback function to call every iteration - this should return SIZE_MAX to continue the iteration, or [0, SIZE_MAX-1] + this should return ERROR_OCCURRED to continue the iteration, or [0, KEY_NOT_FOUND] to stop; the first argument is the current index, the second argument is the current key-value pair, and the final argument is the same pointer ``ctx`` as the final argument to SUNHashMapIterate. * ``ctx`` -- a pointer to pass on to ``yieldfn`` **Returns:** - * ``SIZE_MAX`` -- an error occurred + * ``ERROR_OCCURRED`` -- an error occurred * ``capacity`` -- iterated the whole map * ``>=0`` -- the index at which the iteration stopped */ -size_t SUNHashMap_Iterate(SUNHashMap map, size_t start, - size_t (*yieldfn)(size_t, SUNHashMapKeyValue, - const void*), - const void* ctx) +int64_t SUNHashMap_Iterate(SUNHashMap map, int64_t start, + int64_t (*yieldfn)(int64_t, SUNHashMapKeyValue, + const void*), + const void* ctx) { - if (map == NULL || yieldfn == NULL) { return SIZE_MAX; } + if (map == NULL || yieldfn == NULL) { return ERROR_OCCURRED; } - for (size_t i = start; i < SUNStlVector_SUNHashMapKeyValue_Size(map->buckets); - i++) + for (int64_t i = start; + i < SUNStlVector_SUNHashMapKeyValue_Size(map->buckets); i++) { - size_t retval = + int64_t retval = yieldfn(i, *SUNStlVector_SUNHashMapKeyValue_At(map->buckets, i), ctx); - if (retval == SIZE_MAX) { continue; /* keep looking */ } + if (retval == ERROR_OCCURRED) { continue; /* keep looking */ } else { return (retval); /* yieldfn indicates the loop should break */ } } return SUNHashMap_Capacity(map); } -static size_t sunHashMapLinearProbeInsert(size_t idx, SUNHashMapKeyValue kv, - SUNDIALS_MAYBE_UNUSED const void* ctx) +static int64_t sunHashMapLinearProbeInsert(int64_t idx, SUNHashMapKeyValue kv, + SUNDIALS_MAYBE_UNUSED const void* ctx) { /* find the next open spot */ if (kv == NULL) { return (idx); /* open spot found at idx */ } - return SIZE_MAX; /* keep looking */ + return ERROR_OCCURRED; /* keep looking */ } static void sunHashMapResize(SUNHashMap map) { - size_t old_capacity = SUNHashMap_Capacity(map); - size_t new_capacity = old_capacity * 2; + int64_t old_capacity = SUNHashMap_Capacity(map); + int64_t new_capacity = old_capacity * 2; SUNStlVector_SUNHashMapKeyValue old_buckets = map->buckets; map->buckets = SUNStlVector_SUNHashMapKeyValue_New(new_capacity, map->destroyKeyValue); /* Set all buckets to NULL */ - for (size_t i = 0; i < new_capacity; i++) + for (int64_t i = 0; i < new_capacity; i++) { SUNStlVector_SUNHashMapKeyValue_PushBack(map->buckets, NULL); } /* Rehash and reinsert */ - for (size_t i = 0; i < old_capacity; i++) + for (int64_t i = 0; i < old_capacity; i++) { SUNHashMapKeyValue kvp = *SUNStlVector_SUNHashMapKeyValue_At(old_buckets, i); if (kvp) { SUNHashMap_Insert(map, kvp->key, kvp->value); } @@ -217,16 +220,16 @@ static void sunHashMapResize(SUNHashMap map) **Returns:** * ``0`` -- success - * ``SIZE_MAX`` -- an error occurred - * ``SIZE_MAX-1`` -- duplicate key + * ``ERROR_OCCURRED`` -- an error occurred + * ``KEY_NOT_FOUND`` -- duplicate key */ -size_t SUNHashMap_Insert(SUNHashMap map, const char* key, void* value) +int64_t SUNHashMap_Insert(SUNHashMap map, const char* key, void* value) { - size_t idx; - size_t retval; + int64_t idx; + int64_t retval; SUNHashMapKeyValue kvp; - if (map == NULL || key == NULL || value == NULL) { return SIZE_MAX; } + if (map == NULL || key == NULL || value == NULL) { return ERROR_OCCURRED; } idx = sunHashMapIdxFromKey(map, key); @@ -235,11 +238,11 @@ size_t SUNHashMap_Insert(SUNHashMap map, const char* key, void* value) if (kvp != NULL) { /* Determine if key is actually a duplicate (not allowed) */ - if (!strcmp(key, kvp->key)) { return SIZE_MAX - 1; } + if (!strcmp(key, kvp->key)) { return KEY_NOT_FOUND; } /* OK, it was a real collision, so find the next open spot */ retval = SUNHashMap_Iterate(map, idx + 1, sunHashMapLinearProbeInsert, NULL); - if (retval == SIZE_MAX) + if (retval == ERROR_OCCURRED) { /* an error occurred */ return retval; @@ -258,7 +261,7 @@ size_t SUNHashMap_Insert(SUNHashMap map, const char* key, void* value) kvp = (SUNHashMapKeyValue)malloc(sizeof(*kvp)); /* Copy the original_key so that the hashmap owns it */ - size_t len = strlen(key) + 1; + int64_t len = strlen(key) + 1; char* key_copy = malloc(sizeof(*key) * len); strcpy(key_copy, key); @@ -271,22 +274,22 @@ size_t SUNHashMap_Insert(SUNHashMap map, const char* key, void* value) return (0); } -static size_t sunHashMapLinearProbeGet(size_t idx, SUNHashMapKeyValue kv, - const void* key) +static int64_t sunHashMapLinearProbeGet(int64_t idx, SUNHashMapKeyValue kv, + const void* key) { /* target key cannot be NULL */ - if (key == NULL) { return SIZE_MAX - 1; } + if (key == NULL) { return KEY_NOT_FOUND; } /* find the matching entry */ if (kv == NULL) { - return SIZE_MAX; /* keep looking since this bucket is empty */ + return ERROR_OCCURRED; /* keep looking since this bucket is empty */ } if (!strcmp(kv->key, (const char*)key)) { return (idx); /* found it at idx */ } - return SIZE_MAX; /* keep looking */ + return ERROR_OCCURRED; /* keep looking */ } /* @@ -299,13 +302,13 @@ static size_t sunHashMapLinearProbeGet(size_t idx, SUNHashMapKeyValue kv, **Returns:** * ``0`` -- success - * ``SIZE_MAX`` -- an error occurred - * ``SIZE_MAX-1`` -- key not found + * ``ERROR_OCCURRED`` -- an error occurred + * ``KEY_NOT_FOUND`` -- key not found */ -size_t SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value) +int64_t SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value) { - size_t idx; - size_t retval; + int64_t idx; + int64_t retval; sunbooleantype collision; if (map == NULL || key == NULL || value == NULL) { return (-1); } @@ -322,7 +325,7 @@ size_t SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value) if (collision) { retval = SUNHashMap_Iterate(map, idx + 1, sunHashMapLinearProbeGet, key); - if (retval == SIZE_MAX) + if (retval == ERROR_OCCURRED) { /* the key was either not found anywhere or an error occurred */ return retval; @@ -334,7 +337,7 @@ size_t SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value) SUNHashMapKeyValue* kvp_ptr = SUNStlVector_SUNHashMapKeyValue_At(map->buckets, idx); if (kvp_ptr) { *value = (*kvp_ptr)->value; } - else { return SIZE_MAX - 1; } + else { return KEY_NOT_FOUND; } return (0); } @@ -349,16 +352,16 @@ size_t SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value) **Returns:** * ``0`` -- success - * ``SIZE_MAX`` -- an error occurred - * ``SIZE_MAX-1`` -- key not found + * ``ERROR_OCCURRED`` -- an error occurred + * ``KEY_NOT_FOUND`` -- key not found */ -size_t SUNHashMap_Remove(SUNHashMap map, const char* key, void** value) +int64_t SUNHashMap_Remove(SUNHashMap map, const char* key, void** value) { - size_t idx; - size_t retval; + int64_t idx; + int64_t retval; sunbooleantype collision; - if (map == NULL || key == NULL) { SIZE_MAX; } + if (map == NULL || key == NULL) { return ERROR_OCCURRED; } idx = sunHashMapIdxFromKey(map, key); @@ -374,7 +377,7 @@ size_t SUNHashMap_Remove(SUNHashMap map, const char* key, void** value) /* Keys did not match, so we have a collision and need to probe */ retval = SUNHashMap_Iterate(map, idx + 1, sunHashMapLinearProbeGet, (const void*)key); - if (retval == SIZE_MAX) + if (retval == ERROR_OCCURRED) { /* an error occurred or the key was not found anywhere */ return retval; @@ -417,7 +420,7 @@ SUNErrCode SUNHashMap_Sort(SUNHashMap map, SUNHashMapKeyValue** sorted, if (!(*sorted)) { return SUN_ERR_MALLOC_FAIL; } /* Copy the buckets into a new array */ - for (size_t i = 0; i < SUNHashMap_Capacity(map); i++) + for (int64_t i = 0; i < SUNHashMap_Capacity(map); i++) { (*sorted)[i] = *SUNStlVector_SUNHashMapKeyValue_At(map->buckets, i); } @@ -438,7 +441,7 @@ SUNErrCode SUNHashMap_Sort(SUNHashMap map, SUNHashMapKeyValue** sorted, **Returns:** * A SUNErrCode indicating success or a failure */ -SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values, size_t value_size) +SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values, int64_t value_size) { int count = 0; @@ -448,7 +451,7 @@ SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values, size_t value_size) if (!values) { return SUN_ERR_MALLOC_FAIL; } /* Copy the values into a new array */ - for (size_t i = 0; i < SUNHashMap_Capacity(map); i++) + for (int64_t i = 0; i < SUNHashMap_Capacity(map); i++) { SUNHashMapKeyValue kvp = *SUNStlVector_SUNHashMapKeyValue_At(map->buckets, i); if (kvp) { (*values)[count++] = kvp->value; } @@ -463,7 +466,7 @@ SUNErrCode SUNHashMap_PrintKeys(SUNHashMap map, FILE* file) /* Print keys into a new array */ fprintf(file, "["); - for (size_t i = 0; i < SUNHashMap_Capacity(map); i++) + for (int64_t i = 0; i < SUNHashMap_Capacity(map); i++) { SUNHashMapKeyValue kvp = *SUNStlVector_SUNHashMapKeyValue_At(map->buckets, i); if (kvp) { fprintf(file, "%s, ", kvp->key); } diff --git a/src/sundials/sundials_hashmap_impl.h b/src/sundials/sundials_hashmap_impl.h index d70c1b55c6..846e069fea 100644 --- a/src/sundials/sundials_hashmap_impl.h +++ b/src/sundials/sundials_hashmap_impl.h @@ -42,34 +42,34 @@ typedef struct SUNHashMap_* SUNHashMap; struct SUNHashMap_ { - size_t capacity; /* max number of entries */ + int64_t capacity; /* max number of entries */ void (*destroyKeyValue)(SUNHashMapKeyValue*); SUNStlVector_SUNHashMapKeyValue buckets; }; -SUNErrCode SUNHashMap_New(size_t capacity, +SUNErrCode SUNHashMap_New(int64_t capacity, void (*destroyValue)(SUNHashMapKeyValue* value_ptr), SUNHashMap* map); -size_t SUNHashMap_Capacity(SUNHashMap map); +int64_t SUNHashMap_Capacity(SUNHashMap map); SUNErrCode SUNHashMap_Destroy(SUNHashMap* map); -size_t SUNHashMap_Iterate(SUNHashMap map, size_t start, - size_t (*yieldfn)(size_t, SUNHashMapKeyValue, - const void*), - const void* ctx); +int64_t SUNHashMap_Iterate(SUNHashMap map, int64_t start, + int64_t (*yieldfn)(int64_t, SUNHashMapKeyValue, + const void*), + const void* ctx); -size_t SUNHashMap_Insert(SUNHashMap map, const char* key, void* value); +int64_t SUNHashMap_Insert(SUNHashMap map, const char* key, void* value); -size_t SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value); +int64_t SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value); -size_t SUNHashMap_Remove(SUNHashMap map, const char* key, void** value); +int64_t SUNHashMap_Remove(SUNHashMap map, const char* key, void** value); SUNErrCode SUNHashMap_Sort(SUNHashMap map, SUNHashMapKeyValue** sorted, int (*compar)(const void*, const void*)); -SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values, size_t value_size); +SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values, int64_t value_size); SUNErrCode SUNHashMap_PrintKeys(SUNHashMap map, FILE* file); diff --git a/src/sundials/sundials_profiler.c b/src/sundials/sundials_profiler.c index 55b0ab13a4..f24938ca86 100644 --- a/src/sundials/sundials_profiler.c +++ b/src/sundials/sundials_profiler.c @@ -419,7 +419,7 @@ SUNErrCode SUNProfiler_Print(SUNProfiler p, FILE* fp) #endif /* Print all the other timers out */ - for (size_t i = 0; i < SUNHashMap_Capacity(p->map); i++) + for (int64_t i = 0; i < SUNHashMap_Capacity(p->map); i++) { if (sorted[i]) { sunPrintTimer(sorted[i], fp, (void*)p); } } @@ -467,13 +467,13 @@ SUNErrCode sunCollectTimers(SUNProfiler p) sunTimerStruct** values = NULL; - size_t map_size = SUNHashMap_Capacity(p->map); + int64_t map_size = SUNHashMap_Capacity(p->map); /* Extract the elapsed times from the hash map */ SUNHashMap_Values(p->map, (void***)&values, sizeof(sunTimerStruct)); sunTimerStruct* reduced = (sunTimerStruct*)malloc(map_size * sizeof(sunTimerStruct)); - for (size_t i = 0; i < map_size; ++i) { reduced[i] = *values[i]; } + for (int64_t i = 0; i < map_size; ++i) { reduced[i] = *values[i]; } /* Register MPI datatype for sunTimerStruct */ MPI_Datatype tmp_type, MPI_sunTimerStruct; @@ -511,7 +511,7 @@ SUNErrCode sunCollectTimers(SUNProfiler p) MPI_Op_free(&MPI_sunTimerStruct_MAXANDSUM); /* Update the values that are in this rank's hash map. */ - for (size_t i = 0; i < map_size; ++i) + for (int64_t i = 0; i < map_size; ++i) { values[i]->average = reduced[i].average / (double)nranks; values[i]->maximum = reduced[i].maximum; diff --git a/src/sundials/sundials_utils.h b/src/sundials/sundials_utils.h index 0decaaac0f..f3d704e2cc 100644 --- a/src/sundials/sundials_utils.h +++ b/src/sundials/sundials_utils.h @@ -24,15 +24,6 @@ #include #include -static inline char* sunUnsignedToString(uint64_t val) -{ - char* str = NULL; - size_t length = snprintf(NULL, 0, "%llu", (unsigned long long)val); - str = (char*)malloc(sizeof(*str) * (length + 1)); - snprintf(str, length + 1, "%llu", (unsigned long long)val); - return str; -} - static inline char* sunSignedToString(int64_t val) { char* str = NULL;