Esempio n. 1
0
def test_randomfunc(brng, meth_args):

    meth, args = meth_args

    mesh = UnitSquareMesh(10, 10)
    V0 = VectorFunctionSpace(mesh, "CG", 1)
    V1 = FunctionSpace(mesh, "CG", 1)
    V = V0 * V1
    seed = 123456789
    # Original
    bgen = getattr(randomgen, brng)(seed=seed)
    if brng == 'PCG64':
        state = bgen.state
        state['state'] = {'state': seed, 'inc': V.comm.Get_rank()}
        bgen.state = state
    rg_base = randomgen.Generator(bgen)
    # Firedrake wrapper
    fgen = getattr(randomfunctiongen, brng)(seed=seed)
    if brng == 'PCG64':
        state = fgen.state
        state['state'] = {'state': seed, 'inc': V.comm.Get_rank()}
        fgen.state = state
    rg_wrap = randomfunctiongen.Generator(fgen)
    for i in range(1, 10):
        f = getattr(rg_wrap, meth)(V, *args)
        with f.dat.vec_ro as v:
            if meth in ('rand', 'randn'):
                assert np.allclose(getattr(rg_base, meth)(v.local_size), v.array[:])
            else:
                kwargs = {'size': (v.local_size, )}
                assert np.allclose(getattr(rg_base, meth)(*args, **kwargs), v.array[:])
Esempio n. 2
0
def test_randomfunc_parallel_philox():
    mesh = UnitSquareMesh(10, 10)
    V0 = VectorFunctionSpace(mesh, "CG", 1)
    V1 = FunctionSpace(mesh, "CG", 1)
    V = V0 * V1

    key_size = 2
    # Original
    key = np.zeros(key_size, dtype=np.uint64)
    key[0] = V.comm.Get_rank()
    rg_base = randomgen.Generator(randomgen.Philox(counter=12345678910, key=key))
    # Firedrake wrapper
    rg_wrap = randomfunctiongen.Generator(randomfunctiongen.Philox(counter=12345678910))
    for i in range(1, 10):
        f = rg_wrap.beta(V, 0.3, 0.5)
        with f.dat.vec_ro as v:
            assert np.allclose(rg_base.beta(0.3, 0.5, size=(v.local_size,)), v.array[:])
Esempio n. 3
0
def test_randomfunc_parallel_pcg64():
    mesh = UnitSquareMesh(10, 10)
    V0 = VectorFunctionSpace(mesh, "CG", 1)
    V1 = FunctionSpace(mesh, "CG", 1)
    V = V0 * V1

    seed = 123456789
    # Original
    bgen = randomgen.PCG64(seed=seed)
    state = bgen.state
    state['state'] = {'state': seed, 'inc': V.comm.Get_rank()}
    bgen.state = state
    rg_base = randomgen.Generator(bgen)
    # Firedrake wrapper
    fgen = randomfunctiongen.PCG64(seed=seed)
    state = fgen.state
    state['state'] = {'state': seed, 'inc': V.comm.Get_rank()}
    fgen.state = state
    rg_wrap = randomfunctiongen.Generator(fgen)
    for i in range(1, 10):
        f = rg_wrap.beta(V, 0.3, 0.5)
        with f.dat.vec_ro as v:
            assert np.allclose(rg_base.beta(0.3, 0.5, size=(v.local_size,)), v.array[:])
Esempio n. 4
0
def test_randomgen_brng(brng, meth_args_kwargs):

    meth, args, kwargs = meth_args_kwargs

    seed = 123456789
    # Original
    bgen = getattr(randomgen, brng)(seed=seed)
    if brng == 'PCG64':
        state = bgen.state
        state['state'] = {'state': seed, 'inc': 0}
        bgen.state = state
    rg_base = randomgen.Generator(bgen)
    # Firedrake wrapper
    fgen = getattr(randomfunctiongen, brng)(seed=seed)
    if brng == 'PCG64':
        state = fgen.state
        state['state'] = {'state': seed, 'inc': 0}
        fgen.state = state
    rg_wrap = randomfunctiongen.Generator(fgen)

    if meth == 'bytes':
        a0 = getattr(rg_base, meth)(17)
        a1 = getattr(rg_wrap, meth)(17)
        assert a0 == a1
        return
    elif meth == 'shuffle':
        arr0 = np.arange(17)
        arr1 = np.arange(17)
        getattr(rg_base, meth)(arr0)
        getattr(rg_wrap, meth)(arr1)
        assert np.allclose(arr0, arr1)
        return

    for i in range(1, 10):
        assert np.allclose(
            getattr(rg_base, meth)(*args, **kwargs),
            getattr(rg_wrap, meth)(*args, **kwargs))
Esempio n. 5
0
def test_randomfunc(brng, meth):

    mesh = UnitSquareMesh(10, 10)
    V0 = VectorFunctionSpace(mesh, "CG", 1)
    V1 = FunctionSpace(mesh, "CG", 1)
    V = V0 * V1

    seed = 123456789
    # Original
    bgen = getattr(randomgen, brng)(seed=seed)
    if brng == 'PCG64':
        state = bgen.state
        state['state'] = {'state': seed, 'inc': V.comm.Get_rank()}
        bgen.state = state
    rg_base = randomgen.Generator(bgen)
    # Firedrake wrapper
    fgen = getattr(randomfunctiongen, brng)(seed=seed)
    if brng == 'PCG64':
        state = fgen.state
        state['state'] = {'state': seed, 'inc': V.comm.Get_rank()}
        fgen.state = state
    rg_wrap = randomfunctiongen.Generator(fgen)

    if meth == 'beta':
        args = (0.3, 0.5)
    elif meth == 'binomial':
        args = (7, 0.3)
    elif meth == 'chisquare':
        args = (7,)
    elif meth == 'choice':
        args = (1234 * 5678,)
    elif meth == 'exponential':
        args = ()
    elif meth == 'f':
        args = (7, 17)
    elif meth == 'gamma':
        args = (3.14,)
    elif meth == 'geometric':
        args = (0.5,)
    elif meth == 'gumbel':
        args = ()
    elif meth == 'hypergeometric':
        args = (17, 2 * 17, 3 * 17 - 1)
    elif meth == 'laplace':
        args = ()
    elif meth == 'logistic':
        args = ()
    elif meth == 'lognormal':
        args = ()
    elif meth == 'logseries':
        args = (0.3,)
    elif meth == 'negative_binomial':
        args = (7, 0.3)
    elif meth == 'noncentral_chisquare':
        args = (7, 3.14)
    elif meth == 'noncentral_f':
        args = (7, 17, 3.14)
    elif meth == 'normal':
        args = ()
    elif meth == 'pareto':
        args = (3.14,)
    elif meth == 'poisson':
        args = ()
    elif meth == 'power':
        args = (3.14,)
    elif meth == 'random':
        args = ()
    elif meth == 'rayleigh':
        args = ()
    elif meth == 'standard_cauchy':
        args = ()
    elif meth == 'standard_exponential':
        args = ()
    elif meth == 'standard_gamma':
        args = (3.14,)
    elif meth == 'standard_normal':
        args = ()
    elif meth == 'standard_t':
        args = (7,)
    elif meth == 'triangular':
        args = (2.71, 3.14, 10.)
    elif meth == 'uniform':
        args = ()
    elif meth == 'vonmises':
        args = (2.71, 3.14)
    elif meth == 'wald':
        args = (2.71, 3.14)
    elif meth == 'weibull':
        args = (3.14,)
    elif meth == 'zipf':
        args = (3.14,)
    else:
        raise RuntimeError("Unknown method: add test for %s." % meth)

    for i in range(1, 10):
        f = getattr(rg_wrap, meth)(V, *args)
        with f.dat.vec_ro as v:
            if meth in ('rand', 'randn'):
                assert np.allclose(getattr(rg_base, meth)(v.local_size), v.array[:])
            else:
                kwargs = {'size': (v.local_size, )}
                assert np.allclose(getattr(rg_base, meth)(*args, **kwargs), v.array[:])
