gpt4 book ai didi

使用 Random123 通过 OpenCL 生成随机数

转载 作者:行者123 更新时间:2023-12-02 08:10:31 25 4
gpt4 key购买 nike

我一直在查看这个 lib Random123 和相关引用:

One mysterious man came to my booth and asked what I knew about generating random numbers with OpenCL. I told him about implementations of the Mersenne Twister, but he wasn't impressed. He told me about a new technical paper that explains how to generate random numbers on GPUs by combining integer counters and block ciphers. In reverential tones, he said that counter-based random number generators (CBRNGs) produce numbers with greater statistical randomness than the MT and with much greater speed.

我能够使用此内核运行演示:

__kernel void counthits(unsigned n, __global uint2 *hitsp) {
unsigned tid = get_global_id(0);
unsigned hits = 0, tries = 0;
threefry4x32_key_t k = {{tid, 0xdecafbad, 0xfacebead, 0x12345678}};
threefry4x32_ctr_t c = {{0, 0xf00dcafe, 0xdeadbeef, 0xbeeff00d}};
while (tries < n) {
union {
threefry4x32_ctr_t c;
int4 i;
} u;
c.v[0]++;
u.c = threefry4x32(c, k);
long x1 = u.i.x, y1 = u.i.y;
long x2 = u.i.z, y2 = u.i.w;
if ((x1*x1 + y1*y1) < (1L<<62)) {
hits++;
}
tries++;
if ((x2*x2 + y2*y2) < (1L<<62)) {
hits++;
}
tries++;
}
hitsp[tid].x = hits;
hitsp[tid].y = tries;
}

我现在的问题是,每次运行时不会生成相同的随机数吗?随机数是基于全局 ID 的吗?我怎样才能每次生成新的随机数。可以提供种子作为内核的参数,然后以某种方式使用它吗?

有人使用过这个库并且可以给我一些关于它的使用的更多见解吗?

最佳答案

是的。示例代码每次调用时都会生成相同的随机数序列。

要获得不同的随机数流,只需以不同的方式初始化 k[1..3] 和/或 c[1..3] 中的任意值即可。您可以从命令行参数、环境变量、时间、保存的状态、/dev/urandom 或任何其他来源初始化它们。请注意:

a) 如果您在两次不同的运行中以完全相同的方式初始化所有这些,那么这两次运行将获得相同的随机数流

b) 如果您在两次不同的运行中以不同的方式初始化它们,那么这两次运行将获得不同的随机数流。

有时你想要属性(property)a)。有时你想要属性(property) b).花点时间想想你想要什么,并确保你正在做你想做的事。

更一般地说,库中的函数(例如 Threefry4x32)无状态。如果更改输入中的任何位(即 c 或 k 的任何元素中的任何位),您将获得完全不同的随机、统计独立、均匀分布的输出。

附注我是该库和论文“Parallel Numbers: As Easy as 1, 2, 3”的作者之一: http://dl.acm.org/citation.cfm?id=2063405

如果您不是 ACM 数字图书馆的订阅者,上面的链接可能会遇到付费墙。或者,您可以通过本页上的链接免费获取该论文:

http://www.thesalmons.org/john/random123/index.html

关于使用 Random123 通过 OpenCL 生成随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11268023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com