Thread Safety
SSoelvsten opened this issue · 1 comments
In many contexts, BDD packages should be threadsafe such that they can be used as part of parallel algorithms. It is not possible for such a program to know early how many threads may exist before it starts its calculations. Hence, Adiar has to adapt on-the-fly to the number of concurrent calls.
Luckily for us, the write-once/read-only design of Adiar's files makes thread-safety much easier (since we have no unique node table to lock). Yet, instead we have one global pool of M memory in TPIE that we are allowed to use. Hence, we need some kind of locking/scheduler to make sure every thread can claim a share of M "early". Otherwise, another thread can by accident overtake it and both ask TPIE for the same "free" memory.
- Add
ADIAR_THREADSAFE
to CMake (default: OFF). If on, then the logic for thread-safety below is included. - Add
memory_share
class that uses RAII to claim a share of memory.- Claim up-to half of the unclaimed memory.
- Claim at least the minimum amount of memory (128 MiB, or maybe a more sophisticated number?). If not currently available, then the thread has to wait for someone else to finish their calculations.
- If the internal-memory cases use less than is claimed, then relinquish the unused amount early. Alternatively, first claim memory after all internal-memory calculations have been made.
While talking to Riko Jacob, it can be a bad idea to just naively split the amount of memory (as described above). Iterating on his point, we should consider to:
- Have an upper bound on the number of concurrent jobs (set by the user / CPU specification).
- Schedule/Throttle the concurrent jobs (in a FIFO fashion?)