Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_random_state() is not thread-safe #77

Closed
niboshi opened this issue May 31, 2017 · 0 comments
Closed

get_random_state() is not thread-safe #77

niboshi opened this issue May 31, 2017 · 0 comments
Labels
Milestone

Comments

@niboshi
Copy link
Member

niboshi commented May 31, 2017

In the code below, the seed should be set to 10 no matter what order these threads are executed.

Code:

import threading
import random
import cupy

SEED = 10

def func_seed():
    cupy.random.seed(SEED)

def func_get_random_state():
    cupy.random.get_random_state()

def test():
    procs = [
        threading.Thread(target=func_seed),
        threading.Thread(target=func_get_random_state),
        threading.Thread(target=func_get_random_state),
        threading.Thread(target=func_get_random_state),
        threading.Thread(target=func_get_random_state),
        threading.Thread(target=func_get_random_state),
        threading.Thread(target=func_get_random_state),
    ]
    random.shuffle(procs)

    for p in procs:
        p.start()
        #p.join()  # this hides the problem

    for p in procs:
        p.join()

    actual = cupy.random.uniform()

    cupy.random.seed(SEED)
    expected = cupy.random.uniform()

    print("Expected: {}".format(expected))
    print("Actual  : {}".format(actual))

test()

Result:

Expected: 0.6320792449063122
Actual  : 0.41662013290707867
@niboshi niboshi added the cat:bug Bugs label May 31, 2017
@okuta okuta closed this as completed in #78 Jun 5, 2017
@okuta okuta added this to the v2.0.0a milestone Jul 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants