koma icon indicating copy to clipboard operation
koma copied to clipboard

RNG on native needs to consider thread safety

Open kyonifer opened this issue 6 years ago • 2 comments

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.

kyonifer avatar Oct 30 '18 04:10 kyonifer

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.

peastman avatar Oct 31 '18 00:10 peastman

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.

kyonifer avatar Oct 31 '18 04:10 kyonifer