koma
koma copied to clipboard
RNG on native needs to consider thread safety
Right now on K/native we currently just allow multiple threads to access the same mutable state, which will probably error out.
We need some sort of thread-safe solution using K/native's threading paradigm. For example, using native's worker threads that return frozen memory to be copied into mutable matrices on each thread.
This might just be a matter of documentation: make it clear that KomaRandom is not thread safe on Native and you shouldn't share a single generator between threads. That seems to be the main principle behind threading on native: never have any mutable object accessible by two threads at the same time. The library and compiler work fairly hard to prevent you from doing it accidentally.
I think we absolutely should support threadsafe rng on native. By that I mean that calls to the top-level functions randn
and rand
should be possible from multiple threads (k/native workers) concurrently. Right now that won't fly because they are using shared mutable state underneath. But I don't believe there's any reason we couldn't have thread-local instances that each worker would call into when they accessed the top-level function.