def test_randomgen_brng(brng, meth):

    seed = 123456789
    # Original
    bgen = getattr(randomgen, brng)(seed=seed)
    if brng == 'PCG64':
        state = bgen.state
        state['state'] = {'state': seed, 'inc': 0}
        bgen.state = state
    rg_base = randomgen.Generator(bgen)
    # Firedrake wrapper
    fgen = getattr(randomfunctiongen, brng)(seed=seed)
    if brng == 'PCG64':
        state = fgen.state
        state['state'] = {'state': seed, 'inc': 0}
        fgen.state = state
    rg_wrap = randomfunctiongen.Generator(fgen)

    if meth == 'beta':
        args = (0.3, 0.5)
        kwargs = {'size': 17}
    elif meth == 'binomial':
        args = (7, 0.3)
        kwargs = {'size': 17}
    elif meth == 'bytes':
        a0 = getattr(rg_base, meth)(17 + 1)
        a1 = getattr(rg_wrap, meth)(17 + 1)
        assert a0 == a1
        return
    elif meth == 'chisquare':
        args = (7, )
        kwargs = {'size': 17}
    elif meth == 'choice':
        args = (17, )
        kwargs = {'size': 17}
    elif meth == 'complex_normal':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'dirichlet':
        args = (np.arange(1, 17 + 1), )
        kwargs = {'size': 17}
    elif meth == 'exponential':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'f':
        args = (7, 17)
        kwargs = {'size': 17}
    elif meth == 'gamma':
        args = (3.14, )
        kwargs = {'size': 17}
    elif meth == 'geometric':
        args = (0.5, )
        kwargs = {'size': 17}
    elif meth == 'gumbel':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'hypergeometric':
        args = (17, 2 * 17, 3 * 17 - 1)
        kwargs = {'size': 17}
    elif meth == 'laplace':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'logistic':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'lognormal':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'logseries':
        args = (0.3, )
        kwargs = {'size': 17}
    elif meth == 'multinomial':
        args = (17, [0.1, 0.2, 0.3, 0.4])
        kwargs = {'size': 17}
    elif meth == 'multivariate_hypergeometric':
        args = ([16, 8, 4], 6)
        kwargs = {'size': 3}
    elif meth == 'multivariate_normal':
        args = ([3.14, 2.71], [[1.00, 0.01], [0.01, 2.00]])
        kwargs = {'size': 17}
    elif meth == 'negative_binomial':
        args = (7, 0.3)
        kwargs = {'size': 17}
    elif meth == 'noncentral_chisquare':
        args = (7, 3.14)
        kwargs = {'size': 17}
    elif meth == 'noncentral_f':
        args = (7, 17, 3.14)
        kwargs = {'size': 17}
    elif meth == 'normal':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'pareto':
        args = (3.14, )
        kwargs = {'size': 17}
    elif meth == 'permutation':
        args = (10 * 17, )
        kwargs = {}
    elif meth == 'poisson':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'power':
        args = (3.14, )
        kwargs = {'size': 17}
    elif meth == 'rand':
        args = (17, )
        kwargs = {}
    elif meth == 'random':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'rayleigh':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'shuffle':
        arr0 = np.arange(17 + 10)
        arr1 = np.arange(17 + 10)
        getattr(rg_base, meth)(arr0)
        getattr(rg_wrap, meth)(arr1)
        assert np.allclose(arr0, arr1)
        return
    elif meth == 'standard_cauchy':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'standard_exponential':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'standard_gamma':
        args = (3.14, )
        kwargs = {'size': 17}
    elif meth == 'standard_normal':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'standard_t':
        args = (7, )
        kwargs = {'size': 17}
    elif meth == 'tomaxint':
        args = (7, )
        kwargs = {}
    elif meth == 'triangular':
        args = (2.71, 3.14, 10.)
        kwargs = {'size': 17}
    elif meth == 'uniform':
        args = ()
        kwargs = {'size': 17}
    elif meth == 'vonmises':
        args = (2.71, 3.14)
        kwargs = {'size': 17}
    elif meth == 'wald':
        args = (2.71, 3.14)
        kwargs = {'size': 17}
    elif meth == 'weibull':
        args = (3.14, )
        kwargs = {'size': 17}
    elif meth == 'zipf':
        args = (3.14, )
        kwargs = {'size': 17}
    else:
        raise RuntimeError("Unknown method: add test for %s." % meth)

    for i in range(1, 10):
        assert np.allclose(
            getattr(rg_base, meth)(*args, **kwargs),
            getattr(rg_wrap, meth)(*args, **kwargs))