具有Bays-Durham的L'Ecuyer的随机数生成器 [英] Random number generator of L'Ecuyer with Bays-Durham

查看:172
本文介绍了具有Bays-Durham的L'Ecuyer的随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Monte Carlo模拟一起寻找PI的小数位.到目前为止还算不错,但是OpenMP进来了,我意识到ran2(可以说是最好的RGN)不是线程安全的!该实现是此处.由于我还没有使用过OpenMP,也没有太多关于多线程的知识,所以我一直坚持使用OpenMP确保该线程的安全性.

I am working with Monte Carlo simulations to find the decimal places of PI. So far so good but OpenMP came in and I realize that ran2, arguably the best RGN, is not threadsafe! The implementation is here. Since I have not worked with OpenMP and neither a lot on multi-threading I am stuck at making this thread safe using OpenMP.

到目前为止,我所知道的是,如果一个函数不修改非本地内存并且不调用任何执行此操作的函数,则该函数已经是线程安全的.在这种情况下,有3个静态变量,因此如果被不同的线程使用,则会对其进行修改.

So far what I know is that a function is already thread-safe if it doesn't modify non-local memory and it doesn't call any function that does. In this case, there are 3 variables which are static and thus will be modified if gets used by different threads.

一种可能的解决方案是将线程ran2的调用包含在关键部分中,从而以线程安全的方式进行调用,但这没有任何意义,因为我没有得到加速.

One possible solution is to call this in a thread safe way by enclosing the calling of ran2 in a critical section but that makes no sense as I get no speedup.

有人可以给我有关如何进行此操作的指示吗,或者有人可以提供任何有用的参考吗?

Can somebody give me pointers on how to proceed with this or if somebody has any reference that will be great too!

推荐答案

使过程线程安全的方法通常是将以前的静态数据与线程本地数据相关联.例如,查看rand_r()的手册页,它是rand()的线程安全版本.

What is generally done to render a procedure thread safe is to associate previously static data to a thread local data. Look for instance at the man page of rand_r() that is a thread safe version of rand().

所以在您的L'Ecuyer版本中:

So in your version of L'Ecuyer:

  1. 定义一个保存静态数据的结构(例如状态)

  1. define a struct (say state) that holds the static data

重新定义过程ran2()以具有一个附加参数,该参数是指向此结构状态的指针,并相应地修改代码.令ran2_r()为新名称.

redefine procedure ran2() to have an additional parameter that is a pointer to this struct state and modify the code accordingly. Let ran2_r() be the new name.

在每个线程中定义一个本地结构状态以保持状态

define in every thread a local struct state to hold the state

可能需要播种状态.您可以使用get_thread_num()提供每个线程的种子,以在进入线程时正确初始化状态.

probably state needs to be seeded. You can use get_thread_num() to provide a per thread seed to initialize properly the state when entering the thread.

现在,您只需要使用指向此状态的指针来调用新的ran2_r().该过程将对其进行修改,但是修改将存储在线程本地状态var中.

Now you just need to call your new ran2_r() with a pointer to this state. It will be modified by the procedure, but modifications will be stored in the thread local state var.

这篇关于具有Bays-Durham的L'Ecuyer的随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