首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Numba-如何在二维线程的gpu内核函数中创建随机数?

Numba-如何在二维线程的gpu内核函数中创建随机数?

提问于 2019-10-28 22:41:19
回答 0关注 0查看 266

numba官方文档的例子:

代码语言:javascript
复制
from numba import cuda
from numba.cuda.random import create_xoroshiro128p_states, xoroshiro128p_uniform_float32
import numpy as np

@cuda.jit
def compute_pi(rng_states, iterations, out):
    """Find the maximum value in values and store in result[0]"""
    thread_id = cuda.grid(1)

    # Compute pi by drawing random (x, y) points and finding what
    # fraction lie inside a unit circle
    inside = 0
    for i in range(iterations):
        x = xoroshiro128p_uniform_float32(rng_states, thread_id)
        y = xoroshiro128p_uniform_float32(rng_states, thread_id)
        if x**2 + y**2 <= 1.0:
            inside += 1

    out[thread_id] = 4.0 * inside / iterations

threads_per_block = 64
blocks = 24
rng_states = create_xoroshiro128p_states(threads_per_block * blocks, seed=1)
out = np.zeros(threads_per_block * blocks, dtype=np.float32)

compute_pi[blocks, threads_per_block](rng_states, 10000, out)
print('pi:', out.mean())

这个例子中线程分配是1维的,因为看不懂create_xoroshiro128p_states() 和 xoroshiro128p_uniform_float32()两个函数所需参数的具体含义,所以在我想改成cuda.grid(2)情况下的时候遇到了问题,不知道该怎么设置这两个函数的参数,求大神告知。

我最终的目的是生成随机整数,目前还没找到能直接生成整数的方法。

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档