numba 中的随机种子和多线程
我想知道在 numba 中使用 parallel==True
和 jitted 函数时是否有任何方法可以重复地绘制随机数。我知道对于单线程代码,您可以在 jitted 函数中设置 numpy 或标准随机模块的随机种子,但这似乎不适用于多线程代码。也许有人可以使用某种解决方法?
I wondered if there is any way to reproducibly draw random numbers when using parallel==True
with jitted functions in numba. I know that for singlethreaded code, you can set the random seed for numpy or the standard random module within a jitted function, but that does not seem to work for multithreaded code. Maybe there is some sort of workaround one could use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同时,每个工作线程都需要有自己的种子,因为随机数生成器不能同时高效和线程安全。如果您希望线程数量不对结果产生影响,那么您需要将计算拆分为块,并为每个块设置一个种子(由一个线程计算)。为给定块选择的种子可以是例如块ID。
In parallel, each worker need to have its own seed as a random number generator cannot be both efficient and and thread safe at the same time. If you want the number of threads not to have an impact on the result, then you need to split the computation in chunks and set a seed for each chunk (computed by one thread). The seed chosen for a given chunk can be for example the chunk ID.