zoukankan      html  css  js  c++  java
  • 适用于CUDA GPU的Numba 随机数生成

    适用于CUDA GPU的Numba 随机数生成

    随机数生成

    Numba提供了可以在GPU上执行的随机数生成算法。由于NVIDIA如何实现cuRAND的技术问题,Numba的GPU随机数生成器并非基于cuRAND。相反,Numba的GPU RNG是xoroshiro128 +算法的实现。xoroshiro128 +算法的周期为2**128 - 1,比cuRAND中默认使用的XORWOW算法的周期短,但是xoroshiro128 +算法仍然通过了随机数发生器质量的BigCrush测试。

    在GPU上使用任何RNG时,重要的是要确保每个线程都有其自己的RNG状态,并且它们已初始化为产生不重叠的序列。numba.cuda.random模块提供了执行此操作的主机功能,以及提供统一或正态分布的随机数的CUDA设备功能。

    注意

    Numba (like cuRAND) uses the Box-Muller transform <https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform>从统一生成器生成正态分布的随机数。但是,Box-Muller生成随机数对,当前实现只返回其中之一。结果,生成正态分布的值是均匀分布的值的速度的一半。

    numba.cuda.random.create_xoroshiro128p_states(n, seed, subsequence_start=0, stream=0)

    返回为n个随机数生成器初始化的新设备数组。

    这将初始化RNG状态,以便数组中的每个状态与主序列中彼此分开2 ** 64步的子序列相对应。因此,只要没有CUDA线程请求超过2 ** 64个随机数,就可以保证此函数产生的所有RNG状态都是独立的。

    subsequence_start参数可用于将第一个RNG状态提前2 ** 64步的倍数。

    参数:

    • nint)–要创建的RNG状态数
    • seeduint64)–生成器列表的起始种子
    • subsequence_startuint64)–
    • StreamCUDA流)–在其上运行初始化内核的流

    numba.cuda.random.init_xoroshiro128p_states(states, seed, subsequence_start=0, stream=0)

    在GPU上为并行生成器初始化RNG状态。

    这将初始化RNG状态,以便数组中的每个状态与主序列中彼此分开2 ** 64步的子序列相对应。因此,只要没有CUDA线程请求超过2 ** 64个随机数,就可以保证此函数产生的所有RNG状态都是独立的。

    subsequence_start参数可用于将第一个RNG状态提前2 ** 64步的倍数。

    参数:

    • states (1D DeviceNDArray, dtype=xoroshiro128p_dtype)– RNG状态数组
    • seeduint64)–生成器列表的起始种子

    numba.cuda.random.xoroshiro128p_uniform_float32

    返回范围为[0.0,1.0)的float32并前进states[index]

    参数:

    • states (1D DeviceNDArray, dtype=xoroshiro128p_dtype)– RNG状态数组
    • indexint64)–要更新的状态的偏移量

    返回类型:

    float32

    numba.cuda.random.xoroshiro128p_uniform_float64

    返回范围为[0.0,1.0)的float64并前进states[index]

    参数:

    • 状态states (1D array, dtype=xoroshiro128p_dtype)– RNG状态数组
    • indexint64)–要更新的状态的偏移量

    返回类型:

    float64

    numba.cuda.random.xoroshiro128p_normal_float32

    返回正态分布的float32并前进states[index]

    使用Box-Muller变换从平均值= 0和sigma = 1的高斯中得出返回值。这使RNG序列前进了两个步骤。

    参数:

    • states (1D array, dtype=xoroshiro128p_dtype)– RNG状态数组
    • indexint64)–要更新的状态的偏移量

    返回类型:

    float32

    numba.cuda.random.xoroshiro128p_normal_float64

    返回正态分布的float32并前进states[index]

    使用Box-Muller变换从平均值= 0和sigma = 1的高斯中得出返回值。这使RNG序列前进了两个步骤。

    参数:

    • 状态states (1D array, dtype=xoroshiro128p_dtype)– RNG状态数组
    • indexint64)–要更新的状态的偏移量

    返回类型:

    float64

    这是使用随机数生成器的示例程序:

    from __future__ import print_function, absolute_import

     

    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())

    人工智能芯片与自动驾驶
  • 相关阅读:
    poj1511
    poj2996
    poj1062
    poj3259
    poj2993
    手动上传SNAPSHOT文件到Maven私服Nexus的方法
    安全可靠国产系统下的应用怎么搭建?
    手动上传SNAPSHOT文件到Maven私服Nexus的方法
    Maven 初学+http://mvnrepository.com/
    IDEA中Maven依赖下载失败解决方案
  • 原文地址:https://www.cnblogs.com/wujianming-110117/p/14192840.html
Copyright © 2011-2022 走看看