Aliquot Sequence Research  2.0
Compute properties of the sum-of-proper-divisors function.
PackedArray.h
1 
2 #ifndef POMYANG_KPARENT_INC_PACKEDARRAY_H_
3 #define POMYANG_KPARENT_INC_PACKEDARRAY_H_
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
9 #include <omp.h>
10 #include <stdint.h>
11 
12 /*
13 
14 PackedArray principle:
15  . compact storage of <= 32 bits items
16  . items are tightly packed into a buffer of uint32_t integers
17 
18 PackedArray requirements:
19  . you must know in advance how many bits are needed to hold a single item
20  . you must know in advance how many items you want to store
21  . when packing, behavior is undefined if items have more than bitsPerItem bits
22 
23 PackedArray general in memory representation:
24  |-------------------------------------------------- - - -
25  | b0 | b1 | b2 |
26  |-------------------------------------------------- - - -
27  | i0 | i1 | i2 | i3 | i4 | i5 | i6 | i7 | i8 | i9 |
28  |-------------------------------------------------- - - -
29 
30  . items are tightly packed together
31  . several items end up inside the same buffer cell, e.g. i0, i1, i2
32  . some items span two buffer cells, e.g. i3, i6
33 
34 */
35 
49 typedef struct {
50  uint32_t num_locks;
51  uint32_t lock_interval;
52  omp_lock_t* locks;
53 } lock_info_t;
54 
74 typedef struct {
75  uint32_t bitsPerItem;
76  uint64_t count;
78  uint32_t padding[2];
79  uint32_t buffer[];
80 } PackedArray;
81 
82 // creation / destruction
83 PackedArray* PackedArray_create(uint32_t bitsPerItem, uint64_t count, uint32_t num_locks);
84 void PackedArray_destroy(PackedArray* a);
85 uint64_t PackedArray_estimate_heap(uint32_t bitsPerItem, uint64_t count, uint32_t num_locks);
86 
87 // packing / unpacking
88 // offset is expressed in number of elements
89 void PackedArray_pack(PackedArray* a, const uint64_t offset, const uint32_t* in, uint64_t count);
90 void PackedArray_unpack(const PackedArray* a, const uint64_t offset, uint32_t* out, uint64_t count);
91 
92 // single item access
93 void PackedArray_set(PackedArray* a, const uint64_t offset, const uint32_t in);
94 uint32_t PackedArray_get(const PackedArray* a, const uint64_t offset);
95 
96 // helpers
97 uint32_t PackedArray_bufferSize(const PackedArray* a);
98 uint32_t PackedArray_computeBitsPerItem(const uint32_t* in, uint64_t count);
99 void PackedArray_unlock_offset(PackedArray* a, const uint64_t offset);
100 void PackedArray_lock_offset(PackedArray* a, const uint64_t offset);
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif // POMYANG_KPARENT_INC_PACKEDARRAY_H_
lock_info_t::locks
omp_lock_t * locks
Definition: PackedArray.h:52
PackedArray::lock_info
lock_info_t lock_info
Definition: PackedArray.h:77
PackedArray
PackedArray handle.
Definition: PackedArray.h:74
lock_info_t
Protects buffer underlying PackedArray with an array of mutexs.
Definition: PackedArray.h:49
lock_info_t::lock_interval
uint32_t lock_interval
Definition: PackedArray.h:51
PackedArray::count
uint64_t count
Definition: PackedArray.h:76
lock_info_t::num_locks
uint32_t num_locks
Definition: PackedArray.h:50
PackedArray::bitsPerItem
uint32_t bitsPerItem
Definition: PackedArray.h:75